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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #!/bin/sh
  2. # Checking cross compile
  3. hostos=`uname -s | sed -e 's/\-.*//'`
  4. hostarch=`uname -m | sed -e 's/i.86/x86/'`
  5. if [ "$hostos" = "AIX" ] || [ "$hostos" = "SunOS" ]; then
  6. hostarch=`uname -p`
  7. fi
  8. case "$hostarch" in
  9. amd64) hostarch=x86_64 ;;
  10. arm*) [ "$hostarch" = "arm64" ] || hostarch='arm' ;;
  11. aarch64) hostarch=arm64 ;;
  12. powerpc*|ppc*) hostarch=power ;;
  13. s390x) hostarch=zarch ;;
  14. esac
  15. makefile="$1"
  16. config="$2"
  17. compiler_name="$3"
  18. shift 3
  19. flags="$*"
  20. # First, we need to know the target OS and compiler name
  21. {
  22. data=`$compiler_name $flags -E ctest.c`
  23. } || {
  24. printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
  25. exit 1
  26. }
  27. cross_suffix=""
  28. if [ "`dirname "$compiler_name"`" != '.' ]; then
  29. cross_suffix="$cross_suffix`dirname "$compiler_name"`/"
  30. fi
  31. cn=`echo $compiler_name | sed -e 's/ -.*//'`
  32. bn=`basename "$cn"`
  33. case "$bn" in
  34. *-*) if [ "$bn" != '-' ]; then
  35. cross_suffix="$cross_suffix${bn%-*}-"
  36. fi
  37. esac
  38. compiler=""
  39. case "$data" in
  40. *COMPILER_LSB*) compiler=LSB ;;
  41. *COMPILER_CLANG*) compiler=CLANG ;;
  42. *COMPILER_PGI*) compiler=PGI ;;
  43. *COMPILER_PATHSCALE*) compiler=PATHSCALE ;;
  44. *COMPILER_INTEL*) compiler=INTEL ;;
  45. *COMPILER_OPEN64*) compiler=OPEN64 ;;
  46. *COMPILER_SUN*) compiler=SUN ;;
  47. *COMPILER_IBM*) compiler=IBM ;;
  48. *COMPILER_DEC*) compiler=DEC ;;
  49. *COMPILER_FUJITSU*) compiler=FUJITSU ;;
  50. esac
  51. if [ -z "$compiler" ]; then
  52. compiler=GCC
  53. fi
  54. case "$data" in *OS_LINUX*) os=Linux ;; esac
  55. case "$data" in *OS_FREEBSD*) os=FreeBSD ;; esac
  56. case "$data" in *OS_NETBSD*) os=NetBSD ;; esac
  57. case "$data" in *OS_OPENBSD*) os=OpenBSD ;; esac
  58. case "$data" in *OS_DRAGONFLY*) os=DragonFly ;; esac
  59. case "$data" in *OS_DARWIN*) os=Darwin ;; esac
  60. case "$data" in *OS_SUNOS*) os=SunOS ;; esac
  61. case "$data" in *OS_AIX*) os=AIX ;; esac
  62. case "$data" in *OS_OSF*) os=osf ;; esac
  63. case "$data" in *OS_WINNT*) os=WINNT ;; esac
  64. case "$data" in *OS_CYGWIN_NT*) os=CYGWIN_NT ;; esac
  65. case "$data" in *OS_INTERIX*) os=Interix ;; esac
  66. case "$data" in *OS_ANDROID*) os=Android ;; esac
  67. case "$data" in *OS_HAIKU*) os=Haiku ;; esac
  68. case "$data" in
  69. *ARCH_X86_64*) architecture=x86_64 ;;
  70. *ARCH_X86*) architecture=x86 ;;
  71. *ARCH_E2K*) architecture=e2k ;;
  72. *ARCH_POWER*) architecture=power ;;
  73. *ARCH_MIPS64*) architecture=mips64 ;;
  74. *ARCH_MIPS*) architecture=mips ;;
  75. *ARCH_ALPHA*) architecture=alpha ;;
  76. *ARCH_SPARC*) architecture=sparc ;;
  77. *ARCH_IA64*) architecture=ia64 ;;
  78. *ARCH_ARM64*) architecture=arm64 ;;
  79. *ARCH_ARM*) architecture=arm ;;
  80. *ARCH_ZARCH*) architecture=zarch ;;
  81. *ARCH_RISCV64*) architecture=riscv64 ;;
  82. *ARCH_LOONGARCH64*) architecture=loongarch64 ;;
  83. esac
  84. defined=0
  85. if [ "$os" = "AIX" ]; then
  86. if [ "$compiler" = "GCC" ]; then
  87. case "$BINARY" in
  88. 32) compiler_name="$compiler_name -maix32" ;;
  89. 64) compiler_name="$compiler_name -maix64" ;;
  90. esac
  91. defined=1
  92. else
  93. case "$BINARY" in
  94. 32) compiler_name="$compiler_name -m32" ;;
  95. 64) compiler_name="$compiler_name -m64" ;;
  96. esac
  97. defined=1
  98. fi
  99. fi
  100. case "$architecture" in
  101. mips)
  102. compiler_name="$compiler_name -mabi=32"
  103. defined=1
  104. ;;
  105. mips64)
  106. case "$BINARY" in
  107. 32) compiler_name="$compiler_name -mabi=n32" ;;
  108. 64) compiler_name="$compiler_name -mabi=64" ;;
  109. esac
  110. defined=1
  111. ;;
  112. arm|arm64) defined=1 ;;
  113. zarch|e2k|alpha|ia64|riscv64|loonarch64)
  114. defined=1
  115. BINARY=64
  116. ;;
  117. x86)
  118. [ "$os" != "Darwin" ] && [ "$os" != "SunOS" ] && {
  119. defined=1
  120. BINARY=32
  121. }
  122. ;;
  123. esac
  124. case "$compiler" in
  125. PGI)
  126. case "$BINARY" in
  127. 32) compiler_name="$compiler_name -tp p7" ;;
  128. 64) compiler_name="$compiler_name -tp p7-64" ;;
  129. esac
  130. openmp='-mp'
  131. defined=1
  132. ;;
  133. IBM)
  134. case "$BINARY" in
  135. 32) compiler_name="$compiler_name -q32" ;;
  136. 64) compiler_name="$compiler_name -q64" ;;
  137. esac
  138. openmp='-qsmp=omp'
  139. defined=1
  140. ;;
  141. INTEL) openmp='-openmp' ;;
  142. PATHSCALE|OPEN64) openmp='-mp' ;;
  143. CLANG|GCC|LSB) openmp='-fopenmp' ;;
  144. FUJITSU) openmp='-Kopenmp' ;;
  145. esac
  146. if [ "$defined" -eq 0 ]; then
  147. case "$BINARY" in
  148. 32) compiler_name="$compiler_name -m32" ;;
  149. 64) compiler_name="$compiler_name -m64" ;;
  150. esac
  151. fi
  152. # Do again
  153. {
  154. data="$($compiler_name $flags -E ctest.c)"
  155. } || {
  156. printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
  157. exit 1
  158. }
  159. no_msa=0
  160. if [ "$architecture" = "mips" ] || [ "$architecture" = "mips64" ]; then
  161. tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
  162. tmpf="$tmpd/a.c"
  163. code='"addvi.b $w0, $w1, 1"'
  164. msa_flags='-mmsa -mfp64 -mload-store-pairs'
  165. printf "#include <msa.h>\n\n" >> "$tmpf"
  166. printf "void main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
  167. args="$msa_flags -o $tmpf.o $tmpf"
  168. {
  169. $compiler_name $flags $args >/dev/null 2>&1
  170. } || {
  171. no_msa=1
  172. }
  173. rm -rf "$tmpd"
  174. fi
  175. no_lsx=0
  176. no_lasx=0
  177. if [ "$architecture" = "loongarch64" ]; then
  178. tmpd="$(mktemp -d)"
  179. tmplsx="$tmpd/lsx.c"
  180. codelsx='"vadd.b $vr0, $vr0, $vr0"'
  181. lsx_flags='-march=loongarch64'
  182. printf "void main(void){ __asm__ volatile(%s);}\n" "$codelsx" >> "$tmplsx"
  183. args="$lsx_flags -o $tmplsx.o $tmplsx"
  184. {
  185. $compiler_name $flags $args >/dev/null 2>&1
  186. } || {
  187. no_lsx=1
  188. }
  189. tmplasx="$tmpd/lasx.c"
  190. codelasx='"xvadd.b $xr0, $xr0, $xr0"'
  191. lasx_flags='-march=loongarch64'
  192. printf "void main(void){ __asm__ volatile(%s);}\n" "$codelasx" >> "$tmplasx"
  193. args="$lasx_flags -o $tmplasx.o $tmplasx"
  194. {
  195. $compiler_name $flags $args >/dev/null 2>&1
  196. } || {
  197. no_lasx=1
  198. }
  199. rm -rf "$tmpd"
  200. fi
  201. case "$data" in
  202. *ARCH_X86_64*) architecture=x86_64 ;;
  203. *ARCH_X86*) architecture=x86 ;;
  204. *ARCH_E2K*) architecture=e2k ;;
  205. *ARCH_POWER*) architecture=power ;;
  206. *ARCH_MIPS64*) architecture=mips64 ;;
  207. *ARCH_MIPS*) architecture=mips ;;
  208. *ARCH_ALPHA*) architecture=alpha ;;
  209. *ARCH_SPARC*) architecture=sparc ;;
  210. *ARCH_IA64*) architecture=ia64 ;;
  211. *ARCH_ARM64*) architecture=arm64 ;;
  212. *ARCH_ARM*) architecture=arm ;;
  213. *ARCH_ZARCH*) architecture=zarch ;;
  214. *ARCH_LOONGARCH64*) architecture=loongarch64 ;;
  215. esac
  216. binformat='bin32'
  217. case "$data" in
  218. *BINARY_64*) binformat='bin64' ;;
  219. esac
  220. no_avx512=0
  221. if [ "$architecture" = "x86" ] || [ "$architecture" = "x86_64" ]; then
  222. tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
  223. tmpf="$tmpd/a.c"
  224. code='"vbroadcastss -4 * 4(%rsi), %zmm2"'
  225. printf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
  226. if [ "$compiler" = "PGI" ]; then
  227. args=" -tp skylake -c -o $tmpf.o $tmpf"
  228. else
  229. args=" -march=skylake-avx512 -c -o $tmpf.o $tmpf"
  230. fi
  231. no_avx512=0
  232. {
  233. $compiler_name $flags $args >/dev/null 2>&1
  234. } || {
  235. no_avx512=1
  236. }
  237. rm -rf "$tmpd"
  238. fi
  239. no_rv64gv=0
  240. if [ "$architecture" = "riscv64" ]; then
  241. tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
  242. tmpf="$tmpd/a.c"
  243. code='"vsetvli zero, zero, e8, m1\n"'
  244. printf "int main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
  245. args=" -march=rv64gv -c -o $tmpf.o $tmpf"
  246. no_rv64gv=0
  247. {
  248. $compiler_name $flags $args >/dev/null 2>&1
  249. } || {
  250. no_rv64gv=1
  251. }
  252. rm -rf "$tmpd"
  253. fi
  254. no_sve=0
  255. if [ "$architecture" = "arm64" ]; then
  256. tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
  257. tmpf="$tmpd/a.c"
  258. printf "#include <arm_sve.h>\n\n int main(void){}\n">> "$tmpf"
  259. args=" -march=armv8-a+sve -c -o $tmpf.o $tmpf"
  260. no_sve=0
  261. {
  262. $compiler_name $flags $args >/dev/null 2>&1
  263. } || {
  264. args=" -Msve_intrinsics -c -o $tmpf.o $tmpf"
  265. $compiler_name $flags $args >/dev/null 2>&1
  266. } || {
  267. no_sve=1
  268. }
  269. rm -rf "$tmpd"
  270. fi
  271. c11_atomics=0
  272. case "$data" in
  273. *HAVE_C11*)
  274. tmpd=$(mktemp -d 2>/dev/null || mktemp -d -t 'OBC')
  275. tmpf="$tmpd/a.c"
  276. printf "#include <stdatomic.h>\nint main(void){}\n" >> "$tmpf"
  277. args=" -c -o $tmpf.o $tmpf"
  278. c11_atomics=1
  279. {
  280. $compiler_name $flags $args >/dev/null 2>&1
  281. } || {
  282. c11_atomics=0
  283. }
  284. rm -rf "$tmpd"
  285. ;;
  286. esac
  287. oldgcc=0
  288. no_avx2=0
  289. if [ "$compiler" = "GCC" ]; then
  290. case "$architecture" in x86|x86_64)
  291. no_avx2=0
  292. oldgcc=0
  293. data=`$compiler_name -dumpversion`
  294. case "$data" in *.*.*)
  295. data="${data%.*}"
  296. esac
  297. if awk -v n1=$data -v n2=4.6 'BEGIN { exit !(n1 <= n2) }'; then
  298. no_avx2=1
  299. oldgcc=1
  300. fi
  301. esac
  302. fi
  303. data=`$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`
  304. need_fu=''
  305. if echo "$data" | grep 'globl[[:space:]][_\.]'; then
  306. need_fu="${data##*globl[[:space:]]}"
  307. need_fu="${need_fu%%[!_\.]*}"
  308. fi
  309. cross=0
  310. if [ "$architecture" != "$hostarch" ]; then
  311. cross=1
  312. [ "$hostarch" = "x86_64" ] && [ "$architecture" = "x86" ] && cross=0
  313. [ "$hostarch" = "mips64" ] && [ "$architecture" = "mips" ] && cross=0
  314. fi
  315. [ "$os" != "$hostos" ] && cross=1
  316. [ "$os" = "Android" ] && [ "$hostos" = "Linux" ] && [ -n "$TERMUX_APP_PID" ] \
  317. && cross=0
  318. [ "$USE_OPENMP" != 1 ] && openmp=''
  319. linker_L=""
  320. linker_l=""
  321. linker_a=""
  322. link=`$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`
  323. link=`echo "$link" | sed 's/\-Y[[:space:]]P\,/\-Y/g'`
  324. flags=`echo $link | tr "'[[:space:]],\n" " "`
  325. # Strip trailing quotes
  326. old_flags="$flags"
  327. flags=''
  328. for flag in $old_flags; do
  329. f=`echo "$flag" | tr '"' ' '`
  330. flags="$flags $f"
  331. done
  332. for flag in $flags; do
  333. case "$flag" in -L*)
  334. case "$flag" in
  335. -LIST:*|-LANG:*) ;;
  336. *) linker_L="$linker_L $flag" ;;
  337. esac
  338. esac
  339. case "$flag" in -Y*)
  340. linker_L="$linker_L -Wl,$flag" ;;
  341. esac
  342. case "$flag" in --exclude-libs*)
  343. linker_L="$linker_L -Wl,$flag"
  344. flags=""
  345. ;;
  346. esac
  347. case "$flag" in -l*)
  348. case "$flag" in
  349. *gfortranbegin*|*frtbegin*|*pathfstart*|*numa*|*crt[0-9]*|\
  350. *gcc*|*user32*|*kernel32*|*advapi32*|*shell32*|*omp*|\
  351. *[0-9]*) ;;
  352. *) linker_l="$linker_l $flag" ;;
  353. esac
  354. esac
  355. case "$flag" in *.a) linker_a="$linker_a $flag" ;; esac
  356. done
  357. [ "$makefile" = "-" ] && {
  358. [ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
  359. [ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
  360. [ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
  361. [ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
  362. exit 0
  363. }
  364. :> "$makefile" || exit 1
  365. :> "$config" || exit 1
  366. # print $data, "\n";
  367. {
  368. printf "OSNAME=%s\n" "$os"
  369. printf "ARCH=%s\n" "$architecture"
  370. printf "C_COMPILER=%s\n" "$compiler"
  371. [ $binformat != 'bin32' ] && printf "BINARY32=\n"
  372. [ $binformat != 'bin64' ] && printf "BINARY64=\n"
  373. [ "$binformat" = "bin32" ] && printf "BINARY32=1\n"
  374. [ "$binformat" = "bin64" ] && printf "BINARY64=1\n"
  375. [ -n "$need_fu" ] && printf 'FU=%s\n' "$need_fu"
  376. [ "$cross" -ne 0 ] && [ -n "$cross_suffix" ] && \
  377. printf "CROSS_SUFFIX=%s\n" "$cross_suffix"
  378. [ "$cross" -ne 0 ] && printf "CROSS=1\n"
  379. printf "CEXTRALIB=%s %s %s\n" "$linker_L" "$linker_l" "$linker_a"
  380. [ "$no_msa" -eq 1 ] && printf "NO_MSA=1\n"
  381. [ "$no_sve" -eq 1 ] && printf "NO_SVE=1\n"
  382. [ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
  383. [ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
  384. [ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
  385. [ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
  386. [ "$no_lsx" -eq 1 ] && printf "NO_LSX=1\n"
  387. [ "$no_lasx" -eq 1 ] && printf "NO_LASX=1\n"
  388. } >> "$makefile"
  389. os=`echo "$os" | tr '[[:lower:]]' '[[:upper:]]'/ `
  390. architecture=`echo "$architecture" | tr '[[:lower:]]' '[[:upper:]]' `
  391. compiler=`echo "$compiler" | tr '[[:lower:]]' '[[:upper:]]' `
  392. {
  393. printf "#define OS_%s\t1\n" "$os"
  394. printf "#define ARCH_%s\t1\n" "$architecture"
  395. printf "#define C_%s\t1\n" "$compiler"
  396. [ "$binformat" = "bin32" ] && printf "#define __32BIT__\t1\n"
  397. [ "$binformat" = "bin64" ] && printf "#define __64BIT__\t1\n"
  398. [ -n "$need_fu" ] && printf "#define FUNDERSCORE\t%s\n" "$need_fu"
  399. [ "$no_msa" -eq 1 ] && printf "#define NO_MSA\t1\n"
  400. [ "$c11_atomics" -eq 1 ] && printf "#define HAVE_C11\t1\n"
  401. [ "$no_lsx" -eq 1 ] && printf "#define NO_LSX\t1\n"
  402. [ "$no_lasx" -eq 1 ] && printf "#define NO_LASX\t1\n"
  403. } >> "$config"
  404. if [ "$os" = "LINUX" ]; then
  405. # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
  406. # if ($pthread[2] ne "") {
  407. # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
  408. # } else {
  409. printf "#define PTHREAD_CREATE_FUNC pthread_create\n" >> "$config"
  410. # }
  411. else
  412. printf "#define PTHREAD_CREATE_FUNC pthread_create\n" >> "$config"
  413. fi