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