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