blob: 6daa5b3686cb280ca48df4605873a82951143a40 [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 ...]
36
37Use these variables to override the choices made by configure.
38
39EOF
40exit 0
41}
42
43# Helper functions
44
45echo () { printf "%s\n" "$*" ; }
46fail () { echo "$*" ; exit 1 ; }
47fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
48cmdexists () { type "$1" >/dev/null 2>&1 ; }
49trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
50
51setdir () {
52if eval "test -z \"\${$1}\"" ; then eval "$1=\$2"
53else eval "fnmatch '*/' \"\${$1}\"" && eval "$1=\${$1%/}" ; fi
54}
55
56tryflag () {
57printf "checking whether compiler accepts %s... " "$2"
58echo "typedef int x;" > "$tmpc"
59if "$CC" "$2" -c -o /dev/null "$tmpc" 2>/dev/null ; then
60printf "yes\n"
61eval "$1=\"\${$1} \$2\""
62eval "$1=\${$1# }"
63return 0
64else
65printf "no\n"
66return 1
67fi
68}
69
Rich Felker08f70a32012-06-06 20:45:52 -040070tryldflag () {
71printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040072echo "typedef int x;" > "$tmpc"
Rich Felkerf1fd7572012-06-07 00:27:34 -040073if "$CC" -nostdlib -shared "$2" -o /dev/null "$tmpc" 2>/dev/null ; then
Rich Felker08f70a32012-06-06 20:45:52 -040074printf "yes\n"
75eval "$1=\"\${$1} \$2\""
76eval "$1=\${$1# }"
77return 0
78else
79printf "no\n"
80return 1
81fi
82}
83
Rich Felker64d2f8e2012-05-04 23:24:51 -040084
85
86# Beginning of actual script
87
Rich Felker08f70a32012-06-06 20:45:52 -040088CFLAGS_C99FSE=
89CFLAGS_AUTO=
90LDFLAGS_AUTO=
Rich Felker64d2f8e2012-05-04 23:24:51 -040091prefix=
92exec_prefix=
93bindir=
94libdir=
95includedir=
96syslibdir=
Rich Felker278883d2012-06-03 16:22:13 -040097target=
Rich Felker64d2f8e2012-05-04 23:24:51 -040098debug=no
99warnings=
100shared=yes
101static=yes
102
103for arg ; do
104case "$arg" in
105--help) usage ;;
106--prefix=*) prefix=${arg#*=} ;;
107--exec-prefix=*) exec_prefix=${arg#*=} ;;
108--bindir=*) bindir=${arg#*=} ;;
109--libdir=*) libdir=${arg#*=} ;;
110--includedir=*) includedir=${arg#*=} ;;
111--syslibdir=*) syslibdir=${arg#*=} ;;
112--enable-shared|--enable-shared=yes) shared=yes ;;
113--disable-shared|--enable-shared=no) shared=no ;;
114--enable-static|--enable-static=yes) static=yes ;;
115--disable-static|--enable-static=no) static=no ;;
116--enable-debug|--enable-debug=yes) debug=yes ;;
117--disable-debug|--enable-debug=no) debug=no ;;
118--enable-warnings|--enable-warnings=yes) warnings=yes ;;
119--disable-warnings|--enable-warnings=no) warnings=no ;;
120--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
121--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400122--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
123--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400124-* ) echo "$0: unknown option $arg" ;;
125CC=*) CC=${arg#*=} ;;
126CFLAGS=*) CFLAGS=${arg#*=} ;;
127CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
128LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400129*=*) ;;
130*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400131esac
132done
133
134setdir prefix /usr/local/musl
135setdir exec_prefix '$(prefix)'
136setdir bindir '$(exec_prefix)/bin'
137setdir libdir '$(prefix)/lib'
138setdir includedir '$(prefix)/include'
139setdir syslibdir '/lib'
140
141#
142# Get a temp filename we can use
143#
144i=0
145set -C
146while : ; do i=$(($i+1))
147tmpc="./conf$$-$PPID-$i.c"
1482>/dev/null > "$tmpc" && break
149test "$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... "
158trycc gcc
159trycc c99
160trycc cc
161printf "%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 Felkerc5f3add2012-05-14 14:52:55 -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... "
190test -n "$target" || target=$("$CC" -dumpmachine 2>/dev/null) || target=unknown
191printf "%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 Felker63150042012-07-11 04:22:13 -0400200mips-*) ARCH=mips ;;
Rich Felker278883d2012-06-03 16:22:13 -0400201unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
202*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400203esac
204
205#
206# Try to get a conforming C99 freestanding environment
207#
208tryflag CFLAGS_C99FSE -std=c99
209tryflag CFLAGS_C99FSE -nostdinc
210tryflag CFLAGS_C99FSE -ffreestanding \
211|| tryflag CFLAGS_C99FSE -fno-builtin
212tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400213|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400214tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400215
216#
217# Setup basic default CFLAGS: debug, optimization, and -pipe
218#
219if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
220else
221tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
222fi
223test "x$debug" = xyes && CFLAGS_AUTO="-g"
224tryflag CFLAGS_AUTO -pipe
225
226#
227# If debugging is disabled, omit bloated DWARF2 unwind tables & frame ptr
228#
229if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
230else
231tryflag CFLAGS_AUTO -fno-unwind-tables
232tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
233tryflag CFLAGS_AUTO -fomit-frame-pointer
234fi
235
236#
237# Some optimization levels add bloated alignment that hurt performance
238#
239tryflag CFLAGS_AUTO -falign-functions=1
240tryflag CFLAGS_AUTO -falign-labels=1
241tryflag CFLAGS_AUTO -falign-loops=1
242tryflag CFLAGS_AUTO -falign-jumps=1
243
244#
245# On x86, make sure we don't have incompatible instruction set
246# extensions enabled by default. This is bad for making static binaries.
247# We cheat and use i486 rather than i386 because i386 really does not
248# work anyway (issues with atomic ops).
249#
250if test "$ARCH" = "i386" ; then
251fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryflag CFLAGS_AUTO -march=i486
252fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryflag CFLAGS_AUTO -mtune=generic
253fi
254
255if test "x$warnings" = xyes ; then
256tryflag CFLAGS_AUTO -Wall
257tryflag CFLAGS_AUTO -Wpointer-arith
258tryflag CFLAGS_AUTO -Wcast-align
259tryflag CFLAGS_AUTO -Wno-parentheses
260tryflag CFLAGS_AUTO -Wno-uninitialized
261tryflag CFLAGS_AUTO -Wno-missing-braces
262tryflag CFLAGS_AUTO -Wno-unused-value
263tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
264tryflag CFLAGS_AUTO -Wno-unknown-pragmas
265fi
266
Rich Felker0c5efde2012-06-06 22:00:08 -0400267# Some patched GCC builds have these defaults messed up...
268tryflag CFLAGS_AUTO -fno-stack-protector
Rich Felker08f70a32012-06-06 20:45:52 -0400269tryldflag LDFLAGS_AUTO -Wl,--hash-style=sysv
270
Rich Felker498a1002012-06-07 00:32:22 -0400271# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
272LDFLAGS_DUMMY=
273tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
274printf "warning: disabling dynamic linking support\n"
275shared=no
276}
277
Rich Felkera1546e82012-07-12 14:24:10 -0400278test "$ARCH" = "mips" && {
279printf "warning: disabling dynamic linking support on mips (not yet supported)\n"
280shared=no
281}
282
Rich Felker64d2f8e2012-05-04 23:24:51 -0400283
284printf "creating config.mak... "
285
286exec 3>&1 1>config.mak
287
288
289cat << EOF
290# This version of config.mak was generated by configure
291# Any changes made here will be lost if configure is re-run
292ARCH = $ARCH
293prefix = $prefix
294exec_prefix = $exec_prefix
295bindir = $bindir
296libdir = $libdir
297includedir = $includedir
298syslibdir = $syslibdir
299CC = $CC
300CFLAGS= $CFLAGS_AUTO $CFLAGS
301CFLAGS_C99FSE = $CFLAGS_C99FSE
302CPPFLAGS = $CPPFLAGS
Rich Felker08f70a32012-06-06 20:45:52 -0400303LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
Rich Felker64d2f8e2012-05-04 23:24:51 -0400304EOF
305test "x$static" = xno && echo "STATIC_LIBS ="
306test "x$shared" = xno && echo "SHARED_LIBS ="
307test "x$wrapper" = xno && echo "ALL_TOOLS ="
308test "x$wrapper" = xno && echo "TOOL_LIBS ="
309exec 1>&3 3>&-
310
311printf "done\n"