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