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