blob: c2db298ce95f71afce00efae02e8f164ec47271b [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 Felker2d49c222016-04-29 19:50:33 -040028 --build=BUILD build system type; used only to infer cross-compiling
Rich Felker64d2f8e2012-05-04 23:24:51 -040029
30Optional features:
Rich Felkera80847d2013-07-22 21:22:04 -040031 --enable-optimize=... optimize listed components for speed over size [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040032 --enable-debug build with debugging information [disabled]
33 --enable-warnings build with recommended warnings flags [disabled]
Rich Felkerde2b67f2015-04-19 22:05:29 -040034 --enable-visibility use global visibility options to optimize PIC [auto]
Shizb3cd7d12015-06-28 23:08:19 +020035 --enable-wrapper=... build given musl toolchain wrapper [auto]
Rich Felker64d2f8e2012-05-04 23:24:51 -040036 --disable-shared inhibit building shared library [enabled]
37 --disable-static inhibit building static library [enabled]
38
39Some influential environment variables:
40 CC C compiler command [detected]
41 CFLAGS C compiler flags [-Os -pipe ...]
Rich Felker94e920d2012-08-14 22:50:16 -040042 CROSS_COMPILE prefix for cross compiler and tools [none]
Rich Felker2c1cd232012-09-10 15:30:52 -040043 LIBCC compiler runtime library [detected]
Rich Felker64d2f8e2012-05-04 23:24:51 -040044
45Use these variables to override the choices made by configure.
46
47EOF
48exit 0
49}
50
51# Helper functions
52
Rich Felker453f4622013-08-16 18:19:47 -040053quote () {
54tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
55$1
56EOF
Rich Felkere4499742013-08-20 13:51:46 -040057printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
Rich Felker453f4622013-08-16 18:19:47 -040058}
Rich Felker64d2f8e2012-05-04 23:24:51 -040059echo () { printf "%s\n" "$*" ; }
60fail () { echo "$*" ; exit 1 ; }
61fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
62cmdexists () { type "$1" >/dev/null 2>&1 ; }
63trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
64
Rich Felker3d9e3a32012-11-08 17:20:50 -050065stripdir () {
66while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
Rich Felker64d2f8e2012-05-04 23:24:51 -040067}
68
Rich Felker3e7f1862013-07-18 20:30:58 -040069trycppif () {
70printf "checking preprocessor condition %s... " "$1"
Rich Felkerdf065782013-07-18 20:37:19 -040071echo "typedef int x;" > "$tmpc"
72echo "#if $1" >> "$tmpc"
Rich Felker3e7f1862013-07-18 20:30:58 -040073echo "#error yes" >> "$tmpc"
74echo "#endif" >> "$tmpc"
75if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
76printf "false\n"
77return 1
78else
79printf "true\n"
80return 0
81fi
82}
83
Rich Felker64d2f8e2012-05-04 23:24:51 -040084tryflag () {
85printf "checking whether compiler accepts %s... " "$2"
86echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +020087if $CC $CFLAGS_TRY $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker64d2f8e2012-05-04 23:24:51 -040088printf "yes\n"
89eval "$1=\"\${$1} \$2\""
90eval "$1=\${$1# }"
91return 0
92else
93printf "no\n"
94return 1
95fi
96}
97
Rich Felker08f70a32012-06-06 20:45:52 -040098tryldflag () {
99printf "checking whether linker accepts %s... " "$2"
Rich Felker67a03832012-06-07 00:23:58 -0400100echo "typedef int x;" > "$tmpc"
Shizfc431d32015-05-28 05:52:22 +0200101if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker08f70a32012-06-06 20:45:52 -0400102printf "yes\n"
103eval "$1=\"\${$1} \$2\""
104eval "$1=\${$1# }"
105return 0
106else
107printf "no\n"
108return 1
109fi
110}
111
Rich Felker64d2f8e2012-05-04 23:24:51 -0400112
113
114# Beginning of actual script
115
Rich Felker08f70a32012-06-06 20:45:52 -0400116CFLAGS_C99FSE=
117CFLAGS_AUTO=
Rich Felker4a1f55e2013-08-01 17:12:23 -0400118CFLAGS_MEMOPS=
Rich Felker1ef849c2015-04-13 20:13:10 -0400119CFLAGS_NOSSP=
Shizfc431d32015-05-28 05:52:22 +0200120CFLAGS_TRY=
Rich Felker08f70a32012-06-06 20:45:52 -0400121LDFLAGS_AUTO=
Shizfc431d32015-05-28 05:52:22 +0200122LDFLAGS_TRY=
Rich Felkera80847d2013-07-22 21:22:04 -0400123OPTIMIZE_GLOBS=
Petr Hosek2f853dd2015-11-18 12:07:32 -0800124srcdir=
Rich Felker3d9e3a32012-11-08 17:20:50 -0500125prefix=/usr/local/musl
126exec_prefix='$(prefix)'
127bindir='$(exec_prefix)/bin'
128libdir='$(prefix)/lib'
129includedir='$(prefix)/include'
130syslibdir='/lib'
Shizb3cd7d12015-06-28 23:08:19 +0200131tools=
132tool_libs=
Rich Felker2d49c222016-04-29 19:50:33 -0400133build=
Rich Felker278883d2012-06-03 16:22:13 -0400134target=
Rich Felkera80847d2013-07-22 21:22:04 -0400135optimize=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400136debug=no
Rich Felker3d9e3a32012-11-08 17:20:50 -0500137warnings=no
Rich Felkerde2b67f2015-04-19 22:05:29 -0400138visibility=auto
Rich Felkerd79b2772014-06-10 12:11:12 -0400139shared=auto
Rich Felker64d2f8e2012-05-04 23:24:51 -0400140static=yes
Rich Felker9ca4dae2014-05-19 10:33:28 -0400141wrapper=auto
Shizb3cd7d12015-06-28 23:08:19 +0200142gcc_wrapper=no
Shizfb585452015-06-28 23:08:21 +0200143clang_wrapper=no
Rich Felker64d2f8e2012-05-04 23:24:51 -0400144
145for arg ; do
146case "$arg" in
Rich Felker47314f12016-02-02 21:14:09 -0500147--help|-h) usage ;;
Petr Hosek2f853dd2015-11-18 12:07:32 -0800148--srcdir=*) srcdir=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400149--prefix=*) prefix=${arg#*=} ;;
150--exec-prefix=*) exec_prefix=${arg#*=} ;;
151--bindir=*) bindir=${arg#*=} ;;
152--libdir=*) libdir=${arg#*=} ;;
153--includedir=*) includedir=${arg#*=} ;;
154--syslibdir=*) syslibdir=${arg#*=} ;;
155--enable-shared|--enable-shared=yes) shared=yes ;;
156--disable-shared|--enable-shared=no) shared=no ;;
157--enable-static|--enable-static=yes) static=yes ;;
158--disable-static|--enable-static=no) static=no ;;
Rich Felkera80847d2013-07-22 21:22:04 -0400159--enable-optimize) optimize=yes ;;
160--enable-optimize=*) optimize=${arg#*=} ;;
161--disable-optimize) optimize=no ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400162--enable-debug|--enable-debug=yes) debug=yes ;;
163--disable-debug|--enable-debug=no) debug=no ;;
164--enable-warnings|--enable-warnings=yes) warnings=yes ;;
165--disable-warnings|--enable-warnings=no) warnings=no ;;
Rich Felkerde2b67f2015-04-19 22:05:29 -0400166--enable-visibility|--enable-visibility=yes) visibility=yes ;;
167--disable-visibility|--enable-visibility=no) visibility=no ;;
Shizb3cd7d12015-06-28 23:08:19 +0200168--enable-wrapper|--enable-wrapper=yes) wrapper=detect ;;
Shizfb585452015-06-28 23:08:21 +0200169--enable-wrapper=all) wrapper=yes ; gcc_wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200170--enable-wrapper=gcc) wrapper=yes ; gcc_wrapper=yes ;;
Shizfb585452015-06-28 23:08:21 +0200171--enable-wrapper=clang) wrapper=yes ; clang_wrapper=yes ;;
Shizb3cd7d12015-06-28 23:08:19 +0200172--disable-wrapper|--enable-wrapper=no) wrapper=no ;;
173--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ; gcc_wrapper=yes ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400174--disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
LeMay, Michael6bc7d9c2016-05-04 03:29:42 +0000175--enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;;
Rich Felker278883d2012-06-03 16:22:13 -0400176--host=*|--target=*) target=${arg#*=} ;;
Rich Felker2d49c222016-04-29 19:50:33 -0400177--build=*) build=${arg#*=} ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400178-* ) echo "$0: unknown option $arg" ;;
179CC=*) CC=${arg#*=} ;;
180CFLAGS=*) CFLAGS=${arg#*=} ;;
181CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
182LDFLAGS=*) LDFLAGS=${arg#*=} ;;
Rich Felker94e920d2012-08-14 22:50:16 -0400183CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
Rich Felker2c1cd232012-09-10 15:30:52 -0400184LIBCC=*) LIBCC=${arg#*=} ;;
Rich Felker278883d2012-06-03 16:22:13 -0400185*=*) ;;
Rich Felker2d49c222016-04-29 19:50:33 -0400186*) build=$arg ; target=$arg ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400187esac
188done
189
Petr Hosek2f853dd2015-11-18 12:07:32 -0800190for i in srcdir prefix exec_prefix bindir libdir includedir syslibdir ; do
Rich Felker3d9e3a32012-11-08 17:20:50 -0500191stripdir $i
192done
Rich Felker64d2f8e2012-05-04 23:24:51 -0400193
194#
Petr Hosek2f853dd2015-11-18 12:07:32 -0800195# Get the source dir for out-of-tree builds
196#
197if test -z "$srcdir" ; then
198srcdir="${0%/configure}"
199stripdir srcdir
200fi
201abs_builddir="$(pwd)" || fail "$0: cannot determine working directory"
202abs_srcdir="$(cd $srcdir && pwd)" || fail "$0: invalid source directory $srcdir"
203test "$abs_srcdir" = "$abs_builddir" && srcdir=.
204test "$srcdir" != "." -a -f Makefile -a ! -h Makefile && fail "$0: Makefile already exists in the working directory"
205
206#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400207# Get a temp filename we can use
208#
209i=0
210set -C
211while : ; do i=$(($i+1))
212tmpc="./conf$$-$PPID-$i.c"
Rich Felker6c0cba82012-11-18 23:15:47 -05002132>|/dev/null > "$tmpc" && break
Rich Felker64d2f8e2012-05-04 23:24:51 -0400214test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
215done
216set +C
217trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
218
219#
Rich Felker2d49c222016-04-29 19:50:33 -0400220# Check whether we are cross-compiling, and set a default
221# CROSS_COMPILE prefix if none was provided.
222#
223test "$target" && \
224test "$target" != "$build" && \
225test -z "$CROSS_COMPILE" && \
226CROSS_COMPILE="$target-"
227
228#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400229# Find a C compiler to use
230#
231printf "checking for C compiler... "
Rich Felker94e920d2012-08-14 22:50:16 -0400232trycc ${CROSS_COMPILE}gcc
233trycc ${CROSS_COMPILE}c99
234trycc ${CROSS_COMPILE}cc
Rich Felker64d2f8e2012-05-04 23:24:51 -0400235printf "%s\n" "$CC"
236test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
237
Rich Felker89456672014-05-12 14:22:57 -0400238printf "checking whether C compiler works... "
239echo "typedef int x;" > "$tmpc"
240if output=$($CC $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" 2>&1) ; then
241printf "yes\n"
242else
243printf "no; compiler output follows:\n%s\n" "$output"
244exit 1
245fi
246
Rich Felker64d2f8e2012-05-04 23:24:51 -0400247#
Shizfc431d32015-05-28 05:52:22 +0200248# Figure out options to force errors on unknown flags.
249#
250tryflag CFLAGS_TRY -Werror=unknown-warning-option
251tryflag CFLAGS_TRY -Werror=unused-command-line-argument
252tryldflag LDFLAGS_TRY -Werror=unknown-warning-option
253tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
254
255#
Shizb3cd7d12015-06-28 23:08:19 +0200256# Need to know if the compiler is gcc or clang to decide which toolchain
257# wrappers to build.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400258#
Shizb3cd7d12015-06-28 23:08:19 +0200259printf "checking for C compiler family... "
260cc_ver="$(LC_ALL=C $CC -v 2>&1)"
261cc_family=unknown
262if fnmatch '*gcc\ version*' "$cc_ver" ; then
263cc_family=gcc
Shizfb585452015-06-28 23:08:21 +0200264elif fnmatch '*clang\ version*' "$cc_ver" ; then
265cc_family=clang
Rich Felker9ca4dae2014-05-19 10:33:28 -0400266fi
Shizb3cd7d12015-06-28 23:08:19 +0200267echo "$cc_family"
Rich Felker9ca4dae2014-05-19 10:33:28 -0400268
269#
Shizb3cd7d12015-06-28 23:08:19 +0200270# Figure out toolchain wrapper to build
Rich Felker9ca4dae2014-05-19 10:33:28 -0400271#
Shizb3cd7d12015-06-28 23:08:19 +0200272if test "$wrapper" = auto -o "$wrapper" = detect ; then
Shizf8db6f72015-06-28 23:08:20 +0200273echo "#include <stdlib.h>" > "$tmpc"
274echo "#if ! __GLIBC__" >> "$tmpc"
275echo "#error no" >> "$tmpc"
276echo "#endif" >> "$tmpc"
Shizb3cd7d12015-06-28 23:08:19 +0200277printf "checking for toolchain wrapper to build... "
Shizf8db6f72015-06-28 23:08:20 +0200278if test "$wrapper" = auto && ! $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
279echo "none"
280elif test "$cc_family" = gcc ; then
Shizb3cd7d12015-06-28 23:08:19 +0200281gcc_wrapper=yes
Shizf8db6f72015-06-28 23:08:20 +0200282echo "gcc"
Shizfb585452015-06-28 23:08:21 +0200283elif test "$cc_family" = clang ; then
284clang_wrapper=yes
285echo "clang"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400286else
Shizb3cd7d12015-06-28 23:08:19 +0200287echo "none"
288if test "$wrapper" = detect ; then
289fail "$0: could not find an appropriate toolchain wrapper"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400290fi
Shizb3cd7d12015-06-28 23:08:19 +0200291fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400292fi
293
Shizb3cd7d12015-06-28 23:08:19 +0200294if test "$gcc_wrapper" = yes ; then
Petr Hosek2f853dd2015-11-18 12:07:32 -0800295tools="$tools obj/musl-gcc"
Shizb3cd7d12015-06-28 23:08:19 +0200296tool_libs="$tool_libs lib/musl-gcc.specs"
297fi
Shizfb585452015-06-28 23:08:21 +0200298if test "$clang_wrapper" = yes ; then
Petr Hosek2f853dd2015-11-18 12:07:32 -0800299tools="$tools obj/musl-clang obj/ld.musl-clang"
Shizfb585452015-06-28 23:08:21 +0200300fi
Rich Felker64d2f8e2012-05-04 23:24:51 -0400301
302#
Rich Felker278883d2012-06-03 16:22:13 -0400303# Find the target architecture
Rich Felker64d2f8e2012-05-04 23:24:51 -0400304#
Rich Felker278883d2012-06-03 16:22:13 -0400305printf "checking target system type... "
Rich Felker01e5a1b2012-10-18 23:02:53 -0400306test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
Rich Felker278883d2012-06-03 16:22:13 -0400307printf "%s\n" "$target"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400308
309#
310# Convert to just ARCH
311#
Rich Felker278883d2012-06-03 16:22:13 -0400312case "$target" in
Rich Felker0b8f0c52014-02-28 13:12:40 -0500313# Catch these early to simplify matching for 32-bit archs
Rich Felker64d2f8e2012-05-04 23:24:51 -0400314arm*) ARCH=arm ;;
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000315aarch64*) ARCH=aarch64 ;;
Rich Felker4c101e12016-02-19 00:10:23 -0500316i?86-nt32*) ARCH=nt32 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400317i?86*) ARCH=i386 ;;
Rich Felkerf162c062014-03-17 17:38:22 -0400318x86_64-x32*|x32*|x86_64*x32) ARCH=x32 ;;
Rich Felker4c101e12016-02-19 00:10:23 -0500319x86_64-nt64*) ARCH=nt64 ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400320x86_64*) ARCH=x86_64 ;;
Szabolcs Nagyfff88012016-08-24 12:40:10 +0200321mips64*|mipsisa64*) ARCH=mips64 ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500322mips*) ARCH=mips ;;
323microblaze*) ARCH=microblaze ;;
Stefan Kristiansson200d1542014-07-17 22:09:10 +0300324or1k*) ARCH=or1k ;;
Bobby Binghamc0ede9e2016-04-30 19:18:17 -0500325powerpc64*) ARCH=powerpc64 ;;
Rich Felker0b8f0c52014-02-28 13:12:40 -0500326powerpc*) ARCH=powerpc ;;
327sh[1-9bel-]*|sh|superh*) ARCH=sh ;;
Bobby Bingham15094942016-11-11 21:52:05 -0600328s390x*) ARCH=s390x ;;
Rich Felker278883d2012-06-03 16:22:13 -0400329unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
330*) fail "$0: unknown or unsupported target \"$target\"" ;;
Rich Felker64d2f8e2012-05-04 23:24:51 -0400331esac
332
333#
334# Try to get a conforming C99 freestanding environment
335#
336tryflag CFLAGS_C99FSE -std=c99
337tryflag CFLAGS_C99FSE -nostdinc
338tryflag CFLAGS_C99FSE -ffreestanding \
339|| tryflag CFLAGS_C99FSE -fno-builtin
340tryflag CFLAGS_C99FSE -fexcess-precision=standard \
Rich Felker2121b8a2012-07-03 23:53:05 -0400341|| { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
Rich Felkerb4ccc3c2012-05-05 17:18:31 -0400342tryflag CFLAGS_C99FSE -frounding-math
Rich Felker64d2f8e2012-05-04 23:24:51 -0400343
Rich Felker4a1f55e2013-08-01 17:12:23 -0400344#
Rich Felker06ceee82013-08-27 17:33:47 -0400345# We may use the may_alias attribute if __GNUC__ is defined, so
346# if the compiler defines __GNUC__ but does not provide it,
347# it must be defined away as part of the CFLAGS.
348#
349printf "checking whether compiler needs attribute((may_alias)) suppression... "
350cat > "$tmpc" <<EOF
351typedef int
352#ifdef __GNUC__
353__attribute__((__may_alias__))
354#endif
355x;
356EOF
Rich Felkere1d99892016-01-27 19:01:21 -0500357if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
Rich Felker06ceee82013-08-27 17:33:47 -0400358 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
359printf "no\n"
360else
361printf "yes\n"
362CFLAGS_C99FSE="$CFLAGS_C99FSE -D__may_alias__="
363fi
364
365#
Rich Felkerbc0c4842015-10-23 00:01:01 -0400366# The GNU toolchain defaults to assuming unmarked files need an
367# executable stack, potentially exposing vulnerabilities in programs
368# linked with such object files. Fix this.
369#
370tryflag CFLAGS_C99FSE -Wa,--noexecstack
371
372#
Rich Felker1ef849c2015-04-13 20:13:10 -0400373# Check for options to disable stack protector, which needs to be
374# disabled for a few early-bootstrap translation units. If not found,
375# this is not an error; we assume the toolchain does not do ssp.
376#
377tryflag CFLAGS_NOSSP -fno-stack-protector
378
379#
Rich Felker4a1f55e2013-08-01 17:12:23 -0400380# Check for options that may be needed to prevent the compiler from
381# generating self-referential versions of memcpy,, memmove, memcmp,
382# and memset. Really, we should add a check to determine if this
383# option is sufficient, and if not, add a macro to cripple these
384# functions with volatile...
385#
386tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
Rich Felkera80847d2013-07-22 21:22:04 -0400387
Rich Felker64d2f8e2012-05-04 23:24:51 -0400388#
Rich Felker4ad35882014-06-20 16:10:48 -0400389# Enable debugging if requessted.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400390#
Rich Felker4ad35882014-06-20 16:10:48 -0400391test "$debug" = yes && CFLAGS_AUTO=-g
Rich Felkera80847d2013-07-22 21:22:04 -0400392
393#
Alex Dowad35b33122015-07-10 15:03:24 +0200394# Preprocess asm files to add extra debugging information if debug is
395# enabled, our assembler supports the needed directives, and the
396# preprocessing script has been written for our architecture.
397#
398printf "checking whether we should preprocess assembly to add debugging information... "
399if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" &&
400 test -f "tools/add-cfi.$ARCH.awk" &&
401 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 -
402then
403 ADD_CFI=yes
404else
405 ADD_CFI=no
406fi
407printf "%s\n" "$ADD_CFI"
408
409#
Rich Felkera80847d2013-07-22 21:22:04 -0400410# Possibly add a -O option to CFLAGS and select modules to optimize with
411# -O3 based on the status of --enable-optimize and provided CFLAGS.
412#
413printf "checking for optimization settings... "
414case "x$optimize" in
415xauto)
416if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
417printf "using provided CFLAGS\n" ;optimize=no
418else
419printf "using defaults\n" ; optimize=yes
420fi
421;;
422xsize|xnone) printf "minimize size\n" ; optimize=size ;;
423xno|x) printf "disabled\n" ; optimize=no ;;
424*) printf "custom\n" ;;
425esac
426
427test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
Rich Felker43d25312013-07-24 23:21:45 -0400428test "$optimize" = yes && optimize="internal,malloc,string"
Rich Felkera80847d2013-07-22 21:22:04 -0400429
430if fnmatch 'no|size' "$optimize" ; then :
431else
432printf "components to be optimized for speed:"
433while test "$optimize" ; do
434case "$optimize" in
435*,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
436*) this=$optimize optimize=
437esac
438printf " $this"
439case "$this" in
440*/*.c) ;;
441*/*) this=$this*.c ;;
442*) this=$this/*.c ;;
443esac
444OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
445done
446OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
447printf "\n"
448fi
449
450# Always try -pipe
Rich Felker64d2f8e2012-05-04 23:24:51 -0400451tryflag CFLAGS_AUTO -pipe
452
453#
Rich Felkerb439c052012-08-29 09:36:02 -0400454# If debugging is disabled, omit frame pointer. Modern GCC does this
455# anyway on most archs even when debugging is enabled since the frame
456# pointer is no longer needed for debugging.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400457#
458if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
459else
Rich Felker64d2f8e2012-05-04 23:24:51 -0400460tryflag CFLAGS_AUTO -fomit-frame-pointer
461fi
462
463#
Rich Felkerb439c052012-08-29 09:36:02 -0400464# Modern GCC wants to put DWARF tables (used for debugging and
465# unwinding) in the loaded part of the program where they are
466# unstrippable. These options force them back to debug sections (and
467# cause them not to get generated at all if debugging is off).
468#
469tryflag CFLAGS_AUTO -fno-unwind-tables
470tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
471
472#
Rich Felker27c1ecc2015-11-04 13:24:11 -0500473# Attempt to put each function and each data object in its own
474# section. This both allows additional size optimizations at link
475# time and works around a dangerous class of compiler/assembler bugs
476# whereby relative address expressions are constant-folded by the
477# assembler even when one or more of the symbols involved is
478# replaceable. See gas pr 18561 and gcc pr 66609, 68178, etc.
479#
480tryflag CFLAGS_AUTO -ffunction-sections
481tryflag CFLAGS_AUTO -fdata-sections
482
483#
Rich Felker64d2f8e2012-05-04 23:24:51 -0400484# On x86, make sure we don't have incompatible instruction set
485# extensions enabled by default. This is bad for making static binaries.
486# We cheat and use i486 rather than i386 because i386 really does not
487# work anyway (issues with atomic ops).
Andre McCurdya6274a12015-04-21 10:34:05 -0700488# Some build environments pass -march and -mtune options via CC, so
489# check both CC and CFLAGS.
Rich Felker64d2f8e2012-05-04 23:24:51 -0400490#
491if test "$ARCH" = "i386" ; then
Andre McCurdya6274a12015-04-21 10:34:05 -0700492fnmatch '-march=*|*\ -march=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
493fnmatch '-mtune=*|*\ -mtune=*' "$CC $CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
Rich Felker64d2f8e2012-05-04 23:24:51 -0400494fi
495
Rich Felker2384f272012-12-11 23:28:31 -0500496#
497# Even with -std=c99, gcc accepts some constructs which are constraint
498# violations. We want to treat these as errors regardless of whether
499# other purely stylistic warnings are enabled -- especially implicit
500# function declarations, which are a dangerous programming error.
501#
502tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
503tryflag CFLAGS_AUTO -Werror=implicit-int
504tryflag CFLAGS_AUTO -Werror=pointer-sign
505tryflag CFLAGS_AUTO -Werror=pointer-arith
506
Rich Felker64d2f8e2012-05-04 23:24:51 -0400507if test "x$warnings" = xyes ; then
508tryflag CFLAGS_AUTO -Wall
Rich Felker64d2f8e2012-05-04 23:24:51 -0400509tryflag CFLAGS_AUTO -Wno-parentheses
510tryflag CFLAGS_AUTO -Wno-uninitialized
511tryflag CFLAGS_AUTO -Wno-missing-braces
512tryflag CFLAGS_AUTO -Wno-unused-value
513tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
514tryflag CFLAGS_AUTO -Wno-unknown-pragmas
rofl0radbeefb2014-01-08 03:11:46 +0100515tryflag CFLAGS_AUTO -Wno-pointer-to-int-cast
Rich Felker64d2f8e2012-05-04 23:24:51 -0400516fi
517
Rich Felker3aacb542015-04-22 22:11:48 -0400518if test "x$visibility" = xauto ; then
Rich Felkerde2b67f2015-04-19 22:05:29 -0400519# This test checks toolchain support for several things:
520# - the -include option
521# - the attributes/pragmas used in vis.h
522# - linking code that takes the address of protected symbols
Rich Felkerf3a53f02015-09-29 02:44:05 +0000523# - gcc 3.x bug that wrongly claims declarations mismatch
Rich Felkerde2b67f2015-04-19 22:05:29 -0400524printf "checking whether global visibility preinclude works... "
Rich Felkerf3a53f02015-09-29 02:44:05 +0000525cat > "$tmpc" <<EOF
526__attribute__((__visibility__("default")))
527extern struct a *const x;
528typedef struct a b;
529extern b *const x;
530b *const x;
531int (*fp)(void);
532int foo(void) { }
533int bar(void) { fp = foo; return foo(); }
534EOF
Rich Felker60ed9882015-04-20 18:14:19 -0400535if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS \
Rich Felkerce3e24e2016-01-20 19:43:37 +0000536 -DSHARED -fPIC -I$srcdir/src/internal -include vis.h \
Rich Felkerde2b67f2015-04-19 22:05:29 -0400537 -nostdlib -shared -Wl,-Bsymbolic-functions \
538 -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
539visibility=yes
540else
541visibility=no
542fi
543printf "%s\n" "$visibility"
544fi
545
Rich Felker3aacb542015-04-22 22:11:48 -0400546if test "x$visibility" = xyes ; then
Rich Felker428462a2015-04-22 01:41:00 -0400547CFLAGS_AUTO="$CFLAGS_AUTO -include vis.h"
Rich Felkerde2b67f2015-04-19 22:05:29 -0400548CFLAGS_AUTO="${CFLAGS_AUTO# }"
549fi
550
Rich Felker16191272016-01-25 19:57:38 -0500551# Determine if the compiler produces position-independent code (PIC)
552# by default. If so, we don't need to compile separate object files
553# for libc.a and libc.so.
554if trycppif __PIC__ "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
555pic_default=yes
556else
557pic_default=no
558fi
559
Rich Felker2efd38e2015-11-04 21:39:13 -0500560# Reduce space lost to padding for alignment purposes by sorting data
561# objects according to their alignment reqirements. This approximates
562# optimal packing.
563tryldflag LDFLAGS_AUTO -Wl,--sort-section,alignment
564tryldflag LDFLAGS_AUTO -Wl,--sort-common
565
Rich Felker6a851e32015-11-04 21:40:36 -0500566# When linking shared library, drop dummy weak definitions that were
567# replaced by strong definitions from other translation units.
568tryldflag LDFLAGS_AUTO -Wl,--gc-sections
569
Rich Felker0c5efde2012-06-06 22:00:08 -0400570# Some patched GCC builds have these defaults messed up...
Rich Felker2bd05a42012-08-25 17:13:28 -0400571tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
Rich Felker08f70a32012-06-06 20:45:52 -0400572
Rich Felker24623702015-09-22 15:45:40 +0000573# Prevent linking if there are undefined symbols; if any exist,
574# libc.so will crash at runtime during relocation processing.
575# The common way this can happen is failure to link the compiler
576# runtime library; implementation error is also a possibility.
577tryldflag LDFLAGS_AUTO -Wl,--no-undefined
578
Rich Felkerea1e2c52015-11-07 20:23:49 -0500579# Avoid exporting symbols from compiler runtime libraries. They
580# should be hidden anyway, but some toolchains including old gcc
581# versions built without shared library support and pcc are broken.
582tryldflag LDFLAGS_AUTO -Wl,--exclude-libs=ALL
583
Rich Felker65498f22016-01-31 00:40:33 -0500584# Linking with -Bsymbolic-functions is no longer mandatory for
585# the dynamic linker to work, but enable it if it works as
586# a linking optimization.
587tryldflag LDFLAGS_AUTO -Wl,-Bsymbolic-functions
Rich Felker498a1002012-06-07 00:32:22 -0400588
Rich Felker2c1cd232012-09-10 15:30:52 -0400589# Find compiler runtime library
590test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
591test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
Rich Felkercd31a1f2012-10-26 18:15:51 -0400592test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
593 && tryldflag LIBCC "$try_libcc"
Rich Felker2c1cd232012-09-10 15:30:52 -0400594printf "using compiler runtime libraries: %s\n" "$LIBCC"
Rich Felkera1546e82012-07-12 14:24:10 -0400595
Rich Felker3e7f1862013-07-18 20:30:58 -0400596# Figure out arch variants for archs with variants
597SUBARCH=
Rich Felker428462a2015-04-22 01:41:00 -0400598t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS"
Rich Felker3e7f1862013-07-18 20:30:58 -0400599
rofl0r8c820232014-03-19 22:31:00 +0100600if test "$ARCH" = "x86_64" ; then
601trycppif __ILP32__ "$t" && ARCH=x32
602fi
603
Rich Felker3e7f1862013-07-18 20:30:58 -0400604if test "$ARCH" = "arm" ; then
Rich Felker088c9672016-12-19 21:53:33 -0500605if trycppif __thumb2__ "$t" ; then
606tryflag CFLAGS_AUTO -Wa,-mimplicit-it=always
607tryflag CFLAGS_AUTO -Wa,-mthumb
608fi
Rich Felker3e7f1862013-07-18 20:30:58 -0400609trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
Rich Felker4918c2b2013-08-16 17:09:07 -0400610trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
Rich Felker71c334f2016-02-19 01:20:07 +0000611# Versions of clang up until at least 3.8 have the wrong constraint codes
612# for floating point operands to inline asm. Detect this so the affected
613# source files can just disable the asm.
614if test "$cc_family" = clang ; then
615printf "checking whether clang's vfp asm constraints work... "
616echo 'float f(float x) { __asm__("":"+t"(x)); return x; }' > "$tmpc"
617if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
618printf "yes\n"
619else
620printf "no\n"
621CFLAGS_AUTO="$CFLAGS_AUTO -DBROKEN_VFP_ASM"
622CFLAGS_AUTO="${CFLAGS_AUTO# }"
623fi
624fi
Rich Felker3e7f1862013-07-18 20:30:58 -0400625fi
626
Szabolcs Nagy01ef3dd2015-03-10 21:18:41 +0000627if test "$ARCH" = "aarch64" ; then
628trycppif __AARCH64EB__ "$t" && SUBARCH=${SUBARCH}_be
629fi
630
Szabolcs Nagye5bb1652014-02-24 23:16:29 +0100631if test "$ARCH" = "mips" ; then
Rich Felker6d99ad92016-04-03 10:42:37 +0000632trycppif "__mips_isa_rev >= 6" "$t" && SUBARCH=${SUBARCH}r6
Szabolcs Nagye5bb1652014-02-24 23:16:29 +0100633trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
634trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
635fi
Rich Felker3e7f1862013-07-18 20:30:58 -0400636
Rich Felker83933572016-03-06 17:41:56 +0000637if test "$ARCH" = "mips64" ; then
Rich Felker5972c4a2016-04-18 05:19:13 +0000638trycppif "_MIPS_SIM != _ABI64" "$t" && ARCH=mipsn32
Rich Felker6d99ad92016-04-03 10:42:37 +0000639trycppif "__mips_isa_rev >= 6" "$t" && SUBARCH=${SUBARCH}r6
Rich Felker83933572016-03-06 17:41:56 +0000640trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" && SUBARCH=${SUBARCH}el
641trycppif __mips_soft_float "$t" && SUBARCH=${SUBARCH}-sf
642fi
643
Felix Fietkau5a92dd92016-01-25 13:20:52 +0100644if test "$ARCH" = "powerpc" ; then
Rich Felker636a4792016-03-06 17:11:29 -0500645trycppif "__NO_FPRS__ && !_SOFT_FLOAT" "$t" && fail \
646 "$0: error: compiler's floating point configuration is unsupported"
Felix Fietkau5a92dd92016-01-25 13:20:52 +0100647trycppif _SOFT_FLOAT "$t" && SUBARCH=${SUBARCH}-sf
648fi
649
Rich Felker3e7f1862013-07-18 20:30:58 -0400650test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
651&& SUBARCH=${SUBARCH}el
652
Bobby Binghamc0ede9e2016-04-30 19:18:17 -0500653if test "$ARCH" = "powerpc64" ; then
654trycppif "_CALL_ELF == 2" "$t" || fail "$0: error: unsupported powerpc64 ABI"
655trycppif __LITTLE_ENDIAN__ "$t" && SUBARCH=${SUBARCH}le
656trycppif _SOFT_FLOAT "$t" && fail "$0: error: soft-float not supported on powerpc64"
657fi
658
Rich Felkerb1683a12014-02-27 23:18:42 -0500659if test "$ARCH" = "sh" ; then
Rich Felker79789982015-10-15 17:21:07 -0400660tryflag CFLAGS_AUTO -Wa,--isa=any
Rich Felkerb1683a12014-02-27 23:18:42 -0500661trycppif __BIG_ENDIAN__ "$t" && SUBARCH=${SUBARCH}eb
Bobby Bingham23d64182014-04-27 21:13:59 -0500662if trycppif "__SH_FPU_ANY__ || __SH4__" "$t" ; then
Rich Felkerb1683a12014-02-27 23:18:42 -0500663# Some sh configurations are broken and replace double with float
664# rather than using softfloat when the fpu is present but only
665# supports single precision. Reject them.
666printf "checking whether compiler's double type is IEEE double... "
Rich Felker946b9fa2014-02-27 23:55:04 -0500667echo 'typedef char dblcheck[(int)sizeof(double)-5];' > "$tmpc"
Rich Felkerb1683a12014-02-27 23:18:42 -0500668if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
669printf "yes\n"
670else
671printf "no\n"
672fail "$0: error: compiler's floating point configuration is unsupported"
673fi
674else
675SUBARCH=${SUBARCH}-nofpu
676fi
Rich Felkerd4c82d02015-09-12 03:22:19 +0000677if trycppif __SH_FDPIC__ "$t" ; then
678SUBARCH=${SUBARCH}-fdpic
Rich Felkerd4c82d02015-09-12 03:22:19 +0000679fi
Rich Felkerb1683a12014-02-27 23:18:42 -0500680fi
Bobby Bingham3a3c8132013-10-05 05:13:18 -0500681
Rich Felker3e7f1862013-07-18 20:30:58 -0400682test "$SUBARCH" \
683&& printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
Rich Felker64d2f8e2012-05-04 23:24:51 -0400684
Rich Felker90d77722013-08-11 03:27:35 -0400685case "$ARCH$SUBARCH" in
686arm) ASMSUBARCH=el ;;
687*) ASMSUBARCH=$SUBARCH ;;
688esac
689
Rich Felker86cc54b2013-08-02 19:34:22 -0400690#
691# Some archs (powerpc) have different possible long double formats
692# that the compiler can be configured for. The logic for whether this
693# is supported is in bits/float.h; in general, it is not. We need to
694# check for mismatches here or code in printf, strotd, and scanf will
695# be dangerously incorrect because it depends on (1) the macros being
696# correct, and (2) IEEE semantics.
697#
698printf "checking whether compiler's long double definition matches float.h... "
699echo '#include <float.h>' > "$tmpc"
700echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
701echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
702echo '#endif' >> "$tmpc"
Rich Felkerefdf04c2016-01-27 19:31:15 -0500703if $CC $CFLAGS_C99FSE \
704 -I$srcdir/arch/$ARCH -I$srcdir/arch/generic -I$srcdir/include \
705 $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
Rich Felker86cc54b2013-08-02 19:34:22 -0400706printf "yes\n"
707else
708printf "no\n"
709fail "$0: error: unsupported long double type"
710fi
711
Rich Felker80fbaac2016-02-17 13:53:54 -0500712#
713# Some build systems globally pass in broken CFLAGS like -ffast-math
714# for all packages. On recent GCC we can detect this and error out
715# early rather than producing a seriously-broken math library.
716#
Rich Felker5030e4a2016-02-18 04:09:33 +0000717if trycppif "__FAST_MATH__" \
Rich Felker80fbaac2016-02-17 13:53:54 -0500718 "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
719fail "$0: error: compiler has broken floating point; check CFLAGS"
720fi
721
Rich Felker64d2f8e2012-05-04 23:24:51 -0400722printf "creating config.mak... "
723
Rich Felker453f4622013-08-16 18:19:47 -0400724cmdline=$(quote "$0")
725for i ; do cmdline="$cmdline $(quote "$i")" ; done
726
Rich Felker64d2f8e2012-05-04 23:24:51 -0400727exec 3>&1 1>config.mak
728
729
730cat << EOF
Rich Felker453f4622013-08-16 18:19:47 -0400731# This version of config.mak was generated by:
732# $cmdline
Rich Felker64d2f8e2012-05-04 23:24:51 -0400733# Any changes made here will be lost if configure is re-run
734ARCH = $ARCH
Rich Felker3e7f1862013-07-18 20:30:58 -0400735SUBARCH = $SUBARCH
Rich Felker90d77722013-08-11 03:27:35 -0400736ASMSUBARCH = $ASMSUBARCH
Petr Hosek2f853dd2015-11-18 12:07:32 -0800737srcdir = $srcdir
Rich Felker64d2f8e2012-05-04 23:24:51 -0400738prefix = $prefix
739exec_prefix = $exec_prefix
740bindir = $bindir
741libdir = $libdir
742includedir = $includedir
743syslibdir = $syslibdir
744CC = $CC
Rich Felker4cd8b472015-11-02 16:58:14 -0500745CFLAGS = $CFLAGS
746CFLAGS_AUTO = $CFLAGS_AUTO
Rich Felker64d2f8e2012-05-04 23:24:51 -0400747CFLAGS_C99FSE = $CFLAGS_C99FSE
Rich Felker4a1f55e2013-08-01 17:12:23 -0400748CFLAGS_MEMOPS = $CFLAGS_MEMOPS
Rich Felker1ef849c2015-04-13 20:13:10 -0400749CFLAGS_NOSSP = $CFLAGS_NOSSP
Rich Felker64d2f8e2012-05-04 23:24:51 -0400750CPPFLAGS = $CPPFLAGS
Rich Felker4cd8b472015-11-02 16:58:14 -0500751LDFLAGS = $LDFLAGS
752LDFLAGS_AUTO = $LDFLAGS_AUTO
Rich Felker94e920d2012-08-14 22:50:16 -0400753CROSS_COMPILE = $CROSS_COMPILE
Rich Felker2c1cd232012-09-10 15:30:52 -0400754LIBCC = $LIBCC
Rich Felkera80847d2013-07-22 21:22:04 -0400755OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
Shizb3cd7d12015-06-28 23:08:19 +0200756ALL_TOOLS = $tools
757TOOL_LIBS = $tool_libs
Alex Dowad35b33122015-07-10 15:03:24 +0200758ADD_CFI = $ADD_CFI
Rich Felker64d2f8e2012-05-04 23:24:51 -0400759EOF
760test "x$static" = xno && echo "STATIC_LIBS ="
761test "x$shared" = xno && echo "SHARED_LIBS ="
Shizb3cd7d12015-06-28 23:08:19 +0200762test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
Shizfb585452015-06-28 23:08:21 +0200763test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
Rich Felker16191272016-01-25 19:57:38 -0500764test "x$pic_default" = xyes && echo 'AOBJS = $(LOBJS)'
Rich Felker64d2f8e2012-05-04 23:24:51 -0400765exec 1>&3 3>&-
766
Petr Hosek2f853dd2015-11-18 12:07:32 -0800767test "$srcdir" = "." || ln -sf $srcdir/Makefile .
768
Rich Felker64d2f8e2012-05-04 23:24:51 -0400769printf "done\n"