datapath: Fix build with Centos 5.3 kernel.
[sliver-openvswitch.git] / acinclude.m4
1 # -*- autoconf -*-
2
3 # Copyright (c) 2008, 2009 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 Checks for --disable-userspace.
18 AC_DEFUN([OVS_CHECK_USERSPACE],
19   [AC_ARG_ENABLE(
20      [userspace],
21      [AC_HELP_STRING([--disable-userspace], 
22                      [Disable building userspace components.])],
23      [case "${enableval}" in
24         (yes) build_userspace=true ;;
25         (no)  build_userspace=false ;;
26         (*) AC_MSG_ERROR([bad value ${enableval} for --enable-userspace]) ;;
27       esac],
28      [build_userspace=true])
29    AM_CONDITIONAL([ENABLE_USERSPACE], [$build_userspace])])
30
31 dnl OVS_CHECK_LINUX(OPTION, VERSION, VARIABLE, CONDITIONAL)
32 dnl
33 dnl Configure linux kernel source tree 
34 AC_DEFUN([OVS_CHECK_LINUX], [
35   AC_ARG_WITH([$1],
36               [AC_HELP_STRING([--with-$1=/path/to/linux-$2],
37                               [Specify the linux $2 kernel sources])],
38               [path="$withval"], [path=])dnl
39   if test -n "$path"; then
40     path=`eval echo "$path"`
41
42     AC_MSG_CHECKING([for $path directory])
43     if test -d "$path"; then
44         AC_MSG_RESULT([yes])
45         $3=$path
46         AC_SUBST($3)
47     else
48         AC_MSG_RESULT([no])
49         AC_ERROR([source dir $path doesn't exist])
50     fi
51
52     AC_MSG_CHECKING([for $path kernel version])
53     patchlevel=`sed -n 's/^PATCHLEVEL = //p' "$path/Makefile"`
54     sublevel=`sed -n 's/^SUBLEVEL = //p' "$path/Makefile"`
55     AC_MSG_RESULT([2.$patchlevel.$sublevel])
56     if test "2.$patchlevel" != '$2'; then
57        AC_ERROR([Linux kernel source in $path is not version $2])
58     fi
59     if ! test -e "$path"/include/linux/version.h || \
60        ! test -e "$path"/include/linux/autoconf.h; then
61         AC_MSG_ERROR([Linux kernel source in $path is not configured])
62     fi
63     m4_if($2, [2.6], [OVS_CHECK_LINUX26_COMPAT])
64   fi
65   AM_CONDITIONAL($4, test -n "$path")
66 ])
67
68 dnl OVS_GREP_IFELSE(FILE, REGEX, IF-MATCH, IF-NO-MATCH)
69 dnl
70 dnl Greps FILE for REGEX.  If it matches, runs IF-MATCH, otherwise IF-NO-MATCH.
71 AC_DEFUN([OVS_GREP_IFELSE], [
72   AC_MSG_CHECKING([whether $2 matches in $1])
73   grep '$2' $1 >/dev/null 2>&1
74   status=$?
75   case $status in
76     0) 
77       AC_MSG_RESULT([yes])
78       $3
79       ;;
80     1) 
81       AC_MSG_RESULT([no])
82       $4
83       ;;
84     *) 
85       AC_MSG_ERROR([grep exited with status $status]) 
86       ;;
87   esac
88 ])
89
90 dnl OVS_DEFINE(NAME)
91 dnl
92 dnl Defines NAME to 1 in kcompat.h.
93 AC_DEFUN([OVS_DEFINE], [
94   echo '#define $1 1' >> datapath/linux-2.6/kcompat.h.new
95 ])
96
97 AC_DEFUN([OVS_CHECK_VETH], [
98   AC_MSG_CHECKING([whether to build veth module])
99   if test "$sublevel" = 18; then
100     AC_MSG_RESULT([yes])
101     AC_SUBST([BUILD_VETH], 1)
102   else
103     AC_MSG_RESULT([no])
104   fi
105 ])
106
107 AC_DEFUN([OVS_CHECK_LOG2_H], [
108   AC_MSG_CHECKING([for $KSRC26/include/linux/log2.h])
109   if test -e $KSRC26/include/linux/log2.h; then
110     AC_MSG_RESULT([yes])
111     OVS_DEFINE([HAVE_LOG2_H])
112   else
113     AC_MSG_RESULT([no])
114   fi
115 ])
116
117 dnl OVS_CHECK_LINUX26_COMPAT
118 dnl
119 dnl Runs various Autoconf checks on the Linux 2.6 kernel source in
120 dnl the directory in $KSRC26.
121 AC_DEFUN([OVS_CHECK_LINUX26_COMPAT], [
122   rm -f datapath/linux-2.6/kcompat.h.new
123   mkdir -p datapath/linux-2.6
124   : > datapath/linux-2.6/kcompat.h.new
125   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_transport_header],
126                   [OVS_DEFINE([HAVE_SKBUFF_HEADER_HELPERS])])
127   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [raw],
128                   [OVS_DEFINE([HAVE_MAC_RAW])])
129   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], 
130                   [skb_copy_from_linear_data_offset],
131                   [OVS_DEFINE([HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET])])
132   OVS_GREP_IFELSE([$KSRC26/include/net/netlink.h], [NLA_NUL_STRING],
133                   [OVS_DEFINE([HAVE_NLA_NUL_STRING])])
134   OVS_GREP_IFELSE([$KSRC26/include/linux/err.h], [ERR_CAST],
135                   [OVS_DEFINE([HAVE_ERR_CAST])])
136   OVS_GREP_IFELSE([$KSRC26/include/net/checksum.h], [csum_unfold],
137                   [OVS_DEFINE([HAVE_CSUM_UNFOLD])])
138   OVS_GREP_IFELSE([$KSRC26/include/linux/skbuff.h], [skb_cow],
139                   [OVS_DEFINE([HAVE_SKB_COW])])
140   OVS_CHECK_LOG2_H
141   OVS_CHECK_VETH
142   if cmp -s datapath/linux-2.6/kcompat.h.new \
143             datapath/linux-2.6/kcompat.h >/dev/null 2>&1; then
144     rm datapath/linux-2.6/kcompat.h.new
145   else
146     mv datapath/linux-2.6/kcompat.h.new datapath/linux-2.6/kcompat.h
147   fi
148 ])
149
150 dnl Checks for net/if_packet.h.
151 AC_DEFUN([OVS_CHECK_IF_PACKET],
152   [AC_CHECK_HEADER([net/if_packet.h],
153                    [HAVE_IF_PACKET=yes],
154                    [HAVE_IF_PACKET=no])
155    AM_CONDITIONAL([HAVE_IF_PACKET], [test "$HAVE_IF_PACKET" = yes])
156    if test "$HAVE_IF_PACKET" = yes; then
157       AC_DEFINE([HAVE_IF_PACKET], [1],
158                 [Define to 1 if net/if_packet.h is available.])
159    fi])
160
161 dnl Checks for buggy strtok_r.
162 dnl
163 dnl Some versions of glibc 2.7 has a bug in strtok_r when compiling
164 dnl with optimization that can cause segfaults:
165 dnl
166 dnl http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
167 AC_DEFUN([OVS_CHECK_STRTOK_R],
168   [AC_CACHE_CHECK(
169      [whether strtok_r macro segfaults on some inputs],
170      [ovs_cv_strtok_r_bug],
171      [AC_RUN_IFELSE(
172         [AC_LANG_PROGRAM([#include <stdio.h>
173                           #include <string.h>
174                          ],
175                          [[char string[] = ":::";
176                            char *save_ptr = (char *) 0xc0ffee;
177                            char *token1, *token2;
178                            token1 = strtok_r(string, ":", &save_ptr);
179                            token2 = strtok_r(NULL, ":", &save_ptr);
180                            printf ("%s %s\n", token1, token2);
181                            return 0;
182                           ]])],
183         [ovs_cv_strtok_r_bug=no],
184         [ovs_cv_strtok_r_bug=yes],
185         [ovs_cv_strtok_r_bug=yes])])
186    if test $ovs_cv_strtok_r_bug = yes; then
187      AC_DEFINE([HAVE_STRTOK_R_BUG], [1],
188                [Define if strtok_r macro segfaults on some inputs])
189    fi
190 ])
191
192 dnl ----------------------------------------------------------------------
193 dnl These macros are from GNU PSPP, with the following original license:
194 dnl Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
195 dnl This file is free software; the Free Software Foundation
196 dnl gives unlimited permission to copy and/or distribute it,
197 dnl with or without modifications, as long as this notice is preserved.
198
199 dnl OVS_CHECK_CC_OPTION([OPTION], [ACTION-IF-ACCEPTED], [ACTION-IF-REJECTED])
200 dnl Check whether the given C compiler OPTION is accepted.
201 dnl If so, execute ACTION-IF-ACCEPTED, otherwise ACTION-IF-REJECTED.
202 AC_DEFUN([OVS_CHECK_CC_OPTION],
203 [
204   m4_define([ovs_cv_name], [ovs_cv_[]m4_translit([$1], [-], [_])])dnl
205   AC_CACHE_CHECK([whether $CC accepts $1], [ovs_cv_name], 
206     [ovs_save_CFLAGS="$CFLAGS"
207      CFLAGS="$CFLAGS $1"
208      AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,)], [ovs_cv_name[]=yes], [ovs_cv_name[]=no])
209      CFLAGS="$ovs_save_CFLAGS"])
210   if test $ovs_cv_name = yes; then
211     m4_if([$2], [], [;], [$2])
212   else
213     m4_if([$3], [], [:], [$3])
214   fi
215 ])
216
217 dnl OVS_ENABLE_OPTION([OPTION])
218 dnl Check whether the given C compiler OPTION is accepted.
219 dnl If so, add it to CFLAGS.
220 dnl Example: OVS_ENABLE_OPTION([-Wdeclaration-after-statement])
221 AC_DEFUN([OVS_ENABLE_OPTION], 
222   [OVS_CHECK_CC_OPTION([$1], [CFLAGS="$CFLAGS $1"])])
223 dnl ----------------------------------------------------------------------