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

12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #!/usr/bin/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/`;chop($hostarch);
  7. $hostarch = `uname -p` if ($hostos eq "AIX");
  8. $hostarch = "x86_64" if ($hostarch eq "amd64");
  9. $hostarch = "arm" if ($hostarch ne "arm64" && $hostarch =~ /^arm.*/);
  10. $hostarch = "arm64" if ($hostarch eq "aarch64");
  11. $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
  12. $hostarch = "zarch" if ($hostarch eq "s390x");
  13. #$tmpf = new File::Temp( UNLINK => 1 );
  14. $binary = $ENV{"BINARY"};
  15. $makefile = shift(@ARGV);
  16. $config = shift(@ARGV);
  17. $compiler_name = shift(@ARGV);
  18. $flags = join(" ", @ARGV);
  19. # First, we need to know the target OS and compiler name
  20. $data = `$compiler_name $flags -E ctest.c`;
  21. if ($?) {
  22. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  23. die 1;
  24. }
  25. $cross_suffix = "";
  26. eval "use File::Basename";
  27. if ($@){
  28. warn "could not load PERL module File::Basename, emulating its functionality";
  29. my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
  30. if ($dirnam ne ".") {
  31. $cross_suffix .= $dirnam . "/";
  32. }
  33. my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
  34. if ($basnam =~ /([^\s]*-)(.*)/) {
  35. $cross_suffix .= $1;
  36. }
  37. } else {
  38. if (dirname($compiler_name) ne ".") {
  39. $cross_suffix .= dirname($compiler_name) . "/";
  40. }
  41. if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
  42. $cross_suffix .= $1;
  43. }
  44. }
  45. $compiler = "";
  46. $compiler = LSB if ($data =~ /COMPILER_LSB/);
  47. $compiler = CLANG if ($data =~ /COMPILER_CLANG/);
  48. $compiler = PGI if ($data =~ /COMPILER_PGI/);
  49. $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
  50. $compiler = INTEL if ($data =~ /COMPILER_INTEL/);
  51. $compiler = OPEN64 if ($data =~ /COMPILER_OPEN64/);
  52. $compiler = SUN if ($data =~ /COMPILER_SUN/);
  53. $compiler = IBM if ($data =~ /COMPILER_IBM/);
  54. $compiler = DEC if ($data =~ /COMPILER_DEC/);
  55. $compiler = GCC if ($compiler eq "");
  56. $os = Linux if ($data =~ /OS_LINUX/);
  57. $os = FreeBSD if ($data =~ /OS_FREEBSD/);
  58. $os = NetBSD if ($data =~ /OS_NETBSD/);
  59. $os = OpenBSD if ($data =~ /OS_OPENBSD/);
  60. $os = DragonFly if ($data =~ /OS_DRAGONFLY/);
  61. $os = Darwin if ($data =~ /OS_DARWIN/);
  62. $os = SunOS if ($data =~ /OS_SUNOS/);
  63. $os = AIX if ($data =~ /OS_AIX/);
  64. $os = osf if ($data =~ /OS_OSF/);
  65. $os = WINNT if ($data =~ /OS_WINNT/);
  66. $os = CYGWIN_NT if ($data =~ /OS_CYGWIN_NT/);
  67. $os = Interix if ($data =~ /OS_INTERIX/);
  68. $os = Android if ($data =~ /OS_ANDROID/);
  69. $os = Haiku if ($data =~ /OS_HAIKU/);
  70. $architecture = x86 if ($data =~ /ARCH_X86/);
  71. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  72. $architecture = power if ($data =~ /ARCH_POWER/);
  73. $architecture = mips if ($data =~ /ARCH_MIPS/);
  74. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  75. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  76. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  77. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  78. $architecture = arm if ($data =~ /ARCH_ARM/);
  79. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  80. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  81. $defined = 0;
  82. if ($os eq "AIX") {
  83. $compiler_name .= " -maix32" if ($binary eq "32");
  84. $compiler_name .= " -maix64" if ($binary eq "64");
  85. $defined = 1;
  86. }
  87. if ($architecture eq "mips") {
  88. $compiler_name .= " -mabi=32";
  89. $defined = 1;
  90. }
  91. if ($architecture eq "mips64") {
  92. $compiler_name .= " -mabi=n32" if ($binary eq "32");
  93. $compiler_name .= " -mabi=64" if ($binary eq "64");
  94. $defined = 1;
  95. }
  96. if (($architecture eq "arm") || ($architecture eq "arm64")) {
  97. $defined = 1;
  98. }
  99. if ($architecture eq "zarch") {
  100. $defined = 1;
  101. $binary = 64;
  102. }
  103. if ($architecture eq "alpha") {
  104. $defined = 1;
  105. $binary = 64;
  106. }
  107. if ($architecture eq "ia64") {
  108. $defined = 1;
  109. $binary = 64;
  110. }
  111. if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
  112. $defined = 1;
  113. $binary =32;
  114. }
  115. if ($compiler eq "PGI") {
  116. $compiler_name .= " -tp p7" if ($binary eq "32");
  117. $compiler_name .= " -tp p7-64" if ($binary eq "64");
  118. $openmp = "-mp";
  119. $defined = 1;
  120. }
  121. if ($compiler eq "IBM") {
  122. $compiler_name .= " -q32" if ($binary eq "32");
  123. $compiler_name .= " -q64" if ($binary eq "64");
  124. $openmp = "-qsmp=omp";
  125. $defined = 1;
  126. }
  127. if ($compiler eq "INTEL") {
  128. $openmp = "-openmp";
  129. }
  130. if ($compiler eq "PATHSCALE") {
  131. $openmp = "-mp";
  132. }
  133. if ($compiler eq "OPEN64") {
  134. $openmp = "-mp";
  135. }
  136. if ($compiler eq "CLANG") {
  137. $openmp = "-fopenmp";
  138. }
  139. if ($compiler eq "GCC" || $compiler eq "LSB") {
  140. $openmp = "-fopenmp";
  141. }
  142. if ($defined == 0) {
  143. $compiler_name .= " -m32" if ($binary eq "32");
  144. $compiler_name .= " -m64" if ($binary eq "64");
  145. }
  146. # Do again
  147. $data = `$compiler_name $flags -E ctest.c`;
  148. if ($?) {
  149. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  150. die 1;
  151. }
  152. $have_msa = 0;
  153. if (($architecture eq "mips") || ($architecture eq "mips64")) {
  154. eval "use File::Temp qw(tempfile)";
  155. if ($@){
  156. warn "could not load PERL module File::Temp, so could not check MSA capatibility";
  157. } else {
  158. $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
  159. $code = '"addvi.b $w0, $w1, 1"';
  160. $msa_flags = "-mmsa -mfp64 -msched-weight -mload-store-pairs";
  161. print $tmpf "#include <msa.h>\n\n";
  162. print $tmpf "void main(void){ __asm__ volatile($code); }\n";
  163. $args = "$msa_flags -o $tmpf.o $tmpf";
  164. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  165. system(@cmd) == 0;
  166. if ($? != 0) {
  167. $have_msa = 0;
  168. } else {
  169. $have_msa = 1;
  170. }
  171. unlink("$tmpf.o");
  172. }
  173. }
  174. $architecture = x86 if ($data =~ /ARCH_X86/);
  175. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  176. $architecture = power if ($data =~ /ARCH_POWER/);
  177. $architecture = mips if ($data =~ /ARCH_MIPS/);
  178. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  179. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  180. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  181. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  182. $architecture = arm if ($data =~ /ARCH_ARM/);
  183. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  184. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  185. $binformat = bin32;
  186. $binformat = bin64 if ($data =~ /BINARY_64/);
  187. $no_avx512= 0;
  188. if (($architecture eq "x86") || ($architecture eq "x86_64")) {
  189. eval "use File::Temp qw(tempfile)";
  190. if ($@){
  191. warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
  192. $no_avx512 = 0;
  193. } else {
  194. # $tmpf = new File::Temp( UNLINK => 1 );
  195. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  196. $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
  197. print $tmpf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
  198. $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
  199. if ($compiler eq "PGI") {
  200. $args = " -tp skylake -c -o $tmpf.o $tmpf";
  201. }
  202. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  203. system(@cmd) == 0;
  204. if ($? != 0) {
  205. $no_avx512 = 1;
  206. } else {
  207. $no_avx512 = 0;
  208. }
  209. unlink("$tmpf.o");
  210. }
  211. }
  212. $c11_atomics = 0;
  213. if ($data =~ /HAVE_C11/) {
  214. eval "use File::Temp qw(tempfile)";
  215. if ($@){
  216. warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
  217. $c11_atomics = 0;
  218. } else {
  219. ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
  220. print $tmpf "#include <stdatomic.h>\nint main(void){}\n";
  221. $args = " -c -o $tmpf.o $tmpf";
  222. my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
  223. system(@cmd) == 0;
  224. if ($? != 0) {
  225. $c11_atomics = 0;
  226. } else {
  227. $c11_atomics = 1;
  228. }
  229. unlink("$tmpf.o");
  230. }
  231. }
  232. $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
  233. $data =~ /globl\s([_\.]*)(.*)/;
  234. $need_fu = $1;
  235. $cross = 0;
  236. if ($architecture ne $hostarch) {
  237. $cross = 1;
  238. $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
  239. $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
  240. }
  241. $cross = 1 if ($os ne $hostos);
  242. $openmp = "" if $ENV{USE_OPENMP} != 1;
  243. $linker_L = "";
  244. $linker_l = "";
  245. $linker_a = "";
  246. {
  247. $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`;
  248. $link =~ s/\-Y\sP\,/\-Y/g;
  249. @flags = split(/[\s\,\n]/, $link);
  250. # remove leading and trailing quotes from each flag.
  251. @flags = map {s/^['"]|['"]$//g; $_} @flags;
  252. foreach $flags (@flags) {
  253. if (
  254. ($flags =~ /^\-L/)
  255. && ($flags !~ /^-LIST:/)
  256. && ($flags !~ /^-LANG:/)
  257. ) {
  258. $linker_L .= $flags . " "
  259. }
  260. if ($flags =~ /^\-Y/) {
  261. $linker_L .= "-Wl,". $flags . " "
  262. }
  263. if ($flags =~ /^\--exclude-libs/) {
  264. $linker_L .= "-Wl,". $flags . " ";
  265. $flags="";
  266. }
  267. if (
  268. ($flags =~ /^\-l/)
  269. && ($flags !~ /gfortranbegin/)
  270. && ($flags !~ /frtbegin/)
  271. && ($flags !~ /pathfstart/)
  272. && ($flags !~ /numa/)
  273. && ($flags !~ /crt[0-9]/)
  274. && ($flags !~ /gcc/)
  275. && ($flags !~ /user32/)
  276. && ($flags !~ /kernel32/)
  277. && ($flags !~ /advapi32/)
  278. && ($flags !~ /shell32/)
  279. && ($flags !~ /omp/)
  280. && ($flags !~ /[0-9]+/)
  281. ) {
  282. $linker_l .= $flags . " "
  283. }
  284. $linker_a .= $flags . " " if $flags =~ /\.a$/;
  285. }
  286. }
  287. open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
  288. open(CONFFILE, "> $config" ) || die "Can't create $config";
  289. # print $data, "\n";
  290. print MAKEFILE "OSNAME=$os\n";
  291. print MAKEFILE "ARCH=$architecture\n";
  292. print MAKEFILE "C_COMPILER=$compiler\n";
  293. print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
  294. print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
  295. print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
  296. print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
  297. print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
  298. print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
  299. print MAKEFILE "CROSS=1\n" if $cross != 0;
  300. print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
  301. print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
  302. print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
  303. print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
  304. $os =~ tr/[a-z]/[A-Z]/;
  305. $architecture =~ tr/[a-z]/[A-Z]/;
  306. $compiler =~ tr/[a-z]/[A-Z]/;
  307. print CONFFILE "#define OS_$os\t1\n";
  308. print CONFFILE "#define ARCH_$architecture\t1\n";
  309. print CONFFILE "#define C_$compiler\t1\n";
  310. print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
  311. print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
  312. print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
  313. print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
  314. print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
  315. if ($os eq "LINUX") {
  316. # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
  317. # if ($pthread[2] ne "") {
  318. # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
  319. # } else {
  320. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  321. # }
  322. } else {
  323. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  324. }
  325. close(MAKEFILE);
  326. close(CONFFILE);