New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
makemake_XMLIO.perl in trunk/EXTERNAL/MISC – NEMO

source: trunk/EXTERNAL/MISC/makemake_XMLIO.perl @ 1549

Last change on this file since 1549 was 1549, checked in by rblod, 15 years ago

external: update NVTK, renaiming again and afain

  • Property svn:executable set to *
File size: 11.6 KB
Line 
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#
15open(MAKEFILE, "> AA_make");
16#
17#print MAKEFILE "PROG =\t$ARGV[0]\n\n";
18#
19# Misc
20
21print MAKEFILE "MAKE_NAM = \$(MAKE)\n";
22print MAKEFILE "ifneq (\$(MAKE_NAM),\$(M_K))\n";
23print MAKEFILE " \@\$(error You must invoke this Makefile with the \$(M_K) command)\n";
24print MAKEFILE "endif\n";
25print MAKEFILE "USER_DIR = \$(shell /bin/pwd)\n";
26print MAKEFILE "MAKE_DIR = '??'\n";
27print MAKEFILE "ifneq (\$(USER_DIR),\$(MAKE_DIR))\n";
28print MAKEFILE " \@\$(error You must invoke this Makefile from its directory)\n";
29print MAKEFILE "endif\n";
30print MAKEFILE "#-\n";
31print MAKEFILE "F_O := \$(F_O) -I../../../lib\n";
32print MAKEFILE "P_O := \$(P_O) -traditional\n";
33print MAKEFILE "#---------------------------------------------------------------------\n";
34print MAKEFILE "#- Create libioserver and libxmlio\n";
35print MAKEFILE "#---------------------------------------------------------------------\n\n";
36
37print MAKEFILE ".PRECIOUS : \$(MODEL_LIB1) \$(MODEL_LIB2)\n";
38print MAKEFILE "SXMODEL_LIB1 = \$(MODEL_LIB1)\n";
39print MAKEFILE "SXMODEL_LIB2 = \$(MODEL_LIB2)\n";
40print MAKEFILE "ifeq (\$(L_X),1)\n";
41print MAKEFILE "SXMODEL_LIB1 = \$(LIBDIR)/libsxioserver.a\n";
42print MAKEFILE "SXMODEL_LIB2 = \$(LIBDIR)/libsxxmlio.a\n";
43print MAKEFILE ".PRECIOUS : \$(SXMODEL_LIB1) \$(SXMODEL_LIB2)\n";
44print MAKEFILE "endif\n\n";
45
46# Source listing
47#IOSERVER
48print MAKEFILE "MODS1 =\t";
49@srcs = < ../src/IOSERVER/*.F90 ../src/IOSERVER/*.f90>;
50&PrintWords(8, 0, @srcs);
51print MAKEFILE "\n\n";
52#
53# Object listing
54#
55print MAKEFILE "OBJSMODS1 =\t";
56@objs =  @srcs;
57foreach (@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);
66print MAKEFILE "\n\n";
67
68# Source listing
69#XMLIO
70print MAKEFILE "MODS2 =\t";
71@srcs = < ../src/XMLIO/*.F90 ../src/XMLIO/*.f90>;
72&PrintWords(8, 0, @srcs);
73print MAKEFILE "\n\n";
74#
75# Object listing
76#
77print MAKEFILE "OBJSMODS2 =\t";
78@objs =  @srcs;
79foreach (@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);
88print MAKEFILE "\n\n";
89
90#
91# make
92#
93print MAKEFILE ".SUFFIXES: .f90 .F90\n\n";
94
95print MAKEFILE "all: libioipsl libxmlf90 dirxml \$(MODEL_LIB2)(\$(OBJSMODS2)) \$(MODEL_LIB1)(\$(OBJSMODS1)) server.o EXEC_BIN\n";
96print MAKEFILE "\t\@echo IOSERVER is OK\n\n";
97
98# Main program
99print MAKEFILE "ifeq (\$(P_P),-DNO_MPI)\n";
100print MAKEFILE "EXEC_BIN :\n";
101print MAKEFILE "server.o :\n";
102print MAKEFILE "else\n";
103print MAKEFILE "EXEC_BIN : server.o\n";
104print 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";
105print MAKEFILE "server.o :       ../src/IOSERVER/server.f90\n";
106print MAKEFILE "\t\@\$(F_C) \$(F_O) -c ../src/IOSERVER/server.f90\n";
107print MAKEFILE "endif\n\n";
108
109
110# .f90 -> .o
111#
112
113print MAKEFILE "#Rules for ioserver\n";
114print MAKEFILE "\$(MODEL_LIB1)(%.o): ../src/IOSERVER/%.f90\n";
115print MAKEFILE "\t\$(F_C) \$(F_O) -I\$(NCDF_INC) \$<\n";
116print MAKEFILE "\t\$(A_C) \$(MODEL_LIB1) \$*.o\n";
117print MAKEFILE "ifeq (\$(L_X),1)\n";
118print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB1) \$*.o\n";
119print MAKEFILE "endif\n";
120print MAKEFILE "#-Q- sgi6       mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n";
121print MAKEFILE "ifeq (\$(M_M),1)\n";
122print MAKEFILE "\t\@mv -f *.mod \$(MODDIR)  2> /dev/null || true\n";
123print MAKEFILE "endif\n";
124print 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";
129print MAKEFILE "\$(MODEL_LIB1)(%.o): ../src/IOSERVER/%.F90\n";
130print MAKEFILE "\t\$(F_C) \$(F_O) \$(P_P) -I\$(NCDF_INC) -I../src/IOSERVER \$<\n";
131print MAKEFILE "\t\$(A_C) \$(MODEL_LIB1) \$*.o\n";
132print MAKEFILE "ifeq (\$(L_X),1)\n";
133print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB1) \$*.o\n";
134print MAKEFILE "endif\n";
135print MAKEFILE "#-Q- sgi6       mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n";
136print MAKEFILE "ifeq (\$(M_M),1)\n";
137print MAKEFILE "\t\@mv -f *.mod \$(MODDIR)  2> /dev/null || true\n";
138print MAKEFILE "endif\n";
139print MAKEFILE "\t\$(RM) \$*.o\n\n";
140
141print MAKEFILE "#Rules for xmlio\n";
142print MAKEFILE "\$(MODEL_LIB2)(%.o): ../src/XMLIO/%.f90\n";
143print MAKEFILE "\t\$(F_C) \$(F_O) -I\$(NCDF_INC) -I../src/XMLIO \$<\n";
144print MAKEFILE "\t\$(A_C) \$(MODEL_LIB2) \$*.o\n";
145print MAKEFILE "ifeq (\$(L_X),1)\n";
146print MAKEFILE "\t\$(A_X) \$(SXMODEL_LIB2) \$*.o\n";
147print MAKEFILE "endif\n";
148print MAKEFILE "#-Q- sgi6       mv \$(shell echo \$* | tr '[:lower:]' '[:upper:]').mod \$(MODDIR)\n";
149print MAKEFILE "ifeq (\$(M_M),1)\n";
150print MAKEFILE "\t\@mv -f *.mod \$(MODDIR)  2> /dev/null || true\n";
151print MAKEFILE "endif\n";
152print MAKEFILE "\t\$(RM) \$*.o\n\n";
153
154
155
156
157print MAKEFILE "dirxml: \n";
158print MAKEFILE "\t\@if [ ! -d ../../../lib/libxmlio_server ] ; then mkdir ../../../lib/libxmlio_server ; fi\n\n";
159
160print MAKEFILE "# IOIPSL library\n";
161print MAKEFILE "libioipsl :\n";
162print MAKEFILE "\t(cd ../../IOIPSL/src ; \$(M_K) -f Makefile )\n\n";
163
164print MAKEFILE "# XMLF90 library\n";
165print MAKEFILE "libxmlf90 :\n";
166print MAKEFILE "\t(cd ../../XMLF90/external ; \$(M_K) -f Makefile )\n\n";
167
168#
169# make clean
170#
171print MAKEFILE "clean:\n";
172print MAKEFILE "\t\$(RM) *.*~ Makefile~ core *.o *.mod i.*.L *.L\n";
173print 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#
187sub 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#
220sub 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#
247sub 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#
256sub 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#
270sub 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#
295sub 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   }
Note: See TracBrowser for help on using the repository browser.