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