Prefer gtk-2 over gtk-3 for the time being as some functions are now marked as deprec...
[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 if ! type "$CC" >/dev/null 2>&1; then
174         echo "$CC is not an executable"
175         exit 1
176 fi
177 [ "$CC" != cc ] && echo "CC=    $CC" >>$CONFIG_MK
178 $CC --version | $SED -e '1!d'
179
180 # Set to blank, then append user config
181 # We do this so our SED call to append to XCC remains portable
182 if [ -n "$CFLAGS" ]; then
183         echo "CFLAGS=" >>$CONFIG_MK
184         echo "CFLAGS+=  $CFLAGS" >>$CONFIG_MK
185 fi
186 if [ -n "$CPPFLAGS" ]; then
187         echo "CPPFLAGS=" >>$CONFIG_MK
188         echo "CPPFLAGS+=        $CPPFLAGS" >>$CONFIG_MK
189 fi
190 if [ -n "$LDFLAGS" ]; then
191         echo "LDFLAGS=" >>$CONFIG_MK
192         echo "LDFLAGS+= $LDFLAGS" >>$CONFIG_MK
193 fi
194
195 for x in $INCLUDEDIR; do
196         echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
197 done
198
199 if [ -z "$DEBUG" -a -f .fslckout ]; then
200         printf "Found fossil checkout ... "
201         DEBUG=yes
202 else
203         DEBUG=no
204 fi
205 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
206         echo "Adding debugging CFLAGS"
207         cat <<EOF >>$CONFIG_MK
208 CFLAGS+=        -g -Wall -Wextra -Wshadow -Wformat=2
209 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
210 CFLAGS+=        -Wmissing-noreturn -Wmissing-format-attribute
211 CFLAGS+=        -Wnested-externs
212 CFLAGS+=        -Winline -Wwrite-strings -Wcast-align -Wcast-qual
213 CFLAGS+=        -Wpointer-arith -Wstrict-overflow
214 CFLAGS+=        -Wdeclaration-after-statement
215 CFLAGS+=        -Wconversion
216 EOF
217         case "$OS" in
218         mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers
219         *)              echo "CFLAGS+=  -Wredundant-decls" >>$CONFIG_MK;;
220         esac
221 fi
222
223 # Add CPPFLAGS and CFLAGS to CC for testing features
224 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
225 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
226
227 if [ -z "$STRLCPY" ]; then
228         printf "Testing for strlcpy ... "
229         cat <<EOF >_strlcpy.c
230 #include <string.h>
231 int main(void) {
232         const char s1[] = "foo";
233         char s2[10];
234         strlcpy(s2, s1, sizeof(s2));
235         return 0;
236 }
237 EOF
238         if $XCC _strlcpy.c -o _strlcpy 2>/dev/null; then
239                 STRLCPY=yes
240         else
241                 STRLCPY=no
242         fi
243         echo "$STRLCPY"
244         rm -f _strlcpy.c _strlcpy
245 fi
246 if [ "$STRLCPY" = no ]; then
247         echo "#define strlcpy(dst, src, n) snprintf(dst, n, \"%s\", src)" \
248         >>$CONFIG_H
249 fi
250
251 if [ -z "$LIBDIR" ]; then
252         printf "lib directory name ... "
253         case `readlink /lib` in
254                 "") LIBNAME="lib";;
255                 *) LIBNAME=$(basename `readlink /lib`);;
256         esac
257         echo "$LIBNAME"
258         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
259         LIBDIR=${PREFIX}/${LIBNAME}
260         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
261 else
262         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
263 fi
264
265 # We normally want to build in the static library
266 if [ "$LIBDHCPCD" = yes ]; then
267         x="-ldhcpcd"
268         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
269 else
270         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
271 fi
272 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
273
274 # Due to our use of pkg-config, pass --as-needed to ld so we only link
275 # what we actually need.
276 echo "LDADD+=   -Wl,--as-needed" >>$CONFIG_MK
277
278 # Get DBus CFLAGS
279 DBUS_CFLAGS=$(pkg-config --cflags dbus-1) || exit 1
280 DBUS_LIBS=$(pkg-config --libs dbus-1) || exit 1
281 echo "DBus CFLAGS=$DBUS_CFLAGS"
282 echo "DBus LIBS=$DBUS_LIBS"
283 echo "DBUS_CFLAGS=      $DBUS_CFLAGS" >>$CONFIG_MK
284 echo "DBUS_LIBS=        $DBUS_LIBS" >>$CONFIG_MK
285
286 UI=
287 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
288         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&1; then
289                 WITH_GTK="gtk+-2.0"
290         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&1; then
291                 WITH_GTK="gtk+-3.0"
292         fi
293 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
294         if ! pkg-config --cflags $WITH_GTK >/dev/null; then
295                 echo "$0: no suitable GTK+ library has been found" >&2
296                 exit 1
297         fi
298 fi
299
300 if [ -n "$WITH_GTK" ]; then
301         PKG="dbus-1 $WITH_GTK"
302         if [ "$NOTIFY" = yes ]; then
303                 PKG="$PKG${PKG:+ }libnotify"
304                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
305         fi
306         GTK_CFLAGS=$(pkg-config --cflags $PKG)
307         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
308         GTK_LIBS=$(pkg-config --libs $PKG)
309         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
310         echo "GTK+ CFLAGS=$GTK_CFLAGS"
311         echo "GTK+ LIBS=$GTK_LIBS"
312         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
313         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
314         UI="$UI${UI:+ }dhcpcd-gtk"
315 fi
316
317 if [ -z "$UI" ]; then
318         echo "$0: no UI is available, not building." >&2
319         exit 1
320 fi
321 echo "UI=               $UI" >>config.mk
322
323 echo
324 echo "   SYSCONFDIR =           $SYSCONFDIR"
325 echo "   BINDIR =               $BINDIR"
326 echo "   LIBDIR =               $LIBDIR"
327 echo "   ICONDIR =              $ICONDIR"
328 echo "   MANDIR =               $MANDIR"
329 echo "   UI =                   $UI"
330 echo
331
332 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK