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