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