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.6 kB

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