-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmakemake
executable file
·251 lines (245 loc) · 6.14 KB
/
makemake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#! /usr/bin/perl
#
# Usage: makemake {<program name> {<F90 compiler or fc or f77 or cc or c>}}
#
# Generate a Makefile from the sources in the current directory. The source
# files may be in either C, FORTRAN 77, Fortran 90 or some combination of
# these languages. If the F90 compiler specified is cray or parasoft, then
# the Makefile generated will conform to the conventions of these compilers.
# To run makemake, it will be necessary to modify the first line of this script
# to point to the actual location of Perl on your system.
#
# Written by Michael Wester <wester@math.unm.edu> February 16, 1995
# Cotopaxi (Consulting), Albuquerque, New Mexico
#
open(MAKEFILE, "> Makefile");
#
print MAKEFILE "PROG =\t$ARGV[0]\n\n";
#
# Source listing
#
print MAKEFILE "SRCS =\t";
@srcs = <*.f90 *.f *.F *.c>;
&PrintWords(8, 0, @srcs);
print MAKEFILE "\n\n";
#
# Object listing
#
print MAKEFILE "OBJS =\t";
@objs = @srcs;
foreach (@objs) { s/\.[^.]+$/.o/ };
&PrintWords(8, 0, @objs);
print MAKEFILE "\n\n";
#
# Define common macros
#
print MAKEFILE "LIBS =\t\n\n";
print MAKEFILE "CC = cc\n";
print MAKEFILE "CFLAGS = -O\n";
print MAKEFILE "FC = f77\n";
print MAKEFILE "FFLAGS = -O\n";
print MAKEFILE "F90 = f90\n";
print MAKEFILE "F90FLAGS = -O\n";
print MAKEFILE "LDFLAGS = -s\n\n";
#
# make
#
print MAKEFILE "all: \$(PROG)\n\n";
print MAKEFILE "\$(PROG): \$(OBJS)\n";
print MAKEFILE "\t\$(", &LanguageCompiler($ARGV[1], @srcs);
print MAKEFILE ") \$(LDFLAGS) -o \$@ \$(OBJS) \$(LIBS)\n\n";
#
# make clean
#
print MAKEFILE "clean:\n";
print MAKEFILE "\trm -f \$(PROG) \$(OBJS) *.mod\n\n";
#
# Make .f90 a valid suffix
#
print MAKEFILE ".SUFFIXES: \$(SUFFIXES) .f90\n\n";
#
# .f90 -> .o
#
print MAKEFILE ".f90.o:\n";
print MAKEFILE "\t\$(F90) \$(F90FLAGS) -c \$<\n\n";
#
# Dependency listings
#
&MakeDependsf90($ARGV[1]);
&MakeDepends("*.f *.F", '^\s*include\s+["\']([^"\']+)["\']');
&MakeDepends("*.c", '^\s*#\s*include\s+["\']([^"\']+)["\']');
#
# &PrintWords(current output column, extra tab?, word list); --- print words
# nicely
#
sub PrintWords {
local($columns) = 78 - shift(@_);
local($extratab) = shift(@_);
local($wordlength);
#
print MAKEFILE @_[0];
$columns -= length(shift(@_));
foreach $word (@_) {
$wordlength = length($word);
if ($wordlength + 1 < $columns) {
print MAKEFILE " $word";
$columns -= $wordlength + 1;
}
else {
#
# Continue onto a new line
#
if ($extratab) {
print MAKEFILE " \\\n\t\t$word";
$columns = 62 - $wordlength;
}
else {
print MAKEFILE " \\\n\t$word";
$columns = 70 - $wordlength;
}
}
}
}
#
# &LanguageCompiler(compiler, sources); --- determine the correct language
# compiler
#
sub LanguageCompiler {
local($compiler) = &toLower(shift(@_));
local(@srcs) = @_;
#
if (length($compiler) > 0) {
CASE: {
grep(/^$compiler$/, ("fc", "f77")) &&
do { $compiler = "FC"; last CASE; };
grep(/^$compiler$/, ("cc", "c")) &&
do { $compiler = "CC"; last CASE; };
$compiler = "F90";
}
}
else {
CASE: {
grep(/\.f90$/, @srcs) && do { $compiler = "F90"; last CASE; };
grep(/\.(f|F)$/, @srcs) && do { $compiler = "FC"; last CASE; };
grep(/\.c$/, @srcs) && do { $compiler = "CC"; last CASE; };
$compiler = "???";
}
}
$compiler;
}
#
# &toLower(string); --- convert string into lower case
#
sub toLower {
local($string) = @_[0];
$string =~ tr/A-Z/a-z/;
$string;
}
#
# &uniq(sorted word list); --- remove adjacent duplicate words
#
sub uniq {
local(@words);
foreach $word (@_) {
if ($word ne $words[$#words]) {
push(@words, $word);
}
}
@words;
}
#
# &MakeDepends(language pattern, include file sed pattern); --- dependency
# maker
#
sub MakeDepends {
local(@incs);
local($lang) = @_[0];
local($pattern) = @_[1];
#
foreach $file (<${lang}>) {
open(FILE, $file) || warn "Cannot open $file: $!\n";
while (<FILE>) {
/$pattern/i && push(@incs, $1);
}
if (defined @incs) {
$file =~ s/\.[^.]+$/.o/;
print MAKEFILE "$file: ";
&PrintWords(length($file) + 2, 0, @incs);
print MAKEFILE "\n";
undef @incs;
}
}
}
#
# &MakeDependsf90(f90 compiler); --- FORTRAN 90 dependency maker
#
sub MakeDependsf90 {
local($compiler) = &toLower(@_[0]);
local(@dependencies);
local(%filename);
local(@incs);
local(@modules);
local($objfile);
#
# Associate each module with the name of the file that contains it
#
foreach $file (<*.f90>) {
open(FILE, $file) || warn "Cannot open $file: $!\n";
while (<FILE>) {
/^\s*module\s+([^\s!]+)/i &&
($filename{&toLower($1)} = $file) =~ s/\.f90$/.o/;
}
}
#
# Print the dependencies of each file that has one or more include's or
# references one or more modules
#
foreach $file (<*.f90>) {
open(FILE, $file);
while (<FILE>) {
/^\s*include\s+["\']([^"\']+)["\']/i && push(@incs, $1);
/^\s*use\s+([^\s,!]+)/i && push(@modules, &toLower($1));
}
if (defined @incs || defined @modules) {
($objfile = $file) =~ s/\.f90$/.o/;
print MAKEFILE "$objfile: ";
undef @dependencies;
foreach $module (@modules) {
push(@dependencies, $filename{$module});
}
@dependencies = &uniq(sort(@dependencies));
&PrintWords(length($objfile) + 2, 0,
@dependencies, &uniq(sort(@incs)));
print MAKEFILE "\n";
undef @incs;
undef @modules;
#
# Cray F90 compiler
#
if ($compiler eq "cray") {
print MAKEFILE "\t\$(F90) \$(F90FLAGS) -c ";
foreach $depend (@dependencies) {
push(@modules, "-p", $depend);
}
push(@modules, $file);
&PrintWords(30, 1, @modules);
print MAKEFILE "\n";
undef @modules;
}
#
# ParaSoft F90 compiler
#
if ($compiler eq "parasoft") {
print MAKEFILE "\t\$(F90) \$(F90FLAGS) -c ";
foreach $depend (@dependencies) {
$depend =~ s/\.o$/.f90/;
push(@modules, "-module", $depend);
}
push(@modules, $file);
&PrintWords(30, 1, @modules);
print MAKEFILE "\n";
undef @modules;
}
}
}
}