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