Add support for a configure script
[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
10 for x; do
11         opt=${x%%=*}
12         var=${x#*=}
13         case "$opt" in
14         --os|OS) OS=$var;;
15         --with-cc|CC) CC=$var;;
16         --debug) DEBUG=$var;;
17         --disable-debug) DEBUG=no;;
18         --enable-debug) DEBUG=yes;;
19         --prefix) PREFIX=$var;;
20         --sysconfdir) SYSCONFDIR=$var;;
21         --bindir) BINDIR=$var;;
22         --libexecdir) LIBEXECDIR=$var;;
23         --statedir|--localstatedir) STATEDIR=$var;;
24         --rundir) RUNDIR=$var;;
25         --mandir) MANDIR=$var;;
26         --with-ccopts|CFLAGS) CFLAGS=$var;;
27         CPPFLAGS) CPPFLAGS=$var;;
28         --build) BUILD=$var;;
29         --host) HOST=$var;;
30         --target) TARGET=$var;;
31         --libdir) LIBDIR=$var;;
32         --datadir|--infodir) ;; # ignore autotools
33         --icondir) ICONDIR=$var;;
34         --with-gtk|--with-gtk+) WITH_GTK=${var:-yes};;
35         --without-gtk|--without-gtk+) WITH_GTK=no;;
36         --enable-notification) NOTIFY=yes;;
37         --disable-notification) NOTIFY=no;;
38         --help) echo "See the README file for available options"; exit 0;;
39         *) echo "$0: WARNING: unknown option $opt" >&2;;
40         esac
41 done
42
43 : ${INSTALL:=install}
44 : ${SED:=sed}
45
46 : ${PREFIX:=/usr/local}
47 : ${SYSCONFDIR:=$PREFIX/etc}
48 : ${BINDIR:=$PREFIX/bin}
49 : ${LIBEXECDIR:=$PREFIX/libexec}
50 : ${STATEDIR:=/var}
51 : ${MANDIR:=${PREFIX:-/usr}/share/man}
52
53 : ${ICONDIR:=$PREFIX/share/dhcpcd/icons}
54
55 _which()
56 {
57         x="$(which "$1" 2>/dev/null)"
58         if [ -n "$x" ]; then
59                 echo "$x"
60                 return 0
61         fi
62         for x in /sbin/"$1" /usr/sbin/"$1" \
63                 /usr/pkg/sbin/"$1" /usr/local/sbin/"$1"
64         do
65                 if [ -e "$x" ]; then
66                         echo "$x"
67                         return 0
68                 fi
69         done
70         return 1
71 }
72
73 CONFIG_H=config.h
74 CONFIG_MK=config.mk
75
76 if [ -z "$BUILD" ]; then
77         BUILD=`uname -m`-unknown-`uname -s | tr '[:upper:]' '[:lower:]'`
78 fi
79 if [ -z "$HOST" ]; then
80         [ -z "$TARGET" ] && TARGET=$BUILD
81         HOST=$TARGET
82 fi
83 if [ -z "$TARGET" ]; then
84         [ -z "$HOST" ] && HOST=$BUILD
85         TARGET=$HOST
86 fi
87
88 if [ -z "$OS" ]; then
89         # Derive OS from cpu-manufacturer-os-kernel
90         CPU=${TARGET%%-*}
91         REST=${TARGET#*-}
92         MANU=${REST%%-*}
93         REST=${REST#*-}
94         OS=${REST%%-*}
95         REST=${REST#*-}
96         KERNEL=${REST%%-*}
97 fi
98
99 echo "Configuring dhcpcd-dbus for ... $OS"
100 rm -f $CONFIG_H $CONFIG_MK
101 echo "# $OS" >$CONFIG_MK
102 echo "/* $OS */" >$CONFIG_H
103
104 for x in PREFIX SYSCONFDIR BINDIRi ICONDIR; do
105         eval v=\$$x
106         # Make files look nice for import
107         l=$((10 - ${#x}))
108         unset t
109         [ $l -gt 3 ] && t="     "
110         echo "$x=$t     $v" >>$CONFIG_MK
111         unset t
112         [ $l -gt 2 ] && t="     "
113         echo "#define $x$t      \"$v\"" >>$CONFIG_H
114 done
115
116 if [ -z "$CC" ]; then
117         printf "Looking for compiler ... "
118         for b in $TARGET- ""; do
119                 for cc in gcc pcc icc cc; do
120                         if type $b$cc >/dev/null 2>&1; then
121                                 CC=$b$cc
122                                 echo "$CC"
123                                 break
124                         fi
125                 done
126                 [ -n "$CC" ] && break
127         done
128         if [ -z "$CC" ]; then
129                 echo
130                 echo "no suitable compiler found - aborting" >&2
131                 exit 1
132         fi
133 else
134         echo "Using compiler $CC"
135 fi
136 echo "CC=               $CC" >>$CONFIG_MK
137
138 if [ -z "$CFLAGS"]; then
139         CFLAGS="-O2"
140 fi
141 echo "CFLAGS=           $CFLAGS" >>$CONFIG_MK
142 if [ -n "$CPPLAGS" ]; then
143         echo "CPPLAGS=          $CPPLAGS" >>$CONFIG_MK
144 fi
145
146 if [ -n "$DEBUG" -a "$DEBUG" != no -a "$DEBUG" != false ]; then
147         echo "Enabling memory debugging"
148         echo "CPPFLAGS+=        -DDEBUG_MEMORY" >>$CONFIG_MK
149 elif [ -z "$DEBUG" -a -d .git ]; then
150         printf "Found git ... "
151         DEBUG=yes
152         echo "LDADD+=           -Wl,-rpath=../libdhcpcd" >>$CONFIG_MK
153 else
154         DEBUG=no
155 fi
156 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
157         echo "Adding debugging CFLAGS"
158         cat <<EOF >>$CONFIG_MK
159 CFLAGS+=        -Wall -Wextra -Wimplicit -Wshadow -Wformat=2
160 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
161 CFLAGS+=        -Wmissing-noreturn -Wmissing-format-attribute
162 CFLAGS+=        -Wredundant-decls  -Wnested-externs
163 CFLAGS+=        -Winline -Wwrite-strings -Wcast-align -Wcast-qual
164 CFLAGS+=        -Wpointer-arith
165 CFLAGS+=        -Wdeclaration-after-statement -Wsequence-point
166 CFLAGS+=        -fno-common
167 EOF
168 fi
169
170 # Add CPPFLAGS and CFLAGS to CC for testing features
171 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
172 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
173
174 if [ -z "$STRLCPY" ]; then
175         printf "Testing for strlcpy ... "
176         cat <<EOF >_strlcpy.c
177 #include <string.h>
178 int main(void) {
179         const char s1[] = "foo";
180         char s2[10];
181         strlcpy(s2, s1, sizeof(s2));
182         return 0;
183 }
184 EOF
185         if $XCC _strlcpy.c -o _strlcpy 2>/dev/null; then
186                 STRLCPY=yes
187         else
188                 STRLCPY=no
189         fi
190         echo "$STRLCPY"
191         rm -f _strlcpy.c _strlcpy
192 fi
193 if [ "$STRLCPY" = no ]; then
194         echo "#define strlcpy(dst, src, n) snprintf(dst, n, \"%s\", src)" \
195         >>$CONFIG_H
196 fi
197
198 if [ -z "$LIBDIR" ]; then
199         printf "lib directory name ... "
200         case `readlink /lib` in
201                 "") LIBNAME="lib";;
202                 *) LIBNAME=$(basename `readlink /lib`);;
203         esac
204         echo "$LIBNAME"
205         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
206         LIBDIR=${PREFIX}/${LIBNAME}
207         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
208 else
209         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
210 fi
211
212 # We normally want to build in the static library
213 if [ "$LIBDHCPCD" = yes ]; then
214         x="-ldhcpcd"
215         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
216 else
217         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
218 fi
219 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
220
221 # Get DBus CFLAGS
222 DBUS_CFLAGS=$(pkg-config --cflags dbus-1) || exit 1
223 DBUS_LIBS=$(pkg-config --libs dbus-1) || exit 1
224 echo "DBus CFLAGS=$DBUS_CFLAGS"
225 echo "DBus LIBS=$DBUS_LIBS"
226 echo "DBUS_CFLAGS=      $DBUS_CFLAGS" >>$CONFIG_MK
227 echo "DBUS_LIBS=        $DBUS_LIBS" >>$CONFIG_MK
228
229 UI=
230 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
231         PKG="gtk+-2.0"
232         if [ "$NOTIFY" = yes ]; then
233                 PKG="$PKG${PKG:+ }libnotify"
234                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
235         fi
236         GTK_CFLAGS=$(pkg-config --cflags $PKG)
237         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
238         GTK_LIBS=$(pkg-config --libs $PKG)
239         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
240         echo "GTK+ CFLAGS=$GTK_CFLAGS"
241         echo "GTK+ LIBS=$GTK_LIBS"
242         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
243         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
244         UI="$UI${UI:+ }dhcpcd-gtk"
245 fi
246
247 if [ -z "$UI" ]; then
248         echo "no UI is available, not building." >&2
249         exit 1
250 fi
251 echo "UI=               $UI" >>config.mk
252
253 echo
254 echo "   SYSCONFDIR =           $SYSCONFDIR"
255 echo "   BINDIR =               $BINDIR"
256 echo "   LIBDIR =               $LIBDIR"
257 echo "   UI =                   $UI"
258 echo
259
260 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK