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