blob: 48465f7cb62532b8f9aa91b7d58c4eceaa624e5b [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=
Rich Felker4a1f55e2013-08-01 17:12:23 -0400107CFLAGS_MEMOPS=
Rich Felker08f70a32012-06-06 20:45:52 -0400108LDFLAGS_AUTO=
Rich Felkera80847d2013-07-22 21:22:04 -0400109OPTIMIZE_GLOBS=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500110prefix=/usr/local/musl
111exec_prefix='$(prefix)'
112bindir='$(exec_prefix)/bin'
113libdir='$(prefix)/lib'
114includedir='$(prefix)/include'
115syslibdir='/lib'
Rich Felker278883d2012-06-03 16:22:13 -0400116target=
Rich Felkera80847d2013-07-22 21:22:04 -0400117optimize=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400118debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500119warnings=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400120shared=yes
121static=yes
122
123for arg ; do
124case "$arg" in
125--help) usage ;;
126--prefix=*) prefix=${arg#*=} ;;
127--exec-prefix=*) exec_prefix=${arg#*=} ;;
128--bindir=*) bindir=${arg#*=} ;;
129--libdir=*) libdir=${arg#*=} ;;
130--includedir=*) includedir=${arg#*=} ;;
131--syslibdir=*) syslibdir=${arg#*=} ;;
132--enable-shared|--enable-shared=yes) shared=yes ;;
133--disable-shared|--enable-shared=no) shared=no ;;
134--enable-static|--enable-static=yes) static=yes ;;
135--disable-static|--enable-static=no) static=no ;;
Rich Felkera80847d2013-07-22 21:22:04 -0400136--enable-optimize) optimize=yes ;;
137--enable-optimize=*) optimize=${arg#*=} ;;
138--disable-optimize) optimize=no ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400139--enable-debug|--enable-debug=yes) debug=yes ;;
140--disable-debug|--enable-debug=no) debug=no ;;
141--enable-warnings|--enable-warnings=yes) warnings=yes ;;
142--disable-warnings|--enable-warnings=no) warnings=no ;;
143--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
144--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400145--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
146--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400147-* ) echo "$0: unknown option $arg" ;;
148CC=*) CC=${arg#*=} ;;
149CFLAGS=*) CFLAGS=${arg#*=} ;;
150CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
151LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400152CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400153LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400154*=*) ;;
155*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400156esac
157done
158
Rich Felker3d9e3a32012-11-08 17:20:50 -0500159for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
160stripdir $i
161done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400162
163#
164# Get a temp filename we can use
165#
166i=0
167set -C
168while : ; do i=$(($i+1))
169tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001702>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400171test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
172done
173set +C
174trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
175
176#
177# Find a C compiler to use
178#
179printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400180trycc ${CROSS_COMPILE}gcc
181trycc ${CROSS_COMPILE}c99
182trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400183printf "%s\n" "$CC"
184test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
185
186#
187# Only build musl-gcc wrapper if toolchain does not already target musl
188#
189if test -z "$wrapper" ; then
190printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400191if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400192echo yes
193printf "checking whether to build musl-gcc wrapper... "
194wrapper=yes
195while read line ; do
196case "$line" in */ld-musl-*) wrapper=no ;; esac
197done <<EOF
198$($CC -dumpspecs)
199EOF
200echo $wrapper
201else
202echo no
203fi
204fi
205
206
207
208#
Rich Felker278883d2012-06-03 16:22:13 -0400209# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400210#
Rich Felker278883d2012-06-03 16:22:13 -0400211printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400212test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400213printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400214
215#
216# Convert to just ARCH
217#
Rich Felker278883d2012-06-03 16:22:13 -0400218case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400219arm*) ARCH=arm ;;
220i?86*) ARCH=i386 ;;
221x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400222mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400223microblaze-*) ARCH=microblaze ;;
rofl0r1c8eb8b2012-11-09 23:36:55 +0100224powerpc-*) ARCH=powerpc ;;
Rich Felker278883d2012-06-03 16:22:13 -0400225unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
226*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400227esac
228
229#
230# Try to get a conforming C99 freestanding environment
231#
232tryflag CFLAGS_C99FSE -std=c99
233tryflag CFLAGS_C99FSE -nostdinc
234tryflag CFLAGS_C99FSE -ffreestanding \
235|| tryflag CFLAGS_C99FSE -fno-builtin
236tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400237|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400238tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400239
Rich Felker4a1f55e2013-08-01 17:12:23 -0400240#
241# Check for options that may be needed to prevent the compiler from
242# generating self-referential versions of memcpy,, memmove, memcmp,
243# and memset. Really, we should add a check to determine if this
244# option is sufficient, and if not, add a macro to cripple these
245# functions with volatile...
246#
247tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
Rich Felkera80847d2013-07-22 21:22:04 -0400248
Rich Felker64d2f8e2012-05-04 23:24:51 -0400249#
Rich Felkera80847d2013-07-22 21:22:04 -0400250# If debugging is explicitly enabled, don't auto-enable optimizations
Rich Felker64d2f8e2012-05-04 23:24:51 -0400251#
Rich Felkera80847d2013-07-22 21:22:04 -0400252if test "$debug" = yes ; then
253CFLAGS_AUTO=-g
254test "$optimize" = auto && optimize=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400255fi
Rich Felkera80847d2013-07-22 21:22:04 -0400256
257#
258# Possibly add a -O option to CFLAGS and select modules to optimize with
259# -O3 based on the status of --enable-optimize and provided CFLAGS.
260#
261printf "checking for optimization settings... "
262case "x$optimize" in
263xauto)
264if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
265printf "using provided CFLAGS\n" ;optimize=no
266else
267printf "using defaults\n" ; optimize=yes
268fi
269;;
270xsize|xnone) printf "minimize size\n" ; optimize=size ;;
271xno|x) printf "disabled\n" ; optimize=no ;;
272*) printf "custom\n" ;;
273esac
274
275test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
Rich Felker43d25312013-07-24 23:21:45 -0400276test "$optimize" = yes && optimize="internal,malloc,string"
Rich Felkera80847d2013-07-22 21:22:04 -0400277
278if fnmatch 'no|size' "$optimize" ; then :
279else
280printf "components to be optimized for speed:"
281while test "$optimize" ; do
282case "$optimize" in
283*,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
284*) this=$optimize optimize=
285esac
286printf " $this"
287case "$this" in
288*/*.c) ;;
289*/*) this=$this*.c ;;
290*) this=$this/*.c ;;
291esac
292OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
293done
294OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
295printf "\n"
296fi
297
298# Always try -pipe
Rich Felker64d2f8e2012-05-04 23:24:51 -0400299tryflag CFLAGS_AUTO -pipe
300
301#
Rich Felkerb439c052012-08-29 09:36:02 -0400302# If debugging is disabled, omit frame pointer. Modern GCC does this
303# anyway on most archs even when debugging is enabled since the frame
304# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400305#
306if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
307else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400308tryflag CFLAGS_AUTO -fomit-frame-pointer
309fi
310
311#
Rich Felkerb439c052012-08-29 09:36:02 -0400312# Modern GCC wants to put DWARF tables (used for debugging and
313# unwinding) in the loaded part of the program where they are
314# unstrippable. These options force them back to debug sections (and
315# cause them not to get generated at all if debugging is off).
316#
317tryflag CFLAGS_AUTO -fno-unwind-tables
318tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
319
320#
Rich Felkeradefe832012-10-03 11:49:58 -0400321# The GNU toolchain defaults to assuming unmarked files need an
322# executable stack, potentially exposing vulnerabilities in programs
323# linked with such object files. Fix this.
324#
325tryflag CFLAGS_AUTO -Wa,--noexecstack
326
327#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400328# On x86, make sure we don't have incompatible instruction set
329# extensions enabled by default. This is bad for making static binaries.
330# We cheat and use i486 rather than i386 because i386 really does not
331# work anyway (issues with atomic ops).
332#
333if test "$ARCH" = "i386" ; then
Rich Felker80a45452012-10-25 14:52:12 -0400334fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
335fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400336fi
337
Rich Felker2384f272012-12-11 23:28:31 -0500338#
339# Even with -std=c99, gcc accepts some constructs which are constraint
340# violations. We want to treat these as errors regardless of whether
341# other purely stylistic warnings are enabled -- especially implicit
342# function declarations, which are a dangerous programming error.
343#
344tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
345tryflag CFLAGS_AUTO -Werror=implicit-int
346tryflag CFLAGS_AUTO -Werror=pointer-sign
347tryflag CFLAGS_AUTO -Werror=pointer-arith
348
Rich Felker64d2f8e2012-05-04 23:24:51 -0400349if test "x$warnings" = xyes ; then
350tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400351tryflag CFLAGS_AUTO -Wcast-align
352tryflag CFLAGS_AUTO -Wno-parentheses
353tryflag CFLAGS_AUTO -Wno-uninitialized
354tryflag CFLAGS_AUTO -Wno-missing-braces
355tryflag CFLAGS_AUTO -Wno-unused-value
356tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
357tryflag CFLAGS_AUTO -Wno-unknown-pragmas
358fi
359
Rich Felker0c5efde2012-06-06 22:00:08 -0400360# Some patched GCC builds have these defaults messed up...
361tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400362tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400363
Rich Felker498a1002012-06-07 00:32:22 -0400364# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
365LDFLAGS_DUMMY=
366tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
367printf "warning: disabling dynamic linking support\n"
368shared=no
369}
370
Rich Felker2c1cd232012-09-10 15:30:52 -0400371# Find compiler runtime library
372test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
373test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400374test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
375 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400376printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400377
Rich Felker3e7f1862013-07-18 20:30:58 -0400378# Figure out arch variants for archs with variants
379SUBARCH=
380t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
381
382if test "$ARCH" = "arm" ; then
383trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
384trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
385fi
386
387test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
388&& SUBARCH=${SUBARCH}el
389
390test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
391&& SUBARCH=${SUBARCH}el
392
393test "$SUBARCH" \
394&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400395
Rich Felker86cc54b2013-08-02 19:34:22 -0400396#
397# Some archs (powerpc) have different possible long double formats
398# that the compiler can be configured for. The logic for whether this
399# is supported is in bits/float.h; in general, it is not. We need to
400# check for mismatches here or code in printf, strotd, and scanf will
401# be dangerously incorrect because it depends on (1) the macros being
402# correct, and (2) IEEE semantics.
403#
404printf "checking whether compiler's long double definition matches float.h... "
405echo '#include <float.h>' > "$tmpc"
406echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
407echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
408echo '#endif' >> "$tmpc"
409if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
410 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
411printf "yes\n"
412else
413printf "no\n"
414fail "$0: error: unsupported long double type"
415fi
416
Rich Felker64d2f8e2012-05-04 23:24:51 -0400417printf "creating config.mak... "
418
419exec 3>&1 1>config.mak
420
421
422cat << EOF
423# This version of config.mak was generated by configure
424# Any changes made here will be lost if configure is re-run
425ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400426SUBARCH = $SUBARCH
Rich Felker64d2f8e2012-05-04 23:24:51 -0400427prefix = $prefix
428exec_prefix = $exec_prefix
429bindir = $bindir
430libdir = $libdir
431includedir = $includedir
432syslibdir = $syslibdir
433CC = $CC
434CFLAGS= $CFLAGS_AUTO $CFLAGS
435CFLAGS_C99FSE = $CFLAGS_C99FSE
Rich Felker4a1f55e2013-08-01 17:12:23 -0400436CFLAGS_MEMOPS = $CFLAGS_MEMOPS
Rich Felker64d2f8e2012-05-04 23:24:51 -0400437CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400438LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400439CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400440LIBCC = $LIBCC
Rich Felkera80847d2013-07-22 21:22:04 -0400441OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
Rich Felker64d2f8e2012-05-04 23:24:51 -0400442EOF
443test "x$static" = xno && echo "STATIC_LIBS ="
444test "x$shared" = xno && echo "SHARED_LIBS ="
445test "x$wrapper" = xno && echo "ALL_TOOLS ="
446test "x$wrapper" = xno && echo "TOOL_LIBS ="
447exec 1>&3 3>&-
448
449printf "done\n"