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