use EXIT_FAILURE for exit status
[vis.git] / configure
blobb100b48710dfd86e275be8a75ed347b2f676ae08
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]
15 --environment-only check environment only, no system libraries
16 --static prepare for static build
18 Installation directories:
19 --prefix=PREFIX main installation prefix [/usr/local]
20 --exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
22 Fine tuning of the installation directories:
23 --bindir=DIR user executables [EPREFIX/bin]
24 --sharedir=DIR share directories [PREFIX/share]
25 --mandir=DIR man pages [PREFIX/share/man]
27 Optional features:
28 --enable-lua build with Lua support [auto]
29 --enable-selinux build with SELinux support [auto]
30 --enable-acl build with POSIX ACL support [auto]
32 Some influential environment variables:
33 CC C compiler command [detected]
34 CFLAGS C compiler flags [-Os -pipe ...]
35 LDFLAGS Linker flags
37 Use these variables to override the choices made by configure.
39 EOF
40 exit 0
43 # Helper functions
45 quote () {
46 tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
48 EOF
49 printf %s\\n "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#"
51 echo () { printf "%s\n" "$*" ; }
52 fail () { echo "$*" ; exit 1 ; }
53 fnmatch () { eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac" ; }
54 cmdexists () { type "$1" >/dev/null 2>&1 ; }
55 trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
57 stripdir () {
58 while eval "fnmatch '*/' \"\${$1}\"" ; do eval "$1=\${$1%/}" ; done
61 trycppif () {
62 printf "checking preprocessor condition %s... " "$1"
63 echo "typedef int x;" > "$tmpc"
64 echo "#if $1" >> "$tmpc"
65 echo "#error yes" >> "$tmpc"
66 echo "#endif" >> "$tmpc"
67 if $CC $2 -c -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then
68 printf "false\n"
69 return 1
70 else
71 printf "true\n"
72 return 0
76 tryflag () {
77 printf "checking whether compiler accepts %s... " "$2"
78 echo "typedef int x;" > "$tmpc"
79 if $CC $CFLAGS_TRY $2 -c -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then
80 printf "yes\n"
81 eval "$1=\"\${$1} \$2\""
82 eval "$1=\${$1# }"
83 return 0
84 else
85 printf "no\n"
86 return 1
90 tryldflag () {
91 printf "checking whether linker accepts %s... " "$2"
92 echo "typedef int x;" > "$tmpc"
93 if $CC $LDFLAGS_TRY -nostdlib -shared "$2" -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then
94 printf "yes\n"
95 eval "$1=\"\${$1} \$2\""
96 eval "$1=\${$1# }"
97 return 0
98 else
99 printf "no\n"
100 return 1
104 # Beginning of actual script
106 CFLAGS_AUTO=
107 CFLAGS_TRY=
108 LDFLAGS_AUTO=
109 LDFLAGS_TRY=
110 SRCDIR=
111 PREFIX=/usr/local
112 EXEC_PREFIX='$(PREFIX)'
113 BINDIR='$(EXEC_PREFIX)/bin'
114 SHAREDIR='$(PREFIX)/share'
115 MANDIR='$(PREFIX)/share/man'
117 lua=auto
118 selinux=auto
119 acl=auto
121 for arg ; do
122 case "$arg" in
123 --help|-h) usage ;;
124 --srcdir=*) SRCDIR=${arg#*=} ;;
125 --prefix=*) PREFIX=${arg#*=} ;;
126 --exec-prefix=*) EXEC_PREFIX=${arg#*=} ;;
127 --bindir=*) BINDIR=${arg#*=} ;;
128 --sharedir=*) SHAREDIR=${arg#*=} ;;
129 --mandir=*) MANDIR=${arg#*=} ;;
130 --environment-only) environmentonly=yes ;;
131 --static) static=yes ;;
132 --enable-lua|--enable-lua=yes) lua=yes ;;
133 --disable-lua|--enable-lua=no) lua=no ;;
134 --enable-selinux|--enable-selinux=yes) selinux=yes ;;
135 --disable-selinux|--enable-selinux=no) selinux=no ;;
136 --enable-acl|--enable-acl=yes) acl=yes ;;
137 --disable-acl|--enable-acl=no) acl=no ;;
138 --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
139 -* ) echo "$0: unknown option $arg" ;;
140 CC=*) CC=${arg#*=} ;;
141 CFLAGS=*) CFLAGS=${arg#*=} ;;
142 CPPFLAGS=*) CPPFLAGS=${arg#*=} ;;
143 LDFLAGS=*) LDFLAGS=${arg#*=} ;;
144 *=*) ;;
145 *) ;;
146 esac
147 done
149 for i in SRCDIR PREFIX EXEC_PREFIX BINDIR SHAREDIR MANDIR ; do
150 stripdir $i
151 done
154 # Get the source dir for out-of-tree builds
156 if test -z "$SRCDIR" ; then
157 SRCDIR="${0%/configure}"
158 stripdir SRCDIR
160 abs_builddir="$(pwd)" || fail "$0: cannot determine working directory"
161 abs_srcdir="$(cd $SRCDIR && pwd)" || fail "$0: invalid source directory $SRCDIR"
162 test "$abs_srcdir" = "$abs_builddir" && SRCDIR=.
163 test "$SRCDIR" != "." -a -f Makefile -a ! -h Makefile && fail "$0: Makefile already exists in the working directory"
166 # Get a temp filename we can use
169 set -C
170 while : ; do i=$(($i+1))
171 tmpc="./conf$$-$PPID-$i.c"
172 tmpo="./conf$$-$PPID-$i.o"
173 2>|/dev/null > "$tmpc" && break
174 test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
175 done
176 set +C
177 trap 'rm -f "$tmpc" "$tmpo"' EXIT INT QUIT TERM HUP
180 # Find a C compiler to use
182 printf "checking for C compiler... "
183 trycc cc
184 trycc gcc
185 trycc clang
186 printf "%s\n" "$CC"
187 test -n "$CC" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
189 printf "checking whether C compiler works... "
190 echo "typedef int x;" > "$tmpc"
191 if output=$($CC $CPPFLAGS $CFLAGS -c -o "$tmpo" "$tmpc" 2>&1) ; then
192 printf "yes\n"
193 else
194 printf "no; compiler output follows:\n%s\n" "$output"
195 exit 1
199 # Figure out options to force errors on unknown flags.
201 tryflag CFLAGS_TRY -Werror=unknown-warning-option
202 tryflag CFLAGS_TRY -Werror=unused-command-line-argument
203 tryldflag LDFLAGS_TRY -Werror=unknown-warning-option
204 tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
206 CFLAGS_STD="-std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DNDEBUG -D_FORTIFY_SOURCE=2"
207 LDFLAGS_STD="-lc"
209 OS=$(uname)
211 case "$OS" in
212 FreeBSD) CFLAGS_STD="$CFLAGS_STD -D_BSD_SOURCE -D__BSD_VISIBLE=1" ;;
213 *BSD) CFLAGS_STD="$CFLAGS_STD -D_BSD_SOURCE" ;;
214 Darwin) CFLAGS_STD="$CFLAGS_STD -D_DARWIN_C_SOURCE" ;;
215 AIX) CFLAGS_STD="$CFLAGS_STD -D_ALL_SOURCE" ;;
216 esac
218 tryflag CFLAGS -pipe
220 # Try flags to optimize binary size
221 tryflag CFLAGS -Os
222 #tryflag CFLAGS -ffunction-sections
223 #tryflag CFLAGS -fdata-sections
224 #tryldflag LDFLAGS_AUTO -Wl,--gc-sections
226 # Try hardening flags
227 tryflag CFLAGS -fPIE
228 tryflag CFLAGS_AUTO -fstack-protector-all
229 tryldflag LDFLAGS -Wl,-z,now
230 tryldflag LDFLAGS -Wl,-z,relro
231 # in theory it should be perfectly fine to produce a staticically linked PIE
232 # however in practice it is not yet properly supported by gcc:
234 # cc -fPIE -pie --static
236 # will always add a PT_INTERP referencing the dynamic loader/linker
237 if test "$static" != "yes" || tryldflag LDFLAGS_AUTO -Wl,--no-dynamic-linker ; then
238 tryldflag LDFLAGS_AUTO -pie
241 printf "creating config.mk... "
243 cmdline=$(quote "$0")
244 for i ; do cmdline="$cmdline $(quote "$i")" ; done
246 exec 3>&1 1>config.mk
248 cat << EOF
249 # This version of config.mk was generated by:
250 # $cmdline
251 # Any changes made here will be lost if configure is re-run
252 SRCDIR = $SRCDIR
253 PREFIX = $PREFIX
254 EXEC_PREFIX = $EXEC_PREFIX
255 BINDIR = $BINDIR
256 MANPREFIX = $MANDIR
257 SHAREPREFIX = $SHAREDIR
258 CC = $CC
259 CFLAGS = $CFLAGS
260 LDFLAGS = $LDFLAGS
261 CFLAGS_STD = $CFLAGS_STD
262 LDFLAGS_STD = $LDFLAGS_STD
263 CFLAGS_AUTO = $CFLAGS_AUTO
264 LDFLAGS_AUTO = $LDFLAGS_AUTO
266 exec 1>&3 3>&-
268 printf "done\n"
270 if test "$environmentonly" = "yes"; then
271 exit 0
274 have_pkgconfig=no
275 printf "checking for pkg-config... "
276 cmdexists pkg-config && have_pkgconfig=yes
277 printf "%s\n" "$have_pkgconfig"
279 # libcurses is a mandatory dependency
281 printf "checking for libcurses...\n"
282 cat > "$tmpc" <<EOF
283 #include <curses.h>
285 int main(int argc, char *argv[]) {
286 initscr();
287 endwin();
288 return 0;
292 CONFIG_CURSES=0
294 for curses in ncursesw ncurses curses; do
295 printf " checking for %s... " "$curses"
297 if test "$have_pkgconfig" = "yes" ; then
298 CFLAGS_CURSES=$(pkg-config --cflags $curses 2>/dev/null)
299 LDFLAGS_CURSES=$(pkg-config --libs $curses 2>/dev/null)
300 if test $? -eq 0 && $CC $CFLAGS $CFLAGS_CURSES "$tmpc" \
301 $LDFLAGS $LDFLAGS_CURSES -o "$tmpo" >/dev/null 2>&1 ; then
302 CONFIG_CURSES=1
303 printf "yes\n"
304 break
308 CFLAGS_CURSES=""
309 LDFLAGS_CURSES="-l$curses"
311 if $CC $CFLAGS $CFLAGS_CURSES "$tmpc" \
312 $LDFLAGS $LDFLAGS_CURSES -o "$tmpo" >/dev/null 2>&1 ; then
313 CONFIG_CURSES=1
314 printf "yes\n"
315 break
316 else
317 printf "no\n"
319 done
321 test $CONFIG_CURSES -ne 1 && fail "$0: cannot find libcurses"
323 # libtermkey is a mandatory dependency
325 printf "checking for libtermkey... "
326 cat > "$tmpc" <<EOF
327 #include <termkey.h>
329 int main(int argc, char *argv[]) {
330 TERMKEY_CHECK_VERSION;
331 return 0;
335 if test "$have_pkgconfig" = "yes" ; then
336 CFLAGS_TERMKEY=$(pkg-config --cflags termkey 2>/dev/null)
337 LDFLAGS_TERMKEY=$(pkg-config --libs termkey 2>/dev/null)
340 if test -z "$LDFLAGS_TERMKEY"; then
341 CFLAGS_TERMKEY=""
342 LDFLAGS_TERMKEY="-ltermkey"
345 if $CC $CFLAGS $CFLAGS_TERMKEY "$tmpc" $LDFLAGS $LDFLAGS_TERMKEY $LDFLAGS_CURSES \
346 -o "$tmpo" >/dev/null 2>&1; then
347 printf "%s\n" "yes"
348 else
349 printf "%s\n" "no"
350 fail "$0: cannot find libtermkey"
353 CONFIG_LUA=0
355 if test "$lua" != "no" ; then
357 printf "checking for liblua >= 5.2 ...\n"
359 cat > "$tmpc" <<EOF
360 #include <lua.h>
361 #include <lauxlib.h>
363 #if LUA_VERSION_NUM < 502
364 #error "Need at least Lua 5.2"
365 #endif
367 int main(int argc, char *argv[]) {
368 lua_State *L = luaL_newstate();
369 luaL_openlibs(L);
370 lua_close(L);
371 return 0;
375 for liblua in lua lua5.2 lua5.3 lua-5.2 lua-5.3 lua52 lua53; do
376 printf " checking for %s... " "$liblua"
378 if test "$have_pkgconfig" = "yes" ; then
379 CFLAGS_LUA=$(pkg-config --cflags $liblua 2>/dev/null)
380 LDFLAGS_LUA=$(pkg-config --libs $liblua 2>/dev/null)
381 if test $? -eq 0 && $CC $CFLAGS $CFLAGS_LUA "$tmpc" \
382 $LDFLAGS $LDFLAGS_LUA -o "$tmpo" >/dev/null 2>&1 ; then
383 CONFIG_LUA=1
384 printf "yes\n"
385 break
389 CFLAGS_LUA=""
390 LDFLAGS_LUA="-l$liblua -lm -ldl"
392 if $CC $CFLAGS $CFLAGS_LUA "$tmpc" \
393 $LDFLAGS $LDFLAGS_LUA -o "$tmpo" >/dev/null 2>&1 ; then
394 CONFIG_LUA=1
395 printf "yes\n"
396 break
397 else
398 printf "no\n"
399 CFLAGS_LUA=""
400 LDFLAGS_LUA=""
402 done
404 test "$lua" = "yes" -a $CONFIG_LUA -ne 1 && fail "$0: cannot find liblua"
406 if test $CONFIG_LUA -eq 1; then
407 CFLAGS_LUA="$CFLAGS_LUA -DLUA_COMPAT_5_1 -DLUA_COMPAT_5_2 -DLUA_COMPAT_ALL"
411 CONFIG_ACL=0
413 if test "$OS" = "Linux" -a "$acl" != "no"; then
414 printf "checking for libacl... "
416 cat > "$tmpc" <<EOF
417 #include <sys/types.h>
418 #include <sys/acl.h>
420 int main(int argc, char *argv[]) {
421 acl_t acl = acl_get_fd(0);
422 return 0;
426 if test "$have_pkgconfig" = "yes" ; then
427 CFLAGS_ACL=$(pkg-config --cflags acl 2>/dev/null)
428 LDFLAGS_ACL=$(pkg-config --libs acl 2>/dev/null)
431 if test -z "$LDFLAGS_ACL"; then
432 CFLAGS_ACL=""
433 LDFLAGS_ACL="-lacl"
436 if $CC $CFLAGS $CFLAGS_ACL "$tmpc" \
437 $LDFLAGS $LDFLAGS_ACL -o "$tmpo" >/dev/null 2>&1; then
438 CONFIG_ACL=1
439 printf "%s\n" "yes"
440 else
441 printf "%s\n" "no"
442 CFLAGS_ACL=""
443 LDFLAGS_ACL=""
444 test "$acl" = "yes" && fail "$0: cannot find libacl"
448 CONFIG_SELINUX=0
450 if test "$OS" = "Linux" -a "$selinux" != "no"; then
451 printf "checking for libselinux... "
453 cat > "$tmpc" <<EOF
454 #include <selinux/selinux.h>
456 int main(int argc, char *argv[]) {
457 return is_selinux_enabled();
461 if test "$have_pkgconfig" = "yes" ; then
462 CFLAGS_SELINUX=$(pkg-config --cflags selinux 2>/dev/null)
463 LDFLAGS_SELINUX=$(pkg-config --libs selinux 2>/dev/null)
466 if test -z "$LDFLAGS_SELINUX"; then
467 CFLAGS_SELINUX=""
468 LDFLAGS_SELINUX="-lselinux"
471 if $CC $CFLAGS $CFLAGS_SELINUX "$tmpc" \
472 $LDFLAGS $LDFLAGS_SELINUX -o "$tmpo" >/dev/null 2>&1; then
473 CONFIG_SELINUX=1
474 printf "%s\n" "yes"
475 else
476 printf "%s\n" "no"
477 CFLAGS_SELINUX=""
478 LDFLAGS_SELINUX=""
479 test "$selinux" = "yes" && fail "$0: cannot find libselinux"
483 printf "completing config.mk... "
485 exec 3>&1 1>>config.mk
487 cat << EOF
488 CFLAGS_CURSES = $CFLAGS_CURSES
489 LDFLAGS_CURSES = $LDFLAGS_CURSES
490 CFLAGS_TERMKEY = $CFLAGS_TERMKEY
491 LDFLAGS_TERMKEY = $LDFLAGS_TERMKEY
492 CONFIG_LUA = $CONFIG_LUA
493 CFLAGS_LUA = $CFLAGS_LUA
494 LDFLAGS_LUA = $LDFLAGS_LUA
495 CONFIG_ACL = $CONFIG_ACL
496 CFLAGS_ACL = $CFLAGS_ACL
497 LDFLAGS_ACL = $LDFLAGS_ACL
498 CONFIG_SELINUX = $CONFIG_SELINUX
499 CFLAGS_SELINUX = $CFLAGS_SELINUX
500 LDFLAGS_SELINUX = $LDFLAGS_SELINUX
502 exec 1>&3 3>&-
504 test "$SRCDIR" = "." || ln -sf $SRCDIR/Makefile .
506 printf "done\n"