configure 10 KB

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