blob: 248c45005eb0c3eeff2121a369572436c91aa67c [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
Petr Hosek2f853dd2015-11-18 12:07:32 -080012Configuration:
13 --srcdir=DIR source directory [detected]
14
Rich Felker64d2f8e2012-05-04 23:24:51 -040015Installation directories:
16 --prefix=PREFIX main installation prefix [/usr/local/musl]
17 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
18
19Fine tuning of the installation directories:
20 --bindir=DIR user executables [EPREFIX/bin]
21 --libdir=DIR library files for the linker [PREFIX/lib]
22 --includedir=DIR include files for the C compiler [PREFIX/include]
23 --syslibdir=DIR location for the dynamic linker [/lib]
24
25System types:
26 --target=TARGET configure to run on target TARGET [detected]
Rich Felker278883d2012-06-03 16:22:13 -040027 --host=HOST same as --target
Rich Felker64d2f8e2012-05-04 23:24:51 -040028
29Optional features:
Rich Felkera80847d2013-07-22 21:22:04 -040030 --enable-optimize=... optimize listed components for speed over size [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040031 --enable-debug build with debugging information [disabled]
32 --enable-warnings build with recommended warnings flags [disabled]
Rich Felkerde2b67f2015-04-19 22:05:29 -040033 --enable-visibility use global visibility options to optimize PIC [auto]
Shizb3cd7d12015-06-28 23:08:19 +020034 --enable-wrapper=... build given musl toolchain wrapper [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040035 --disable-shared inhibit building shared library [enabled]
36 --disable-static inhibit building static library [enabled]
37
38Some influential environment variables:
39 CC C compiler command [detected]
40 CFLAGS C compiler flags [-Os -pipe ...]
Rich Felker94e920d2012-08-14 22:50:16 -040041 CROSS_COMPILE prefix for cross compiler and tools [none]
Rich Felker2c1cd232012-09-10 15:30:52 -040042 LIBCC compiler runtime library [detected]
Rich Felker64d2f8e2012-05-04 23:24:51 -040043
44Use these variables to override the choices made by configure.
45
46EOF
47exit 0
48}
49
50# Helper functions
51
Rich Felker453f4622013-08-16 18:19:47 -040052quote () {
53tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
54$1
55EOF
Rich Felkere4499742013-08-20 13:51:46 -040056printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
Rich Felker453f4622013-08-16 18:19:47 -040057}
Rich Felker64d2f8e2012-05-04 23:24:51 -040058echo () { printf "%s\n" "$*" ; }
59fail () { echo "$*" ; exit 1 ; }
60fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
61cmdexists () { type "$1" >/dev/null 2>&1 ; }
62trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
63
Rich Felker3d9e3a32012-11-08 17:20:50 -050064stripdir () {
65while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
Rich Felker64d2f8e2012-05-04 23:24:51 -040066}
67
Rich Felker3e7f1862013-07-18 20:30:58 -040068trycppif () {
69printf "checking preprocessor condition %s... " "$1"
Rich Felkerdf065782013-07-18 20:37:19 -040070echo "typedef int x;" > "$tmpc"
71echo "#if $1" >> "$tmpc"
Rich Felker3e7f1862013-07-18 20:30:58 -040072echo "#error yes" >> "$tmpc"
73echo "#endif" >> "$tmpc"
74if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
75printf "false\n"
76return 1
77else
78printf "true\n"
79return 0
80fi
81}
82
Rich Felker64d2f8e2012-05-04 23:24:51 -040083tryflag () {
84printf "checking whether compiler accepts %s... " "$2"
85echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +020086if $CC $CFLAGS_TRY $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040087printf "yes\n"
88eval "$1=\"\${$1} \$2\""
89eval "$1=\${$1# }"
90return 0
91else
92printf "no\n"
93return 1
94fi
95}
96
Rich Felker08f70a32012-06-06 20:45:52 -040097tryldflag () {
98printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -040099echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +0200100if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -0400101printf "yes\n"
102eval "$1=\"\${$1} \$2\""
103eval "$1=\${$1# }"
104return 0
105else
106printf "no\n"
107return 1
108fi
109}
110
Rich Felker64d2f8e2012-05-04 23:24:51 -0400111
112
113# Beginning of actual script
114
Rich Felker08f70a32012-06-06 20:45:52 -0400115CFLAGS_C99FSE=
116CFLAGS_AUTO=
Rich Felker4a1f55e2013-08-01 17:12:23 -0400117CFLAGS_MEMOPS=
Rich Felker1ef849c2015-04-13 20:13:10 -0400118CFLAGS_NOSSP=
Shizfc431d32015-05-28 05:52:22 +0200119CFLAGS_TRY=
Rich Felker08f70a32012-06-06 20:45:52 -0400120LDFLAGS_AUTO=
Shizfc431d32015-05-28 05:52:22 +0200121LDFLAGS_TRY=
Rich Felkera80847d2013-07-22 21:22:04 -0400122OPTIMIZE_GLOBS=
Petr Hosek2f853dd2015-11-18 12:07:32 -0800123srcdir=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500124prefix=/usr/local/musl
125exec_prefix='$(prefix)'
126bindir='$(exec_prefix)/bin'
127libdir='$(prefix)/lib'
128includedir='$(prefix)/include'
129syslibdir='/lib'
Shizb3cd7d12015-06-28 23:08:19 +0200130tools=
131tool_libs=
Rich Felker278883d2012-06-03 16:22:13 -0400132target=
Rich Felkera80847d2013-07-22 21:22:04 -0400133optimize=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400134debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500135warnings=no
Rich Felkerde2b67f2015-04-19 22:05:29 -0400136visibility=auto
Rich Felkerd79b2772014-06-10 12:11:12 -0400137shared=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400138static=yes
Rich Felker9ca4dae2014-05-19 10:33:28 -0400139wrapper=auto
Shizb3cd7d12015-06-28 23:08:19 +0200140gcc_wrapper=no
Shizfb585452015-06-28 23:08:21 +0200141clang_wrapper=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400142
143for arg ; do
144case "$arg" in
Rich Felker47314f12016-02-02 21:14:09 -0500145--help|-h) usage ;;
Petr Hosek2f853dd2015-11-18 12:07:32 -0800146--srcdir=*) srcdir=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400147--prefix=*) prefix=${arg#*=} ;;
148--exec-prefix=*) exec_prefix=${arg#*=} ;;
149--bindir=*) bindir=${arg#*=} ;;
150--libdir=*) libdir=${arg#*=} ;;
151--includedir=*) includedir=${arg#*=} ;;
152--syslibdir=*) syslibdir=${arg#*=} ;;
153--enable-shared|--enable-shared=yes) shared=yes ;;
154--disable-shared|--enable-shared=no) shared=no ;;
155--enable-static|--enable-static=yes) static=yes ;;
156--disable-static|--enable-static=no) static=no ;;
Rich Felkera80847d2013-07-22 21:22:04 -0400157--enable-optimize) optimize=yes ;;
158--enable-optimize=*) optimize=${arg#*=} ;;
159--disable-optimize) optimize=no ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400160--enable-debug|--enable-debug=yes) debug=yes ;;
161--disable-debug|--enable-debug=no) debug=no ;;
162--enable-warnings|--enable-warnings=yes) warnings=yes ;;
163--disable-warnings|--enable-warnings=no) warnings=no ;;
Rich Felkerde2b67f2015-04-19 22:05:29 -0400164--enable-visibility|--enable-visibility=yes) visibility=yes ;;
165--disable-visibility|--enable-visibility=no) visibility=no ;;
Shizb3cd7d12015-06-28 23:08:19 +0200166--enable-wrapper|--enable-wrapper=yes) wrapper=detect ;;
Shizfb585452015-06-28 23:08:21 +0200167--enable-wrapper=all) wrapper=yes ; gcc_wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200168--enable-wrapper=gcc) wrapper=yes ; gcc_wrapper=yes ;;
Shizfb585452015-06-28 23:08:21 +0200169--enable-wrapper=clang) wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200170--disable-wrapper|--enable-wrapper=no) wrapper=no ;;
171--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ; gcc_wrapper=yes ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400172--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
Rich Felker278883d2012-06-03 16:22:13 -0400173--enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
174--host=*|--target=*) target=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400175-* ) echo "$0: unknown option $arg" ;;
176CC=*) CC=${arg#*=} ;;
177CFLAGS=*) CFLAGS=${arg#*=} ;;
178CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
179LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400180CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400181LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400182*=*) ;;
183*) target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400184esac
185done
186
Petr Hosek2f853dd2015-11-18 12:07:32 -0800187for i in srcdir prefix exec_prefix bindir libdir includedir syslibdir ; do
Rich Felker3d9e3a32012-11-08 17:20:50 -0500188stripdir $i
189done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400190
191#
Petr Hosek2f853dd2015-11-18 12:07:32 -0800192# Get the source dir for out-of-tree builds
193#
194if test -z "$srcdir" ; then
195srcdir="${0%/configure}"
196stripdir srcdir
197fi
198abs_builddir="$(pwd)" || fail "$0: cannot determine working directory"
199abs_srcdir="$(cd $srcdir && pwd)" || fail "$0: invalid source directory $srcdir"
200test "$abs_srcdir" = "$abs_builddir" && srcdir=.
201test "$srcdir" != "." -a -f Makefile -a ! -h Makefile && fail "$0: Makefile already exists in the working directory"
202
203#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400204# Get a temp filename we can use
205#
206i=0
207set -C
208while : ; do i=$(($i+1))
209tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05002102>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400211test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
212done
213set +C
214trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
215
216#
217# Find a C compiler to use
218#
219printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400220trycc ${CROSS_COMPILE}gcc
221trycc ${CROSS_COMPILE}c99
222trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400223printf "%s\n" "$CC"
224test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
225
Rich Felker89456672014-05-12 14:22:57 -0400226printf "checking whether C compiler works... "
227echo "typedef int x;" > "$tmpc"
228if output=$($CC $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" 2>&1) ; then
229printf "yes\n"
230else
231printf "no; compiler output follows:\n%s\n" "$output"
232exit 1
233fi
234
Rich Felker64d2f8e2012-05-04 23:24:51 -0400235#
Shizfc431d32015-05-28 05:52:22 +0200236# Figure out options to force errors on unknown flags.
237#
238tryflag CFLAGS_TRY -Werror=unknown-warning-option
239tryflag CFLAGS_TRY -Werror=unused-command-line-argument
240tryldflag LDFLAGS_TRY -Werror=unknown-warning-option
241tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
242
243#
Shizb3cd7d12015-06-28 23:08:19 +0200244# Need to know if the compiler is gcc or clang to decide which toolchain
245# wrappers to build.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400246#
Shizb3cd7d12015-06-28 23:08:19 +0200247printf "checking for C compiler family... "
248cc_ver="$(LC_ALL=C $CC -v 2>&1)"
249cc_family=unknown
250if fnmatch '*gcc\ version*' "$cc_ver" ; then
251cc_family=gcc
Shizfb585452015-06-28 23:08:21 +0200252elif fnmatch '*clang\ version*' "$cc_ver" ; then
253cc_family=clang
Rich Felker9ca4dae2014-05-19 10:33:28 -0400254fi
Shizb3cd7d12015-06-28 23:08:19 +0200255echo "$cc_family"
Rich Felker9ca4dae2014-05-19 10:33:28 -0400256
257#
Shizb3cd7d12015-06-28 23:08:19 +0200258# Figure out toolchain wrapper to build
Rich Felker9ca4dae2014-05-19 10:33:28 -0400259#
Shizb3cd7d12015-06-28 23:08:19 +0200260if test "$wrapper" = auto -o "$wrapper" = detect ; then
Shizf8db6f72015-06-28 23:08:20 +0200261echo "#include <stdlib.h>" > "$tmpc"
262echo "#if ! __GLIBC__" >> "$tmpc"
263echo "#error no" >> "$tmpc"
264echo "#endif" >> "$tmpc"
Shizb3cd7d12015-06-28 23:08:19 +0200265printf "checking for toolchain wrapper to build... "
Shizf8db6f72015-06-28 23:08:20 +0200266if test "$wrapper" = auto && ! $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
267echo "none"
268elif test "$cc_family" = gcc ; then
Shizb3cd7d12015-06-28 23:08:19 +0200269gcc_wrapper=yes
Shizf8db6f72015-06-28 23:08:20 +0200270echo "gcc"
Shizfb585452015-06-28 23:08:21 +0200271elif test "$cc_family" = clang ; then
272clang_wrapper=yes
273echo "clang"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400274else
Shizb3cd7d12015-06-28 23:08:19 +0200275echo "none"
276if test "$wrapper" = detect ; then
277fail "$0: could not find an appropriate toolchain wrapper"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400278fi
Shizb3cd7d12015-06-28 23:08:19 +0200279fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400280fi
281
Shizb3cd7d12015-06-28 23:08:19 +0200282if test "$gcc_wrapper" = yes ; then
Petr Hosek2f853dd2015-11-18 12:07:32 -0800283tools="$tools obj/musl-gcc"
Shizb3cd7d12015-06-28 23:08:19 +0200284tool_libs="$tool_libs lib/musl-gcc.specs"
285fi
Shizfb585452015-06-28 23:08:21 +0200286if test "$clang_wrapper" = yes ; then
Petr Hosek2f853dd2015-11-18 12:07:32 -0800287tools="$tools obj/musl-clang obj/ld.musl-clang"
Shizfb585452015-06-28 23:08:21 +0200288fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400289
290#
Rich Felker278883d2012-06-03 16:22:13 -0400291# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400292#
Rich Felker278883d2012-06-03 16:22:13 -0400293printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400294test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400295printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400296
297#
298# Convert to just ARCH
299#
Rich Felker278883d2012-06-03 16:22:13 -0400300case "$target" in
Rich Felker0b8f0c52014-02-28 13:12:40 -0500301# Catch these early to simplify matching for 32-bit archs
302mips64*|powerpc64*) fail "$0: unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400303arm*) ARCH=arm ;;
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000304aarch64*) ARCH=aarch64 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400305i?86*) ARCH=i386 ;;
Rich Felkerf162c062014-03-17 17:38:22 -0400306x86_64-x32*|x32*|x86_64*x32) ARCH=x32 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400307x86_64*) ARCH=x86_64 ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500308mips*) ARCH=mips ;;
309microblaze*) ARCH=microblaze ;;
Stefan Kristiansson200d1542014-07-17 22:09:10 +0300310or1k*) ARCH=or1k ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500311powerpc*) ARCH=powerpc ;;
312sh[1-9bel-]*|sh|superh*) ARCH=sh ;;
Rich Felker278883d2012-06-03 16:22:13 -0400313unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
314*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400315esac
316
317#
318# Try to get a conforming C99 freestanding environment
319#
320tryflag CFLAGS_C99FSE -std=c99
321tryflag CFLAGS_C99FSE -nostdinc
322tryflag CFLAGS_C99FSE -ffreestanding \
323|| tryflag CFLAGS_C99FSE -fno-builtin
324tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400325|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400326tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400327
Rich Felker4a1f55e2013-08-01 17:12:23 -0400328#
Rich Felker06ceee82013-08-27 17:33:47 -0400329# We may use the may_alias attribute if __GNUC__ is defined, so
330# if the compiler defines __GNUC__ but does not provide it,
331# it must be defined away as part of the CFLAGS.
332#
333printf "checking whether compiler needs attribute((may_alias)) suppression... "
334cat > "$tmpc" <<EOF
335typedef int
336#ifdef __GNUC__
337__attribute__((__may_alias__))
338#endif
339x;
340EOF
Rich Felkere1d99892016-01-27 19:01:21 -0500341if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
Rich Felker06ceee82013-08-27 17:33:47 -0400342 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
343printf "no\n"
344else
345printf "yes\n"
346CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
347fi
348
349#
Rich Felkerbc0c4842015-10-23 00:01:01 -0400350# The GNU toolchain defaults to assuming unmarked files need an
351# executable stack, potentially exposing vulnerabilities in programs
352# linked with such object files. Fix this.
353#
354tryflag CFLAGS_C99FSE -Wa,--noexecstack
355
356#
Rich Felker1ef849c2015-04-13 20:13:10 -0400357# Check for options to disable stack protector, which needs to be
358# disabled for a few early-bootstrap translation units. If not found,
359# this is not an error; we assume the toolchain does not do ssp.
360#
361tryflag CFLAGS_NOSSP -fno-stack-protector
362
363#
Rich Felker4a1f55e2013-08-01 17:12:23 -0400364# Check for options that may be needed to prevent the compiler from
365# generating self-referential versions of memcpy,, memmove, memcmp,
366# and memset. Really, we should add a check to determine if this
367# option is sufficient, and if not, add a macro to cripple these
368# functions with volatile...
369#
370tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
Rich Felkera80847d2013-07-22 21:22:04 -0400371
Rich Felker64d2f8e2012-05-04 23:24:51 -0400372#
Rich Felker4ad35882014-06-20 16:10:48 -0400373# Enable debugging if requessted.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400374#
Rich Felker4ad35882014-06-20 16:10:48 -0400375test "$debug" = yes && CFLAGS_AUTO=-g
Rich Felkera80847d2013-07-22 21:22:04 -0400376
377#
Alex Dowad35b33122015-07-10 15:03:24 +0200378# Preprocess asm files to add extra debugging information if debug is
379# enabled, our assembler supports the needed directives, and the
380# preprocessing script has been written for our architecture.
381#
382printf "checking whether we should preprocess assembly to add debugging information... "
383if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" &&
384 test -f "tools/add-cfi.$ARCH.awk" &&
385 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 -
386then
387 ADD_CFI=yes
388else
389 ADD_CFI=no
390fi
391printf "%s\n" "$ADD_CFI"
392
393#
Rich Felkera80847d2013-07-22 21:22:04 -0400394# Possibly add a -O option to CFLAGS and select modules to optimize with
395# -O3 based on the status of --enable-optimize and provided CFLAGS.
396#
397printf "checking for optimization settings... "
398case "x$optimize" in
399xauto)
400if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
401printf "using provided CFLAGS\n" ;optimize=no
402else
403printf "using defaults\n" ; optimize=yes
404fi
405;;
406xsize|xnone) printf "minimize size\n" ; optimize=size ;;
407xno|x) printf "disabled\n" ; optimize=no ;;
408*) printf "custom\n" ;;
409esac
410
411test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
Rich Felker43d25312013-07-24 23:21:45 -0400412test "$optimize" = yes && optimize="internal,malloc,string"
Rich Felkera80847d2013-07-22 21:22:04 -0400413
414if fnmatch 'no|size' "$optimize" ; then :
415else
416printf "components to be optimized for speed:"
417while test "$optimize" ; do
418case "$optimize" in
419*,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
420*) this=$optimize optimize=
421esac
422printf " $this"
423case "$this" in
424*/*.c) ;;
425*/*) this=$this*.c ;;
426*) this=$this/*.c ;;
427esac
428OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
429done
430OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
431printf "\n"
432fi
433
434# Always try -pipe
Rich Felker64d2f8e2012-05-04 23:24:51 -0400435tryflag CFLAGS_AUTO -pipe
436
437#
Rich Felkerb439c052012-08-29 09:36:02 -0400438# If debugging is disabled, omit frame pointer. Modern GCC does this
439# anyway on most archs even when debugging is enabled since the frame
440# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400441#
442if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
443else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400444tryflag CFLAGS_AUTO -fomit-frame-pointer
445fi
446
447#
Rich Felkerb439c052012-08-29 09:36:02 -0400448# Modern GCC wants to put DWARF tables (used for debugging and
449# unwinding) in the loaded part of the program where they are
450# unstrippable. These options force them back to debug sections (and
451# cause them not to get generated at all if debugging is off).
452#
453tryflag CFLAGS_AUTO -fno-unwind-tables
454tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
455
456#
Rich Felker27c1ecc2015-11-04 13:24:11 -0500457# Attempt to put each function and each data object in its own
458# section. This both allows additional size optimizations at link
459# time and works around a dangerous class of compiler/assembler bugs
460# whereby relative address expressions are constant-folded by the
461# assembler even when one or more of the symbols involved is
462# replaceable. See gas pr 18561 and gcc pr 66609, 68178, etc.
463#
464tryflag CFLAGS_AUTO -ffunction-sections
465tryflag CFLAGS_AUTO -fdata-sections
466
467#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400468# On x86, make sure we don't have incompatible instruction set
469# extensions enabled by default. This is bad for making static binaries.
470# We cheat and use i486 rather than i386 because i386 really does not
471# work anyway (issues with atomic ops).
Andre McCurdya6274a12015-04-21 10:34:05 -0700472# Some build environments pass -march and -mtune options via CC, so
473# check both CC and CFLAGS.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400474#
475if test "$ARCH" = "i386" ; then
Andre McCurdya6274a12015-04-21 10:34:05 -0700476fnmatch '-march=*|*\ -march=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
477fnmatch '-mtune=*|*\ -mtune=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400478fi
479
Rich Felker2384f272012-12-11 23:28:31 -0500480#
481# Even with -std=c99, gcc accepts some constructs which are constraint
482# violations. We want to treat these as errors regardless of whether
483# other purely stylistic warnings are enabled -- especially implicit
484# function declarations, which are a dangerous programming error.
485#
486tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
487tryflag CFLAGS_AUTO -Werror=implicit-int
488tryflag CFLAGS_AUTO -Werror=pointer-sign
489tryflag CFLAGS_AUTO -Werror=pointer-arith
490
Rich Felker64d2f8e2012-05-04 23:24:51 -0400491if test "x$warnings" = xyes ; then
492tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400493tryflag CFLAGS_AUTO -Wno-parentheses
494tryflag CFLAGS_AUTO -Wno-uninitialized
495tryflag CFLAGS_AUTO -Wno-missing-braces
496tryflag CFLAGS_AUTO -Wno-unused-value
497tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
498tryflag CFLAGS_AUTO -Wno-unknown-pragmas
rofl0radbeefb2014-01-08 03:11:46 +0100499tryflag CFLAGS_AUTO -Wno-pointer-to-int-cast
Rich Felker64d2f8e2012-05-04 23:24:51 -0400500fi
501
Rich Felker3aacb542015-04-22 22:11:48 -0400502if test "x$visibility" = xauto ; then
Rich Felkerde2b67f2015-04-19 22:05:29 -0400503# This test checks toolchain support for several things:
504# - the -include option
505# - the attributes/pragmas used in vis.h
506# - linking code that takes the address of protected symbols
Rich Felkerf3a53f02015-09-29 02:44:05 +0000507# - gcc 3.x bug that wrongly claims declarations mismatch
Rich Felkerde2b67f2015-04-19 22:05:29 -0400508printf "checking whether global visibility preinclude works... "
Rich Felkerf3a53f02015-09-29 02:44:05 +0000509cat > "$tmpc" <<EOF
510__attribute__((__visibility__("default")))
511extern struct a *const x;
512typedef struct a b;
513extern b *const x;
514b *const x;
515int (*fp)(void);
516int foo(void) { }
517int bar(void) { fp = foo; return foo(); }
518EOF
Rich Felker60ed9882015-04-20 18:14:19 -0400519if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
Rich Felkerce3e24e2016-01-20 19:43:37 +0000520 -DSHARED -fPIC -I$srcdir/src/internal -include vis.h \
Rich Felkerde2b67f2015-04-19 22:05:29 -0400521 -nostdlib -shared -Wl,-Bsymbolic-functions \
522 -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
523visibility=yes
524else
525visibility=no
526fi
527printf "%s\n" "$visibility"
528fi
529
Rich Felker3aacb542015-04-22 22:11:48 -0400530if test "x$visibility" = xyes ; then
Rich Felker428462a2015-04-22 01:41:00 -0400531CFLAGS_AUTO="$CFLAGS_AUTO -include vis.h"
Rich Felkerde2b67f2015-04-19 22:05:29 -0400532CFLAGS_AUTO="${CFLAGS_AUTO# }"
533fi
534
Rich Felker16191272016-01-25 19:57:38 -0500535# Determine if the compiler produces position-independent code (PIC)
536# by default. If so, we don't need to compile separate object files
537# for libc.a and libc.so.
538if trycppif __PIC__ "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
539pic_default=yes
540else
541pic_default=no
542fi
543
Rich Felker2efd38e2015-11-04 21:39:13 -0500544# Reduce space lost to padding for alignment purposes by sorting data
545# objects according to their alignment reqirements. This approximates
546# optimal packing.
547tryldflag LDFLAGS_AUTO -Wl,--sort-section,alignment
548tryldflag LDFLAGS_AUTO -Wl,--sort-common
549
Rich Felker6a851e32015-11-04 21:40:36 -0500550# When linking shared library, drop dummy weak definitions that were
551# replaced by strong definitions from other translation units.
552tryldflag LDFLAGS_AUTO -Wl,--gc-sections
553
Rich Felker0c5efde2012-06-06 22:00:08 -0400554# Some patched GCC builds have these defaults messed up...
Rich Felker2bd05a42012-08-25 17:13:28 -0400555tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400556
Rich Felker24623702015-09-22 15:45:40 +0000557# Prevent linking if there are undefined symbols; if any exist,
558# libc.so will crash at runtime during relocation processing.
559# The common way this can happen is failure to link the compiler
560# runtime library; implementation error is also a possibility.
561tryldflag LDFLAGS_AUTO -Wl,--no-undefined
562
Rich Felkerea1e2c52015-11-07 20:23:49 -0500563# Avoid exporting symbols from compiler runtime libraries. They
564# should be hidden anyway, but some toolchains including old gcc
565# versions built without shared library support and pcc are broken.
566tryldflag LDFLAGS_AUTO -Wl,--exclude-libs=ALL
567
Rich Felker65498f22016-01-31 00:40:33 -0500568# Linking with -Bsymbolic-functions is no longer mandatory for
569# the dynamic linker to work, but enable it if it works as
570# a linking optimization.
571tryldflag LDFLAGS_AUTO -Wl,-Bsymbolic-functions
Rich Felker498a1002012-06-07 00:32:22 -0400572
Rich Felker2c1cd232012-09-10 15:30:52 -0400573# Find compiler runtime library
574test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
575test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400576test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
577 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400578printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400579
Rich Felker3e7f1862013-07-18 20:30:58 -0400580# Figure out arch variants for archs with variants
581SUBARCH=
Rich Felker428462a2015-04-22 01:41:00 -0400582t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS"
Rich Felker3e7f1862013-07-18 20:30:58 -0400583
rofl0r8c820232014-03-19 22:31:00 +0100584if test "$ARCH" = "x86_64" ; then
585trycppif __ILP32__ "$t" && ARCH=x32
586fi
587
Rich Felker3e7f1862013-07-18 20:30:58 -0400588if test "$ARCH" = "arm" ; then
589trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
Rich Felker4918c2b2013-08-16 17:09:07 -0400590trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
Rich Felker3e7f1862013-07-18 20:30:58 -0400591fi
592
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000593if test "$ARCH" = "aarch64" ; then
594trycppif __AARCH64EB__ "$t" && SUBARCH=${SUBARCH}_be
595fi
596
Szabolcs Nagye5bb1652014-02-24 23:16:29 +0100597if test "$ARCH" = "mips" ; then
598trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
599trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
600fi
Rich Felker3e7f1862013-07-18 20:30:58 -0400601
602test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
603&& SUBARCH=${SUBARCH}el
604
Rich Felkerb1683a12014-02-27 23:18:42 -0500605if test "$ARCH" = "sh" ; then
Rich Felker79789982015-10-15 17:21:07 -0400606tryflag CFLAGS_AUTO -Wa,--isa=any
Rich Felkerb1683a12014-02-27 23:18:42 -0500607trycppif __BIG_ENDIAN__ "$t" && SUBARCH=${SUBARCH}eb
Bobby Bingham23d64182014-04-27 21:13:59 -0500608if trycppif "__SH_FPU_ANY__ || __SH4__" "$t" ; then
Rich Felkerb1683a12014-02-27 23:18:42 -0500609# Some sh configurations are broken and replace double with float
610# rather than using softfloat when the fpu is present but only
611# supports single precision. Reject them.
612printf "checking whether compiler's double type is IEEE double... "
Rich Felker946b9fa2014-02-27 23:55:04 -0500613echo 'typedef char dblcheck[(int)sizeof(double)-5];' > "$tmpc"
Rich Felkerb1683a12014-02-27 23:18:42 -0500614if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
615printf "yes\n"
616else
617printf "no\n"
618fail "$0: error: compiler's floating point configuration is unsupported"
619fi
620else
621SUBARCH=${SUBARCH}-nofpu
622fi
Rich Felkerd4c82d02015-09-12 03:22:19 +0000623if trycppif __SH_FDPIC__ "$t" ; then
624SUBARCH=${SUBARCH}-fdpic
Rich Felkerd4c82d02015-09-12 03:22:19 +0000625fi
Rich Felkerb1683a12014-02-27 23:18:42 -0500626fi
Bobby Bingham3a3c8132013-10-05 05:13:18 -0500627
Rich Felker3e7f1862013-07-18 20:30:58 -0400628test "$SUBARCH" \
629&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400630
Rich Felker90d77722013-08-11 03:27:35 -0400631case "$ARCH$SUBARCH" in
632arm) ASMSUBARCH=el ;;
633*) ASMSUBARCH=$SUBARCH ;;
634esac
635
Rich Felker86cc54b2013-08-02 19:34:22 -0400636#
637# Some archs (powerpc) have different possible long double formats
638# that the compiler can be configured for. The logic for whether this
639# is supported is in bits/float.h; in general, it is not. We need to
640# check for mismatches here or code in printf, strotd, and scanf will
641# be dangerously incorrect because it depends on (1) the macros being
642# correct, and (2) IEEE semantics.
643#
644printf "checking whether compiler's long double definition matches float.h... "
645echo '#include <float.h>' > "$tmpc"
646echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
647echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
648echo '#endif' >> "$tmpc"
Rich Felkerefdf04c2016-01-27 19:31:15 -0500649if $CC $CFLAGS_C99FSE \
650 -I$srcdir/arch/$ARCH -I$srcdir/arch/generic -I$srcdir/include \
651 $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker86cc54b2013-08-02 19:34:22 -0400652printf "yes\n"
653else
654printf "no\n"
655fail "$0: error: unsupported long double type"
656fi
657
Rich Felker64d2f8e2012-05-04 23:24:51 -0400658printf "creating config.mak... "
659
Rich Felker453f4622013-08-16 18:19:47 -0400660cmdline=$(quote "$0")
661for i ; do cmdline="$cmdline $(quote "$i")" ; done
662
Rich Felker64d2f8e2012-05-04 23:24:51 -0400663exec 3>&1 1>config.mak
664
665
666cat << EOF
Rich Felker453f4622013-08-16 18:19:47 -0400667# This version of config.mak was generated by:
668# $cmdline
Rich Felker64d2f8e2012-05-04 23:24:51 -0400669# Any changes made here will be lost if configure is re-run
670ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400671SUBARCH = $SUBARCH
Rich Felker90d77722013-08-11 03:27:35 -0400672ASMSUBARCH = $ASMSUBARCH
Petr Hosek2f853dd2015-11-18 12:07:32 -0800673srcdir = $srcdir
Rich Felker64d2f8e2012-05-04 23:24:51 -0400674prefix = $prefix
675exec_prefix = $exec_prefix
676bindir = $bindir
677libdir = $libdir
678includedir = $includedir
679syslibdir = $syslibdir
680CC = $CC
Rich Felker4cd8b472015-11-02 16:58:14 -0500681CFLAGS = $CFLAGS
682CFLAGS_AUTO = $CFLAGS_AUTO
Rich Felker64d2f8e2012-05-04 23:24:51 -0400683CFLAGS_C99FSE = $CFLAGS_C99FSE
Rich Felker4a1f55e2013-08-01 17:12:23 -0400684CFLAGS_MEMOPS = $CFLAGS_MEMOPS
Rich Felker1ef849c2015-04-13 20:13:10 -0400685CFLAGS_NOSSP = $CFLAGS_NOSSP
Rich Felker64d2f8e2012-05-04 23:24:51 -0400686CPPFLAGS = $CPPFLAGS
Rich Felker4cd8b472015-11-02 16:58:14 -0500687LDFLAGS = $LDFLAGS
688LDFLAGS_AUTO = $LDFLAGS_AUTO
Rich Felker94e920d2012-08-14 22:50:16 -0400689CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400690LIBCC = $LIBCC
Rich Felkera80847d2013-07-22 21:22:04 -0400691OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
Shizb3cd7d12015-06-28 23:08:19 +0200692ALL_TOOLS = $tools
693TOOL_LIBS = $tool_libs
Alex Dowad35b33122015-07-10 15:03:24 +0200694ADD_CFI = $ADD_CFI
Rich Felker64d2f8e2012-05-04 23:24:51 -0400695EOF
696test "x$static" = xno && echo "STATIC_LIBS ="
697test "x$shared" = xno && echo "SHARED_LIBS ="
Shizb3cd7d12015-06-28 23:08:19 +0200698test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
Shizfb585452015-06-28 23:08:21 +0200699test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
Rich Felker16191272016-01-25 19:57:38 -0500700test "x$pic_default" = xyes && echo 'AOBJS = $(LOBJS)'
Rich Felker64d2f8e2012-05-04 23:24:51 -0400701exec 1>&3 3>&-
702
Petr Hosek2f853dd2015-11-18 12:07:32 -0800703test "$srcdir" = "." || ln -sf $srcdir/Makefile .
704
Rich Felker64d2f8e2012-05-04 23:24:51 -0400705printf "done\n"