1 | #! /bin/ksh |
---|
2 | # |
---|
3 | # Usage : Create_dependencies.pl 'modelname' 'source code directory' |
---|
4 | # |
---|
5 | # Limits: the name of the file which contains a module |
---|
6 | # and the module name must be the same. |
---|
7 | # |
---|
8 | # Functionality: |
---|
9 | # |
---|
10 | # The script.. |
---|
11 | # |
---|
12 | # 1.) checks all files in 'source code directory' ('scd') for |
---|
13 | # inclusion of header files (#include or INCLUDE) |
---|
14 | # or usage of Fortran modules. |
---|
15 | # 2.) It outputs for each file in 'scd' a dependency record with prerequisites |
---|
16 | # ('header file name' or 'module name'.o) for use in the Makefile of 'scd' |
---|
17 | # if any dependency was found. |
---|
18 | # 3.) It checks for all prerequisites whether the file (for header files) or |
---|
19 | # a 'module name'.[f,F][90] resides in 'scd' |
---|
20 | # 4.) Those which do not reside in 'scd' are searched in all other |
---|
21 | # source code directories at the same directory tree level. |
---|
22 | # If directories with platform dependent code exist, only the relevant |
---|
23 | # directories are checked. |
---|
24 | # 5.) If not found in any of the other source code directories, |
---|
25 | # the file is listed (e.g. psmile routines, platform dependent |
---|
26 | # include (header) files |
---|
27 | # 6.) If found, a 2nd level dependency check is done: |
---|
28 | # 7.) The 2nd level dependency check includes steps |
---|
29 | # 2.) for the prerequisites already found |
---|
30 | # 3.) checks for all prerequisites whether there reside in the same dir |
---|
31 | # 4.) Those which do not reside in 'scd' are searched in all other |
---|
32 | # source code directories at the same directory tree level. |
---|
33 | # 5.) If not found in any of the other source code directories, |
---|
34 | # the file is listed |
---|
35 | # 6.) If found new prerequisites are added |
---|
36 | # 8.) For some model another iteration is added |
---|
37 | # 9.) Finally, |
---|
38 | # - directories other than 'scd' are listed for inclusion in the VPATH |
---|
39 | # parameter of Makefile_1 if needed |
---|
40 | # - a list with unresolved dependencies is given. |
---|
41 | # |
---|
42 | # Unresolved dependencies may be resolved manually |
---|
43 | # (e.g. prism_kind_proto in the component models) and |
---|
44 | # listed in Makefile_1. They will not be lost with the new |
---|
45 | # creation of 'Makefile' since the dependency are appended. |
---|
46 | # |
---|
47 | |
---|
48 | # |
---|
49 | # Two command line parameters are mandatory |
---|
50 | # |
---|
51 | |
---|
52 | if [ "$2" = "" ] ; then |
---|
53 | echo '***************************************************************' |
---|
54 | echo '* ' |
---|
55 | echo '* This script must be called with 2 parameters:' |
---|
56 | echo '* 1. model name (directory name of the model source code)' |
---|
57 | echo '* 2. source code subdirectory for which to create the Makefile ' |
---|
58 | echo '* One or both of the parameters is empty!' |
---|
59 | echo '* The script is stopped!' |
---|
60 | echo '* ' |
---|
61 | echo '* A 3rd parameter (nodename for a valid PRISM site) is optional.' |
---|
62 | echo '* ' |
---|
63 | echo '***************************************************************' |
---|
64 | exit 1 |
---|
65 | fi |
---|
66 | |
---|
67 | # |
---|
68 | # Set 3rd parameter (processor type) for MOZART |
---|
69 | # |
---|
70 | if [ "$1" = "mozart" ]; then |
---|
71 | node=`uname -n` |
---|
72 | if [ ! "$3" = "" ]; then |
---|
73 | node=$3 |
---|
74 | fi |
---|
75 | node12=`echo $node | cut -c1-2` |
---|
76 | node13=`echo $node | cut -c1-3` |
---|
77 | node14=`echo $node | cut -c1-4` |
---|
78 | if [ "$node12" = "ds" ]; then |
---|
79 | proctype=vector |
---|
80 | elif [ "$node" = "dcm13" ]; then |
---|
81 | proctype=cache |
---|
82 | elif [ "$node" = "dcm23" ]; then |
---|
83 | proctype=cache |
---|
84 | elif [ "$node" = "elnino" ]; then |
---|
85 | proctype=cache |
---|
86 | elif [ "$node14" = "hpca" ]; then |
---|
87 | proctype=cache |
---|
88 | elif [ "$node12" = "p1" ]; then |
---|
89 | proctype=cache |
---|
90 | elif [ "$node14" = "xbar" ]; then |
---|
91 | proctype=vector |
---|
92 | elif [ "$node" = "SFV800" ]; then |
---|
93 | proctype=vector |
---|
94 | else |
---|
95 | echo Unknown site! |
---|
96 | echo The script is stopped! |
---|
97 | fi |
---|
98 | fi |
---|
99 | |
---|
100 | scriptname=`basename $0` |
---|
101 | scriptdirec=`dirname $0` |
---|
102 | perlname=`which perl | grep /perl` |
---|
103 | export perlname |
---|
104 | |
---|
105 | cat > ${scriptname}.pl << EOF |
---|
106 | #!$perlname |
---|
107 | EOF |
---|
108 | cat >> ${scriptname}.pl << \EOF |
---|
109 | # |
---|
110 | # Based on createMakefiles.pl by |
---|
111 | # Uwe Schulzweida <schulzweida@dkrz.de> June, 1999 |
---|
112 | # Stephanie Legutke, MPI M&D Oct 2003 |
---|
113 | # - loop on all source code directories for prerequisites |
---|
114 | # at the 2nd level |
---|
115 | # Stephanie Legutke, MPI M&D June 2004 |
---|
116 | # - '# include' (with blanks allowed) |
---|
117 | # - 3rd level check added June 2004 |
---|
118 | # |
---|
119 | #################################################################### |
---|
120 | |
---|
121 | $PROG=`basename $0`; |
---|
122 | |
---|
123 | $scriptdir=@ARGV[0]; |
---|
124 | $modname=@ARGV[1]; |
---|
125 | $dir=@ARGV[2]; |
---|
126 | $proctype=@ARGV[3]; |
---|
127 | |
---|
128 | # |
---|
129 | # Print directories |
---|
130 | # |
---|
131 | |
---|
132 | $presdir=`pwd`; |
---|
133 | chdir $presdir; |
---|
134 | chdir $scriptdir; |
---|
135 | $scriptdir=`pwd`; |
---|
136 | chdir "../../src/mod/"; |
---|
137 | $srcdir=`pwd`; |
---|
138 | |
---|
139 | # |
---|
140 | # Check whether valid model |
---|
141 | # |
---|
142 | if ( ! -d $modname ) { |
---|
143 | print "******************************************************** \n"; |
---|
144 | print "*\n"; |
---|
145 | print "* This script must be called with 2 parameters:\n"; |
---|
146 | print "* 1. model name (directory name of the model source code \n"; |
---|
147 | print "* (all lower case))\n"; |
---|
148 | print "* 2. source code subdirectory (for which to create make \n"; |
---|
149 | print "* dependencies) \n"; |
---|
150 | print "* The first parameter is not a valid model name:\n"; |
---|
151 | print "* model name = $modname\n"; |
---|
152 | |
---|
153 | # Create list of models. |
---|
154 | |
---|
155 | opendir (MODDIR, "."); |
---|
156 | foreach $file (readdir(MODDIR)) { |
---|
157 | if ( -d $file ) { |
---|
158 | ($nfile = $file) =~ (s/^\.//) ; |
---|
159 | if ( ("$file" eq "$nfile") ) { |
---|
160 | push (@modlist, $file); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | close (MODDIR); |
---|
165 | print "* Valid models are : @{modlist}\n"; |
---|
166 | print "*\n"; |
---|
167 | print "******************************************************** \n"; |
---|
168 | exit; |
---|
169 | } |
---|
170 | |
---|
171 | chop(${srcdir}); |
---|
172 | $moddir="${srcdir}/".${modname}; |
---|
173 | print "********************************************************\n"; |
---|
174 | print "* Source root directory : $srcdir\n"; |
---|
175 | print "* Model name : $modname\n"; |
---|
176 | |
---|
177 | chdir $modname; |
---|
178 | |
---|
179 | # |
---|
180 | # Processor type dependent strings for selection of directories |
---|
181 | # |
---|
182 | $string1=" "; |
---|
183 | $string2=" "; |
---|
184 | if ( "$modname" eq "mozart" ) { |
---|
185 | if ( "$proctype" eq "vector" ) { |
---|
186 | $string1="cache"; |
---|
187 | $string2="scalar"; |
---|
188 | print "* Makefile dependencies will be created for a vector processor \n"; |
---|
189 | print "* maschine from the vector-directories.\n"; |
---|
190 | |
---|
191 | } |
---|
192 | if ( "$proctype" eq "cache" ) { |
---|
193 | $string1="vector"; |
---|
194 | $string2="scalar"; |
---|
195 | print "* Makefile dependencies will be created for a cache processor \n"; |
---|
196 | print "* maschine from the cache-directories.\n"; |
---|
197 | } |
---|
198 | if ( "$proctype" eq "scalar" ) { |
---|
199 | $string1="cache"; |
---|
200 | $string2="scalar"; |
---|
201 | print "* Makefile dependencies will be created for a scalar processor \n"; |
---|
202 | print "* maschine from the scalar-directories.\n" |
---|
203 | } |
---|
204 | } |
---|
205 | # |
---|
206 | # List of source code directories |
---|
207 | # (For compilation of mozart exclude |
---|
208 | # the $string1 and $string2 directories.) |
---|
209 | # |
---|
210 | undef @dirlist; |
---|
211 | opendir (MODDIR, "."); |
---|
212 | foreach $file (readdir(MODDIR)) { |
---|
213 | if ( -d $file ) { |
---|
214 | ($nfile = $file) =~ (s/^\.//) ; |
---|
215 | ($n1file = $nfile ) =~ (s/$string1//) ; |
---|
216 | ($n2file = $n1file ) =~ (s/$string2//) ; |
---|
217 | if ( ( "$file" ne "doc" ) && ("$file" eq "$n2file") ) { |
---|
218 | push (@dirlist, $file); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | close (MODDIR); |
---|
224 | |
---|
225 | # if ( "$modname" eq "mozart" ) { |
---|
226 | if ( ( $dir eq "" ) || ( ! -d $dir )) { |
---|
227 | print "******************************************************** \n"; |
---|
228 | print "*\n"; |
---|
229 | print "* This script must be called with 2 parameters:\n"; |
---|
230 | print "* 2. source code subdirectory \n"; |
---|
231 | print "* (for which to create Makefile dependencies) \n"; |
---|
232 | print "* The specified vakue is not a valid directory name.\n"; |
---|
233 | print "* Valid directories are : @{dirlist}\n"; |
---|
234 | print "*\n"; |
---|
235 | print "******************************************************** \n"; |
---|
236 | exit; |
---|
237 | } |
---|
238 | #} |
---|
239 | |
---|
240 | print "* Create dependencies for Makefile in source code directory : $dir\n"; |
---|
241 | |
---|
242 | # |
---|
243 | # Work on directory @ARGV[1]; can be changed in a loop on @dirlist later |
---|
244 | # |
---|
245 | |
---|
246 | chdir $dir; |
---|
247 | |
---|
248 | # |
---|
249 | # Check for Makefile_1 |
---|
250 | # |
---|
251 | |
---|
252 | $file="Makefile_1"; |
---|
253 | if ( ! (-f $file) ) { |
---|
254 | print "* No Makefile_1 in directory $dir !!\n"; |
---|
255 | exit; |
---|
256 | } |
---|
257 | |
---|
258 | # |
---|
259 | # Remove old Makefile |
---|
260 | # |
---|
261 | |
---|
262 | $file="Makefile"; |
---|
263 | if ( -f $file ) { |
---|
264 | unlink ($file); |
---|
265 | } |
---|
266 | |
---|
267 | # |
---|
268 | # Open new Makefile |
---|
269 | # |
---|
270 | |
---|
271 | open(DEPENDS, ">> Makefile"); |
---|
272 | print DEPENDS "\n"; |
---|
273 | print DEPENDS "# This file is generated automatically\n"; |
---|
274 | |
---|
275 | # |
---|
276 | # Copy body of Makefile |
---|
277 | # |
---|
278 | |
---|
279 | open(MAKEBODY, "Makefile_1"); |
---|
280 | while ( ( $line = <MAKEBODY>) && ( "$line" ne "# Dont add anything behind this line\n" ) ) { |
---|
281 | print DEPENDS "$line"; |
---|
282 | } |
---|
283 | print DEPENDS "\n"; |
---|
284 | close(MAKEBODY); |
---|
285 | |
---|
286 | |
---|
287 | # |
---|
288 | # Create dependency listings (dirlist=all directories) |
---|
289 | # |
---|
290 | |
---|
291 | &MakeDepends(@{dirlist}); |
---|
292 | chdir ".."; |
---|
293 | chdir @ARGV[2]; |
---|
294 | |
---|
295 | # |
---|
296 | # Add more iteration |
---|
297 | # |
---|
298 | |
---|
299 | $times=0; |
---|
300 | |
---|
301 | if ( @ARGV[1] eq "mozart" && @ARGV[2] eq "base" ) { $times=2; } |
---|
302 | if ( @ARGV[1] eq "mozart" && @ARGV[2] eq "base_vector") { $times=2; } |
---|
303 | if ( @ARGV[1] eq "mozart" && @ARGV[2] eq "chem" ) { $times=1; } |
---|
304 | if ( @ARGV[1] eq "mozart" && @ARGV[2] eq "chem_vector") { $times=1; } |
---|
305 | |
---|
306 | foreach $time ( 1 .. $times ) { |
---|
307 | print "* Add Second Iteration\n"; |
---|
308 | &AddDepends(@{Dirlist}); |
---|
309 | chdir ".."; |
---|
310 | chdir @ARGV[2]; |
---|
311 | rename(Makefile_new,Makefile); |
---|
312 | } |
---|
313 | |
---|
314 | exit; |
---|
315 | |
---|
316 | ############################################################################ |
---|
317 | # |
---|
318 | # Usage &MakeDepends(@dirlist); |
---|
319 | # @_ = @dirlist = $directories |
---|
320 | # @directories: list of all model directories (excl. 'doc') |
---|
321 | # $presdir: present directory |
---|
322 | # @dirs: list of all model directories (excl. 'doc' and present) |
---|
323 | # %Filename: key=module name of Fortran files in present directory |
---|
324 | # value= file name (no .h file) |
---|
325 | # %dotOname: key=module name of Fortran files in present directory |
---|
326 | # value= $Filename.o (no .h file) |
---|
327 | # for each $file in present directory: @modules list of modules used |
---|
328 | # @incs list of files included |
---|
329 | # for each $file in present directory: $objfile=$Filename.o (.h unchanged) |
---|
330 | # |
---|
331 | # |
---|
332 | sub MakeDepends { |
---|
333 | local(@dependencies); |
---|
334 | local(%dotOname); |
---|
335 | local(%Filename); |
---|
336 | local(@incs); |
---|
337 | local(@modules); |
---|
338 | local(@directories); |
---|
339 | local($objfile); |
---|
340 | |
---|
341 | @directories = @_ ; |
---|
342 | # |
---|
343 | # Eliminate present directory $presdir from directory list |
---|
344 | # |
---|
345 | $presdir=`pwd`; |
---|
346 | $presdir=`basename $presdir`; |
---|
347 | chop($presdir); |
---|
348 | foreach $dir (@directories) { |
---|
349 | if ( $dir ne $presdir ) { |
---|
350 | push(@dirs, $dir); |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | # |
---|
355 | # Associate each module with the name of the file (%Filename) it is contained in. |
---|
356 | # |
---|
357 | foreach $file (<*.F90 *.f90 *.F *.f>) { |
---|
358 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
359 | while (<FILE>) { |
---|
360 | /^\s*module\s+([^\s!]+)/i && |
---|
361 | ($Filename{&toLower($1)} = $file) ; |
---|
362 | } |
---|
363 | close (FILE); |
---|
364 | } |
---|
365 | |
---|
366 | # |
---|
367 | # Replace Fortran suffixes with '.o' in (%dotOname) |
---|
368 | # |
---|
369 | while (($key,$vari) = each(%Filename)) { |
---|
370 | ($dotOname{$key}=$Filename{$key}) =~ s/\.[f,F].*$/.o/i; |
---|
371 | } |
---|
372 | |
---|
373 | # |
---|
374 | # Note: according to PRISM coding rules filename must be module name |
---|
375 | # |
---|
376 | while (($key,$vari) = each(%Filename)) { |
---|
377 | ($testname=$Filename{$key}) =~ s/\.[f,F].*$//i; |
---|
378 | if (&toLower($testname) ne $key && $key ne "procedure") { |
---|
379 | print " File/Module name: ${Filename{$key}} , $key\n"; |
---|
380 | print " This is not allowed! \n"; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | # |
---|
385 | # Loop on files in present directory for dependency search |
---|
386 | # |
---|
387 | foreach $file (<*.F90 *.f90 *.F *.f *.c *.h>) { |
---|
388 | |
---|
389 | # |
---|
390 | # Get object file name (Fortran and c, .h file names remain unchanged) |
---|
391 | # references one or more modules |
---|
392 | # |
---|
393 | ($objfile = $file) =~ s/\.[f,F].*$/.o/i; |
---|
394 | ($objfile = $objfile) =~ s/\.c$/.o/; |
---|
395 | open(FILE, $file); |
---|
396 | while (<FILE>) { |
---|
397 | /^\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
398 | /^\s*#\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
399 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
400 | } |
---|
401 | |
---|
402 | if (defined @incs && $#incs < 0) {undef @incs;} |
---|
403 | |
---|
404 | undef @redmodules; |
---|
405 | if (defined @modules) { |
---|
406 | foreach $mod (@modules) { |
---|
407 | if ("&toLower($objfile)" ne "&toLower($dotOname{$mod})") { |
---|
408 | push(@redmodules,$mod); |
---|
409 | } else { |
---|
410 | print "* => reflective use: $objfile: $mod\n"; |
---|
411 | } |
---|
412 | } |
---|
413 | } |
---|
414 | @modules=@redmodules; |
---|
415 | # |
---|
416 | # Add .o- name for modules not in present directories (needs refinement) |
---|
417 | # |
---|
418 | |
---|
419 | foreach $mod (@modules) { |
---|
420 | if ( ${dotOname{$mod}} eq "" ) { |
---|
421 | ${dotOname{$mod}}=$mod."\.o"; |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | if (defined @modules && $#modules < 0) {undef @modules;} |
---|
426 | # |
---|
427 | # If dependecies or includes are present ... |
---|
428 | # |
---|
429 | if (defined @incs || defined @modules) { |
---|
430 | # |
---|
431 | # ...but not in present directory: list file name @otherincs/@othermods |
---|
432 | # |
---|
433 | foreach $inc (@incs) { |
---|
434 | open(INC, $inc) || push(@otherincs, $inc); |
---|
435 | } |
---|
436 | foreach $mod (@modules) { |
---|
437 | open(MOD, $Filename{$mod}) || push(@othermods, $mod); |
---|
438 | } |
---|
439 | # |
---|
440 | # ... add line do Makefile (present dir only) |
---|
441 | # |
---|
442 | # |
---|
443 | # Define dotOname (@modules)??? |
---|
444 | # |
---|
445 | print DEPENDS "$objfile: "; |
---|
446 | undef @dependencies; |
---|
447 | foreach $module (@modules) { |
---|
448 | push(@dependencies, $dotOname{$module}); |
---|
449 | } |
---|
450 | @dependencies = &uniq(sort(@dependencies)); |
---|
451 | &PrintWords(length($objfile) + 2, 0, |
---|
452 | @dependencies, &uniq(sort(@incs))); |
---|
453 | print DEPENDS "\n"; |
---|
454 | undef @incs; |
---|
455 | undef @modules; |
---|
456 | } |
---|
457 | } |
---|
458 | # |
---|
459 | # End of loop on files in present directory for dependency search |
---|
460 | # |
---|
461 | |
---|
462 | @remainincs = &uniq(sort(@otherincs)); |
---|
463 | @remainmods = &uniq(sort(@othermods)); |
---|
464 | if ( defined @otherincs ) { |
---|
465 | print "* \n"; |
---|
466 | $no_remincs=@remainincs; |
---|
467 | $ele_1=0; |
---|
468 | $ele_2=0; |
---|
469 | print "* Included files not in present directory:\n"; |
---|
470 | until ( $ele_2 > $no_remincs ) { |
---|
471 | $ele_2=$ele_1 + 3; |
---|
472 | print " @remainincs[$ele_1 .. $ele_2]\n"; |
---|
473 | $ele_1=$ele_2 + 1; |
---|
474 | } |
---|
475 | } |
---|
476 | if ( defined @othermods ) { |
---|
477 | print "* \n"; |
---|
478 | $no_remmods=@remainmods; |
---|
479 | $ele_1=0; |
---|
480 | $ele_2=0; |
---|
481 | print "* Used modules not in present directory: \n"; |
---|
482 | until ( $ele_2 > $no_remmods ) { |
---|
483 | $ele_2=$ele_1 + 3; |
---|
484 | print " @remainmods[$ele_1 .. $ele_2]\n"; |
---|
485 | $ele_1=$ele_2 + 1; |
---|
486 | } |
---|
487 | } |
---|
488 | if ( defined @othermods || defined @otherincs ) { |
---|
489 | print "* \n"; |
---|
490 | print "* They are searched in the other directories,\n"; |
---|
491 | print "* i.e. in \"@dirs\"\n"; |
---|
492 | print "* \n"; |
---|
493 | print "* Warning: In this 2nd iteration it will not be checked whether \n"; |
---|
494 | print "* the files included or the modules used in these files \n"; |
---|
495 | print "* exist in the present or in any of the model directories.\n"; |
---|
496 | print "* To do this another iteration (not a rerun) would be needed!\n"; |
---|
497 | } |
---|
498 | # |
---|
499 | # Loop on other directories for file search |
---|
500 | # |
---|
501 | |
---|
502 | # |
---|
503 | # Do the included files first... |
---|
504 | # |
---|
505 | foreach $dir (@dirs) { |
---|
506 | chdir ".." ; |
---|
507 | chdir $dir; |
---|
508 | undef @restincs; |
---|
509 | foreach $file (@remainincs) { |
---|
510 | open(FILE, $file) || push(@restincs, $file); |
---|
511 | undef @incs; |
---|
512 | undef @modules; |
---|
513 | while (<FILE>) { |
---|
514 | /^\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
515 | /^\s*#\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
516 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
517 | push(@vpathdirs,$dir); |
---|
518 | } |
---|
519 | $objfile = $file; |
---|
520 | if (defined @incs && $#incs < 0) {undef @incs;} |
---|
521 | if (defined @modules && $#modules < 0) {undef @modules;} |
---|
522 | foreach $mod (@modules) { |
---|
523 | if ( ${Filename{$mod}} eq "" ) { |
---|
524 | ${dotOname{$mod}}=$mod."\.o"; |
---|
525 | } |
---|
526 | } |
---|
527 | if (defined @incs || defined @modules) { |
---|
528 | print DEPENDS "$objfile: "; |
---|
529 | undef @dependencies; |
---|
530 | foreach $mod (@modules) { |
---|
531 | push(@dependencies, $dotOname{$mod}); |
---|
532 | } |
---|
533 | @dependencies = &uniq(sort(@dependencies)); |
---|
534 | &PrintWords(length($objfile) + 2, 0, |
---|
535 | @dependencies, &uniq(sort(@incs))); |
---|
536 | print DEPENDS "\n"; |
---|
537 | undef @incs; |
---|
538 | undef @modules; |
---|
539 | } |
---|
540 | } |
---|
541 | @remainincs = &uniq(sort(@restincs)); |
---|
542 | } |
---|
543 | |
---|
544 | @vpathdirs = &uniq(sort(@vpathdirs)); |
---|
545 | if ( $#vpathdirs >= 0) { |
---|
546 | print "* Header file directories to be included in VPATH: \"@vpathdirs\"\n"; |
---|
547 | } |
---|
548 | # |
---|
549 | # Do the uses then ... |
---|
550 | # |
---|
551 | undef @vpathdirs; |
---|
552 | foreach $dir (@dirs) { |
---|
553 | undef @restmods; |
---|
554 | chdir ".." ; |
---|
555 | chdir $dir; |
---|
556 | $vpath="no"; |
---|
557 | foreach $Mod (@remainmods) { |
---|
558 | ${Filename{$Mod}}=""; |
---|
559 | foreach $file (<*.F90 *.f90 *.F *.f>) { |
---|
560 | ($File = $file) =~ s/\.[f,F].*$// ; |
---|
561 | if (&toLower($File) eq $Mod) { |
---|
562 | ${Filename{$Mod}}=$file; |
---|
563 | } |
---|
564 | } |
---|
565 | if (${Filename{$Mod}} eq "") { |
---|
566 | push(@restmods, $Mod); |
---|
567 | } |
---|
568 | else { |
---|
569 | $vpath="yes"; |
---|
570 | open(FILE, ${Filename{$Mod}}); |
---|
571 | undef @incs; |
---|
572 | undef @modules; |
---|
573 | while (<FILE>) { |
---|
574 | /^\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
575 | /^\s*#\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
576 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
577 | } |
---|
578 | $objfile = $Mod."\.o"; |
---|
579 | if (defined @incs && $#incs < 0) {undef @incs;} |
---|
580 | if (defined @modules && $#modules < 0) {undef @modules;} |
---|
581 | if (defined @incs || defined @modules) { |
---|
582 | print DEPENDS "$objfile: "; |
---|
583 | undef @dependencies; |
---|
584 | foreach $mod (@modules) { |
---|
585 | push(@dependencies, $mod."\.o"); |
---|
586 | } |
---|
587 | |
---|
588 | @dependencies = &uniq(sort(@dependencies)); |
---|
589 | &PrintWords(length($objfile) + 2, 0, |
---|
590 | @dependencies, &uniq(sort(@incs))); |
---|
591 | print DEPENDS "\n"; |
---|
592 | undef @incs; |
---|
593 | undef @modules; |
---|
594 | } |
---|
595 | } |
---|
596 | } |
---|
597 | @remainmods = &uniq(sort(@restmods)); |
---|
598 | if ( $vpath eq "yes" ) { |
---|
599 | push(@vpathdirs,$dir); |
---|
600 | } |
---|
601 | } |
---|
602 | if ( $#vpathdirs >= 0) { |
---|
603 | print "*\n"; |
---|
604 | print "* Directories to be included in VPATH: \"@vpathdirs\"\n"; |
---|
605 | } |
---|
606 | if ( $#remainincs >= 0) { |
---|
607 | print "*\n"; |
---|
608 | print "* Included files not found at all: \"@remainincs\"\n"; |
---|
609 | } |
---|
610 | if ( $#remainmods >= 0) { |
---|
611 | print "*\n"; |
---|
612 | print "* Used modules not found at all: \"@remainmods\"\n"; |
---|
613 | } |
---|
614 | print "*\n"; |
---|
615 | print "* Note: The *prism*/*clim* modules reside in the libraries \n"; |
---|
616 | print "* \"psmile\"/\"clim\". They are always compiled before \n"; |
---|
617 | print "* the model and therefore must be included in the INCL\n"; |
---|
618 | print "* variable only.\n"; |
---|
619 | print "*************************************************************\n"; |
---|
620 | } |
---|
621 | |
---|
622 | ############################################################################ |
---|
623 | # |
---|
624 | # Usage &AddDepends(@dirlist); |
---|
625 | # @_ = @dirlist = $directories |
---|
626 | # @directories: list of all model directories (excl. 'doc') |
---|
627 | # $presdir: present directory |
---|
628 | # @dirs: list of all model directories (excl. 'doc' and present) |
---|
629 | # @modules: list of modules used |
---|
630 | # @incs: list of files included |
---|
631 | # |
---|
632 | # Scan the already existing dependency-lines of Makefile and check |
---|
633 | # each .o prerequisite whether it implies |
---|
634 | # - new prerequisites and/or |
---|
635 | # - new dependency lines |
---|
636 | # |
---|
637 | # |
---|
638 | # |
---|
639 | # |
---|
640 | sub AddDepends { |
---|
641 | local(@dependencies); |
---|
642 | local(%dotOname); |
---|
643 | local(%Filename); |
---|
644 | local(@incs); |
---|
645 | local(@modules); |
---|
646 | local(@directories); |
---|
647 | local($objfile); |
---|
648 | |
---|
649 | @directories = @_ ; |
---|
650 | |
---|
651 | # |
---|
652 | # Scan the existing dependencies (line by line) and add prerequisites |
---|
653 | # |
---|
654 | close (DEPENDS); |
---|
655 | open(DEPENDS, "> Makefile_new"); |
---|
656 | |
---|
657 | # |
---|
658 | # Copy body of Makefile |
---|
659 | # |
---|
660 | $line=""; |
---|
661 | open(MAKEBODY, "Makefile"); |
---|
662 | while ( ( $line = <MAKEBODY>) && ( "$line" ne "# Dont add anything behind this line\n" ) ) { |
---|
663 | print DEPENDS "$line"; |
---|
664 | } |
---|
665 | print DEPENDS "\n"; |
---|
666 | |
---|
667 | # |
---|
668 | # Loop over all dependency lines ... |
---|
669 | # |
---|
670 | undef $fullline; |
---|
671 | undef @alltargets; |
---|
672 | undef $allprereqs; |
---|
673 | while ( $line = <MAKEBODY> ) { |
---|
674 | # |
---|
675 | # Read full continued line ... |
---|
676 | # |
---|
677 | ( $nline = $line ) =~ ( s/\\// ) ; |
---|
678 | if ( ! ($nline eq $line) ) { |
---|
679 | chop ($nline); |
---|
680 | $fullline="$fullline".$nline; |
---|
681 | } |
---|
682 | if ( $nline eq $line ) { |
---|
683 | chop ($nline); |
---|
684 | $fullline="$fullline".$nline; |
---|
685 | # |
---|
686 | # Work on full line ... |
---|
687 | |
---|
688 | ( $target = $fullline ) =~ ( s/:.*$// ); |
---|
689 | push (@alltargets,$target); |
---|
690 | |
---|
691 | ( $reqline = $fullline ) =~ ( s/.*:// ); |
---|
692 | ( $reqs = $reqline ) =~ (s/\.h//g); |
---|
693 | ( $reqs = $reqline ) =~ (s/\.o//g); |
---|
694 | @prereqs = split(' ',$reqs); |
---|
695 | |
---|
696 | undef $fullline; |
---|
697 | |
---|
698 | # |
---|
699 | # Loop over prerequisites and extend with new prerequisites |
---|
700 | |
---|
701 | undef @incs; |
---|
702 | undef @modules; |
---|
703 | # |
---|
704 | # Loop over other directories other than present ... |
---|
705 | |
---|
706 | $scalar=@prereqs.""; |
---|
707 | $remains=join(" ",@prereqs); |
---|
708 | until ( $scalar eq 0 ) { |
---|
709 | $new_name=shift(@prereqs); |
---|
710 | $scalar=@prereqs.""; |
---|
711 | foreach $dir (@directories) { |
---|
712 | chdir ".." ; |
---|
713 | chdir $dir; |
---|
714 | foreach $file (<*.F90 *.f90 *.F *.f *.c *.h>) { |
---|
715 | ( $file_basename = $file ) =~ s/\.[f,F,c,h].*$//i; |
---|
716 | if ( $new_name eq $file_basename ) { |
---|
717 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
718 | while (<FILE>) { |
---|
719 | /^\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
720 | /^\s*#\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
721 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
722 | } |
---|
723 | } |
---|
724 | } |
---|
725 | } |
---|
726 | } |
---|
727 | # |
---|
728 | # Append to prerequisites list |
---|
729 | |
---|
730 | @reqlist=split(" ",$reqline); |
---|
731 | foreach $newmod (@modules) { |
---|
732 | push(@reqlist, $newmod."\.o"); |
---|
733 | } |
---|
734 | foreach $newinc (@incs) { |
---|
735 | push(@reqlist, $newinc); |
---|
736 | } |
---|
737 | # |
---|
738 | # Sorting and elimination of doubly defined prerequisites |
---|
739 | |
---|
740 | @reqlist = &uniq(sort(@reqlist)); |
---|
741 | # |
---|
742 | # Write rule to Makefile_new |
---|
743 | |
---|
744 | $rule=join(" ",@reqlist); |
---|
745 | $allprereqs = $allprereqs.$rule." "; |
---|
746 | $rule=$target.": ".$rule; |
---|
747 | $target=$target.": "; |
---|
748 | print DEPENDS "$target"; |
---|
749 | undef @incs; |
---|
750 | &PrintWords(length($target) + 2, 0, @reqlist, @incs ); |
---|
751 | print DEPENDS "\n"; |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | # |
---|
756 | # Check whether new targets popped up ... |
---|
757 | |
---|
758 | @alltargets = &uniq(sort(@alltargets)); |
---|
759 | |
---|
760 | @allprereqs = split (" ",$allprereqs); |
---|
761 | @allprereqs = &uniq(sort(@allprereqs)); |
---|
762 | |
---|
763 | # |
---|
764 | # Subtract targets from list of all prerequisites |
---|
765 | |
---|
766 | $count=@alltargets; |
---|
767 | until ( ( $count == 0 ) || ( $#allprereqs == 0 ) ){ |
---|
768 | |
---|
769 | if ($allprereqs[0] eq $alltargets[0] ) { |
---|
770 | $junk=shift (@alltargets); |
---|
771 | $junk=shift (@allprereqs); |
---|
772 | } |
---|
773 | |
---|
774 | if ($allprereqs[0] gt $alltargets[0] ) { |
---|
775 | $junk=shift (@alltargets); |
---|
776 | } |
---|
777 | |
---|
778 | if ($allprereqs[0] lt $alltargets[0] ) { |
---|
779 | push(@prereqs_left,$allprereqs[0]); |
---|
780 | $junk=shift (@allprereqs); |
---|
781 | } |
---|
782 | $count=@alltargets; |
---|
783 | |
---|
784 | } |
---|
785 | |
---|
786 | # |
---|
787 | # If new targets popped up ... |
---|
788 | |
---|
789 | $scalar=@prereqs_left.""; #26 |
---|
790 | $remains=join(" ",@prereqs_left); |
---|
791 | # print "* Having $scalar new targets : $remains \n"; |
---|
792 | until ( $scalar eq 0 ) { |
---|
793 | $target=shift(@prereqs_left); |
---|
794 | # print "* Target= $target\n"; |
---|
795 | $scalar=@prereqs_left.""; |
---|
796 | undef @modules; |
---|
797 | undef @incs; |
---|
798 | foreach $dir (@dirs) { |
---|
799 | chdir ".." ; |
---|
800 | chdir $dir; |
---|
801 | foreach $file (<*.F90 *.f90 *.F *.f *.c *.h>) { |
---|
802 | ( $file_basename = $file ) =~ s/\.[f,F,c,h].*$//; |
---|
803 | ( $new_name = $target ) =~ s/\.[o,h]$//; |
---|
804 | if ( $new_name eq $file_basename ) { |
---|
805 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
806 | while (<FILE>) { |
---|
807 | /^\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
808 | /^\s*#\s*include\s*["\']([^"\']+)["\']/i && push(@incs, $1.".h"); |
---|
809 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1.".o")); |
---|
810 | } |
---|
811 | } |
---|
812 | } |
---|
813 | if ( defined @incs || defined @modules) { |
---|
814 | @modules=&uniq(sort(@modules)); |
---|
815 | @incs=&uniq(sort(@incs)); |
---|
816 | $target=$target.": "; |
---|
817 | print DEPENDS "$target"; |
---|
818 | &PrintWords(length($target) + 2, 0, @modules, @incs ); |
---|
819 | print DEPENDS "\n"; |
---|
820 | undef @modules; |
---|
821 | undef @incs; |
---|
822 | } |
---|
823 | } |
---|
824 | } |
---|
825 | |
---|
826 | close (DEPENDS); |
---|
827 | |
---|
828 | } |
---|
829 | ############################################################################ |
---|
830 | # |
---|
831 | # &PrintWords(current output column, extra tab?, word list); --- print words |
---|
832 | # nicely |
---|
833 | # |
---|
834 | sub PrintWords { |
---|
835 | local($columns) = 78 - shift(@_); |
---|
836 | local($extratab) = shift(@_); |
---|
837 | local($wordlength); |
---|
838 | # |
---|
839 | print DEPENDS @_[0]; |
---|
840 | $columns -= length(shift(@_)); |
---|
841 | foreach $word (@_) { |
---|
842 | $wordlength = length($word); |
---|
843 | if ($wordlength + 1 < $columns) { |
---|
844 | print DEPENDS " $word"; |
---|
845 | $columns -= $wordlength + 1; |
---|
846 | } |
---|
847 | else { |
---|
848 | # |
---|
849 | # Continue onto a new line |
---|
850 | # |
---|
851 | if ($extratab) { |
---|
852 | print DEPENDS " \\\n\t\t$word"; |
---|
853 | $columns = 62 - $wordlength; |
---|
854 | } |
---|
855 | else { |
---|
856 | print DEPENDS " \\\n\t$word"; |
---|
857 | $columns = 70 - $wordlength; |
---|
858 | } |
---|
859 | } |
---|
860 | } |
---|
861 | } |
---|
862 | |
---|
863 | ############################################################################ |
---|
864 | # |
---|
865 | # &toLower(string); --- convert string into lower case |
---|
866 | # |
---|
867 | sub toLower { |
---|
868 | local($string) = @_[0]; |
---|
869 | $string =~ tr/A-Z/a-z/; |
---|
870 | $string; |
---|
871 | } |
---|
872 | |
---|
873 | ############################################################################ |
---|
874 | # |
---|
875 | # &uniq(sorted word list); --- remove adjacent duplicate words |
---|
876 | #@others = &uniq(sort(@others) |
---|
877 | sub uniq { |
---|
878 | local(@words); |
---|
879 | foreach $word (@_) { |
---|
880 | if ($word ne $words[$#words]) { |
---|
881 | push(@words, $word); |
---|
882 | } |
---|
883 | } |
---|
884 | @words; |
---|
885 | } |
---|
886 | |
---|
887 | EOF |
---|
888 | |
---|
889 | chmod 755 ${scriptname}.pl |
---|
890 | |
---|
891 | ./${scriptname}.pl $scriptdirec "$1" "$2" $proctype |
---|
892 | |
---|
893 | rm ${scriptname}.pl |
---|
894 | |
---|
895 | exit |
---|