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