blob: c1bcac7c7c67c3ecdf7337e5e976e200394b3efd [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
53setdir () {
54if eval "test -z \"\${$1}\"" ; then eval "$1=\$2"
55else eval "fnmatch '*/' \"\${$1}\"" && eval "$1=\${$1%/}" ; fi
56}
57
58tryflag () {
59printf "checking whether compiler accepts %s... " "$2"
60echo "typedef int x;" > "$tmpc"
Rich Felker01e5a1b2012-10-18 23:02:53 -040061if $CC "$2" -c -o /dev/null "$tmpc" 2>/dev/null ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040062printf "yes\n"
63eval "$1=\"\${$1} \$2\""
64eval "$1=\${$1# }"
65return 0
66else
67printf "no\n"
68return 1
69fi
70}
71
Rich Felker08f70a32012-06-06 20:45:52 -040072tryldflag () {
73printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040074echo "typedef int x;" > "$tmpc"
Rich Felker01e5a1b2012-10-18 23:02:53 -040075if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" 2>/dev/null ; then
Rich Felker08f70a32012-06-06 20:45:52 -040076printf "yes\n"
77eval "$1=\"\${$1} \$2\""
78eval "$1=\${$1# }"
79return 0
80else
81printf "no\n"
82return 1
83fi
84}
85
Rich Felker64d2f8e2012-05-04 23:24:51 -040086
87
88# Beginning of actual script
89
Rich Felker08f70a32012-06-06 20:45:52 -040090CFLAGS_C99FSE=
91CFLAGS_AUTO=
92LDFLAGS_AUTO=
Rich Felker64d2f8e2012-05-04 23:24:51 -040093prefix=
94exec_prefix=
95bindir=
96libdir=
97includedir=
98syslibdir=
Rich Felker278883d2012-06-03 16:22:13 -040099target=
Rich Felker64d2f8e2012-05-04 23:24:51 -0400100debug=no
101warnings=
102shared=yes
103static=yes
104
105for arg ; do
106case "$arg" in
107--help) usage ;;
108--prefix=*) prefix=${arg#*=} ;;
109--exec-prefix=*) exec_prefix=${arg#*=} ;;
110--bindir=*) bindir=${arg#*=} ;;
111--libdir=*) libdir=${arg#*=} ;;
112--includedir=*) includedir=${arg#*=} ;;
113--syslibdir=*) syslibdir=${arg#*=} ;;
114--enable-shared|--enable-shared=yes) shared=yes ;;
115--disable-shared|--enable-shared=no) shared=no ;;
116--enable-static|--enable-static=yes) static=yes ;;
117--disable-static|--enable-static=no) static=no ;;
118--enable-debug|--enable-debug=yes) debug=yes ;;
119--disable-debug|--enable-debug=no) debug=no ;;
120--enable-warnings|--enable-warnings=yes) warnings=yes ;;
121--disable-warnings|--enable-warnings=no) warnings=no ;;
122--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
123--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400124--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
125--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400126-* ) echo "$0: unknown option $arg" ;;
127CC=*) CC=${arg#*=} ;;
128CFLAGS=*) CFLAGS=${arg#*=} ;;
129CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
130LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400131CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400132LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400133*=*) ;;
134*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400135esac
136done
137
138setdir prefix /usr/local/musl
139setdir exec_prefix '$(prefix)'
140setdir bindir '$(exec_prefix)/bin'
141setdir libdir '$(prefix)/lib'
142setdir includedir '$(prefix)/include'
143setdir syslibdir '/lib'
144
145#
146# Get a temp filename we can use
147#
148i=0
149set -C
150while : ; do i=$(($i+1))
151tmpc="./conf$$-$PPID-$i.c"
1522>/dev/null > "$tmpc" && break
153test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
154done
155set +C
156trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
157
158#
159# Find a C compiler to use
160#
161printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400162trycc ${CROSS_COMPILE}gcc
163trycc ${CROSS_COMPILE}c99
164trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400165printf "%s\n" "$CC"
166test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
167
168#
169# Only build musl-gcc wrapper if toolchain does not already target musl
170#
171if test -z "$wrapper" ; then
172printf "checking whether compiler is gcc... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400173if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -0400174echo yes
175printf "checking whether to build musl-gcc wrapper... "
176wrapper=yes
177while read line ; do
178case "$line" in */ld-musl-*) wrapper=no ;; esac
179done <<EOF
180$($CC -dumpspecs)
181EOF
182echo $wrapper
183else
184echo no
185fi
186fi
187
188
189
190#
Rich Felker278883d2012-06-03 16:22:13 -0400191# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400192#
Rich Felker278883d2012-06-03 16:22:13 -0400193printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400194test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400195printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400196
197#
198# Convert to just ARCH
199#
Rich Felker278883d2012-06-03 16:22:13 -0400200case "$target" in
Rich Felker64d2f8e2012-05-04 23:24:51 -0400201arm*) ARCH=arm ;;
202i?86*) ARCH=i386 ;;
203x86_64*) ARCH=x86_64 ;;
Rich Felker721564a2012-08-05 17:23:38 -0400204mips-*|mipsel-*) ARCH=mips ;;
Rich Felker8c0a3d92012-09-29 01:05:31 -0400205microblaze-*) ARCH=microblaze ;;
Rich Felker278883d2012-06-03 16:22:13 -0400206unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
207*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400208esac
209
210#
211# Try to get a conforming C99 freestanding environment
212#
213tryflag CFLAGS_C99FSE -std=c99
214tryflag CFLAGS_C99FSE -nostdinc
215tryflag CFLAGS_C99FSE -ffreestanding \
216|| tryflag CFLAGS_C99FSE -fno-builtin
217tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400218|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400219tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400220
221#
222# Setup basic default CFLAGS: debug, optimization, and -pipe
223#
224if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
225else
226tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
227fi
228test "x$debug" = xyes && CFLAGS_AUTO="-g"
229tryflag CFLAGS_AUTO -pipe
230
231#
Rich Felkerb439c052012-08-29 09:36:02 -0400232# If debugging is disabled, omit frame pointer. Modern GCC does this
233# anyway on most archs even when debugging is enabled since the frame
234# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400235#
236if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
237else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400238tryflag CFLAGS_AUTO -fomit-frame-pointer
239fi
240
241#
Rich Felkerb439c052012-08-29 09:36:02 -0400242# Modern GCC wants to put DWARF tables (used for debugging and
243# unwinding) in the loaded part of the program where they are
244# unstrippable. These options force them back to debug sections (and
245# cause them not to get generated at all if debugging is off).
246#
247tryflag CFLAGS_AUTO -fno-unwind-tables
248tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
249
250#
Rich Felkeradefe832012-10-03 11:49:58 -0400251# The GNU toolchain defaults to assuming unmarked files need an
252# executable stack, potentially exposing vulnerabilities in programs
253# linked with such object files. Fix this.
254#
255tryflag CFLAGS_AUTO -Wa,--noexecstack
256
257#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400258# Some optimization levels add bloated alignment that hurt performance
259#
260tryflag CFLAGS_AUTO -falign-functions=1
261tryflag CFLAGS_AUTO -falign-labels=1
262tryflag CFLAGS_AUTO -falign-loops=1
263tryflag CFLAGS_AUTO -falign-jumps=1
264
265#
266# On x86, make sure we don't have incompatible instruction set
267# extensions enabled by default. This is bad for making static binaries.
268# We cheat and use i486 rather than i386 because i386 really does not
269# work anyway (issues with atomic ops).
270#
271if test "$ARCH" = "i386" ; then
272fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryflag CFLAGS_AUTO -march=i486
273fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryflag CFLAGS_AUTO -mtune=generic
274fi
275
276if test "x$warnings" = xyes ; then
277tryflag CFLAGS_AUTO -Wall
278tryflag CFLAGS_AUTO -Wpointer-arith
279tryflag CFLAGS_AUTO -Wcast-align
280tryflag CFLAGS_AUTO -Wno-parentheses
281tryflag CFLAGS_AUTO -Wno-uninitialized
282tryflag CFLAGS_AUTO -Wno-missing-braces
283tryflag CFLAGS_AUTO -Wno-unused-value
284tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
285tryflag CFLAGS_AUTO -Wno-unknown-pragmas
286fi
287
Rich Felker0c5efde2012-06-06 22:00:08 -0400288# Some patched GCC builds have these defaults messed up...
289tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker2bd05a42012-08-25 17:13:28 -0400290tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400291
Rich Felker498a1002012-06-07 00:32:22 -0400292# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
293LDFLAGS_DUMMY=
294tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
295printf "warning: disabling dynamic linking support\n"
296shared=no
297}
298
Rich Felker2c1cd232012-09-10 15:30:52 -0400299# Find compiler runtime library
300test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
301test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
302printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400303
Rich Felker64d2f8e2012-05-04 23:24:51 -0400304
305printf "creating config.mak... "
306
307exec 3>&1 1>config.mak
308
309
310cat << EOF
311# This version of config.mak was generated by configure
312# Any changes made here will be lost if configure is re-run
313ARCH = $ARCH
314prefix = $prefix
315exec_prefix = $exec_prefix
316bindir = $bindir
317libdir = $libdir
318includedir = $includedir
319syslibdir = $syslibdir
320CC = $CC
321CFLAGS= $CFLAGS_AUTO $CFLAGS
322CFLAGS_C99FSE = $CFLAGS_C99FSE
323CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400324LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker94e920d2012-08-14 22:50:16 -0400325CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400326LIBCC = $LIBCC
Rich Felker64d2f8e2012-05-04 23:24:51 -0400327EOF
328test "x$static" = xno && echo "STATIC_LIBS ="
329test "x$shared" = xno && echo "SHARED_LIBS ="
330test "x$wrapper" = xno && echo "ALL_TOOLS ="
331test "x$wrapper" = xno && echo "TOOL_LIBS ="
332exec 1>&3 3>&-
333
334printf "done\n"