73a761f4b4ba582c629ea27443aad3b404368ad2
[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_TOOLS=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-tools) WITH_TOOLS=${var:-yes};;
48         --without-tools) WITH_TOOLS=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 "$VIS" ]; then
307         printf "Testing for vis ... "
308         cat <<EOF >_vis.c
309 #include <vis.h>
310
311 int main(void) {
312         char s[10];
313         xxvis(s, 0, 0, 0);
314         return 0;
315 }
316 EOF
317         if $XCC _vis.c -o _vis 2>&3; then
318                 VIS=yes
319         else
320                 VIS=no
321         fi
322         echo "$VIS"
323         rm -f _vis.c _vis
324 fi
325 if [ "$VIS" = no ]; then
326         echo "VIS_SRC=          vis.c" >>$CONFIG_MK
327         echo "#define vis       dhcpcd_vis" >>$CONFIG_H
328 else
329         echo "#define HAVE_VIS_H" >>$CONFIG_H
330 fi
331
332 if [ -z "$STRNUNVIS" ]; then
333         printf "Testing for strnunvis ... "
334         cat <<EOF >_strnunvis.c
335 #include <vis.h>
336 #ifdef __FreeBSD__
337 #error FreeBSD has broken strnunvis support
338 #endif
339 #ifdef __NetBSD_Version__
340 #  if __NetBSD_Version__ <799000200
341 #  error Upgrade NetBSD to 7.99.2 to get a working strnunvis
342 #  endif
343 #endif
344
345 int main(void) {
346         char s[10];
347         strunvis(s, 0, s);
348         return 0;
349 }
350 EOF
351         if $XCC _strnunvis.c -o _strnunvis 2>&3; then
352                 STRNUNVIS=yes
353         else
354                 STRNUNVIS=no
355         fi
356         echo "$STRNUNVIS"
357         rm -f _strnunvis.c _strnunvis
358 fi
359 if [ "$STRNUNVIS" = no ]; then
360         echo "UNVIS_SRC=        unvis.c" >>$CONFIG_MK
361         echo "#define strnunvis dhcpcd_strnunvis" >>$CONFIG_H
362 else
363         echo "#define HAVE_STRNUNVIS" >>$CONFIG_H
364 fi
365
366 if [ -z "$STRVERSCMP" ]; then
367         printf "Testing for strverscmp ... "
368         cat <<EOF >_strverscmp.c
369 #define _GNU_SOURCE
370 #include <string.h>
371 int main(void) {
372         strverscmp(NULL, NULL);
373         return 0;
374 }
375 EOF
376         if $XCC _strverscmp.c -o _strverscmp 2>&3; then
377                 STRVERSCMP=yes
378         else
379                 STRVERSCMP=no
380         fi
381         echo "$STRVERSCMP"
382         rm -f _strverscmp.c _strverscmp
383 fi
384 if [ "$STRVERSCMP" = yes ]; then
385         echo "#define HAVE_STRVERSCMP" >>$CONFIG_H
386 fi
387
388 if [ -z "$LIBDIR" ]; then
389         printf "lib directory name ... "
390         case `readlink /lib` in
391                 "") LIBNAME="lib";;
392                 *) LIBNAME=$(basename `readlink /lib`);;
393         esac
394         echo "$LIBNAME"
395         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
396         LIBDIR=${PREFIX}/${LIBNAME}
397         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
398 else
399         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
400 fi
401
402 # We normally want to build in the static library
403 if [ "$LIBDHCPCD" = yes ]; then
404         x="-ldhcpcd"
405         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
406 else
407         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
408 fi
409 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
410
411 # Due to our use of pkg-config, pass --as-needed to ld so we only link
412 # what we actually need.
413 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
414
415 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
416         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&3; then
417                 WITH_GTK="gtk+-2.0"
418         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&3; then
419                 WITH_GTK="gtk+-3.0"
420         fi
421 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
422         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
423                 echo "$0: no suitable GTK+ library has been found" >&2
424                 exit 1
425         fi
426 fi
427         
428 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
429         PKG="$WITH_GTK"
430         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
431                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
432                         echo "libnotify not found" >&2
433                         [ "$NOTIFY" = yes ] && exit 1
434                         NOTIFY=no
435                 fi
436                 NOTIFY=yes
437         fi
438         if [ "$NOTIFY" = yes ]; then
439                 PKG="$PKG${PKG:+ }libnotify"
440                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
441                 echo "Building dhcpcd-gtk with notification support"
442         fi
443         GTK_CFLAGS=$(pkg-config --cflags $PKG)
444         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
445         GTK_LIBS=$(pkg-config --libs $PKG)
446         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
447         echo "GTK+ CFLAGS=$GTK_CFLAGS"
448         echo "GTK+ LIBS=$GTK_LIBS"
449         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
450         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
451         UI="$UI${UI:+ }dhcpcd-gtk"
452 fi
453
454 DHCPCD_QT=false
455 if [ "WITH_QT" = yes -o -z "$WITH_QT" ]; then
456         if type qmake >/dev/null 2>&3; then
457                 UI="$UI${UI+ }dhcpcd-qt"
458                 DHCPCD_QT=true
459         fi
460 elif [ -n "$WITH_QT" -a "$WITH_QT" != no ]; then
461         if type qmake >/dev/null 2>&3; then
462                 UI="$UI${UI+ }dhcpcd-qt"
463                 DHCPCD_QT=true
464         else
465                 echo "$0: no suitable QT library has been found" >&2
466                 exit 1
467         fi
468 fi
469
470 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
471         WITH_ICONS=yes
472 fi
473 if [ "$WITH_ICONS" = yes ]; then
474         echo "MKICONS=  icons" >>$CONFIG_MK
475 fi
476
477 if [ -n "$WITH_TOOLS" -a "$WITH_TOOLS" != no ]; then
478         UI="dhcpcd-decode dhcpcd-online${UI:+ }$UI"
479 fi
480 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
481
482 echo
483 echo "   SYSCONFDIR =           $SYSCONFDIR"
484 echo "   BINDIR =               $BINDIR"
485 echo "   LIBDIR =               $LIBDIR"
486 echo "   ICONDIR =              $ICONDIR"
487 echo "   MANDIR =               $MANDIR"
488 echo "   UI =                   $UI"
489 echo
490
491 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
492
493 if $DHCPCD_QT; then
494         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX SYSCONFDIR=$SYSCONFDIR"
495         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
496         if ! (cd src/dhcpcd-qt; qmake $QMAKE_CONFIG dhcpcd-qt.pro); then
497                 echo "$0: failed to configure dhcpcd-qt" >&2
498                 exit 1
499         fi
500 fi