configure: Try to extract kernel source directory from build Makefile.
[sliver-openvswitch.git] / acinclude.m4
1 # -*- autoconf -*-
2
3 # Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 dnl OVS_ENABLE_WERROR
18 AC_DEFUN([OVS_ENABLE_WERROR],
19   [AC_ARG_ENABLE(
20      [Werror],
21      [AC_HELP_STRING([--enable-Werror], [Add -Werror to CFLAGS])],
22      [], [enable_Werror=no])
23    AC_CONFIG_COMMANDS_PRE(
24      [if test "X$enable_Werror" = Xyes; then
25         CFLAGS="$CFLAGS -Werror"
26       fi])])
27
28 dnl OVS_CHECK_LINUX
29 dnl
30 dnl Configure linux kernel source tree 
31 AC_DEFUN([OVS_CHECK_LINUX], [
32   AC_ARG_WITH([linux],
33               [AC_HELP_STRING([--with-linux=/path/to/linux],
34                               [Specify the Linux kernel build directory])])
35   AC_ARG_WITH([linux-source],
36               [AC_HELP_STRING([--with-linux-source=/path/to/linux-source],
37                               [Specify the Linux kernel source directory
38                                (usually figured out automatically from build
39                                directory)])])
40
41   # Deprecated equivalents to --with-linux, --with-linux-source.
42   AC_ARG_WITH([l26])
43   AC_ARG_WITH([l26-source])
44
45   if test X"$with_linux" != X; then
46     KBUILD=$with_linux
47   elif test X"$with_l26" != X; then
48     KBUILD=$with_l26
49     AC_MSG_WARN([--with-l26 is deprecated, please use --with-linux instead])
50   else
51     KBUILD=
52   fi
53
54   if test X"$KBUILD" != X; then
55     if test X"$with_linux_source" != X; then
56       KSRC=$with_linux_source
57     elif test X"$with_l26_source" != X; then
58       KSRC=$with_l26_source
59       AC_MSG_WARN([--with-l26-source is deprecated, please use --with-linux-source instead])
60     else
61       KSRC=
62     fi
63   elif test X"$with_linux_source" != X || test X"$with_l26_source" != X; then
64     AC_MSG_ERROR([Linux source directory may not be specified without Linux build directory])
65   fi
66
67   if test -n "$KBUILD"; then
68     KBUILD=`eval echo "$KBUILD"`
69     case $KBUILD in
70         /*) ;;
71         *) KBUILD=`pwd`/$KBUILD ;;
72     esac
73
74     # The build directory is what the user provided.
75     # Make sure that it exists.
76     AC_MSG_CHECKING([for Linux build directory])
77     if test -d "$KBUILD"; then
78         AC_MSG_RESULT([$KBUILD])
79         AC_SUBST(KBUILD)
80     else
81         AC_MSG_RESULT([no])
82         AC_ERROR([source dir $KBUILD doesn't exist])
83     fi
84
85     # Debian breaks kernel headers into "source" header and "build" headers.
86     # We want the source headers, but $KBUILD gives us the "build" headers.
87     # Use heuristics to find the source headers.
88     AC_MSG_CHECKING([for Linux source directory])
89     if test -n "$KSRC"; then
90       KSRC=`eval echo "$KSRC"`
91       case $KSRC in
92           /*) ;;
93           *) KSRC=`pwd`/$KSRC ;;
94       esac
95       if test ! -e $KSRC/include/linux/kernel.h; then
96         AC_MSG_ERROR([$KSRC is not a kernel source directory])
97       fi
98     else
99       KSRC=$KBUILD
100       if test ! -e $KSRC/include/linux/kernel.h; then
101         # Debian kernel build Makefiles tend to include a line of the form:
102         # MAKEARGS := -C /usr/src/linux-headers-3.2.0-1-common O=/usr/src/linux-headers-3.2.0-1-486
103         # First try to extract the source directory from this line.
104         KSRC=`sed -n 's/.*-C \([[^ ]]*\).*/\1/p' "$KBUILD"/Makefile`
105         if test ! -e "$KSRC"/include/linux/kernel.h; then
106           # Didn't work.  Fall back to name-based heuristics that used to work.
107           case `echo "$KBUILD" | sed 's,/*$,,'` in # (
108             */build)
109               KSRC=`echo "$KBUILD" | sed 's,/build/*$,/source,'`
110               ;; # (
111             *)
112               KSRC=`(cd $KBUILD && pwd -P) | sed 's,-[[^-]]*$,-common,'`
113               ;;
114           esac
115         fi
116       fi
117       if test ! -e "$KSRC"/include/linux/kernel.h; then
118         AC_MSG_ERROR([cannot find source directory (please use --with-linux-source)])
119       fi
120     fi
121     AC_MSG_RESULT([$KSRC])
122
123     AC_MSG_CHECKING([for kernel version])
124     version=`sed -n 's/^VERSION = //p' "$KSRC/Makefile"`
125     patchlevel=`sed -n 's/^PATCHLEVEL = //p' "$KSRC/Makefile"`
126     sublevel=`sed -n 's/^SUBLEVEL = //p' "$KSRC/Makefile"`
127     if test X"$version" = X || test X"$patchlevel" = X; then
128        AC_ERROR([cannot determine kernel version])
129     elif test X"$sublevel" = X; then
130        kversion=$version.$patchlevel
131     else
132        kversion=$version.$patchlevel.$sublevel
133     fi
134     AC_MSG_RESULT([$kversion])
135
136     if test "$version" -ge 3; then
137        : # Linux 3.x
138     elif test "$version" = 2 && test "$patchlevel" -ge 6; then
139        : # Linux 2.6.x
140     else
141        if test "$KBUILD" = "$KSRC"; then
142          AC_ERROR([Linux kernel in $KBUILD is version $kversion, but version 2.6 or later is required])
143        else
144          AC_ERROR([Linux kernel in build tree $KBUILD (source tree $KSRC) is version $kversion, but version 2.6 or later is required])
145        fi
146     fi
147     if test ! -e "$KBUILD"/include/linux/version.h || \
148        (test ! -e "$KBUILD"/include/linux/autoconf.h && \
149         test ! -e "$KBUILD"/include/generated/autoconf.h); then
150         AC_MSG_ERROR([Linux kernel source in $KBUILD is not configured])
151     fi
152     OVS_CHECK_LINUX_COMPAT
153   fi
154   AM_CONDITIONAL(LINUX_ENABLED, test -n "$KBUILD")
155 ])
156
157 dnl OVS_GREP_IFELSE(FILE, REGEX, [IF-MATCH], [IF-NO-MATCH])
158 dnl
159 dnl Greps FILE for REGEX.  If it matches, runs IF-MATCH, otherwise IF-NO-MATCH.
160 dnl If IF-MATCH is empty then it defines to OVS_DEFINE(HAVE_<REGEX>), with
161 dnl <REGEX> translated to uppercase.
162 AC_DEFUN([OVS_GREP_IFELSE], [
163   AC_MSG_CHECKING([whether $2 matches in $1])
164   if test -f $1; then
165     grep '$2' $1 >/dev/null 2>&1
166     status=$?
167     case $status in
168       0) 
169         AC_MSG_RESULT([yes])
170         m4_if([$3], [], [OVS_DEFINE([HAVE_]m4_toupper([$2]))], [$3])
171         ;;
172       1) 
173         AC_MSG_RESULT([no])
174         $4
175         ;;
176       *) 
177         AC_MSG_ERROR([grep exited with status $status])
178         ;;
179     esac
180   else
181     AC_MSG_RESULT([file not found])
182     $4
183   fi
184 ])
185
186 dnl OVS_DEFINE(NAME)
187 dnl
188 dnl Defines NAME to 1 in kcompat.h.
189 AC_DEFUN([OVS_DEFINE], [
190   echo '#define $1 1' >> datapath/linux/kcompat.h.new
191 ])
192
193 AC_DEFUN([OVS_CHECK_LOG2_H], [
194   AC_MSG_CHECKING([for $KSRC/include/linux/log2.h])
195   if test -e $KSRC/include/linux/log2.h; then
196     AC_MSG_RESULT([yes])
197     OVS_DEFINE([HAVE_LOG2_H])
198   else
199     AC_MSG_RESULT([no])
200   fi
201 ])
202
203 dnl OVS_CHECK_LINUX_COMPAT
204 dnl
205 dnl Runs various Autoconf checks on the Linux 2.6 kernel source in
206 dnl the directory in $KBUILD.
207 AC_DEFUN([OVS_CHECK_LINUX_COMPAT], [
208   rm -f datapath/linux/kcompat.h.new
209   mkdir -p datapath/linux
210   : > datapath/linux/kcompat.h.new
211
212   OVS_GREP_IFELSE([$KSRC/arch/x86/include/asm/checksum_32.h], [src_err,],
213                   [OVS_DEFINE([HAVE_CSUM_COPY_DBG])])
214
215   OVS_GREP_IFELSE([$KSRC/include/linux/err.h], [ERR_CAST])
216
217   OVS_GREP_IFELSE([$KSRC/include/linux/in.h], [ipv4_is_multicast])
218
219   OVS_GREP_IFELSE([$KSRC/include/linux/netdevice.h], [dev_disable_lro])
220   OVS_GREP_IFELSE([$KSRC/include/linux/netdevice.h], [dev_get_stats])
221   OVS_GREP_IFELSE([$KSRC/include/linux/netdevice.h], [dev_get_by_index_rcu])
222
223   OVS_GREP_IFELSE([$KSRC/include/linux/rcupdate.h], [rcu_read_lock_held], [],
224                   [OVS_GREP_IFELSE([$KSRC/include/linux/rtnetlink.h],
225                                    [rcu_read_lock_held])])
226   
227   # Check for the proto_data_valid member in struct sk_buff.  The [^@]
228   # is necessary because some versions of this header remove the
229   # member but retain the kerneldoc comment that describes it (which
230   # starts with @).  The brackets must be doubled because of m4
231   # quoting rules.
232   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [[[^@]]proto_data_valid],
233                   [OVS_DEFINE([HAVE_PROTO_DATA_VALID])])
234   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [raw],
235                   [OVS_DEFINE([HAVE_MAC_RAW])])
236   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [skb_dst(],
237                   [OVS_DEFINE([HAVE_SKB_DST_ACCESSOR_FUNCS])])
238   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], 
239                   [skb_copy_from_linear_data_offset])
240   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h],
241                   [skb_reset_tail_pointer])
242   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [skb_cow_head])
243   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [skb_transport_header],
244                   [OVS_DEFINE([HAVE_SKBUFF_HEADER_HELPERS])])
245   OVS_GREP_IFELSE([$KSRC/include/linux/icmpv6.h], [icmp6_hdr],
246                   [OVS_DEFINE([HAVE_ICMP6_HDR])])
247   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [skb_warn_if_lro],
248                   [OVS_DEFINE([HAVE_SKB_WARN_LRO])])
249   OVS_GREP_IFELSE([$KSRC/include/linux/skbuff.h], [consume_skb])
250
251   OVS_GREP_IFELSE([$KSRC/include/linux/string.h], [kmemdup], [],
252                   [OVS_GREP_IFELSE([$KSRC/include/linux/slab.h], [kmemdup])])
253
254   OVS_GREP_IFELSE([$KSRC/include/linux/types.h], [bool],
255                   [OVS_DEFINE([HAVE_BOOL_TYPE])])
256   OVS_GREP_IFELSE([$KSRC/include/linux/types.h], [__wsum],
257                   [OVS_DEFINE([HAVE_CSUM_TYPES])])
258
259   OVS_GREP_IFELSE([$KSRC/include/net/checksum.h], [csum_replace4])
260   OVS_GREP_IFELSE([$KSRC/include/net/checksum.h], [csum_unfold])
261
262   OVS_GREP_IFELSE([$KSRC/include/net/netlink.h], [NLA_NUL_STRING])
263   OVS_GREP_IFELSE([$KSRC/include/net/netlink.h], [nla_get_be16])
264   OVS_GREP_IFELSE([$KSRC/include/net/netlink.h], [nla_find_nested])
265
266   OVS_GREP_IFELSE([$KSRC/include/linux/if_vlan.h], [ADD_ALL_VLANS_CMD],
267                   [OVS_DEFINE([HAVE_VLAN_BUG_WORKAROUND])])
268
269   OVS_CHECK_LOG2_H
270
271   if cmp -s datapath/linux/kcompat.h.new \
272             datapath/linux/kcompat.h >/dev/null 2>&1; then
273     rm datapath/linux/kcompat.h.new
274   else
275     mv datapath/linux/kcompat.h.new datapath/linux/kcompat.h
276   fi
277 ])
278
279 dnl Checks for net/if_packet.h.
280 AC_DEFUN([OVS_CHECK_IF_PACKET],
281   [AC_CHECK_HEADER([net/if_packet.h],
282                    [HAVE_IF_PACKET=yes],
283                    [HAVE_IF_PACKET=no])
284    AM_CONDITIONAL([HAVE_IF_PACKET], [test "$HAVE_IF_PACKET" = yes])
285    if test "$HAVE_IF_PACKET" = yes; then
286       AC_DEFINE([HAVE_IF_PACKET], [1],
287                 [Define to 1 if net/if_packet.h is available.])
288    fi])
289
290 dnl Checks for buggy strtok_r.
291 dnl
292 dnl Some versions of glibc 2.7 has a bug in strtok_r when compiling
293 dnl with optimization that can cause segfaults:
294 dnl
295 dnl http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
296 AC_DEFUN([OVS_CHECK_STRTOK_R],
297   [AC_CACHE_CHECK(
298      [whether strtok_r macro segfaults on some inputs],
299      [ovs_cv_strtok_r_bug],
300      [AC_RUN_IFELSE(
301         [AC_LANG_PROGRAM([#include <stdio.h>
302                           #include <string.h>
303                          ],
304                          [[char string[] = ":::";
305                            char *save_ptr = (char *) 0xc0ffee;
306                            char *token1, *token2;
307                            token1 = strtok_r(string, ":", &save_ptr);
308                            token2 = strtok_r(NULL, ":", &save_ptr);
309                            freopen ("/dev/null", "w", stdout);
310                            printf ("%s %s\n", token1, token2);
311                            return 0;
312                           ]])],
313         [ovs_cv_strtok_r_bug=no],
314         [ovs_cv_strtok_r_bug=yes],
315         [ovs_cv_strtok_r_bug=yes])])
316    if test $ovs_cv_strtok_r_bug = yes; then
317      AC_DEFINE([HAVE_STRTOK_R_BUG], [1],
318                [Define if strtok_r macro segfaults on some inputs])
319    fi
320 ])
321
322 dnl ----------------------------------------------------------------------
323 dnl These macros are from GNU PSPP, with the following original license:
324 dnl Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
325 dnl This file is free software; the Free Software Foundation
326 dnl gives unlimited permission to copy and/or distribute it,
327 dnl with or without modifications, as long as this notice is preserved.
328
329 dnl OVS_CHECK_CC_OPTION([OPTION], [ACTION-IF-ACCEPTED], [ACTION-IF-REJECTED])
330 dnl Check whether the given C compiler OPTION is accepted.
331 dnl If so, execute ACTION-IF-ACCEPTED, otherwise ACTION-IF-REJECTED.
332 AC_DEFUN([OVS_CHECK_CC_OPTION],
333 [
334   m4_define([ovs_cv_name], [ovs_cv_[]m4_translit([$1], [-], [_])])dnl
335   AC_CACHE_CHECK([whether $CC accepts $1], [ovs_cv_name], 
336     [ovs_save_CFLAGS="$CFLAGS"
337      CFLAGS="$CFLAGS $1"
338      AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,)], [ovs_cv_name[]=yes], [ovs_cv_name[]=no])
339      CFLAGS="$ovs_save_CFLAGS"])
340   if test $ovs_cv_name = yes; then
341     m4_if([$2], [], [:], [$2])
342   else
343     m4_if([$3], [], [:], [$3])
344   fi
345 ])
346
347 dnl OVS_ENABLE_OPTION([OPTION])
348 dnl Check whether the given C compiler OPTION is accepted.
349 dnl If so, add it to WARNING_FLAGS.
350 dnl Example: OVS_ENABLE_OPTION([-Wdeclaration-after-statement])
351 AC_DEFUN([OVS_ENABLE_OPTION], 
352   [OVS_CHECK_CC_OPTION([$1], [WARNING_FLAGS="$WARNING_FLAGS $1"])
353    AC_SUBST([WARNING_FLAGS])])
354
355 dnl OVS_CONDITIONAL_CC_OPTION([OPTION], [CONDITIONAL])
356 dnl Check whether the given C compiler OPTION is accepted.
357 dnl If so, enable the given Automake CONDITIONAL.
358
359 dnl Example: OVS_CONDITIONAL_CC_OPTION([-Wno-unused], [HAVE_WNO_UNUSED])
360 AC_DEFUN([OVS_CONDITIONAL_CC_OPTION],
361   [OVS_CHECK_CC_OPTION(
362     [$1], [ovs_have_cc_option=yes], [ovs_have_cc_option=no])
363    AM_CONDITIONAL([$2], [test $ovs_have_cc_option = yes])])
364 dnl ----------------------------------------------------------------------
365
366 dnl Check for too-old XenServer.
367 AC_DEFUN([OVS_CHECK_XENSERVER_VERSION],
368   [AC_CACHE_CHECK([XenServer release], [ovs_cv_xsversion],
369     [if test -e /etc/redhat-release; then
370        ovs_cv_xsversion=`sed -n 's/^XenServer DDK release \([[^-]]*\)-.*/\1/p' /etc/redhat-release`
371      fi
372      if test -z "$ovs_cv_xsversion"; then
373        ovs_cv_xsversion=none
374      fi])
375   case $ovs_cv_xsversion in
376     none)
377       ;;
378
379     [[1-9]][[0-9]]* |                    dnl XenServer 10 or later
380     [[6-9]]* |                           dnl XenServer 6 or later
381     5.[[7-9]]* |                         dnl XenServer 5.7 or later
382     5.6.[[1-9]][[0-9]][[0-9]][[0-9]]* |  dnl XenServer 5.6.1000 or later
383     5.6.[[2-9]][[0-9]][[0-9]]* |         dnl XenServer 5.6.200 or later
384     5.6.1[[0-9]][[0-9]])                 dnl Xenserver 5.6.100 or later
385       ;;
386
387     *)
388       AC_MSG_ERROR([This appears to be XenServer $ovs_cv_xsversion, but only XenServer 5.6.100 or later is supported.  (If you are really using a supported version of XenServer, you may override this error message by specifying 'ovs_cv_xsversion=5.6.100' on the "configure" command line.)])
389       ;;
390   esac])
391
392 dnl OVS_MAKE_HAS_IF([if-true], [if-false])
393 dnl
394 dnl Checks whether make has the GNU make $(if condition,then,else) extension.
395 dnl Runs 'if-true' if so, 'if-false' otherwise.
396 AC_DEFUN([OVS_MAKE_HAS_IF],
397   [AC_CACHE_CHECK(
398      [whether ${MAKE-make} has GNU make \$(if) extension],
399      [ovs_cv_gnu_make_if],
400      [cat <<'EOF' > conftest.mk
401 conftest.out:
402         echo $(if x,y,z) > conftest.out
403 .PHONY: all
404 EOF
405       rm -f conftest.out
406       AS_ECHO(["$as_me:$LINENO: invoking ${MAKE-make} -f conftest.mk all:"]) >&AS_MESSAGE_LOG_FD 2>&1
407       ${MAKE-make} -f conftest.mk conftest.out >&AS_MESSAGE_LOG_FD 2>&1
408       AS_ECHO(["$as_me:$LINENO: conftest.out contains:"]) >&AS_MESSAGE_LOG_FD 2>&1
409       cat conftest.out >&AS_MESSAGE_LOG_FD 2>&1
410       result=`cat conftest.out`
411       rm -f conftest.mk conftest.out
412       if test "X$result" = "Xy"; then
413         ovs_cv_gnu_make_if=yes
414       else
415         ovs_cv_gnu_make_if=no
416       fi])
417    AS_IF([test $ovs_cv_gnu_make_if = yes], [$1], [$2])])
418
419 dnl OVS_CHECK_SPARSE_TARGET
420 dnl
421 dnl The "cgcc" script from "sparse" isn't very good at detecting the
422 dnl target for which the code is being built.  This helps it out.
423 AC_DEFUN([OVS_CHECK_SPARSE_TARGET],
424   [AC_CACHE_CHECK(
425     [target hint for cgcc],
426     [ac_cv_sparse_target],
427     [AS_CASE([`$CC -dumpmachine 2>/dev/null`],
428        [i?86-* | athlon-*], [ac_cv_sparse_target=x86],
429        [x86_64-*], [ac_cv_sparse_target=x86_64],
430        [ac_cv_sparse_target=other])])
431    AS_CASE([$ac_cv_sparse_target],
432      [x86], [SPARSEFLAGS= CGCCFLAGS=-target=i86],
433      [x86_64], [SPARSEFLAGS=-m64 CGCCFLAGS=-target=x86_64],
434      [SPARSEFLAGS= CGCCFLAGS=])
435    AC_SUBST([SPARSEFLAGS])
436    AC_SUBST([CGCCFLAGS])])
437
438 dnl OVS_ENABLE_SPARSE
439 AC_DEFUN([OVS_ENABLE_SPARSE],
440   [AC_REQUIRE([OVS_CHECK_SPARSE_TARGET])
441    OVS_MAKE_HAS_IF(
442      [AC_CONFIG_COMMANDS_PRE(
443         [: ${SPARSE=sparse}
444          AC_SUBST([SPARSE])
445          CC='$(if $(C),REAL_CC="'"$CC"'" CHECK="$(SPARSE) -I $(top_srcdir)/include/sparse $(SPARSEFLAGS)" cgcc $(CGCCFLAGS),'"$CC"')'])])])