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

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