move RPATH search after LD_LIBRARY_PATH search
[musl.git] / configure
blob1d734726a4b8abb1d55e653ec44eb6cf1c5e2878
1 #!/bin/sh
3 usage () {
4 cat <<EOF
5 Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
7 To assign environment variables (e.g., CC, CFLAGS...), specify them as
8 VAR=VALUE. See below for descriptions of some of the useful variables.
10 Defaults for the options are specified in brackets.
12 Installation directories:
13 --prefix=PREFIX main installation prefix [/usr/local/musl]
14 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
16 Fine tuning of the installation directories:
17 --bindir=DIR user executables [EPREFIX/bin]
18 --libdir=DIR library files for the linker [PREFIX/lib]
19 --includedir=DIR include files for the C compiler [PREFIX/include]
20 --syslibdir=DIR location for the dynamic linker [/lib]
22 System types:
23 --target=TARGET configure to run on target TARGET [detected]
24 --host=HOST same as --target
26 Optional features:
27 --enable-optimize=... optimize listed components for speed over size [auto]
28 --enable-debug build with debugging information [disabled]
29 --enable-warnings build with recommended warnings flags [disabled]
30 --enable-gcc-wrapper build musl-gcc toolchain wrapper [auto]
31 --disable-shared inhibit building shared library [enabled]
32 --disable-static inhibit building static library [enabled]
34 Some influential environment variables:
35 CC C compiler command [detected]
36 CFLAGS C compiler flags [-Os -pipe ...]
37 CROSS_COMPILE prefix for cross compiler and tools [none]
38 LIBCC compiler runtime library [detected]
40 Use these variables to override the choices made by configure.
42 EOF
43 exit 0
46 # Helper functions
48 echo () { printf "%s\n" "$*" ; }
49 fail () { echo "$*" ; exit 1 ; }
50 fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
51 cmdexists () { type "$1" >/dev/null 2>&1 ; }
52 trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
54 stripdir () {
55 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
58 trycppif () {
59 printf "checking preprocessor condition %s... " "$1"
60 echo "typedef int x;" > "$tmpc"
61 echo "#if $1" >> "$tmpc"
62 echo "#error yes" >> "$tmpc"
63 echo "#endif" >> "$tmpc"
64 if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
65 printf "false\n"
66 return 1
67 else
68 printf "true\n"
69 return 0
73 tryflag () {
74 printf "checking whether compiler accepts %s... " "$2"
75 echo "typedef int x;" > "$tmpc"
76 if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
77 printf "yes\n"
78 eval "$1=\"\${$1} \$2\""
79 eval "$1=\${$1# }"
80 return 0
81 else
82 printf "no\n"
83 return 1
87 tryldflag () {
88 printf "checking whether linker accepts %s... " "$2"
89 echo "typedef int x;" > "$tmpc"
90 if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
91 printf "yes\n"
92 eval "$1=\"\${$1} \$2\""
93 eval "$1=\${$1# }"
94 return 0
95 else
96 printf "no\n"
97 return 1
103 # Beginning of actual script
105 CFLAGS_C99FSE=
106 CFLAGS_AUTO=
107 CFLAGS_MEMOPS=
108 LDFLAGS_AUTO=
109 OPTIMIZE_GLOBS=
110 prefix=/usr/local/musl
111 exec_prefix='$(prefix)'
112 bindir='$(exec_prefix)/bin'
113 libdir='$(prefix)/lib'
114 includedir='$(prefix)/include'
115 syslibdir='/lib'
116 target=
117 optimize=auto
118 debug=no
119 warnings=no
120 shared=yes
121 static=yes
123 for arg ; do
124 case "$arg" in
125 --help) usage ;;
126 --prefix=*) prefix=${arg#*=} ;;
127 --exec-prefix=*) exec_prefix=${arg#*=} ;;
128 --bindir=*) bindir=${arg#*=} ;;
129 --libdir=*) libdir=${arg#*=} ;;
130 --includedir=*) includedir=${arg#*=} ;;
131 --syslibdir=*) syslibdir=${arg#*=} ;;
132 --enable-shared|--enable-shared=yes) shared=yes ;;
133 --disable-shared|--enable-shared=no) shared=no ;;
134 --enable-static|--enable-static=yes) static=yes ;;
135 --disable-static|--enable-static=no) static=no ;;
136 --enable-optimize) optimize=yes ;;
137 --enable-optimize=*) optimize=${arg#*=} ;;
138 --disable-optimize) optimize=no ;;
139 --enable-debug|--enable-debug=yes) debug=yes ;;
140 --disable-debug|--enable-debug=no) debug=no ;;
141 --enable-warnings|--enable-warnings=yes) warnings=yes ;;
142 --disable-warnings|--enable-warnings=no) warnings=no ;;
143 --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
144 --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
145 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
146 --host=*|--target=*) target=${arg#*=} ;;
147 -* ) echo "$0: unknown option $arg" ;;
148 CC=*) CC=${arg#*=} ;;
149 CFLAGS=*) CFLAGS=${arg#*=} ;;
150 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
151 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
152 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
153 LIBCC=*) LIBCC=${arg#*=} ;;
154 *=*) ;;
155 *) target=$arg ;;
156 esac
157 done
159 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
160 stripdir $i
161 done
164 # Get a temp filename we can use
167 set -C
168 while : ; do i=$(($i+1))
169 tmpc="./conf$$-$PPID-$i.c"
170 2>|/dev/null > "$tmpc" && break
171 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
172 done
173 set +C
174 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
177 # Find a C compiler to use
179 printf "checking for C compiler... "
180 trycc ${CROSS_COMPILE}gcc
181 trycc ${CROSS_COMPILE}c99
182 trycc ${CROSS_COMPILE}cc
183 printf "%s\n" "$CC"
184 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
187 # Only build musl-gcc wrapper if toolchain does not already target musl
189 if test -z "$wrapper" ; then
190 printf "checking whether compiler is gcc... "
191 if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
192 echo yes
193 printf "checking whether to build musl-gcc wrapper... "
194 wrapper=yes
195 while read line ; do
196 case "$line" in */ld-musl-*) wrapper=no ;; esac
197 done <<EOF
198 $($CC -dumpspecs)
200 echo $wrapper
201 else
202 echo no
209 # Find the target architecture
211 printf "checking target system type... "
212 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
213 printf "%s\n" "$target"
216 # Convert to just ARCH
218 case "$target" in
219 arm*) ARCH=arm ;;
220 i?86*) ARCH=i386 ;;
221 x86_64*) ARCH=x86_64 ;;
222 mips-*|mipsel-*) ARCH=mips ;;
223 microblaze-*) ARCH=microblaze ;;
224 powerpc-*) ARCH=powerpc ;;
225 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
226 *) fail "$0: unknown or unsupported target \"$target\"" ;;
227 esac
230 # Try to get a conforming C99 freestanding environment
232 tryflag CFLAGS_C99FSE -std=c99
233 tryflag CFLAGS_C99FSE -nostdinc
234 tryflag CFLAGS_C99FSE -ffreestanding \
235 || tryflag CFLAGS_C99FSE -fno-builtin
236 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
237 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
238 tryflag CFLAGS_C99FSE -frounding-math
241 # Check for options that may be needed to prevent the compiler from
242 # generating self-referential versions of memcpy,, memmove, memcmp,
243 # and memset. Really, we should add a check to determine if this
244 # option is sufficient, and if not, add a macro to cripple these
245 # functions with volatile...
247 tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
250 # If debugging is explicitly enabled, don't auto-enable optimizations
252 if test "$debug" = yes ; then
253 CFLAGS_AUTO=-g
254 test "$optimize" = auto && optimize=no
258 # Possibly add a -O option to CFLAGS and select modules to optimize with
259 # -O3 based on the status of --enable-optimize and provided CFLAGS.
261 printf "checking for optimization settings... "
262 case "x$optimize" in
263 xauto)
264 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
265 printf "using provided CFLAGS\n" ;optimize=no
266 else
267 printf "using defaults\n" ; optimize=yes
270 xsize|xnone) printf "minimize size\n" ; optimize=size ;;
271 xno|x) printf "disabled\n" ; optimize=no ;;
272 *) printf "custom\n" ;;
273 esac
275 test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
276 test "$optimize" = yes && optimize="internal,malloc,string"
278 if fnmatch 'no|size' "$optimize" ; then :
279 else
280 printf "components to be optimized for speed:"
281 while test "$optimize" ; do
282 case "$optimize" in
283 *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
284 *) this=$optimize optimize=
285 esac
286 printf " $this"
287 case "$this" in
288 */*.c) ;;
289 */*) this=$this*.c ;;
290 *) this=$this/*.c ;;
291 esac
292 OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
293 done
294 OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
295 printf "\n"
298 # Always try -pipe
299 tryflag CFLAGS_AUTO -pipe
302 # If debugging is disabled, omit frame pointer. Modern GCC does this
303 # anyway on most archs even when debugging is enabled since the frame
304 # pointer is no longer needed for debugging.
306 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
307 else
308 tryflag CFLAGS_AUTO -fomit-frame-pointer
312 # Modern GCC wants to put DWARF tables (used for debugging and
313 # unwinding) in the loaded part of the program where they are
314 # unstrippable. These options force them back to debug sections (and
315 # cause them not to get generated at all if debugging is off).
317 tryflag CFLAGS_AUTO -fno-unwind-tables
318 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
321 # The GNU toolchain defaults to assuming unmarked files need an
322 # executable stack, potentially exposing vulnerabilities in programs
323 # linked with such object files. Fix this.
325 tryflag CFLAGS_AUTO -Wa,--noexecstack
328 # On x86, make sure we don't have incompatible instruction set
329 # extensions enabled by default. This is bad for making static binaries.
330 # We cheat and use i486 rather than i386 because i386 really does not
331 # work anyway (issues with atomic ops).
333 if test "$ARCH" = "i386" ; then
334 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
335 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
339 # Even with -std=c99, gcc accepts some constructs which are constraint
340 # violations. We want to treat these as errors regardless of whether
341 # other purely stylistic warnings are enabled -- especially implicit
342 # function declarations, which are a dangerous programming error.
344 tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
345 tryflag CFLAGS_AUTO -Werror=implicit-int
346 tryflag CFLAGS_AUTO -Werror=pointer-sign
347 tryflag CFLAGS_AUTO -Werror=pointer-arith
349 if test "x$warnings" = xyes ; then
350 tryflag CFLAGS_AUTO -Wall
351 tryflag CFLAGS_AUTO -Wcast-align
352 tryflag CFLAGS_AUTO -Wno-parentheses
353 tryflag CFLAGS_AUTO -Wno-uninitialized
354 tryflag CFLAGS_AUTO -Wno-missing-braces
355 tryflag CFLAGS_AUTO -Wno-unused-value
356 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
357 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
360 # Some patched GCC builds have these defaults messed up...
361 tryflag CFLAGS_AUTO -fno-stack-protector
362 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
364 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
365 LDFLAGS_DUMMY=
366 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
367 printf "warning: disabling dynamic linking support\n"
368 shared=no
371 # Find compiler runtime library
372 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
373 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
374 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
375 && tryldflag LIBCC "$try_libcc"
376 printf "using compiler runtime libraries: %s\n" "$LIBCC"
378 # Figure out arch variants for archs with variants
379 SUBARCH=
380 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
382 if test "$ARCH" = "arm" ; then
383 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
384 trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
387 test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
388 && SUBARCH=${SUBARCH}el
390 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
391 && SUBARCH=${SUBARCH}el
393 test "$SUBARCH" \
394 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
396 printf "creating config.mak... "
398 exec 3>&1 1>config.mak
401 cat << EOF
402 # This version of config.mak was generated by configure
403 # Any changes made here will be lost if configure is re-run
404 ARCH = $ARCH
405 SUBARCH = $SUBARCH
406 prefix = $prefix
407 exec_prefix = $exec_prefix
408 bindir = $bindir
409 libdir = $libdir
410 includedir = $includedir
411 syslibdir = $syslibdir
412 CC = $CC
413 CFLAGS= $CFLAGS_AUTO $CFLAGS
414 CFLAGS_C99FSE = $CFLAGS_C99FSE
415 CFLAGS_MEMOPS = $CFLAGS_MEMOPS
416 CPPFLAGS = $CPPFLAGS
417 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
418 CROSS_COMPILE = $CROSS_COMPILE
419 LIBCC = $LIBCC
420 OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
422 test "x$static" = xno && echo "STATIC_LIBS ="
423 test "x$shared" = xno && echo "SHARED_LIBS ="
424 test "x$wrapper" = xno && echo "ALL_TOOLS ="
425 test "x$wrapper" = xno && echo "TOOL_LIBS ="
426 exec 1>&3 3>&-
428 printf "done\n"