1
0

configure 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. #!/bin/sh
  2. usage () {
  3. cat <<EOF
  4. Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
  5. To assign environment variables (e.g., CC, CFLAGS...), specify them as
  6. VAR=VALUE. See below for descriptions of some of the useful variables.
  7. Defaults for the options are specified in brackets.
  8. Configuration:
  9. --srcdir=DIR source directory [detected]
  10. Installation directories:
  11. --prefix=PREFIX main installation prefix [/usr/local/musl]
  12. --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
  13. Fine tuning of the installation directories:
  14. --bindir=DIR user executables [EPREFIX/bin]
  15. --libdir=DIR library files for the linker [PREFIX/lib]
  16. --includedir=DIR include files for the C compiler [PREFIX/include]
  17. --syslibdir=DIR location for the dynamic linker [/lib]
  18. System types:
  19. --target=TARGET configure to run on target TARGET [detected]
  20. --host=HOST same as --target
  21. --build=BUILD build system type; used only to infer cross-compiling
  22. Optional features:
  23. --enable-optimize=... optimize listed components for speed over size [auto]
  24. --enable-debug build with debugging information [disabled]
  25. --disable-warnings build with recommended warnings flags [enabled]
  26. --enable-wrapper=... build given musl toolchain wrapper [auto]
  27. --disable-shared inhibit building shared library [enabled]
  28. --disable-static inhibit building static library [enabled]
  29. Optional packages:
  30. --with-malloc=... choose malloc implementation [mallocng]
  31. Some influential environment variables:
  32. CC C compiler command [detected]
  33. CFLAGS C compiler flags [-Os -pipe ...]
  34. CROSS_COMPILE prefix for cross compiler and tools [none]
  35. LIBCC compiler runtime library [detected]
  36. Use these variables to override the choices made by configure.
  37. EOF
  38. exit 0
  39. }
  40. # Helper functions
  41. quote () {
  42. tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
  43. $1
  44. EOF
  45. printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
  46. }
  47. echo () { printf "%s\n" "$*" ; }
  48. fail () { echo "$*" ; exit 1 ; }
  49. fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
  50. cmdexists () { type "$1" >/dev/null 2>&1 ; }
  51. trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
  52. stripdir () {
  53. while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
  54. }
  55. trycppif () {
  56. printf "checking preprocessor condition %s... " "$1"
  57. echo "typedef int x;" > "$tmpc"
  58. echo "#if $1" >> "$tmpc"
  59. echo "#error yes" >> "$tmpc"
  60. echo "#endif" >> "$tmpc"
  61. if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  62. printf "false\n"
  63. return 1
  64. else
  65. printf "true\n"
  66. return 0
  67. fi
  68. }
  69. tryflag () {
  70. printf "checking whether compiler accepts %s... " "$2"
  71. echo "typedef int x;" > "$tmpc"
  72. if $CC $CFLAGS_TRY $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  73. printf "yes\n"
  74. eval "$1=\"\${$1} \$2\""
  75. eval "$1=\${$1# }"
  76. return 0
  77. else
  78. printf "no\n"
  79. return 1
  80. fi
  81. }
  82. tryldflag () {
  83. printf "checking whether linker accepts %s... " "$2"
  84. echo "typedef int x;" > "$tmpc"
  85. if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  86. printf "yes\n"
  87. eval "$1=\"\${$1} \$2\""
  88. eval "$1=\${$1# }"
  89. return 0
  90. else
  91. printf "no\n"
  92. return 1
  93. fi
  94. }
  95. # Beginning of actual script
  96. CFLAGS_C99FSE=
  97. CFLAGS_AUTO=
  98. CFLAGS_MEMOPS=
  99. CFLAGS_NOSSP=
  100. CFLAGS_TRY=
  101. LDFLAGS_AUTO=
  102. LDFLAGS_TRY=
  103. OPTIMIZE_GLOBS=
  104. srcdir=
  105. prefix=/usr/local/musl
  106. exec_prefix='$(prefix)'
  107. bindir='$(exec_prefix)/bin'
  108. libdir='$(prefix)/lib'
  109. includedir='$(prefix)/include'
  110. syslibdir='/lib'
  111. tools=
  112. tool_libs=
  113. build=
  114. target=
  115. optimize=auto
  116. debug=no
  117. warnings=yes
  118. shared=auto
  119. static=yes
  120. wrapper=auto
  121. gcc_wrapper=no
  122. clang_wrapper=no
  123. malloc_dir=mallocng
  124. for arg ; do
  125. case "$arg" in
  126. --help|-h) usage ;;
  127. --srcdir=*) srcdir=${arg#*=} ;;
  128. --prefix=*) prefix=${arg#*=} ;;
  129. --exec-prefix=*) exec_prefix=${arg#*=} ;;
  130. --bindir=*) bindir=${arg#*=} ;;
  131. --libdir=*) libdir=${arg#*=} ;;
  132. --includedir=*) includedir=${arg#*=} ;;
  133. --syslibdir=*) syslibdir=${arg#*=} ;;
  134. --enable-shared|--enable-shared=yes) shared=yes ;;
  135. --disable-shared|--enable-shared=no) shared=no ;;
  136. --enable-static|--enable-static=yes) static=yes ;;
  137. --disable-static|--enable-static=no) static=no ;;
  138. --enable-optimize) optimize=yes ;;
  139. --enable-optimize=*) optimize=${arg#*=} ;;
  140. --disable-optimize) optimize=no ;;
  141. --enable-debug|--enable-debug=yes) debug=yes ;;
  142. --disable-debug|--enable-debug=no) debug=no ;;
  143. --enable-warnings|--enable-warnings=yes) warnings=yes ;;
  144. --disable-warnings|--enable-warnings=no) warnings=no ;;
  145. --enable-wrapper|--enable-wrapper=yes) wrapper=detect ;;
  146. --enable-wrapper=all) wrapper=yes ; gcc_wrapper=yes ; clang_wrapper=yes ;;
  147. --enable-wrapper=gcc) wrapper=yes ; gcc_wrapper=yes ;;
  148. --enable-wrapper=clang) wrapper=yes ; clang_wrapper=yes ;;
  149. --disable-wrapper|--enable-wrapper=no) wrapper=no ;;
  150. --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ; gcc_wrapper=yes ;;
  151. --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
  152. --with-malloc=*) malloc_dir=${arg#*=} ;;
  153. --enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;;
  154. --host=*|--target=*) target=${arg#*=} ;;
  155. --build=*) build=${arg#*=} ;;
  156. -* ) echo "$0: unknown option $arg" ;;
  157. AR=*) AR=${arg#*=} ;;
  158. RANLIB=*) RANLIB=${arg#*=} ;;
  159. CC=*) CC=${arg#*=} ;;
  160. CFLAGS=*) CFLAGS=${arg#*=} ;;
  161. CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
  162. LDFLAGS=*) LDFLAGS=${arg#*=} ;;
  163. CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
  164. LIBCC=*) LIBCC=${arg#*=} ;;
  165. *=*) ;;
  166. *) build=$arg ; target=$arg ;;
  167. esac
  168. done
  169. for i in srcdir prefix exec_prefix bindir libdir includedir syslibdir ; do
  170. stripdir $i
  171. done
  172. #
  173. # Get the source dir for out-of-tree builds
  174. #
  175. if test -z "$srcdir" ; then
  176. srcdir="${0%/configure}"
  177. stripdir srcdir
  178. fi
  179. abs_builddir="$(pwd)" || fail "$0: cannot determine working directory"
  180. abs_srcdir="$(cd $srcdir && pwd)" || fail "$0: invalid source directory $srcdir"
  181. test "$abs_srcdir" = "$abs_builddir" && srcdir=.
  182. test "$srcdir" != "." && test -f Makefile && test ! -h Makefile && fail "$0: Makefile already exists in the working directory"
  183. #
  184. # Get a temp filename we can use
  185. #
  186. i=0
  187. set -C
  188. while : ; do i=$(($i+1))
  189. tmpc="./conf$$-$PPID-$i.c"
  190. 2>|/dev/null > "$tmpc" && break
  191. test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
  192. done
  193. set +C
  194. trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
  195. #
  196. # Check that the requested malloc implementation exists
  197. #
  198. test -d "$srcdir/src/malloc/$malloc_dir" \
  199. || fail "$0: error: chosen malloc implementation '$malloc_dir' does not exist"
  200. #
  201. # Check whether we are cross-compiling, and set a default
  202. # CROSS_COMPILE prefix if none was provided.
  203. #
  204. test "$target" && \
  205. test "$target" != "$build" && \
  206. test -z "$CROSS_COMPILE" && \
  207. CROSS_COMPILE="$target-"
  208. #
  209. # Find a C compiler to use
  210. #
  211. printf "checking for C compiler... "
  212. trycc ${CROSS_COMPILE}gcc
  213. trycc ${CROSS_COMPILE}c99
  214. trycc ${CROSS_COMPILE}cc
  215. printf "%s\n" "$CC"
  216. test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
  217. printf "checking whether C compiler works... "
  218. echo "typedef int x;" > "$tmpc"
  219. if output=$($CC $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" 2>&1) ; then
  220. printf "yes\n"
  221. else
  222. printf "no; compiler output follows:\n%s\n" "$output"
  223. exit 1
  224. fi
  225. #
  226. # Figure out options to force errors on unknown flags.
  227. #
  228. tryflag CFLAGS_TRY -Werror=unknown-warning-option
  229. tryflag CFLAGS_TRY -Werror=unused-command-line-argument
  230. tryflag CFLAGS_TRY -Werror=ignored-optimization-argument
  231. tryldflag LDFLAGS_TRY -Werror=unknown-warning-option
  232. tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
  233. #
  234. # Need to know if the compiler is gcc or clang to decide which toolchain
  235. # wrappers to build.
  236. #
  237. printf "checking for C compiler family... "
  238. cc_ver="$(LC_ALL=C $CC -v 2>&1)"
  239. cc_family=unknown
  240. if fnmatch '*gcc\ version*' "$cc_ver" ; then
  241. cc_family=gcc
  242. elif fnmatch '*clang\ version*' "$cc_ver" ; then
  243. cc_family=clang
  244. fi
  245. echo "$cc_family"
  246. #
  247. # Figure out toolchain wrapper to build
  248. #
  249. if test "$wrapper" = auto || test "$wrapper" = detect ; then
  250. echo "#include <stdlib.h>" > "$tmpc"
  251. echo "#if ! __GLIBC__" >> "$tmpc"
  252. echo "#error no" >> "$tmpc"
  253. echo "#endif" >> "$tmpc"
  254. printf "checking for toolchain wrapper to build... "
  255. if test "$wrapper" = auto && ! $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  256. echo "none"
  257. elif test "$cc_family" = gcc ; then
  258. gcc_wrapper=yes
  259. echo "gcc"
  260. elif test "$cc_family" = clang ; then
  261. clang_wrapper=yes
  262. echo "clang"
  263. else
  264. echo "none"
  265. if test "$wrapper" = detect ; then
  266. fail "$0: could not find an appropriate toolchain wrapper"
  267. fi
  268. fi
  269. fi
  270. if test "$gcc_wrapper" = yes ; then
  271. tools="$tools obj/musl-gcc"
  272. tool_libs="$tool_libs lib/musl-gcc.specs"
  273. fi
  274. if test "$clang_wrapper" = yes ; then
  275. tools="$tools obj/musl-clang obj/ld.musl-clang"
  276. fi
  277. #
  278. # Find the target architecture
  279. #
  280. printf "checking target system type... "
  281. test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
  282. printf "%s\n" "$target"
  283. #
  284. # Convert to just ARCH
  285. #
  286. case "$target" in
  287. # Catch these early to simplify matching for 32-bit archs
  288. arm*) ARCH=arm ;;
  289. aarch64*) ARCH=aarch64 ;;
  290. i?86-nt32*) ARCH=nt32 ;;
  291. i?86*) ARCH=i386 ;;
  292. x86_64-x32*|x32*|x86_64*x32) ARCH=x32 ;;
  293. x86_64-nt64*) ARCH=nt64 ;;
  294. x86_64*) ARCH=x86_64 ;;
  295. loongarch64*) ARCH=loongarch64 ;;
  296. m68k*) ARCH=m68k ;;
  297. mips64*|mipsisa64*) ARCH=mips64 ;;
  298. mips*) ARCH=mips ;;
  299. microblaze*) ARCH=microblaze ;;
  300. or1k*) ARCH=or1k ;;
  301. powerpc64*|ppc64*) ARCH=powerpc64 ;;
  302. powerpc*|ppc*) ARCH=powerpc ;;
  303. riscv64*) ARCH=riscv64 ;;
  304. riscv32*) ARCH=riscv32 ;;
  305. sh[1-9bel-]*|sh|superh*) ARCH=sh ;;
  306. s390x*) ARCH=s390x ;;
  307. unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
  308. *) fail "$0: unknown or unsupported target \"$target\"" ;;
  309. esac
  310. #
  311. # Try to get a conforming C99 freestanding environment
  312. #
  313. tryflag CFLAGS_C99FSE -std=c99
  314. tryflag CFLAGS_C99FSE -nostdinc
  315. tryflag CFLAGS_C99FSE -ffreestanding \
  316. || tryflag CFLAGS_C99FSE -fno-builtin
  317. tryflag CFLAGS_C99FSE -fexcess-precision=standard \
  318. || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
  319. tryflag CFLAGS_C99FSE -frounding-math
  320. #
  321. # Semantically we want to insist that our sources follow the
  322. # C rules for type-based aliasing, but most if not all real-world
  323. # compilers are known or suspected to have critical bugs in their
  324. # type-based aliasing analysis. See for example GCC bug 107107.
  325. #
  326. tryflag CFLAGS_C99FSE -fno-strict-aliasing
  327. #
  328. # We may use the may_alias attribute if __GNUC__ is defined, so
  329. # if the compiler defines __GNUC__ but does not provide it,
  330. # it must be defined away as part of the CFLAGS.
  331. #
  332. printf "checking whether compiler needs attribute((may_alias)) suppression... "
  333. cat > "$tmpc" <<EOF
  334. typedef int
  335. #ifdef __GNUC__
  336. __attribute__((__may_alias__))
  337. #endif
  338. x;
  339. EOF
  340. if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
  341. -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  342. printf "no\n"
  343. else
  344. printf "yes\n"
  345. CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
  346. fi
  347. #
  348. # The GNU toolchain defaults to assuming unmarked files need an
  349. # executable stack, potentially exposing vulnerabilities in programs
  350. # linked with such object files. Fix this.
  351. #
  352. tryflag CFLAGS_C99FSE -Wa,--noexecstack
  353. #
  354. # Check for options to disable stack protector, which needs to be
  355. # disabled for a few early-bootstrap translation units. If not found,
  356. # this is not an error; we assume the toolchain does not do ssp.
  357. #
  358. tryflag CFLAGS_NOSSP -fno-stack-protector
  359. #
  360. # Check for options that may be needed to prevent the compiler from
  361. # generating self-referential versions of memcpy,, memmove, memcmp,
  362. # and memset. Really, we should add a check to determine if this
  363. # option is sufficient, and if not, add a macro to cripple these
  364. # functions with volatile...
  365. #
  366. tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
  367. #
  368. # Enable debugging if requessted.
  369. #
  370. test "$debug" = yes && CFLAGS_AUTO=-g
  371. #
  372. # Preprocess asm files to add extra debugging information if debug is
  373. # enabled, our assembler supports the needed directives, and the
  374. # preprocessing script has been written for our architecture.
  375. #
  376. printf "checking whether we should preprocess assembly to add debugging information... "
  377. if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" &&
  378. test -f "$srcdir/tools/add-cfi.$ARCH.awk" &&
  379. printf ".file 1 \"srcfile.s\"\n.line 1\n.cfi_startproc\n.cfi_endproc" | $CC -g -x assembler -c -o /dev/null 2>/dev/null -
  380. then
  381. ADD_CFI=yes
  382. else
  383. ADD_CFI=no
  384. fi
  385. printf "%s\n" "$ADD_CFI"
  386. #
  387. # Possibly add a -O option to CFLAGS and select modules to optimize with
  388. # -O3 based on the status of --enable-optimize and provided CFLAGS.
  389. #
  390. printf "checking for optimization settings... "
  391. case "x$optimize" in
  392. xauto)
  393. if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
  394. printf "using provided CFLAGS\n" ;optimize=no
  395. else
  396. printf "using defaults\n" ; optimize=yes
  397. fi
  398. ;;
  399. xsize|xnone) printf "minimize size\n" ; optimize=size ;;
  400. xno|x) printf "disabled\n" ; optimize=no ;;
  401. *) printf "custom\n" ;;
  402. esac
  403. if test "$optimize" = no ; then :
  404. else
  405. tryflag CFLAGS_AUTO -O2
  406. tryflag CFLAGS_AUTO -fno-align-jumps
  407. tryflag CFLAGS_AUTO -fno-align-functions
  408. tryflag CFLAGS_AUTO -fno-align-loops
  409. tryflag CFLAGS_AUTO -fno-align-labels
  410. tryflag CFLAGS_AUTO -fira-region=one
  411. tryflag CFLAGS_AUTO -fira-hoist-pressure
  412. tryflag CFLAGS_AUTO -freorder-blocks-algorithm=simple \
  413. || tryflag CFLAGS_AUTO -fno-reorder-blocks
  414. tryflag CFLAGS_AUTO -fno-prefetch-loop-arrays
  415. tryflag CFLAGS_AUTO -fno-tree-ch
  416. fi
  417. test "$optimize" = yes && optimize="internal,malloc,string"
  418. if fnmatch 'no|size' "$optimize" ; then :
  419. else
  420. printf "components to be optimized for speed:"
  421. while test "$optimize" ; do
  422. case "$optimize" in
  423. *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
  424. *) this=$optimize optimize=
  425. esac
  426. printf " $this"
  427. case "$this" in
  428. */*.c) ;;
  429. */*) this=$this*.c ;;
  430. *) this=$this/*.c ;;
  431. esac
  432. OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
  433. done
  434. OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
  435. printf "\n"
  436. fi
  437. # Always try -pipe
  438. tryflag CFLAGS_AUTO -pipe
  439. #
  440. # If debugging is disabled, omit frame pointer. Modern GCC does this
  441. # anyway on most archs even when debugging is enabled since the frame
  442. # pointer is no longer needed for debugging.
  443. #
  444. if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
  445. else
  446. tryflag CFLAGS_AUTO -fomit-frame-pointer
  447. fi
  448. #
  449. # Modern GCC wants to put DWARF tables (used for debugging and
  450. # unwinding) in the loaded part of the program where they are
  451. # unstrippable. These options force them back to debug sections (and
  452. # cause them not to get generated at all if debugging is off).
  453. #
  454. tryflag CFLAGS_AUTO -fno-unwind-tables
  455. tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
  456. #
  457. # Attempt to put each function and each data object in its own
  458. # section. This both allows additional size optimizations at link
  459. # time and works around a dangerous class of compiler/assembler bugs
  460. # whereby relative address expressions are constant-folded by the
  461. # assembler even when one or more of the symbols involved is
  462. # replaceable. See gas pr 18561 and gcc pr 66609, 68178, etc.
  463. #
  464. tryflag CFLAGS_AUTO -ffunction-sections
  465. tryflag CFLAGS_AUTO -fdata-sections
  466. #
  467. # On x86, make sure we don't have incompatible instruction set
  468. # extensions enabled by default. This is bad for making static binaries.
  469. # We cheat and use i486 rather than i386 because i386 really does not
  470. # work anyway (issues with atomic ops).
  471. # Some build environments pass -march and -mtune options via CC, so
  472. # check both CC and CFLAGS.
  473. #
  474. if test "$ARCH" = "i386" ; then
  475. fnmatch '-march=*|*\ -march=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
  476. fnmatch '-mtune=*|*\ -mtune=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
  477. fi
  478. #
  479. # GCC defines -w as overriding any -W options, regardless of order, but
  480. # clang has a bunch of annoying warnings enabled by default and needs -w
  481. # to start from a clean slate. So use -w if building with clang. Also
  482. # turn off a common on-by-default cast warning regardless of compiler.
  483. #
  484. test "$cc_family" = clang && tryflag CFLAGS_AUTO -w
  485. tryflag CFLAGS_AUTO -Wno-pointer-to-int-cast
  486. #
  487. # Even with -std=c99, gcc accepts some constructs which are constraint
  488. # violations. We want to treat these as errors regardless of whether
  489. # other purely stylistic warnings are enabled -- especially implicit
  490. # function declarations, which are a dangerous programming error.
  491. #
  492. tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
  493. tryflag CFLAGS_AUTO -Werror=implicit-int
  494. tryflag CFLAGS_AUTO -Werror=pointer-sign
  495. tryflag CFLAGS_AUTO -Werror=pointer-arith
  496. tryflag CFLAGS_AUTO -Werror=int-conversion
  497. tryflag CFLAGS_AUTO -Werror=incompatible-pointer-types
  498. tryflag CFLAGS_AUTO -Werror=discarded-qualifiers
  499. tryflag CFLAGS_AUTO -Werror=discarded-array-qualifiers
  500. #
  501. # GCC ignores unused arguements by default, but Clang needs this extra
  502. # parameter to stop printing warnings about LDFLAGS passed during
  503. # compiling stage and CFLAGS passed during linking stage.
  504. #
  505. test "$cc_family" = clang && tryflag CFLAGS_AUTO -Qunused-arguments
  506. if test "x$warnings" = xyes ; then
  507. tryflag CFLAGS_AUTO -Waddress
  508. tryflag CFLAGS_AUTO -Warray-bounds
  509. tryflag CFLAGS_AUTO -Wchar-subscripts
  510. tryflag CFLAGS_AUTO -Wduplicate-decl-specifier
  511. tryflag CFLAGS_AUTO -Winit-self
  512. tryflag CFLAGS_AUTO -Wreturn-type
  513. tryflag CFLAGS_AUTO -Wsequence-point
  514. tryflag CFLAGS_AUTO -Wstrict-aliasing
  515. tryflag CFLAGS_AUTO -Wunused-function
  516. tryflag CFLAGS_AUTO -Wunused-label
  517. tryflag CFLAGS_AUTO -Wunused-variable
  518. fi
  519. # Determine if the compiler produces position-independent code (PIC)
  520. # by default. If so, we don't need to compile separate object files
  521. # for libc.a and libc.so.
  522. if trycppif __PIC__ "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
  523. pic_default=yes
  524. else
  525. pic_default=no
  526. fi
  527. # Reduce space lost to padding for alignment purposes by sorting data
  528. # objects according to their alignment reqirements. This approximates
  529. # optimal packing.
  530. tryldflag LDFLAGS_AUTO -Wl,--sort-section,alignment
  531. tryldflag LDFLAGS_AUTO -Wl,--sort-common
  532. # When linking shared library, drop dummy weak definitions that were
  533. # replaced by strong definitions from other translation units.
  534. tryldflag LDFLAGS_AUTO -Wl,--gc-sections
  535. # Some patched GCC builds have these defaults messed up...
  536. tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
  537. # Prevent linking if there are undefined symbols; if any exist,
  538. # libc.so will crash at runtime during relocation processing.
  539. # The common way this can happen is failure to link the compiler
  540. # runtime library; implementation error is also a possibility.
  541. tryldflag LDFLAGS_AUTO -Wl,--no-undefined
  542. # Avoid exporting symbols from compiler runtime libraries. They
  543. # should be hidden anyway, but some toolchains including old gcc
  544. # versions built without shared library support and pcc are broken.
  545. tryldflag LDFLAGS_AUTO -Wl,--exclude-libs=ALL
  546. # Public data symbols must be interposable to allow for copy
  547. # relocations, but otherwise we want to bind symbols at libc link
  548. # time to eliminate startup relocations and PLT overhead. Use
  549. # --dynamic-list rather than -Bsymbolic-functions for greater
  550. # control over what symbols are left unbound.
  551. tryldflag LDFLAGS_AUTO -Wl,--dynamic-list="$srcdir/dynamic.list"
  552. # Find compiler runtime library
  553. test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
  554. test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
  555. test -z "$LIBCC" && try_libcc=`$CC -print-libgcc-file-name 2>/dev/null` \
  556. && tryldflag LIBCC "$try_libcc"
  557. test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
  558. && tryldflag LIBCC "$try_libcc"
  559. printf "using compiler runtime libraries: %s\n" "$LIBCC"
  560. # Figure out arch variants for archs with variants
  561. SUBARCH=
  562. t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS"
  563. if test "$ARCH" = "i386" ; then
  564. printf "checking whether compiler can use ebx in PIC asm constraints... "
  565. cat > "$tmpc" <<EOF
  566. int foo(int x) { __asm__ ( "" : "+b"(x) ); return x; }
  567. EOF
  568. if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -fPIC \
  569. -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  570. printf "yes\n"
  571. else
  572. printf "no\n"
  573. CFLAGS_AUTO="$CFLAGS_AUTO -DBROKEN_EBX_ASM"
  574. fi
  575. fi
  576. if test "$ARCH" = "x86_64" ; then
  577. trycppif __ILP32__ "$t" && ARCH=x32
  578. fi
  579. if test "$ARCH" = "arm" ; then
  580. if trycppif __thumb2__ "$t" ; then
  581. tryflag CFLAGS_AUTO -mimplicit-it=always
  582. tryflag CFLAGS_AUTO -Wa,-mimplicit-it=always
  583. tryflag CFLAGS_AUTO -Wa,-mthumb
  584. fi
  585. trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
  586. trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
  587. # Versions of clang up until at least 3.8 have the wrong constraint codes
  588. # for floating point operands to inline asm. Detect this so the affected
  589. # source files can just disable the asm.
  590. if test "$cc_family" = clang ; then
  591. printf "checking whether clang's vfp asm constraints work... "
  592. echo 'float f(float x) { __asm__("":"+t"(x)); return x; }' > "$tmpc"
  593. if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  594. printf "yes\n"
  595. else
  596. printf "no\n"
  597. CFLAGS_AUTO="$CFLAGS_AUTO -DBROKEN_VFP_ASM"
  598. CFLAGS_AUTO="${CFLAGS_AUTO# }"
  599. fi
  600. fi
  601. fi
  602. if test "$ARCH" = "aarch64" ; then
  603. trycppif __AARCH64EB__ "$t" && SUBARCH=${SUBARCH}_be
  604. fi
  605. if test "$ARCH" = "loongarch64" ; then
  606. trycppif __loongarch_soft_float "$t" && SUBARCH=${SUBARCH}-sf
  607. trycppif __loongarch_single_float "$t" && SUBARCH=${SUBARCH}-sp
  608. printf "checking whether assembler support FCSRs... "
  609. echo "__asm__(\"movfcsr2gr \$t0,\$fcsr0\");" > "$tmpc"
  610. if $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  611. printf "yes\n"
  612. else
  613. printf "no\n"
  614. CFLAGS_AUTO="$CFLAGS_AUTO -DBROKEN_LOONGARCH_FCSR_ASM"
  615. fi
  616. fi
  617. if test "$ARCH" = "m68k" ; then
  618. if trycppif "__HAVE_68881__" ; then : ;
  619. elif trycppif "__mcffpu__" ; then SUBARCH="-fp64"
  620. else SUBARCH="-sf"
  621. fi
  622. fi
  623. if test "$ARCH" = "mips" ; then
  624. trycppif "__mips_isa_rev >= 6" "$t" && SUBARCH=${SUBARCH}r6
  625. trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
  626. trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
  627. fi
  628. if test "$ARCH" = "mips64" ; then
  629. trycppif "_MIPS_SIM != _ABI64" "$t" && ARCH=mipsn32
  630. trycppif "__mips_isa_rev >= 6" "$t" && SUBARCH=${SUBARCH}r6
  631. trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
  632. trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
  633. fi
  634. if test "$ARCH" = "powerpc" ; then
  635. trycppif "_SOFT_FLOAT || __NO_FPRS__" "$t" && SUBARCH=${SUBARCH}-sf
  636. printf "checking whether compiler can use 'd' constraint in asm... "
  637. echo 'double f(double x) { __asm__ ("fabs %0, %1" : "=d"(x) : "d"(x)); return x; }' > "$tmpc"
  638. if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  639. printf "yes\n"
  640. else
  641. printf "no\n"
  642. CFLAGS_AUTO="$CFLAGS_AUTO -DBROKEN_PPC_D_ASM"
  643. CFLAGS_AUTO="${CFLAGS_AUTO# }"
  644. fi
  645. fi
  646. test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
  647. && SUBARCH=${SUBARCH}el
  648. if test "$ARCH" = "powerpc64" ; then
  649. trycppif "_CALL_ELF == 2" "$t" || fail "$0: error: unsupported powerpc64 ABI"
  650. trycppif __LITTLE_ENDIAN__ "$t" && SUBARCH=${SUBARCH}le
  651. trycppif _SOFT_FLOAT "$t" && fail "$0: error: soft-float not supported on powerpc64"
  652. fi
  653. if test "$ARCH" = "riscv64" -o "$ARCH" = "riscv32" ; then
  654. trycppif __riscv_float_abi_soft "$t" && SUBARCH=${SUBARCH}-sf
  655. trycppif __riscv_float_abi_single "$t" && SUBARCH=${SUBARCH}-sp
  656. fi
  657. if test "$ARCH" = "sh" ; then
  658. tryflag CFLAGS_AUTO -Wa,--isa=any
  659. trycppif __BIG_ENDIAN__ "$t" && SUBARCH=${SUBARCH}eb
  660. if trycppif "__SH_FPU_ANY__ || __SH4__" "$t" ; then
  661. # Some sh configurations are broken and replace double with float
  662. # rather than using softfloat when the fpu is present but only
  663. # supports single precision. Reject them.
  664. printf "checking whether compiler's double type is IEEE double... "
  665. echo 'typedef char dblcheck[(int)sizeof(double)-5];' > "$tmpc"
  666. if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  667. printf "yes\n"
  668. else
  669. printf "no\n"
  670. fail "$0: error: compiler's floating point configuration is unsupported"
  671. fi
  672. else
  673. SUBARCH=${SUBARCH}-nofpu
  674. fi
  675. if trycppif __SH_FDPIC__ "$t" ; then
  676. SUBARCH=${SUBARCH}-fdpic
  677. fi
  678. fi
  679. test "$SUBARCH" \
  680. && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
  681. #
  682. # Some archs (powerpc) have different possible long double formats
  683. # that the compiler can be configured for. The logic for whether this
  684. # is supported is in bits/float.h; in general, it is not. We need to
  685. # check for mismatches here or code in printf, strotd, and scanf will
  686. # be dangerously incorrect because it depends on (1) the macros being
  687. # correct, and (2) IEEE semantics.
  688. #
  689. printf "checking whether compiler's long double definition matches float.h... "
  690. echo '#include <float.h>' > "$tmpc"
  691. echo '#define C(m,s) (m==LDBL_MANT_DIG && s==sizeof(long double))' >> "$tmpc"
  692. echo 'typedef char ldcheck[(C(53,8)||C(64,12)||C(64,16)||C(113,16))*2-1];' >> "$tmpc"
  693. if $CC $CFLAGS_C99FSE \
  694. -I$srcdir/arch/$ARCH -I$srcdir/arch/generic -I$srcdir/include \
  695. $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  696. printf "yes\n"
  697. else
  698. printf "no\n"
  699. fail "$0: error: unsupported long double type"
  700. fi
  701. #
  702. # Some build systems globally pass in broken CFLAGS like -ffast-math
  703. # for all packages. On recent GCC we can detect this and error out
  704. # early rather than producing a seriously-broken math library.
  705. #
  706. if trycppif "__FAST_MATH__" \
  707. "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
  708. fail "$0: error: compiler has broken floating point; check CFLAGS"
  709. fi
  710. printf "creating config.mak... "
  711. cmdline=$(quote "$0")
  712. for i ; do cmdline="$cmdline $(quote "$i")" ; done
  713. exec 3>&1 1>config.mak
  714. cat << EOF
  715. # This version of config.mak was generated by:
  716. # $cmdline
  717. # Any changes made here will be lost if configure is re-run
  718. AR = ${AR:-\$(CROSS_COMPILE)ar}
  719. RANLIB = ${RANLIB:-\$(CROSS_COMPILE)ranlib}
  720. ARCH = $ARCH
  721. SUBARCH = $SUBARCH
  722. ASMSUBARCH = $ASMSUBARCH
  723. srcdir = $srcdir
  724. prefix = $prefix
  725. exec_prefix = $exec_prefix
  726. bindir = $bindir
  727. libdir = $libdir
  728. includedir = $includedir
  729. syslibdir = $syslibdir
  730. CC = $CC
  731. CFLAGS = $CFLAGS
  732. CFLAGS_AUTO = $CFLAGS_AUTO
  733. CFLAGS_C99FSE = $CFLAGS_C99FSE
  734. CFLAGS_MEMOPS = $CFLAGS_MEMOPS
  735. CFLAGS_NOSSP = $CFLAGS_NOSSP
  736. CPPFLAGS = $CPPFLAGS
  737. LDFLAGS = $LDFLAGS
  738. LDFLAGS_AUTO = $LDFLAGS_AUTO
  739. CROSS_COMPILE = $CROSS_COMPILE
  740. LIBCC = $LIBCC
  741. OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
  742. ALL_TOOLS = $tools
  743. TOOL_LIBS = $tool_libs
  744. ADD_CFI = $ADD_CFI
  745. MALLOC_DIR = $malloc_dir
  746. EOF
  747. test "x$static" = xno && echo "STATIC_LIBS ="
  748. test "x$shared" = xno && echo "SHARED_LIBS ="
  749. test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
  750. test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
  751. test "x$pic_default" = xyes && echo 'AOBJS = $(LOBJS)'
  752. exec 1>&3 3>&-
  753. test "$srcdir" = "." || ln -sf $srcdir/Makefile .
  754. printf "done\n"