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

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