remove -Wcast-align from --enable-warnings
[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-optimize=...   optimize listed components for speed over size [auto]
28   --enable-debug          build with debugging information [disabled]
29   --enable-warnings       build with recommended warnings flags [disabled]
30   --enable-gcc-wrapper    build musl-gcc toolchain wrapper [auto]
31   --disable-shared        inhibit building shared library [enabled]
32   --disable-static        inhibit building static library [enabled]
33
34 Some influential environment variables:
35   CC                      C compiler command [detected]
36   CFLAGS                  C compiler flags [-Os -pipe ...]
37   CROSS_COMPILE           prefix for cross compiler and tools [none]
38   LIBCC                   compiler runtime library [detected]
39
40 Use these variables to override the choices made by configure.
41
42 EOF
43 exit 0
44 }
45
46 # Helper functions
47
48 quote () {
49 tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
50 $1
51 EOF
52 printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
53 }
54 echo () { printf "%s\n" "$*" ; }
55 fail () { echo "$*" ; exit 1 ; }
56 fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
57 cmdexists () { type "$1" >/dev/null 2>&1 ; }
58 trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
59
60 stripdir () {
61 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
62 }
63
64 trycppif () {
65 printf "checking preprocessor condition %s... " "$1"
66 echo "typedef int x;" > "$tmpc"
67 echo "#if $1" >> "$tmpc"
68 echo "#error yes" >> "$tmpc"
69 echo "#endif" >> "$tmpc"
70 if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
71 printf "false\n"
72 return 1
73 else
74 printf "true\n"
75 return 0
76 fi
77 }
78
79 tryflag () {
80 printf "checking whether compiler accepts %s... " "$2"
81 echo "typedef int x;" > "$tmpc"
82 if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
83 printf "yes\n"
84 eval "$1=\"\${$1} \$2\""
85 eval "$1=\${$1# }"
86 return 0
87 else
88 printf "no\n"
89 return 1
90 fi
91 }
92
93 tryldflag () {
94 printf "checking whether linker accepts %s... " "$2"
95 echo "typedef int x;" > "$tmpc"
96 if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
97 printf "yes\n"
98 eval "$1=\"\${$1} \$2\""
99 eval "$1=\${$1# }"
100 return 0
101 else
102 printf "no\n"
103 return 1
104 fi
105 }
106
107
108
109 # Beginning of actual script
110
111 CFLAGS_C99FSE=
112 CFLAGS_AUTO=
113 CFLAGS_MEMOPS=
114 LDFLAGS_AUTO=
115 OPTIMIZE_GLOBS=
116 prefix=/usr/local/musl
117 exec_prefix='$(prefix)'
118 bindir='$(exec_prefix)/bin'
119 libdir='$(prefix)/lib'
120 includedir='$(prefix)/include'
121 syslibdir='/lib'
122 target=
123 optimize=auto
124 debug=no
125 warnings=no
126 shared=yes
127 static=yes
128
129 for arg ; do
130 case "$arg" in
131 --help) usage ;;
132 --prefix=*) prefix=${arg#*=} ;;
133 --exec-prefix=*) exec_prefix=${arg#*=} ;;
134 --bindir=*) bindir=${arg#*=} ;;
135 --libdir=*) libdir=${arg#*=} ;;
136 --includedir=*) includedir=${arg#*=} ;;
137 --syslibdir=*) syslibdir=${arg#*=} ;;
138 --enable-shared|--enable-shared=yes) shared=yes ;;
139 --disable-shared|--enable-shared=no) shared=no ;;
140 --enable-static|--enable-static=yes) static=yes ;;
141 --disable-static|--enable-static=no) static=no ;;
142 --enable-optimize) optimize=yes ;;
143 --enable-optimize=*) optimize=${arg#*=} ;;
144 --disable-optimize) optimize=no ;;
145 --enable-debug|--enable-debug=yes) debug=yes ;;
146 --disable-debug|--enable-debug=no) debug=no ;;
147 --enable-warnings|--enable-warnings=yes) warnings=yes ;;
148 --disable-warnings|--enable-warnings=no) warnings=no ;;
149 --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
150 --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
151 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
152 --host=*|--target=*) target=${arg#*=} ;;
153 -* ) echo "$0: unknown option $arg" ;;
154 CC=*) CC=${arg#*=} ;;
155 CFLAGS=*) CFLAGS=${arg#*=} ;;
156 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
157 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
158 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
159 LIBCC=*) LIBCC=${arg#*=} ;;
160 *=*) ;;
161 *) target=$arg ;;
162 esac
163 done
164
165 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
166 stripdir $i
167 done
168
169 #
170 # Get a temp filename we can use
171 #
172 i=0
173 set -C
174 while : ; do i=$(($i+1))
175 tmpc="./conf$$-$PPID-$i.c"
176 2>|/dev/null > "$tmpc" && break
177 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
178 done
179 set +C
180 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
181
182 #
183 # Find a C compiler to use
184 #
185 printf "checking for C compiler... "
186 trycc ${CROSS_COMPILE}gcc
187 trycc ${CROSS_COMPILE}c99
188 trycc ${CROSS_COMPILE}cc
189 printf "%s\n" "$CC"
190 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
191
192 #
193 # Only build musl-gcc wrapper if toolchain does not already target musl
194 #
195 if test -z "$wrapper" ; then
196 printf "checking whether compiler is gcc... "
197 if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
198 echo yes
199 printf "checking whether to build musl-gcc wrapper... "
200 wrapper=yes
201 while read line ; do
202 case "$line" in */ld-musl-*) wrapper=no ;; esac
203 done <<EOF
204 $($CC -dumpspecs)
205 EOF
206 echo $wrapper
207 else
208 echo no
209 fi
210 fi
211
212
213
214 #
215 # Find the target architecture
216 #
217 printf "checking target system type... "
218 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
219 printf "%s\n" "$target"
220
221 #
222 # Convert to just ARCH
223 #
224 case "$target" in
225 arm*) ARCH=arm ;;
226 i?86*) ARCH=i386 ;;
227 x86_64*) ARCH=x86_64 ;;
228 mips-*|mipsel-*) ARCH=mips ;;
229 microblaze-*) ARCH=microblaze ;;
230 powerpc-*) ARCH=powerpc ;;
231 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
232 *) fail "$0: unknown or unsupported target \"$target\"" ;;
233 esac
234
235 #
236 # Try to get a conforming C99 freestanding environment
237 #
238 tryflag CFLAGS_C99FSE -std=c99
239 tryflag CFLAGS_C99FSE -nostdinc
240 tryflag CFLAGS_C99FSE -ffreestanding \
241 || tryflag CFLAGS_C99FSE -fno-builtin
242 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
243 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
244 tryflag CFLAGS_C99FSE -frounding-math
245
246 #
247 # We may use the may_alias attribute if __GNUC__ is defined, so
248 # if the compiler defines __GNUC__ but does not provide it,
249 # it must be defined away as part of the CFLAGS.
250 #
251 printf "checking whether compiler needs attribute((may_alias)) suppression... "
252 cat > "$tmpc" <<EOF
253 typedef int
254 #ifdef __GNUC__
255 __attribute__((__may_alias__))
256 #endif
257 x;
258 EOF
259 if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
260   -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
261 printf "no\n"
262 else
263 printf "yes\n"
264 CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
265 fi
266
267 #
268 # Check for options that may be needed to prevent the compiler from
269 # generating self-referential versions of memcpy,, memmove, memcmp,
270 # and memset. Really, we should add a check to determine if this
271 # option is sufficient, and if not, add a macro to cripple these
272 # functions with volatile...
273 #
274 tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
275
276 #
277 # If debugging is explicitly enabled, don't auto-enable optimizations
278 #
279 if test "$debug" = yes ; then
280 CFLAGS_AUTO=-g
281 test "$optimize" = auto && optimize=no
282 fi
283
284 #
285 # Possibly add a -O option to CFLAGS and select modules to optimize with
286 # -O3 based on the status of --enable-optimize and provided CFLAGS.
287 #
288 printf "checking for optimization settings... "
289 case "x$optimize" in
290 xauto)
291 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
292 printf "using provided CFLAGS\n" ;optimize=no
293 else
294 printf "using defaults\n" ; optimize=yes
295 fi
296 ;;
297 xsize|xnone) printf "minimize size\n" ; optimize=size ;;
298 xno|x) printf "disabled\n" ; optimize=no ;;
299 *) printf "custom\n" ;;
300 esac
301
302 test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
303 test "$optimize" = yes && optimize="internal,malloc,string"
304
305 if fnmatch 'no|size' "$optimize" ; then :
306 else
307 printf "components to be optimized for speed:"
308 while test "$optimize" ; do
309 case "$optimize" in
310 *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
311 *) this=$optimize optimize=
312 esac
313 printf " $this"
314 case "$this" in
315 */*.c) ;;
316 */*) this=$this*.c ;;
317 *) this=$this/*.c ;;
318 esac
319 OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
320 done
321 OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
322 printf "\n"
323 fi
324
325 # Always try -pipe
326 tryflag CFLAGS_AUTO -pipe
327
328 #
329 # If debugging is disabled, omit frame pointer. Modern GCC does this
330 # anyway on most archs even when debugging is enabled since the frame
331 # pointer is no longer needed for debugging.
332 #
333 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
334 else 
335 tryflag CFLAGS_AUTO -fomit-frame-pointer
336 fi
337
338 #
339 # Modern GCC wants to put DWARF tables (used for debugging and
340 # unwinding) in the loaded part of the program where they are
341 # unstrippable. These options force them back to debug sections (and
342 # cause them not to get generated at all if debugging is off).
343 #
344 tryflag CFLAGS_AUTO -fno-unwind-tables
345 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
346
347 #
348 # The GNU toolchain defaults to assuming unmarked files need an
349 # executable stack, potentially exposing vulnerabilities in programs
350 # linked with such object files. Fix this.
351 #
352 tryflag CFLAGS_AUTO -Wa,--noexecstack
353
354 #
355 # On x86, make sure we don't have incompatible instruction set
356 # extensions enabled by default. This is bad for making static binaries.
357 # We cheat and use i486 rather than i386 because i386 really does not
358 # work anyway (issues with atomic ops).
359 #
360 if test "$ARCH" = "i386" ; then
361 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
362 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
363 fi
364
365 #
366 # Even with -std=c99, gcc accepts some constructs which are constraint
367 # violations. We want to treat these as errors regardless of whether
368 # other purely stylistic warnings are enabled -- especially implicit
369 # function declarations, which are a dangerous programming error.
370 #
371 tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
372 tryflag CFLAGS_AUTO -Werror=implicit-int
373 tryflag CFLAGS_AUTO -Werror=pointer-sign
374 tryflag CFLAGS_AUTO -Werror=pointer-arith
375
376 if test "x$warnings" = xyes ; then
377 tryflag CFLAGS_AUTO -Wall
378 tryflag CFLAGS_AUTO -Wno-parentheses
379 tryflag CFLAGS_AUTO -Wno-uninitialized
380 tryflag CFLAGS_AUTO -Wno-missing-braces
381 tryflag CFLAGS_AUTO -Wno-unused-value
382 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
383 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
384 fi
385
386 # Some patched GCC builds have these defaults messed up...
387 tryflag CFLAGS_AUTO -fno-stack-protector
388 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
389
390 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
391 LDFLAGS_DUMMY=
392 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
393 printf "warning: disabling dynamic linking support\n"
394 shared=no
395 }
396
397 # Find compiler runtime library
398 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
399 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
400 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
401                  && tryldflag LIBCC "$try_libcc"
402 printf "using compiler runtime libraries: %s\n" "$LIBCC"
403
404 # Figure out arch variants for archs with variants
405 SUBARCH=
406 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
407
408 if test "$ARCH" = "arm" ; then
409 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
410 trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
411 fi
412
413 test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
414 && SUBARCH=${SUBARCH}el
415
416 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
417 && SUBARCH=${SUBARCH}el
418
419 test "$SUBARCH" \
420 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
421
422 case "$ARCH$SUBARCH" in
423 arm) ASMSUBARCH=el ;;
424 *) ASMSUBARCH=$SUBARCH ;;
425 esac
426
427 #
428 # Some archs (powerpc) have different possible long double formats
429 # that the compiler can be configured for. The logic for whether this
430 # is supported is in bits/float.h; in general, it is not. We need to
431 # check for mismatches here or code in printf, strotd, and scanf will
432 # be dangerously incorrect because it depends on (1) the macros being
433 # correct, and (2) IEEE semantics.
434 #
435 printf "checking whether compiler's long double definition matches float.h... "
436 echo '#include <float.h>' > "$tmpc"
437 echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
438 echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
439 echo '#endif' >> "$tmpc"
440 if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
441   -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
442 printf "yes\n"
443 else
444 printf "no\n"
445 fail "$0: error: unsupported long double type"
446 fi
447
448 printf "creating config.mak... "
449
450 cmdline=$(quote "$0")
451 for i ; do cmdline="$cmdline $(quote "$i")" ; done
452
453 exec 3>&1 1>config.mak
454
455
456 cat << EOF
457 # This version of config.mak was generated by:
458 # $cmdline
459 # Any changes made here will be lost if configure is re-run
460 ARCH = $ARCH
461 SUBARCH = $SUBARCH
462 ASMSUBARCH = $ASMSUBARCH
463 prefix = $prefix
464 exec_prefix = $exec_prefix
465 bindir = $bindir
466 libdir = $libdir
467 includedir = $includedir
468 syslibdir = $syslibdir
469 CC = $CC
470 CFLAGS= $CFLAGS_AUTO $CFLAGS
471 CFLAGS_C99FSE = $CFLAGS_C99FSE
472 CFLAGS_MEMOPS = $CFLAGS_MEMOPS
473 CPPFLAGS = $CPPFLAGS
474 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
475 CROSS_COMPILE = $CROSS_COMPILE
476 LIBCC = $LIBCC
477 OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
478 EOF
479 test "x$static" = xno && echo "STATIC_LIBS ="
480 test "x$shared" = xno && echo "SHARED_LIBS ="
481 test "x$wrapper" = xno && echo "ALL_TOOLS ="
482 test "x$wrapper" = xno && echo "TOOL_LIBS ="
483 exec 1>&3 3>&-
484
485 printf "done\n"