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

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