configure (6244B)
1 #!/bin/sh 2 # Based on the configure script from musl libc, MIT licensed 3 4 usage () { 5 cat <<EOF 6 Usage: $0 [OPTION]... [VAR=VALUE]... 7 8 To assign environment variables (e.g., CC, CFLAGS...), specify them as 9 VAR=VALUE. See below for descriptions of some of the useful variables. 10 11 Defaults for the options are specified in brackets. 12 13 Configuration: 14 --srcdir=DIR source directory [detected] 15 16 Installation directories: 17 --prefix=PREFIX main installation prefix [/usr/local] 18 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX] 19 20 Fine tuning of the installation directories: 21 --bindir=DIR user executables [EPREFIX/bin] 22 --sharedir=DIR share directories [PREFIX/share] 23 --docdir=DIR misc. documentation [PREFIX/share/doc] 24 --mandir=DIR man pages [PREFIX/share/man] 25 26 Some influential environment variables: 27 CC C compiler command [detected] 28 CFLAGS C compiler flags [-Os -pipe ...] 29 LDFLAGS Linker flags 30 31 Use these variables to override the choices made by configure. 32 33 EOF 34 exit 0 35 } 36 37 # Helper functions 38 39 quote () { 40 tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; } 41 $1 42 EOF 43 printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#" 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 stripdir () { 52 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done 53 } 54 55 trycppif () { 56 printf "checking preprocessor condition %s... " "$1" 57 echo "typedef int x;" > "$tmpc" 58 echo "#if $1" >> "$tmpc" 59 echo "#error yes" >> "$tmpc" 60 echo "#endif" >> "$tmpc" 61 if $CC $2 -c -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then 62 printf "false\n" 63 return 1 64 else 65 printf "true\n" 66 return 0 67 fi 68 } 69 70 tryflag () { 71 printf "checking whether compiler accepts %s... " "$2" 72 echo "typedef int x;" > "$tmpc" 73 if $CC $CFLAGS_TRY $2 -c -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then 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 84 tryldflag () { 85 printf "checking whether linker accepts %s... " "$2" 86 echo "typedef int x;" > "$tmpc" 87 if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then 88 printf "yes\n" 89 eval "$1=\"\${$1} \$2\"" 90 eval "$1=\${$1# }" 91 return 0 92 else 93 printf "no\n" 94 return 1 95 fi 96 } 97 98 # Beginning of actual script 99 100 CFLAGS_AUTO= 101 CFLAGS_TRY= 102 LDFLAGS_AUTO= 103 LDFLAGS_TRY= 104 SRCDIR= 105 PREFIX=/usr/local 106 EXEC_PREFIX='$(PREFIX)' 107 BINDIR='$(EXEC_PREFIX)/bin' 108 MANDIR='$(PREFIX)/share/man' 109 110 for arg ; do 111 case "$arg" in 112 --help|-h) usage ;; 113 --srcdir=*) SRCDIR=${arg#*=} ;; 114 --prefix=*) PREFIX=${arg#*=} ;; 115 --exec-prefix=*) EXEC_PREFIX=${arg#*=} ;; 116 --bindir=*) BINDIR=${arg#*=} ;; 117 --sharedir=*) SHAREDIR=${arg#*=} ;; 118 --docdir=*) DOCDIR=${arg#*=} ;; 119 --mandir=*) MANDIR=${arg#*=} ;; 120 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;; 121 -* ) echo "$0: unknown option $arg" ;; 122 CC=*) CC=${arg#*=} ;; 123 CFLAGS=*) CFLAGS=${arg#*=} ;; 124 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;; 125 LDFLAGS=*) LDFLAGS=${arg#*=} ;; 126 *=*) ;; 127 *) ;; 128 esac 129 done 130 131 for i in SRCDIR PREFIX EXEC_PREFIX BINDIR MANDIR ; do 132 stripdir $i 133 done 134 135 # 136 # Get the source dir for out-of-tree builds 137 # 138 if test -z "$SRCDIR" ; then 139 SRCDIR="${0%/configure}" 140 stripdir SRCDIR 141 fi 142 abs_builddir="$(pwd)" || fail "$0: cannot determine working directory" 143 abs_srcdir="$(cd $SRCDIR && pwd)" || fail "$0: invalid source directory $SRCDIR" 144 test "$abs_srcdir" = "$abs_builddir" && SRCDIR=. 145 test "$SRCDIR" != "." -a -f Makefile -a ! -h Makefile && fail "$0: Makefile already exists in the working directory" 146 147 # 148 # Get a temp filename we can use 149 # 150 i=0 151 set -C 152 while : ; do i=$(($i+1)) 153 tmpc="./conf$$-$PPID-$i.c" 154 tmpo="./conf$$-$PPID-$i.o" 155 2>|/dev/null > "$tmpc" && break 156 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc" 157 done 158 set +C 159 trap 'rm -f "$tmpc" "$tmpo"' EXIT INT QUIT TERM HUP 160 161 # 162 # Find a C compiler to use 163 # 164 printf "checking for C compiler... " 165 trycc cc 166 trycc gcc 167 trycc clang 168 printf "%s\n" "$CC" 169 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; } 170 171 printf "checking whether C compiler works... " 172 echo "typedef int x;" > "$tmpc" 173 if output=$($CC $CPPFLAGS $CFLAGS -c -o "$tmpo" "$tmpc" 2>&1) ; then 174 printf "yes\n" 175 else 176 printf "no; compiler output follows:\n%s\n" "$output" 177 exit 1 178 fi 179 180 # 181 # Figure out options to force errors on unknown flags. 182 # 183 tryflag CFLAGS_TRY -Werror=unknown-warning-option 184 tryflag CFLAGS_TRY -Werror=unused-command-line-argument 185 tryldflag LDFLAGS_TRY -Werror=unknown-warning-option 186 tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument 187 188 CFLAGS_STD="-std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DNDEBUG -D_FORTIFY_SOURCE=2" 189 LDFLAGS_STD="-lc -lutil" 190 191 OS=$(uname) 192 193 case "$OS" in 194 FreeBSD) CFLAGS_STD="$CFLAGS_STD -D_BSD_SOURCE -D__BSD_VISIBLE=1" ;; 195 *BSD) CFLAGS_STD="$CFLAGS_STD -D_BSD_SOURCE" ;; 196 Darwin) CFLAGS_STD="$CFLAGS_STD -D_DARWIN_C_SOURCE" ;; 197 AIX) CFLAGS_STD="$CFLAGS_STD -D_ALL_SOURCE" ;; 198 esac 199 200 tryflag CFLAGS -pipe 201 202 # Try flags to optimize binary size 203 tryflag CFLAGS -Os 204 tryflag CFLAGS -ffunction-sections 205 tryflag CFLAGS -fdata-sections 206 tryldflag LDFLAGS_AUTO -Wl,--gc-sections 207 208 # Try hardening flags 209 tryflag CFLAGS -fPIE 210 tryflag CFLAGS_AUTO -fstack-protector-all 211 tryldflag LDFLAGS -Wl,-z,now 212 tryldflag LDFLAGS -Wl,-z,relro 213 tryldflag LDFLAGS_AUTO -pie 214 215 printf "creating config.mk... " 216 217 cmdline=$(quote "$0") 218 for i ; do cmdline="$cmdline $(quote "$i")" ; done 219 220 exec 3>&1 1>config.mk 221 222 cat << EOF 223 # This version of config.mk was generated by: 224 # $cmdline 225 # Any changes made here will be lost if configure is re-run 226 SRCDIR = $SRCDIR 227 PREFIX = $PREFIX 228 EXEC_PREFIX = $EXEC_PREFIX 229 BINDIR = $BINDIR 230 MANPREFIX = $MANDIR 231 CC = $CC 232 CFLAGS = $CFLAGS 233 LDFLAGS = $LDFLAGS 234 CFLAGS_STD = $CFLAGS_STD 235 LDFLAGS_STD = $LDFLAGS_STD 236 CFLAGS_AUTO = $CFLAGS_AUTO 237 LDFLAGS_AUTO = $LDFLAGS_AUTO 238 CFLAGS_DEBUG = -U_FORTIFY_SOURCE -UNDEBUG -O0 -g -ggdb -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-sign-compare 239 EOF 240 exec 1>&3 3>&- 241 242 printf "done\n" 243 244 test "$SRCDIR" = "." || ln -sf $SRCDIR/Makefile .