1
0

configure 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. Use these variables to override the choices made by configure.
  30. EOF
  31. exit 0
  32. }
  33. # Helper functions
  34. echo () { printf "%s\n" "$*" ; }
  35. fail () { echo "$*" ; exit 1 ; }
  36. fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
  37. cmdexists () { type "$1" >/dev/null 2>&1 ; }
  38. trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
  39. setdir () {
  40. if eval "test -z \"\${$1}\"" ; then eval "$1=\$2"
  41. else eval "fnmatch '*/' \"\${$1}\"" && eval "$1=\${$1%/}" ; fi
  42. }
  43. tryflag () {
  44. printf "checking whether compiler accepts %s... " "$2"
  45. echo "typedef int x;" > "$tmpc"
  46. if "$CC" "$2" -c -o /dev/null "$tmpc" 2>/dev/null ; then
  47. printf "yes\n"
  48. eval "$1=\"\${$1} \$2\""
  49. eval "$1=\${$1# }"
  50. return 0
  51. else
  52. printf "no\n"
  53. return 1
  54. fi
  55. }
  56. tryldflag () {
  57. printf "checking whether linker accepts %s... " "$2"
  58. echo "typedef int x;" > "$tmpc"
  59. if "$CC" -nostdlib -shared "$2" -o /dev/null "$tmpc" 2>/dev/null ; 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. # Beginning of actual script
  70. CFLAGS_C99FSE=
  71. CFLAGS_AUTO=
  72. LDFLAGS_AUTO=
  73. prefix=
  74. exec_prefix=
  75. bindir=
  76. libdir=
  77. includedir=
  78. syslibdir=
  79. target=
  80. debug=no
  81. warnings=
  82. shared=yes
  83. static=yes
  84. for arg ; do
  85. case "$arg" in
  86. --help) usage ;;
  87. --prefix=*) prefix=${arg#*=} ;;
  88. --exec-prefix=*) exec_prefix=${arg#*=} ;;
  89. --bindir=*) bindir=${arg#*=} ;;
  90. --libdir=*) libdir=${arg#*=} ;;
  91. --includedir=*) includedir=${arg#*=} ;;
  92. --syslibdir=*) syslibdir=${arg#*=} ;;
  93. --enable-shared|--enable-shared=yes) shared=yes ;;
  94. --disable-shared|--enable-shared=no) shared=no ;;
  95. --enable-static|--enable-static=yes) static=yes ;;
  96. --disable-static|--enable-static=no) static=no ;;
  97. --enable-debug|--enable-debug=yes) debug=yes ;;
  98. --disable-debug|--enable-debug=no) debug=no ;;
  99. --enable-warnings|--enable-warnings=yes) warnings=yes ;;
  100. --disable-warnings|--enable-warnings=no) warnings=no ;;
  101. --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
  102. --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
  103. --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
  104. --host=*|--target=*) target=${arg#*=} ;;
  105. -* ) echo "$0: unknown option $arg" ;;
  106. CC=*) CC=${arg#*=} ;;
  107. CFLAGS=*) CFLAGS=${arg#*=} ;;
  108. CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
  109. LDFLAGS=*) LDFLAGS=${arg#*=} ;;
  110. CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
  111. *=*) ;;
  112. *) target=$arg ;;
  113. esac
  114. done
  115. setdir prefix /usr/local/musl
  116. setdir exec_prefix '$(prefix)'
  117. setdir bindir '$(exec_prefix)/bin'
  118. setdir libdir '$(prefix)/lib'
  119. setdir includedir '$(prefix)/include'
  120. setdir syslibdir '/lib'
  121. #
  122. # Get a temp filename we can use
  123. #
  124. i=0
  125. set -C
  126. while : ; do i=$(($i+1))
  127. tmpc="./conf$$-$PPID-$i.c"
  128. 2>/dev/null > "$tmpc" && break
  129. test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
  130. done
  131. set +C
  132. trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
  133. #
  134. # Find a C compiler to use
  135. #
  136. printf "checking for C compiler... "
  137. trycc ${CROSS_COMPILE}gcc
  138. trycc ${CROSS_COMPILE}c99
  139. trycc ${CROSS_COMPILE}cc
  140. printf "%s\n" "$CC"
  141. test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
  142. #
  143. # Only build musl-gcc wrapper if toolchain does not already target musl
  144. #
  145. if test -z "$wrapper" ; then
  146. printf "checking whether compiler is gcc... "
  147. if fnmatch '*gcc\ version*' "$("$CC" -v 2>&1)" ; then
  148. echo yes
  149. printf "checking whether to build musl-gcc wrapper... "
  150. wrapper=yes
  151. while read line ; do
  152. case "$line" in */ld-musl-*) wrapper=no ;; esac
  153. done <<EOF
  154. $($CC -dumpspecs)
  155. EOF
  156. echo $wrapper
  157. else
  158. echo no
  159. fi
  160. fi
  161. #
  162. # Find the target architecture
  163. #
  164. printf "checking target system type... "
  165. test -n "$target" || target=$("$CC" -dumpmachine 2>/dev/null) || target=unknown
  166. printf "%s\n" "$target"
  167. #
  168. # Convert to just ARCH
  169. #
  170. case "$target" in
  171. arm*) ARCH=arm ;;
  172. i?86*) ARCH=i386 ;;
  173. x86_64*) ARCH=x86_64 ;;
  174. mips-*|mipsel-*) ARCH=mips ;;
  175. unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
  176. *) fail "$0: unknown or unsupported target \"$target\"" ;;
  177. esac
  178. #
  179. # Try to get a conforming C99 freestanding environment
  180. #
  181. tryflag CFLAGS_C99FSE -std=c99
  182. tryflag CFLAGS_C99FSE -nostdinc
  183. tryflag CFLAGS_C99FSE -ffreestanding \
  184. || tryflag CFLAGS_C99FSE -fno-builtin
  185. tryflag CFLAGS_C99FSE -fexcess-precision=standard \
  186. || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
  187. tryflag CFLAGS_C99FSE -frounding-math
  188. #
  189. # Setup basic default CFLAGS: debug, optimization, and -pipe
  190. #
  191. if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
  192. else
  193. tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
  194. fi
  195. test "x$debug" = xyes && CFLAGS_AUTO="-g"
  196. tryflag CFLAGS_AUTO -pipe
  197. #
  198. # If debugging is disabled, omit frame pointer. Modern GCC does this
  199. # anyway on most archs even when debugging is enabled since the frame
  200. # pointer is no longer needed for debugging.
  201. #
  202. if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
  203. else
  204. tryflag CFLAGS_AUTO -fomit-frame-pointer
  205. fi
  206. #
  207. # Modern GCC wants to put DWARF tables (used for debugging and
  208. # unwinding) in the loaded part of the program where they are
  209. # unstrippable. These options force them back to debug sections (and
  210. # cause them not to get generated at all if debugging is off).
  211. #
  212. tryflag CFLAGS_AUTO -fno-unwind-tables
  213. tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
  214. #
  215. # Some optimization levels add bloated alignment that hurt performance
  216. #
  217. tryflag CFLAGS_AUTO -falign-functions=1
  218. tryflag CFLAGS_AUTO -falign-labels=1
  219. tryflag CFLAGS_AUTO -falign-loops=1
  220. tryflag CFLAGS_AUTO -falign-jumps=1
  221. #
  222. # On x86, make sure we don't have incompatible instruction set
  223. # extensions enabled by default. This is bad for making static binaries.
  224. # We cheat and use i486 rather than i386 because i386 really does not
  225. # work anyway (issues with atomic ops).
  226. #
  227. if test "$ARCH" = "i386" ; then
  228. fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryflag CFLAGS_AUTO -march=i486
  229. fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryflag CFLAGS_AUTO -mtune=generic
  230. fi
  231. if test "x$warnings" = xyes ; then
  232. tryflag CFLAGS_AUTO -Wall
  233. tryflag CFLAGS_AUTO -Wpointer-arith
  234. tryflag CFLAGS_AUTO -Wcast-align
  235. tryflag CFLAGS_AUTO -Wno-parentheses
  236. tryflag CFLAGS_AUTO -Wno-uninitialized
  237. tryflag CFLAGS_AUTO -Wno-missing-braces
  238. tryflag CFLAGS_AUTO -Wno-unused-value
  239. tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
  240. tryflag CFLAGS_AUTO -Wno-unknown-pragmas
  241. fi
  242. # Some patched GCC builds have these defaults messed up...
  243. tryflag CFLAGS_AUTO -fno-stack-protector
  244. tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
  245. # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
  246. LDFLAGS_DUMMY=
  247. tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
  248. printf "warning: disabling dynamic linking support\n"
  249. shared=no
  250. }
  251. printf "creating config.mak... "
  252. exec 3>&1 1>config.mak
  253. cat << EOF
  254. # This version of config.mak was generated by configure
  255. # Any changes made here will be lost if configure is re-run
  256. ARCH = $ARCH
  257. prefix = $prefix
  258. exec_prefix = $exec_prefix
  259. bindir = $bindir
  260. libdir = $libdir
  261. includedir = $includedir
  262. syslibdir = $syslibdir
  263. CC = $CC
  264. CFLAGS= $CFLAGS_AUTO $CFLAGS
  265. CFLAGS_C99FSE = $CFLAGS_C99FSE
  266. CPPFLAGS = $CPPFLAGS
  267. LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
  268. CROSS_COMPILE = $CROSS_COMPILE
  269. EOF
  270. test "x$static" = xno && echo "STATIC_LIBS ="
  271. test "x$shared" = xno && echo "SHARED_LIBS ="
  272. test "x$wrapper" = xno && echo "ALL_TOOLS ="
  273. test "x$wrapper" = xno && echo "TOOL_LIBS ="
  274. exec 1>&3 3>&-
  275. printf "done\n"