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