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