riscv64: Add RISC-V target
[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", "ftn", "crayftn");
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 =~ /Cray/) {
80
81             $vendor = CRAY;
82             $openmp = "-fopenmp";
83
84         } elsif ($data =~ /GNU/ || $data =~ /GCC/ ) {
85
86             $data =~ s/\(+.*?\)+//g;
87             $data =~ /(\d+)\.(\d+).(\d+)/;
88             $major = $1;
89             $minor = $2;
90
91             if ($major >= 4) {
92                 $vendor = GFORTRAN;
93                 $openmp = "-fopenmp";
94             } else {
95                 if ($compiler =~ /flang/) {
96                     $vendor = FLANG;
97                     $openmp = "-fopenmp";
98             } elsif ($compiler =~ /ifx/) {
99                     $vendor = INTEL;
100                     $openmp = "-fopenmp";
101             } elsif ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
102                     $vendor = PGI;
103                     $openmp = "-mp";
104                 } else {
105                     $vendor = G77;
106                     $openmp = "";
107                 }
108             }
109
110         }
111
112         if ($data =~ /g95/) {
113             $vendor = G95;
114             $openmp = "";
115         }
116
117         if ($data =~ /Intel/) {
118             $vendor = INTEL;
119             $openmp = "-fopenmp";
120         }
121
122         if ($data =~ /Sun Fortran/) {
123             $vendor = SUN;
124             $openmp = "-xopenmp=parallel";
125         }
126
127         if ($data =~ /PathScale/) {
128             $vendor = PATHSCALE;
129             $openmp = "-openmp";
130         }
131
132         if ($data =~ /Open64/) {
133             $vendor = OPEN64;
134             $openmp = "-mp";
135         }
136
137         if ($data =~ /PGF/ || $data =~ /NVF/) {
138             $vendor = PGI;
139             $openmp = "-mp";
140         }
141
142         if ($data =~ /IBM XL/) {
143             $vendor = IBM;
144             $openmp = "-openmp";
145         }
146
147         if ($data =~ /NAG/) {
148             $vendor = NAG;
149             $openmp = "-openmp";
150         }
151
152         # for embedded underscore name, e.g. zho_ge, it may append 2 underscores.
153         $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.s && rm -f ftest3.s`;
154         if ($data eq "") {
155                 $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.c && rm -f ftest3.c`;
156         }
157         if ($data =~ / zho_ge__/) {
158             $need2bu       = 1;
159         }
160         if ($vendor =~ /G95/) {
161           if ($ENV{NO_LAPACKE} != 1) {
162                 $need2bu = "";
163           }
164         }
165     }
166
167     if ($vendor eq "") {
168
169         if ($compiler =~ /g77/) {
170             $vendor = G77;
171             $bu       = "_";
172             $openmp = "";
173         }
174
175         if ($compiler =~ /g95/) {
176             $vendor = G95;
177             $bu       = "_";
178             $openmp = "";
179         }
180
181         if ($compiler =~ /gfortran/) {
182             $vendor = GFORTRAN;
183             $bu       = "_";
184             $openmp = "-fopenmp";
185         }
186
187         if ($compiler =~ /ifort/ || $compiler =~ /ifx/) {
188             $vendor = INTEL;
189             $bu       = "_";
190             $openmp = "-fopenmp";
191         }
192
193         if ($compiler =~ /pathf/) {
194             $vendor = PATHSCALE;
195             $bu       = "_";
196             $openmp = "-mp";
197         }
198
199         if ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
200             $vendor = PGI;
201             $bu       = "_";
202             $openmp = "-mp";
203         }
204
205         if ($compiler =~ /ftn/) {
206             $vendor = PGI;
207             $bu       = "_";
208             $openmp = "-openmp";
209         }
210
211         if ($compiler =~ /frt/) {
212             $vendor = FUJITSU;
213             $bu       = "_";
214             $openmp = "-openmp";
215         }
216
217         if ($compiler =~ /sunf77|sunf90|sunf95/) {
218             $vendor = SUN;
219             $bu       = "_";
220             $openmp = "-xopenmp=parallel";
221         }
222
223         if ($compiler =~ /ppuf/) {
224             $vendor = IBM;
225             $openmp = "-openmp";
226         }
227
228         if ($compiler =~ /xlf/) {
229             $vendor = IBM;
230             $openmp = "-openmp";
231         }
232
233         if ($compiler =~ /open64/) {
234             $vendor = OPEN64;
235             $openmp = "-mp";
236         }
237
238         if ($compiler =~ /flang/) {
239             $vendor = FLANG;
240             $bu     = "_";
241             $openmp = "-fopenmp";
242         }
243
244         if ($compiler =~ /nagfor/) {
245             $vendor = NAG;
246             $bu     = "_";
247             $openmp = "-openmp";
248         }
249
250         if ($vendor eq "") {
251             $nofortran = 1;
252             $compiler = "gfortran";
253             $vendor = GFORTRAN;
254             $bu       = "_";
255             $openmp = "";
256         }
257
258     }
259 }
260
261 $data = `which $compiler_bin > /dev/null 2> /dev/null`;
262
263 if (!$?) {
264
265     $binary = $ENV{"BINARY"};
266
267     $openmp = "" if $ENV{USE_OPENMP} != 1;
268
269     if ($binary == 32) {
270         $link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
271         if ($?) {
272             $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
273         }
274         # for AIX
275         if ($?) {
276             $link = `$compiler $openmp -maix32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
277         }
278        #For gfortran MIPS
279         if ($?) {
280     $mips_data = `$compiler_bin -E -dM - < /dev/null`;
281     if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
282         $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
283     } else {
284         $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
285     }
286         }
287         $binary = "" if ($?);
288     }
289
290     if ($binary == 64) {
291         $link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
292         if ($?) {
293             $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
294         }
295         # for AIX
296         if ($?) {
297             $link = `$compiler $openmp -maix64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
298         }
299        #For gfortran MIPS
300         if ($?) {
301             $link = `$compiler $openmp -mabi=64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
302         }
303        #For nagfor
304         if ($?) {
305             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
306         }
307         $binary = "" if ($?);
308     }
309     if ($binary eq "") {
310         $link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
311     }
312 }
313
314 if ( $vendor eq "NAG") {
315             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
316     }
317 if ( $vendor eq "CRAY") {
318             $link = `$compiler $openmp -hnopattern ftest2.f 2>&1 && rm -f a.out a.exe`;
319     }
320 $linker_L = "";
321 $linker_l = "";
322 $linker_a = "";
323
324 if ($link ne "") {
325
326     $link =~ s/\-Y\sP\,/\-Y/g;
327     
328     $link =~ s/\-R\s*/\-rpath\%/g;
329
330     $link =~ s/\-rpath\s+/\-rpath\%/g;
331
332     $link =~ s/\-rpath-link\s+/\-rpath-link\%/g;
333
334     @flags = split(/[\s\,\n]/, $link);
335     # remove leading and trailing quotes from each flag.
336     @flags = map {s/^['"]|['"]$//g; $_} @flags;
337
338     foreach $flags (@flags) {
339         if (
340             ($flags =~ /^\-L/)
341             && ($flags !~ /^-LIST:/)
342             && ($flags !~ /^-LANG:/)
343             ) {
344             $linker_L .= $flags . " ";
345         }
346
347         if ($flags =~ /^\-Y/) {
348             next if ($hostos eq 'SunOS');
349             $linker_L .= "-Wl,". $flags . " ";
350         }
351
352         if ($flags =~ /^\--exclude-libs/) {
353             $linker_L .= "-Wl,". $flags . " ";
354             $flags="";
355         }
356
357
358         if ($flags =~ /^\-rpath\%/) {
359             $flags =~ s/\%/\,/g;
360             $linker_L .= "-Wl,". $flags . " " ;
361         }
362
363         if ($flags =~ /^\-rpath-link\%/) {
364             $flags =~ s/\%/\,/g;
365             $linker_L .= "-Wl,". $flags . " " ;
366         }
367         if ($flags =~ /-lgomp/ && $ENV{"CC"} =~ /clang/) {
368             $flags = "-lomp";
369         }
370
371         if (
372             ($flags =~ /^\-l/)
373             && ($flags !~ /ibrary/)
374             && ($flags !~ /gfortranbegin/)
375             && ($flags !~ /flangmain/)
376             && ($flags !~ /frtbegin/)
377             && ($flags !~ /pathfstart/)
378             && ($flags !~ /crt[0-9]/)
379             && ($flags !~ /gcc/)
380             && ($flags !~ /user32/)
381             && ($flags !~ /kernel32/)
382             && ($flags !~ /advapi32/)
383             && ($flags !~ /shell32/)
384             && ($flags !~ /omp/ || ($vendor !~ /PGI/ && $vendor !~ /FUJITSU/ && $flags =~ /omp/))
385             && ($flags !~ /[0-9]+/ || ($vendor == FUJITSU && $flags =~ /^-lfj90/))
386                 && ($flags !~ /^\-l$/)
387             ) {
388             $linker_l .= $flags . " ";
389         }
390
391         if ( $flags =~ /quickfit.o/ && $vendor == NAG) {
392             $linker_l .= $flags . " ";
393         }
394         if ( $flags =~ /safefit.o/ && $vendor == NAG) {
395             $linker_l .= $flags . " ";
396         }
397         if ( $flags =~ /thsafe.o/ && $vendor == NAG) {
398             $linker_l .= $flags . " ";
399         }
400
401         $linker_a .= $flags . " " if $flags =~ /\.a$/;
402     }
403
404 }
405
406 if ($vendor eq "FLANG"){
407     $linker_a .= "-lflang"
408 }
409
410 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
411 open(CONFFILE, ">> $config"  ) || die "Can't append $config";
412
413 print MAKEFILE "F_COMPILER=$vendor\n";
414 print MAKEFILE "FC=$compiler\n";
415 print MAKEFILE "BU=$bu\n" if $bu ne "";
416 print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
417
418 print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
419 print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
420 print CONFFILE "#define NEED2UNDERSCORES\t1\n" if $need2bu ne "";
421
422 print MAKEFILE "NEED2UNDERSCORES=1\n" if $need2bu ne "";
423
424 if (($linker_l ne "") || ($linker_a ne "")) {
425     print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
426 }
427
428 close(MAKEFILE);
429 close(CONFFILE);