You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

f_check.pl 9.4 kB

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