Clean up some compile warnings and avoid spamming lost connection on stop.
[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 for x in PREFIX SYSCONFDIR BINDIR; do
150         eval v=\$$x
151         # Make files look nice for import
152         l=$((10 - ${#x}))
153         unset t
154         [ $l -gt 3 ] && t="     "
155         echo "$x=$t     $v" >>$CONFIG_MK
156         unset t
157         [ $l -gt 2 ] && t="     "
158         echo "#define $x$t      \"$v\"" >>$CONFIG_H
159 done
160 echo "#define ICONDIR           \"$ICONDIR\"" >>$CONFIG_H
161 echo "MANDIR=           $MANDIR" >>$CONFIG_MK
162
163 # Always obey CC.
164 if [ -n "$CC" ]; then
165         HOSTCC=
166 else
167         CC=cc
168         _COMPILERS="cc clang gcc pcc icc"
169 fi
170 # Only look for a cross compiler if --host and --build are not the same
171 if [ -n "$HOSTCC" -a "$BUILD" != "$HOST" ]; then
172         for _CC in $_COMPILERS; do
173                 _CC=$(_which "$HOSTCC$_CC")
174                 if [ -x "$_CC" ]; then
175                         CC=$_CC
176                         break
177                 fi
178         done
179 fi
180 if ! type "$CC" >/dev/null 2>&1; then
181         for _CC in $_COMPILERS; do
182                 _CC=$(_which "$_CC")
183                 if [ -x "$_CC" ]; then
184                         CC=$_CC
185                         break
186                 fi
187         done
188 fi
189
190 echo "Using compiler .. $CC"
191 cat <<EOF >_test.c
192 int main(void) {
193         return 0;
194 }
195 EOF
196 _CC=false
197 if $CC _test.c -o _test >/dev/null 2>&3; then
198         [ -x _test ] && _CC=true
199 fi
200 rm -f _test.c _test
201 if ! $_CC; then
202         echo "$CC does not create executables"
203         exit 1
204 fi
205 [ "$CC" != cc ] && echo "CC=    $CC" >>$CONFIG_MK
206 $CC --version | $SED -e '1!d'
207
208 # Set to blank, then append user config
209 # We do this so our SED call to append to XCC remains portable
210 if [ -n "$CFLAGS" ]; then
211         echo "CFLAGS=" >>$CONFIG_MK
212         echo "CFLAGS+=  $CFLAGS" >>$CONFIG_MK
213 fi
214 if [ -n "$CPPFLAGS" ]; then
215         echo "CPPFLAGS=" >>$CONFIG_MK
216         echo "CPPFLAGS+=        $CPPFLAGS" >>$CONFIG_MK
217 fi
218 if [ -n "$LDFLAGS" ]; then
219         echo "LDFLAGS=" >>$CONFIG_MK
220         echo "LDFLAGS+= $LDFLAGS" >>$CONFIG_MK
221 fi
222 QMAKE_CONFIG=
223
224 for x in $INCLUDEDIR; do
225         echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
226 done
227
228 if [ -z "$DEBUG" -a -f .fslckout ]; then
229         printf "Found fossil checkout ... "
230         DEBUG=yes
231 else
232         DEBUG=no
233 fi
234 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
235         echo "Adding debugging CFLAGS"
236         cat <<EOF >>$CONFIG_MK
237 CFLAGS+=        -g -Wall -Wextra -Wshadow -Wformat=2
238 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
239 CFLAGS+=        -Wmissing-noreturn -Wmissing-format-attribute
240 CFLAGS+=        -Wnested-externs
241 CFLAGS+=        -Winline -Wwrite-strings -Wcast-align -Wcast-qual
242 CFLAGS+=        -Wpointer-arith -Wstrict-overflow
243 CFLAGS+=        -Wdeclaration-after-statement
244 CFLAGS+=        -Wconversion
245 EOF
246         case "$OS" in
247         mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers
248         *)              echo "CFLAGS+=  -Wredundant-decls" >>$CONFIG_MK;;
249         esac
250 else
251         # Disable assertations
252         echo "CFLAGS+=  -DNDEBUG" >>$CONFIG_MK
253 fi
254
255 printf "OS supports background scanning ... "
256 # Most OS's don't support background scanning
257 case "$OS" in
258         linux*|freebsd*)
259                 BGSCAN=true;;
260         *)      BGSCAN=false;;
261 esac
262 printf "%s ... " "$OS"
263 if $BGSCAN; then
264         echo "yes"
265         echo "CFLAGS+=  -DBG_SCAN" >> $CONFIG_MK
266         QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=build_bgscan"
267 else
268         echo "no"
269 fi
270
271
272 # Add CPPFLAGS and CFLAGS to CC for testing features
273 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
274 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
275
276 if [ -z "$GETTEXT" ]; then
277         printf "Testing for gettext ... "
278         cat <<EOF >_gettext.c
279 #include <libintl.h>
280 int main(void) {
281         gettext("foo");
282         return 0;
283 }
284 EOF
285         LIB_INTL=
286         if $XCC _gettext.c -o _gettext 2>&3; then
287                 GETTEXT=yes
288         elif $XCC _gettext.c -o _gettext -lintl 2>&3; then
289                 GETTEXT="yes (-lintl)"
290                 LIB_INTL=-lintl
291                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libintl"
292         else
293                 GETTEXT=no
294         fi
295         echo "$GETTEXT"
296         rm -f _gettext.c _gettext
297 fi
298 if [ "$GETTEXT" != no ]; then
299         echo "#define HAS_GETTEXT" >>$CONFIG_H
300         echo "LIB_INTL= $LIB_INTL" >>$CONFIG_MK
301 fi
302
303 if [ -z "$STRLCPY" ]; then
304         printf "Testing for strlcpy ... "
305         cat <<EOF >_strlcpy.c
306 #include <string.h>
307 int main(void) {
308         const char s1[] = "foo";
309         char s2[10];
310         strlcpy(s2, s1, sizeof(s2));
311         return 0;
312 }
313 EOF
314         if $XCC _strlcpy.c -o _strlcpy 2>&3; then
315                 STRLCPY=yes
316         else
317                 STRLCPY=no
318         fi
319         echo "$STRLCPY"
320         rm -f _strlcpy.c _strlcpy
321 fi
322 if [ "$STRLCPY" = no ]; then
323         echo "#define strlcpy(dst, src, n) " \
324             "((size_t)snprintf(dst, n, \"%s\", src))" \
325             >>$CONFIG_H
326 fi
327
328 if [ -z "$STRVERSCMP" ]; then
329         printf "Testing for strverscmp ... "
330         cat <<EOF >_strverscmp.c
331 #define _GNU_SOURCE
332 #include <string.h>
333 int main(void) {
334         strverscmp(NULL, NULL);
335         return 0;
336 }
337 EOF
338         if $XCC _strverscmp.c -o _strverscmp 2>&3; then
339                 STRVERSCMP=yes
340         else
341                 STRVERSCMP=no
342         fi
343         echo "$STRVERSCMP"
344         rm -f _strverscmp.c _strverscmp
345 fi
346 if [ "$STRVERSCMP" = yes ]; then
347         echo "#define HAVE_STRVERSCMP" >>$CONFIG_H
348 fi
349
350 if [ -z "$LIBDIR" ]; then
351         printf "lib directory name ... "
352         case `readlink /lib` in
353                 "") LIBNAME="lib";;
354                 *) LIBNAME=$(basename `readlink /lib`);;
355         esac
356         echo "$LIBNAME"
357         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
358         LIBDIR=${PREFIX}/${LIBNAME}
359         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
360 else
361         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
362 fi
363
364 # We normally want to build in the static library
365 if [ "$LIBDHCPCD" = yes ]; then
366         x="-ldhcpcd"
367         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
368 else
369         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
370 fi
371 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
372
373 # Due to our use of pkg-config, pass --as-needed to ld so we only link
374 # what we actually need.
375 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
376
377 _NOTIFY="$NOTIFY"
378 _WITH_GTK="$WITH_GTK"
379 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
380         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&3; then
381                 WITH_GTK="gtk+-2.0"
382         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&3; then
383                 WITH_GTK="gtk+-3.0"
384         else
385                 echo "No suitable GTK+ library has been found"
386         fi
387 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
388         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
389                 echo "No suitable GTK+ library has been found" >&2
390                 exit 1
391         fi
392 fi
393         
394 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
395         PKG="$WITH_GTK"
396         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
397                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
398                         echo "libnotify not found" >&2
399                         [ "$NOTIFY" = yes -a -n "$_WITH_GTK" ] && exit 1
400                 fi
401                 NOTIFY=yes
402         fi
403         if [ "$NOTIFY" = yes ]; then
404                 PKG="$PKG${PKG:+ }libnotify"
405                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
406                 echo "Building dhcpcd-gtk with notification support"
407         fi
408         GTK_CFLAGS=$(pkg-config --cflags $PKG)
409         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
410         GTK_LIBS=$(pkg-config --libs $PKG)
411         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
412         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
413         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
414         UI="$UI${UI:+ }dhcpcd-gtk"
415 fi
416
417 DHCPCD_QT=false
418 if [ -z "$WITH_QT" -o "$WITH_QT" != no ]; then
419         if [ -z "$QMAKE" ]; then
420                 if [ -x "$QTDIR"/bin/qmake ]; then
421                         QMAKE="$QTDIR"/bin/qmake
422                 else
423                         QMAKE=$(_which qmake)
424                 fi
425         fi
426         if [ -x "$QMAKE" ]; then
427                 UI="$UI${UI+ }dhcpcd-qt"
428                 DHCPCD_QT=true
429         else
430                 if [ -n "$WITH_QT" ]; then
431                         echo "No suitable Qt library has been found" >&2
432                         exit 1
433                 fi
434                 echo "No suitable Qt library has been found"
435         fi
436 fi
437
438 NOTIFY="$_NOTIFY"
439 if $DHCPCD_QT && [ -z "$NOTFY" -o "$NOTIFY" = yes ]; then
440         if kde4-config --path lib --locate libkdeui.so >/dev/null 2>&3; then
441                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libkdeui"
442                 echo "Building dhcpcd-qt with notification support"
443         elif [ "$NOTIFY" = yes ]; then
444                 echo "$0: knotify4 not found" >&2
445                 exit 1
446         fi
447 fi
448
449 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
450         WITH_ICONS=yes
451 fi
452 if [ "$WITH_ICONS" = yes ]; then
453         echo "MKICONS=  icons" >>$CONFIG_MK
454 fi
455
456 LIB_CURSES=
457 if [ "$WITH_CURSES" = yes -o -z "$WITH_CURSES" ]; then
458         printf "Looking for libcurses ... "
459         cat <<EOF >_curses.c
460 #include <curses.h>
461 int main(void) {
462         initscr();
463         return 0;
464 }
465 EOF
466         if $XCC _curses.c -o _curses -lcurses 2>&3; then
467                 LIB_CURSES=-lcurses
468         fi
469         rm -f _curses.c _curses
470 fi
471 if [ -n "$LIB_CURSES" ]; then
472         echo "$LIB_CURSES"
473         echo "LIB_CURSES=       $LIB_CURSES" >>$CONFIG_MK
474         UI="dhcpcd-curses${UI:+ }$UI"
475
476         if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
477                 printf "Testing for _nc_free_and_exit() in $LIB_CURSES ... "
478                 cat <<EOF >_curses.c
479 #include <curses.h>
480 int main(void) {
481         _nc_free_and_exit();
482         return 0;
483 }
484 EOF
485                 if $XCC _curses.c -o _curses "$LIB_CURSES" 2>&3; then
486                         echo "yes"
487                         echo "CURSES_CPPFLAGS=  -DHAVE_NC_FREE_AND_EXIT" >>$CONFIG_MK
488                 else
489                         echo "no"
490                 fi
491                 rm -f _curses.c _curses
492         fi
493 elif [ ! "$WITH_CURSES" = no ]; then
494         echo "no"
495 fi
496
497 if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_DHCPCD_ONLINE" != no ]; then
498         UI="dhcpcd-online${UI:+ }$UI"
499 fi
500
501 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
502
503 echo
504 echo "   SYSCONFDIR =           $SYSCONFDIR"
505 echo "   BINDIR =               $BINDIR"
506 echo "   LIBDIR =               $LIBDIR"
507 echo "   ICONDIR =              $ICONDIR"
508 echo "   MANDIR =               $MANDIR"
509 echo "   UI =                   $UI"
510 echo
511
512 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
513
514 if $DHCPCD_QT; then
515         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX SYSCONFDIR=$SYSCONFDIR"
516         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
517         if ! (cd src/dhcpcd-qt; $QMAKE $QMAKE_CONFIG dhcpcd-qt.pro); then
518                 echo "$0: failed to configure dhcpcd-qt" >&2
519                 exit 1
520         fi
521 fi