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

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