Merge pull request #3635 from martin-frbg/issue3634
[platform/upstream/openblas.git] / f_check.pl
1 #!/usr/bin/env perl
2
3 $hostos   = `uname -s | sed -e s/\-.*//`;    chop($hostos);
4
5 #
6 # 1. Not specified
7 #   1.1 Automatically detect, then check compiler
8 #   1.2 If no fortran compiler is detected, gfortran is default with NOFORTRAN definition
9 # 2. Specified
10 #   2.1 If path is correct, check compiler
11 #   2.2 If path is not correct, but still valid compiler name, force setting
12 #     2.2.2 Path is not correct, invalid compiler name, then gfortran is default with NOFORTRAN definition
13 #
14
15 $makefile = shift(@ARGV);
16 $config   = shift(@ARGV);
17
18 $nofortran = 0;
19
20 $compiler = join(" ", @ARGV);
21 $compiler_bin = shift(@ARGV);
22  
23 # f77 is too ambiguous
24 $compiler = "" if $compiler eq "f77";
25
26 @path = split(/:/, $ENV{"PATH"});
27
28 if ($compiler eq "") {
29
30     @lists = ("gfortran", "g95", "frt", "fort", "openf90", "openf95",
31               "sunf77", "sunf90", "sunf95",
32               "xlf95", "xlf90", "xlf",
33               "ppuf77", "ppuf95", "ppuf90", "ppuxlf",
34               "pathf90", "pathf95",
35               "pgf95", "pgf90", "pgf77", "pgfortran", "nvfortran",
36               "flang", "egfortran",
37               "ifort", "nagfor", "ifx");
38
39 OUTER:
40     foreach $lists (@lists) {
41         foreach $path (@path) {
42             if (-x $path . "/" . $lists) {
43                 $compiler = $lists;
44                 $compiler_bin = $lists;
45                 last OUTER;
46             }
47         }
48     }
49
50 }
51
52 if ($compiler eq "") {
53
54     $nofortran = 1;
55     $compiler = "gfortran";
56     $vendor = GFORTRAN;
57     $bu       = "_";
58
59 } else {
60
61     $data = `which $compiler_bin > /dev/null 2> /dev/null`;
62     $vendor = "";
63
64     if (!$?) {
65
66         $data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.s && rm -f ftest.s`;
67         if ($data eq "") {
68                 $data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.c && rm -f ftest.c`;
69         }
70         if ($data =~ /zhoge_/) {
71             $bu       = "_";
72         }
73
74         if ($data =~ /Fujitsu/) {
75
76             $vendor = FUJITSU;
77             $openmp = "-Kopenmp";
78
79         } elsif ($data =~ /GNU/ || $data =~ /GCC/ ) {
80
81             $data =~ s/\(+.*?\)+//g;
82             $data =~ /(\d+)\.(\d+).(\d+)/;
83             $major = $1;
84             $minor = $2;
85
86             if ($major >= 4) {
87                 $vendor = GFORTRAN;
88                 $openmp = "-fopenmp";
89             } else {
90                 if ($compiler =~ /flang/) {
91                     $vendor = FLANG;
92                     $openmp = "-fopenmp";
93             } elsif ($compiler =~ /ifx/) {
94                     $vendor = INTEL;
95                     $openmp = "-fopenmp";
96             } elsif ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
97                     $vendor = PGI;
98                     $openmp = "-mp";
99                 } else {
100                     $vendor = G77;
101                     $openmp = "";
102                 }
103             }
104
105         }
106
107         if ($data =~ /g95/) {
108             $vendor = G95;
109             $openmp = "";
110         }
111
112         if ($data =~ /Intel/) {
113             $vendor = INTEL;
114             $openmp = "-fopenmp";
115         }
116
117         if ($data =~ /Sun Fortran/) {
118             $vendor = SUN;
119             $openmp = "-xopenmp=parallel";
120         }
121
122         if ($data =~ /PathScale/) {
123             $vendor = PATHSCALE;
124             $openmp = "-openmp";
125         }
126
127         if ($data =~ /Open64/) {
128             $vendor = OPEN64;
129             $openmp = "-mp";
130         }
131
132         if ($data =~ /PGF/ || $data =~ /NVF/) {
133             $vendor = PGI;
134             $openmp = "-mp";
135         }
136
137         if ($data =~ /IBM XL/) {
138             $vendor = IBM;
139             $openmp = "-openmp";
140         }
141
142         if ($data =~ /NAG/) {
143             $vendor = NAG;
144             $openmp = "-openmp";
145         }
146
147         # for embedded underscore name, e.g. zho_ge, it may append 2 underscores.
148         $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.s && rm -f ftest3.s`;
149         if ($data eq "") {
150                 $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.c && rm -f ftest3.c`;
151         }
152         if ($data =~ / zho_ge__/) {
153             $need2bu       = 1;
154         }
155         if ($vendor =~ /G95/) {
156           if ($ENV{NO_LAPACKE} != 1) {
157                 $need2bu = "";
158           }
159         }
160     }
161
162     if ($vendor eq "") {
163
164         if ($compiler =~ /g77/) {
165             $vendor = G77;
166             $bu       = "_";
167             $openmp = "";
168         }
169
170         if ($compiler =~ /g95/) {
171             $vendor = G95;
172             $bu       = "_";
173             $openmp = "";
174         }
175
176         if ($compiler =~ /gfortran/) {
177             $vendor = GFORTRAN;
178             $bu       = "_";
179             $openmp = "-fopenmp";
180         }
181
182         if ($compiler =~ /ifort/ || $compiler =~ /ifx/) {
183             $vendor = INTEL;
184             $bu       = "_";
185             $openmp = "-fopenmp";
186         }
187
188         if ($compiler =~ /pathf/) {
189             $vendor = PATHSCALE;
190             $bu       = "_";
191             $openmp = "-mp";
192         }
193
194         if ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
195             $vendor = PGI;
196             $bu       = "_";
197             $openmp = "-mp";
198         }
199
200         if ($compiler =~ /ftn/) {
201             $vendor = PGI;
202             $bu       = "_";
203             $openmp = "-openmp";
204         }
205
206         if ($compiler =~ /frt/) {
207             $vendor = FUJITSU;
208             $bu       = "_";
209             $openmp = "-openmp";
210         }
211
212         if ($compiler =~ /sunf77|sunf90|sunf95/) {
213             $vendor = SUN;
214             $bu       = "_";
215             $openmp = "-xopenmp=parallel";
216         }
217
218         if ($compiler =~ /ppuf/) {
219             $vendor = IBM;
220             $openmp = "-openmp";
221         }
222
223         if ($compiler =~ /xlf/) {
224             $vendor = IBM;
225             $openmp = "-openmp";
226         }
227
228         if ($compiler =~ /open64/) {
229             $vendor = OPEN64;
230             $openmp = "-mp";
231         }
232
233         if ($compiler =~ /flang/) {
234             $vendor = FLANG;
235             $bu     = "_";
236             $openmp = "-fopenmp";
237         }
238
239         if ($compiler =~ /nagfor/) {
240             $vendor = NAG;
241             $bu     = "_";
242             $openmp = "-openmp";
243         }
244
245         if ($vendor eq "") {
246             $nofortran = 1;
247             $compiler = "gfortran";
248             $vendor = GFORTRAN;
249             $bu       = "_";
250             $openmp = "";
251         }
252
253     }
254 }
255
256 $data = `which $compiler_bin > /dev/null 2> /dev/null`;
257
258 if (!$?) {
259
260     $binary = $ENV{"BINARY"};
261
262     $openmp = "" if $ENV{USE_OPENMP} != 1;
263
264     if ($binary == 32) {
265         $link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
266         if ($?) {
267             $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
268         }
269         # for AIX
270         if ($?) {
271             $link = `$compiler $openmp -maix32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
272         }
273        #For gfortran MIPS
274         if ($?) {
275     $mips_data = `$compiler_bin -E -dM - < /dev/null`;
276     if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
277         $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
278     } else {
279         $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
280     }
281         }
282         $binary = "" if ($?);
283     }
284
285     if ($binary == 64) {
286         $link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
287         if ($?) {
288             $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
289         }
290         # for AIX
291         if ($?) {
292             $link = `$compiler $openmp -maix64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
293         }
294        #For gfortran MIPS
295         if ($?) {
296             $link = `$compiler $openmp -mabi=64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
297         }
298        #For nagfor
299         if ($?) {
300             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
301         }
302         $binary = "" if ($?);
303     }
304     if ($binary eq "") {
305         $link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
306     }
307 }
308
309 if ( $vendor eq "NAG") {
310             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
311     }
312 $linker_L = "";
313 $linker_l = "";
314 $linker_a = "";
315
316 if ($link ne "") {
317
318     $link =~ s/\-Y\sP\,/\-Y/g;
319     
320     $link =~ s/\-R\s*/\-rpath\%/g;
321
322     $link =~ s/\-rpath\s+/\-rpath\%/g;
323
324     $link =~ s/\-rpath-link\s+/\-rpath-link\%/g;
325
326     @flags = split(/[\s\,\n]/, $link);
327     # remove leading and trailing quotes from each flag.
328     @flags = map {s/^['"]|['"]$//g; $_} @flags;
329
330     foreach $flags (@flags) {
331         if (
332             ($flags =~ /^\-L/)
333             && ($flags !~ /^-LIST:/)
334             && ($flags !~ /^-LANG:/)
335             ) {
336             $linker_L .= $flags . " ";
337         }
338
339         if ($flags =~ /^\-Y/) {
340             next if ($hostos eq 'SunOS');
341             $linker_L .= "-Wl,". $flags . " ";
342         }
343
344         if ($flags =~ /^\--exclude-libs/) {
345             $linker_L .= "-Wl,". $flags . " ";
346             $flags="";
347         }
348
349
350         if ($flags =~ /^\-rpath\%/) {
351             $flags =~ s/\%/\,/g;
352             $linker_L .= "-Wl,". $flags . " " ;
353         }
354
355         if ($flags =~ /^\-rpath-link\%/) {
356             $flags =~ s/\%/\,/g;
357             $linker_L .= "-Wl,". $flags . " " ;
358         }
359         if ($flags =~ /-lgomp/ && $ENV{"CC"} =~ /clang/) {
360             $flags = "-lomp";
361         }
362
363         if (
364             ($flags =~ /^\-l/)
365             && ($flags !~ /ibrary/)
366             && ($flags !~ /gfortranbegin/)
367             && ($flags !~ /flangmain/)
368             && ($flags !~ /frtbegin/)
369             && ($flags !~ /pathfstart/)
370             && ($flags !~ /crt[0-9]/)
371             && ($flags !~ /gcc/)
372             && ($flags !~ /user32/)
373             && ($flags !~ /kernel32/)
374             && ($flags !~ /advapi32/)
375             && ($flags !~ /shell32/)
376             && ($flags !~ /omp/ || ($vendor !~ /PGI/ && $vendor !~ /FUJITSU/ && $flags =~ /omp/))
377             && ($flags !~ /[0-9]+/ || ($vendor == FUJITSU && $flags =~ /^-lfj90/))
378                 && ($flags !~ /^\-l$/)
379             ) {
380             $linker_l .= $flags . " ";
381         }
382
383         if ( $flags =~ /quickfit.o/ && $vendor == NAG) {
384             $linker_l .= $flags . " ";
385         }
386         if ( $flags =~ /safefit.o/ && $vendor == NAG) {
387             $linker_l .= $flags . " ";
388         }
389         if ( $flags =~ /thsafe.o/ && $vendor == NAG) {
390             $linker_l .= $flags . " ";
391         }
392
393         $linker_a .= $flags . " " if $flags =~ /\.a$/;
394     }
395
396 }
397
398 if ($vendor eq "FLANG"){
399     $linker_a .= "-lflang"
400 }
401
402 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
403 open(CONFFILE, ">> $config"  ) || die "Can't append $config";
404
405 print MAKEFILE "F_COMPILER=$vendor\n";
406 print MAKEFILE "FC=$compiler\n";
407 print MAKEFILE "BU=$bu\n" if $bu ne "";
408 print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
409
410 print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
411 print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
412 print CONFFILE "#define NEED2UNDERSCORES\t1\n" if $need2bu ne "";
413
414 print MAKEFILE "NEED2UNDERSCORES=1\n" if $need2bu ne "";
415
416 if (($linker_l ne "") || ($linker_a ne "")) {
417     print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
418 }
419
420 close(MAKEFILE);
421 close(CONFFILE);