blob: 87207b62abd6370a577c572dfeed24213b5ff78e [file] [log] [blame]
Rich Felker64d2f8e2012-05-04 23:24:51 -04001#!/bin/sh
2
3usage () {
4cat <<EOF
Rich Felker278883d2012-06-03 16:22:13 -04005Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
Rich Felker64d2f8e2012-05-04 23:24:51 -04006
7To assign environment variables (e.g., CC, CFLAGS...), specify them as
8VAR=VALUE. See below for descriptions of some of the useful variables.
9
10Defaults for the options are specified in brackets.
11
12Installation directories:
13 --prefix=PREFIX main installation prefix [/usr/local/musl]
14 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
15
16Fine tuning of the installation directories:
17 --bindir=DIR user executables [EPREFIX/bin]
18 --libdir=DIR library files for the linker [PREFIX/lib]
19 --includedir=DIR include files for the C compiler [PREFIX/include]
20 --syslibdir=DIR location for the dynamic linker [/lib]
21
22System types:
23 --target=TARGET configure to run on target TARGET [detected]
Rich Felker278883d2012-06-03 16:22:13 -040024 --host=HOST same as --target
Rich Felker64d2f8e2012-05-04 23:24:51 -040025
26Optional features:
Rich Felkera80847d2013-07-22 21:22:04 -040027 --enable-optimize=... optimize listed components for speed over size [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040028 --enable-debug build with debugging information [disabled]
29 --enable-warnings build with recommended warnings flags [disabled]
30 --enable-gcc-wrapper build musl-gcc toolchain wrapper [auto]
31 --disable-shared inhibit building shared library [enabled]
32 --disable-static inhibit building static library [enabled]
33
34Some influential environment variables:
35 CC C compiler command [detected]
36 CFLAGS C compiler flags [-Os -pipe ...]
Rich Felker94e920d2012-08-14 22:50:16 -040037 CROSS_COMPILE prefix for cross compiler and tools [none]
Rich Felker2c1cd232012-09-10 15:30:52 -040038 LIBCC compiler runtime library [detected]
Rich Felker64d2f8e2012-05-04 23:24:51 -040039
40Use these variables to override the choices made by configure.
41
42EOF
43exit 0
44}
45
46# Helper functions
47
48echo () { printf "%s\n" "$*" ; }
49fail () { echo "$*" ; exit 1 ; }
50fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
51cmdexists () { type "$1" >/dev/null 2>&1 ; }
52trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
53
Rich Felker3d9e3a32012-11-08 17:20:50 -050054stripdir () {
55while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
Rich Felker64d2f8e2012-05-04 23:24:51 -040056}
57
Rich Felker3e7f1862013-07-18 20:30:58 -040058trycppif () {
59printf "checking preprocessor condition %s... " "$1"
Rich Felkerdf065782013-07-18 20:37:19 -040060echo "typedef int x;" > "$tmpc"
61echo "#if $1" >> "$tmpc"
Rich Felker3e7f1862013-07-18 20:30:58 -040062echo "#error yes" >> "$tmpc"
63echo "#endif" >> "$tmpc"
64if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
65printf "false\n"
66return 1
67else
68printf "true\n"
69return 0
70fi
71}
72
Rich Felker64d2f8e2012-05-04 23:24:51 -040073tryflag () {
74printf "checking whether compiler accepts %s... " "$2"
75echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040076if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040077printf "yes\n"
78eval "$1=\"\${$1} \$2\""
79eval "$1=\${$1# }"
80return 0
81else
82printf "no\n"
83return 1
84fi
85}
86
Rich Felker08f70a32012-06-06 20:45:52 -040087tryldflag () {
88printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040089echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040090if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -040091printf "yes\n"
92eval "$1=\"\${$1} \$2\""
93eval "$1=\${$1# }"
94return 0
95else
96printf "no\n"
97return 1
98fi
99}
100
Rich Felker64d2f8e2012-05-04 23:24:51 -0400101
102
103# Beginning of actual script
104
Rich Felker08f70a32012-06-06 20:45:52 -0400105CFLAGS_C99FSE=
106CFLAGS_AUTO=
107LDFLAGS_AUTO=
Rich Felkera80847d2013-07-22 21:22:04 -0400108OPTIMIZE_GLOBS=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500109prefix=/usr/local/musl
110exec_prefix='$(prefix)'
111bindir='$(exec_prefix)/bin'
112libdir='$(prefix)/lib'
113includedir='$(prefix)/include'
114syslibdir='/lib'
Rich Felker278883d2012-06-03 16:22:13 -0400115target=
Rich Felkera80847d2013-07-22 21:22:04 -0400116optimize=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400117debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500118warnings=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400119shared=yes
120static=yes
121
122for arg ; do
123case "$arg" in
124--help) usage ;;
125--prefix=*) prefix=${arg#*=} ;;
126--exec-prefix=*) exec_prefix=${arg#*=} ;;
127--bindir=*) bindir=${arg#*=} ;;
128--libdir=*) libdir=${arg#*=} ;;
129--includedir=*) includedir=${arg#*=} ;;
130--syslibdir=*) syslibdir=${arg#*=} ;;
131--enable-shared|--enable-shared=yes) shared=yes ;;
132--disable-shared|--enable-shared=no) shared=no ;;
133--enable-static|--enable-static=yes) static=yes ;;
134--disable-static|--enable-static=no) static=no ;;
Rich Felkera80847d2013-07-22 21:22:04 -0400135--enable-optimize) optimize=yes ;;
136--enable-optimize=*) optimize=${arg#*=} ;;
137--disable-optimize) optimize=no ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400138--enable-debug|--enable-debug=yes) debug=yes ;;
139--disable-debug|--enable-debug=no) debug=no ;;
140--enable-warnings|--enable-warnings=yes) warnings=yes ;;
141--disable-warnings|--enable-warnings=no) warnings=no ;;
142--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
143--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400144--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
145--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400146-* ) echo "$0: unknown option $arg" ;;
147CC=*) CC=${arg#*=} ;;
148CFLAGS=*) CFLAGS=${arg#*=} ;;
149CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
150LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400151CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400152LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400153*=*) ;;
154*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400155esac
156done
157
Rich Felker3d9e3a32012-11-08 17:20:50 -0500158for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
159stripdir $i
160done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400161
162#
163# Get a temp filename we can use
164#
165i=0
166set -C
167while : ; do i=$(($i+1))
168tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001692>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400170test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
171done
172set +C
173trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
174
175#
176# Find a C compiler to use
177#
178printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400179trycc ${CROSS_COMPILE}gcc
180trycc ${CROSS_COMPILE}c99
181trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400182printf "%s\n" "$CC"
183test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
184
185#
186# Only build musl-gcc wrapper if toolchain does not already target musl
187#
188if test -z "$wrapper" ; then
189printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400190if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400191echo yes
192printf "checking whether to build musl-gcc wrapper... "
193wrapper=yes
194while read line ; do
195case "$line" in */ld-musl-*) wrapper=no ;; esac
196done <<EOF
197$($CC -dumpspecs)
198EOF
199echo $wrapper
200else
201echo no
202fi
203fi
204
205
206
207#
Rich Felker278883d2012-06-03 16:22:13 -0400208# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400209#
Rich Felker278883d2012-06-03 16:22:13 -0400210printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400211test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400212printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400213
214#
215# Convert to just ARCH
216#
Rich Felker278883d2012-06-03 16:22:13 -0400217case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400218arm*) ARCH=arm ;;
219i?86*) ARCH=i386 ;;
220x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400221mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400222microblaze-*) ARCH=microblaze ;;
rofl0r1c8eb8b2012-11-09 23:36:55 +0100223powerpc-*) ARCH=powerpc ;;
Rich Felker278883d2012-06-03 16:22:13 -0400224unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
225*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400226esac
227
228#
229# Try to get a conforming C99 freestanding environment
230#
231tryflag CFLAGS_C99FSE -std=c99
232tryflag CFLAGS_C99FSE -nostdinc
233tryflag CFLAGS_C99FSE -ffreestanding \
234|| tryflag CFLAGS_C99FSE -fno-builtin
235tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400236|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400237tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400238
Rich Felkera80847d2013-07-22 21:22:04 -0400239
Rich Felker64d2f8e2012-05-04 23:24:51 -0400240#
Rich Felkera80847d2013-07-22 21:22:04 -0400241# If debugging is explicitly enabled, don't auto-enable optimizations
Rich Felker64d2f8e2012-05-04 23:24:51 -0400242#
Rich Felkera80847d2013-07-22 21:22:04 -0400243if test "$debug" = yes ; then
244CFLAGS_AUTO=-g
245test "$optimize" = auto && optimize=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400246fi
Rich Felkera80847d2013-07-22 21:22:04 -0400247
248#
249# Possibly add a -O option to CFLAGS and select modules to optimize with
250# -O3 based on the status of --enable-optimize and provided CFLAGS.
251#
252printf "checking for optimization settings... "
253case "x$optimize" in
254xauto)
255if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
256printf "using provided CFLAGS\n" ;optimize=no
257else
258printf "using defaults\n" ; optimize=yes
259fi
260;;
261xsize|xnone) printf "minimize size\n" ; optimize=size ;;
262xno|x) printf "disabled\n" ; optimize=no ;;
263*) printf "custom\n" ;;
264esac
265
266test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
Rich Felker43d25312013-07-24 23:21:45 -0400267test "$optimize" = yes && optimize="internal,malloc,string"
Rich Felkera80847d2013-07-22 21:22:04 -0400268
269if fnmatch 'no|size' "$optimize" ; then :
270else
271printf "components to be optimized for speed:"
272while test "$optimize" ; do
273case "$optimize" in
274*,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
275*) this=$optimize optimize=
276esac
277printf " $this"
278case "$this" in
279*/*.c) ;;
280*/*) this=$this*.c ;;
281*) this=$this/*.c ;;
282esac
283OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
284done
285OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
286printf "\n"
287fi
288
289# Always try -pipe
Rich Felker64d2f8e2012-05-04 23:24:51 -0400290tryflag CFLAGS_AUTO -pipe
291
292#
Rich Felkerb439c052012-08-29 09:36:02 -0400293# If debugging is disabled, omit frame pointer. Modern GCC does this
294# anyway on most archs even when debugging is enabled since the frame
295# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400296#
297if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
298else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400299tryflag CFLAGS_AUTO -fomit-frame-pointer
300fi
301
302#
Rich Felkerb439c052012-08-29 09:36:02 -0400303# Modern GCC wants to put DWARF tables (used for debugging and
304# unwinding) in the loaded part of the program where they are
305# unstrippable. These options force them back to debug sections (and
306# cause them not to get generated at all if debugging is off).
307#
308tryflag CFLAGS_AUTO -fno-unwind-tables
309tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
310
311#
Rich Felkeradefe832012-10-03 11:49:58 -0400312# The GNU toolchain defaults to assuming unmarked files need an
313# executable stack, potentially exposing vulnerabilities in programs
314# linked with such object files. Fix this.
315#
316tryflag CFLAGS_AUTO -Wa,--noexecstack
317
318#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400319# On x86, make sure we don't have incompatible instruction set
320# extensions enabled by default. This is bad for making static binaries.
321# We cheat and use i486 rather than i386 because i386 really does not
322# work anyway (issues with atomic ops).
323#
324if test "$ARCH" = "i386" ; then
Rich Felker80a45452012-10-25 14:52:12 -0400325fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
326fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400327fi
328
Rich Felker2384f272012-12-11 23:28:31 -0500329#
330# Even with -std=c99, gcc accepts some constructs which are constraint
331# violations. We want to treat these as errors regardless of whether
332# other purely stylistic warnings are enabled -- especially implicit
333# function declarations, which are a dangerous programming error.
334#
335tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
336tryflag CFLAGS_AUTO -Werror=implicit-int
337tryflag CFLAGS_AUTO -Werror=pointer-sign
338tryflag CFLAGS_AUTO -Werror=pointer-arith
339
Rich Felker64d2f8e2012-05-04 23:24:51 -0400340if test "x$warnings" = xyes ; then
341tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400342tryflag CFLAGS_AUTO -Wcast-align
343tryflag CFLAGS_AUTO -Wno-parentheses
344tryflag CFLAGS_AUTO -Wno-uninitialized
345tryflag CFLAGS_AUTO -Wno-missing-braces
346tryflag CFLAGS_AUTO -Wno-unused-value
347tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
348tryflag CFLAGS_AUTO -Wno-unknown-pragmas
349fi
350
Rich Felker0c5efde2012-06-06 22:00:08 -0400351# Some patched GCC builds have these defaults messed up...
352tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400353tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400354
Rich Felker498a1002012-06-07 00:32:22 -0400355# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
356LDFLAGS_DUMMY=
357tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
358printf "warning: disabling dynamic linking support\n"
359shared=no
360}
361
Rich Felker2c1cd232012-09-10 15:30:52 -0400362# Find compiler runtime library
363test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
364test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400365test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
366 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400367printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400368
Rich Felker3e7f1862013-07-18 20:30:58 -0400369# Figure out arch variants for archs with variants
370SUBARCH=
371t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
372
373if test "$ARCH" = "arm" ; then
374trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
375trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
376fi
377
378test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
379&& SUBARCH=${SUBARCH}el
380
381test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
382&& SUBARCH=${SUBARCH}el
383
384test "$SUBARCH" \
385&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400386
387printf "creating config.mak... "
388
389exec 3>&1 1>config.mak
390
391
392cat << EOF
393# This version of config.mak was generated by configure
394# Any changes made here will be lost if configure is re-run
395ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400396SUBARCH = $SUBARCH
Rich Felker64d2f8e2012-05-04 23:24:51 -0400397prefix = $prefix
398exec_prefix = $exec_prefix
399bindir = $bindir
400libdir = $libdir
401includedir = $includedir
402syslibdir = $syslibdir
403CC = $CC
404CFLAGS= $CFLAGS_AUTO $CFLAGS
405CFLAGS_C99FSE = $CFLAGS_C99FSE
406CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400407LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400408CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400409LIBCC = $LIBCC
Rich Felkera80847d2013-07-22 21:22:04 -0400410OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
Rich Felker64d2f8e2012-05-04 23:24:51 -0400411EOF
412test "x$static" = xno && echo "STATIC_LIBS ="
413test "x$shared" = xno && echo "SHARED_LIBS ="
414test "x$wrapper" = xno && echo "ALL_TOOLS ="
415test "x$wrapper" = xno && echo "TOOL_LIBS ="
416exec 1>&3 3>&-
417
418printf "done\n"