enhance build process to allow selective -O3 optimization
[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 LDFLAGS_AUTO=
108 OPTIMIZE_GLOBS=
109 prefix=/usr/local/musl
110 exec_prefix='$(prefix)'
111 bindir='$(exec_prefix)/bin'
112 libdir='$(prefix)/lib'
113 includedir='$(prefix)/include'
114 syslibdir='/lib'
115 target=
116 optimize=auto
117 debug=no
118 warnings=no
119 shared=yes
120 static=yes
121
122 for arg ; do
123 case "$arg" in
124 --help) usage ;;
125 --prefix=*) prefix=${arg#*=} ;;
126 --exec-prefix=*) exec_prefix=${arg#*=} ;;
127 --bindir=*) bindir=${arg#*=} ;;
128 --libdir=*) libdir=${arg#*=} ;;
129 --includedir=*) includedir=${arg#*=} ;;
130 --syslibdir=*) syslibdir=${arg#*=} ;;
131 --enable-shared|--enable-shared=yes) shared=yes ;;
132 --disable-shared|--enable-shared=no) shared=no ;;
133 --enable-static|--enable-static=yes) static=yes ;;
134 --disable-static|--enable-static=no) static=no ;;
135 --enable-optimize) optimize=yes ;;
136 --enable-optimize=*) optimize=${arg#*=} ;;
137 --disable-optimize) optimize=no ;;
138 --enable-debug|--enable-debug=yes) debug=yes ;;
139 --disable-debug|--enable-debug=no) debug=no ;;
140 --enable-warnings|--enable-warnings=yes) warnings=yes ;;
141 --disable-warnings|--enable-warnings=no) warnings=no ;;
142 --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
143 --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
144 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
145 --host=*|--target=*) target=${arg#*=} ;;
146 -* ) echo "$0: unknown option $arg" ;;
147 CC=*) CC=${arg#*=} ;;
148 CFLAGS=*) CFLAGS=${arg#*=} ;;
149 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
150 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
151 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
152 LIBCC=*) LIBCC=${arg#*=} ;;
153 *=*) ;;
154 *) target=$arg ;;
155 esac
156 done
157
158 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
159 stripdir $i
160 done
161
162 #
163 # Get a temp filename we can use
164 #
165 i=0
166 set -C
167 while : ; do i=$(($i+1))
168 tmpc="./conf$$-$PPID-$i.c"
169 2>|/dev/null > "$tmpc" && break
170 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
171 done
172 set +C
173 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
174
175 #
176 # Find a C compiler to use
177 #
178 printf "checking for C compiler... "
179 trycc ${CROSS_COMPILE}gcc
180 trycc ${CROSS_COMPILE}c99
181 trycc ${CROSS_COMPILE}cc
182 printf "%s\n" "$CC"
183 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
184
185 #
186 # Only build musl-gcc wrapper if toolchain does not already target musl
187 #
188 if test -z "$wrapper" ; then
189 printf "checking whether compiler is gcc... "
190 if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
191 echo yes
192 printf "checking whether to build musl-gcc wrapper... "
193 wrapper=yes
194 while read line ; do
195 case "$line" in */ld-musl-*) wrapper=no ;; esac
196 done <<EOF
197 $($CC -dumpspecs)
198 EOF
199 echo $wrapper
200 else
201 echo no
202 fi
203 fi
204
205
206
207 #
208 # Find the target architecture
209 #
210 printf "checking target system type... "
211 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
212 printf "%s\n" "$target"
213
214 #
215 # Convert to just ARCH
216 #
217 case "$target" in
218 arm*) ARCH=arm ;;
219 i?86*) ARCH=i386 ;;
220 x86_64*) ARCH=x86_64 ;;
221 mips-*|mipsel-*) ARCH=mips ;;
222 microblaze-*) ARCH=microblaze ;;
223 powerpc-*) ARCH=powerpc ;;
224 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
225 *) fail "$0: unknown or unsupported target \"$target\"" ;;
226 esac
227
228 #
229 # Try to get a conforming C99 freestanding environment
230 #
231 tryflag CFLAGS_C99FSE -std=c99
232 tryflag CFLAGS_C99FSE -nostdinc
233 tryflag CFLAGS_C99FSE -ffreestanding \
234 || tryflag CFLAGS_C99FSE -fno-builtin
235 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
236 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
237 tryflag CFLAGS_C99FSE -frounding-math
238
239
240 #
241 # If debugging is explicitly enabled, don't auto-enable optimizations
242 #
243 if test "$debug" = yes ; then
244 CFLAGS_AUTO=-g
245 test "$optimize" = auto && optimize=no
246 fi
247
248 #
249 # Possibly add a -O option to CFLAGS and select modules to optimize with
250 # -O3 based on the status of --enable-optimize and provided CFLAGS.
251 #
252 printf "checking for optimization settings... "
253 case "x$optimize" in
254 xauto)
255 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
256 printf "using provided CFLAGS\n" ;optimize=no
257 else
258 printf "using defaults\n" ; optimize=yes
259 fi
260 ;;
261 xsize|xnone) printf "minimize size\n" ; optimize=size ;;
262 xno|x) printf "disabled\n" ; optimize=no ;;
263 *) printf "custom\n" ;;
264 esac
265
266 test "$optimize" = no || tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
267 test "$optimize" = yes && optimize="internal,malloc,math,string"
268
269 if fnmatch 'no|size' "$optimize" ; then :
270 else
271 printf "components to be optimized for speed:"
272 while test "$optimize" ; do
273 case "$optimize" in
274 *,*) this=${optimize%%,*} optimize=${optimize#*,} ;;
275 *) this=$optimize optimize=
276 esac
277 printf " $this"
278 case "$this" in
279 */*.c) ;;
280 */*) this=$this*.c ;;
281 *) this=$this/*.c ;;
282 esac
283 OPTIMIZE_GLOBS="$OPTIMIZE_GLOBS $this"
284 done
285 OPTIMIZE_GLOBS=${OPTIMIZE_GLOBS# }
286 printf "\n"
287 fi
288
289 # Always try -pipe
290 tryflag CFLAGS_AUTO -pipe
291
292 #
293 # If debugging is disabled, omit frame pointer. Modern GCC does this
294 # anyway on most archs even when debugging is enabled since the frame
295 # pointer is no longer needed for debugging.
296 #
297 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
298 else 
299 tryflag CFLAGS_AUTO -fomit-frame-pointer
300 fi
301
302 #
303 # Modern GCC wants to put DWARF tables (used for debugging and
304 # unwinding) in the loaded part of the program where they are
305 # unstrippable. These options force them back to debug sections (and
306 # cause them not to get generated at all if debugging is off).
307 #
308 tryflag CFLAGS_AUTO -fno-unwind-tables
309 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
310
311 #
312 # The GNU toolchain defaults to assuming unmarked files need an
313 # executable stack, potentially exposing vulnerabilities in programs
314 # linked with such object files. Fix this.
315 #
316 tryflag CFLAGS_AUTO -Wa,--noexecstack
317
318 #
319 # On x86, make sure we don't have incompatible instruction set
320 # extensions enabled by default. This is bad for making static binaries.
321 # We cheat and use i486 rather than i386 because i386 really does not
322 # work anyway (issues with atomic ops).
323 #
324 if test "$ARCH" = "i386" ; then
325 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
326 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
327 fi
328
329 #
330 # Even with -std=c99, gcc accepts some constructs which are constraint
331 # violations. We want to treat these as errors regardless of whether
332 # other purely stylistic warnings are enabled -- especially implicit
333 # function declarations, which are a dangerous programming error.
334 #
335 tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
336 tryflag CFLAGS_AUTO -Werror=implicit-int
337 tryflag CFLAGS_AUTO -Werror=pointer-sign
338 tryflag CFLAGS_AUTO -Werror=pointer-arith
339
340 if test "x$warnings" = xyes ; then
341 tryflag CFLAGS_AUTO -Wall
342 tryflag CFLAGS_AUTO -Wcast-align
343 tryflag CFLAGS_AUTO -Wno-parentheses
344 tryflag CFLAGS_AUTO -Wno-uninitialized
345 tryflag CFLAGS_AUTO -Wno-missing-braces
346 tryflag CFLAGS_AUTO -Wno-unused-value
347 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
348 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
349 fi
350
351 # Some patched GCC builds have these defaults messed up...
352 tryflag CFLAGS_AUTO -fno-stack-protector
353 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
354
355 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
356 LDFLAGS_DUMMY=
357 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
358 printf "warning: disabling dynamic linking support\n"
359 shared=no
360 }
361
362 # Find compiler runtime library
363 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
364 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
365 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
366                  && tryldflag LIBCC "$try_libcc"
367 printf "using compiler runtime libraries: %s\n" "$LIBCC"
368
369 # Figure out arch variants for archs with variants
370 SUBARCH=
371 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
372
373 if test "$ARCH" = "arm" ; then
374 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
375 trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
376 fi
377
378 test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
379 && SUBARCH=${SUBARCH}el
380
381 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
382 && SUBARCH=${SUBARCH}el
383
384 test "$SUBARCH" \
385 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
386
387 printf "creating config.mak... "
388
389 exec 3>&1 1>config.mak
390
391
392 cat << EOF
393 # This version of config.mak was generated by configure
394 # Any changes made here will be lost if configure is re-run
395 ARCH = $ARCH
396 SUBARCH = $SUBARCH
397 prefix = $prefix
398 exec_prefix = $exec_prefix
399 bindir = $bindir
400 libdir = $libdir
401 includedir = $includedir
402 syslibdir = $syslibdir
403 CC = $CC
404 CFLAGS= $CFLAGS_AUTO $CFLAGS
405 CFLAGS_C99FSE = $CFLAGS_C99FSE
406 CPPFLAGS = $CPPFLAGS
407 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
408 CROSS_COMPILE = $CROSS_COMPILE
409 LIBCC = $LIBCC
410 OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
411 EOF
412 test "x$static" = xno && echo "STATIC_LIBS ="
413 test "x$shared" = xno && echo "SHARED_LIBS ="
414 test "x$wrapper" = xno && echo "ALL_TOOLS ="
415 test "x$wrapper" = xno && echo "TOOL_LIBS ="
416 exec 1>&3 3>&-
417
418 printf "done\n"