blob: df1d301114edfb8dfa2aa998e8c39cafdc994a69 [file] [log] [blame]
Rich Felker64d2f8e2012-05-04 23:24:51 -04001#!/bin/sh
2
3usage () {
4cat <<EOF
5Usage: $0 [OPTION]... [VAR=VALUE]... [BUILDTYPE]
6
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]
24 --build=BUILD same as --target
25
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
70
71
72# Beginning of actual script
73
74prefix=
75exec_prefix=
76bindir=
77libdir=
78includedir=
79syslibdir=
80build=
81debug=no
82warnings=
83shared=yes
84static=yes
85
86for arg ; do
87case "$arg" in
88--help) usage ;;
89--prefix=*) prefix=${arg#*=} ;;
90--exec-prefix=*) exec_prefix=${arg#*=} ;;
91--bindir=*) bindir=${arg#*=} ;;
92--libdir=*) libdir=${arg#*=} ;;
93--includedir=*) includedir=${arg#*=} ;;
94--syslibdir=*) syslibdir=${arg#*=} ;;
95--enable-shared|--enable-shared=yes) shared=yes ;;
96--disable-shared|--enable-shared=no) shared=no ;;
97--enable-static|--enable-static=yes) static=yes ;;
98--disable-static|--enable-static=no) static=no ;;
99--enable-debug|--enable-debug=yes) debug=yes ;;
100--disable-debug|--enable-debug=no) debug=no ;;
101--enable-warnings|--enable-warnings=yes) warnings=yes ;;
102--disable-warnings|--enable-warnings=no) warnings=no ;;
103--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
104--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
105--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--host=*) ;;
106--build=*|--target=*) build=${arg#*=} ;;
107-* ) echo "$0: unknown option $arg" ;;
108CC=*) CC=${arg#*=} ;;
109CFLAGS=*) CFLAGS=${arg#*=} ;;
110CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
111LDFLAGS=*) LDFLAGS=${arg#*=} ;;
112*) build=$arg ;;
113esac
114done
115
116setdir prefix /usr/local/musl
117setdir exec_prefix '$(prefix)'
118setdir bindir '$(exec_prefix)/bin'
119setdir libdir '$(prefix)/lib'
120setdir includedir '$(prefix)/include'
121setdir syslibdir '/lib'
122
123#
124# Get a temp filename we can use
125#
126i=0
127set -C
128while : ; do i=$(($i+1))
129tmpc="./conf$$-$PPID-$i.c"
1302>/dev/null > "$tmpc" && break
131test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
132done
133set +C
134trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
135
136#
137# Find a C compiler to use
138#
139printf "checking for C compiler... "
140trycc gcc
141trycc c99
142trycc cc
143printf "%s\n" "$CC"
144test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
145
146#
147# Only build musl-gcc wrapper if toolchain does not already target musl
148#
149if test -z "$wrapper" ; then
150printf "checking whether compiler is gcc... "
151if fnmatch 'gcc*|GCC*' "$("$CC" --version 2>/dev/null)" ; then
152echo yes
153printf "checking whether to build musl-gcc wrapper... "
154wrapper=yes
155while read line ; do
156case "$line" in */ld-musl-*) wrapper=no ;; esac
157done <<EOF
158$($CC -dumpspecs)
159EOF
160echo $wrapper
161else
162echo no
163fi
164fi
165
166
167
168#
169# Find the build architecture
170#
171printf "checking build system type... "
172test -n "$build" || build=$("$CC" -dumpmachine 2>/dev/null) || build=unknown
173printf "%s\n" "$build"
174
175#
176# Convert to just ARCH
177#
178case "$build" in
179arm*) ARCH=arm ;;
180i?86*) ARCH=i386 ;;
181x86_64*) ARCH=x86_64 ;;
182unknown) fail "$0: unable to detect built target; try $0 --build=..." ;;
183*) fail "$0: unknown or unsupported build target \"$build\"" ;;
184esac
185
186#
187# Try to get a conforming C99 freestanding environment
188#
189tryflag CFLAGS_C99FSE -std=c99
190tryflag CFLAGS_C99FSE -nostdinc
191tryflag CFLAGS_C99FSE -ffreestanding \
192|| tryflag CFLAGS_C99FSE -fno-builtin
193tryflag CFLAGS_C99FSE -fexcess-precision=standard \
194|| tryflag CFLAGS_C99FSE -ffloat-store
195
196#
197# Setup basic default CFLAGS: debug, optimization, and -pipe
198#
199if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
200else
201tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
202fi
203test "x$debug" = xyes && CFLAGS_AUTO="-g"
204tryflag CFLAGS_AUTO -pipe
205
206#
207# If debugging is disabled, omit bloated DWARF2 unwind tables & frame ptr
208#
209if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
210else
211tryflag CFLAGS_AUTO -fno-unwind-tables
212tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
213tryflag CFLAGS_AUTO -fomit-frame-pointer
214fi
215
216#
217# Some optimization levels add bloated alignment that hurt performance
218#
219tryflag CFLAGS_AUTO -falign-functions=1
220tryflag CFLAGS_AUTO -falign-labels=1
221tryflag CFLAGS_AUTO -falign-loops=1
222tryflag CFLAGS_AUTO -falign-jumps=1
223
224#
225# On x86, make sure we don't have incompatible instruction set
226# extensions enabled by default. This is bad for making static binaries.
227# We cheat and use i486 rather than i386 because i386 really does not
228# work anyway (issues with atomic ops).
229#
230if test "$ARCH" = "i386" ; then
231fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryflag CFLAGS_AUTO -march=i486
232fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryflag CFLAGS_AUTO -mtune=generic
233fi
234
235if test "x$warnings" = xyes ; then
236tryflag CFLAGS_AUTO -Wall
237tryflag CFLAGS_AUTO -Wpointer-arith
238tryflag CFLAGS_AUTO -Wcast-align
239tryflag CFLAGS_AUTO -Wno-parentheses
240tryflag CFLAGS_AUTO -Wno-uninitialized
241tryflag CFLAGS_AUTO -Wno-missing-braces
242tryflag CFLAGS_AUTO -Wno-unused-value
243tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
244tryflag CFLAGS_AUTO -Wno-unknown-pragmas
245fi
246
247
248printf "creating config.mak... "
249
250exec 3>&1 1>config.mak
251
252
253cat << EOF
254# This version of config.mak was generated by configure
255# Any changes made here will be lost if configure is re-run
256ARCH = $ARCH
257prefix = $prefix
258exec_prefix = $exec_prefix
259bindir = $bindir
260libdir = $libdir
261includedir = $includedir
262syslibdir = $syslibdir
263CC = $CC
264CFLAGS= $CFLAGS_AUTO $CFLAGS
265CFLAGS_C99FSE = $CFLAGS_C99FSE
266CPPFLAGS = $CPPFLAGS
267LDFLAGS = $LDFLAGS
268EOF
269test "x$static" = xno && echo "STATIC_LIBS ="
270test "x$shared" = xno && echo "SHARED_LIBS ="
271test "x$wrapper" = xno && echo "ALL_TOOLS ="
272test "x$wrapper" = xno && echo "TOOL_LIBS ="
273exec 1>&3 3>&-
274
275printf "done\n"