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