Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | usage () { |
| 4 | cat <<EOF |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 5 | Usage: $0 [OPTION]... [VAR=VALUE]... [TARGET] |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 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] |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 24 | --host=HOST same as --target |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 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 ...] |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 36 | CROSS_COMPILE prefix for cross compiler and tools [none] |
Rich Felker | 2c1cd23 | 2012-09-10 15:30:52 -0400 | [diff] [blame] | 37 | LIBCC compiler runtime library [detected] |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 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 | setdir () { |
| 54 | if eval "test -z \"\${$1}\"" ; then eval "$1=\$2" |
| 55 | else eval "fnmatch '*/' \"\${$1}\"" && eval "$1=\${$1%/}" ; fi |
| 56 | } |
| 57 | |
| 58 | tryflag () { |
| 59 | printf "checking whether compiler accepts %s... " "$2" |
| 60 | echo "typedef int x;" > "$tmpc" |
Rich Felker | 01e5a1b | 2012-10-18 23:02:53 -0400 | [diff] [blame] | 61 | if $CC "$2" -c -o /dev/null "$tmpc" 2>/dev/null ; then |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 62 | printf "yes\n" |
| 63 | eval "$1=\"\${$1} \$2\"" |
| 64 | eval "$1=\${$1# }" |
| 65 | return 0 |
| 66 | else |
| 67 | printf "no\n" |
| 68 | return 1 |
| 69 | fi |
| 70 | } |
| 71 | |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 72 | tryldflag () { |
| 73 | printf "checking whether linker accepts %s... " "$2" |
Rich Felker | 67a0383 | 2012-06-07 00:23:58 -0400 | [diff] [blame] | 74 | echo "typedef int x;" > "$tmpc" |
Rich Felker | 01e5a1b | 2012-10-18 23:02:53 -0400 | [diff] [blame] | 75 | if $CC -nostdlib -shared "$2" -o /dev/null "$tmpc" 2>/dev/null ; then |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 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 | |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 86 | |
| 87 | |
| 88 | # Beginning of actual script |
| 89 | |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 90 | CFLAGS_C99FSE= |
| 91 | CFLAGS_AUTO= |
| 92 | LDFLAGS_AUTO= |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 93 | prefix= |
| 94 | exec_prefix= |
| 95 | bindir= |
| 96 | libdir= |
| 97 | includedir= |
| 98 | syslibdir= |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 99 | target= |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 100 | debug=no |
| 101 | warnings= |
| 102 | shared=yes |
| 103 | static=yes |
| 104 | |
| 105 | for arg ; do |
| 106 | case "$arg" in |
| 107 | --help) usage ;; |
| 108 | --prefix=*) prefix=${arg#*=} ;; |
| 109 | --exec-prefix=*) exec_prefix=${arg#*=} ;; |
| 110 | --bindir=*) bindir=${arg#*=} ;; |
| 111 | --libdir=*) libdir=${arg#*=} ;; |
| 112 | --includedir=*) includedir=${arg#*=} ;; |
| 113 | --syslibdir=*) syslibdir=${arg#*=} ;; |
| 114 | --enable-shared|--enable-shared=yes) shared=yes ;; |
| 115 | --disable-shared|--enable-shared=no) shared=no ;; |
| 116 | --enable-static|--enable-static=yes) static=yes ;; |
| 117 | --disable-static|--enable-static=no) static=no ;; |
| 118 | --enable-debug|--enable-debug=yes) debug=yes ;; |
| 119 | --disable-debug|--enable-debug=no) debug=no ;; |
| 120 | --enable-warnings|--enable-warnings=yes) warnings=yes ;; |
| 121 | --disable-warnings|--enable-warnings=no) warnings=no ;; |
| 122 | --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;; |
| 123 | --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 124 | --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;; |
| 125 | --host=*|--target=*) target=${arg#*=} ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 126 | -* ) echo "$0: unknown option $arg" ;; |
| 127 | CC=*) CC=${arg#*=} ;; |
| 128 | CFLAGS=*) CFLAGS=${arg#*=} ;; |
| 129 | CPPFLAGS=*) CPPFLAGS=${arg#*=} ;; |
| 130 | LDFLAGS=*) LDFLAGS=${arg#*=} ;; |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 131 | CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;; |
Rich Felker | 2c1cd23 | 2012-09-10 15:30:52 -0400 | [diff] [blame] | 132 | LIBCC=*) LIBCC=${arg#*=} ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 133 | *=*) ;; |
| 134 | *) target=$arg ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 135 | esac |
| 136 | done |
| 137 | |
| 138 | setdir prefix /usr/local/musl |
| 139 | setdir exec_prefix '$(prefix)' |
| 140 | setdir bindir '$(exec_prefix)/bin' |
| 141 | setdir libdir '$(prefix)/lib' |
| 142 | setdir includedir '$(prefix)/include' |
| 143 | setdir syslibdir '/lib' |
| 144 | |
| 145 | # |
| 146 | # Get a temp filename we can use |
| 147 | # |
| 148 | i=0 |
| 149 | set -C |
| 150 | while : ; do i=$(($i+1)) |
| 151 | tmpc="./conf$$-$PPID-$i.c" |
| 152 | 2>/dev/null > "$tmpc" && break |
| 153 | test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc" |
| 154 | done |
| 155 | set +C |
| 156 | trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP |
| 157 | |
| 158 | # |
| 159 | # Find a C compiler to use |
| 160 | # |
| 161 | printf "checking for C compiler... " |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 162 | trycc ${CROSS_COMPILE}gcc |
| 163 | trycc ${CROSS_COMPILE}c99 |
| 164 | trycc ${CROSS_COMPILE}cc |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 165 | printf "%s\n" "$CC" |
| 166 | test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; } |
| 167 | |
| 168 | # |
| 169 | # Only build musl-gcc wrapper if toolchain does not already target musl |
| 170 | # |
| 171 | if test -z "$wrapper" ; then |
| 172 | printf "checking whether compiler is gcc... " |
Rich Felker | 01e5a1b | 2012-10-18 23:02:53 -0400 | [diff] [blame] | 173 | if fnmatch '*gcc\ version*' "$($CC -v 2>&1)" ; then |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 174 | echo yes |
| 175 | printf "checking whether to build musl-gcc wrapper... " |
| 176 | wrapper=yes |
| 177 | while read line ; do |
| 178 | case "$line" in */ld-musl-*) wrapper=no ;; esac |
| 179 | done <<EOF |
| 180 | $($CC -dumpspecs) |
| 181 | EOF |
| 182 | echo $wrapper |
| 183 | else |
| 184 | echo no |
| 185 | fi |
| 186 | fi |
| 187 | |
| 188 | |
| 189 | |
| 190 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 191 | # Find the target architecture |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 192 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 193 | printf "checking target system type... " |
Rich Felker | 01e5a1b | 2012-10-18 23:02:53 -0400 | [diff] [blame] | 194 | test -n "$target" || target=$($CC -dumpmachine 2>/dev/null) || target=unknown |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 195 | printf "%s\n" "$target" |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 196 | |
| 197 | # |
| 198 | # Convert to just ARCH |
| 199 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 200 | case "$target" in |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 201 | arm*) ARCH=arm ;; |
| 202 | i?86*) ARCH=i386 ;; |
| 203 | x86_64*) ARCH=x86_64 ;; |
Rich Felker | 721564a | 2012-08-05 17:23:38 -0400 | [diff] [blame] | 204 | mips-*|mipsel-*) ARCH=mips ;; |
Rich Felker | 8c0a3d9 | 2012-09-29 01:05:31 -0400 | [diff] [blame] | 205 | microblaze-*) ARCH=microblaze ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 206 | unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;; |
| 207 | *) fail "$0: unknown or unsupported target \"$target\"" ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 208 | esac |
| 209 | |
| 210 | # |
| 211 | # Try to get a conforming C99 freestanding environment |
| 212 | # |
| 213 | tryflag CFLAGS_C99FSE -std=c99 |
| 214 | tryflag CFLAGS_C99FSE -nostdinc |
| 215 | tryflag CFLAGS_C99FSE -ffreestanding \ |
| 216 | || tryflag CFLAGS_C99FSE -fno-builtin |
| 217 | tryflag CFLAGS_C99FSE -fexcess-precision=standard \ |
Rich Felker | 2121b8a | 2012-07-03 23:53:05 -0400 | [diff] [blame] | 218 | || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; } |
Rich Felker | b4ccc3c | 2012-05-05 17:18:31 -0400 | [diff] [blame] | 219 | tryflag CFLAGS_C99FSE -frounding-math |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 220 | |
| 221 | # |
| 222 | # Setup basic default CFLAGS: debug, optimization, and -pipe |
| 223 | # |
| 224 | if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then : |
| 225 | else |
| 226 | tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2 |
| 227 | fi |
| 228 | test "x$debug" = xyes && CFLAGS_AUTO="-g" |
| 229 | tryflag CFLAGS_AUTO -pipe |
| 230 | |
| 231 | # |
Rich Felker | b439c05 | 2012-08-29 09:36:02 -0400 | [diff] [blame] | 232 | # If debugging is disabled, omit frame pointer. Modern GCC does this |
| 233 | # anyway on most archs even when debugging is enabled since the frame |
| 234 | # pointer is no longer needed for debugging. |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 235 | # |
| 236 | if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then : |
| 237 | else |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 238 | tryflag CFLAGS_AUTO -fomit-frame-pointer |
| 239 | fi |
| 240 | |
| 241 | # |
Rich Felker | b439c05 | 2012-08-29 09:36:02 -0400 | [diff] [blame] | 242 | # Modern GCC wants to put DWARF tables (used for debugging and |
| 243 | # unwinding) in the loaded part of the program where they are |
| 244 | # unstrippable. These options force them back to debug sections (and |
| 245 | # cause them not to get generated at all if debugging is off). |
| 246 | # |
| 247 | tryflag CFLAGS_AUTO -fno-unwind-tables |
| 248 | tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables |
| 249 | |
| 250 | # |
Rich Felker | adefe83 | 2012-10-03 11:49:58 -0400 | [diff] [blame] | 251 | # The GNU toolchain defaults to assuming unmarked files need an |
| 252 | # executable stack, potentially exposing vulnerabilities in programs |
| 253 | # linked with such object files. Fix this. |
| 254 | # |
| 255 | tryflag CFLAGS_AUTO -Wa,--noexecstack |
| 256 | |
| 257 | # |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 258 | # Some optimization levels add bloated alignment that hurt performance |
| 259 | # |
| 260 | tryflag CFLAGS_AUTO -falign-functions=1 |
| 261 | tryflag CFLAGS_AUTO -falign-labels=1 |
| 262 | tryflag CFLAGS_AUTO -falign-loops=1 |
| 263 | tryflag CFLAGS_AUTO -falign-jumps=1 |
| 264 | |
| 265 | # |
| 266 | # On x86, make sure we don't have incompatible instruction set |
| 267 | # extensions enabled by default. This is bad for making static binaries. |
| 268 | # We cheat and use i486 rather than i386 because i386 really does not |
| 269 | # work anyway (issues with atomic ops). |
| 270 | # |
| 271 | if test "$ARCH" = "i386" ; then |
Rich Felker | 80a4545 | 2012-10-25 14:52:12 -0400 | [diff] [blame] | 272 | fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -march=i486 |
| 273 | fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryldflag CFLAGS_AUTO -mtune=generic |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 274 | fi |
| 275 | |
| 276 | if test "x$warnings" = xyes ; then |
| 277 | tryflag CFLAGS_AUTO -Wall |
| 278 | tryflag CFLAGS_AUTO -Wpointer-arith |
| 279 | tryflag CFLAGS_AUTO -Wcast-align |
| 280 | tryflag CFLAGS_AUTO -Wno-parentheses |
| 281 | tryflag CFLAGS_AUTO -Wno-uninitialized |
| 282 | tryflag CFLAGS_AUTO -Wno-missing-braces |
| 283 | tryflag CFLAGS_AUTO -Wno-unused-value |
| 284 | tryflag CFLAGS_AUTO -Wno-unused-but-set-variable |
| 285 | tryflag CFLAGS_AUTO -Wno-unknown-pragmas |
| 286 | fi |
| 287 | |
Rich Felker | 0c5efde | 2012-06-06 22:00:08 -0400 | [diff] [blame] | 288 | # Some patched GCC builds have these defaults messed up... |
| 289 | tryflag CFLAGS_AUTO -fno-stack-protector |
Rich Felker | 2bd05a4 | 2012-08-25 17:13:28 -0400 | [diff] [blame] | 290 | tryldflag LDFLAGS_AUTO -Wl,--hash-style=both |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 291 | |
Rich Felker | 498a100 | 2012-06-07 00:32:22 -0400 | [diff] [blame] | 292 | # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions |
| 293 | LDFLAGS_DUMMY= |
| 294 | tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || { |
| 295 | printf "warning: disabling dynamic linking support\n" |
| 296 | shared=no |
| 297 | } |
| 298 | |
Rich Felker | 2c1cd23 | 2012-09-10 15:30:52 -0400 | [diff] [blame] | 299 | # Find compiler runtime library |
| 300 | test -z "$LIBCC" && tryldflag LIBCC -lgcc && tryldflag LIBCC -lgcc_eh |
| 301 | test -z "$LIBCC" && tryldflag LIBCC -lcompiler_rt |
Rich Felker | 3d99266 | 2012-10-26 16:30:07 -0400 | [diff] [blame^] | 302 | test -z "$LIBCC" && tryldflag LIBCC -lpcc |
Rich Felker | 2c1cd23 | 2012-09-10 15:30:52 -0400 | [diff] [blame] | 303 | printf "using compiler runtime libraries: %s\n" "$LIBCC" |
Rich Felker | a1546e8 | 2012-07-12 14:24:10 -0400 | [diff] [blame] | 304 | |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 305 | |
| 306 | printf "creating config.mak... " |
| 307 | |
| 308 | exec 3>&1 1>config.mak |
| 309 | |
| 310 | |
| 311 | cat << EOF |
| 312 | # This version of config.mak was generated by configure |
| 313 | # Any changes made here will be lost if configure is re-run |
| 314 | ARCH = $ARCH |
| 315 | prefix = $prefix |
| 316 | exec_prefix = $exec_prefix |
| 317 | bindir = $bindir |
| 318 | libdir = $libdir |
| 319 | includedir = $includedir |
| 320 | syslibdir = $syslibdir |
| 321 | CC = $CC |
| 322 | CFLAGS= $CFLAGS_AUTO $CFLAGS |
| 323 | CFLAGS_C99FSE = $CFLAGS_C99FSE |
| 324 | CPPFLAGS = $CPPFLAGS |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 325 | LDFLAGS = $LDFLAGS_AUTO $LDFLAGS |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 326 | CROSS_COMPILE = $CROSS_COMPILE |
Rich Felker | 2c1cd23 | 2012-09-10 15:30:52 -0400 | [diff] [blame] | 327 | LIBCC = $LIBCC |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 328 | EOF |
| 329 | test "x$static" = xno && echo "STATIC_LIBS =" |
| 330 | test "x$shared" = xno && echo "SHARED_LIBS =" |
| 331 | test "x$wrapper" = xno && echo "ALL_TOOLS =" |
| 332 | test "x$wrapper" = xno && echo "TOOL_LIBS =" |
| 333 | exec 1>&3 3>&- |
| 334 | |
| 335 | printf "done\n" |