blob: 96f93b24cc705bde21bd3308bbeb0efacab09d5c [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
57tryflag () {
58printf "checking whether compiler accepts %s... " "$2"
59echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040060if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040061printf "yes\n"
62eval "$1=\"\${$1} \$2\""
63eval "$1=\${$1# }"
64return 0
65else
66printf "no\n"
67return 1
68fi
69}
70
Rich Felker08f70a32012-06-06 20:45:52 -040071tryldflag () {
72printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040073echo "typedef int x;" > "$tmpc"
Rich Felkercd31a1f2012-10-26 18:15:51 -040074if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -040075printf "yes\n"
76eval "$1=\"\${$1} \$2\""
77eval "$1=\${$1# }"
78return 0
79else
80printf "no\n"
81return 1
82fi
83}
84
Rich Felker64d2f8e2012-05-04 23:24:51 -040085
86
87# Beginning of actual script
88
Rich Felker08f70a32012-06-06 20:45:52 -040089CFLAGS_C99FSE=
90CFLAGS_AUTO=
91LDFLAGS_AUTO=
Rich Felker3d9e3a32012-11-08 17:20:50 -050092prefix=/usr/local/musl
93exec_prefix='$(prefix)'
94bindir='$(exec_prefix)/bin'
95libdir='$(prefix)/lib'
96includedir='$(prefix)/include'
97syslibdir='/lib'
Rich Felker278883d2012-06-03 16:22:13 -040098target=
Rich Felker64d2f8e2012-05-04 23:24:51 -040099debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500100warnings=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400101shared=yes
102static=yes
103
104for arg ; do
105case "$arg" in
106--help) usage ;;
107--prefix=*) prefix=${arg#*=} ;;
108--exec-prefix=*) exec_prefix=${arg#*=} ;;
109--bindir=*) bindir=${arg#*=} ;;
110--libdir=*) libdir=${arg#*=} ;;
111--includedir=*) includedir=${arg#*=} ;;
112--syslibdir=*) syslibdir=${arg#*=} ;;
113--enable-shared|--enable-shared=yes) shared=yes ;;
114--disable-shared|--enable-shared=no) shared=no ;;
115--enable-static|--enable-static=yes) static=yes ;;
116--disable-static|--enable-static=no) static=no ;;
117--enable-debug|--enable-debug=yes) debug=yes ;;
118--disable-debug|--enable-debug=no) debug=no ;;
119--enable-warnings|--enable-warnings=yes) warnings=yes ;;
120--disable-warnings|--enable-warnings=no) warnings=no ;;
121--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
122--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400123--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
124--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400125-* ) echo "$0: unknown option $arg" ;;
126CC=*) CC=${arg#*=} ;;
127CFLAGS=*) CFLAGS=${arg#*=} ;;
128CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
129LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400130CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400131LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400132*=*) ;;
133*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400134esac
135done
136
Rich Felker3d9e3a32012-11-08 17:20:50 -0500137for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
138stripdir $i
139done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400140
141#
142# Get a temp filename we can use
143#
144i=0
145set -C
146while : ; do i=$(($i+1))
147tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001482>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400149test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
150done
151set +C
152trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
153
154#
155# Find a C compiler to use
156#
157printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400158trycc ${CROSS_COMPILE}gcc
159trycc ${CROSS_COMPILE}c99
160trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400161printf "%s\n" "$CC"
162test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
163
164#
165# Only build musl-gcc wrapper if toolchain does not already target musl
166#
167if test -z "$wrapper" ; then
168printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400169if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400170echo yes
171printf "checking whether to build musl-gcc wrapper... "
172wrapper=yes
173while read line ; do
174case "$line" in */ld-musl-*) wrapper=no ;; esac
175done <<EOF
176$($CC -dumpspecs)
177EOF
178echo $wrapper
179else
180echo no
181fi
182fi
183
184
185
186#
Rich Felker278883d2012-06-03 16:22:13 -0400187# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400188#
Rich Felker278883d2012-06-03 16:22:13 -0400189printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400190test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400191printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400192
193#
194# Convert to just ARCH
195#
Rich Felker278883d2012-06-03 16:22:13 -0400196case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400197arm*) ARCH=arm ;;
198i?86*) ARCH=i386 ;;
199x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400200mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400201microblaze-*) ARCH=microblaze ;;
rofl0r1c8eb8b2012-11-09 23:36:55 +0100202powerpc-*) ARCH=powerpc ;;
Rich Felker278883d2012-06-03 16:22:13 -0400203unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
204*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400205esac
206
207#
208# Try to get a conforming C99 freestanding environment
209#
210tryflag CFLAGS_C99FSE -std=c99
211tryflag CFLAGS_C99FSE -nostdinc
212tryflag CFLAGS_C99FSE -ffreestanding \
213|| tryflag CFLAGS_C99FSE -fno-builtin
214tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400215|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400216tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400217
218#
219# Setup basic default CFLAGS: debug, optimization, and -pipe
220#
221if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
222else
223tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
224fi
225test "x$debug" = xyes && CFLAGS_AUTO="-g"
226tryflag CFLAGS_AUTO -pipe
227
228#
Rich Felkerb439c052012-08-29 09:36:02 -0400229# If debugging is disabled, omit frame pointer. Modern GCC does this
230# anyway on most archs even when debugging is enabled since the frame
231# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400232#
233if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
234else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400235tryflag CFLAGS_AUTO -fomit-frame-pointer
236fi
237
238#
Rich Felkerb439c052012-08-29 09:36:02 -0400239# Modern GCC wants to put DWARF tables (used for debugging and
240# unwinding) in the loaded part of the program where they are
241# unstrippable. These options force them back to debug sections (and
242# cause them not to get generated at all if debugging is off).
243#
244tryflag CFLAGS_AUTO -fno-unwind-tables
245tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
246
247#
Rich Felkeradefe832012-10-03 11:49:58 -0400248# The GNU toolchain defaults to assuming unmarked files need an
249# executable stack, potentially exposing vulnerabilities in programs
250# linked with such object files. Fix this.
251#
252tryflag CFLAGS_AUTO -Wa,--noexecstack
253
254#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400255# Some optimization levels add bloated alignment that hurt performance
256#
257tryflag CFLAGS_AUTO -falign-functions=1
258tryflag CFLAGS_AUTO -falign-labels=1
259tryflag CFLAGS_AUTO -falign-loops=1
260tryflag CFLAGS_AUTO -falign-jumps=1
261
262#
263# On x86, make sure we don't have incompatible instruction set
264# extensions enabled by default. This is bad for making static binaries.
265# We cheat and use i486 rather than i386 because i386 really does not
266# work anyway (issues with atomic ops).
267#
268if test "$ARCH" = "i386" ; then
Rich Felker80a45452012-10-25 14:52:12 -0400269fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
270fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400271fi
272
Rich Felker2384f272012-12-11 23:28:31 -0500273#
274# Even with -std=c99, gcc accepts some constructs which are constraint
275# violations. We want to treat these as errors regardless of whether
276# other purely stylistic warnings are enabled -- especially implicit
277# function declarations, which are a dangerous programming error.
278#
279tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
280tryflag CFLAGS_AUTO -Werror=implicit-int
281tryflag CFLAGS_AUTO -Werror=pointer-sign
282tryflag CFLAGS_AUTO -Werror=pointer-arith
283
Rich Felker64d2f8e2012-05-04 23:24:51 -0400284if test "x$warnings" = xyes ; then
285tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400286tryflag CFLAGS_AUTO -Wcast-align
287tryflag CFLAGS_AUTO -Wno-parentheses
288tryflag CFLAGS_AUTO -Wno-uninitialized
289tryflag CFLAGS_AUTO -Wno-missing-braces
290tryflag CFLAGS_AUTO -Wno-unused-value
291tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
292tryflag CFLAGS_AUTO -Wno-unknown-pragmas
293fi
294
Rich Felker0c5efde2012-06-06 22:00:08 -0400295# Some patched GCC builds have these defaults messed up...
296tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400297tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400298
Rich Felker498a1002012-06-07 00:32:22 -0400299# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
300LDFLAGS_DUMMY=
301tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
302printf "warning: disabling dynamic linking support\n"
303shared=no
304}
305
Rich Felker2c1cd232012-09-10 15:30:52 -0400306# Find compiler runtime library
307test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
308test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400309test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
310 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400311printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400312
Rich Felker64d2f8e2012-05-04 23:24:51 -0400313
314printf "creating config.mak... "
315
316exec 3>&1 1>config.mak
317
318
319cat << EOF
320# This version of config.mak was generated by configure
321# Any changes made here will be lost if configure is re-run
322ARCH = $ARCH
323prefix = $prefix
324exec_prefix = $exec_prefix
325bindir = $bindir
326libdir = $libdir
327includedir = $includedir
328syslibdir = $syslibdir
329CC = $CC
330CFLAGS= $CFLAGS_AUTO $CFLAGS
331CFLAGS_C99FSE = $CFLAGS_C99FSE
332CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400333LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400334CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400335LIBCC = $LIBCC
Rich Felker64d2f8e2012-05-04 23:24:51 -0400336EOF
337test "x$static" = xno && echo "STATIC_LIBS ="
338test "x$shared" = xno && echo "SHARED_LIBS ="
339test "x$wrapper" = xno && echo "ALL_TOOLS ="
340test "x$wrapper" = xno && echo "TOOL_LIBS ="
341exec 1>&3 3>&-
342
343printf "done\n"