blob: 042c41bd28790d98837748644d73de89a0143a76 [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:
27 --enable-debug build with debugging information [disabled]
28 --enable-warnings build with recommended warnings flags [disabled]
29 --enable-gcc-wrapper build musl-gcc toolchain wrapper [auto]
30 --disable-shared inhibit building shared library [enabled]
31 --disable-static inhibit building static library [enabled]
32
33Some influential environment variables:
34 CC C compiler command [detected]
35 CFLAGS C compiler flags [-Os -pipe ...]
Rich Felker94e920d2012-08-14 22:50:16 -040036 CROSS_COMPILE prefix for cross compiler and tools [none]
Rich Felker2c1cd232012-09-10 15:30:52 -040037 LIBCC compiler runtime library [detected]
Rich Felker64d2f8e2012-05-04 23:24:51 -040038
39Use these variables to override the choices made by configure.
40
41EOF
42exit 0
43}
44
45# Helper functions
46
47echo () { printf "%s\n" "$*" ; }
48fail () { echo "$*" ; exit 1 ; }
49fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
50cmdexists () { type "$1" >/dev/null 2>&1 ; }
51trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
52
Rich Felker3d9e3a32012-11-08 17:20:50 -050053stripdir () {
54while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
Rich Felker64d2f8e2012-05-04 23:24:51 -040055}
56
Rich Felker3e7f1862013-07-18 20:30:58 -040057trycppif () {
58printf "checking preprocessor condition %s... " "$1"
59echo "#if $1" > "$tmpc"
60echo "#error yes" >> "$tmpc"
61echo "#endif" >> "$tmpc"
62if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
63printf "false\n"
64return 1
65else
66printf "true\n"
67return 0
68fi
69}
70
Rich Felker64d2f8e2012-05-04 23:24:51 -040071tryflag () {
72printf "checking whether compiler accepts %s... " "$2"
73echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040074if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040075printf "yes\n"
76eval "$1=\"\${$1} \$2\""
77eval "$1=\${$1# }"
78return 0
79else
80printf "no\n"
81return 1
82fi
83}
84
Rich Felker08f70a32012-06-06 20:45:52 -040085tryldflag () {
86printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040087echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040088if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -040089printf "yes\n"
90eval "$1=\"\${$1} \$2\""
91eval "$1=\${$1# }"
92return 0
93else
94printf "no\n"
95return 1
96fi
97}
98
Rich Felker64d2f8e2012-05-04 23:24:51 -040099
100
101# Beginning of actual script
102
Rich Felker08f70a32012-06-06 20:45:52 -0400103CFLAGS_C99FSE=
104CFLAGS_AUTO=
105LDFLAGS_AUTO=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500106prefix=/usr/local/musl
107exec_prefix='$(prefix)'
108bindir='$(exec_prefix)/bin'
109libdir='$(prefix)/lib'
110includedir='$(prefix)/include'
111syslibdir='/lib'
Rich Felker278883d2012-06-03 16:22:13 -0400112target=
Rich Felker64d2f8e2012-05-04 23:24:51 -0400113debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500114warnings=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400115shared=yes
116static=yes
117
118for arg ; do
119case "$arg" in
120--help) usage ;;
121--prefix=*) prefix=${arg#*=} ;;
122--exec-prefix=*) exec_prefix=${arg#*=} ;;
123--bindir=*) bindir=${arg#*=} ;;
124--libdir=*) libdir=${arg#*=} ;;
125--includedir=*) includedir=${arg#*=} ;;
126--syslibdir=*) syslibdir=${arg#*=} ;;
127--enable-shared|--enable-shared=yes) shared=yes ;;
128--disable-shared|--enable-shared=no) shared=no ;;
129--enable-static|--enable-static=yes) static=yes ;;
130--disable-static|--enable-static=no) static=no ;;
131--enable-debug|--enable-debug=yes) debug=yes ;;
132--disable-debug|--enable-debug=no) debug=no ;;
133--enable-warnings|--enable-warnings=yes) warnings=yes ;;
134--disable-warnings|--enable-warnings=no) warnings=no ;;
135--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
136--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400137--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
138--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400139-* ) echo "$0: unknown option $arg" ;;
140CC=*) CC=${arg#*=} ;;
141CFLAGS=*) CFLAGS=${arg#*=} ;;
142CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
143LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400144CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400145LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400146*=*) ;;
147*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400148esac
149done
150
Rich Felker3d9e3a32012-11-08 17:20:50 -0500151for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
152stripdir $i
153done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400154
155#
156# Get a temp filename we can use
157#
158i=0
159set -C
160while : ; do i=$(($i+1))
161tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001622>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400163test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
164done
165set +C
166trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
167
168#
169# Find a C compiler to use
170#
171printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400172trycc ${CROSS_COMPILE}gcc
173trycc ${CROSS_COMPILE}c99
174trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400175printf "%s\n" "$CC"
176test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
177
178#
179# Only build musl-gcc wrapper if toolchain does not already target musl
180#
181if test -z "$wrapper" ; then
182printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400183if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400184echo yes
185printf "checking whether to build musl-gcc wrapper... "
186wrapper=yes
187while read line ; do
188case "$line" in */ld-musl-*) wrapper=no ;; esac
189done <<EOF
190$($CC -dumpspecs)
191EOF
192echo $wrapper
193else
194echo no
195fi
196fi
197
198
199
200#
Rich Felker278883d2012-06-03 16:22:13 -0400201# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400202#
Rich Felker278883d2012-06-03 16:22:13 -0400203printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400204test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400205printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400206
207#
208# Convert to just ARCH
209#
Rich Felker278883d2012-06-03 16:22:13 -0400210case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400211arm*) ARCH=arm ;;
212i?86*) ARCH=i386 ;;
213x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400214mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400215microblaze-*) ARCH=microblaze ;;
rofl0r1c8eb8b2012-11-09 23:36:55 +0100216powerpc-*) ARCH=powerpc ;;
Rich Felker278883d2012-06-03 16:22:13 -0400217unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
218*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400219esac
220
221#
222# Try to get a conforming C99 freestanding environment
223#
224tryflag CFLAGS_C99FSE -std=c99
225tryflag CFLAGS_C99FSE -nostdinc
226tryflag CFLAGS_C99FSE -ffreestanding \
227|| tryflag CFLAGS_C99FSE -fno-builtin
228tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400229|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400230tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400231
232#
233# Setup basic default CFLAGS: debug, optimization, and -pipe
234#
235if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
236else
237tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
238fi
239test "x$debug" = xyes && CFLAGS_AUTO="-g"
240tryflag CFLAGS_AUTO -pipe
241
242#
Rich Felkerb439c052012-08-29 09:36:02 -0400243# If debugging is disabled, omit frame pointer. Modern GCC does this
244# anyway on most archs even when debugging is enabled since the frame
245# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400246#
247if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
248else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400249tryflag CFLAGS_AUTO -fomit-frame-pointer
250fi
251
252#
Rich Felkerb439c052012-08-29 09:36:02 -0400253# Modern GCC wants to put DWARF tables (used for debugging and
254# unwinding) in the loaded part of the program where they are
255# unstrippable. These options force them back to debug sections (and
256# cause them not to get generated at all if debugging is off).
257#
258tryflag CFLAGS_AUTO -fno-unwind-tables
259tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
260
261#
Rich Felkeradefe832012-10-03 11:49:58 -0400262# The GNU toolchain defaults to assuming unmarked files need an
263# executable stack, potentially exposing vulnerabilities in programs
264# linked with such object files. Fix this.
265#
266tryflag CFLAGS_AUTO -Wa,--noexecstack
267
268#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400269# Some optimization levels add bloated alignment that hurt performance
270#
271tryflag CFLAGS_AUTO -falign-functions=1
272tryflag CFLAGS_AUTO -falign-labels=1
273tryflag CFLAGS_AUTO -falign-loops=1
274tryflag CFLAGS_AUTO -falign-jumps=1
275
276#
277# On x86, make sure we don't have incompatible instruction set
278# extensions enabled by default. This is bad for making static binaries.
279# We cheat and use i486 rather than i386 because i386 really does not
280# work anyway (issues with atomic ops).
281#
282if test "$ARCH" = "i386" ; then
Rich Felker80a45452012-10-25 14:52:12 -0400283fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
284fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400285fi
286
Rich Felker2384f272012-12-11 23:28:31 -0500287#
288# Even with -std=c99, gcc accepts some constructs which are constraint
289# violations. We want to treat these as errors regardless of whether
290# other purely stylistic warnings are enabled -- especially implicit
291# function declarations, which are a dangerous programming error.
292#
293tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
294tryflag CFLAGS_AUTO -Werror=implicit-int
295tryflag CFLAGS_AUTO -Werror=pointer-sign
296tryflag CFLAGS_AUTO -Werror=pointer-arith
297
Rich Felker64d2f8e2012-05-04 23:24:51 -0400298if test "x$warnings" = xyes ; then
299tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400300tryflag CFLAGS_AUTO -Wcast-align
301tryflag CFLAGS_AUTO -Wno-parentheses
302tryflag CFLAGS_AUTO -Wno-uninitialized
303tryflag CFLAGS_AUTO -Wno-missing-braces
304tryflag CFLAGS_AUTO -Wno-unused-value
305tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
306tryflag CFLAGS_AUTO -Wno-unknown-pragmas
307fi
308
Rich Felker0c5efde2012-06-06 22:00:08 -0400309# Some patched GCC builds have these defaults messed up...
310tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400311tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400312
Rich Felker498a1002012-06-07 00:32:22 -0400313# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
314LDFLAGS_DUMMY=
315tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
316printf "warning: disabling dynamic linking support\n"
317shared=no
318}
319
Rich Felker2c1cd232012-09-10 15:30:52 -0400320# Find compiler runtime library
321test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
322test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400323test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
324 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400325printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400326
Rich Felker3e7f1862013-07-18 20:30:58 -0400327# Figure out arch variants for archs with variants
328SUBARCH=
329t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
330
331if test "$ARCH" = "arm" ; then
332trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
333trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
334fi
335
336test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
337&& SUBARCH=${SUBARCH}el
338
339test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
340&& SUBARCH=${SUBARCH}el
341
342test "$SUBARCH" \
343&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400344
345printf "creating config.mak... "
346
347exec 3>&1 1>config.mak
348
349
350cat << EOF
351# This version of config.mak was generated by configure
352# Any changes made here will be lost if configure is re-run
353ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400354SUBARCH = $SUBARCH
Rich Felker64d2f8e2012-05-04 23:24:51 -0400355prefix = $prefix
356exec_prefix = $exec_prefix
357bindir = $bindir
358libdir = $libdir
359includedir = $includedir
360syslibdir = $syslibdir
361CC = $CC
362CFLAGS= $CFLAGS_AUTO $CFLAGS
363CFLAGS_C99FSE = $CFLAGS_C99FSE
364CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400365LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400366CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400367LIBCC = $LIBCC
Rich Felker64d2f8e2012-05-04 23:24:51 -0400368EOF
369test "x$static" = xno && echo "STATIC_LIBS ="
370test "x$shared" = xno && echo "SHARED_LIBS ="
371test "x$wrapper" = xno && echo "ALL_TOOLS ="
372test "x$wrapper" = xno && echo "TOOL_LIBS ="
373exec 1>&3 3>&-
374
375printf "done\n"