Make test suite a bit more robust
[abduco.git] / configure
blob2dd43749d269bfa95ec958d148fc05c1f39981fc
1 #!/bin/sh
2 # Based on the configure script from musl libc, MIT licensed
4 usage () {
5 cat <<EOF
6 Usage: $0 [OPTION]... [VAR=VALUE]...
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.
11 Defaults for the options are specified in brackets.
13 Configuration:
14 --srcdir=DIR source directory [detected]
16 Installation directories:
17 --prefix=PREFIX main installation prefix [/usr/local]
18 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
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]
26 Some influential environment variables:
27 CC C compiler command [detected]
28 CFLAGS C compiler flags [-Os -pipe ...]
29 LDFLAGS Linker flags
31 Use these variables to override the choices made by configure.
33 EOF
34 exit 0
37 # Helper functions
39 quote () {
40 tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
42 EOF
43 printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
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 ; }
51 stripdir () {
52 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
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
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
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
98 # Beginning of actual script
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'
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
131 for i in SRCDIR PREFIX EXEC_PREFIX BINDIR MANDIR ; do
132 stripdir $i
133 done
136 # Get the source dir for out-of-tree builds
138 if test -z "$SRCDIR" ; then
139 SRCDIR="${0%/configure}"
140 stripdir SRCDIR
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"
148 # Get a temp filename we can use
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
162 # Find a C compiler to use
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 ; }
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
181 # Figure out options to force errors on unknown flags.
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
188 CFLAGS_STD="-std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DNDEBUG -D_FORTIFY_SOURCE=2"
189 LDFLAGS_STD="-lc -lutil"
191 OS=$(uname)
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
200 tryflag CFLAGS -pipe
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
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
215 printf "creating config.mk... "
217 cmdline=$(quote "$0")
218 for i ; do cmdline="$cmdline $(quote "$i")" ; done
220 exec 3>&1 1>config.mk
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
240 exec 1>&3 3>&-
242 printf "done\n"
244 test "$SRCDIR" = "." || ln -sf $SRCDIR/Makefile .