configure 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. Installation directories:
  9. --prefix=PREFIX main installation prefix [/usr/local/musl]
  10. --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
  11. Fine tuning of the installation directories:
  12. --bindir=DIR user executables [EPREFIX/bin]
  13. --libdir=DIR library files for the linker [PREFIX/lib]
  14. --includedir=DIR include files for the C compiler [PREFIX/include]
  15. --syslibdir=DIR location for the dynamic linker [/lib]
  16. System types:
  17. --target=TARGET configure to run on target TARGET [detected]
  18. --host=HOST same as --target
  19. Optional features:
  20. --enable-optimize=... optimize listed components for speed over size [auto]
  21. --enable-debug build with debugging information [disabled]
  22. --enable-warnings build with recommended warnings flags [disabled]
  23. --enable-gcc-wrapper build musl-gcc toolchain wrapper [auto]
  24. --disable-shared inhibit building shared library [enabled]
  25. --disable-static inhibit building static library [enabled]
  26. Some influential environment variables:
  27. CC C compiler command [detected]
  28. CFLAGS C compiler flags [-Os -pipe ...]
  29. CROSS_COMPILE prefix for cross compiler and tools [none]
  30. LIBCC compiler runtime library [detected]
  31. Use these variables to override the choices made by configure.
  32. EOF
  33. exit 0
  34. }
  35. # Helper functions
  36. quote () {
  37. tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
  38. $1
  39. EOF
  40. printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/;\$s/\$/'/" -e "s/^'\([-[:alnum:]_,./:]*\)=\(.*\)\$/\1='\2/"
  41. }
  42. echo () { printf "%s\n" "$*" ; }
  43. fail () { echo "$*" ; exit 1 ; }
  44. fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
  45. cmdexists () { type "$1" >/dev/null 2>&1 ; }
  46. trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
  47. stripdir () {
  48. while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
  49. }
  50. trycppif () {
  51. printf "checking preprocessor condition %s... " "$1"
  52. echo "typedef int x;" > "$tmpc"
  53. echo "#if $1" >> "$tmpc"
  54. echo "#error yes" >> "$tmpc"
  55. echo "#endif" >> "$tmpc"
  56. if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  57. printf "false\n"
  58. return 1
  59. else
  60. printf "true\n"
  61. return 0
  62. fi
  63. }
  64. tryflag () {
  65. printf "checking whether compiler accepts %s... " "$2"
  66. echo "typedef int x;" > "$tmpc"
  67. if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  68. printf "yes\n"
  69. eval "$1=\"\${$1} \$2\""
  70. eval "$1=\${$1# }"
  71. return 0
  72. else
  73. printf "no\n"
  74. return 1
  75. fi
  76. }
  77. tryldflag () {
  78. printf "checking whether linker accepts %s... " "$2"
  79. echo "typedef int x;" > "$tmpc"
  80. if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  81. printf "yes\n"
  82. eval "$1=\"\${$1} \$2\""
  83. eval "$1=\${$1# }"
  84. return 0
  85. else
  86. printf "no\n"
  87. return 1
  88. fi
  89. }
  90. # Beginning of actual script
  91. CFLAGS_C99FSE=
  92. CFLAGS_AUTO=
  93. CFLAGS_MEMOPS=
  94. LDFLAGS_AUTO=
  95. OPTIMIZE_GLOBS=
  96. prefix=/usr/local/musl
  97. exec_prefix='$(prefix)'
  98. bindir='$(exec_prefix)/bin'
  99. libdir='$(prefix)/lib'
  100. includedir='$(prefix)/include'
  101. syslibdir='/lib'
  102. target=
  103. optimize=auto
  104. debug=no
  105. warnings=no
  106. shared=yes
  107. static=yes
  108. for arg ; do
  109. case "$arg" in
  110. --help) usage ;;
  111. --prefix=*) prefix=${arg#*=} ;;
  112. --exec-prefix=*) exec_prefix=${arg#*=} ;;
  113. --bindir=*) bindir=${arg#*=} ;;
  114. --libdir=*) libdir=${arg#*=} ;;
  115. --includedir=*) includedir=${arg#*=} ;;
  116. --syslibdir=*) syslibdir=${arg#*=} ;;
  117. --enable-shared|--enable-shared=yes) shared=yes ;;
  118. --disable-shared|--enable-shared=no) shared=no ;;
  119. --enable-static|--enable-static=yes) static=yes ;;
  120. --disable-static|--enable-static=no) static=no ;;
  121. --enable-optimize) optimize=yes ;;
  122. --enable-optimize=*) optimize=${arg#*=} ;;
  123. --disable-optimize) optimize=no ;;
  124. --enable-debug|--enable-debug=yes) debug=yes ;;
  125. --disable-debug|--enable-debug=no) debug=no ;;
  126. --enable-warnings|--enable-warnings=yes) warnings=yes ;;
  127. --disable-warnings|--enable-warnings=no) warnings=no ;;
  128. --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
  129. --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
  130. --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
  131. --host=*|--target=*) target=${arg#*=} ;;
  132. -* ) echo "$0: unknown option $arg" ;;
  133. CC=*) CC=${arg#*=} ;;
  134. CFLAGS=*) CFLAGS=${arg#*=} ;;
  135. CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
  136. LDFLAGS=*) LDFLAGS=${arg#*=} ;;
  137. CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
  138. LIBCC=*) LIBCC=${arg#*=} ;;
  139. *=*) ;;
  140. *) target=$arg ;;
  141. esac
  142. done
  143. for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
  144. stripdir $i
  145. done
  146. #
  147. # Get a temp filename we can use
  148. #
  149. i=0
  150. set -C
  151. while : ; do i=$(($i+1))
  152. tmpc="./conf$$-$PPID-$i.c"
  153. 2>|/dev/null > "$tmpc" && break
  154. test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
  155. done
  156. set +C
  157. trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
  158. #
  159. # Find a C compiler to use
  160. #
  161. printf "checking for C compiler... "
  162. trycc ${CROSS_COMPILE}gcc
  163. trycc ${CROSS_COMPILE}c99
  164. trycc ${CROSS_COMPILE}cc
  165. printf "%s\n" "$CC"
  166. test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
  167. #
  168. # Only build musl-gcc wrapper if toolchain does not already target musl
  169. #
  170. if test -z "$wrapper" ; then
  171. printf "checking whether compiler is gcc... "
  172. if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
  173. echo yes
  174. printf "checking whether to build musl-gcc wrapper... "
  175. wrapper=yes
  176. while read line ; do
  177. case "$line" in */ld-musl-*) wrapper=no ;; esac
  178. done <<EOF
  179. $($CC -dumpspecs)
  180. EOF
  181. echo $wrapper
  182. else
  183. echo no
  184. fi
  185. fi
  186. #
  187. # Find the target architecture
  188. #
  189. printf "checking target system type... "
  190. test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
  191. printf "%s\n" "$target"
  192. #
  193. # Convert to just ARCH
  194. #
  195. case "$target" in
  196. arm*) ARCH=arm ;;
  197. i?86*) ARCH=i386 ;;
  198. x86_64*) ARCH=x86_64 ;;
  199. mips-*|mipsel-*) ARCH=mips ;;
  200. microblaze-*) ARCH=microblaze ;;
  201. powerpc-*) ARCH=powerpc ;;
  202. unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
  203. *) fail "$0: unknown or unsupported target \"$target\"" ;;
  204. esac
  205. #
  206. # Try to get a conforming C99 freestanding environment
  207. #
  208. tryflag CFLAGS_C99FSE -std=c99
  209. tryflag CFLAGS_C99FSE -nostdinc
  210. tryflag CFLAGS_C99FSE -ffreestanding \
  211. || tryflag CFLAGS_C99FSE -fno-builtin
  212. tryflag CFLAGS_C99FSE -fexcess-precision=standard \
  213. || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
  214. tryflag CFLAGS_C99FSE -frounding-math
  215. #
  216. # Check for options that may be needed to prevent the compiler from
  217. # generating self-referential versions of memcpy,, memmove, memcmp,
  218. # and memset. Really, we should add a check to determine if this
  219. # option is sufficient, and if not, add a macro to cripple these
  220. # functions with volatile...
  221. #
  222. tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
  223. #
  224. # If debugging is explicitly enabled, don't auto-enable optimizations
  225. #
  226. if test "$debug" = yes ; then
  227. CFLAGS_AUTO=-g
  228. test "$optimize" = auto && optimize=no
  229. fi
  230. #
  231. # Possibly add a -O option to CFLAGS and select modules to optimize with
  232. # -O3 based on the status of --enable-optimize and provided CFLAGS.
  233. #
  234. printf "checking for optimization settings... "
  235. case "x$optimize" in
  236. xauto)
  237. if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
  238. printf "using provided CFLAGS\n" ;optimize=no
  239. else
  240. printf "using defaults\n" ; optimize=yes
  241. fi
  242. ;;
  243. xsize|xnone) printf "minimize size\n" ; optimize=size ;;
  244. xno|x) printf "disabled\n" ; optimize=no ;;
  245. *) printf "custom\n" ;;
  246. esac
  247. test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
  248. test "$optimize" = yes && optimize="internal,malloc,string"
  249. if fnmatch 'no|size' "$optimize" ; then :
  250. else
  251. printf "components to be optimized for speed:"
  252. while test "$optimize" ; do
  253. case "$optimize" in
  254. *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
  255. *) this=$optimize optimize=
  256. esac
  257. printf " $this"
  258. case "$this" in
  259. */*.c) ;;
  260. */*) this=$this*.c ;;
  261. *) this=$this/*.c ;;
  262. esac
  263. OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
  264. done
  265. OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
  266. printf "\n"
  267. fi
  268. # Always try -pipe
  269. tryflag CFLAGS_AUTO -pipe
  270. #
  271. # If debugging is disabled, omit frame pointer. Modern GCC does this
  272. # anyway on most archs even when debugging is enabled since the frame
  273. # pointer is no longer needed for debugging.
  274. #
  275. if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
  276. else
  277. tryflag CFLAGS_AUTO -fomit-frame-pointer
  278. fi
  279. #
  280. # Modern GCC wants to put DWARF tables (used for debugging and
  281. # unwinding) in the loaded part of the program where they are
  282. # unstrippable. These options force them back to debug sections (and
  283. # cause them not to get generated at all if debugging is off).
  284. #
  285. tryflag CFLAGS_AUTO -fno-unwind-tables
  286. tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
  287. #
  288. # The GNU toolchain defaults to assuming unmarked files need an
  289. # executable stack, potentially exposing vulnerabilities in programs
  290. # linked with such object files. Fix this.
  291. #
  292. tryflag CFLAGS_AUTO -Wa,--noexecstack
  293. #
  294. # On x86, make sure we don't have incompatible instruction set
  295. # extensions enabled by default. This is bad for making static binaries.
  296. # We cheat and use i486 rather than i386 because i386 really does not
  297. # work anyway (issues with atomic ops).
  298. #
  299. if test "$ARCH" = "i386" ; then
  300. fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
  301. fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
  302. fi
  303. #
  304. # Even with -std=c99, gcc accepts some constructs which are constraint
  305. # violations. We want to treat these as errors regardless of whether
  306. # other purely stylistic warnings are enabled -- especially implicit
  307. # function declarations, which are a dangerous programming error.
  308. #
  309. tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
  310. tryflag CFLAGS_AUTO -Werror=implicit-int
  311. tryflag CFLAGS_AUTO -Werror=pointer-sign
  312. tryflag CFLAGS_AUTO -Werror=pointer-arith
  313. if test "x$warnings" = xyes ; then
  314. tryflag CFLAGS_AUTO -Wall
  315. tryflag CFLAGS_AUTO -Wcast-align
  316. tryflag CFLAGS_AUTO -Wno-parentheses
  317. tryflag CFLAGS_AUTO -Wno-uninitialized
  318. tryflag CFLAGS_AUTO -Wno-missing-braces
  319. tryflag CFLAGS_AUTO -Wno-unused-value
  320. tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
  321. tryflag CFLAGS_AUTO -Wno-unknown-pragmas
  322. fi
  323. # Some patched GCC builds have these defaults messed up...
  324. tryflag CFLAGS_AUTO -fno-stack-protector
  325. tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
  326. # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
  327. LDFLAGS_DUMMY=
  328. tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
  329. printf "warning: disabling dynamic linking support\n"
  330. shared=no
  331. }
  332. # Find compiler runtime library
  333. test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
  334. test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
  335. test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
  336. && tryldflag LIBCC "$try_libcc"
  337. printf "using compiler runtime libraries: %s\n" "$LIBCC"
  338. # Figure out arch variants for archs with variants
  339. SUBARCH=
  340. t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
  341. if test "$ARCH" = "arm" ; then
  342. trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
  343. trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
  344. fi
  345. test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
  346. && SUBARCH=${SUBARCH}el
  347. test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
  348. && SUBARCH=${SUBARCH}el
  349. test "$SUBARCH" \
  350. && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
  351. case "$ARCH$SUBARCH" in
  352. arm) ASMSUBARCH=el ;;
  353. *) ASMSUBARCH=$SUBARCH ;;
  354. esac
  355. #
  356. # Some archs (powerpc) have different possible long double formats
  357. # that the compiler can be configured for. The logic for whether this
  358. # is supported is in bits/float.h; in general, it is not. We need to
  359. # check for mismatches here or code in printf, strotd, and scanf will
  360. # be dangerously incorrect because it depends on (1) the macros being
  361. # correct, and (2) IEEE semantics.
  362. #
  363. printf "checking whether compiler's long double definition matches float.h... "
  364. echo '#include <float.h>' > "$tmpc"
  365. echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
  366. echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
  367. echo '#endif' >> "$tmpc"
  368. if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
  369. -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  370. printf "yes\n"
  371. else
  372. printf "no\n"
  373. fail "$0: error: unsupported long double type"
  374. fi
  375. printf "creating config.mak... "
  376. cmdline=$(quote "$0")
  377. for i ; do cmdline="$cmdline $(quote "$i")" ; done
  378. exec 3>&1 1>config.mak
  379. cat << EOF
  380. # This version of config.mak was generated by:
  381. # $cmdline
  382. # Any changes made here will be lost if configure is re-run
  383. ARCH = $ARCH
  384. SUBARCH = $SUBARCH
  385. ASMSUBARCH = $ASMSUBARCH
  386. prefix = $prefix
  387. exec_prefix = $exec_prefix
  388. bindir = $bindir
  389. libdir = $libdir
  390. includedir = $includedir
  391. syslibdir = $syslibdir
  392. CC = $CC
  393. CFLAGS= $CFLAGS_AUTO $CFLAGS
  394. CFLAGS_C99FSE = $CFLAGS_C99FSE
  395. CFLAGS_MEMOPS = $CFLAGS_MEMOPS
  396. CPPFLAGS = $CPPFLAGS
  397. LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
  398. CROSS_COMPILE = $CROSS_COMPILE
  399. LIBCC = $LIBCC
  400. OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
  401. EOF
  402. test "x$static" = xno && echo "STATIC_LIBS ="
  403. test "x$shared" = xno && echo "SHARED_LIBS ="
  404. test "x$wrapper" = xno && echo "ALL_TOOLS ="
  405. test "x$wrapper" = xno && echo "TOOL_LIBS ="
  406. exec 1>&3 3>&-
  407. printf "done\n"