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/cml:../src/dom:../src/sax:../src/strings:../src/xpath:../src/wxml\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- sxdkrz F_O = \$(F_D) \$(F_P) -C vsafe -size_t64 -I \$(MODDIR)\n"; |
---|
33 | print MAKEFILE "#-Q- eshpux F_O = \$(F_D) \$(F_P) -C vsafe -I \$(MODDIR)\n"; |
---|
34 | print MAKEFILE "#-Q- sx8brodie F_O = \$(F_D) \$(F_P) -I \$(MODDIR)\n"; |
---|
35 | print MAKEFILE "#-Q- sx6nec F_O = \$(F_D) \$(F_P) -size_t64 -Wf\,\"-pvctl loopcnt=999999\" -I \$(MODDIR)\n"; |
---|
36 | print MAKEFILE "#-Q- sx8mercure F_O = \$(F_D) \$(F_P) -size_t64 -Wf\,\"-pvctl loopcnt=999999\" -I \$(MODDIR)\n"; |
---|
37 | print MAKEFILE "#-Q- sx9mercure F_O = \$(F_D) \$(F_P) -size_t64 -Wf\,\"-pvctl loopcnt=999999\" -I \$(MODDIR)\n"; |
---|
38 | print MAKEFILE "#-Q- aix6 F_O = \$(F_P) -O3 -qextname -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
39 | print MAKEFILE "#-Q- aix F_O = \$(F_P) -O3 -qextname -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
40 | print MAKEFILE "#-Q- osxxlf = \$(F_P) -O3 -qextname -qsuffix=f=f90 -qmoddir=\$(MODDIR) -I \$(MODDIR)\n"; |
---|
41 | print MAKEFILE "#---------------------------------------------------------------------\n"; |
---|
42 | print MAKEFILE "#- Create libxmlf90\n"; |
---|
43 | print MAKEFILE "#---------------------------------------------------------------------\n\n"; |
---|
44 | |
---|
45 | print MAKEFILE ".PRECIOUS : \$(MODEL_LIB)\n"; |
---|
46 | print MAKEFILE "SXMODEL_LIB = \$(MODEL_LIB)\n"; |
---|
47 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
48 | print MAKEFILE "SXMODEL_LIB = \$(LIBDIR)/libsxxmlf90.a\n"; |
---|
49 | print MAKEFILE ".PRECIOUS : \$(SXMODEL_LIB)\n"; |
---|
50 | print MAKEFILE "endif\n\n"; |
---|
51 | |
---|
52 | # Source listing |
---|
53 | # |
---|
54 | print MAKEFILE "MODS1 =\t"; |
---|
55 | @srcs = < ../*/*/*.f90 >; |
---|
56 | &PrintWords(8, 0, @srcs); |
---|
57 | print MAKEFILE "\n\n"; |
---|
58 | # |
---|
59 | # Object listing |
---|
60 | # |
---|
61 | print MAKEFILE "OBJSMODS1 =\t"; |
---|
62 | @objs = @srcs; |
---|
63 | foreach (@objs) { s/\.[^.]+$/.o/ }; |
---|
64 | foreach $objs (@objs) { |
---|
65 | @tab = split('/', $objs); |
---|
66 | $objs = $tab[$#tab] ; |
---|
67 | }; |
---|
68 | |
---|
69 | &PrintWords(8, 0, @objs); |
---|
70 | print MAKEFILE "\n\n"; |
---|
71 | # |
---|
72 | # Define common macros |
---|
73 | # |
---|
74 | #print MAKEFILE "LIBS =\t\n\n"; |
---|
75 | #print MAKEFILE "CC = cc\n"; |
---|
76 | #print MAKEFILE "CFLAGS = -O\n"; |
---|
77 | #print MAKEFILE "FC = f77\n"; |
---|
78 | #print MAKEFILE "FFLAGS = -O\n"; |
---|
79 | #print MAKEFILE "F90 = f90\n"; |
---|
80 | #print MAKEFILE "F90FLAGS = -O\n"; |
---|
81 | #print MAKEFILE "LDFLAGS = -s\n\n"; |
---|
82 | # |
---|
83 | # make |
---|
84 | # |
---|
85 | print MAKEFILE "all: dirxml \$(MODEL_LIB)(\$(OBJSMODS1))\n"; |
---|
86 | print MAKEFILE "\t\@echo XMLF90 is OK\n\n"; |
---|
87 | |
---|
88 | # .f90 -> .o |
---|
89 | # |
---|
90 | |
---|
91 | print MAKEFILE "#---USE VPATH--- \$(MODEL_LIB)(%.o): ../*/*/%.f90\n"; |
---|
92 | print MAKEFILE "\$(MODEL_LIB)(%.o): %.f90\n"; |
---|
93 | print MAKEFILE "\t\$(F_C) \$(F_O) -I\$(NCDF_INC) \$<\n"; |
---|
94 | print MAKEFILE "\t\$(A_C) \$(MODEL_LIB) \$*.o\n"; |
---|
95 | print MAKEFILE "ifeq (\$(L_X),1)\n"; |
---|
96 | print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB) \$*.o\n"; |
---|
97 | print MAKEFILE "endif\n"; |
---|
98 | print MAKEFILE "#-Q- sgi6 mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n"; |
---|
99 | print MAKEFILE "ifeq (\$(M_M),1)\n"; |
---|
100 | print MAKEFILE "\t\@mv -f *.mod \$(MODDIR) 2> /dev/null || true\n"; |
---|
101 | print MAKEFILE "endif\n"; |
---|
102 | print MAKEFILE "\t\$(RM) \$*.o\n\n";; |
---|
103 | |
---|
104 | |
---|
105 | print MAKEFILE "dirxml: \n"; |
---|
106 | print MAKEFILE "\t\@if [ ! -d ../../../lib/libxmlio_server ] ; then mkdir ../../../lib/libxmlio_server ; fi\n\n"; |
---|
107 | # |
---|
108 | # make clean |
---|
109 | # |
---|
110 | print MAKEFILE "clean:\n"; |
---|
111 | print MAKEFILE "\t\$(RM) *.*~ Makefile~ core *.o *.mod i.*.L *.L\n"; |
---|
112 | print MAKEFILE "\t\$(RM) \$(MODEL_LIB) \n\n"; |
---|
113 | # |
---|
114 | # |
---|
115 | # Dependency listings |
---|
116 | # |
---|
117 | &MakeDependsf90($ARGV[1]); |
---|
118 | &MakeDepends("*.f *.F", '^\s*include\s+["\']([^"\']+)["\']'); |
---|
119 | &MakeDepends("*.c", '^\s*#\s*include\s+["\']([^"\']+)["\']'); |
---|
120 | |
---|
121 | # |
---|
122 | # &PrintWords(current output column, extra tab?, word list); --- print words |
---|
123 | # nicely |
---|
124 | # |
---|
125 | sub PrintWords { |
---|
126 | local($columns) = 78 - shift(@_); |
---|
127 | local($extratab) = shift(@_); |
---|
128 | local($wordlength); |
---|
129 | # |
---|
130 | print MAKEFILE @_[0]; |
---|
131 | $columns -= length(shift(@_)); |
---|
132 | foreach $word (@_) { |
---|
133 | $wordlength = length($word); |
---|
134 | if ($wordlength + 1 < $columns) { |
---|
135 | print MAKEFILE " $word"; |
---|
136 | $columns -= $wordlength + 1; |
---|
137 | } |
---|
138 | else { |
---|
139 | # |
---|
140 | # Continue onto a new line |
---|
141 | # |
---|
142 | if ($extratab) { |
---|
143 | print MAKEFILE " \\\n\t\t$word"; |
---|
144 | $columns = 62 - $wordlength; |
---|
145 | } |
---|
146 | else { |
---|
147 | print MAKEFILE " \\\n\t$word"; |
---|
148 | $columns = 70 - $wordlength; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | # |
---|
155 | # &LanguageCompiler(compiler, sources); --- determine the correct language |
---|
156 | # compiler |
---|
157 | # |
---|
158 | sub LanguageCompiler { |
---|
159 | local($compiler) = &toLower(shift(@_)); |
---|
160 | local(@srcs) = @_; |
---|
161 | # |
---|
162 | if (length($compiler) > 0) { |
---|
163 | CASE: { |
---|
164 | grep(/^$compiler$/, ("fc", "f77")) && |
---|
165 | do { $compiler = "FC"; last CASE; }; |
---|
166 | grep(/^$compiler$/, ("cc", "c")) && |
---|
167 | do { $compiler = "CC"; last CASE; }; |
---|
168 | $compiler = "F90"; |
---|
169 | } |
---|
170 | } |
---|
171 | else { |
---|
172 | CASE: { |
---|
173 | grep(/\.f90$/, @srcs) && do { $compiler = "F90"; last CASE; }; |
---|
174 | grep(/\.(f|F)$/, @srcs) && do { $compiler = "FC"; last CASE; }; |
---|
175 | grep(/\.c$/, @srcs) && do { $compiler = "CC"; last CASE; }; |
---|
176 | $compiler = "???"; |
---|
177 | } |
---|
178 | } |
---|
179 | $compiler; |
---|
180 | } |
---|
181 | |
---|
182 | # |
---|
183 | # &toLower(string); --- convert string into lower case |
---|
184 | # |
---|
185 | sub toLower { |
---|
186 | local($string) = @_[0]; |
---|
187 | $string =~ tr/A-Z/a-z/; |
---|
188 | $string; |
---|
189 | } |
---|
190 | |
---|
191 | # |
---|
192 | # &uniq(sorted word list); --- remove adjacent duplicate words |
---|
193 | # |
---|
194 | sub uniq { |
---|
195 | local(@words); |
---|
196 | foreach $word (@_) { |
---|
197 | if ($word ne $words[$#words]) { |
---|
198 | push(@words, $word); |
---|
199 | } |
---|
200 | } |
---|
201 | @words; |
---|
202 | } |
---|
203 | |
---|
204 | # |
---|
205 | # &MakeDepends(language pattern, include file sed pattern); --- dependency |
---|
206 | # maker |
---|
207 | # |
---|
208 | sub MakeDepends { |
---|
209 | local(@incs); |
---|
210 | local($lang) = @_[0]; |
---|
211 | local($pattern) = @_[1]; |
---|
212 | # |
---|
213 | foreach $file (<${lang}>) { |
---|
214 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
215 | while (<FILE>) { |
---|
216 | /$pattern/i && push(@incs, $1); |
---|
217 | } |
---|
218 | if (defined @incs) { |
---|
219 | $file =~ s/\.[^.]+$/.o/; |
---|
220 | @tab = split('/', $file); |
---|
221 | $file = '$(MODEL_LIB)(' . $tab[$#tab] . ')' ; |
---|
222 | print MAKEFILE "$file: "; |
---|
223 | &PrintWords(length($file) + 2, 0, @incs); |
---|
224 | print MAKEFILE "\n"; |
---|
225 | undef @incs; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | # |
---|
231 | # &MakeDependsf90(f90 compiler); --- FORTRAN 90 dependency maker |
---|
232 | # |
---|
233 | sub MakeDependsf90 { |
---|
234 | local($compiler) = &toLower(@_[0]); |
---|
235 | local(@dependencies); |
---|
236 | local(%filename); |
---|
237 | local(@incs); |
---|
238 | local(@modules); |
---|
239 | local($objfile); |
---|
240 | # |
---|
241 | # Associate each module with the name of the file that contains it |
---|
242 | # |
---|
243 | foreach $file (<../*/*/*.f90>) { |
---|
244 | open(FILE, $file) || warn "Cannot open $file: $!\n"; |
---|
245 | while (<FILE>) { |
---|
246 | /^\s*module\s+([^\s!]+)/i && |
---|
247 | ($filename{&toLower($1)} = $file) =~ s/\.f90$/.o/; |
---|
248 | } |
---|
249 | } |
---|
250 | # |
---|
251 | # Print the dependencies of each file that has one or more include's or |
---|
252 | # references one or more modules |
---|
253 | # |
---|
254 | foreach $file (<../*/*/*.f90>) { |
---|
255 | open(FILE, $file); |
---|
256 | while (<FILE>) { |
---|
257 | /^\s*include\s+["\']([^"\']+)["\']/i && push(@incs, $1); |
---|
258 | /^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1)); |
---|
259 | } |
---|
260 | if (defined @incs || defined @modules) { |
---|
261 | ($objfile = $file) =~ s/\.f90$/.o/; |
---|
262 | @tab = split('/', $objfile); |
---|
263 | $objfile = '$(MODEL_LIB)(' . $tab[$#tab] . ')' ; |
---|
264 | |
---|
265 | print MAKEFILE "$objfile: "; |
---|
266 | undef @dependencies; |
---|
267 | foreach $module (@modules) { |
---|
268 | push(@dependencies, $filename{$module}); |
---|
269 | } |
---|
270 | @dependencies = &uniq(sort(@dependencies)); |
---|
271 | |
---|
272 | foreach $dependencies (@dependencies) { |
---|
273 | @tab = split('/', $dependencies); |
---|
274 | $dependencies = '$(MODEL_LIB)(' . $tab[$#tab] . ')'; |
---|
275 | } |
---|
276 | |
---|
277 | &PrintWords(length($objfile) + 2, 0, |
---|
278 | @dependencies, &uniq(sort(@incs))); |
---|
279 | print MAKEFILE "\n"; |
---|
280 | undef @incs; |
---|
281 | undef @modules; |
---|
282 | # |
---|
283 | # |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|