Fix make dist to export the fossil trunk, not whats on the disk.
[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 "$STRVERSCMP" ]; then
307         printf "Testing for strverscmp ... "
308         cat <<EOF >_strverscmp.c
309 #define _GNU_SOURCE
310 #include <string.h>
311 int main(void) {
312         strverscmp(NULL, NULL);
313         return 0;
314 }
315 EOF
316         if $XCC _strverscmp.c -o _strverscmp 2>&3; then
317                 STRVERSCMP=yes
318         else
319                 STRVERSCMP=no
320         fi
321         echo "$STRVERSCMP"
322         rm -f _strverscmp.c _strverscmp
323 fi
324 if [ "$STRVERSCMP" = yes ]; then
325         echo "#define HAVE_STRVERSCMP" >>$CONFIG_H
326 fi
327
328 if [ -z "$LIBDIR" ]; then
329         printf "lib directory name ... "
330         case `readlink /lib` in
331                 "") LIBNAME="lib";;
332                 *) LIBNAME=$(basename `readlink /lib`);;
333         esac
334         echo "$LIBNAME"
335         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
336         LIBDIR=${PREFIX}/${LIBNAME}
337         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
338 else
339         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
340 fi
341
342 # We normally want to build in the static library
343 if [ "$LIBDHCPCD" = yes ]; then
344         x="-ldhcpcd"
345         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
346 else
347         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
348 fi
349 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
350
351 # Due to our use of pkg-config, pass --as-needed to ld so we only link
352 # what we actually need.
353 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
354
355 _NOTIFY="$NOTIFY"
356 _WITH_GTK="$WITH_GTK"
357 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
358         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&3; then
359                 WITH_GTK="gtk+-2.0"
360         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&3; then
361                 WITH_GTK="gtk+-3.0"
362         fi
363 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
364         if ! pkg-config --cflags $WITH_GTK >/dev/null 2>&3; then
365                 echo "$0: no suitable GTK+ library has been found" >&2
366                 exit 1
367         fi
368 fi
369         
370 if [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
371         PKG="$WITH_GTK"
372         if [ "$NOTIFY" = yes -o -z "$NOTIFY" ]; then
373                 if ! pkg-config --cflags libnotify >/dev/null 2>&3; then
374                         echo "libnotify not found" >&2
375                         [ "$NOTIFY" = yes -n "$_WITH_GTK" ] && exit 1
376                 fi
377                 NOTIFY=yes
378         fi
379         if [ "$NOTIFY" = yes ]; then
380                 PKG="$PKG${PKG:+ }libnotify"
381                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
382                 echo "Building dhcpcd-gtk with notification support"
383         fi
384         GTK_CFLAGS=$(pkg-config --cflags $PKG)
385         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
386         GTK_LIBS=$(pkg-config --libs $PKG)
387         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
388         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
389         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
390         UI="$UI${UI:+ }dhcpcd-gtk"
391 fi
392
393 DHCPCD_QT=false
394 if [ "WITH_QT" = yes -o -z "$WITH_QT" ]; then
395         if type qmake >/dev/null 2>&3; then
396                 UI="$UI${UI+ }dhcpcd-qt"
397                 DHCPCD_QT=true
398         fi
399 elif [ -n "$WITH_QT" -a "$WITH_QT" != no ]; then
400         if type qmake >/dev/null 2>&3; then
401                 UI="$UI${UI+ }dhcpcd-qt"
402                 DHCPCD_QT=true
403         else
404                 echo "$0: no suitable QT library has been found" >&2
405                 exit 1
406         fi
407 fi
408
409 NOTIFY="$_NOTIFY"
410 if $DHCPCD_QT && [ -z "$NOTFY" -o "$NOTIFY" = yes ]; then
411         if type knotify4 >/dev/null 2>&3; then
412                 QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libkdeui"
413                 echo "Building dhcpcd-qt with notification support"
414         elif [ "$NOTIFY" = yes ]; then
415                 echo "$0: knotify4 not found" >&2
416                 exit 1
417         fi
418 fi
419
420 if [ -n "$UI" -a -z "$WITH_ICONS" ]; then
421         WITH_ICONS=yes
422 fi
423 if [ "$WITH_ICONS" = yes ]; then
424         echo "MKICONS=  icons" >>$CONFIG_MK
425 fi
426
427 if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_ONLINE" != no ]; then
428         UI="dhcpcd-online${UI:+ }$UI"
429 fi
430 echo "UI=               ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
431
432 echo
433 echo "   SYSCONFDIR =           $SYSCONFDIR"
434 echo "   BINDIR =               $BINDIR"
435 echo "   LIBDIR =               $LIBDIR"
436 echo "   ICONDIR =              $ICONDIR"
437 echo "   MANDIR =               $MANDIR"
438 echo "   UI =                   $UI"
439 echo
440
441 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
442
443 if $DHCPCD_QT; then
444         QMAKE_CONFIG="$QMAKE_CONFIG PREFIX=$PREFIX SYSCONFDIR=$SYSCONFDIR"
445         QMAKE_CONFIG="$QMAKE_CONFIG MANDIR=$MANDIR"
446         if ! (cd src/dhcpcd-qt; qmake $QMAKE_CONFIG dhcpcd-qt.pro); then
447                 echo "$0: failed to configure dhcpcd-qt" >&2
448                 exit 1
449         fi
450 fi