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 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. $defined = 0;
  87. if ($os eq "AIX") {
  88. $compiler_name .= " -maix32" if ($binary eq "32");
  89. $compiler_name .= " -maix64" if ($binary eq "64");
  90. $defined = 1;
  91. }
  92. if ($architecture eq "mips") {
  93. $compiler_name .= " -mabi=32";
  94. $defined = 1;
  95. }
  96. if ($architecture eq "mips64") {
  97. $compiler_name .= " -mabi=n32" if ($binary eq "32");
  98. $compiler_name .= " -mabi=64" if ($binary eq "64");
  99. $defined = 1;
  100. }
  101. if (($architecture eq "arm") || ($architecture eq "arm64")) {
  102. $defined = 1;
  103. }
  104. if ($architecture eq "zarch") {
  105. $defined = 1;
  106. $binary = 64;
  107. }
  108. if ($architecture eq "e2k") {
  109. $defined = 1;
  110. $binary = 64;
  111. }
  112. if ($architecture eq "alpha") {
  113. $defined = 1;
  114. $binary = 64;
  115. }
  116. if ($architecture eq "ia64") {
  117. $defined = 1;
  118. $binary = 64;
  119. }
  120. if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
  121. $defined = 1;
  122. $binary =32;
  123. }
  124. if ($architecture eq "riscv64") {
  125. $defined = 1;
  126. $binary = 64;
  127. }
  128. if ($architecture eq "loongarch64") {
  129. $defined = 1;
  130. $binary = 64;
  131. }
  132. if ($compiler eq "PGI") {
  133. $compiler_name .= " -tp p7" if ($binary eq "32");
  134. $compiler_name .= " -tp p7-64" if ($binary eq "64");
  135. $openmp = "-mp";
  136. $defined = 1;
  137. }
  138. if ($compiler eq "IBM") {
  139. $compiler_name .= " -q32" if ($binary eq "32");
  140. $compiler_name .= " -q64" if ($binary eq "64");
  141. $openmp = "-qsmp=omp";
  142. $defined = 1;
  143. }
  144. if ($compiler eq "INTEL") {
  145. $openmp = "-openmp";
  146. }
  147. if ($compiler eq "PATHSCALE") {
  148. $openmp = "-mp";
  149. }
  150. if ($compiler eq "OPEN64") {
  151. $openmp = "-mp";
  152. }
  153. if ($compiler eq "CLANG") {
  154. $openmp = "-fopenmp";
  155. }
  156. if ($compiler eq "GCC" || $compiler eq "LSB") {
  157. $openmp = "-fopenmp";
  158. }
  159. if ($compiler eq "FUJITSU") {
  160. $openmp = "-Kopenmp";
  161. }
  162. if ($defined == 0) {
  163. $compiler_name .= " -m32" if ($binary eq "32");
  164. $compiler_name .= " -m64" if ($binary eq "64");
  165. }
  166. # Do again
  167. $data = `$compiler_name $flags -E ctest.c`;
  168. if ($?) {
  169. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  170. die 1;
  171. }
  172. $have_msa = 0;
  173. if (($architecture eq "mips") || ($architecture eq "mips64")) {
  174. eval "use File::Temp qw(tempfile)";
  175. if ($@){
  176. warn "could not load PERL module File::Temp, so could not check MSA capatibility";
  177. } else {
  178. $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
  179. $code = '"addvi.b $w0, $w1, 1"';
  180. $msa_flags = "-mmsa -mfp64 -mload-store-pairs";
  181. print $tmpf "#include <msa.h>\n\n";
  182. print $tmpf "void main(void){ __asm__ volatile($code); }\n";
  183. $args = "$msa_flags -o $tmpf.o $tmpf";
  184. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  185. system(@cmd) == 0;
  186. if ($? != 0) {
  187. $have_msa = 0;
  188. } else {
  189. $have_msa = 1;
  190. }
  191. unlink("$tmpf.o");
  192. }
  193. }
  194. $architecture = x86 if ($data =~ /ARCH_X86/);
  195. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  196. $architecture = e2k if ($data =~ /ARCH_E2K/);
  197. $architecture = power if ($data =~ /ARCH_POWER/);
  198. $architecture = mips if ($data =~ /ARCH_MIPS/);
  199. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  200. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  201. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  202. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  203. $architecture = arm if ($data =~ /ARCH_ARM/);
  204. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  205. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  206. $architecture = loongarch64 if ($data =~ /ARCH_LOONGARCH64/);
  207. $binformat = bin32;
  208. $binformat = bin64 if ($data =~ /BINARY_64/);
  209. $no_avx512= 0;
  210. if (($architecture eq "x86") || ($architecture eq "x86_64")) {
  211. eval "use File::Temp qw(tempfile)";
  212. if ($@){
  213. warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
  214. $no_avx512 = 0;
  215. } else {
  216. # $tmpf = new File::Temp( UNLINK => 1 );
  217. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  218. $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
  219. print $fh "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
  220. $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
  221. if ($compiler eq "PGI") {
  222. $args = " -tp skylake -c -o $tmpf.o $tmpf";
  223. }
  224. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  225. system(@cmd) == 0;
  226. if ($? != 0) {
  227. $no_avx512 = 1;
  228. } else {
  229. $no_avx512 = 0;
  230. }
  231. unlink("$tmpf.o");
  232. }
  233. }
  234. $no_rv64gv= 0;
  235. if (($architecture eq "riscv64")) {
  236. eval "use File::Temp qw(tempfile)";
  237. if ($@){
  238. warn "could not load PERL module File::Temp, so could not check compiler compatibility with the RISCV vector extension";
  239. $no_rv64gv = 0;
  240. } else {
  241. # $tmpf = new File::Temp( UNLINK => 1 );
  242. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  243. $code = '"vsetvli zero, zero, e8, m1\n"';
  244. print $fh "int main(void){ __asm__ volatile($code); }\n";
  245. $args = " -march=rv64gv -c -o $tmpf.o $tmpf";
  246. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  247. system(@cmd) == 0;
  248. if ($? != 0) {
  249. $no_rv64gv = 1;
  250. } else {
  251. $no_rv64gv = 0;
  252. }
  253. unlink("$tmpf.o");
  254. }
  255. }
  256. $c11_atomics = 0;
  257. if ($data =~ /HAVE_C11/) {
  258. eval "use File::Temp qw(tempfile)";
  259. if ($@){
  260. warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
  261. $c11_atomics = 0;
  262. } else {
  263. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  264. print $fh "#include <stdatomic.h>\nint main(void){}\n";
  265. $args = " -c -o $tmpf.o $tmpf";
  266. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  267. system(@cmd) == 0;
  268. if ($? != 0) {
  269. $c11_atomics = 0;
  270. } else {
  271. $c11_atomics = 1;
  272. }
  273. unlink("$tmpf.o");
  274. }
  275. }
  276. if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
  277. $no_avx2 = 0;
  278. $oldgcc = 0;
  279. $data = `$compiler_name -dumpversion`;
  280. if ($data <= 4.6) {
  281. $no_avx2 = 1;
  282. $oldgcc = 1;
  283. }
  284. }
  285. $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
  286. $data =~ /globl\s([_\.]*)(.*)/;
  287. $need_fu = $1;
  288. $cross = 0;
  289. if ($architecture ne $hostarch) {
  290. $cross = 1;
  291. $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
  292. $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
  293. }
  294. $cross = 1 if ($os ne $hostos);
  295. $cross = 0 if (($os eq "Android") && ($hostos eq "Linux") && ($ENV{TERMUX_APP_PID} != ""));
  296. $openmp = "" if $ENV{USE_OPENMP} != 1;
  297. $linker_L = "";
  298. $linker_l = "";
  299. $linker_a = "";
  300. {
  301. $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`;
  302. $link =~ s/\-Y\sP\,/\-Y/g;
  303. @flags = split(/[\s\,\n]/, $link);
  304. # remove leading and trailing quotes from each flag.
  305. @flags = map {s/^['"]|['"]$//g; $_} @flags;
  306. foreach $flags (@flags) {
  307. if (
  308. ($flags =~ /^\-L/)
  309. && ($flags !~ /^-LIST:/)
  310. && ($flags !~ /^-LANG:/)
  311. ) {
  312. $linker_L .= $flags . " "
  313. }
  314. if ($flags =~ /^\-Y/) {
  315. $linker_L .= "-Wl,". $flags . " "
  316. }
  317. if ($flags =~ /^\--exclude-libs/) {
  318. $linker_L .= "-Wl,". $flags . " ";
  319. $flags="";
  320. }
  321. if (
  322. ($flags =~ /^\-l/)
  323. && ($flags !~ /gfortranbegin/)
  324. && ($flags !~ /frtbegin/)
  325. && ($flags !~ /pathfstart/)
  326. && ($flags !~ /numa/)
  327. && ($flags !~ /crt[0-9]/)
  328. && ($flags !~ /gcc/)
  329. && ($flags !~ /user32/)
  330. && ($flags !~ /kernel32/)
  331. && ($flags !~ /advapi32/)
  332. && ($flags !~ /shell32/)
  333. && ($flags !~ /omp/)
  334. && ($flags !~ /[0-9]+/)
  335. ) {
  336. $linker_l .= $flags . " "
  337. }
  338. $linker_a .= $flags . " " if $flags =~ /\.a$/;
  339. }
  340. }
  341. open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
  342. open(CONFFILE, "> $config" ) || die "Can't create $config";
  343. # print $data, "\n";
  344. print MAKEFILE "OSNAME=$os\n";
  345. print MAKEFILE "ARCH=$architecture\n";
  346. print MAKEFILE "C_COMPILER=$compiler\n";
  347. print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
  348. print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
  349. print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
  350. print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
  351. print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
  352. print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
  353. print MAKEFILE "CROSS=1\n" if $cross != 0;
  354. print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
  355. print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
  356. print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
  357. print MAKEFILE "NO_RV64GV=1\n" if $no_rv64gv eq 1;
  358. print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
  359. print MAKEFILE "NO_AVX2=1\n" if $no_avx2 eq 1;
  360. print MAKEFILE "OLDGCC=1\n" if $oldgcc eq 1;
  361. $os =~ tr/[a-z]/[A-Z]/;
  362. $architecture =~ tr/[a-z]/[A-Z]/;
  363. $compiler =~ tr/[a-z]/[A-Z]/;
  364. print CONFFILE "#define OS_$os\t1\n";
  365. print CONFFILE "#define ARCH_$architecture\t1\n";
  366. print CONFFILE "#define C_$compiler\t1\n";
  367. print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
  368. print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
  369. print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
  370. print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
  371. print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
  372. if ($os eq "LINUX") {
  373. # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
  374. # if ($pthread[2] ne "") {
  375. # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
  376. # } else {
  377. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  378. # }
  379. } else {
  380. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  381. }
  382. close(MAKEFILE);
  383. close(CONFFILE);