Clean up with new debug flags and gcc.
[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
238 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
239 CFLAGS+=        -Wmissing-format-attribute -Wnested-externs
240 CFLAGS+=        -Winline -Wcast-align -Wcast-qual -Wpointer-arith
241 CFLAGS+=        -Wreturn-type -Wswitch -Wshadow
242 CFLAGS+=        -Wcast-qual -Wwrite-strings
243 CFLAGS+=        -Wconversion
244 CFLAGS+=        -Wformat=2
245 CFLAGS+=        -Wpointer-sign -Wmissing-noreturn
246 EOF
247         case "$OS" in
248         mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers
249         *)              echo "CFLAGS+=  -Wredundant-decls" >>$CONFIG_MK;;
250         esac
251 else
252         # Disable assertations
253         echo "CFLAGS+=  -DNDEBUG" >>$CONFIG_MK
254 fi
255
256 # Add CPPFLAGS and CFLAGS to CC for testing features
257 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
258 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
259
260 if [ -z "$GETTEXT" ]; then
261         printf "Testing for gettext ... "
262         cat <<EOF >_gettext.c
263 #include <libintl.h>
264 int main(void) {
265         gettext("foo");
266         return 0;
267 }
268 EOF
269         LIB_INTL=
270         if $XCC _gettext.c -o _gettext 2>&3; then
271                 GETTEXT=yes
272         elif $XCC _gettext.c -o _gettext -lintl 2>&3; then
273                 GETTEXT="yes (-lintl)"
274                 LIB_INTL=-lintl
275                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libintl"
276         else
277                 GETTEXT=no
278         fi
279         echo "$GETTEXT"
280         rm -f _gettext.c _gettext
281 fi
282 if [ "$GETTEXT" != no ]; then
283         echo "#define HAS_GETTEXT" >>$CONFIG_H
284         echo "LIB_INTL= $LIB_INTL" >>$CONFIG_MK
285 fi
286
287 if [ -z "$STRLCPY" ]; then
288         printf "Testing for strlcpy ... "
289         cat <<EOF >_strlcpy.c
290 #include <string.h>
291 int main(void) {
292         const char s1[] = "foo";
293         char s2[10];
294         strlcpy(s2, s1, sizeof(s2));
295         return 0;
296 }
297 EOF
298         if $XCC _strlcpy.c -o _strlcpy 2>&3; then
299                 STRLCPY=yes
300         else
301                 STRLCPY=no
302         fi
303         echo "$STRLCPY"
304         rm -f _strlcpy.c _strlcpy
305 fi
306 if [ "$STRLCPY" = no ]; then
307         echo "#define strlcpy(dst, src, n) " \
308             "((size_t)snprintf(dst, n, \"%s\", src))" \
309             >>$CONFIG_H
310 fi
311
312 if [ -z "$STRVERSCMP" ]; then
313         printf "Testing for strverscmp ... "
314         cat <<EOF >_strverscmp.c
315 #define _GNU_SOURCE
316 #include <string.h>
317 int main(void) {
318         strverscmp(NULL, NULL);
319         return 0;
320 }
321 EOF
322         if $XCC _strverscmp.c -o _strverscmp 2>&3; then
323                 STRVERSCMP=yes
324         else
325                 STRVERSCMP=no
326         fi
327         echo "$STRVERSCMP"
328         rm -f _strverscmp.c _strverscmp
329 fi
330 if [ "$STRVERSCMP" = yes ]; then
331         echo "#define HAVE_STRVERSCMP" >>$CONFIG_H
332 fi
333
334 if [ -z "$LIBDIR" ]; then
335         printf "lib directory name ... "
336         case `readlink /lib` in
337                 "") LIBNAME="lib";;
338                 *) LIBNAME=$(basename `readlink /lib`);;
339         esac
340         echo "$LIBNAME"
341         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
342         LIBDIR=${PREFIX}/${LIBNAME}
343         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
344 else
345         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
346 fi
347
348 # We normally want to build in the static library
349 if [ "$LIBDHCPCD" = yes ]; then
350         x="-ldhcpcd"
351         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
352 else
353         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
354 fi
355 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
356
357 # Due to our use of pkg-config, pass --as-needed to ld so we only link
358 # what we actually need.
359 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
360
361 _NOTIFY="$NOTIFY"
362 _WITH_GTK="$WITH_GTK"
363 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
364         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&3; then
365                 WITH_GTK="gtk+-2.0"
366         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&3; then
367                 WITH_GTK="gtk+-3.0"
368         else
369                 echo "No suitable GTK+ library has been found"
370         fi
371 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
372         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
373                 echo "No suitable GTK+ library has been found" >&2
374                 exit 1
375         fi
376 fi
377         
378 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
379         PKG="$WITH_GTK"
380         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
381                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
382                         echo "libnotify not found" >&2
383                         [ "$NOTIFY" = yes -a -n "$_WITH_GTK" ] && exit 1
384                 fi
385                 NOTIFY=yes
386         fi
387         if [ "$NOTIFY" = yes ]; then
388                 PKG="$PKG${PKG:+ }libnotify"
389                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
390                 echo "Building dhcpcd-gtk with notification support"
391         fi
392         GTK_CFLAGS=$(pkg-config --cflags $PKG)
393         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
394         GTK_LIBS=$(pkg-config --libs $PKG)
395         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
396         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
397         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
398         UI="$UI${UI:+ }dhcpcd-gtk"
399 fi
400
401 DHCPCD_QT=false
402 if [ -z "$WITH_QT" -o "$WITH_QT" != no ]; then
403         if [ -z "$QMAKE" ]; then
404                 if [ -x "$QTDIR"/bin/qmake ]; then
405                         QMAKE="$QTDIR"/bin/qmake
406                 else
407                         QMAKE=$(_which qmake)
408                 fi
409         fi
410         if [ -x "$QMAKE" ]; then
411                 UI="$UI${UI+ }dhcpcd-qt"
412                 DHCPCD_QT=true
413         else
414                 if [ -n "$WITH_QT" ]; then
415                         echo "No suitable Qt library has been found" >&2
416                         exit 1
417                 fi
418                 echo "No suitable Qt library has been found"
419         fi
420 fi
421
422 NOTIFY="$_NOTIFY"
423 if $DHCPCD_QT && [ -z "$NOTFY" -o "$NOTIFY" = yes ]; then
424         if kde4-config --path lib --locate libkdeui.so >/dev/null 2>&3; then
425                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libkdeui"
426                 echo "Building dhcpcd-qt with notification support"
427         elif [ "$NOTIFY" = yes ]; then
428                 echo "$0: knotify4 not found" >&2
429                 exit 1
430         fi
431 fi
432
433 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
434         WITH_ICONS=yes
435 fi
436 if [ "$WITH_ICONS" = yes ]; then
437         echo "MKICONS=  icons" >>$CONFIG_MK
438 fi
439
440 LIB_CURSES=
441 if [ "$WITH_CURSES" = yes -o -z "$WITH_CURSES" ]; then
442         printf "Looking for libcurses ... "
443         cat <<EOF >_curses.c
444 #include <curses.h>
445 int main(void) {
446         initscr();
447         return 0;
448 }
449 EOF
450         if $XCC _curses.c -o _curses -lcurses 2>&3; then
451                 LIB_CURSES=-lcurses
452         fi
453         rm -f _curses.c _curses
454 fi
455 if [ -n "$LIB_CURSES" ]; then
456         echo "$LIB_CURSES"
457         echo "LIB_CURSES=       $LIB_CURSES" >>$CONFIG_MK
458         UI="dhcpcd-curses${UI:+ }$UI"
459
460         if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
461                 printf "Testing for _nc_free_and_exit() in $LIB_CURSES ... "
462                 cat <<EOF >_curses.c
463 #include <curses.h>
464 int main(void) {
465         _nc_free_and_exit();
466         return 0;
467 }
468 EOF
469                 if $XCC _curses.c -o _curses "$LIB_CURSES" 2>&3; then
470                         echo "yes"
471                         echo "CURSES_CPPFLAGS=  -DHAVE_NC_FREE_AND_EXIT" >>$CONFIG_MK
472                 else
473                         echo "no"
474                 fi
475                 rm -f _curses.c _curses
476         fi
477 elif [ ! "$WITH_CURSES" = no ]; then
478         echo "no"
479 fi
480
481 if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_DHCPCD_ONLINE" != no ]; then
482         UI="dhcpcd-online${UI:+ }$UI"
483 fi
484
485 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
486
487 echo
488 echo "   SYSCONFDIR =           $SYSCONFDIR"
489 echo "   BINDIR =               $BINDIR"
490 echo "   LIBDIR =               $LIBDIR"
491 echo "   ICONDIR =              $ICONDIR"
492 echo "   MANDIR =               $MANDIR"
493 echo "   UI =                   $UI"
494 echo
495
496 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
497
498 if $DHCPCD_QT; then
499         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX SYSCONFDIR=$SYSCONFDIR"
500         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
501         if ! (cd src/dhcpcd-qt; $QMAKE $QMAKE_CONFIG dhcpcd-qt.pro); then
502                 echo "$0: failed to configure dhcpcd-qt" >&2
503                 exit 1
504         fi
505 fi