Add configure check for gettext(3).
[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         # autoconf target triplet: cpu-vendor-os
90         BUILD=$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')
91 fi
92 : ${HOST:=$BUILD}
93
94 if [ -z "$OS" ]; then
95         echo "Deriving operating system from ... $HOST"
96         # Derive OS from cpu-vendor-[kernel-]os
97         CPU=${HOST%%-*}
98         REST=${HOST#*-}
99         if [ "$CPU" != "$REST" ]; then
100                 VENDOR=${REST%%-*}
101                 REST=${REST#*-}
102                 if [ "$VENDOR" != "$REST" ]; then
103                         # Use kernel if given, otherwise os
104                         OS=${REST%%-*}
105                 else
106                         # 2 tupple
107                         OS=$VENDOR
108                         VENDOR=
109                 fi
110         fi
111
112         # Work with cpu-kernel-os, ie Debian
113         case "$VENDOR" in
114         linux*|kfreebsd*) OS=$VENDOR; VENDOR= ;;
115         esac
116         # Special case
117         case "$OS" in
118         gnu*) OS=hurd;; # No HURD support as yet
119         esac
120 fi
121
122 echo "Configuring dhcpcd for ... $OS"
123 rm -f $CONFIG_H $CONFIG_MK
124 echo "# $OS" >$CONFIG_MK
125 echo "/* $OS */" >$CONFIG_H
126
127 VERSION=$($SED -ne 's/VERSION=  //p' Makefile)
128 echo "#define VERSION \"$VERSION\"" >>$CONFIG_H
129
130 for x in PREFIX SYSCONFDIR BINDIR ICONDIR; do
131         eval v=\$$x
132         # Make files look nice for import
133         l=$((10 - ${#x}))
134         unset t
135         [ $l -gt 3 ] && t="     "
136         echo "$x=$t     $v" >>$CONFIG_MK
137         unset t
138         [ $l -gt 2 ] && t="     "
139         echo "#define $x$t      \"$v\"" >>$CONFIG_H
140 done
141 echo "MANDIR=           ${MANDIR}" >>$CONFIG_MK
142
143 # Always obey CC.
144 # However, if CC is not specified and we are given GNU style
145 # --host or --build targets the expectation is we try and match that
146 # to a compiler.
147 if [ -n "$CC" ]; then
148         HOSTCC=
149 else
150         CC=cc
151         _COMPILERS="cc clang gcc pcc icc"
152 fi
153 if [ -n "$HOSTCC" ]; then
154         for _CC in $_COMPILERS; do
155                 _CC=$(_which "$HOSTCC$_CC")
156                 if [ -x "$_CC" ]; then
157                         CC=$_CC
158                         break
159                 fi
160         done
161 fi
162 if ! type "$CC" >/dev/null 2>&1; then
163         for _CC in $_COMPILERS; do
164                 _CC=$(_which "$_CC")
165                 if [ -x "$_CC" ]; then
166                         CC=$_CC
167                         break
168                 fi
169         done
170 fi
171
172 echo "Using compiler .. $CC"
173 cat <<EOF >_test.c
174 int main(void) {
175         return 0;
176 }
177 EOF
178 _CC=false
179 if $CC _test.c -o _test >/dev/null 2>&1; then
180         [ -x _test ] && _CC=true
181 fi
182 rm -f _test.c _test
183 if ! $_CC; then
184         echo "$CC does not create executables"
185         exit 1
186 fi
187 [ "$CC" != cc ] && echo "CC=    $CC" >>$CONFIG_MK
188 $CC --version | $SED -e '1!d'
189
190 # Set to blank, then append user config
191 # We do this so our SED call to append to XCC remains portable
192 if [ -n "$CFLAGS" ]; then
193         echo "CFLAGS=" >>$CONFIG_MK
194         echo "CFLAGS+=  $CFLAGS" >>$CONFIG_MK
195 fi
196 if [ -n "$CPPFLAGS" ]; then
197         echo "CPPFLAGS=" >>$CONFIG_MK
198         echo "CPPFLAGS+=        $CPPFLAGS" >>$CONFIG_MK
199 fi
200 if [ -n "$LDFLAGS" ]; then
201         echo "LDFLAGS=" >>$CONFIG_MK
202         echo "LDFLAGS+= $LDFLAGS" >>$CONFIG_MK
203 fi
204
205 for x in $INCLUDEDIR; do
206         echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
207 done
208
209 if [ -z "$DEBUG" -a -f .fslckout ]; then
210         printf "Found fossil checkout ... "
211         DEBUG=yes
212 else
213         DEBUG=no
214 fi
215 if [ "$DEBUG" != no -a "$DEBUG" != false ]; then
216         echo "Adding debugging CFLAGS"
217         cat <<EOF >>$CONFIG_MK
218 CFLAGS+=        -g -Wall -Wextra -Wshadow -Wformat=2
219 CFLAGS+=        -Wmissing-prototypes -Wmissing-declarations
220 CFLAGS+=        -Wmissing-noreturn -Wmissing-format-attribute
221 CFLAGS+=        -Wnested-externs
222 CFLAGS+=        -Winline -Wwrite-strings -Wcast-align -Wcast-qual
223 CFLAGS+=        -Wpointer-arith -Wstrict-overflow
224 CFLAGS+=        -Wdeclaration-after-statement
225 CFLAGS+=        -Wconversion
226 EOF
227         case "$OS" in
228         mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers
229         *)              echo "CFLAGS+=  -Wredundant-decls" >>$CONFIG_MK;;
230         esac
231 else
232         # Disable assertations
233         echo "CFLAGS+=  -DNDEBUG" >>$CONFIG_MK
234 fi
235
236 # Add CPPFLAGS and CFLAGS to CC for testing features
237 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
238 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
239
240 if [ -z "$GETTEXT" ]; then
241         printf "Testing for gettext ... "
242         cat <<EOF >_gettext.c
243 #include <libintl.h>
244 int main(void) {
245         gettext("foo");
246         return 0;
247 }
248 EOF
249         LIB_INTL=
250         if $XCC _gettext.c -o _gettext 2>/dev/null; then
251                 GETTEXT=yes
252         elif $XCC _gettext.c -o _gettext -lintl 2>/dev/null; then
253                 GETTEXT="yes (-lintl)"
254                 LIB_INTL=-lintl
255         else
256                 GETTEXT=no
257         fi
258         echo "$GETTEXT"
259         rm -f _gettext.c _gettext
260 fi
261 if [ "$GETTEXT" != no ]; then
262         echo "#define HAS_GETTEXT" >>$CONFIG_H
263         echo "LIB_INTL= $LIB_INTL" >>$CONFIG_MK
264 fi
265
266 if [ -z "$STRLCPY" ]; then
267         printf "Testing for strlcpy ... "
268         cat <<EOF >_strlcpy.c
269 #include <string.h>
270 int main(void) {
271         const char s1[] = "foo";
272         char s2[10];
273         strlcpy(s2, s1, sizeof(s2));
274         return 0;
275 }
276 EOF
277         if $XCC _strlcpy.c -o _strlcpy 2>/dev/null; then
278                 STRLCPY=yes
279         else
280                 STRLCPY=no
281         fi
282         echo "$STRLCPY"
283         rm -f _strlcpy.c _strlcpy
284 fi
285 if [ "$STRLCPY" = no ]; then
286         echo "#define strlcpy(dst, src, n) " \
287             "((size_t)snprintf(dst, n, \"%s\", src))" \
288             >>$CONFIG_H
289 fi
290
291 if [ -z "$LIBDIR" ]; then
292         printf "lib directory name ... "
293         case `readlink /lib` in
294                 "") LIBNAME="lib";;
295                 *) LIBNAME=$(basename `readlink /lib`);;
296         esac
297         echo "$LIBNAME"
298         echo "LIBNAME=  $LIBNAME" >>$CONFIG_MK
299         LIBDIR=${PREFIX}/${LIBNAME}
300         echo "LIBDIR=           \${PREFIX}/\${LIBNAME}" >>$CONFIG_MK
301 else
302         echo "LIBDIR=   $LIBDIR" >>$CONFIG_MK
303 fi
304
305 # We normally want to build in the static library
306 if [ "$LIBDHCPCD" = yes ]; then
307         x="-ldhcpcd"
308         echo "LIB_DHCPCD_INSTALL=       _libinstall" >>$CONFIG_MK
309 else
310         x="-Wl,-Bstatic -ldhcpcd -Wl,-Bdynamic"
311 fi
312 echo "LIB_DHCPCD=       $x" >>$CONFIG_MK
313
314 # Due to our use of pkg-config, pass --as-needed to ld so we only link
315 # what we actually need.
316 echo "LDADD+=           -Wl,--as-needed" >>$CONFIG_MK
317
318 UI=
319 if [ "$WITH_GTK" = yes -o -z "$WITH_GTK" ]; then
320         if pkg-config --cflags gtk+-2.0 >/dev/null 2>&1; then
321                 WITH_GTK="gtk+-2.0"
322         elif pkg-config --cflags gtk+-3.0 >/dev/null 2>&1; then
323                 WITH_GTK="gtk+-3.0"
324         fi
325 elif [ -n "$WITH_GTK" -a "$WITH_GTK" != no ]; then
326         if ! pkg-config --cflags $WITH_GTK >/dev/null; then
327                 echo "$0: no suitable GTK+ library has been found" >&2
328                 exit 1
329         fi
330 fi
331         
332 if [ -n "$WITH_GTK" ]; then
333         PKG="$WITH_GTK"
334         if [ "$NOTIFY" = yes ]; then
335                 PKG="$PKG${PKG:+ }libnotify"
336                 echo "NOTIFY=           -DNOTIFY" >>$CONFIG_MK
337         fi
338         GTK_CFLAGS=$(pkg-config --cflags $PKG)
339         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
340         GTK_LIBS=$(pkg-config --libs $PKG)
341         [ $? != 0 -a "$WITH_GTK" = yes ] && exit 1
342         echo "GTK+ CFLAGS=$GTK_CFLAGS"
343         echo "GTK+ LIBS=$GTK_LIBS"
344         echo "GTK_CFLAGS=       $GTK_CFLAGS" >>$CONFIG_MK
345         echo "GTK_LIBS= $GTK_LIBS" >>$CONFIG_MK
346         UI="$UI${UI:+ }dhcpcd-gtk"
347 fi
348
349 DHCPCD_QT=false
350 if [ "WITH_QT" = yes -o -z "$WITH_QT" ]; then
351         if type qmake >/dev/null 2>&1; then
352                 UI="$UI${UI+ }dhcpcd-qt"
353                 DHCPCD_QT=true
354         fi
355 elif [ -n "$WITH_QT" -a "$WITH_QT" != no ]; then
356         if type qmake >/dev/null 2>&1; then
357                 UI="$UI${UI+ }dhcpcd-qt"
358                 DHCPCD_QT=true
359         else
360                 echo "$0: no suitable QT library has been found" >&2
361                 exit 1
362         fi
363 fi
364
365 if [ -z "$UI" ]; then
366         echo "$0: no UI is available, not building." >&2
367         exit 1
368 fi
369 echo "UI=               $UI" >>config.mk
370
371 echo
372 echo "   SYSCONFDIR =           $SYSCONFDIR"
373 echo "   BINDIR =               $BINDIR"
374 echo "   LIBDIR =               $LIBDIR"
375 echo "   ICONDIR =              $ICONDIR"
376 echo "   MANDIR =               $MANDIR"
377 echo "   UI =                   $UI"
378 echo
379
380 echo "MKDIR=            \${TOPDIR}/mk" >>$CONFIG_MK
381
382 if $DHCPCD_QT; then
383         if ! (cd src/dhcpcd-qt; qmake dhcpcd-qt.pro); then
384                 echo "$0: failed to configure dhcpcd-qt" >&2
385                 exit 1
386         fi
387 fi