blob: b679986f7179dafd54683018ac0a0dbd5142a0a4 [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"
Rich Felkerdf065782013-07-18 20:37:19 -040059echo "typedef int x;" > "$tmpc"
60echo "#if $1" >> "$tmpc"
Rich Felker3e7f1862013-07-18 20:30:58 -040061echo "#error yes" >> "$tmpc"
62echo "#endif" >> "$tmpc"
63if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
64printf "false\n"
65return 1
66else
67printf "true\n"
68return 0
69fi
70}
71
Rich Felker64d2f8e2012-05-04 23:24:51 -040072tryflag () {
73printf "checking whether compiler accepts %s... " "$2"
74echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040075if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040076printf "yes\n"
77eval "$1=\"\${$1} \$2\""
78eval "$1=\${$1# }"
79return 0
80else
81printf "no\n"
82return 1
83fi
84}
85
Rich Felker08f70a32012-06-06 20:45:52 -040086tryldflag () {
87printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040088echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040089if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -040090printf "yes\n"
91eval "$1=\"\${$1} \$2\""
92eval "$1=\${$1# }"
93return 0
94else
95printf "no\n"
96return 1
97fi
98}
99
Rich Felker64d2f8e2012-05-04 23:24:51 -0400100
101
102# Beginning of actual script
103
Rich Felker08f70a32012-06-06 20:45:52 -0400104CFLAGS_C99FSE=
105CFLAGS_AUTO=
106LDFLAGS_AUTO=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500107prefix=/usr/local/musl
108exec_prefix='$(prefix)'
109bindir='$(exec_prefix)/bin'
110libdir='$(prefix)/lib'
111includedir='$(prefix)/include'
112syslibdir='/lib'
Rich Felker278883d2012-06-03 16:22:13 -0400113target=
Rich Felker64d2f8e2012-05-04 23:24:51 -0400114debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500115warnings=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400116shared=yes
117static=yes
118
119for arg ; do
120case "$arg" in
121--help) usage ;;
122--prefix=*) prefix=${arg#*=} ;;
123--exec-prefix=*) exec_prefix=${arg#*=} ;;
124--bindir=*) bindir=${arg#*=} ;;
125--libdir=*) libdir=${arg#*=} ;;
126--includedir=*) includedir=${arg#*=} ;;
127--syslibdir=*) syslibdir=${arg#*=} ;;
128--enable-shared|--enable-shared=yes) shared=yes ;;
129--disable-shared|--enable-shared=no) shared=no ;;
130--enable-static|--enable-static=yes) static=yes ;;
131--disable-static|--enable-static=no) static=no ;;
132--enable-debug|--enable-debug=yes) debug=yes ;;
133--disable-debug|--enable-debug=no) debug=no ;;
134--enable-warnings|--enable-warnings=yes) warnings=yes ;;
135--disable-warnings|--enable-warnings=no) warnings=no ;;
136--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
137--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400138--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
139--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400140-* ) echo "$0: unknown option $arg" ;;
141CC=*) CC=${arg#*=} ;;
142CFLAGS=*) CFLAGS=${arg#*=} ;;
143CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
144LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400145CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400146LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400147*=*) ;;
148*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400149esac
150done
151
Rich Felker3d9e3a32012-11-08 17:20:50 -0500152for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
153stripdir $i
154done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400155
156#
157# Get a temp filename we can use
158#
159i=0
160set -C
161while : ; do i=$(($i+1))
162tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001632>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400164test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
165done
166set +C
167trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
168
169#
170# Find a C compiler to use
171#
172printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400173trycc ${CROSS_COMPILE}gcc
174trycc ${CROSS_COMPILE}c99
175trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400176printf "%s\n" "$CC"
177test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
178
179#
180# Only build musl-gcc wrapper if toolchain does not already target musl
181#
182if test -z "$wrapper" ; then
183printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400184if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400185echo yes
186printf "checking whether to build musl-gcc wrapper... "
187wrapper=yes
188while read line ; do
189case "$line" in */ld-musl-*) wrapper=no ;; esac
190done <<EOF
191$($CC -dumpspecs)
192EOF
193echo $wrapper
194else
195echo no
196fi
197fi
198
199
200
201#
Rich Felker278883d2012-06-03 16:22:13 -0400202# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400203#
Rich Felker278883d2012-06-03 16:22:13 -0400204printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400205test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400206printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400207
208#
209# Convert to just ARCH
210#
Rich Felker278883d2012-06-03 16:22:13 -0400211case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400212arm*) ARCH=arm ;;
213i?86*) ARCH=i386 ;;
214x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400215mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400216microblaze-*) ARCH=microblaze ;;
rofl0r1c8eb8b2012-11-09 23:36:55 +0100217powerpc-*) ARCH=powerpc ;;
Rich Felker278883d2012-06-03 16:22:13 -0400218unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
219*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400220esac
221
222#
223# Try to get a conforming C99 freestanding environment
224#
225tryflag CFLAGS_C99FSE -std=c99
226tryflag CFLAGS_C99FSE -nostdinc
227tryflag CFLAGS_C99FSE -ffreestanding \
228|| tryflag CFLAGS_C99FSE -fno-builtin
229tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400230|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400231tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400232
233#
234# Setup basic default CFLAGS: debug, optimization, and -pipe
235#
236if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
237else
238tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
239fi
240test "x$debug" = xyes && CFLAGS_AUTO="-g"
241tryflag CFLAGS_AUTO -pipe
242
243#
Rich Felkerb439c052012-08-29 09:36:02 -0400244# If debugging is disabled, omit frame pointer. Modern GCC does this
245# anyway on most archs even when debugging is enabled since the frame
246# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400247#
248if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
249else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400250tryflag CFLAGS_AUTO -fomit-frame-pointer
251fi
252
253#
Rich Felkerb439c052012-08-29 09:36:02 -0400254# Modern GCC wants to put DWARF tables (used for debugging and
255# unwinding) in the loaded part of the program where they are
256# unstrippable. These options force them back to debug sections (and
257# cause them not to get generated at all if debugging is off).
258#
259tryflag CFLAGS_AUTO -fno-unwind-tables
260tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
261
262#
Rich Felkeradefe832012-10-03 11:49:58 -0400263# The GNU toolchain defaults to assuming unmarked files need an
264# executable stack, potentially exposing vulnerabilities in programs
265# linked with such object files. Fix this.
266#
267tryflag CFLAGS_AUTO -Wa,--noexecstack
268
269#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400270# Some optimization levels add bloated alignment that hurt performance
271#
272tryflag CFLAGS_AUTO -falign-functions=1
273tryflag CFLAGS_AUTO -falign-labels=1
274tryflag CFLAGS_AUTO -falign-loops=1
275tryflag CFLAGS_AUTO -falign-jumps=1
276
277#
278# On x86, make sure we don't have incompatible instruction set
279# extensions enabled by default. This is bad for making static binaries.
280# We cheat and use i486 rather than i386 because i386 really does not
281# work anyway (issues with atomic ops).
282#
283if test "$ARCH" = "i386" ; then
Rich Felker80a45452012-10-25 14:52:12 -0400284fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
285fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400286fi
287
Rich Felker2384f272012-12-11 23:28:31 -0500288#
289# Even with -std=c99, gcc accepts some constructs which are constraint
290# violations. We want to treat these as errors regardless of whether
291# other purely stylistic warnings are enabled -- especially implicit
292# function declarations, which are a dangerous programming error.
293#
294tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
295tryflag CFLAGS_AUTO -Werror=implicit-int
296tryflag CFLAGS_AUTO -Werror=pointer-sign
297tryflag CFLAGS_AUTO -Werror=pointer-arith
298
Rich Felker64d2f8e2012-05-04 23:24:51 -0400299if test "x$warnings" = xyes ; then
300tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400301tryflag CFLAGS_AUTO -Wcast-align
302tryflag CFLAGS_AUTO -Wno-parentheses
303tryflag CFLAGS_AUTO -Wno-uninitialized
304tryflag CFLAGS_AUTO -Wno-missing-braces
305tryflag CFLAGS_AUTO -Wno-unused-value
306tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
307tryflag CFLAGS_AUTO -Wno-unknown-pragmas
308fi
309
Rich Felker0c5efde2012-06-06 22:00:08 -0400310# Some patched GCC builds have these defaults messed up...
311tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400312tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400313
Rich Felker498a1002012-06-07 00:32:22 -0400314# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
315LDFLAGS_DUMMY=
316tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
317printf "warning: disabling dynamic linking support\n"
318shared=no
319}
320
Rich Felker2c1cd232012-09-10 15:30:52 -0400321# Find compiler runtime library
322test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
323test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400324test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
325 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400326printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400327
Rich Felker3e7f1862013-07-18 20:30:58 -0400328# Figure out arch variants for archs with variants
329SUBARCH=
330t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
331
332if test "$ARCH" = "arm" ; then
333trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
334trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
335fi
336
337test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
338&& SUBARCH=${SUBARCH}el
339
340test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
341&& SUBARCH=${SUBARCH}el
342
343test "$SUBARCH" \
344&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400345
346printf "creating config.mak... "
347
348exec 3>&1 1>config.mak
349
350
351cat << EOF
352# This version of config.mak was generated by configure
353# Any changes made here will be lost if configure is re-run
354ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400355SUBARCH = $SUBARCH
Rich Felker64d2f8e2012-05-04 23:24:51 -0400356prefix = $prefix
357exec_prefix = $exec_prefix
358bindir = $bindir
359libdir = $libdir
360includedir = $includedir
361syslibdir = $syslibdir
362CC = $CC
363CFLAGS= $CFLAGS_AUTO $CFLAGS
364CFLAGS_C99FSE = $CFLAGS_C99FSE
365CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400366LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400367CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400368LIBCC = $LIBCC
Rich Felker64d2f8e2012-05-04 23:24:51 -0400369EOF
370test "x$static" = xno && echo "STATIC_LIBS ="
371test "x$shared" = xno && echo "SHARED_LIBS ="
372test "x$wrapper" = xno && echo "ALL_TOOLS ="
373test "x$wrapper" = xno && echo "TOOL_LIBS ="
374exec 1>&3 3>&-
375
376printf "done\n"