1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # Usage: makemake {<program name> {<F90 compiler or fc or f77 or cc or c>}} |
---|
4 | # |
---|
5 | # Generate a Makefile from the sources in the current directory. The source |
---|
6 | # files may be in either C, FORTRAN 77, Fortran 90 or some combination of |
---|
7 | # these languages. If the F90 compiler specified is cray or parasoft, then |
---|
8 | # the Makefile generated will conform to the conventions of these compilers. |
---|
9 | # To run makemake, it will be necessary to modify the first line of this script |
---|
10 | # to point to the actual location of Perl on your system. |
---|
11 | # |
---|
12 | # Written by Michael Wester <wester@math.unm.edu> February 16, 1995 |
---|
13 | # Cotopaxi (Consulting), Albuquerque, New Mexico |
---|
14 | # |
---|
15 | open(MAKEFILE, "> AA_make"); |
---|
16 | # |
---|
17 | #print MAKEFILE "PROG =\t$ARGV[0]\n\n"; |
---|
18 | # |
---|
19 | # Misc |
---|
20 | |
---|
21 | print MAKEFILE "VPATH=../src/IOSERVER:../src/XMLIO\n"; |
---|
22 | print MAKEFILE "MAKE_NAM = \$(MAKE)\n"; |
---|
23 | print MAKEFILE "ifneq (\$(MAKE_NAM),\$(M_K))\n"; |
---|
24 | print MAKEFILE " \@\$(error You must invoke this Makefile with the \$(M_K) command)\n"; |
---|
25 | print MAKEFILE "endif\n"; |
---|
26 | print MAKEFILE "USER_DIR = \$(shell /bin/pwd)\n"; |
---|
27 | print MAKEFILE "MAKE_DIR = '??'\n"; |
---|
28 | print MAKEFILE "ifneq (\$(USER_DIR),\$(MAKE_DIR))\n"; |
---|
29 | print MAKEFILE " \@\$(error You must invoke this Makefile from its directory)\n"; |
---|
30 | print MAKEFILE "endif\n"; |
---|
31 | print MAKEFILE "#-\n"; |
---|
32 | print MAKEFILE "#-Q- aix6 F_O = \$(F_P) -O3 -qextname -qsuffix=cpp=F90 -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
33 | print MAKEFILE "#-Q- aix F_O = \$(F_P) -O3 -qextname -qsuffix=cpp=F90 -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
34 | print MAKEFILE "#-Q- osxxlf F_O = \$(F_P) -O3 -qextname -qsuffix=cpp=F90 -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
35 | print MAKEFILE "F_O := \$(F_O) -I../../../lib\n"; |
---|
36 | print MAKEFILE "P_O := \$(P_O) -traditional\n"; |
---|
37 | print MAKEFILE "#-Q- aix6 L_O = \$(F_P) -q64 -O3\n"; |
---|
38 | print MAKEFILE "#-Q- aix L_O = \$(F_P) -q64 -O3\n"; |
---|
39 | print MAKEFILE "prefix=''\n"; |
---|
40 | print MAKEFILE "#-Q- aix6 prefix='-WF,'\n"; |
---|
41 | print MAKEFILE "#-Q- aix prefix='-WF,'\n"; |
---|
42 | print MAKEFILE "override P_P := \$(P_P:\%=\$(prefix)\%)\n\n"; |
---|
43 | print MAKEFILE "#---------------------------------------------------------------------\n"; |
---|
44 | print MAKEFILE "#- Create libioserver and libxmlio\n"; |
---|
45 | print MAKEFILE "#---------------------------------------------------------------------\n\n"; |
---|
46 | |
---|
47 | print MAKEFILE ".PRECIOUS : \$(MODEL_LIB1) \$(MODEL_LIB2)\n"; |
---|
48 | print MAKEFILE "SXMODEL_LIB1 = \$(MODEL_LIB1)\n"; |
---|
49 | print MAKEFILE "SXMODEL_LIB2 = \$(MODEL_LIB2)\n"; |
---|
50 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
51 | print MAKEFILE "SXMODEL_LIB1 = \$(LIBDIR)/libsxioserver.a\n"; |
---|
52 | print MAKEFILE "SXMODEL_LIB2 = \$(LIBDIR)/libsxxmlio.a\n"; |
---|
53 | print MAKEFILE ".PRECIOUS : \$(SXMODEL_LIB1) \$(SXMODEL_LIB2)\n"; |
---|
54 | print MAKEFILE "endif\n\n"; |
---|
55 | |
---|
56 | # Source listing |
---|
57 | #IOSERVER |
---|
58 | print MAKEFILE "MODS1 =\t"; |
---|
59 | @srcs = < ../src/IOSERVER/*.F90 ../src/IOSERVER/*.f90>; |
---|
60 | &PrintWords(8, 0, @srcs); |
---|
61 | print MAKEFILE "\n\n"; |
---|
62 | # |
---|
63 | # Object listing |
---|
64 | # |
---|
65 | print MAKEFILE "OBJSMODS1 =\t"; |
---|
66 | @objs = @srcs; |
---|
67 | foreach (@objs) { s/\.[^.]+$/.o/ }; |
---|
68 | foreach $objs (@objs) { |
---|
69 | @tab = split('/', $objs); |
---|
70 | $objs = $tab[$#tab] ; |
---|
71 | if( "$objs" eq 'server.o' ){ |
---|
72 | $objs = ''}; |
---|
73 | }; |
---|
74 | |
---|
75 | &PrintWords(8, 0, @objs); |
---|
76 | print MAKEFILE "\n\n"; |
---|
77 | |
---|
78 | # Source listing |
---|
79 | #XMLIO |
---|
80 | print MAKEFILE "MODS2 =\t"; |
---|
81 | @srcs = < ../src/XMLIO/*.F90 ../src/XMLIO/*.f90>; |
---|
82 | &PrintWords(8, 0, @srcs); |
---|
83 | print MAKEFILE "\n\n"; |
---|
84 | # |
---|
85 | # Object listing |
---|
86 | # |
---|
87 | print MAKEFILE "OBJSMODS2 =\t"; |
---|
88 | @objs = @srcs; |
---|
89 | foreach (@objs) { s/\.[^.]+$/.o/ }; |
---|
90 | foreach $objs (@objs) { |
---|
91 | @tab = split('/', $objs); |
---|
92 | $objs = $tab[$#tab] ; |
---|
93 | if( "$objs" eq 'test_xmlio.o' ){ |
---|
94 | $objs = ''}; |
---|
95 | }; |
---|
96 | |
---|
97 | &PrintWords(8, 0, @objs); |
---|
98 | print MAKEFILE "\n\n"; |
---|
99 | |
---|
100 | # |
---|
101 | # make |
---|
102 | # |
---|
103 | print MAKEFILE ".SUFFIXES: .f90 .F90\n\n"; |
---|
104 | |
---|
105 | print MAKEFILE "all: libioipsl libxmlf90 dirxml \$(MODEL_LIB2)(\$(OBJSMODS2)) key \$(MODEL_LIB1)(\$(OBJSMODS1)) server.o EXEC_BIN\n"; |
---|
106 | print MAKEFILE "\t\@echo IOSERVER is OK\n\n"; |
---|
107 | |
---|
108 | # mpi or not |
---|
109 | print MAKEFILE "key :\n"; |
---|
110 | print MAKEFILE "\t\-\@if [ \"`cat KEY_CPP`\" \!= \"\$(P_P)\" ] \; then \\\n"; |
---|
111 | print MAKEFILE "\techo \"\$(P_P)\" > KEY_CPP \; fi\n\n"; |
---|
112 | |
---|
113 | |
---|
114 | # Main program |
---|
115 | print MAKEFILE "ifneq (,\$(findstring NO_MPI,\$(P_P)))\n"; |
---|
116 | print MAKEFILE "EXEC_BIN :\n"; |
---|
117 | print MAKEFILE "server.o :\n"; |
---|
118 | print MAKEFILE "else\n"; |
---|
119 | print MAKEFILE "EXEC_BIN : server.o\n"; |
---|
120 | print MAKEFILE "\t\$(F_L) \$(L_O) -o ../../../bin/server.exe server.o \$(MODEL_LIB1) \$(MODEL_LIB2) ../../../lib/libioipsl.a \$(NCDF_LIB) ../../../lib/libxmlio_server/libxmlf90.a\n"; |
---|
121 | print MAKEFILE "server.o : ../src/IOSERVER/server.f90\n"; |
---|
122 | print MAKEFILE "\t\@\$(F_C) \$(F_O) -c ../src/IOSERVER/server.f90\n"; |
---|
123 | print MAKEFILE "endif\n\n"; |
---|
124 | |
---|
125 | |
---|
126 | # .f90 -> .o |
---|
127 | # |
---|
128 | |
---|
129 | print MAKEFILE "#Rules for ioserver\n"; |
---|
130 | print MAKEFILE "#---- Using VPATH instead \$(MODEL_LIB1)(%.o): ../src/IOSERVER/%.f90\n"; |
---|
131 | print MAKEFILE "\$(MODEL_LIB1)(%.o): %.f90\n"; |
---|
132 | print MAKEFILE "\t\$(F_C) \$(F_O) -I\$(NCDF_INC) \$<\n"; |
---|
133 | print MAKEFILE "\t\$(A_C) \$(MODEL_LIB1) \$*.o\n"; |
---|
134 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
135 | print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB1) \$*.o\n"; |
---|
136 | print MAKEFILE "endif\n"; |
---|
137 | print MAKEFILE "#-Q- sgi6 mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n"; |
---|
138 | print MAKEFILE "ifeq (\$(M_M),1)\n"; |
---|
139 | print MAKEFILE "\t\@mv -f *.mod \$(MODDIR) 2> /dev/null || true\n"; |
---|
140 | print MAKEFILE "endif\n"; |
---|
141 | print MAKEFILE "\t\$(RM) \$*.o\n\n"; |
---|
142 | |
---|
143 | print MAKEFILE "#Rules for ioserver\n"; |
---|
144 | #print MAKEFILE "\$src/IOSERVER/%.f90: ../src/IOSERVER/%.F90\n"; |
---|
145 | #print MAKEFILE "\t@\$(P_C) \$(P_O) \$(P_P) src/IOSERVER/\$(*F).F90 > src/IOSERVER/\$(*F).f90\n\n"; |
---|
146 | print MAKEFILE "#---- Using VPATH instead \$(MODEL_LIB1)(%.o): ../src/IOSERVER/%.F90\n"; |
---|
147 | print MAKEFILE "\$(MODEL_LIB1)(%.o): %.F90 KEY_CPP\n"; |
---|
148 | print MAKEFILE "\t\$(F_C) \$(F_O) \$(P_P) -I\$(NCDF_INC) -I../src/IOSERVER \$<\n"; |
---|
149 | print MAKEFILE "\t\$(A_C) \$(MODEL_LIB1) \$*.o\n"; |
---|
150 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
151 | print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB1) \$*.o\n"; |
---|
152 | print MAKEFILE "endif\n"; |
---|
153 | print MAKEFILE "#-Q- sgi6 mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n"; |
---|
154 | print MAKEFILE "ifeq (\$(M_M),1)\n"; |
---|
155 | print MAKEFILE "\t\@mv -f *.mod \$(MODDIR) 2> /dev/null || true\n"; |
---|
156 | print MAKEFILE "endif\n"; |
---|
157 | print MAKEFILE "\t\$(RM) \$*.o\n\n"; |
---|
158 | |
---|
159 | print MAKEFILE "#Rules for xmlio\n"; |
---|
160 | print MAKEFILE "#---- Using VPATH instead \$(MODEL_LIB1)(%.o): ../src/XMLIO/%.F90\n"; |
---|
161 | print MAKEFILE "\$(MODEL_LIB2)(%.o): %.f90\n"; |
---|
162 | print MAKEFILE "\t\$(F_C) \$(F_O) -I\$(NCDF_INC) -I../src/XMLIO \$<\n"; |
---|
163 | print MAKEFILE "\t\$(A_C) \$(MODEL_LIB2) \$*.o\n"; |
---|
164 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
165 | print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB2) \$*.o\n"; |
---|
166 | print MAKEFILE "endif\n"; |
---|
167 | print MAKEFILE "#-Q- sgi6 mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n"; |
---|
168 | print MAKEFILE "ifeq (\$(M_M),1)\n"; |
---|
169 | print MAKEFILE "\t\@mv -f *.mod \$(MODDIR) 2> /dev/null || true\n"; |
---|
170 | print MAKEFILE "endif\n"; |
---|
171 | print MAKEFILE "\t\$(RM) \$*.o\n\n"; |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | print MAKEFILE "dirxml: \n"; |
---|
177 | print MAKEFILE "\t\@if [ ! -d ../../../lib/libxmlio_server ] ; then mkdir ../../../lib/libxmlio_server ; fi\n\n"; |
---|
178 | |
---|
179 | print MAKEFILE "# IOIPSL library\n"; |
---|
180 | print MAKEFILE "libioipsl :\n"; |
---|
181 | print MAKEFILE "\t(cd ../../IOIPSL/src ; \$(M_K) -f Makefile )\n\n"; |
---|
182 | |
---|
183 | print MAKEFILE "# XMLF90 library\n"; |
---|
184 | print MAKEFILE "libxmlf90 :\n"; |
---|
185 | print MAKEFILE "\t(cd ../../XMLF90/external ; \$(M_K) -f Makefile )\n\n"; |
---|
186 | |
---|
187 | # |
---|
188 | # make clean |
---|
189 | # |
---|
190 | print MAKEFILE "clean:\n"; |
---|
191 | print MAKEFILE "\t\$(RM) *.*~ Makefile~ core *.o *.mod i.*.L *.L\n"; |
---|
192 | print MAKEFILE "\t\$(RM) server.o \$(MODEL_LIB1) \$(MODEL_LIB2)\n\n"; |
---|
193 | # |
---|
194 | # |
---|
195 | # Dependency listings |
---|
196 | # |
---|
197 | &MakeDependsf90($ARGV[1],IOSERVER,MODEL_LIB1); |
---|
198 | &MakeDependsf90($ARGV[1],XMLIO,MODEL_LIB2); |
---|
199 | &MakeDepends("*.f *.F", '^\s*include\s+["\']([^"\']+)["\']'); |
---|
200 | &MakeDepends("*.c", '^\s*#\s*include\s+["\']([^"\']+)["\']'); |
---|
201 | |
---|
202 | # |
---|
203 | # &PrintWords(current output column, extra tab?, word list); --- print words |
---|
204 | # nicely |
---|
205 | # |
---|
206 | sub PrintWords { |
---|
207 | local($columns) = 78 - shift(@_); |
---|
208 | local($extratab) = shift(@_); |
---|
209 | local($wordlength); |
---|
210 | # |
---|
211 | print MAKEFILE @_[0]; |
---|
212 | $columns -= length(shift(@_)); |
---|
213 | foreach $word (@_) { |
---|
214 | $wordlength = length($word); |
---|
215 | if ($wordlength + 1 < $columns) { |
---|
216 | print MAKEFILE " $word"; |
---|
217 | $columns -= $wordlength + 1; |
---|
218 | } |
---|
219 | else { |
---|
220 | # |
---|
221 | # Continue onto a new line |
---|
222 | # |
---|
223 | if ($extratab) { |
---|
224 | print MAKEFILE " \\\n\t\t$word"; |
---|
225 | $columns = 62 - $wordlength; |
---|
226 | } |
---|
227 | else { |
---|
228 | print MAKEFILE " \\\n\t$word"; |
---|
229 | $columns = 70 - $wordlength; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | # |
---|
236 | # &LanguageCompiler(compiler, sources); --- determine the correct language |
---|
237 | # compiler |
---|
238 | # |
---|
239 | sub LanguageCompiler { |
---|
240 | local($compiler) = &toLower(shift(@_)); |
---|
241 | local(@srcs) = @_; |
---|
242 | # |
---|
243 | if (length($compiler) > 0) { |
---|
244 | CASE: { |
---|
245 | grep(/^$compiler$/, ("fc", "f77")) && |
---|
246 | do { $compiler = "FC"; last CASE; }; |
---|
247 | grep(/^$compiler$/, ("cc", "c")) && |
---|
248 | do { $compiler = "CC"; last CASE; }; |
---|
249 | $compiler = "F90"; |
---|
250 | } |
---|
251 | } |
---|
252 | else { |
---|
253 | CASE: { |
---|
254 | grep(/\.f90$/, @srcs) && do { $compiler = "F90"; last CASE; }; |
---|
255 | grep(/\.(f|F)$/, @srcs) && do { $compiler = "FC"; last CASE; }; |
---|
256 | grep(/\.c$/, @srcs) && do { $compiler = "CC"; last CASE; }; |
---|
257 | $compiler = "???"; |
---|
258 | } |
---|
259 | } |
---|
260 | $compiler; |
---|
261 | } |
---|
262 | |
---|
263 | # |
---|
264 | # &toLower(string); --- convert string into lower case |
---|
265 | # |
---|
266 | sub toLower { |
---|
267 | local($string) = @_[0]; |
---|
268 | $string =~ tr/A-Z/a-z/; |
---|
269 | $string; |
---|
270 | } |
---|
271 | |
---|
272 | # |
---|
273 | # &uniq(sorted word list); --- remove adjacent duplicate words |
---|
274 | # |
---|
275 | sub uniq { |
---|
276 | local(@words); |
---|
277 | foreach $word (@_) { |
---|
278 | if ($word ne $words[$#words]) { |
---|
279 | push(@words, $word); |
---|
280 | } |
---|
281 | } |
---|
282 | @words; |
---|
283 | } |
---|
284 | |
---|
285 | # |
---|
286 | # &MakeDepends(language pattern, include file sed pattern); --- dependency |
---|
287 | # maker |
---|
288 | # |
---|
289 | sub MakeDepends { |
---|
290 | local(@incs); |
---|
291 | local($lang) = @_[0]; |
---|
292 | local($pattern) = @_[1]; |
---|
293 | # |
---|
294 | foreach $file (<${lang}>) { |
---|
295 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
296 | while (<FILE>) { |
---|
297 | /$pattern/i && push(@incs, $1); |
---|
298 | } |
---|
299 | if (defined @incs) { |
---|
300 | $file =~ s/\.[^.]+$/.o/; |
---|
301 | @tab = split('/', $file); |
---|
302 | $file = '$(MODEL_LIB)(' . $tab[$#tab] . ')' ; |
---|
303 | print MAKEFILE "$file: "; |
---|
304 | &PrintWords(length($file) + 2, 0, @incs); |
---|
305 | print MAKEFILE "\n"; |
---|
306 | undef @incs; |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | # |
---|
312 | # &MakeDependsf90(f90 compiler); --- FORTRAN 90 dependency maker |
---|
313 | # |
---|
314 | sub MakeDependsf90 { |
---|
315 | local($compiler) = &toLower(@_[0]); |
---|
316 | local(@dependencies); |
---|
317 | local(%filename); |
---|
318 | local(@incs); |
---|
319 | local(@modules); |
---|
320 | local($objfile); |
---|
321 | local($DIR) = @_[1]; |
---|
322 | local($MODEL_LIB) = @_[2]; |
---|
323 | # |
---|
324 | # Associate each module with the name of the file that contains it |
---|
325 | # |
---|
326 | foreach $file (<../*/*/*.f90>) { |
---|
327 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
328 | while (<FILE>) { |
---|
329 | /^\s*module\s+([^\s!]+)/i && |
---|
330 | ($filename{&toLower($1)} = $file) =~ s/\.f90$/.o/; |
---|
331 | } |
---|
332 | } |
---|
333 | foreach $file (<../*/*/*.F90>) { |
---|
334 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
335 | while (<FILE>) { |
---|
336 | /^\s*module\s+([^\s!]+)/i && |
---|
337 | ($filename{&toLower($1)} = $file) =~ s/\.F90$/.o/; |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | # |
---|
342 | # Print the dependencies of each file that has one or more include's or |
---|
343 | # references one or more modules |
---|
344 | # |
---|
345 | foreach $file (<../src/$DIR/*.f90 ../src/$DIR/*.F90>) { |
---|
346 | open(FILE, $file); |
---|
347 | while (<FILE>) { |
---|
348 | /^\s*include\s+["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
349 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
350 | } |
---|
351 | if (defined @incs || defined @modules) { |
---|
352 | # ($objfile = $file) =~ s/\.F90$/.o/; |
---|
353 | ($objfile = $file) =~ s/\.[fF]90$/.o/; |
---|
354 | @tab = split('/', $objfile); |
---|
355 | $objfile = '$(' . $MODEL_LIB . ')(' . $tab[$#tab] . ')' ; |
---|
356 | |
---|
357 | print MAKEFILE "$objfile: "; |
---|
358 | undef @dependencies; |
---|
359 | foreach $module (@modules) { |
---|
360 | push(@dependencies, $filename{$module}); |
---|
361 | } |
---|
362 | @dependencies = &uniq(sort(@dependencies)); |
---|
363 | |
---|
364 | foreach $dependencies (@dependencies) { |
---|
365 | |
---|
366 | @tab = split('/', $dependencies); |
---|
367 | $dependencies = '$(' . $MODEL_LIB . ')(' . $tab[$#tab] . ')'; |
---|
368 | |
---|
369 | if( "$DIR" eq 'IOSERVER' && "$dependencies" eq '$(MODEL_LIB1)(xmlio.o)' ) { |
---|
370 | $dependencies = '$(MODEL_LIB2)' |
---|
371 | # $dependencies = '' |
---|
372 | |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | foreach $incs (@incs) { |
---|
377 | @tab = split('/', $incs); |
---|
378 | @incs = '../src' . '/' . $DIR. '/' . $tab[$#tab] ; |
---|
379 | } |
---|
380 | foreach $incs (@incs) { |
---|
381 | if( "$incs" eq '../src/IOSERVER/mpif.h' || "$incs" eq '../src/IOSERVER/VT.inc' || "$incs" eq '../src/IOSERVER/ios_mpif.h' ) { |
---|
382 | $incs = ''; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | &PrintWords(length($objfile) + 2, 0, |
---|
389 | @dependencies, &uniq(sort(@incs))); |
---|
390 | print MAKEFILE "\n"; |
---|
391 | undef @incs; |
---|
392 | undef @modules; |
---|
393 | # |
---|
394 | # |
---|
395 | } |
---|
396 | } |
---|
397 | } |
---|