add support for ctors/dtors on arm with modern gcc
[musl] / configure
1 #!/bin/sh
2
3 usage () {
4 cat <<EOF
5 Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
6
7 To assign environment variables (e.g., CC, CFLAGS...), specify them as
8 VAR=VALUE.  See below for descriptions of some of the useful variables.
9
10 Defaults for the options are specified in brackets.
11
12 Installation directories:
13   --prefix=PREFIX         main installation prefix [/usr/local/musl]
14   --exec-prefix=EPREFIX   installation prefix for executable files [PREFIX]
15
16 Fine 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
22 System types:
23   --target=TARGET         configure to run on target TARGET [detected]
24   --host=HOST             same as --target
25
26 Optional 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
33 Some influential environment variables:
34   CC                      C compiler command [detected]
35   CFLAGS                  C compiler flags [-Os -pipe ...]
36   CROSS_COMPILE           prefix for cross compiler and tools [none]
37   LIBCC                   compiler runtime library [detected]
38
39 Use these variables to override the choices made by configure.
40
41 EOF
42 exit 0
43 }
44
45 # Helper functions
46
47 echo () { printf "%s\n" "$*" ; }
48 fail () { echo "$*" ; exit 1 ; }
49 fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
50 cmdexists () { type "$1" >/dev/null 2>&1 ; }
51 trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
52
53 stripdir () {
54 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
55 }
56
57 tryflag () {
58 printf "checking whether compiler accepts %s... " "$2"
59 echo "typedef int x;" > "$tmpc"
60 if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
61 printf "yes\n"
62 eval "$1=\"\${$1} \$2\""
63 eval "$1=\${$1# }"
64 return 0
65 else
66 printf "no\n"
67 return 1
68 fi
69 }
70
71 tryldflag () {
72 printf "checking whether linker accepts %s... " "$2"
73 echo "typedef int x;" > "$tmpc"
74 if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
75 printf "yes\n"
76 eval "$1=\"\${$1} \$2\""
77 eval "$1=\${$1# }"
78 return 0
79 else
80 printf "no\n"
81 return 1
82 fi
83 }
84
85
86
87 # Beginning of actual script
88
89 CFLAGS_C99FSE=
90 CFLAGS_AUTO=
91 LDFLAGS_AUTO=
92 prefix=/usr/local/musl
93 exec_prefix='$(prefix)'
94 bindir='$(exec_prefix)/bin'
95 libdir='$(prefix)/lib'
96 includedir='$(prefix)/include'
97 syslibdir='/lib'
98 target=
99 debug=no
100 warnings=no
101 shared=yes
102 static=yes
103
104 for arg ; do
105 case "$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 ;;
123 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
124 --host=*|--target=*) target=${arg#*=} ;;
125 -* ) echo "$0: unknown option $arg" ;;
126 CC=*) CC=${arg#*=} ;;
127 CFLAGS=*) CFLAGS=${arg#*=} ;;
128 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
129 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
130 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
131 LIBCC=*) LIBCC=${arg#*=} ;;
132 *=*) ;;
133 *) target=$arg ;;
134 esac
135 done
136
137 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
138 stripdir $i
139 done
140
141 #
142 # Get a temp filename we can use
143 #
144 i=0
145 set -C
146 while : ; do i=$(($i+1))
147 tmpc="./conf$$-$PPID-$i.c"
148 2>|/dev/null > "$tmpc" && break
149 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
150 done
151 set +C
152 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
153
154 #
155 # Find a C compiler to use
156 #
157 printf "checking for C compiler... "
158 trycc ${CROSS_COMPILE}gcc
159 trycc ${CROSS_COMPILE}c99
160 trycc ${CROSS_COMPILE}cc
161 printf "%s\n" "$CC"
162 test -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 #
167 if test -z "$wrapper" ; then
168 printf "checking whether compiler is gcc... "
169 if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
170 echo yes
171 printf "checking whether to build musl-gcc wrapper... "
172 wrapper=yes
173 while read line ; do
174 case "$line" in */ld-musl-*) wrapper=no ;; esac
175 done <<EOF
176 $($CC -dumpspecs)
177 EOF
178 echo $wrapper
179 else
180 echo no
181 fi
182 fi
183
184
185
186 #
187 # Find the target architecture
188 #
189 printf "checking target system type... "
190 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
191 printf "%s\n" "$target"
192
193 #
194 # Convert to just ARCH
195 #
196 case "$target" in
197 arm*) ARCH=arm ;;
198 i?86*) ARCH=i386 ;;
199 x86_64*) ARCH=x86_64 ;;
200 mips-*|mipsel-*) ARCH=mips ;;
201 microblaze-*) ARCH=microblaze ;;
202 powerpc-*) ARCH=powerpc ;;
203 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
204 *) fail "$0: unknown or unsupported target \"$target\"" ;;
205 esac
206
207 #
208 # Try to get a conforming C99 freestanding environment
209 #
210 tryflag CFLAGS_C99FSE -std=c99
211 tryflag CFLAGS_C99FSE -nostdinc
212 tryflag CFLAGS_C99FSE -ffreestanding \
213 || tryflag CFLAGS_C99FSE -fno-builtin
214 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
215 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
216 tryflag CFLAGS_C99FSE -frounding-math
217
218 #
219 # Setup basic default CFLAGS: debug, optimization, and -pipe
220 #
221 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
222 else
223 tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
224 fi
225 test "x$debug" = xyes && CFLAGS_AUTO="-g"
226 tryflag CFLAGS_AUTO -pipe
227
228 #
229 # 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.
232 #
233 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
234 else 
235 tryflag CFLAGS_AUTO -fomit-frame-pointer
236 fi
237
238 #
239 # 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 #
244 tryflag CFLAGS_AUTO -fno-unwind-tables
245 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
246
247 #
248 # 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 #
252 tryflag CFLAGS_AUTO -Wa,--noexecstack
253
254 #
255 # Some optimization levels add bloated alignment that hurt performance
256 #
257 tryflag CFLAGS_AUTO -falign-functions=1
258 tryflag CFLAGS_AUTO -falign-labels=1
259 tryflag CFLAGS_AUTO -falign-loops=1
260 tryflag 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 #
268 if test "$ARCH" = "i386" ; then
269 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
270 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
271 fi
272
273 if test "x$warnings" = xyes ; then
274 tryflag CFLAGS_AUTO -Wall
275 tryflag CFLAGS_AUTO -Wpointer-arith
276 tryflag CFLAGS_AUTO -Wcast-align
277 tryflag CFLAGS_AUTO -Wno-parentheses
278 tryflag CFLAGS_AUTO -Wno-uninitialized
279 tryflag CFLAGS_AUTO -Wno-missing-braces
280 tryflag CFLAGS_AUTO -Wno-unused-value
281 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
282 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
283 fi
284
285 # Some patched GCC builds have these defaults messed up...
286 tryflag CFLAGS_AUTO -fno-stack-protector
287 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
288
289 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
290 LDFLAGS_DUMMY=
291 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
292 printf "warning: disabling dynamic linking support\n"
293 shared=no
294 }
295
296 # Find compiler runtime library
297 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
298 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
299 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
300                  && tryldflag LIBCC "$try_libcc"
301 printf "using compiler runtime libraries: %s\n" "$LIBCC"
302
303
304 printf "creating config.mak... "
305
306 exec 3>&1 1>config.mak
307
308
309 cat << EOF
310 # This version of config.mak was generated by configure
311 # Any changes made here will be lost if configure is re-run
312 ARCH = $ARCH
313 prefix = $prefix
314 exec_prefix = $exec_prefix
315 bindir = $bindir
316 libdir = $libdir
317 includedir = $includedir
318 syslibdir = $syslibdir
319 CC = $CC
320 CFLAGS= $CFLAGS_AUTO $CFLAGS
321 CFLAGS_C99FSE = $CFLAGS_C99FSE
322 CPPFLAGS = $CPPFLAGS
323 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
324 CROSS_COMPILE = $CROSS_COMPILE
325 LIBCC = $LIBCC
326 EOF
327 test "x$static" = xno && echo "STATIC_LIBS ="
328 test "x$shared" = xno && echo "SHARED_LIBS ="
329 test "x$wrapper" = xno && echo "ALL_TOOLS ="
330 test "x$wrapper" = xno && echo "TOOL_LIBS ="
331 exec 1>&3 3>&-
332
333 printf "done\n"