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