Build as a Debian package.
[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         BUILD=`uname -m`-unknown-`uname -s | tr '[:upper:]' '[:lower:]'`
90 fi
91 if [ -z "$HOST" ]; then
92         [ -z "$TARGET" ] && TARGET=$BUILD
93         HOST=$TARGET
94 fi
95 if [ -z "$TARGET" ]; then
96         [ -z "$HOST" ] && HOST=$BUILD
97         TARGET=$HOST
98 fi
99
100 if [ -z "$OS" ]; then
101         # Derive OS from cpu-manufacturer-os-kernel
102         CPU=${TARGET%%-*}
103         REST=${TARGET#*-}
104         if [ "$CPU" != "$REST" ]; then
105                 MANU=${REST%%-*}
106                 REST=${REST#*-}
107                 if [ "$MANU" != "$REST" ]; then
108                         OS=${REST%%-*}
109                         REST=${REST#*-}
110                         if [ "$OS" != "$REST" ]; then
111                                 KERNEL=${REST%%-*}
112                         else
113                                 # 3 tupple
114                                 KERNEL=$OS
115                                 OS=$MANU
116                                 MANU=
117                         fi
118                 else
119                         # 2 tupple
120                         OS=$MANU
121                         MANU=
122                 fi
123         fi
124 fi
125
126 echo "Configuring dhcpcd-ui for ... $OS"
127 rm -f $CONFIG_H $CONFIG_MK
128 echo "# $OS" >$CONFIG_MK
129 echo "/* $OS */" >$CONFIG_H
130
131 VERSION=$($SED -ne 's/VERSION=  //p' Makefile)
132 echo "#define VERSION \"$VERSION\"" >>$CONFIG_H
133
134 for x in PREFIX SYSCONFDIR BINDIR ICONDIR; do
135         eval v=\$$x
136         # Make files look nice for import
137         l=$((10 - ${#x}))
138         unset t
139         [ $l -gt 3 ] && t="     "
140         echo "$x=$t     $v" >>$CONFIG_MK
141         unset t
142         [ $l -gt 2 ] && t="     "
143         echo "#define $x$t      \"$v\"" >>$CONFIG_H
144 done
145
146 if [ -z "$CC" ]; then
147         printf "Looking for compiler ... "
148         for b in $TARGET- ""; do
149                 for cc in gcc pcc icc cc; do
150                         if type $b$cc >/dev/null 2>&1; then
151                                 CC=$b$cc
152                                 echo "$CC"
153                                 break
154                         fi
155                 done
156                 [ -n "$CC" ] && break
157         done
158         if [ -z "$CC" ]; then
159                 echo
160                 echo "no suitable compiler found - aborting" >&2
161                 exit 1
162         fi
163 else
164         echo "Using compiler $CC"
165 fi
166 echo "CC=               $CC" >>$CONFIG_MK
167
168 if [ -z "$CFLAGS" ]; then
169         CFLAGS="-O2"
170 fi
171 echo "CFLAGS=           $CFLAGS" >>$CONFIG_MK
172 if [ -n "$CPPLAGS" ]; then
173         echo "CPPLAGS=          $CPPLAGS" >>$CONFIG_MK
174 fi
175 for x in $INCLUDEDIR; do
176         echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
177 done
178
179 if [ -n "$DEBUG" -a "$DEBUG" != no -a "$DEBUG" != false ]; then
180         echo "Enabling memory debugging"
181         echo "CPPFLAGS+=        -DDEBUG_MEMORY" >>$CONFIG_MK
182 elif [ -z "$DEBUG" -a -d .git ]; then
183         printf "Found git ... "
184         DEBUG=yes
185         echo "LDADD+=           -Wl,-rpath=../libdhcpcd" >>$CONFIG_MK
186 else
187         DEBUG=no
188 fi
189 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
190         echo "Adding debugging CFLAGS"
191         cat <<EOF >>$CONFIG_MK
192 CFLAGS+=        -Wall -Wextra -Wimplicit -Wshadow -Wformat=2
193 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
194 CFLAGS+=        -Wmissing-noreturn -Wmissing-format-attribute
195 CFLAGS+=        -Wredundant-decls  -Wnested-externs
196 CFLAGS+=        -Winline -Wwrite-strings -Wcast-align -Wcast-qual
197 CFLAGS+=        -Wpointer-arith
198 CFLAGS+=        -Wdeclaration-after-statement -Wsequence-point
199 CFLAGS+=        -fno-common
200 EOF
201 fi
202
203 # Add CPPFLAGS and CFLAGS to CC for testing features
204 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
205 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
206
207 if [ -z "$STRLCPY" ]; then
208         printf "Testing for strlcpy ... "
209         cat <<EOF >_strlcpy.c
210 #include <string.h>
211 int main(void) {
212         const char s1[] = "foo";
213         char s2[10];
214         strlcpy(s2, s1, sizeof(s2));
215         return 0;
216 }
217 EOF
218         if $XCC _strlcpy.c -o _strlcpy 2>/dev/null; then
219                 STRLCPY=yes
220         else
221                 STRLCPY=no
222         fi
223         echo "$STRLCPY"
224         rm -f _strlcpy.c _strlcpy
225 fi
226 if [ "$STRLCPY" = no ]; then
227         echo "#define strlcpy(dst, src, n) snprintf(dst, n, \"%s\", src)" \
228         >>$CONFIG_H
229 fi
230
231 if [ -z "$LIBDIR" ]; then
232         printf "lib directory name ... "
233         case `readlink /lib` in
234                 "") LIBNAME="lib";;
235                 *) LIBNAME=$(basename `readlink /lib`);;
236         esac
237         echo "$LIBNAME"
238         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
239         LIBDIR=${PREFIX}/${LIBNAME}
240         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
241 else
242         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
243 fi
244
245 # We normally want to build in the static library
246 if [ "$LIBDHCPCD" = yes ]; then
247         x="-ldhcpcd"
248         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
249 else
250         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
251 fi
252 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
253
254 # Due to our use of pkg-config, pass --as-needed to ld so we only link
255 # what we actually need.
256 echo "LDADD+=   -Wl,--as-needed" >>$CONFIG_MK
257
258 # Get DBus CFLAGS
259 DBUS_CFLAGS=$(pkg-config --cflags dbus-1) || exit 1
260 DBUS_LIBS=$(pkg-config --libs dbus-1) || exit 1
261 echo "DBus CFLAGS=$DBUS_CFLAGS"
262 echo "DBus LIBS=$DBUS_LIBS"
263 echo "DBUS_CFLAGS=      $DBUS_CFLAGS" >>$CONFIG_MK
264 echo "DBUS_LIBS=        $DBUS_LIBS" >>$CONFIG_MK
265
266 UI=
267 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
268         PKG="dbus-1 gtk+-2.0"
269         if [ "$NOTIFY" = yes ]; then
270                 PKG="$PKG${PKG:+ }libnotify"
271                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
272         fi
273         GTK_CFLAGS=$(pkg-config --cflags $PKG)
274         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
275         GTK_LIBS=$(pkg-config --libs $PKG)
276         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
277         echo "GTK+ CFLAGS=$GTK_CFLAGS"
278         echo "GTK+ LIBS=$GTK_LIBS"
279         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
280         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
281         UI="$UI${UI:+ }dhcpcd-gtk"
282 fi
283
284 if [ -z "$UI" ]; then
285         echo "no UI is available, not building." >&2
286         exit 1
287 fi
288 echo "UI=               $UI" >>config.mk
289
290 echo
291 echo "   SYSCONFDIR =           $SYSCONFDIR"
292 echo "   BINDIR =               $BINDIR"
293 echo "   LIBDIR =               $LIBDIR"
294 echo "   ICONDIR =              $ICONDIR"
295 echo "   UI =                   $UI"
296 echo
297
298 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK