curses: ncurses may define curs_set outside of curses
[dhcpcd-ui] / configure
1 #!/bin/sh
2 # Try and be like autotools configure, but without autotools
3
4 echo "configure args: $*"
5 exec 3>config.log
6
7 # Ensure that we do not inherit these from env
8 DEBUG=
9 LIBDHCPCD=no
10 HOST=
11 HOSTCC=
12 BUILD=
13 WITH_DHCPCD_ONLINE=yes
14 WITH_CURSES=
15 WITH_GTK=
16 WITH_QT=
17 WITH_ICONS=
18 NOTIFY=
19 INCLUDEDIR=
20 UI=
21
22 for x; do
23         opt=${x%%=*}
24         var=${x#*=}
25         [ "$var" = "$opt" ] && var=
26         case "$opt" in
27         --os|OS) OS=$var;;
28         --with-cc|CC) CC=$var;;
29         --debug) DEBUG=$var;;
30         --disable-debug) DEBUG=no;;
31         --enable-debug) DEBUG=yes;;
32         --prefix) prefix=$var;;
33         --sysconfdir) SYSCONFDIR=$var;;
34         --bindir) BINDIR=$var;;
35         --libexecdir) LIBEXECDIR=$var;;
36         --statedir|--localstatedir) STATEDIR=$var;;
37         --rundir) RUNDIR=$var;;
38         --mandir) MANDIR=$var;;
39         --with-ccopts|CFLAGS) CFLAGS=$var;;
40         CPPFLAGS) CPPFLAGS=$var;;
41         --build) BUILD=$var;;
42         --host) HOST=$var; HOSTCC=$var-;;
43         --target) TARGET=$var;;
44         --libdir) LIBDIR=$var;;
45         --includedir) eval INCLUDEDIR="$INCLUDEDIR${INCLUDEDIR:+ }$var";;
46         --datadir|--infodir) ;; # ignore autotools
47         --disable-maintainer-mode|--disable-dependency-tracking) ;;
48         --with-dhcpcd-online) WITH_DHCPCD_ONLINE=${var:-yes};;
49         --without-dhcpcd-online) WITH_DHCPCD_ONLINE=no;;
50         --with-curses) WITH_CURSES=${var:-yes};;
51         --without-curses) WITH_CURSES=${var:-no};;
52         --with-gtk|--with-gtk+) WITH_GTK=${var:-yes};;
53         --without-gtk|--without-gtk+) WITH_GTK=no;;
54         --with-qt) WITH_QT=${var:-yes};;
55         --without-qt) WITH_QT=no;;
56         QTDIR) QTDIR=$var;;
57         --with-icons) WITH_ICONS=${var:-yes};;
58         --without-icons) WITH_ICONS=no;;
59         --enable-notification) NOTIFY=yes;;
60         --disable-notification) NOTIFY=no;;
61         --help) echo "See the README file for available options"; exit 0;;
62         *) echo "$0: WARNING: unknown option $opt" >&2;;
63         esac
64 done
65
66 : ${INSTALL:=install}
67 : ${SED:=sed}
68
69 : ${PREFIX:=${prefix:=/usr/local}}
70 : ${SYSCONFDIR:=$PREFIX/etc}
71 : ${BINDIR:=$PREFIX/bin}
72 : ${LIBEXECDIR:=$PREFIX/libexec}
73 : ${STATEDIR:=/var}
74 : ${MANDIR:=${PREFIX:-/usr}/share/man}
75
76 : ${ICONDIR:=$PREFIX/share/dhcpcd/icons}
77
78 eval SYSCONFDIR="$SYSCONFDIR"
79 eval LIBDIR="$LIBDIR"
80 eval LIBEXECDIR="$LIBEXECDIR"
81 eval STATEDIR="$STATEDIR"
82 eval DBDIR="$DBDIR"
83 eval RUNDIR="$RUNDIR"
84 eval MANDIR="$MANDIR"
85
86 _which()
87 {
88         x="$(which "$1" 2>/dev/null)"
89         if [ -n "$x" ]; then
90                 echo "$x"
91                 return 0
92         fi
93         for x in /sbin/"$1" /usr/sbin/"$1" \
94                 /usr/pkg/sbin/"$1" /usr/local/sbin/"$1"
95         do
96                 if [ -e "$x" ]; then
97                         echo "$x"
98                         return 0
99                 fi
100         done
101         return 1
102 }
103
104 CONFIG_H=config.h
105 CONFIG_MK=config.mk
106
107 if [ -z "$BUILD" ]; then
108         # autoconf target triplet: cpu-vendor-os
109         BUILD=$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')
110 fi
111 : ${HOST:=$BUILD}
112
113 if [ -z "$OS" ]; then
114         echo "Deriving operating system from ... $HOST"
115         # Derive OS from cpu-vendor-[kernel-]os
116         CPU=${HOST%%-*}
117         REST=${HOST#*-}
118         if [ "$CPU" != "$REST" ]; then
119                 VENDOR=${REST%%-*}
120                 REST=${REST#*-}
121                 if [ "$VENDOR" != "$REST" ]; then
122                         # Use kernel if given, otherwise os
123                         OS=${REST%%-*}
124                 else
125                         # 2 tupple
126                         OS=$VENDOR
127                         VENDOR=
128                 fi
129         fi
130
131         # Work with cpu-kernel-os, ie Debian
132         case "$VENDOR" in
133         linux*|kfreebsd*) OS=$VENDOR; VENDOR= ;;
134         esac
135         # Special case
136         case "$OS" in
137         gnu*) OS=hurd;; # No HURD support as yet
138         esac
139 fi
140
141 echo "Configuring dhcpcd-ui for ... $OS"
142 rm -f $CONFIG_H $CONFIG_MK
143 echo "# $OS" >$CONFIG_MK
144 echo "/* $OS */" >$CONFIG_H
145
146 VERSION=$($SED -ne 's/VERSION=  //p' Makefile)
147 echo "#define VERSION           \"$VERSION\"" >>$CONFIG_H
148
149 case "$OS" in
150 linux*)
151         # cksum does't support -a and netpgp is rare
152         echo "CKSUM=            sha256sum --tag" >>$CONFIG_MK
153         echo "PGP=              gpg2" >>$CONFIG_MK
154         ;;
155 esac
156
157 for x in PREFIX SYSCONFDIR BINDIR; do
158         eval v=\$$x
159         # Make files look nice for import
160         l=$((10 - ${#x}))
161         unset t
162         [ $l -gt 3 ] && t="     "
163         echo "$x=$t     $v" >>$CONFIG_MK
164         unset t
165         [ $l -gt 2 ] && t="     "
166         echo "#define $x$t      \"$v\"" >>$CONFIG_H
167 done
168 echo "#define ICONDIR           \"$ICONDIR\"" >>$CONFIG_H
169 echo "MANDIR=           $MANDIR" >>$CONFIG_MK
170
171 # Always obey CC.
172 if [ -n "$CC" ]; then
173         HOSTCC=
174 else
175         CC=cc
176         _COMPILERS="cc clang gcc pcc icc"
177 fi
178 # Only look for a cross compiler if --host and --build are not the same
179 if [ -n "$HOSTCC" -a "$BUILD" != "$HOST" ]; then
180         for _CC in $_COMPILERS; do
181                 _CC=$(_which "$HOSTCC$_CC")
182                 if [ -x "$_CC" ]; then
183                         CC=$_CC
184                         break
185                 fi
186         done
187 fi
188 if ! type "$CC" >/dev/null 2>&1; then
189         for _CC in $_COMPILERS; do
190                 _CC=$(_which "$_CC")
191                 if [ -x "$_CC" ]; then
192                         CC=$_CC
193                         break
194                 fi
195         done
196 fi
197
198 echo "Using compiler .. $CC"
199 cat <<EOF >_test.c
200 int main(void) {
201         return 0;
202 }
203 EOF
204 _CC=false
205 if $CC _test.c -o _test >/dev/null 2>&3; then
206         [ -x _test ] && _CC=true
207 fi
208 rm -f _test.c _test
209 if ! $_CC; then
210         echo "$CC does not create executables"
211         exit 1
212 fi
213 [ "$CC" != cc ] && echo "CC=    $CC" >>$CONFIG_MK
214 $CC --version | $SED -e '1!d'
215
216 # Set to blank, then append user config
217 # We do this so our SED call to append to XCC remains portable
218 if [ -n "$CFLAGS" ]; then
219         echo "CFLAGS=" >>$CONFIG_MK
220         echo "CFLAGS+=  $CFLAGS" >>$CONFIG_MK
221 fi
222 if [ -n "$CPPFLAGS" ]; then
223         echo "CPPFLAGS=" >>$CONFIG_MK
224         echo "CPPFLAGS+=        $CPPFLAGS" >>$CONFIG_MK
225 fi
226 if [ -n "$LDFLAGS" ]; then
227         echo "LDFLAGS=" >>$CONFIG_MK
228         echo "LDFLAGS+= $LDFLAGS" >>$CONFIG_MK
229 fi
230 QMAKE_CONFIG=
231
232 for x in $INCLUDEDIR; do
233         echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
234 done
235
236 if [ -z "$DEBUG" -a -d .git ]; then
237         printf "Found git checkout ... "
238         DEBUG=yes
239 else
240         DEBUG=no
241 fi
242 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
243         echo "Adding debugging CFLAGS"
244         cat <<EOF >>$CONFIG_MK
245 CFLAGS+=        -g -Wall -Wextra
246 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
247 CFLAGS+=        -Wmissing-format-attribute -Wnested-externs
248 CFLAGS+=        -Winline -Wcast-align -Wcast-qual -Wpointer-arith
249 CFLAGS+=        -Wreturn-type -Wswitch -Wshadow
250 CFLAGS+=        -Wcast-qual -Wwrite-strings
251 CFLAGS+=        -Wconversion
252 CFLAGS+=        -Wformat=2
253 CFLAGS+=        -Wpointer-sign -Wmissing-noreturn
254 EOF
255         case "$OS" in
256         mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers
257         *)              echo "CFLAGS+=  -Wredundant-decls" >>$CONFIG_MK;;
258         esac
259 else
260         # Disable assertations
261         echo "CFLAGS+=  -DNDEBUG" >>$CONFIG_MK
262 fi
263
264 # Add CPPFLAGS and CFLAGS to CC for testing features
265 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
266 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
267
268 if [ -z "$GETTEXT" ]; then
269         printf "Testing for gettext ... "
270         cat <<EOF >_gettext.c
271 #include <libintl.h>
272 int main(void) {
273         gettext("foo");
274         return 0;
275 }
276 EOF
277         LIB_INTL=
278         if $XCC _gettext.c -o _gettext 2>&3; then
279                 GETTEXT=yes
280         elif $XCC _gettext.c -o _gettext -lintl 2>&3; then
281                 GETTEXT="yes (-lintl)"
282                 LIB_INTL=-lintl
283                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libintl"
284         else
285                 GETTEXT=no
286         fi
287         echo "$GETTEXT"
288         rm -f _gettext.c _gettext
289 fi
290 if [ "$GETTEXT" != no ]; then
291         echo "#define HAS_GETTEXT" >>$CONFIG_H
292         echo "LIB_INTL= $LIB_INTL" >>$CONFIG_MK
293 fi
294
295 if [ -z "$STRLCPY" ]; then
296         printf "Testing for strlcpy ... "
297         cat <<EOF >_strlcpy.c
298 #include <string.h>
299 int main(void) {
300         const char s1[] = "foo";
301         char s2[10];
302         strlcpy(s2, s1, sizeof(s2));
303         return 0;
304 }
305 EOF
306         if $XCC _strlcpy.c -o _strlcpy 2>&3; then
307                 STRLCPY=yes
308         else
309                 STRLCPY=no
310         fi
311         echo "$STRLCPY"
312         rm -f _strlcpy.c _strlcpy
313 fi
314 if [ "$STRLCPY" = no ]; then
315         echo "#define strlcpy(dst, src, n) " \
316             "((size_t)snprintf(dst, n, \"%s\", src))" \
317             >>$CONFIG_H
318 fi
319
320 if [ -z "$STRVERSCMP" ]; then
321         printf "Testing for strverscmp ... "
322         cat <<EOF >_strverscmp.c
323 #define _GNU_SOURCE
324 #include <string.h>
325 int main(void) {
326         strverscmp(NULL, NULL);
327         return 0;
328 }
329 EOF
330         if $XCC _strverscmp.c -o _strverscmp 2>&3; then
331                 STRVERSCMP=yes
332         else
333                 STRVERSCMP=no
334         fi
335         echo "$STRVERSCMP"
336         rm -f _strverscmp.c _strverscmp
337 fi
338 if [ "$STRVERSCMP" = yes ]; then
339         echo "#define HAVE_STRVERSCMP" >>$CONFIG_H
340 fi
341
342 if [ -z "$LIBDIR" ]; then
343         printf "lib directory name ... "
344         case `readlink /lib` in
345                 "") LIBNAME="lib";;
346                 *) LIBNAME=$(basename `readlink /lib`);;
347         esac
348         echo "$LIBNAME"
349         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
350         LIBDIR=${PREFIX}/${LIBNAME}
351         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
352 else
353         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
354 fi
355
356 # We normally want to build in the static library
357 if [ "$LIBDHCPCD" = yes ]; then
358         x="-ldhcpcd"
359         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
360 else
361         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
362 fi
363 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
364
365 # Due to our use of pkg-config, pass --as-needed to ld so we only link
366 # what we actually need.
367 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
368
369 _NOTIFY="$NOTIFY"
370 _WITH_GTK="$WITH_GTK"
371 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
372         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&3; then
373                 WITH_GTK="gtk+-2.0"
374         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&3; then
375                 WITH_GTK="gtk+-3.0"
376         else
377                 echo "No suitable GTK+ library has been found"
378         fi
379 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
380         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
381                 echo "No suitable GTK+ library has been found" >&2
382                 exit 1
383         fi
384 fi
385         
386 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
387         PKG="$WITH_GTK"
388         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
389                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
390                         echo "libnotify not found" >&2
391                         [ "$NOTIFY" = yes -a -n "$_WITH_GTK" ] && exit 1
392                 else
393                         NOTIFY=yes
394                 fi
395         fi
396         if [ "$NOTIFY" = yes ]; then
397                 PKG="$PKG${PKG:+ }libnotify"
398                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
399                 echo "Building dhcpcd-gtk with notification support"
400         fi
401         GTK_CFLAGS=$(pkg-config --cflags $PKG)
402         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
403         GTK_LIBS=$(pkg-config --libs $PKG)
404         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
405         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
406         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
407         UI="$UI${UI:+ }dhcpcd-gtk"
408 fi
409
410 DHCPCD_QT=false
411 if [ -z "$WITH_QT" -o "$WITH_QT" != no ]; then
412         printf "Looking for qmake ... "
413         if [ -z "$QMAKE" ]; then
414                 if [ -x "$QTDIR"/bin/qmake ]; then
415                         QMAKE="$QTDIR"/bin/qmake
416                 else
417                         QMAKE=$(_which qmake)
418                 fi
419         fi
420         if [ -x "$QMAKE" ]; then
421                 DHCPCD_QT=true
422                 echo "$QMAKE"
423         else
424                 echo "no"
425                 if [ -n "$WITH_QT" ]; then
426                         echo "No suitable Qt library has been found" >&2
427                         exit 1
428                 fi
429         fi
430 fi
431
432 NOTIFY="$_NOTIFY"
433 if $DHCPCD_QT; then
434         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX"
435         QMAKE_CONFIG="$QMAKE_CONFIG SYSCONFDIR=$SYSCONFDIR"
436         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
437         if (cd src/dhcpcd-qt; $QMAKE $QMAKE_CONFIG dhcpcd-qt.pro); then
438                 UI="$UI${UI:+ }dhcpcd-qt"
439         else
440                 echo "Failed to configure dhcpcd-qt" >&2
441                 [ -n "$WITH_QT" ] && exit 1
442                 DHCPCD_QT=false
443         fi
444 fi
445
446 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
447         WITH_ICONS=yes
448 fi
449 if [ "$WITH_ICONS" = yes ]; then
450         echo "MKICONS=  icons" >>$CONFIG_MK
451 fi
452
453 LIB_CURSES=
454 if [ "$WITH_CURSES" = yes -o -z "$WITH_CURSES" ]; then
455         printf "Looking for libcurses ... "
456         cat <<EOF >_curses.c
457 #include <curses.h>
458 int main(void) {
459         initscr();
460         return 0;
461 }
462 EOF
463         if $XCC _curses.c -o _curses -lcurses 2>&3; then
464                 LIB_CURSES=-lcurses
465                 echo "$LIB_CURSES"
466         fi
467         rm -f _curses.c _curses
468 fi
469
470 # ncurses may have curs_set in libtinfo and not libcurses.
471 # This is a very poor design as other curses implementations
472 # do not have this limitation.
473 if [ -n "$LIB_CURSES" ]; then
474         printf "Testing for curs_set in $LIB_CURSES ... "
475         cat <<EOF >_curses.c
476 #include <curses.h>
477 int main(void) {
478         curs_set(1);
479         return 0;
480 }
481 EOF
482         if $XCC _curses.c -o _curses $LIB_CURSES 2>&3; then
483                 echo "yes"
484         else
485                 if $XCC _curses.c -o _curses $LIB_CURSES -ltinfo 2>&3; then
486                         echo "no (libtinfo)"
487                         LIB_CURSES="$LIB_CURSES -ltinfo"
488                 else
489                         echo "no and not found"
490                         [ "$WITH_CURSES" = yes ] && exit 1
491                         LIB_CURSES=
492                 fi
493         fi
494         rm -f _curses.c _curses
495 fi
496
497 if [ -n "$LIB_CURSES" ]; then
498         echo "LIB_CURSES=       $LIB_CURSES" >>$CONFIG_MK
499         UI="dhcpcd-curses${UI:+ }$UI"
500
501         if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
502                 printf "Testing for _nc_free_and_exit() in $LIB_CURSES ... "
503                 cat <<EOF >_curses.c
504 #include <curses.h>
505 int main(void) {
506         _nc_free_and_exit();
507         return 0;
508 }
509 EOF
510                 if $XCC _curses.c -o _curses "$LIB_CURSES" 2>&3; then
511                         echo "yes"
512                         echo "CURSES_CPPFLAGS=  -DHAVE_NC_FREE_AND_EXIT" >>$CONFIG_MK
513                 else
514                         echo "no"
515                 fi
516                 rm -f _curses.c _curses
517         fi
518 elif [ ! "$WITH_CURSES" = no ]; then
519         echo "no"
520 fi
521
522 if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_DHCPCD_ONLINE" != no ]; then
523         UI="dhcpcd-online${UI:+ }$UI"
524 fi
525
526 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
527
528 echo
529 echo "   SYSCONFDIR =           $SYSCONFDIR"
530 echo "   BINDIR =               $BINDIR"
531 echo "   LIBDIR =               $LIBDIR"
532 echo "   ICONDIR =              $ICONDIR"
533 echo "   MANDIR =               $MANDIR"
534 echo "   UI =                   $UI"
535 echo
536
537 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
538