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