harden realloc/free to detect simple overflows
[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-debug          build with debugging information [disabled]
28   --enable-warnings       build with recommended warnings flags [disabled]
29   --enable-gcc-wrapper    build musl-gcc toolchain wrapper [auto]
30   --disable-shared        inhibit building shared library [enabled]
31   --disable-static        inhibit building static library [enabled]
32
33 Some influential environment variables:
34   CC                      C compiler command [detected]
35   CFLAGS                  C compiler flags [-Os -pipe ...]
36   CROSS_COMPILE           prefix for cross compiler and tools [none]
37   LIBCC                   compiler runtime library [detected]
38
39 Use these variables to override the choices made by configure.
40
41 EOF
42 exit 0
43 }
44
45 # Helper functions
46
47 echo () { printf "%s\n" "$*" ; }
48 fail () { echo "$*" ; exit 1 ; }
49 fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
50 cmdexists () { type "$1" >/dev/null 2>&1 ; }
51 trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
52
53 stripdir () {
54 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
55 }
56
57 trycppif () {
58 printf "checking preprocessor condition %s... " "$1"
59 echo "typedef int x;" > "$tmpc"
60 echo "#if $1" >> "$tmpc"
61 echo "#error yes" >> "$tmpc"
62 echo "#endif" >> "$tmpc"
63 if $CC $2 -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
64 printf "false\n"
65 return 1
66 else
67 printf "true\n"
68 return 0
69 fi
70 }
71
72 tryflag () {
73 printf "checking whether compiler accepts %s... " "$2"
74 echo "typedef int x;" > "$tmpc"
75 if $CC "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
76 printf "yes\n"
77 eval "$1=\"\${$1} \$2\""
78 eval "$1=\${$1# }"
79 return 0
80 else
81 printf "no\n"
82 return 1
83 fi
84 }
85
86 tryldflag () {
87 printf "checking whether linker accepts %s... " "$2"
88 echo "typedef int x;" > "$tmpc"
89 if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
90 printf "yes\n"
91 eval "$1=\"\${$1} \$2\""
92 eval "$1=\${$1# }"
93 return 0
94 else
95 printf "no\n"
96 return 1
97 fi
98 }
99
100
101
102 # Beginning of actual script
103
104 CFLAGS_C99FSE=
105 CFLAGS_AUTO=
106 LDFLAGS_AUTO=
107 prefix=/usr/local/musl
108 exec_prefix='$(prefix)'
109 bindir='$(exec_prefix)/bin'
110 libdir='$(prefix)/lib'
111 includedir='$(prefix)/include'
112 syslibdir='/lib'
113 target=
114 debug=no
115 warnings=no
116 shared=yes
117 static=yes
118
119 for arg ; do
120 case "$arg" in
121 --help) usage ;;
122 --prefix=*) prefix=${arg#*=} ;;
123 --exec-prefix=*) exec_prefix=${arg#*=} ;;
124 --bindir=*) bindir=${arg#*=} ;;
125 --libdir=*) libdir=${arg#*=} ;;
126 --includedir=*) includedir=${arg#*=} ;;
127 --syslibdir=*) syslibdir=${arg#*=} ;;
128 --enable-shared|--enable-shared=yes) shared=yes ;;
129 --disable-shared|--enable-shared=no) shared=no ;;
130 --enable-static|--enable-static=yes) static=yes ;;
131 --disable-static|--enable-static=no) static=no ;;
132 --enable-debug|--enable-debug=yes) debug=yes ;;
133 --disable-debug|--enable-debug=no) debug=no ;;
134 --enable-warnings|--enable-warnings=yes) warnings=yes ;;
135 --disable-warnings|--enable-warnings=no) warnings=no ;;
136 --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
137 --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
138 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
139 --host=*|--target=*) target=${arg#*=} ;;
140 -* ) echo "$0: unknown option $arg" ;;
141 CC=*) CC=${arg#*=} ;;
142 CFLAGS=*) CFLAGS=${arg#*=} ;;
143 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
144 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
145 CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;;
146 LIBCC=*) LIBCC=${arg#*=} ;;
147 *=*) ;;
148 *) target=$arg ;;
149 esac
150 done
151
152 for i in prefix exec_prefix bindir libdir includedir syslibdir ; do
153 stripdir $i
154 done
155
156 #
157 # Get a temp filename we can use
158 #
159 i=0
160 set -C
161 while : ; do i=$(($i+1))
162 tmpc="./conf$$-$PPID-$i.c"
163 2>|/dev/null > "$tmpc" && break
164 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
165 done
166 set +C
167 trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
168
169 #
170 # Find a C compiler to use
171 #
172 printf "checking for C compiler... "
173 trycc ${CROSS_COMPILE}gcc
174 trycc ${CROSS_COMPILE}c99
175 trycc ${CROSS_COMPILE}cc
176 printf "%s\n" "$CC"
177 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
178
179 #
180 # Only build musl-gcc wrapper if toolchain does not already target musl
181 #
182 if test -z "$wrapper" ; then
183 printf "checking whether compiler is gcc... "
184 if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then
185 echo yes
186 printf "checking whether to build musl-gcc wrapper... "
187 wrapper=yes
188 while read line ; do
189 case "$line" in */ld-musl-*) wrapper=no ;; esac
190 done <<EOF
191 $($CC -dumpspecs)
192 EOF
193 echo $wrapper
194 else
195 echo no
196 fi
197 fi
198
199
200
201 #
202 # Find the target architecture
203 #
204 printf "checking target system type... "
205 test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown
206 printf "%s\n" "$target"
207
208 #
209 # Convert to just ARCH
210 #
211 case "$target" in
212 arm*) ARCH=arm ;;
213 i?86*) ARCH=i386 ;;
214 x86_64*) ARCH=x86_64 ;;
215 mips-*|mipsel-*) ARCH=mips ;;
216 microblaze-*) ARCH=microblaze ;;
217 powerpc-*) ARCH=powerpc ;;
218 unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;;
219 *) fail "$0: unknown or unsupported target \"$target\"" ;;
220 esac
221
222 #
223 # Try to get a conforming C99 freestanding environment
224 #
225 tryflag CFLAGS_C99FSE -std=c99
226 tryflag CFLAGS_C99FSE -nostdinc
227 tryflag CFLAGS_C99FSE -ffreestanding \
228 || tryflag CFLAGS_C99FSE -fno-builtin
229 tryflag CFLAGS_C99FSE -fexcess-precision=standard \
230 || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
231 tryflag CFLAGS_C99FSE -frounding-math
232
233 #
234 # Setup basic default CFLAGS: debug, optimization, and -pipe
235 #
236 if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then :
237 else
238 tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2
239 fi
240 test "x$debug" = xyes && CFLAGS_AUTO="-g"
241 tryflag CFLAGS_AUTO -pipe
242
243 #
244 # If debugging is disabled, omit frame pointer. Modern GCC does this
245 # anyway on most archs even when debugging is enabled since the frame
246 # pointer is no longer needed for debugging.
247 #
248 if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then :
249 else 
250 tryflag CFLAGS_AUTO -fomit-frame-pointer
251 fi
252
253 #
254 # Modern GCC wants to put DWARF tables (used for debugging and
255 # unwinding) in the loaded part of the program where they are
256 # unstrippable. These options force them back to debug sections (and
257 # cause them not to get generated at all if debugging is off).
258 #
259 tryflag CFLAGS_AUTO -fno-unwind-tables
260 tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
261
262 #
263 # The GNU toolchain defaults to assuming unmarked files need an
264 # executable stack, potentially exposing vulnerabilities in programs
265 # linked with such object files. Fix this.
266 #
267 tryflag CFLAGS_AUTO -Wa,--noexecstack
268
269 #
270 # Some optimization levels add bloated alignment that hurt performance
271 #
272 tryflag CFLAGS_AUTO -falign-functions=1
273 tryflag CFLAGS_AUTO -falign-labels=1
274 tryflag CFLAGS_AUTO -falign-loops=1
275 tryflag CFLAGS_AUTO -falign-jumps=1
276
277 #
278 # On x86, make sure we don't have incompatible instruction set
279 # extensions enabled by default. This is bad for making static binaries.
280 # We cheat and use i486 rather than i386 because i386 really does not
281 # work anyway (issues with atomic ops).
282 #
283 if test "$ARCH" = "i386" ; then
284 fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486
285 fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic
286 fi
287
288 #
289 # Even with -std=c99, gcc accepts some constructs which are constraint
290 # violations. We want to treat these as errors regardless of whether
291 # other purely stylistic warnings are enabled -- especially implicit
292 # function declarations, which are a dangerous programming error.
293 #
294 tryflag CFLAGS_AUTO -Werror=implicit-function-declaration
295 tryflag CFLAGS_AUTO -Werror=implicit-int
296 tryflag CFLAGS_AUTO -Werror=pointer-sign
297 tryflag CFLAGS_AUTO -Werror=pointer-arith
298
299 if test "x$warnings" = xyes ; then
300 tryflag CFLAGS_AUTO -Wall
301 tryflag CFLAGS_AUTO -Wcast-align
302 tryflag CFLAGS_AUTO -Wno-parentheses
303 tryflag CFLAGS_AUTO -Wno-uninitialized
304 tryflag CFLAGS_AUTO -Wno-missing-braces
305 tryflag CFLAGS_AUTO -Wno-unused-value
306 tryflag CFLAGS_AUTO -Wno-unused-but-set-variable
307 tryflag CFLAGS_AUTO -Wno-unknown-pragmas
308 fi
309
310 # Some patched GCC builds have these defaults messed up...
311 tryflag CFLAGS_AUTO -fno-stack-protector
312 tryldflag LDFLAGS_AUTO -Wl,--hash-style=both
313
314 # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions
315 LDFLAGS_DUMMY=
316 tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || {
317 printf "warning: disabling dynamic linking support\n"
318 shared=no
319 }
320
321 # Find compiler runtime library
322 test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh
323 test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt
324 test -z "$LIBCC" && try_libcc=`$CC -print-file-name=libpcc.a 2>/dev/null` \
325                  && tryldflag LIBCC "$try_libcc"
326 printf "using compiler runtime libraries: %s\n" "$LIBCC"
327
328 # Figure out arch variants for archs with variants
329 SUBARCH=
330 t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS_AUTO $CFLAGS"
331
332 if test "$ARCH" = "arm" ; then
333 trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
334 trycppif __SOFTFP__ "$t" || SUBARCH=${SUBARCH}hf
335 fi
336
337 test "$ARCH" = "mips" && trycppif "_MIPSEL || __MIPSEL || __MIPSEL__" "$t" \
338 && SUBARCH=${SUBARCH}el
339
340 test "$ARCH" = "microblaze" && trycppif __MICROBLAZEEL__ "$t" \
341 && SUBARCH=${SUBARCH}el
342
343 test "$SUBARCH" \
344 && printf "configured for %s variant: %s\n" "$ARCH" "$ARCH$SUBARCH"
345
346 printf "creating config.mak... "
347
348 exec 3>&1 1>config.mak
349
350
351 cat << EOF
352 # This version of config.mak was generated by configure
353 # Any changes made here will be lost if configure is re-run
354 ARCH = $ARCH
355 SUBARCH = $SUBARCH
356 prefix = $prefix
357 exec_prefix = $exec_prefix
358 bindir = $bindir
359 libdir = $libdir
360 includedir = $includedir
361 syslibdir = $syslibdir
362 CC = $CC
363 CFLAGS= $CFLAGS_AUTO $CFLAGS
364 CFLAGS_C99FSE = $CFLAGS_C99FSE
365 CPPFLAGS = $CPPFLAGS
366 LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
367 CROSS_COMPILE = $CROSS_COMPILE
368 LIBCC = $LIBCC
369 EOF
370 test "x$static" = xno && echo "STATIC_LIBS ="
371 test "x$shared" = xno && echo "SHARED_LIBS ="
372 test "x$wrapper" = xno && echo "ALL_TOOLS ="
373 test "x$wrapper" = xno && echo "TOOL_LIBS ="
374 exec 1>&3 3>&-
375
376 printf "done\n"