381cf84a21a22c8b354820ab42226dc2ab7147f8
[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         fi
385 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
386         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
387                 echo "$0: no suitable GTK+ library has been found" >&2
388                 exit 1
389         fi
390 fi
391         
392 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
393         PKG="$WITH_GTK"
394         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
395                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
396                         echo "libnotify not found" >&2
397                         [ "$NOTIFY" = yes -a -n "$_WITH_GTK" ] && exit 1
398                 fi
399                 NOTIFY=yes
400         fi
401         if [ "$NOTIFY" = yes ]; then
402                 PKG="$PKG${PKG:+ }libnotify"
403                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
404                 echo "Building dhcpcd-gtk with notification support"
405         fi
406         GTK_CFLAGS=$(pkg-config --cflags $PKG)
407         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
408         GTK_LIBS=$(pkg-config --libs $PKG)
409         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
410         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
411         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
412         UI="$UI${UI:+ }dhcpcd-gtk"
413 fi
414
415 DHCPCD_QT=false
416 if [ -z "$WITH_QT" -o "$WITH_QT" != no ]; then
417         if [ -z "$QMAKE" ]; then
418                 if [ -x "$QTDIR"/bin/qmake ]; then
419                         QMAKE="$QTDIR"/bin/qmake
420                 else
421                         QMAKE=$(_which qmake)
422                 fi
423         fi
424         if [ -x "$QMAKE" ]; then
425                 UI="$UI${UI+ }dhcpcd-qt"
426                 DHCPCD_QT=true
427         else
428                 echo "$0: no suitable Qt library has been found" >&2
429                 [ -z "$WITH_QT" ] || exit 1
430         fi
431 fi
432
433 NOTIFY="$_NOTIFY"
434 if $DHCPCD_QT && [ -z "$NOTFY" -o "$NOTIFY" = yes ]; then
435         if kde4-config --path lib --locate libkdeui.so >/dev/null 2>&3; then
436                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libkdeui"
437                 echo "Building dhcpcd-qt with notification support"
438         elif [ "$NOTIFY" = yes ]; then
439                 echo "$0: knotify4 not found" >&2
440                 exit 1
441         fi
442 fi
443
444 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
445         WITH_ICONS=yes
446 fi
447 if [ "$WITH_ICONS" = yes ]; then
448         echo "MKICONS=  icons" >>$CONFIG_MK
449 fi
450
451 LIB_CURSES=
452 if [ "$WITH_CURSES" = yes -o -z "$WITH_CURSES" ]; then
453         printf "Looking for libcurses ... "
454         cat <<EOF >_curses.c
455 #include <curses.h>
456 int main(void) {
457         initscr();
458         return 0;
459 }
460 EOF
461         if $XCC _curses.c -o _curses -lcurses 2>&3; then
462                 LIB_CURSES=-lcurses
463         fi
464         rm -f _curses.c _curses
465 fi
466 if [ -n "$LIB_CURSES" ]; then
467         echo "$LIB_CURSES"
468         echo "LIB_CURSES=       $LIB_CURSES" >>$CONFIG_MK
469         UI="dhcpcd-curses${UI:+ }$UI"
470
471         if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
472                 printf "Testing for _nc_free_and_exit() in $LIB_CURSES ... "
473                 cat <<EOF >_curses.c
474 #include <curses.h>
475 int main(void) {
476         _nc_free_and_exit();
477         return 0;
478 }
479 EOF
480                 if $XCC _curses.c -o _curses "$LIB_CURSES" 2>&3; then
481                         echo "yes"
482                         echo "CURSES_CPPFLAGS=  -DHAVE_NC_FREE_AND_EXIT" >>$CONFIG_MK
483                 else
484                         echo "no"
485                 fi
486                 rm -f _curses.c _curses
487         fi
488 else
489         echo "no"
490 fi
491
492 if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_DHCPCD_ONLINE" != no ]; then
493         UI="dhcpcd-online${UI:+ }$UI"
494 fi
495
496 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
497
498 echo
499 echo "   SYSCONFDIR =           $SYSCONFDIR"
500 echo "   BINDIR =               $BINDIR"
501 echo "   LIBDIR =               $LIBDIR"
502 echo "   ICONDIR =              $ICONDIR"
503 echo "   MANDIR =               $MANDIR"
504 echo "   UI =                   $UI"
505 echo
506
507 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
508
509 if $DHCPCD_QT; then
510         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX SYSCONFDIR=$SYSCONFDIR"
511         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
512         if ! (cd src/dhcpcd-qt; $QMAKE $QMAKE_CONFIG dhcpcd-qt.pro); then
513                 echo "$0: failed to configure dhcpcd-qt" >&2
514                 exit 1
515         fi
516 fi