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.

c_check.pl 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #!/usr/bin/env perl
  2. #use File::Basename;
  3. # use File::Temp qw(tempfile);
  4. # Checking cross compile
  5. $hostos = `uname -s | sed -e s/\-.*//`; chop($hostos);
  6. $hostarch = `uname -m | sed -e s/i.86/x86/`;
  7. $hostarch = `uname -p` if ($hostos eq "AIX" || $hostos eq "SunOS");
  8. chop($hostarch);
  9. $hostarch = "x86_64" if ($hostarch eq "amd64");
  10. $hostarch = "arm" if ($hostarch ne "arm64" && $hostarch =~ /^arm.*/);
  11. $hostarch = "arm64" if ($hostarch eq "aarch64");
  12. $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
  13. $hostarch = "zarch" if ($hostarch eq "s390x");
  14. #$tmpf = new File::Temp( UNLINK => 1 );
  15. $binary = $ENV{"BINARY"};
  16. $makefile = shift(@ARGV);
  17. $config = shift(@ARGV);
  18. $compiler_name = shift(@ARGV);
  19. $flags = join(" ", @ARGV);
  20. # First, we need to know the target OS and compiler name
  21. $data = `$compiler_name $flags -E ctest.c`;
  22. if ($?) {
  23. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  24. die 1;
  25. }
  26. $cross_suffix = "";
  27. eval "use File::Basename";
  28. if ($@){
  29. warn "could not load PERL module File::Basename, emulating its functionality";
  30. my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
  31. if ($dirnam ne ".") {
  32. $cross_suffix .= $dirnam . "/";
  33. }
  34. my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
  35. if ($basnam =~ /([^\s]*-)(.*)/) {
  36. $cross_suffix .= $1;
  37. }
  38. } else {
  39. if (dirname($compiler_name) ne ".") {
  40. $cross_suffix .= dirname($compiler_name) . "/";
  41. }
  42. if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
  43. $cross_suffix .= $1;
  44. }
  45. }
  46. $compiler = "";
  47. $compiler = LSB if ($data =~ /COMPILER_LSB/);
  48. $compiler = CLANG if ($data =~ /COMPILER_CLANG/);
  49. $compiler = PGI if ($data =~ /COMPILER_PGI/);
  50. $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
  51. $compiler = INTEL if ($data =~ /COMPILER_INTEL/);
  52. $compiler = OPEN64 if ($data =~ /COMPILER_OPEN64/);
  53. $compiler = SUN if ($data =~ /COMPILER_SUN/);
  54. $compiler = IBM if ($data =~ /COMPILER_IBM/);
  55. $compiler = DEC if ($data =~ /COMPILER_DEC/);
  56. $compiler = FUJITSU if ($data =~ /COMPILER_FUJITSU/);
  57. $compiler = GCC if ($compiler eq "");
  58. $os = Linux if ($data =~ /OS_LINUX/);
  59. $os = FreeBSD if ($data =~ /OS_FREEBSD/);
  60. $os = NetBSD if ($data =~ /OS_NETBSD/);
  61. $os = OpenBSD if ($data =~ /OS_OPENBSD/);
  62. $os = DragonFly if ($data =~ /OS_DRAGONFLY/);
  63. $os = Darwin if ($data =~ /OS_DARWIN/);
  64. $os = SunOS if ($data =~ /OS_SUNOS/);
  65. $os = AIX if ($data =~ /OS_AIX/);
  66. $os = osf if ($data =~ /OS_OSF/);
  67. $os = WINNT if ($data =~ /OS_WINNT/);
  68. $os = CYGWIN_NT if ($data =~ /OS_CYGWIN_NT/);
  69. $os = Interix if ($data =~ /OS_INTERIX/);
  70. $os = Android if ($data =~ /OS_ANDROID/);
  71. $os = Haiku if ($data =~ /OS_HAIKU/);
  72. $architecture = x86 if ($data =~ /ARCH_X86/);
  73. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  74. $architecture = e2k if ($data =~ /ARCH_E2K/);
  75. $architecture = power if ($data =~ /ARCH_POWER/);
  76. $architecture = mips if ($data =~ /ARCH_MIPS/);
  77. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  78. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  79. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  80. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  81. $architecture = arm if ($data =~ /ARCH_ARM/);
  82. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  83. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  84. $architecture = riscv64 if ($data =~ /ARCH_RISCV64/);
  85. $architecture = loongarch64 if ($data =~ /ARCH_LOONGARCH64/);
  86. $architecture = csky if ($data =~ /ARCH_CSKY/);
  87. $defined = 0;
  88. if ($os eq "AIX") {
  89. $compiler_name .= " -maix32" if ($binary eq "32");
  90. $compiler_name .= " -maix64" if ($binary eq "64");
  91. $defined = 1;
  92. }
  93. if ($architecture eq "mips") {
  94. $compiler_name .= " -mabi=32";
  95. $defined = 1;
  96. }
  97. if ($architecture eq "mips64") {
  98. $compiler_name .= " -mabi=n32" if ($binary eq "32");
  99. $compiler_name .= " -mabi=64" if ($binary eq "64");
  100. $defined = 1;
  101. }
  102. if (($architecture eq "arm") || ($architecture eq "arm64")) {
  103. $defined = 1;
  104. }
  105. if ($architecture eq "zarch") {
  106. $defined = 1;
  107. $binary = 64;
  108. }
  109. if ($architecture eq "e2k") {
  110. $defined = 1;
  111. $binary = 64;
  112. }
  113. if ($architecture eq "alpha") {
  114. $defined = 1;
  115. $binary = 64;
  116. }
  117. if ($architecture eq "ia64") {
  118. $defined = 1;
  119. $binary = 64;
  120. }
  121. if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
  122. $defined = 1;
  123. $binary =32;
  124. }
  125. if ($architecture eq "riscv64") {
  126. $defined = 1;
  127. $binary = 64;
  128. }
  129. if ($architecture eq "loongarch64") {
  130. $defined = 1;
  131. $binary = 64;
  132. }
  133. if ($architecture eq "csky") {
  134. $defined = 1;
  135. $binary = 32;
  136. }
  137. if ($compiler eq "PGI") {
  138. $compiler_name .= " -tp p7" if ($binary eq "32");
  139. $compiler_name .= " -tp p7-64" if ($binary eq "64");
  140. $openmp = "-mp";
  141. $defined = 1;
  142. }
  143. if ($compiler eq "IBM") {
  144. $compiler_name .= " -q32" if ($binary eq "32");
  145. $compiler_name .= " -q64" if ($binary eq "64");
  146. $openmp = "-qsmp=omp";
  147. $defined = 1;
  148. }
  149. if ($compiler eq "INTEL") {
  150. $openmp = "-openmp";
  151. }
  152. if ($compiler eq "PATHSCALE") {
  153. $openmp = "-mp";
  154. }
  155. if ($compiler eq "OPEN64") {
  156. $openmp = "-mp";
  157. }
  158. if ($compiler eq "CLANG") {
  159. $openmp = "-fopenmp";
  160. }
  161. if ($compiler eq "GCC" || $compiler eq "LSB") {
  162. $openmp = "-fopenmp";
  163. }
  164. if ($compiler eq "FUJITSU") {
  165. $openmp = "-Kopenmp";
  166. }
  167. if ($defined == 0) {
  168. $compiler_name .= " -m32" if ($binary eq "32");
  169. $compiler_name .= " -m64" if ($binary eq "64");
  170. }
  171. # Do again
  172. $data = `$compiler_name $flags -E ctest.c`;
  173. if ($?) {
  174. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  175. die 1;
  176. }
  177. $have_msa = 0;
  178. if (($architecture eq "mips") || ($architecture eq "mips64")) {
  179. eval "use File::Temp qw(tempfile)";
  180. if ($@){
  181. warn "could not load PERL module File::Temp, so could not check MSA capatibility";
  182. } else {
  183. $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
  184. $code = '"addvi.b $w0, $w1, 1"';
  185. $msa_flags = "-mmsa -mfp64 -mload-store-pairs";
  186. print $tmpf "#include <msa.h>\n\n";
  187. print $tmpf "void main(void){ __asm__ volatile($code); }\n";
  188. $args = "$msa_flags -o $tmpf.o $tmpf";
  189. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  190. system(@cmd) == 0;
  191. if ($? != 0) {
  192. $have_msa = 0;
  193. } else {
  194. $have_msa = 1;
  195. }
  196. unlink("$tmpf.o");
  197. }
  198. }
  199. $no_lsx = 0;
  200. $no_lasx = 0;
  201. if (($architecture eq "loongarch64")) {
  202. eval "use File::Temp qw(tempfile)";
  203. if ($@){
  204. warn "could not load PERL module File::Temp, so could not check LSX and LASX capatibility";
  205. } else {
  206. $tmplsx = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
  207. $codelsx = '"vadd.b $vr0, $vr0, $vr0"';
  208. $lsx_flags = "-march=loongarch64";
  209. print $tmplsx "void main(void){ __asm__ volatile($codelsx); }\n";
  210. $args = "$lsx_flags -o $tmplsx.o $tmplsx";
  211. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  212. system(@cmd) == 0;
  213. if ($? != 0) {
  214. $no_lsx = 1;
  215. } else {
  216. $no_lsx = 0;
  217. }
  218. unlink("$tmplsx.o");
  219. $tmplasx = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
  220. $codelasx = '"xvadd.b $xr0, $xr0, $xr0"';
  221. $lasx_flags = "-march=loongarch64";
  222. print $tmplasx "void main(void){ __asm__ volatile($codelasx); }\n";
  223. $args = "$lasx_flags -o $tmplasx.o $tmplasx";
  224. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  225. system(@cmd) == 0;
  226. if ($? != 0) {
  227. $no_lasx = 1;
  228. } else {
  229. $no_lasx = 0;
  230. }
  231. unlink("$tmplasx.o");
  232. }
  233. }
  234. $architecture = x86 if ($data =~ /ARCH_X86/);
  235. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  236. $architecture = e2k if ($data =~ /ARCH_E2K/);
  237. $architecture = power if ($data =~ /ARCH_POWER/);
  238. $architecture = mips if ($data =~ /ARCH_MIPS/);
  239. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  240. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  241. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  242. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  243. $architecture = arm if ($data =~ /ARCH_ARM/);
  244. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  245. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  246. $architecture = loongarch64 if ($data =~ /ARCH_LOONGARCH64/);
  247. $architecture = csky if ($data =~ /ARCH_CSKY/);
  248. $binformat = bin32;
  249. $binformat = bin64 if ($data =~ /BINARY_64/);
  250. $no_avx512= 0;
  251. if (($architecture eq "x86") || ($architecture eq "x86_64")) {
  252. eval "use File::Temp qw(tempfile)";
  253. if ($@){
  254. warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
  255. $no_avx512 = 0;
  256. } else {
  257. # $tmpf = new File::Temp( UNLINK => 1 );
  258. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  259. $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
  260. print $fh "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
  261. $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
  262. if ($compiler eq "PGI") {
  263. $args = " -tp skylake -c -o $tmpf.o $tmpf";
  264. }
  265. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  266. system(@cmd) == 0;
  267. if ($? != 0) {
  268. $no_avx512 = 1;
  269. } else {
  270. $no_avx512 = 0;
  271. }
  272. unlink("$tmpf.o");
  273. }
  274. }
  275. $no_rv64gv= 0;
  276. if (($architecture eq "riscv64")) {
  277. eval "use File::Temp qw(tempfile)";
  278. if ($@){
  279. warn "could not load PERL module File::Temp, so could not check compiler compatibility with the RISCV vector extension";
  280. $no_rv64gv = 0;
  281. } else {
  282. # $tmpf = new File::Temp( UNLINK => 1 );
  283. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  284. $code = '"vsetvli zero, zero, e8, m1\n"';
  285. print $fh "int main(void){ __asm__ volatile($code); }\n";
  286. $args = " -march=rv64gv -c -o $tmpf.o $tmpf";
  287. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  288. system(@cmd) == 0;
  289. if ($? != 0) {
  290. $no_rv64gv = 1;
  291. } else {
  292. $no_rv64gv = 0;
  293. }
  294. unlink("$tmpf.o");
  295. }
  296. }
  297. $c11_atomics = 0;
  298. if ($data =~ /HAVE_C11/) {
  299. eval "use File::Temp qw(tempfile)";
  300. if ($@){
  301. warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
  302. $c11_atomics = 0;
  303. } else {
  304. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  305. print $fh "#include <stdatomic.h>\nint main(void){}\n";
  306. $args = " -c -o $tmpf.o $tmpf";
  307. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  308. system(@cmd) == 0;
  309. if ($? != 0) {
  310. $c11_atomics = 0;
  311. } else {
  312. $c11_atomics = 1;
  313. }
  314. unlink("$tmpf.o");
  315. }
  316. }
  317. if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
  318. $no_avx2 = 0;
  319. $oldgcc = 0;
  320. $data = `$compiler_name -dumpversion`;
  321. if ($data <= 4.6) {
  322. $no_avx2 = 1;
  323. $oldgcc = 1;
  324. }
  325. }
  326. $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
  327. $data =~ /globl\s([_\.]*)(.*)/;
  328. $need_fu = $1;
  329. $cross = 0;
  330. if ($architecture ne $hostarch) {
  331. $cross = 1;
  332. $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
  333. $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
  334. }
  335. $cross = 1 if ($os ne $hostos);
  336. $cross = 0 if (($os eq "Android") && ($hostos eq "Linux") && ($ENV{TERMUX_APP_PID} != ""));
  337. $openmp = "" if $ENV{USE_OPENMP} != 1;
  338. $linker_L = "";
  339. $linker_l = "";
  340. $linker_a = "";
  341. {
  342. $link = `$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`;
  343. $link =~ s/\-Y\sP\,/\-Y/g;
  344. @flags = split(/[\s\,\n]/, $link);
  345. # remove leading and trailing quotes from each flag.
  346. @flags = map {s/^['"]|['"]$//g; $_} @flags;
  347. foreach $flags (@flags) {
  348. if (
  349. ($flags =~ /^\-L/)
  350. && ($flags !~ /^-LIST:/)
  351. && ($flags !~ /^-LANG:/)
  352. ) {
  353. $linker_L .= $flags . " "
  354. }
  355. if ($flags =~ /^\-Y/) {
  356. $linker_L .= "-Wl,". $flags . " "
  357. }
  358. if ($flags =~ /^\--exclude-libs/) {
  359. $linker_L .= "-Wl,". $flags . " ";
  360. $flags="";
  361. }
  362. if (
  363. ($flags =~ /^\-l/)
  364. && ($flags !~ /gfortranbegin/)
  365. && ($flags !~ /frtbegin/)
  366. && ($flags !~ /pathfstart/)
  367. && ($flags !~ /numa/)
  368. && ($flags !~ /crt[0-9]/)
  369. && ($flags !~ /gcc/)
  370. && ($flags !~ /user32/)
  371. && ($flags !~ /kernel32/)
  372. && ($flags !~ /advapi32/)
  373. && ($flags !~ /shell32/)
  374. && ($flags !~ /omp/)
  375. && ($flags !~ /[0-9]+/)
  376. ) {
  377. $linker_l .= $flags . " "
  378. }
  379. $linker_a .= $flags . " " if $flags =~ /\.a$/;
  380. }
  381. }
  382. open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
  383. open(CONFFILE, "> $config" ) || die "Can't create $config";
  384. # print $data, "\n";
  385. print MAKEFILE "OSNAME=$os\n";
  386. print MAKEFILE "ARCH=$architecture\n";
  387. print MAKEFILE "C_COMPILER=$compiler\n";
  388. print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
  389. print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
  390. print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
  391. print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
  392. print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
  393. print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
  394. print MAKEFILE "CROSS=1\n" if $cross != 0;
  395. print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
  396. print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
  397. print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
  398. print MAKEFILE "NO_RV64GV=1\n" if $no_rv64gv eq 1;
  399. print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
  400. print MAKEFILE "NO_AVX2=1\n" if $no_avx2 eq 1;
  401. print MAKEFILE "OLDGCC=1\n" if $oldgcc eq 1;
  402. print MAKEFILE "NO_LSX=1\n" if $no_lsx eq 1;
  403. print MAKEFILE "NO_LASX=1\n" if $no_lasx eq 1;
  404. $os =~ tr/[a-z]/[A-Z]/;
  405. $architecture =~ tr/[a-z]/[A-Z]/;
  406. $compiler =~ tr/[a-z]/[A-Z]/;
  407. print CONFFILE "#define OS_$os\t1\n";
  408. print CONFFILE "#define ARCH_$architecture\t1\n";
  409. print CONFFILE "#define C_$compiler\t1\n";
  410. print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
  411. print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
  412. print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
  413. print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
  414. print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
  415. print CONFFILE "#define NO_LSX\t1\n" if $no_lsx eq 1;
  416. print CONFFILE "#define NO_LASX\t1\n" if $no_lasx eq 1;
  417. if ($os eq "LINUX") {
  418. # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
  419. # if ($pthread[2] ne "") {
  420. # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
  421. # } else {
  422. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  423. # }
  424. } else {
  425. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  426. }
  427. close(MAKEFILE);
  428. close(CONFFILE);