1
0

configure 10 KB

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