fix detection of arm hardfloat
[musl] / configure
1 #!/bin/sh
2
3 usage () {
4 cat <<EOF
5 Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET]
6
7 To assign environment variables (e.g., CC, CFLAGS...), specify them as
8 VAR=VALUE.  See below for descriptions of some of the useful variables.
9
10 Defaults for the options are specified in brackets.
11
12 Installation directories:
13   --prefix=PREFIX         main installation prefix [/usr/local/musl]
14   --exec-prefix=EPREFIX   installation prefix for executable files [PREFIX]
15
16 Fine tuning of the installation directories:
17   --bindir=DIR            user executables [EPREFIX/bin]
18   --libdir=DIR            library files for the linker [PREFIX/lib]
19   --includedir=DIR        include files for the C compiler [PREFIX/include]
20   --syslibdir=DIR         location for the dynamic linker [/lib]
21
22 System types:
23   --target=TARGET         configure to run on target TARGET [detected]
24   --host=HOST             same as --target
25
26 Optional features:
27   --enable-optimize=...   optimize listed components for speed over size [auto]
28   --enable-debug          build with debugging information [disabled]
29   --enable-warnings       build with recommended warnings flags [disabled]
30   --enable-gcc-wrapper    build musl-gcc toolchain wrapper [auto]
31   --disable-shared        inhibit building shared library [enabled]
32   --disable-static        inhibit building static library [enabled]
33
34 Some influential environment variables:
35   CC                      C compiler command [detected]
36   CFLAGS                  C compiler flags [-Os -pipe ...]
37   CROSS_COMPILE           prefix for cross compiler and tools [none]
38   LIBCC                   compiler runtime library [detected]
39
40 Use these variables to override the choices made by configure.
41
42 EOF
43 exit 0
44 }
45
46 # Helper functions
47
48 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 ; }
53
54 stripdir () {
55 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
56 }
57
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
70 fi
71 }
72
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
84 fi
85 }
86
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
98 fi
99 }
100
101
102
103 # Beginning of actual script
104
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
122
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
158
159 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
160 stripdir $i
161 done
162
163 #
164 # Get a temp filename we can use
165 #
166 i=0
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
175
176 #
177 # Find a C compiler to use
178 #
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 ; }
185
186 #
187 # Only build musl-gcc wrapper if toolchain does not already target musl
188 #
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)
199 EOF
200 echo $wrapper
201 else
202 echo no
203 fi
204 fi
205
206
207
208 #
209 # Find the target architecture
210 #
211 printf "checking target system type... "
212 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
213 printf "%s\n" "$target"
214
215 #
216 # Convert to just ARCH
217 #
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
228
229 #
230 # Try to get a conforming C99 freestanding environment
231 #
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
239
240 #
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...
246 #
247 tryflag CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns
248
249 #
250 # If debugging is explicitly enabled, don't auto-enable optimizations
251 #
252 if test "$debug" = yes ; then
253 CFLAGS_AUTO=-g
254 test "$optimize" = auto && optimize=no
255 fi
256
257 #
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.
260 #
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
268 fi
269 ;;
270 xsize|xnone) printf "minimize size\n" ; optimize=size ;;
271 xno|x) printf "disabled\n" ; optimize=no ;;
272 *) printf "custom\n" ;;
273 esac
274
275 test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
276 test "$optimize" = yes && optimize="internal,malloc,string"
277
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"
296 fi
297
298 # Always try -pipe
299 tryflag CFLAGS_AUTO -pipe
300
301 #
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.
305 #
306 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
307 else 
308 tryflag CFLAGS_AUTO -fomit-frame-pointer
309 fi
310
311 #
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).
316 #
317 tryflag CFLAGS_AUTO -fno-unwind-tables
318 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
319
320 #
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.
324 #
325 tryflag CFLAGS_AUTO -Wa,--noexecstack
326
327 #
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).
332 #
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
336 fi
337
338 #
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.
343 #
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
348
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
358 fi
359
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
363
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
369 }
370
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"
377
378 # Figure out arch variants for archs with variants
379 SUBARCH=
380 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
381
382 if test "$ARCH" = "arm" ; then
383 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
384 trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
385 fi
386
387 test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
388 && SUBARCH=${SUBARCH}el
389
390 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
391 && SUBARCH=${SUBARCH}el
392
393 test "$SUBARCH" \
394 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
395
396 case "$ARCH$SUBARCH" in
397 arm) ASMSUBARCH=el ;;
398 *) ASMSUBARCH=$SUBARCH ;;
399 esac
400
401 #
402 # Some archs (powerpc) have different possible long double formats
403 # that the compiler can be configured for. The logic for whether this
404 # is supported is in bits/float.h; in general, it is not. We need to
405 # check for mismatches here or code in printf, strotd, and scanf will
406 # be dangerously incorrect because it depends on (1) the macros being
407 # correct, and (2) IEEE semantics.
408 #
409 printf "checking whether compiler's long double definition matches float.h... "
410 echo '#include <float.h>' > "$tmpc"
411 echo '#if LDBL_MANT_DIG == 53' >> "$tmpc"
412 echo 'typedef char ldcheck[9-(int)sizeof(long double)];' >> "$tmpc"
413 echo '#endif' >> "$tmpc"
414 if $CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include $CPPFLAGS $CFLAGS \
415   -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
416 printf "yes\n"
417 else
418 printf "no\n"
419 fail "$0: error: unsupported long double type"
420 fi
421
422 printf "creating config.mak... "
423
424 exec 3>&1 1>config.mak
425
426
427 cat << EOF
428 # This version of config.mak was generated by configure
429 # Any changes made here will be lost if configure is re-run
430 ARCH = $ARCH
431 SUBARCH = $SUBARCH
432 ASMSUBARCH = $ASMSUBARCH
433 prefix = $prefix
434 exec_prefix = $exec_prefix
435 bindir = $bindir
436 libdir = $libdir
437 includedir = $includedir
438 syslibdir = $syslibdir
439 CC = $CC
440 CFLAGS= $CFLAGS_AUTO $CFLAGS
441 CFLAGS_C99FSE = $CFLAGS_C99FSE
442 CFLAGS_MEMOPS = $CFLAGS_MEMOPS
443 CPPFLAGS = $CPPFLAGS
444 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
445 CROSS_COMPILE = $CROSS_COMPILE
446 LIBCC = $LIBCC
447 OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
448 EOF
449 test "x$static" = xno && echo "STATIC_LIBS ="
450 test "x$shared" = xno && echo "SHARED_LIBS ="
451 test "x$wrapper" = xno && echo "ALL_TOOLS ="
452 test "x$wrapper" = xno && echo "TOOL_LIBS ="
453 exec 1>&3 3>&-
454
455 printf "done\n"