datapath: Fix build with backported dev_get_stats().
[sliver-openvswitch.git] / acinclude.m4
1 # -*- autoconf -*-
2
3 # Copyright (c) 2008, 2009, 2010 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_CHECK_LINUX26
18 dnl
19 dnl Configure linux kernel source tree 
20 AC_DEFUN([OVS_CHECK_LINUX26], [
21   AC_ARG_WITH([l26],
22               [AC_HELP_STRING([--with-l26=/path/to/linux-2.6],
23                               [Specify the linux 2.6 kernel sources])],
24               [KBUILD26="$withval"], [KBUILD26=])dnl
25   if test -n "$KBUILD26"; then
26     KBUILD26=`eval echo "$KBUILD26"`
27     case $KBUILD26 in
28         /*) ;;
29         *) KBUILD26=`pwd`/$KBUILD26 ;;
30     esac
31
32     # The build directory is what the user provided.
33     # Make sure that it exists.
34     AC_MSG_CHECKING([for Linux 2.6 build directory])
35     if test -d "$KBUILD26"; then
36         AC_MSG_RESULT([$KBUILD26])
37         AC_SUBST(KBUILD26)
38     else
39         AC_MSG_RESULT([no])
40         AC_ERROR([source dir $KBUILD26 doesn't exist])
41     fi
42
43     # Debian breaks kernel headers into "source" header and "build" headers.
44     # We want the source headers, but $KBUILD26 gives us the "build" headers.
45     # Use heuristics to find the source headers.
46     AC_MSG_CHECKING([for Linux 2.6 source directory])
47     KSRC26=$KBUILD26
48     if test ! -e $KSRC26/include/linux/kernel.h; then
49       case `echo "$KBUILD26" | sed 's,/*$,,'` in # (
50         */build)
51           KSRC26=`echo "$KBUILD26" | sed 's,/build/*$,/source,'`
52           ;; # (
53         *)
54           KSRC26=`(cd $KBUILD26 && pwd -P) | sed 's,-[[^-]]*$,-common,'`
55           ;;
56       esac
57       if test ! -e $KSRC26/include/linux/kernel.h; then
58         AC_MSG_ERROR([cannot find source directory])
59       fi
60     fi
61     AC_MSG_RESULT([$KSRC26])
62
63     AC_MSG_CHECKING([for kernel version])
64     patchlevel=`sed -n 's/^PATCHLEVEL = //p' "$KSRC26/Makefile"`
65     sublevel=`sed -n 's/^SUBLEVEL = //p' "$KSRC26/Makefile"`
66     if test -z "$patchlevel" || test -z "$sublevel"; then
67        AC_ERROR([cannot determine kernel version])
68     fi
69     AC_MSG_RESULT([2.$patchlevel.$sublevel])
70     if test "2.$patchlevel" != '2.6'; then
71        if test "$BUILD26" = "$KSRC26"; then
72          AC_ERROR([Linux kernel in $KBUILD26 is not version 2.6])
73        else
74          AC_ERROR([Linux kernel in build tree $KBUILD26 (source tree $KSRC26) is not version 2.6])
75        fi
76     fi
77     if test ! -e "$KBUILD26"/include/linux/version.h || \
78        (test ! -e "$KBUILD26"/include/linux/autoconf.h && \
79         test ! -e "$KBUILD26"/include/generated/autoconf.h); then
80         AC_MSG_ERROR([Linux kernel source in $KBUILD26 is not configured])
81     fi
82     OVS_CHECK_LINUX26_COMPAT
83   fi
84   AM_CONDITIONAL(L26_ENABLED, test -n "$KBUILD26")
85 ])
86
87 dnl OVS_GREP_IFELSE(FILE, REGEX, IF-MATCH, IF-NO-MATCH)
88 dnl
89 dnl Greps FILE for REGEX.  If it matches, runs IF-MATCH, otherwise IF-NO-MATCH.
90 AC_DEFUN([OVS_GREP_IFELSE], [
91   AC_MSG_CHECKING([whether $2 matches in $1])
92   grep '$2' $1 >/dev/null 2>&1
93   status=$?
94   case $status in
95     0) 
96       AC_MSG_RESULT([yes])
97       $3
98       ;;
99     1) 
100       AC_MSG_RESULT([no])
101       $4
102       ;;
103     *) 
104       AC_MSG_ERROR([grep exited with status $status]) 
105       ;;
106   esac
107 ])
108
109 dnl OVS_DEFINE(NAME)
110 dnl
111 dnl Defines NAME to 1 in kcompat.h.
112 AC_DEFUN([OVS_DEFINE], [
113   echo '#define $1 1' >> datapath/linux-2.6/kcompat.h.new
114 ])
115
116 AC_DEFUN([OVS_CHECK_VETH], [
117   AC_MSG_CHECKING([whether to build veth module])
118   if test "$sublevel" = 18; then
119     AC_MSG_RESULT([yes])
120     AC_SUBST([BUILD_VETH], 1)
121   else
122     AC_MSG_RESULT([no])
123   fi
124 ])
125
126 AC_DEFUN([OVS_CHECK_LOG2_H], [
127   AC_MSG_CHECKING([for $KSRC26/include/linux/log2.h])
128   if test -e $KSRC26/include/linux/log2.h; then
129     AC_MSG_RESULT([yes])
130     OVS_DEFINE([HAVE_LOG2_H])
131   else
132     AC_MSG_RESULT([no])
133   fi
134 ])
135
136 dnl OVS_CHECK_LINUX26_COMPAT
137 dnl
138 dnl Runs various Autoconf checks on the Linux 2.6 kernel source in
139 dnl the directory in $KBUILD26.
140 AC_DEFUN([OVS_CHECK_LINUX26_COMPAT], [
141   rm -f datapath/linux-2.6/kcompat.h.new
142   mkdir -p datapath/linux-2.6
143   : > datapath/linux-2.6/kcompat.h.new
144
145   OVS_GREP_IFELSE([$KSRC26/include/linux/err.h], [ERR_CAST],
146                   [OVS_DEFINE([HAVE_ERR_CAST])])
147
148   OVS_GREP_IFELSE([$KSRC26/include/linux/in.h], [ipv4_is_multicast],
149                   [OVS_DEFINE([HAVE_IPV4_IS_MULTICAST])])
150
151   OVS_GREP_IFELSE([$KSRC26/include/linux/netdevice.h], [dev_disable_lro],
152                   [OVS_DEFINE([HAVE_DEV_DISABLE_LRO])])
153   OVS_GREP_IFELSE([$KSRC26/include/linux/netdevice.h], [dev_get_stats],
154                   [OVS_DEFINE([HAVE_DEV_GET_STATS])])
155
156   # Check for the proto_data_valid member in struct sk_buff.  The [^@]
157   # is necessary because some versions of this header remove the
158   # member but retain the kerneldoc comment that describes it (which
159   # starts with @).  The brackets must be doubled because of m4
160   # quoting rules.
161   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [[[^@]]proto_data_valid],
162                   [OVS_DEFINE([HAVE_PROTO_DATA_VALID])])
163   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [raw],
164                   [OVS_DEFINE([HAVE_MAC_RAW])])
165   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_dst(],
166                   [OVS_DEFINE([HAVE_SKB_DST_ACCESSOR_FUNCS])])
167   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], 
168                   [skb_copy_from_linear_data_offset],
169                   [OVS_DEFINE([HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET])])
170   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_cow],
171                   [OVS_DEFINE([HAVE_SKB_COW])])
172   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_transport_header],
173                   [OVS_DEFINE([HAVE_SKBUFF_HEADER_HELPERS])])
174   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_warn_if_lro],
175                   [OVS_DEFINE([HAVE_SKB_WARN_LRO])])
176
177   OVS_GREP_IFELSE([$KSRC26/include/linux/string.h $KSRC26/include/linux/slab.h],
178                   [kmemdup], [OVS_DEFINE([HAVE_KMEMDUP])])
179
180   OVS_GREP_IFELSE([$KSRC26/include/linux/types.h], [bool],
181                   [OVS_DEFINE([HAVE_BOOL_TYPE])])
182
183   OVS_GREP_IFELSE([$KSRC26/include/net/checksum.h], [csum_unfold],
184                   [OVS_DEFINE([HAVE_CSUM_UNFOLD])])
185
186   OVS_GREP_IFELSE([$KSRC26/include/net/netlink.h], [NLA_NUL_STRING],
187                   [OVS_DEFINE([HAVE_NLA_NUL_STRING])])
188   OVS_GREP_IFELSE([$KSRC26/include/net/netlink.h], [nla_get_be16],
189                   [OVS_DEFINE([HAVE_NLA_GET_BE16])])
190
191   OVS_CHECK_LOG2_H
192   OVS_CHECK_VETH
193
194   if cmp -s datapath/linux-2.6/kcompat.h.new \
195             datapath/linux-2.6/kcompat.h >/dev/null 2>&1; then
196     rm datapath/linux-2.6/kcompat.h.new
197   else
198     mv datapath/linux-2.6/kcompat.h.new datapath/linux-2.6/kcompat.h
199   fi
200 ])
201
202 dnl Checks for net/if_packet.h.
203 AC_DEFUN([OVS_CHECK_IF_PACKET],
204   [AC_CHECK_HEADER([net/if_packet.h],
205                    [HAVE_IF_PACKET=yes],
206                    [HAVE_IF_PACKET=no])
207    AM_CONDITIONAL([HAVE_IF_PACKET], [test "$HAVE_IF_PACKET" = yes])
208    if test "$HAVE_IF_PACKET" = yes; then
209       AC_DEFINE([HAVE_IF_PACKET], [1],
210                 [Define to 1 if net/if_packet.h is available.])
211    fi])
212
213 dnl Checks for buggy strtok_r.
214 dnl
215 dnl Some versions of glibc 2.7 has a bug in strtok_r when compiling
216 dnl with optimization that can cause segfaults:
217 dnl
218 dnl http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
219 AC_DEFUN([OVS_CHECK_STRTOK_R],
220   [AC_CACHE_CHECK(
221      [whether strtok_r macro segfaults on some inputs],
222      [ovs_cv_strtok_r_bug],
223      [AC_RUN_IFELSE(
224         [AC_LANG_PROGRAM([#include <stdio.h>
225                           #include <string.h>
226                          ],
227                          [[char string[] = ":::";
228                            char *save_ptr = (char *) 0xc0ffee;
229                            char *token1, *token2;
230                            token1 = strtok_r(string, ":", &save_ptr);
231                            token2 = strtok_r(NULL, ":", &save_ptr);
232                            freopen ("/dev/null", "w", stdout);
233                            printf ("%s %s\n", token1, token2);
234                            return 0;
235                           ]])],
236         [ovs_cv_strtok_r_bug=no],
237         [ovs_cv_strtok_r_bug=yes],
238         [ovs_cv_strtok_r_bug=yes])])
239    if test $ovs_cv_strtok_r_bug = yes; then
240      AC_DEFINE([HAVE_STRTOK_R_BUG], [1],
241                [Define if strtok_r macro segfaults on some inputs])
242    fi
243 ])
244
245 dnl ----------------------------------------------------------------------
246 dnl These macros are from GNU PSPP, with the following original license:
247 dnl Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
248 dnl This file is free software; the Free Software Foundation
249 dnl gives unlimited permission to copy and/or distribute it,
250 dnl with or without modifications, as long as this notice is preserved.
251
252 dnl OVS_CHECK_CC_OPTION([OPTION], [ACTION-IF-ACCEPTED], [ACTION-IF-REJECTED])
253 dnl Check whether the given C compiler OPTION is accepted.
254 dnl If so, execute ACTION-IF-ACCEPTED, otherwise ACTION-IF-REJECTED.
255 AC_DEFUN([OVS_CHECK_CC_OPTION],
256 [
257   m4_define([ovs_cv_name], [ovs_cv_[]m4_translit([$1], [-], [_])])dnl
258   AC_CACHE_CHECK([whether $CC accepts $1], [ovs_cv_name], 
259     [ovs_save_CFLAGS="$CFLAGS"
260      CFLAGS="$CFLAGS $1"
261      AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,)], [ovs_cv_name[]=yes], [ovs_cv_name[]=no])
262      CFLAGS="$ovs_save_CFLAGS"])
263   if test $ovs_cv_name = yes; then
264     m4_if([$2], [], [:], [$2])
265   else
266     m4_if([$3], [], [:], [$3])
267   fi
268 ])
269
270 dnl OVS_ENABLE_OPTION([OPTION])
271 dnl Check whether the given C compiler OPTION is accepted.
272 dnl If so, add it to WARNING_FLAGS.
273 dnl Example: OVS_ENABLE_OPTION([-Wdeclaration-after-statement])
274 AC_DEFUN([OVS_ENABLE_OPTION], 
275   [OVS_CHECK_CC_OPTION([$1], [WARNING_FLAGS="$WARNING_FLAGS $1"])
276    AC_SUBST([WARNING_FLAGS])])
277
278 dnl OVS_CONDITIONAL_CC_OPTION([OPTION], [CONDITIONAL])
279 dnl Check whether the given C compiler OPTION is accepted.
280 dnl If so, enable the given Automake CONDITIONAL.
281
282 dnl Example: OVS_CONDITIONAL_CC_OPTION([-Wno-unused], [HAVE_WNO_UNUSED])
283 AC_DEFUN([OVS_CONDITIONAL_CC_OPTION],
284   [OVS_CHECK_CC_OPTION(
285     [$1], [ovs_have_cc_option=yes], [ovs_have_cc_option=no])
286    AM_CONDITIONAL([$2], [test $ovs_have_cc_option = yes])])
287 dnl ----------------------------------------------------------------------