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

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