1#!/usr/bin/perl
2
3#
4# 1. Not specified
5#   1.1 Automatically detect, then check compiler
6#   1.2 If no fortran compiler is detected, g77 is default with NOFORTRAN definition
7# 2. Specified
8#   2.1 If path is correct, check compiler
9#   2.2 If path is not correct, but still valid compiler name, force setting
10#     2.2.2 Path is not correct, invalid compiler name, then g77 is default with NOFORTRAN definition
11#
12
13$makefile = shift(@ARGV);
14$config   = shift(@ARGV);
15
16$nofortran = 0;
17
18$compiler = join(" ", @ARGV);
19
20# f77 is too ambiguous
21$compiler = "" if $compiler eq "f77";
22
23@path = split(/:/, $ENV{"PATH"});
24
25if ($compiler eq "") {
26
27    @lists = ("f77", "g77", "g95", "gfortran", "frt", "fort", "openf90", "openf95",
28	      "sunf77", "sunf90", "sunf95",
29              "xlf95", "xlf90", "xlf",
30              "ppuf77", "ppuf95", "ppuf90", "ppuxlf",
31	      "pathf90", "pathf95",
32	      "pgf95", "pgf90", "pgf77",
33              "ifort");
34
35    foreach $lists (@lists) {
36        foreach $path (@path) {
37            if (-f $path . "/" . $lists) {
38                $compiler = $lists;
39                break;
40            }
41        }
42    }
43
44}
45
46if ($compiler eq "") {
47
48    $nofortran = 1;
49    $compiler = "g77";
50    $vendor = G77;
51    $bu       = "_";
52
53} else {
54
55    $data = `which $compiler > /dev/null 2> /dev/null`;
56    $vendor = "";
57
58    if (!$?) {
59
60	$data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.s && rm -f ftest.s`;
61
62	if ($data =~ /zhoge_/) {
63	    $bu       = "_";
64	}
65
66	if ($data =~ /GNU/) {
67
68	    $data =~ /(\d)\.(\d).(\d)/;
69	    $major = $1;
70	    $minor = $2;
71
72	    if ($major >= 4) {
73		$vendor = GFORTRAN;
74		$openmp = "-fopenmp";
75	    } else {
76		$vendor = G77;
77		$openmp = "";
78	    }
79
80	}
81
82	if ($data =~ /g95/) {
83	    $vendor = G95;
84	    $openmp = "";
85	}
86
87	if ($data =~ /Intel/) {
88	    $vendor = INTEL;
89	    $openmp = "-openmp";
90	}
91
92        if ($data =~ /Sun Fortran/) {
93            $vendor = SUN;
94	    $openmp = "-xopenmp=parallel";
95        }
96
97	if ($data =~ /PathScale/) {
98	    $vendor = PATHSCALE;
99	    $openmp = "-openmp";
100	}
101
102	if ($data =~ /Open64/) {
103	    $vendor = OPEN64;
104	    $openmp = "-mp";
105	}
106
107	if ($data =~ /PGF/) {
108	    $vendor = PGI;
109	    $openmp = "-mp";
110	}
111
112	if ($data =~ /IBM/) {
113	    $vendor = IBM;
114	    $openmp = "-openmp";
115	}
116    }
117
118    if ($vendor eq "") {
119
120	if ($compiler =~ /g77/) {
121	    $vendor = G77;
122	    $bu       = "_";
123	    $openmp = "";
124	}
125
126	if ($compiler =~ /g95/) {
127	    $vendor = G95;
128	    $bu       = "_";
129	    $openmp = "";
130	}
131
132	if ($compiler =~ /gfortran/) {
133	    $vendor = GFORTRAN;
134	    $bu       = "_";
135	    $openmp = "-fopenmp";
136	}
137
138	if ($compiler =~ /ifort/) {
139	    $vendor = INTEL;
140	    $bu       = "_";
141	    $openmp = "-openmp";
142	}
143
144	if ($compiler =~ /pathf/) {
145	    $vendor = PATHSCALE;
146	    $bu       = "_";
147	    $openmp = "-mp";
148	}
149
150	if ($compiler =~ /pgf/) {
151	    $vendor = PGI;
152	    $bu       = "_";
153	    $openmp = "-mp";
154	}
155
156	if ($compiler =~ /ftn/) {
157	    $vendor = PGI;
158	    $bu       = "_";
159	    $openmp = "-openmp";
160	}
161
162	if ($compiler =~ /frt/) {
163	    $vendor = FUJITSU;
164	    $bu       = "_";
165	    $openmp = "-openmp";
166	}
167
168	if ($compiler =~ /sunf77|sunf90|sunf95/) {
169	    $vendor = SUN;
170	    $bu       = "_";
171	    $openmp = "-xopenmp=parallel";
172	}
173
174	if ($compiler =~ /ppuf/) {
175	    $vendor = IBM;
176	    $openmp = "-openmp";
177	}
178
179	if ($compiler =~ /xlf/) {
180	    $vendor = IBM;
181	    $openmp = "-openmp";
182	}
183
184	if ($compiler =~ /open64/) {
185	    $vendor = OPEN64;
186	    $openmp = "-mp";
187	}
188
189	if ($vendor eq "") {
190	    $nofortran = 1;
191	    $compiler = "g77";
192	    $vendor = G77;
193	    $bu       = "_";
194	    $openmp = "";
195	}
196
197    }
198}
199
200$data = `which $compiler > /dev/null 2> /dev/null`;
201
202if (!$?) {
203
204    $binary = $ENV{"BINARY"};
205
206    $openmp = "" if $ENV{USE_OPENMP} != 1;
207
208    if ($binary == 32) {
209	$link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
210	if ($?) {
211	    $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
212	}
213	$binary = "" if ($?);
214    }
215
216    if ($binary == 64) {
217	$link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
218	if ($?) {
219	    $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
220	}
221	$binary = "" if ($?);
222    }
223
224    if ($binary eq "") {
225	$link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
226    }
227}
228
229$linker_L = "";
230$linker_l = "";
231$linker_a = "";
232
233if ($link ne "") {
234
235    $link =~ s/\-Y\sP\,/\-Y/g;
236
237    $link =~ s/\-rpath\s+/\-rpath\@/g;
238
239    @flags = split(/[\s\,\n]/, $link);
240    # remove leading and trailing quotes from each flag.
241    @flags = map {s/^['"]|['"]$//g; $_} @flags;
242
243    foreach $flags (@flags) {
244	if (
245	    ($flags =~ /^\-L/)
246	    && ($flags !~ /^-LIST:/)
247	    && ($flags !~ /^-LANG:/)
248	    ) {
249	    if ($vendor eq "PGI") {
250		$flags =~ s/lib$/libso/;
251	    }
252	    $linker_L .= $flags . " ";
253	}
254
255	if ($flags =~ /^\-Y/) {
256	    $linker_L .= "-Wl,". $flags . " ";
257	    }
258
259	if ($flags =~ /^\-rpath/) {
260	    $flags =~ s/\@/\,/g;
261	    if ($vendor eq "PGI") {
262		$flags =~ s/lib$/libso/;
263	    }
264	    $linker_L .= "-Wl,". $flags . " " ;
265	}
266
267	if (
268	    ($flags =~ /^\-l/)
269	    && ($flags !~ /gfortranbegin/)
270	    && ($flags !~ /frtbegin/)
271	    && ($flags !~ /pathfstart/)
272	    && ($flags !~ /numa/)
273	    && ($flags !~ /crt[0-9]/)
274	    && ($flags !~ /gcc/)
275	    && ($flags !~ /user32/)
276	    && ($flags !~ /kernel32/)
277	    && ($flags !~ /advapi32/)
278	    && ($flags !~ /shell32/)
279		&& ($flags !~ /^\-l$/)
280	    ) {
281	    $linker_l .= $flags . " ";
282	}
283
284	$linker_a .= $flags . " " if $flags =~ /\.a$/;
285    }
286
287}
288
289if ($vendor eq "INTEL"){
290    $linker_a .= "-lgfortran"
291}
292
293open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
294open(CONFFILE, ">> $config"  ) || die "Can't append $config";
295
296print MAKEFILE "F_COMPILER=$vendor\n";
297print MAKEFILE "FC=$compiler\n";
298print MAKEFILE "BU=$bu\n" if $bu ne "";
299print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
300
301print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
302print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
303
304if (($linker_l ne "") || ($linker_a ne "")) {
305    print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
306}
307
308close(MAKEFILE);
309close(CONFFILE);
310