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