add IP_CHECKSUM socket option to netinet/in.h
[oweals/musl.git] / 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 CFLAGS_NOSSP=
115 LDFLAGS_AUTO=
116 OPTIMIZE_GLOBS=
117 prefix=/usr/local/musl
118 exec_prefix='$(prefix)'
119 bindir='$(exec_prefix)/bin'
120 libdir='$(prefix)/lib'
121 includedir='$(prefix)/include'
122 syslibdir='/lib'
123 target=
124 optimize=auto
125 debug=no
126 warnings=no
127 shared=auto
128 static=yes
129 wrapper=auto
130
131 for arg ; do
132 case "$arg" in
133 --help) usage ;;
134 --prefix=*) prefix=${arg#*=} ;;
135 --exec-prefix=*) exec_prefix=${arg#*=} ;;
136 --bindir=*) bindir=${arg#*=} ;;
137 --libdir=*) libdir=${arg#*=} ;;
138 --includedir=*) includedir=${arg#*=} ;;
139 --syslibdir=*) syslibdir=${arg#*=} ;;
140 --enable-shared|--enable-shared=yes) shared=yes ;;
141 --disable-shared|--enable-shared=no) shared=no ;;
142 --enable-static|--enable-static=yes) static=yes ;;
143 --disable-static|--enable-static=no) static=no ;;
144 --enable-optimize) optimize=yes ;;
145 --enable-optimize=*) optimize=${arg#*=} ;;
146 --disable-optimize) optimize=no ;;
147 --enable-debug|--enable-debug=yes) debug=yes ;;
148 --disable-debug|--enable-debug=no) debug=no ;;
149 --enable-warnings|--enable-warnings=yes) warnings=yes ;;
150 --disable-warnings|--enable-warnings=no) warnings=no ;;
151 --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
152 --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
153 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
154 --host=*|--target=*) target=${arg#*=} ;;
155 -* ) echo "$0: unknown option $arg" ;;
156 CC=*) CC=${arg#*=} ;;
157 CFLAGS=*) CFLAGS=${arg#*=} ;;
158 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
159 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
160 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
161 LIBCC=*) LIBCC=${arg#*=} ;;
162 *=*) ;;
163 *) target=$arg ;;
164 esac
165 done
166
167 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
168 stripdir $i
169 done
170
171 #
172 # Get a temp filename we can use
173 #
174 i=0
175 set -C
176 while : ; do i=$(($i+1))
177 tmpc="./conf$$-$PPID-$i.c"
178 2>|/dev/null > "$tmpc" && break
179 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
180 done
181 set +C
182 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
183
184 #
185 # Find a C compiler to use
186 #
187 printf "checking for C compiler... "
188 trycc ${CROSS_COMPILE}gcc
189 trycc ${CROSS_COMPILE}c99
190 trycc ${CROSS_COMPILE}cc
191 printf "%s\n" "$CC"
192 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
193
194 printf "checking whether C compiler works... "
195 echo "typedef int x;" > "$tmpc"
196 if output=$($CC $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" 2>&1) ; then
197 printf "yes\n"
198 else
199 printf "no; compiler output follows:\n%s\n" "$output"
200 exit 1
201 fi
202
203 #
204 # Need to know if the compiler is gcc to decide whether to build the
205 # musl-gcc wrapper, and for critical bug detection in some gcc versions.
206 #
207 printf "checking whether compiler is gcc... "
208 if fnmatch '*gcc\ version*' "$(LC_ALL=C $CC -v 2>&1)" ; then
209 cc_is_gcc=yes
210 else
211 cc_is_gcc=no
212 fi
213 echo "$cc_is_gcc"
214
215 #
216 # Only build musl-gcc wrapper if toolchain does not already target musl
217 #
218 if test "$wrapper" = auto ; then
219 printf "checking whether to build musl-gcc wrapper... "
220 if test "$cc_is_gcc" = yes ; then
221 wrapper=yes
222 while read line ; do
223 case "$line" in */ld-musl-*) wrapper=no ;; esac
224 done <<EOF
225 $($CC -dumpspecs)
226 EOF
227 else
228 wrapper=no
229 fi
230 echo "$wrapper"
231 fi
232
233
234
235 #
236 # Find the target architecture
237 #
238 printf "checking target system type... "
239 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
240 printf "%s\n" "$target"
241
242 #
243 # Convert to just ARCH
244 #
245 case "$target" in
246 # Catch these early to simplify matching for 32-bit archs
247 mips64*|powerpc64*) fail "$0: unsupported target \"$target\"" ;;
248 arm*) ARCH=arm ;;
249 aarch64*) ARCH=aarch64 ;;
250 i?86*) ARCH=i386 ;;
251 x86_64-x32*|x32*|x86_64*x32) ARCH=x32 ;;
252 x86_64*) ARCH=x86_64 ;;
253 mips*) ARCH=mips ;;
254 microblaze*) ARCH=microblaze ;;
255 or1k*) ARCH=or1k ;;
256 powerpc*) ARCH=powerpc ;;
257 sh[1-9bel-]*|sh|superh*) ARCH=sh ;;
258 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
259 *) fail "$0: unknown or unsupported target \"$target\"" ;;
260 esac
261
262 #
263 # Try to get a conforming C99 freestanding environment
264 #
265 tryflag CFLAGS_C99FSE -std=c99
266 tryflag CFLAGS_C99FSE -nostdinc
267 tryflag CFLAGS_C99FSE -ffreestanding \
268 || tryflag CFLAGS_C99FSE -fno-builtin
269 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
270 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
271 tryflag CFLAGS_C99FSE -frounding-math
272
273 #
274 # We may use the may_alias attribute if __GNUC__ is defined, so
275 # if the compiler defines __GNUC__ but does not provide it,
276 # it must be defined away as part of the CFLAGS.
277 #
278 printf "checking whether compiler needs attribute((may_alias)) suppression... "
279 cat > "$tmpc" <<EOF
280 typedef int
281 #ifdef __GNUC__
282 __attribute__((__may_alias__))
283 #endif
284 x;
285 EOF
286 if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
287   -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
288 printf "no\n"
289 else
290 printf "yes\n"
291 CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
292 fi
293
294 #
295 # Check for options to disable stack protector, which needs to be
296 # disabled for a few early-bootstrap translation units. If not found,
297 # this is not an error; we assume the toolchain does not do ssp.
298 #
299 tryflag CFLAGS_NOSSP -fno-stack-protector
300
301 #
302 # Check for options that may be needed to prevent the compiler from
303 # generating self-referential versions of memcpy,, memmove, memcmp,
304 # and memset. Really, we should add a check to determine if this
305 # option is sufficient, and if not, add a macro to cripple these
306 # functions with volatile...
307 #
308 tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
309
310 #
311 # Enable debugging if requessted.
312 #
313 test "$debug" = yes && CFLAGS_AUTO=-g
314
315 #
316 # Possibly add a -O option to CFLAGS and select modules to optimize with
317 # -O3 based on the status of --enable-optimize and provided CFLAGS.
318 #
319 printf "checking for optimization settings... "
320 case "x$optimize" in
321 xauto)
322 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
323 printf "using provided CFLAGS\n" ;optimize=no
324 else
325 printf "using defaults\n" ; optimize=yes
326 fi
327 ;;
328 xsize|xnone) printf "minimize size\n" ; optimize=size ;;
329 xno|x) printf "disabled\n" ; optimize=no ;;
330 *) printf "custom\n" ;;
331 esac
332
333 test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
334 test "$optimize" = yes && optimize="internal,malloc,string"
335
336 if fnmatch 'no|size' "$optimize" ; then :
337 else
338 printf "components to be optimized for speed:"
339 while test "$optimize" ; do
340 case "$optimize" in
341 *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
342 *) this=$optimize optimize=
343 esac
344 printf " $this"
345 case "$this" in
346 */*.c) ;;
347 */*) this=$this*.c ;;
348 *) this=$this/*.c ;;
349 esac
350 OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
351 done
352 OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
353 printf "\n"
354 fi
355
356 # Always try -pipe
357 tryflag CFLAGS_AUTO -pipe
358
359 #
360 # If debugging is disabled, omit frame pointer. Modern GCC does this
361 # anyway on most archs even when debugging is enabled since the frame
362 # pointer is no longer needed for debugging.
363 #
364 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
365 else 
366 tryflag CFLAGS_AUTO -fomit-frame-pointer
367 fi
368
369 #
370 # Modern GCC wants to put DWARF tables (used for debugging and
371 # unwinding) in the loaded part of the program where they are
372 # unstrippable. These options force them back to debug sections (and
373 # cause them not to get generated at all if debugging is off).
374 #
375 tryflag CFLAGS_AUTO -fno-unwind-tables
376 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
377
378 #
379 # The GNU toolchain defaults to assuming unmarked files need an
380 # executable stack, potentially exposing vulnerabilities in programs
381 # linked with such object files. Fix this.
382 #
383 tryflag CFLAGS_AUTO -Wa,--noexecstack
384
385 #
386 # On x86, make sure we don't have incompatible instruction set
387 # extensions enabled by default. This is bad for making static binaries.
388 # We cheat and use i486 rather than i386 because i386 really does not
389 # work anyway (issues with atomic ops).
390 #
391 if test "$ARCH" = "i386" ; then
392 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
393 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
394 fi
395
396 #
397 # Even with -std=c99, gcc accepts some constructs which are constraint
398 # violations. We want to treat these as errors regardless of whether
399 # other purely stylistic warnings are enabled -- especially implicit
400 # function declarations, which are a dangerous programming error.
401 #
402 tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
403 tryflag CFLAGS_AUTO -Werror=implicit-int
404 tryflag CFLAGS_AUTO -Werror=pointer-sign
405 tryflag CFLAGS_AUTO -Werror=pointer-arith
406
407 if test "x$warnings" = xyes ; then
408 tryflag CFLAGS_AUTO -Wall
409 tryflag CFLAGS_AUTO -Wno-parentheses
410 tryflag CFLAGS_AUTO -Wno-uninitialized
411 tryflag CFLAGS_AUTO -Wno-missing-braces
412 tryflag CFLAGS_AUTO -Wno-unused-value
413 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
414 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
415 tryflag CFLAGS_AUTO -Wno-pointer-to-int-cast
416 fi
417
418 # Some patched GCC builds have these defaults messed up...
419 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
420
421 test "$shared" = "no" || {
422 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
423 LDFLAGS_DUMMY=
424 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
425 test "$shared" = "yes" && fail "$0: error: linker cannot build shared library"
426 printf "warning: disabling dynamic linking support\n"
427 shared=no
428 }
429 }
430
431 # Find compiler runtime library
432 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
433 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
434 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
435                  && tryldflag LIBCC "$try_libcc"
436 printf "using compiler runtime libraries: %s\n" "$LIBCC"
437
438 # Figure out arch variants for archs with variants
439 SUBARCH=
440 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
441
442 if test "$ARCH" = "x86_64" ; then
443 trycppif __ILP32__ "$t" && ARCH=x32
444 fi
445
446 if test "$ARCH" = "arm" ; then
447 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
448 trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
449 fi
450
451 if test "$ARCH" = "aarch64" ; then
452 trycppif __AARCH64EB__ "$t" && SUBARCH=${SUBARCH}_be
453 fi
454
455 if test "$ARCH" = "mips" ; then
456 trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
457 trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
458 fi
459
460 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
461 && SUBARCH=${SUBARCH}el
462
463 if test "$ARCH" = "sh" ; then
464 trycppif __BIG_ENDIAN__ "$t" && SUBARCH=${SUBARCH}eb
465 if trycppif "__SH_FPU_ANY__ || __SH4__" "$t" ; then
466 # Some sh configurations are broken and replace double with float
467 # rather than using softfloat when the fpu is present but only
468 # supports single precision. Reject them.
469 printf "checking whether compiler's double type is IEEE double... "
470 echo 'typedef char dblcheck[(int)sizeof(double)-5];' > "$tmpc"
471 if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
472 printf "yes\n"
473 else
474 printf "no\n"
475 fail "$0: error: compiler's floating point configuration is unsupported"
476 fi
477 else
478 SUBARCH=${SUBARCH}-nofpu
479 fi
480 fi
481
482 test "$SUBARCH" \
483 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
484
485 case "$ARCH$SUBARCH" in
486 arm) ASMSUBARCH=el ;;
487 *) ASMSUBARCH=$SUBARCH ;;
488 esac
489
490 #
491 # Some archs (powerpc) have different possible long double formats
492 # that the compiler can be configured for. The logic for whether this
493 # is supported is in bits/float.h; in general, it is not. We need to
494 # check for mismatches here or code in printf, strotd, and scanf will
495 # be dangerously incorrect because it depends on (1) the macros being
496 # correct, and (2) IEEE semantics.
497 #
498 printf "checking whether compiler's long double definition matches float.h... "
499 echo '#include <float.h>' > "$tmpc"
500 echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
501 echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
502 echo '#endif' >> "$tmpc"
503 if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
504   -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
505 printf "yes\n"
506 else
507 printf "no\n"
508 fail "$0: error: unsupported long double type"
509 fi
510
511 printf "creating config.mak... "
512
513 cmdline=$(quote "$0")
514 for i ; do cmdline="$cmdline $(quote "$i")" ; done
515
516 exec 3>&1 1>config.mak
517
518
519 cat << EOF
520 # This version of config.mak was generated by:
521 # $cmdline
522 # Any changes made here will be lost if configure is re-run
523 ARCH = $ARCH
524 SUBARCH = $SUBARCH
525 ASMSUBARCH = $ASMSUBARCH
526 prefix = $prefix
527 exec_prefix = $exec_prefix
528 bindir = $bindir
529 libdir = $libdir
530 includedir = $includedir
531 syslibdir = $syslibdir
532 CC = $CC
533 CFLAGS = $CFLAGS_AUTO $CFLAGS
534 CFLAGS_C99FSE = $CFLAGS_C99FSE
535 CFLAGS_MEMOPS = $CFLAGS_MEMOPS
536 CFLAGS_NOSSP = $CFLAGS_NOSSP
537 CPPFLAGS = $CPPFLAGS
538 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
539 CROSS_COMPILE = $CROSS_COMPILE
540 LIBCC = $LIBCC
541 OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
542 EOF
543 test "x$static" = xno && echo "STATIC_LIBS ="
544 test "x$shared" = xno && echo "SHARED_LIBS ="
545 test "x$wrapper" = xno && echo "ALL_TOOLS ="
546 test "x$wrapper" = xno && echo "TOOL_LIBS ="
547 exec 1>&3 3>&-
548
549 printf "done\n"