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

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