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