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.6 kB

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