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

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