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