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 | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 37 | |
| 38 | Use these variables to override the choices made by configure. |
| 39 | |
| 40 | EOF |
| 41 | exit 0 |
| 42 | } |
| 43 | |
| 44 | # Helper functions |
| 45 | |
| 46 | echo () { printf "%s\n" "$*" ; } |
| 47 | fail () { echo "$*" ; exit 1 ; } |
| 48 | fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; } |
| 49 | cmdexists () { type "$1" >/dev/null 2>&1 ; } |
| 50 | trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; } |
| 51 | |
| 52 | setdir () { |
| 53 | if eval "test -z \"\${$1}\"" ; then eval "$1=\$2" |
| 54 | else eval "fnmatch '*/' \"\${$1}\"" && eval "$1=\${$1%/}" ; fi |
| 55 | } |
| 56 | |
| 57 | tryflag () { |
| 58 | printf "checking whether compiler accepts %s... " "$2" |
| 59 | echo "typedef int x;" > "$tmpc" |
| 60 | if "$CC" "$2" -c -o /dev/null "$tmpc" 2>/dev/null ; then |
| 61 | printf "yes\n" |
| 62 | eval "$1=\"\${$1} \$2\"" |
| 63 | eval "$1=\${$1# }" |
| 64 | return 0 |
| 65 | else |
| 66 | printf "no\n" |
| 67 | return 1 |
| 68 | fi |
| 69 | } |
| 70 | |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 71 | tryldflag () { |
| 72 | printf "checking whether linker accepts %s... " "$2" |
Rich Felker | 67a0383 | 2012-06-07 00:23:58 -0400 | [diff] [blame] | 73 | echo "typedef int x;" > "$tmpc" |
Rich Felker | f1fd757 | 2012-06-07 00:27:34 -0400 | [diff] [blame] | 74 | 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] | 75 | printf "yes\n" |
| 76 | eval "$1=\"\${$1} \$2\"" |
| 77 | eval "$1=\${$1# }" |
| 78 | return 0 |
| 79 | else |
| 80 | printf "no\n" |
| 81 | return 1 |
| 82 | fi |
| 83 | } |
| 84 | |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 85 | |
| 86 | |
| 87 | # Beginning of actual script |
| 88 | |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 89 | CFLAGS_C99FSE= |
| 90 | CFLAGS_AUTO= |
| 91 | LDFLAGS_AUTO= |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 92 | prefix= |
| 93 | exec_prefix= |
| 94 | bindir= |
| 95 | libdir= |
| 96 | includedir= |
| 97 | syslibdir= |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 98 | target= |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 99 | debug=no |
| 100 | warnings= |
| 101 | shared=yes |
| 102 | static=yes |
| 103 | |
| 104 | for arg ; do |
| 105 | case "$arg" in |
| 106 | --help) usage ;; |
| 107 | --prefix=*) prefix=${arg#*=} ;; |
| 108 | --exec-prefix=*) exec_prefix=${arg#*=} ;; |
| 109 | --bindir=*) bindir=${arg#*=} ;; |
| 110 | --libdir=*) libdir=${arg#*=} ;; |
| 111 | --includedir=*) includedir=${arg#*=} ;; |
| 112 | --syslibdir=*) syslibdir=${arg#*=} ;; |
| 113 | --enable-shared|--enable-shared=yes) shared=yes ;; |
| 114 | --disable-shared|--enable-shared=no) shared=no ;; |
| 115 | --enable-static|--enable-static=yes) static=yes ;; |
| 116 | --disable-static|--enable-static=no) static=no ;; |
| 117 | --enable-debug|--enable-debug=yes) debug=yes ;; |
| 118 | --disable-debug|--enable-debug=no) debug=no ;; |
| 119 | --enable-warnings|--enable-warnings=yes) warnings=yes ;; |
| 120 | --disable-warnings|--enable-warnings=no) warnings=no ;; |
| 121 | --enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;; |
| 122 | --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 123 | --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;; |
| 124 | --host=*|--target=*) target=${arg#*=} ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 125 | -* ) echo "$0: unknown option $arg" ;; |
| 126 | CC=*) CC=${arg#*=} ;; |
| 127 | CFLAGS=*) CFLAGS=${arg#*=} ;; |
| 128 | CPPFLAGS=*) CPPFLAGS=${arg#*=} ;; |
| 129 | LDFLAGS=*) LDFLAGS=${arg#*=} ;; |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 130 | CROSS_COMPILE=*) CROSS_COMPILE=${arg#*=} ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 131 | *=*) ;; |
| 132 | *) target=$arg ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 133 | esac |
| 134 | done |
| 135 | |
| 136 | setdir prefix /usr/local/musl |
| 137 | setdir exec_prefix '$(prefix)' |
| 138 | setdir bindir '$(exec_prefix)/bin' |
| 139 | setdir libdir '$(prefix)/lib' |
| 140 | setdir includedir '$(prefix)/include' |
| 141 | setdir syslibdir '/lib' |
| 142 | |
| 143 | # |
| 144 | # Get a temp filename we can use |
| 145 | # |
| 146 | i=0 |
| 147 | set -C |
| 148 | while : ; do i=$(($i+1)) |
| 149 | tmpc="./conf$$-$PPID-$i.c" |
| 150 | 2>/dev/null > "$tmpc" && break |
| 151 | test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc" |
| 152 | done |
| 153 | set +C |
| 154 | trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP |
| 155 | |
| 156 | # |
| 157 | # Find a C compiler to use |
| 158 | # |
| 159 | printf "checking for C compiler... " |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 160 | trycc ${CROSS_COMPILE}gcc |
| 161 | trycc ${CROSS_COMPILE}c99 |
| 162 | trycc ${CROSS_COMPILE}cc |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 163 | printf "%s\n" "$CC" |
| 164 | test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; } |
| 165 | |
| 166 | # |
| 167 | # Only build musl-gcc wrapper if toolchain does not already target musl |
| 168 | # |
| 169 | if test -z "$wrapper" ; then |
| 170 | printf "checking whether compiler is gcc... " |
Rich Felker | c5f3add | 2012-05-14 14:52:55 -0400 | [diff] [blame] | 171 | if fnmatch '*gcc\ version*' "$("$CC" -v 2>&1)" ; then |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 172 | echo yes |
| 173 | printf "checking whether to build musl-gcc wrapper... " |
| 174 | wrapper=yes |
| 175 | while read line ; do |
| 176 | case "$line" in */ld-musl-*) wrapper=no ;; esac |
| 177 | done <<EOF |
| 178 | $($CC -dumpspecs) |
| 179 | EOF |
| 180 | echo $wrapper |
| 181 | else |
| 182 | echo no |
| 183 | fi |
| 184 | fi |
| 185 | |
| 186 | |
| 187 | |
| 188 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 189 | # Find the target architecture |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 190 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 191 | printf "checking target system type... " |
| 192 | test -n "$target" || target=$("$CC" -dumpmachine 2>/dev/null) || target=unknown |
| 193 | printf "%s\n" "$target" |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 194 | |
| 195 | # |
| 196 | # Convert to just ARCH |
| 197 | # |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 198 | case "$target" in |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 199 | arm*) ARCH=arm ;; |
| 200 | i?86*) ARCH=i386 ;; |
| 201 | x86_64*) ARCH=x86_64 ;; |
Rich Felker | 721564a | 2012-08-05 17:23:38 -0400 | [diff] [blame] | 202 | mips-*|mipsel-*) ARCH=mips ;; |
Rich Felker | 278883d | 2012-06-03 16:22:13 -0400 | [diff] [blame] | 203 | unknown) fail "$0: unable to detect target arch; try $0 --target=..." ;; |
| 204 | *) fail "$0: unknown or unsupported target \"$target\"" ;; |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 205 | esac |
| 206 | |
| 207 | # |
| 208 | # Try to get a conforming C99 freestanding environment |
| 209 | # |
| 210 | tryflag CFLAGS_C99FSE -std=c99 |
| 211 | tryflag CFLAGS_C99FSE -nostdinc |
| 212 | tryflag CFLAGS_C99FSE -ffreestanding \ |
| 213 | || tryflag CFLAGS_C99FSE -fno-builtin |
| 214 | tryflag CFLAGS_C99FSE -fexcess-precision=standard \ |
Rich Felker | 2121b8a | 2012-07-03 23:53:05 -0400 | [diff] [blame] | 215 | || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; } |
Rich Felker | b4ccc3c | 2012-05-05 17:18:31 -0400 | [diff] [blame] | 216 | tryflag CFLAGS_C99FSE -frounding-math |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 217 | |
| 218 | # |
| 219 | # Setup basic default CFLAGS: debug, optimization, and -pipe |
| 220 | # |
| 221 | if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then : |
| 222 | else |
| 223 | tryflag CFLAGS_AUTO -Os || tryflag CFLAGS_AUTO -O2 |
| 224 | fi |
| 225 | test "x$debug" = xyes && CFLAGS_AUTO="-g" |
| 226 | tryflag CFLAGS_AUTO -pipe |
| 227 | |
| 228 | # |
| 229 | # If debugging is disabled, omit bloated DWARF2 unwind tables & frame ptr |
| 230 | # |
| 231 | if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" ; then : |
| 232 | else |
| 233 | tryflag CFLAGS_AUTO -fno-unwind-tables |
| 234 | tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables |
| 235 | tryflag CFLAGS_AUTO -fomit-frame-pointer |
| 236 | fi |
| 237 | |
| 238 | # |
| 239 | # Some optimization levels add bloated alignment that hurt performance |
| 240 | # |
| 241 | tryflag CFLAGS_AUTO -falign-functions=1 |
| 242 | tryflag CFLAGS_AUTO -falign-labels=1 |
| 243 | tryflag CFLAGS_AUTO -falign-loops=1 |
| 244 | tryflag CFLAGS_AUTO -falign-jumps=1 |
| 245 | |
| 246 | # |
| 247 | # On x86, make sure we don't have incompatible instruction set |
| 248 | # extensions enabled by default. This is bad for making static binaries. |
| 249 | # We cheat and use i486 rather than i386 because i386 really does not |
| 250 | # work anyway (issues with atomic ops). |
| 251 | # |
| 252 | if test "$ARCH" = "i386" ; then |
| 253 | fnmatch '-march=*|*\ -march=*' "$CFLAGS" || tryflag CFLAGS_AUTO -march=i486 |
| 254 | fnmatch '-mtune=*|*\ -mtune=*' "$CFLAGS" || tryflag CFLAGS_AUTO -mtune=generic |
| 255 | fi |
| 256 | |
| 257 | if test "x$warnings" = xyes ; then |
| 258 | tryflag CFLAGS_AUTO -Wall |
| 259 | tryflag CFLAGS_AUTO -Wpointer-arith |
| 260 | tryflag CFLAGS_AUTO -Wcast-align |
| 261 | tryflag CFLAGS_AUTO -Wno-parentheses |
| 262 | tryflag CFLAGS_AUTO -Wno-uninitialized |
| 263 | tryflag CFLAGS_AUTO -Wno-missing-braces |
| 264 | tryflag CFLAGS_AUTO -Wno-unused-value |
| 265 | tryflag CFLAGS_AUTO -Wno-unused-but-set-variable |
| 266 | tryflag CFLAGS_AUTO -Wno-unknown-pragmas |
| 267 | fi |
| 268 | |
Rich Felker | 0c5efde | 2012-06-06 22:00:08 -0400 | [diff] [blame] | 269 | # Some patched GCC builds have these defaults messed up... |
| 270 | tryflag CFLAGS_AUTO -fno-stack-protector |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 271 | tryldflag LDFLAGS_AUTO -Wl,--hash-style=sysv |
| 272 | |
Rich Felker | 498a100 | 2012-06-07 00:32:22 -0400 | [diff] [blame] | 273 | # Disable dynamic linking if ld is broken and can't do -Bsymbolic-functions |
| 274 | LDFLAGS_DUMMY= |
| 275 | tryldflag LDFLAGS_DUMMY -Wl,-Bsymbolic-functions || { |
| 276 | printf "warning: disabling dynamic linking support\n" |
| 277 | shared=no |
| 278 | } |
| 279 | |
Rich Felker | a1546e8 | 2012-07-12 14:24:10 -0400 | [diff] [blame] | 280 | |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 281 | |
| 282 | printf "creating config.mak... " |
| 283 | |
| 284 | exec 3>&1 1>config.mak |
| 285 | |
| 286 | |
| 287 | cat << EOF |
| 288 | # This version of config.mak was generated by configure |
| 289 | # Any changes made here will be lost if configure is re-run |
| 290 | ARCH = $ARCH |
| 291 | prefix = $prefix |
| 292 | exec_prefix = $exec_prefix |
| 293 | bindir = $bindir |
| 294 | libdir = $libdir |
| 295 | includedir = $includedir |
| 296 | syslibdir = $syslibdir |
| 297 | CC = $CC |
| 298 | CFLAGS= $CFLAGS_AUTO $CFLAGS |
| 299 | CFLAGS_C99FSE = $CFLAGS_C99FSE |
| 300 | CPPFLAGS = $CPPFLAGS |
Rich Felker | 08f70a3 | 2012-06-06 20:45:52 -0400 | [diff] [blame] | 301 | LDFLAGS = $LDFLAGS_AUTO $LDFLAGS |
Rich Felker | 94e920d | 2012-08-14 22:50:16 -0400 | [diff] [blame] | 302 | CROSS_COMPILE = $CROSS_COMPILE |
Rich Felker | 64d2f8e | 2012-05-04 23:24:51 -0400 | [diff] [blame] | 303 | EOF |
| 304 | test "x$static" = xno && echo "STATIC_LIBS =" |
| 305 | test "x$shared" = xno && echo "SHARED_LIBS =" |
| 306 | test "x$wrapper" = xno && echo "ALL_TOOLS =" |
| 307 | test "x$wrapper" = xno && echo "TOOL_LIBS =" |
| 308 | exec 1>&3 3>&- |
| 309 | |
| 310 | printf "done\n" |