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

12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/perl
  2. # Checking cross compile
  3. $hostos = `uname -s | sed -e s/\-.*//`; chop($hostos);
  4. $hostarch = `uname -m | sed -e s/i.86/x86/`;chop($hostarch);
  5. $hostarch = "x86_64" if ($hostarch eq "amd64");
  6. $hostarch = "arm" if ($hostarch =~ /^arm.*/);
  7. $hostarch = "arm64" if ($hostarch eq "aarch64");
  8. $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
  9. $hostarch = "zarch" if ($hostarch eq "s390x");
  10. $binary = $ENV{"BINARY"};
  11. $makefile = shift(@ARGV);
  12. $config = shift(@ARGV);
  13. $compiler_name = join(" ", @ARGV);
  14. # First, we need to know the target OS and compiler name
  15. $data = `$compiler_name -E ctest.c`;
  16. if ($?) {
  17. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  18. die 1;
  19. }
  20. $cross_suffix = "";
  21. if ($ARGV[0] =~ /(.*)(-[.\d]+)/) {
  22. if ($1 =~ /(.*-)(.*)/) {
  23. $cross_suffix = $1;
  24. }
  25. } else {
  26. if ($ARGV[0] =~ /([^\/]*-)([^\/]*$)/) {
  27. $cross_suffix = $1;
  28. }
  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 = mips32 if ($data =~ /ARCH_MIPS32/);
  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 "mips32") || ($architecture eq "mips64")) {
  70. $compiler_name .= " -mabi=n32" if ($binary eq "32");
  71. $compiler_name .= " -mabi=64" if ($binary eq "64");
  72. $defined = 1;
  73. }
  74. if (($architecture eq "arm") || ($architecture eq "arm64")) {
  75. $defined = 1;
  76. }
  77. if ($architecture eq "zarch") {
  78. $defined = 1;
  79. $binary = 64;
  80. }
  81. if ($architecture eq "alpha") {
  82. $defined = 1;
  83. $binary = 64;
  84. }
  85. if ($architecture eq "ia64") {
  86. $defined = 1;
  87. $binary = 64;
  88. }
  89. if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
  90. $defined = 1;
  91. $binary =32;
  92. }
  93. if ($compiler eq "PGI") {
  94. $compiler_name .= " -tp p7" if ($binary eq "32");
  95. $compiler_name .= " -tp p7-64" if ($binary eq "64");
  96. $openmp = "-mp";
  97. $defined = 1;
  98. }
  99. if ($compiler eq "IBM") {
  100. $compiler_name .= " -q32" if ($binary eq "32");
  101. $compiler_name .= " -q64" if ($binary eq "64");
  102. $openmp = "-qsmp=omp";
  103. $defined = 1;
  104. }
  105. if ($compiler eq "INTEL") {
  106. $openmp = "-openmp";
  107. }
  108. if ($compiler eq "PATHSCALE") {
  109. $openmp = "-mp";
  110. }
  111. if ($compiler eq "OPEN64") {
  112. $openmp = "-mp";
  113. }
  114. if ($compiler eq "CLANG") {
  115. $openmp = "-fopenmp";
  116. }
  117. if ($compiler eq "GCC" || $compiler eq "LSB") {
  118. $openmp = "-fopenmp";
  119. }
  120. if ($defined == 0) {
  121. $compiler_name .= " -m32" if ($binary eq "32");
  122. $compiler_name .= " -m64" if ($binary eq "64");
  123. }
  124. # Do again
  125. $data = `$compiler_name -E ctest.c`;
  126. if ($?) {
  127. printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
  128. die 1;
  129. }
  130. $architecture = x86 if ($data =~ /ARCH_X86/);
  131. $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
  132. $architecture = power if ($data =~ /ARCH_POWER/);
  133. $architecture = mips32 if ($data =~ /ARCH_MIPS32/);
  134. $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
  135. $architecture = alpha if ($data =~ /ARCH_ALPHA/);
  136. $architecture = sparc if ($data =~ /ARCH_SPARC/);
  137. $architecture = ia64 if ($data =~ /ARCH_IA64/);
  138. $architecture = arm if ($data =~ /ARCH_ARM/);
  139. $architecture = arm64 if ($data =~ /ARCH_ARM64/);
  140. $architecture = zarch if ($data =~ /ARCH_ZARCH/);
  141. $binformat = bin32;
  142. $binformat = bin64 if ($data =~ /BINARY_64/);
  143. $data = `$compiler_name -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
  144. $data =~ /globl\s([_\.]*)(.*)/;
  145. $need_fu = $1;
  146. $cross = 0;
  147. $cross = 1 if ($os ne $hostos);
  148. if ($architecture ne $hostarch) {
  149. $cross = 1;
  150. $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
  151. $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
  152. }
  153. $openmp = "" if $ENV{USE_OPENMP} != 1;
  154. $linker_L = "";
  155. $linker_l = "";
  156. $linker_a = "";
  157. {
  158. $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`;
  159. $link =~ s/\-Y\sP\,/\-Y/g;
  160. @flags = split(/[\s\,\n]/, $link);
  161. # remove leading and trailing quotes from each flag.
  162. @flags = map {s/^['"]|['"]$//g; $_} @flags;
  163. foreach $flags (@flags) {
  164. if (
  165. ($flags =~ /^\-L/)
  166. && ($flags !~ /^-LIST:/)
  167. && ($flags !~ /^-LANG:/)
  168. ) {
  169. $linker_L .= $flags . " "
  170. }
  171. if ($flags =~ /^\-Y/) {
  172. $linker_L .= "-Wl,". $flags . " "
  173. }
  174. if (
  175. ($flags =~ /^\-l/)
  176. && ($flags !~ /gfortranbegin/)
  177. && ($flags !~ /frtbegin/)
  178. && ($flags !~ /pathfstart/)
  179. && ($flags !~ /numa/)
  180. && ($flags !~ /crt[0-9]/)
  181. && ($flags !~ /gcc/)
  182. && ($flags !~ /user32/)
  183. && ($flags !~ /kernel32/)
  184. && ($flags !~ /advapi32/)
  185. && ($flags !~ /shell32/)
  186. ) {
  187. $linker_l .= $flags . " "
  188. }
  189. $linker_a .= $flags . " " if $flags =~ /\.a$/;
  190. }
  191. }
  192. open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
  193. open(CONFFILE, "> $config" ) || die "Can't create $config";
  194. # print $data, "\n";
  195. print MAKEFILE "OSNAME=$os\n";
  196. print MAKEFILE "ARCH=$architecture\n";
  197. print MAKEFILE "C_COMPILER=$compiler\n";
  198. print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
  199. print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
  200. print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
  201. print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
  202. print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
  203. print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross_suffix ne "";
  204. print MAKEFILE "CROSS=1\n" if $cross != 0;
  205. print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
  206. $os =~ tr/[a-z]/[A-Z]/;
  207. $architecture =~ tr/[a-z]/[A-Z]/;
  208. $compiler =~ tr/[a-z]/[A-Z]/;
  209. print CONFFILE "#define OS_$os\t1\n";
  210. print CONFFILE "#define ARCH_$architecture\t1\n";
  211. print CONFFILE "#define C_$compiler\t1\n";
  212. print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
  213. print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
  214. print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
  215. if ($os eq "LINUX") {
  216. # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
  217. # if ($pthread[2] ne "") {
  218. # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
  219. # } else {
  220. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  221. # }
  222. } else {
  223. print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
  224. }
  225. close(MAKEFILE);
  226. close(CONFFILE);