blob: dece1d09e465051f0f09fa76a8475455e6820b7c [file] [log] [blame]
Rich Felker64d2f8e2012-05-04 23:24:51 -04001#!/bin/sh
2
3usage () {
4cat <<EOF
Rich Felker278883d2012-06-03 16:22:13 -04005Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
Rich Felker64d2f8e2012-05-04 23:24:51 -04006
7To assign environment variables (e.g., CC, CFLAGS...), specify them as
8VAR=VALUE. See below for descriptions of some of the useful variables.
9
10Defaults for the options are specified in brackets.
11
12Installation directories:
13 --prefix=PREFIX main installation prefix [/usr/local/musl]
14 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
15
16Fine 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
22System types:
23 --target=TARGET configure to run on target TARGET [detected]
Rich Felker278883d2012-06-03 16:22:13 -040024 --host=HOST same as --target
Rich Felker64d2f8e2012-05-04 23:24:51 -040025
26Optional features:
Rich Felkera80847d2013-07-22 21:22:04 -040027 --enable-optimize=... optimize listed components for speed over size [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040028 --enable-debug build with debugging information [disabled]
29 --enable-warnings build with recommended warnings flags [disabled]
Rich Felkerde2b67f2015-04-19 22:05:29 -040030 --enable-visibility use global visibility options to optimize PIC [auto]
Shizb3cd7d12015-06-28 23:08:19 +020031 --enable-wrapper=... build given musl toolchain wrapper [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040032 --disable-shared inhibit building shared library [enabled]
33 --disable-static inhibit building static library [enabled]
34
35Some influential environment variables:
36 CC C compiler command [detected]
37 CFLAGS C compiler flags [-Os -pipe ...]
Rich Felker94e920d2012-08-14 22:50:16 -040038 CROSS_COMPILE prefix for cross compiler and tools [none]
Rich Felker2c1cd232012-09-10 15:30:52 -040039 LIBCC compiler runtime library [detected]
Rich Felker64d2f8e2012-05-04 23:24:51 -040040
41Use these variables to override the choices made by configure.
42
43EOF
44exit 0
45}
46
47# Helper functions
48
Rich Felker453f4622013-08-16 18:19:47 -040049quote () {
50tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
51$1
52EOF
Rich Felkere4499742013-08-20 13:51:46 -040053printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
Rich Felker453f4622013-08-16 18:19:47 -040054}
Rich Felker64d2f8e2012-05-04 23:24:51 -040055echo () { printf "%s\n" "$*" ; }
56fail () { echo "$*" ; exit 1 ; }
57fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
58cmdexists () { type "$1" >/dev/null 2>&1 ; }
59trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
60
Rich Felker3d9e3a32012-11-08 17:20:50 -050061stripdir () {
62while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
Rich Felker64d2f8e2012-05-04 23:24:51 -040063}
64
Rich Felker3e7f1862013-07-18 20:30:58 -040065trycppif () {
66printf "checking preprocessor condition %s... " "$1"
Rich Felkerdf065782013-07-18 20:37:19 -040067echo "typedef int x;" > "$tmpc"
68echo "#if $1" >> "$tmpc"
Rich Felker3e7f1862013-07-18 20:30:58 -040069echo "#error yes" >> "$tmpc"
70echo "#endif" >> "$tmpc"
71if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
72printf "false\n"
73return 1
74else
75printf "true\n"
76return 0
77fi
78}
79
Rich Felker64d2f8e2012-05-04 23:24:51 -040080tryflag () {
81printf "checking whether compiler accepts %s... " "$2"
82echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +020083if $CC $CFLAGS_TRY $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040084printf "yes\n"
85eval "$1=\"\${$1} \$2\""
86eval "$1=\${$1# }"
87return 0
88else
89printf "no\n"
90return 1
91fi
92}
93
Rich Felker08f70a32012-06-06 20:45:52 -040094tryldflag () {
95printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040096echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +020097if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -040098printf "yes\n"
99eval "$1=\"\${$1} \$2\""
100eval "$1=\${$1# }"
101return 0
102else
103printf "no\n"
104return 1
105fi
106}
107
Rich Felker64d2f8e2012-05-04 23:24:51 -0400108
109
110# Beginning of actual script
111
Rich Felker08f70a32012-06-06 20:45:52 -0400112CFLAGS_C99FSE=
113CFLAGS_AUTO=
Rich Felker4a1f55e2013-08-01 17:12:23 -0400114CFLAGS_MEMOPS=
Rich Felker1ef849c2015-04-13 20:13:10 -0400115CFLAGS_NOSSP=
Shizfc431d32015-05-28 05:52:22 +0200116CFLAGS_TRY=
Rich Felker08f70a32012-06-06 20:45:52 -0400117LDFLAGS_AUTO=
Shizfc431d32015-05-28 05:52:22 +0200118LDFLAGS_TRY=
Rich Felkera80847d2013-07-22 21:22:04 -0400119OPTIMIZE_GLOBS=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500120prefix=/usr/local/musl
121exec_prefix='$(prefix)'
122bindir='$(exec_prefix)/bin'
123libdir='$(prefix)/lib'
124includedir='$(prefix)/include'
125syslibdir='/lib'
Shizb3cd7d12015-06-28 23:08:19 +0200126tools=
127tool_libs=
Rich Felker278883d2012-06-03 16:22:13 -0400128target=
Rich Felkera80847d2013-07-22 21:22:04 -0400129optimize=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400130debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500131warnings=no
Rich Felkerde2b67f2015-04-19 22:05:29 -0400132visibility=auto
Rich Felkerd79b2772014-06-10 12:11:12 -0400133shared=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400134static=yes
Rich Felker9ca4dae2014-05-19 10:33:28 -0400135wrapper=auto
Shizb3cd7d12015-06-28 23:08:19 +0200136gcc_wrapper=no
Shizfb585452015-06-28 23:08:21 +0200137clang_wrapper=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400138
139for arg ; do
140case "$arg" in
141--help) usage ;;
142--prefix=*) prefix=${arg#*=} ;;
143--exec-prefix=*) exec_prefix=${arg#*=} ;;
144--bindir=*) bindir=${arg#*=} ;;
145--libdir=*) libdir=${arg#*=} ;;
146--includedir=*) includedir=${arg#*=} ;;
147--syslibdir=*) syslibdir=${arg#*=} ;;
148--enable-shared|--enable-shared=yes) shared=yes ;;
149--disable-shared|--enable-shared=no) shared=no ;;
150--enable-static|--enable-static=yes) static=yes ;;
151--disable-static|--enable-static=no) static=no ;;
Rich Felkera80847d2013-07-22 21:22:04 -0400152--enable-optimize) optimize=yes ;;
153--enable-optimize=*) optimize=${arg#*=} ;;
154--disable-optimize) optimize=no ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400155--enable-debug|--enable-debug=yes) debug=yes ;;
156--disable-debug|--enable-debug=no) debug=no ;;
157--enable-warnings|--enable-warnings=yes) warnings=yes ;;
158--disable-warnings|--enable-warnings=no) warnings=no ;;
Rich Felkerde2b67f2015-04-19 22:05:29 -0400159--enable-visibility|--enable-visibility=yes) visibility=yes ;;
160--disable-visibility|--enable-visibility=no) visibility=no ;;
Shizb3cd7d12015-06-28 23:08:19 +0200161--enable-wrapper|--enable-wrapper=yes) wrapper=detect ;;
Shizfb585452015-06-28 23:08:21 +0200162--enable-wrapper=all) wrapper=yes ; gcc_wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200163--enable-wrapper=gcc) wrapper=yes ; gcc_wrapper=yes ;;
Shizfb585452015-06-28 23:08:21 +0200164--enable-wrapper=clang) wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200165--disable-wrapper|--enable-wrapper=no) wrapper=no ;;
166--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ; gcc_wrapper=yes ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400167--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400168--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
169--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400170-* ) echo "$0: unknown option $arg" ;;
171CC=*) CC=${arg#*=} ;;
172CFLAGS=*) CFLAGS=${arg#*=} ;;
173CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
174LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400175CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400176LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400177*=*) ;;
178*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400179esac
180done
181
Rich Felker3d9e3a32012-11-08 17:20:50 -0500182for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
183stripdir $i
184done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400185
186#
187# Get a temp filename we can use
188#
189i=0
190set -C
191while : ; do i=$(($i+1))
192tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05001932>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400194test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
195done
196set +C
197trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
198
199#
200# Find a C compiler to use
201#
202printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400203trycc ${CROSS_COMPILE}gcc
204trycc ${CROSS_COMPILE}c99
205trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400206printf "%s\n" "$CC"
207test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
208
Rich Felker89456672014-05-12 14:22:57 -0400209printf "checking whether C compiler works... "
210echo "typedef int x;" > "$tmpc"
211if output=$($CC $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" 2>&1) ; then
212printf "yes\n"
213else
214printf "no; compiler output follows:\n%s\n" "$output"
215exit 1
216fi
217
Rich Felker64d2f8e2012-05-04 23:24:51 -0400218#
Shizfc431d32015-05-28 05:52:22 +0200219# Figure out options to force errors on unknown flags.
220#
221tryflag CFLAGS_TRY -Werror=unknown-warning-option
222tryflag CFLAGS_TRY -Werror=unused-command-line-argument
223tryldflag LDFLAGS_TRY -Werror=unknown-warning-option
224tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
225
226#
Shizb3cd7d12015-06-28 23:08:19 +0200227# Need to know if the compiler is gcc or clang to decide which toolchain
228# wrappers to build.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400229#
Shizb3cd7d12015-06-28 23:08:19 +0200230printf "checking for C compiler family... "
231cc_ver="$(LC_ALL=C $CC -v 2>&1)"
232cc_family=unknown
233if fnmatch '*gcc\ version*' "$cc_ver" ; then
234cc_family=gcc
Shizfb585452015-06-28 23:08:21 +0200235elif fnmatch '*clang\ version*' "$cc_ver" ; then
236cc_family=clang
Rich Felker9ca4dae2014-05-19 10:33:28 -0400237fi
Shizb3cd7d12015-06-28 23:08:19 +0200238echo "$cc_family"
Rich Felker9ca4dae2014-05-19 10:33:28 -0400239
240#
Shizb3cd7d12015-06-28 23:08:19 +0200241# Figure out toolchain wrapper to build
Rich Felker9ca4dae2014-05-19 10:33:28 -0400242#
Shizb3cd7d12015-06-28 23:08:19 +0200243if test "$wrapper" = auto -o "$wrapper" = detect ; then
Shizf8db6f72015-06-28 23:08:20 +0200244echo "#include <stdlib.h>" > "$tmpc"
245echo "#if ! __GLIBC__" >> "$tmpc"
246echo "#error no" >> "$tmpc"
247echo "#endif" >> "$tmpc"
Shizb3cd7d12015-06-28 23:08:19 +0200248printf "checking for toolchain wrapper to build... "
Shizf8db6f72015-06-28 23:08:20 +0200249if test "$wrapper" = auto && ! $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
250echo "none"
251elif test "$cc_family" = gcc ; then
Shizb3cd7d12015-06-28 23:08:19 +0200252gcc_wrapper=yes
Shizf8db6f72015-06-28 23:08:20 +0200253echo "gcc"
Shizfb585452015-06-28 23:08:21 +0200254elif test "$cc_family" = clang ; then
255clang_wrapper=yes
256echo "clang"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400257else
Shizb3cd7d12015-06-28 23:08:19 +0200258echo "none"
259if test "$wrapper" = detect ; then
260fail "$0: could not find an appropriate toolchain wrapper"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400261fi
Shizb3cd7d12015-06-28 23:08:19 +0200262fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400263fi
264
Shizb3cd7d12015-06-28 23:08:19 +0200265if test "$gcc_wrapper" = yes ; then
266tools="$tools tools/musl-gcc"
267tool_libs="$tool_libs lib/musl-gcc.specs"
268fi
Shizfb585452015-06-28 23:08:21 +0200269if test "$clang_wrapper" = yes ; then
270tools="$tools tools/musl-clang tools/ld.musl-clang"
271fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400272
273#
Rich Felker278883d2012-06-03 16:22:13 -0400274# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400275#
Rich Felker278883d2012-06-03 16:22:13 -0400276printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400277test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400278printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400279
280#
281# Convert to just ARCH
282#
Rich Felker278883d2012-06-03 16:22:13 -0400283case "$target" in
Rich Felker0b8f0c52014-02-28 13:12:40 -0500284# Catch these early to simplify matching for 32-bit archs
285mips64*|powerpc64*) fail "$0: unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400286arm*) ARCH=arm ;;
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000287aarch64*) ARCH=aarch64 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400288i?86*) ARCH=i386 ;;
Rich Felkerf162c062014-03-17 17:38:22 -0400289x86_64-x32*|x32*|x86_64*x32) ARCH=x32 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400290x86_64*) ARCH=x86_64 ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500291mips*) ARCH=mips ;;
292microblaze*) ARCH=microblaze ;;
Stefan Kristiansson200d1542014-07-17 22:09:10 +0300293or1k*) ARCH=or1k ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500294powerpc*) ARCH=powerpc ;;
295sh[1-9bel-]*|sh|superh*) ARCH=sh ;;
Rich Felker278883d2012-06-03 16:22:13 -0400296unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
297*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400298esac
299
300#
301# Try to get a conforming C99 freestanding environment
302#
303tryflag CFLAGS_C99FSE -std=c99
304tryflag CFLAGS_C99FSE -nostdinc
305tryflag CFLAGS_C99FSE -ffreestanding \
306|| tryflag CFLAGS_C99FSE -fno-builtin
307tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400308|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400309tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400310
Rich Felker4a1f55e2013-08-01 17:12:23 -0400311#
Rich Felker06ceee82013-08-27 17:33:47 -0400312# We may use the may_alias attribute if __GNUC__ is defined, so
313# if the compiler defines __GNUC__ but does not provide it,
314# it must be defined away as part of the CFLAGS.
315#
316printf "checking whether compiler needs attribute((may_alias)) suppression... "
317cat > "$tmpc" <<EOF
318typedef int
319#ifdef __GNUC__
320__attribute__((__may_alias__))
321#endif
322x;
323EOF
324if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
325 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
326printf "no\n"
327else
328printf "yes\n"
329CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
330fi
331
332#
Rich Felkerbc0c4842015-10-23 00:01:01 -0400333# The GNU toolchain defaults to assuming unmarked files need an
334# executable stack, potentially exposing vulnerabilities in programs
335# linked with such object files. Fix this.
336#
337tryflag CFLAGS_C99FSE -Wa,--noexecstack
338
339#
Rich Felker1ef849c2015-04-13 20:13:10 -0400340# Check for options to disable stack protector, which needs to be
341# disabled for a few early-bootstrap translation units. If not found,
342# this is not an error; we assume the toolchain does not do ssp.
343#
344tryflag CFLAGS_NOSSP -fno-stack-protector
345
346#
Rich Felker4a1f55e2013-08-01 17:12:23 -0400347# Check for options that may be needed to prevent the compiler from
348# generating self-referential versions of memcpy,, memmove, memcmp,
349# and memset. Really, we should add a check to determine if this
350# option is sufficient, and if not, add a macro to cripple these
351# functions with volatile...
352#
353tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
Rich Felkera80847d2013-07-22 21:22:04 -0400354
Rich Felker64d2f8e2012-05-04 23:24:51 -0400355#
Rich Felker4ad35882014-06-20 16:10:48 -0400356# Enable debugging if requessted.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400357#
Rich Felker4ad35882014-06-20 16:10:48 -0400358test "$debug" = yes && CFLAGS_AUTO=-g
Rich Felkera80847d2013-07-22 21:22:04 -0400359
360#
Alex Dowad35b33122015-07-10 15:03:24 +0200361# Preprocess asm files to add extra debugging information if debug is
362# enabled, our assembler supports the needed directives, and the
363# preprocessing script has been written for our architecture.
364#
365printf "checking whether we should preprocess assembly to add debugging information... "
366if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" &&
367 test -f "tools/add-cfi.$ARCH.awk" &&
368 printf ".file 1 \"srcfile.s\"\n.line 1\n.cfi_startproc\n.cfi_endproc" | $CC -g -x assembler -c -o /dev/null 2>/dev/null -
369then
370 ADD_CFI=yes
371else
372 ADD_CFI=no
373fi
374printf "%s\n" "$ADD_CFI"
375
376#
Rich Felkera80847d2013-07-22 21:22:04 -0400377# Possibly add a -O option to CFLAGS and select modules to optimize with
378# -O3 based on the status of --enable-optimize and provided CFLAGS.
379#
380printf "checking for optimization settings... "
381case "x$optimize" in
382xauto)
383if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
384printf "using provided CFLAGS\n" ;optimize=no
385else
386printf "using defaults\n" ; optimize=yes
387fi
388;;
389xsize|xnone) printf "minimize size\n" ; optimize=size ;;
390xno|x) printf "disabled\n" ; optimize=no ;;
391*) printf "custom\n" ;;
392esac
393
394test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
Rich Felker43d25312013-07-24 23:21:45 -0400395test "$optimize" = yes && optimize="internal,malloc,string"
Rich Felkera80847d2013-07-22 21:22:04 -0400396
397if fnmatch 'no|size' "$optimize" ; then :
398else
399printf "components to be optimized for speed:"
400while test "$optimize" ; do
401case "$optimize" in
402*,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
403*) this=$optimize optimize=
404esac
405printf " $this"
406case "$this" in
407*/*.c) ;;
408*/*) this=$this*.c ;;
409*) this=$this/*.c ;;
410esac
411OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
412done
413OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
414printf "\n"
415fi
416
417# Always try -pipe
Rich Felker64d2f8e2012-05-04 23:24:51 -0400418tryflag CFLAGS_AUTO -pipe
419
420#
Rich Felkerb439c052012-08-29 09:36:02 -0400421# If debugging is disabled, omit frame pointer. Modern GCC does this
422# anyway on most archs even when debugging is enabled since the frame
423# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400424#
425if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
426else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400427tryflag CFLAGS_AUTO -fomit-frame-pointer
428fi
429
430#
Rich Felkerb439c052012-08-29 09:36:02 -0400431# Modern GCC wants to put DWARF tables (used for debugging and
432# unwinding) in the loaded part of the program where they are
433# unstrippable. These options force them back to debug sections (and
434# cause them not to get generated at all if debugging is off).
435#
436tryflag CFLAGS_AUTO -fno-unwind-tables
437tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
438
439#
Rich Felker27c1ecc2015-11-04 13:24:11 -0500440# Attempt to put each function and each data object in its own
441# section. This both allows additional size optimizations at link
442# time and works around a dangerous class of compiler/assembler bugs
443# whereby relative address expressions are constant-folded by the
444# assembler even when one or more of the symbols involved is
445# replaceable. See gas pr 18561 and gcc pr 66609, 68178, etc.
446#
447tryflag CFLAGS_AUTO -ffunction-sections
448tryflag CFLAGS_AUTO -fdata-sections
449
450#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400451# On x86, make sure we don't have incompatible instruction set
452# extensions enabled by default. This is bad for making static binaries.
453# We cheat and use i486 rather than i386 because i386 really does not
454# work anyway (issues with atomic ops).
Andre McCurdya6274a12015-04-21 10:34:05 -0700455# Some build environments pass -march and -mtune options via CC, so
456# check both CC and CFLAGS.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400457#
458if test "$ARCH" = "i386" ; then
Andre McCurdya6274a12015-04-21 10:34:05 -0700459fnmatch '-march=*|*\ -march=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
460fnmatch '-mtune=*|*\ -mtune=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400461fi
462
Rich Felker2384f272012-12-11 23:28:31 -0500463#
464# Even with -std=c99, gcc accepts some constructs which are constraint
465# violations. We want to treat these as errors regardless of whether
466# other purely stylistic warnings are enabled -- especially implicit
467# function declarations, which are a dangerous programming error.
468#
469tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
470tryflag CFLAGS_AUTO -Werror=implicit-int
471tryflag CFLAGS_AUTO -Werror=pointer-sign
472tryflag CFLAGS_AUTO -Werror=pointer-arith
473
Rich Felker64d2f8e2012-05-04 23:24:51 -0400474if test "x$warnings" = xyes ; then
475tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400476tryflag CFLAGS_AUTO -Wno-parentheses
477tryflag CFLAGS_AUTO -Wno-uninitialized
478tryflag CFLAGS_AUTO -Wno-missing-braces
479tryflag CFLAGS_AUTO -Wno-unused-value
480tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
481tryflag CFLAGS_AUTO -Wno-unknown-pragmas
rofl0radbeefb2014-01-08 03:11:46 +0100482tryflag CFLAGS_AUTO -Wno-pointer-to-int-cast
Rich Felker64d2f8e2012-05-04 23:24:51 -0400483fi
484
Rich Felker3aacb542015-04-22 22:11:48 -0400485if test "x$visibility" = xauto ; then
Rich Felkerde2b67f2015-04-19 22:05:29 -0400486# This test checks toolchain support for several things:
487# - the -include option
488# - the attributes/pragmas used in vis.h
489# - linking code that takes the address of protected symbols
Rich Felkerf3a53f02015-09-29 02:44:05 +0000490# - gcc 3.x bug that wrongly claims declarations mismatch
Rich Felkerde2b67f2015-04-19 22:05:29 -0400491printf "checking whether global visibility preinclude works... "
Rich Felkerf3a53f02015-09-29 02:44:05 +0000492cat > "$tmpc" <<EOF
493__attribute__((__visibility__("default")))
494extern struct a *const x;
495typedef struct a b;
496extern b *const x;
497b *const x;
498int (*fp)(void);
499int foo(void) { }
500int bar(void) { fp = foo; return foo(); }
501EOF
Rich Felker60ed9882015-04-20 18:14:19 -0400502if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
Rich Felker428462a2015-04-22 01:41:00 -0400503 -DSHARED -fPIC -I./src/internal -include vis.h \
Rich Felkerde2b67f2015-04-19 22:05:29 -0400504 -nostdlib -shared -Wl,-Bsymbolic-functions \
505 -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
506visibility=yes
507else
508visibility=no
509fi
510printf "%s\n" "$visibility"
511fi
512
Rich Felker3aacb542015-04-22 22:11:48 -0400513if test "x$visibility" = xyes ; then
Rich Felker428462a2015-04-22 01:41:00 -0400514CFLAGS_AUTO="$CFLAGS_AUTO -include vis.h"
Rich Felkerde2b67f2015-04-19 22:05:29 -0400515CFLAGS_AUTO="${CFLAGS_AUTO# }"
516fi
517
Rich Felker2efd38e2015-11-04 21:39:13 -0500518# Reduce space lost to padding for alignment purposes by sorting data
519# objects according to their alignment reqirements. This approximates
520# optimal packing.
521tryldflag LDFLAGS_AUTO -Wl,--sort-section,alignment
522tryldflag LDFLAGS_AUTO -Wl,--sort-common
523
Rich Felker6a851e32015-11-04 21:40:36 -0500524# When linking shared library, drop dummy weak definitions that were
525# replaced by strong definitions from other translation units.
526tryldflag LDFLAGS_AUTO -Wl,--gc-sections
527
Rich Felker0c5efde2012-06-06 22:00:08 -0400528# Some patched GCC builds have these defaults messed up...
Rich Felker2bd05a42012-08-25 17:13:28 -0400529tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400530
Rich Felker24623702015-09-22 15:45:40 +0000531# Prevent linking if there are undefined symbols; if any exist,
532# libc.so will crash at runtime during relocation processing.
533# The common way this can happen is failure to link the compiler
534# runtime library; implementation error is also a possibility.
535tryldflag LDFLAGS_AUTO -Wl,--no-undefined
536
Rich Felkerd79b2772014-06-10 12:11:12 -0400537test "$shared" = "no" || {
Rich Felker498a1002012-06-07 00:32:22 -0400538# Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
539LDFLAGS_DUMMY=
540tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
Rich Felkerd79b2772014-06-10 12:11:12 -0400541test "$shared" = "yes" && fail "$0: error: linker cannot build shared library"
Rich Felker498a1002012-06-07 00:32:22 -0400542printf "warning: disabling dynamic linking support\n"
543shared=no
544}
Rich Felkerd79b2772014-06-10 12:11:12 -0400545}
Rich Felker498a1002012-06-07 00:32:22 -0400546
Rich Felker2c1cd232012-09-10 15:30:52 -0400547# Find compiler runtime library
548test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
549test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400550test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
551 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400552printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400553
Rich Felker3e7f1862013-07-18 20:30:58 -0400554# Figure out arch variants for archs with variants
555SUBARCH=
Rich Felker428462a2015-04-22 01:41:00 -0400556t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS"
Rich Felker3e7f1862013-07-18 20:30:58 -0400557
rofl0r8c820232014-03-19 22:31:00 +0100558if test "$ARCH" = "x86_64" ; then
559trycppif __ILP32__ "$t" && ARCH=x32
560fi
561
Rich Felker3e7f1862013-07-18 20:30:58 -0400562if test "$ARCH" = "arm" ; then
563trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
Rich Felker4918c2b2013-08-16 17:09:07 -0400564trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
Rich Felker3e7f1862013-07-18 20:30:58 -0400565fi
566
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000567if test "$ARCH" = "aarch64" ; then
568trycppif __AARCH64EB__ "$t" && SUBARCH=${SUBARCH}_be
569fi
570
Szabolcs Nagye5bb1652014-02-24 23:16:29 +0100571if test "$ARCH" = "mips" ; then
572trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
573trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
574fi
Rich Felker3e7f1862013-07-18 20:30:58 -0400575
576test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
577&& SUBARCH=${SUBARCH}el
578
Rich Felkerb1683a12014-02-27 23:18:42 -0500579if test "$ARCH" = "sh" ; then
Rich Felker79789982015-10-15 17:21:07 -0400580tryflag CFLAGS_AUTO -Wa,--isa=any
Rich Felkerb1683a12014-02-27 23:18:42 -0500581trycppif __BIG_ENDIAN__ "$t" && SUBARCH=${SUBARCH}eb
Bobby Bingham23d64182014-04-27 21:13:59 -0500582if trycppif "__SH_FPU_ANY__ || __SH4__" "$t" ; then
Rich Felkerb1683a12014-02-27 23:18:42 -0500583# Some sh configurations are broken and replace double with float
584# rather than using softfloat when the fpu is present but only
585# supports single precision. Reject them.
586printf "checking whether compiler's double type is IEEE double... "
Rich Felker946b9fa2014-02-27 23:55:04 -0500587echo 'typedef char dblcheck[(int)sizeof(double)-5];' > "$tmpc"
Rich Felkerb1683a12014-02-27 23:18:42 -0500588if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
589printf "yes\n"
590else
591printf "no\n"
592fail "$0: error: compiler's floating point configuration is unsupported"
593fi
594else
595SUBARCH=${SUBARCH}-nofpu
596fi
Rich Felkerd4c82d02015-09-12 03:22:19 +0000597if trycppif __SH_FDPIC__ "$t" ; then
598SUBARCH=${SUBARCH}-fdpic
Rich Felkerd4c82d02015-09-12 03:22:19 +0000599fi
Rich Felkerb1683a12014-02-27 23:18:42 -0500600fi
Bobby Bingham3a3c8132013-10-05 05:13:18 -0500601
Rich Felker3e7f1862013-07-18 20:30:58 -0400602test "$SUBARCH" \
603&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400604
Rich Felker90d77722013-08-11 03:27:35 -0400605case "$ARCH$SUBARCH" in
606arm) ASMSUBARCH=el ;;
607*) ASMSUBARCH=$SUBARCH ;;
608esac
609
Rich Felker86cc54b2013-08-02 19:34:22 -0400610#
611# Some archs (powerpc) have different possible long double formats
612# that the compiler can be configured for. The logic for whether this
613# is supported is in bits/float.h; in general, it is not. We need to
614# check for mismatches here or code in printf, strotd, and scanf will
615# be dangerously incorrect because it depends on (1) the macros being
616# correct, and (2) IEEE semantics.
617#
618printf "checking whether compiler's long double definition matches float.h... "
619echo '#include <float.h>' > "$tmpc"
620echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
621echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
622echo '#endif' >> "$tmpc"
623if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
624 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
625printf "yes\n"
626else
627printf "no\n"
628fail "$0: error: unsupported long double type"
629fi
630
Rich Felker64d2f8e2012-05-04 23:24:51 -0400631printf "creating config.mak... "
632
Rich Felker453f4622013-08-16 18:19:47 -0400633cmdline=$(quote "$0")
634for i ; do cmdline="$cmdline $(quote "$i")" ; done
635
Rich Felker64d2f8e2012-05-04 23:24:51 -0400636exec 3>&1 1>config.mak
637
638
639cat << EOF
Rich Felker453f4622013-08-16 18:19:47 -0400640# This version of config.mak was generated by:
641# $cmdline
Rich Felker64d2f8e2012-05-04 23:24:51 -0400642# Any changes made here will be lost if configure is re-run
643ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400644SUBARCH = $SUBARCH
Rich Felker90d77722013-08-11 03:27:35 -0400645ASMSUBARCH = $ASMSUBARCH
Rich Felker64d2f8e2012-05-04 23:24:51 -0400646prefix = $prefix
647exec_prefix = $exec_prefix
648bindir = $bindir
649libdir = $libdir
650includedir = $includedir
651syslibdir = $syslibdir
652CC = $CC
Rich Felker4cd8b472015-11-02 16:58:14 -0500653CFLAGS = $CFLAGS
654CFLAGS_AUTO = $CFLAGS_AUTO
Rich Felker64d2f8e2012-05-04 23:24:51 -0400655CFLAGS_C99FSE = $CFLAGS_C99FSE
Rich Felker4a1f55e2013-08-01 17:12:23 -0400656CFLAGS_MEMOPS = $CFLAGS_MEMOPS
Rich Felker1ef849c2015-04-13 20:13:10 -0400657CFLAGS_NOSSP = $CFLAGS_NOSSP
Rich Felker64d2f8e2012-05-04 23:24:51 -0400658CPPFLAGS = $CPPFLAGS
Rich Felker4cd8b472015-11-02 16:58:14 -0500659LDFLAGS = $LDFLAGS
660LDFLAGS_AUTO = $LDFLAGS_AUTO
Rich Felker94e920d2012-08-14 22:50:16 -0400661CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400662LIBCC = $LIBCC
Rich Felkera80847d2013-07-22 21:22:04 -0400663OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
Shizb3cd7d12015-06-28 23:08:19 +0200664ALL_TOOLS = $tools
665TOOL_LIBS = $tool_libs
Alex Dowad35b33122015-07-10 15:03:24 +0200666ADD_CFI = $ADD_CFI
Rich Felker64d2f8e2012-05-04 23:24:51 -0400667EOF
668test "x$static" = xno && echo "STATIC_LIBS ="
669test "x$shared" = xno && echo "SHARED_LIBS ="
Shizb3cd7d12015-06-28 23:08:19 +0200670test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
Shizfb585452015-06-28 23:08:21 +0200671test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
Rich Felker64d2f8e2012-05-04 23:24:51 -0400672exec 1>&3 3>&-
673
674printf "done\n"