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