configure 12 KB

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