98e5ce9be654350aceb7043fc22c115c2311a830
[cascardo/ovs.git] / m4 / openvswitch.m4
1 # -*- autoconf -*-
2
3 # Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 m4_include([m4/compat.at])
18
19 dnl Checks for --enable-coverage and updates CFLAGS and LDFLAGS appropriately.
20 AC_DEFUN([OVS_CHECK_COVERAGE],
21   [AC_REQUIRE([AC_PROG_CC])
22    AC_ARG_ENABLE(
23      [coverage],
24      [AC_HELP_STRING([--enable-coverage],
25                      [Enable gcov coverage tool.])],
26      [case "${enableval}" in
27         (yes) coverage=true ;;
28         (no)  coverage=false ;;
29         (*) AC_MSG_ERROR([bad value ${enableval} for --enable-coverage]) ;;
30       esac],
31      [coverage=false])
32    if $coverage; then
33      # Autoconf by default puts "-g -O2" in CFLAGS.  We need to remove the -O2
34      # option for coverage to be useful.  This does it without otherwise
35      # interfering with anything that the user might have put there.
36      old_CFLAGS=$CFLAGS
37      CFLAGS=
38      for option in $old_CFLAGS; do
39         case $option in
40             (-O2) ;;
41             (*) CFLAGS="$CFLAGS $option" ;;
42         esac
43      done
44
45      OVS_CFLAGS="$OVS_CFLAGS --coverage"
46      OVS_LDFLAGS="$OVS_LDFLAGS --coverage"
47    fi])
48
49 dnl Checks for --enable-ndebug and defines NDEBUG if it is specified.
50 AC_DEFUN([OVS_CHECK_NDEBUG],
51   [AC_ARG_ENABLE(
52      [ndebug],
53      [AC_HELP_STRING([--enable-ndebug],
54                      [Disable debugging features for max performance])],
55      [case "${enableval}" in
56         (yes) ndebug=true ;;
57         (no)  ndebug=false ;;
58         (*) AC_MSG_ERROR([bad value ${enableval} for --enable-ndebug]) ;;
59       esac],
60      [ndebug=false])
61    AM_CONDITIONAL([NDEBUG], [test x$ndebug = xtrue])])
62
63 dnl Checks for ESX.
64 AC_DEFUN([OVS_CHECK_ESX],
65   [AC_CHECK_HEADER([vmware.h],
66                    [ESX=yes],
67                    [ESX=no])
68    AM_CONDITIONAL([ESX], [test "$ESX" = yes])
69    if test "$ESX" = yes; then
70       AC_DEFINE([ESX], [1], [Define to 1 if building on ESX.])
71    fi])
72
73 dnl Checks for MSVC x64 compiler.
74 AC_DEFUN([OVS_CHECK_WIN64],
75   [AC_CACHE_CHECK(
76     [for MSVC x64 compiler],
77     [cl_cv_x64],
78     [dnl "cl" writes x64 output to stdin:
79      if (cl) 2>&1 | grep 'x64' >/dev/null 2>&1; then
80        cl_cv_x64=yes
81        MSVC64_LDFLAGS=" /MACHINE:X64 "
82      else
83        cl_cv_x64=no
84        MSVC64_LDFLAGS=""
85      fi])
86      AC_SUBST([MSVC64_LDFLAGS])
87 ])
88
89 dnl Checks for WINDOWS.
90 AC_DEFUN([OVS_CHECK_WIN32],
91   [AC_CHECK_HEADER([windows.h],
92                    [WIN32=yes],
93                    [WIN32=no])
94    AM_CONDITIONAL([WIN32], [test "$WIN32" = yes])
95    if test "$WIN32" = yes; then
96       AC_ARG_WITH([pthread],
97          [AS_HELP_STRING([--with-pthread=DIR],
98             [root of the pthread-win32 directory])],
99          [
100             case "$withval" in
101             "" | y | ye | yes | n | no)
102             AC_MSG_ERROR([Invalid --with-pthread value])
103               ;;
104             *)
105             if (cl) 2>&1 | grep 'x64' >/dev/null 2>&1; then
106               cl_cv_x64=yes
107             else
108               cl_cv_x64=no
109             fi
110             if test "$cl_cv_x64" = yes; then
111                 PTHREAD_WIN32_DIR=$withval/lib/x64
112                 PTHREAD_WIN32_DIR_DLL=/${withval/:/}/dll/x64
113                 PTHREAD_WIN32_DIR_DLL_WIN_FORM=$withval/dll/x64
114             else
115                 PTHREAD_WIN32_DIR=$withval/lib/x86
116                 PTHREAD_WIN32_DIR_DLL=/${withval/:/}/dll/x86
117                 PTHREAD_WIN32_DIR_DLL_WIN_FORM=$withval/dll/x86
118             fi
119             PTHREAD_INCLUDES=-I$withval/include
120             PTHREAD_LDFLAGS=-L$PTHREAD_WIN32_DIR
121             PTHREAD_LIBS="-lpthreadVC2"
122             AC_SUBST([PTHREAD_WIN32_DIR_DLL_WIN_FORM])
123             AC_SUBST([PTHREAD_WIN32_DIR_DLL])
124             AC_SUBST([PTHREAD_INCLUDES])
125             AC_SUBST([PTHREAD_LDFLAGS])
126             AC_SUBST([PTHREAD_LIBS])
127               ;;
128             esac
129          ], [
130             AC_MSG_ERROR([pthread directory not specified])
131          ]
132       )
133       AC_ARG_WITH([debug],
134          [AS_HELP_STRING([--with-debug],
135             [Build without compiler optimizations])],
136          [
137             MSVC_CFLAGS="-O0"
138             AC_SUBST([MSVC_CFLAGS])
139          ], [
140             MSVC_CFLAGS="-O2"
141             AC_SUBST([MSVC_CFLAGS])
142          ]
143       )
144
145       AC_DEFINE([WIN32], [1], [Define to 1 if building on WIN32.])
146       AH_BOTTOM([#ifdef WIN32
147 #include "include/windows/windefs.h"
148 #endif])
149    fi])
150
151 dnl OVS_CHECK_WINDOWS
152 dnl
153 dnl Configure Visual Studio solution build
154 AC_DEFUN([OVS_CHECK_VISUAL_STUDIO_DDK], [
155 AC_ARG_WITH([vstudiotarget],
156          [AS_HELP_STRING([--with-vstudiotarget=target_type],
157             [Target type: Debug/Release])],
158          [
159             case "$withval" in
160             "Release") ;;
161             "Debug") ;;
162             *) AC_MSG_ERROR([No valid Visual Studio configuration found]) ;;
163             esac
164
165             VSTUDIO_CONFIG=$withval
166          ], [
167             VSTUDIO_CONFIG=
168          ]
169       )
170
171   AC_SUBST([VSTUDIO_CONFIG])
172   AC_DEFINE([VSTUDIO_DDK], [1], [System uses the Visual Studio build target.])
173   AM_CONDITIONAL([VSTUDIO_DDK], [test -n "$VSTUDIO_CONFIG"])
174 ])
175
176 dnl Checks for Netlink support.
177 AC_DEFUN([OVS_CHECK_NETLINK],
178   [AC_CHECK_HEADER([linux/netlink.h],
179                    [HAVE_NETLINK=yes],
180                    [HAVE_NETLINK=no],
181                    [#include <sys/socket.h>
182    ])
183    AM_CONDITIONAL([HAVE_NETLINK], [test "$HAVE_NETLINK" = yes])
184    if test "$HAVE_NETLINK" = yes; then
185       AC_DEFINE([HAVE_NETLINK], [1],
186                 [Define to 1 if Netlink protocol is available.])
187    fi])
188
189 dnl Checks for OpenSSL.
190 AC_DEFUN([OVS_CHECK_OPENSSL],
191   [AC_ARG_ENABLE(
192      [ssl],
193      [AC_HELP_STRING([--disable-ssl], [Disable OpenSSL support])],
194      [case "${enableval}" in
195         (yes) ssl=true ;;
196         (no)  ssl=false ;;
197         (*) AC_MSG_ERROR([bad value ${enableval} for --enable-ssl]) ;;
198       esac],
199      [ssl=check])
200
201    if test "$ssl" != false; then
202        AX_CHECK_OPENSSL(
203          [HAVE_OPENSSL=yes],
204          [HAVE_OPENSSL=no
205           if test "$ssl" = check; then
206             AC_MSG_WARN([Cannot find openssl:
207
208 $SSL_PKG_ERRORS
209
210 OpenFlow connections over SSL will not be supported.
211 (You may use --disable-ssl to suppress this warning.)])
212           else
213             AC_MSG_ERROR([Cannot find openssl (use --disable-ssl to configure without SSL support)])
214           fi])
215    else
216        HAVE_OPENSSL=no
217    fi
218    AC_SUBST([HAVE_OPENSSL])
219    AM_CONDITIONAL([HAVE_OPENSSL], [test "$HAVE_OPENSSL" = yes])
220    if test "$HAVE_OPENSSL" = yes; then
221       AC_DEFINE([HAVE_OPENSSL], [1], [Define to 1 if OpenSSL is installed.])
222    fi])
223
224 dnl Checks for libraries needed by lib/socket-util.c.
225 AC_DEFUN([OVS_CHECK_SOCKET_LIBS],
226   [AC_CHECK_LIB([socket], [connect])
227    AC_SEARCH_LIBS([gethostbyname], [resolv])])
228
229 dnl Checks for the directory in which to store the PKI.
230 AC_DEFUN([OVS_CHECK_PKIDIR],
231   [AC_ARG_WITH(
232      [pkidir],
233      AC_HELP_STRING([--with-pkidir=DIR],
234                     [PKI hierarchy directory [[LOCALSTATEDIR/lib/openvswitch/pki]]]),
235      [PKIDIR=$withval],
236      [PKIDIR='${localstatedir}/lib/openvswitch/pki'])
237    AC_SUBST([PKIDIR])])
238
239 dnl Checks for the directory in which to store pidfiles.
240 AC_DEFUN([OVS_CHECK_RUNDIR],
241   [AC_ARG_WITH(
242      [rundir],
243      AC_HELP_STRING([--with-rundir=DIR],
244                     [directory used for pidfiles
245                     [[LOCALSTATEDIR/run/openvswitch]]]),
246      [RUNDIR=$withval],
247      [RUNDIR='${localstatedir}/run/openvswitch'])
248    AC_SUBST([RUNDIR])])
249
250 dnl Checks for the directory in which to store logs.
251 AC_DEFUN([OVS_CHECK_LOGDIR],
252   [AC_ARG_WITH(
253      [logdir],
254      AC_HELP_STRING([--with-logdir=DIR],
255                     [directory used for logs [[LOCALSTATEDIR/log/PACKAGE]]]),
256      [LOGDIR=$withval],
257      [LOGDIR='${localstatedir}/log/${PACKAGE}'])
258    AC_SUBST([LOGDIR])])
259
260 dnl Checks for the directory in which to store the Open vSwitch database.
261 AC_DEFUN([OVS_CHECK_DBDIR],
262   [AC_ARG_WITH(
263      [dbdir],
264      AC_HELP_STRING([--with-dbdir=DIR],
265                     [directory used for conf.db [[SYSCONFDIR/PACKAGE]]]),
266      [DBDIR=$withval],
267      [DBDIR='${sysconfdir}/${PACKAGE}'])
268    AC_SUBST([DBDIR])])
269
270 dnl Defines HAVE_BACKTRACE if backtrace() is found.
271 AC_DEFUN([OVS_CHECK_BACKTRACE],
272   [AC_SEARCH_LIBS([backtrace], [execinfo ubacktrace],
273                   [AC_DEFINE([HAVE_BACKTRACE], [1],
274                              [Define to 1 if you have backtrace(3).])])])
275
276 dnl Defines HAVE_PERF_EVENT if linux/perf_event.h is found.
277 AC_DEFUN([OVS_CHECK_PERF_EVENT],
278   [AC_CHECK_HEADERS([linux/perf_event.h])])
279
280 dnl Checks for valgrind/valgrind.h.
281 AC_DEFUN([OVS_CHECK_VALGRIND],
282   [AC_CHECK_HEADERS([valgrind/valgrind.h])])
283
284 dnl Checks for Python 2.x, x >= 7.
285 AC_DEFUN([OVS_CHECK_PYTHON],
286   [AC_CACHE_CHECK(
287      [for Python 2.x for x >= 7],
288      [ovs_cv_python],
289      [if test -n "$PYTHON"; then
290         ovs_cv_python=$PYTHON
291       else
292         ovs_cv_python=no
293         for binary in python python2.7; do
294           ovs_save_IFS=$IFS; IFS=$PATH_SEPARATOR
295           for dir in $PATH; do
296             IFS=$ovs_save_IFS
297             test -z "$dir" && dir=.
298             if test -x "$dir"/"$binary" && "$dir"/"$binary" -c 'import sys
299 if sys.hexversion >= 0x02070000 and sys.hexversion < 0x03000000:
300     sys.exit(0)
301 else:
302     sys.exit(1)'; then
303               ovs_cv_python=$dir/$binary
304               break 2
305             fi
306           done
307         done
308       fi])
309    AC_SUBST([HAVE_PYTHON])
310    AM_MISSING_PROG([PYTHON], [python])
311    if test $ovs_cv_python != no; then
312      PYTHON=$ovs_cv_python
313      HAVE_PYTHON=yes
314    else
315      HAVE_PYTHON=no
316    fi
317    AM_CONDITIONAL([HAVE_PYTHON], [test "$HAVE_PYTHON" = yes])])
318
319 dnl Checks for dot.
320 AC_DEFUN([OVS_CHECK_DOT],
321   [AC_CACHE_CHECK(
322     [for dot],
323     [ovs_cv_dot],
324     [dnl "dot" writes -V output to stderr:
325      if (dot -V) 2>&1 | grep '^dot - [[gG]]raphviz version' >/dev/null 2>&1; then
326        ovs_cv_dot=yes
327      else
328        ovs_cv_dot=no
329      fi])
330    AM_CONDITIONAL([HAVE_DOT], [test "$ovs_cv_dot" = yes])])
331
332 dnl Checks for groff.
333 AC_DEFUN([OVS_CHECK_GROFF],
334   [AC_CACHE_CHECK(
335     [for groff],
336     [ovs_cv_groff],
337     [if (groff -v) >/dev/null 2>&1; then
338        ovs_cv_groff=yes
339      else
340        ovs_cv_groff=no
341      fi])
342    AM_CONDITIONAL([HAVE_GROFF], [test "$ovs_cv_groff" = yes])])
343
344 dnl Checks for thread-local storage support.
345 dnl
346 dnl Checks whether the compiler and linker support the C11
347 dnl thread_local macro from <threads.h>, and if so defines
348 dnl HAVE_THREAD_LOCAL.  If not, checks whether the compiler and linker
349 dnl support the GCC __thread extension, and if so defines
350 dnl HAVE___THREAD.
351 AC_DEFUN([OVS_CHECK_TLS],
352   [AC_CACHE_CHECK(
353      [whether $CC has <threads.h> that supports thread_local],
354      [ovs_cv_thread_local],
355      [AC_LINK_IFELSE(
356         [AC_LANG_PROGRAM([#include <threads.h>
357 static thread_local int var;], [return var;])],
358         [ovs_cv_thread_local=yes],
359         [ovs_cv_thread_local=no])])
360    if test $ovs_cv_thread_local = yes; then
361      AC_DEFINE([HAVE_THREAD_LOCAL], [1],
362                [Define to 1 if the C compiler and linker supports the C11
363                 thread_local matcro defined in <threads.h>.])
364    else
365      AC_CACHE_CHECK(
366        [whether $CC supports __thread],
367        [ovs_cv___thread],
368        [AC_LINK_IFELSE(
369           [AC_LANG_PROGRAM([static __thread int var;], [return var;])],
370           [ovs_cv___thread=yes],
371           [ovs_cv___thread=no])])
372      if test $ovs_cv___thread = yes; then
373        AC_DEFINE([HAVE___THREAD], [1],
374                  [Define to 1 if the C compiler and linker supports the
375                   GCC __thread extenions.])
376      fi
377    fi])
378
379 dnl OVS_CHECK_ATOMIC_LIBS
380 dnl
381 dnl Check to see if -latomic is need for GCC atomic built-ins.
382 AC_DEFUN([OVS_CHECK_ATOMIC_LIBS],
383    [AC_SEARCH_LIBS([__atomic_load_8], [atomic])])
384
385 dnl OVS_CHECK_GCC4_ATOMICS
386 dnl
387 dnl Checks whether the compiler and linker support GCC 4.0+ atomic built-ins.
388 dnl A compile-time only check is not enough because the compiler defers
389 dnl unimplemented built-ins to libgcc, which sometimes also lacks
390 dnl implementations.
391 AC_DEFUN([OVS_CHECK_GCC4_ATOMICS],
392   [AC_CACHE_CHECK(
393      [whether $CC supports GCC 4.0+ atomic built-ins],
394      [ovs_cv_gcc4_atomics],
395      [AC_LINK_IFELSE(
396         [AC_LANG_PROGRAM([[#include <stdlib.h>
397
398 #define ovs_assert(expr) if (!(expr)) abort();
399 #define TEST_ATOMIC_TYPE(TYPE)                  \
400     {                                           \
401         TYPE x = 1;                             \
402         TYPE orig;                              \
403                                                 \
404         __sync_synchronize();                   \
405         ovs_assert(x == 1);                     \
406                                                 \
407         __sync_synchronize();                   \
408         x = 3;                                  \
409         __sync_synchronize();                   \
410         ovs_assert(x == 3);                     \
411                                                 \
412         orig = __sync_fetch_and_add(&x, 1);     \
413         ovs_assert(orig == 3);                  \
414         __sync_synchronize();                   \
415         ovs_assert(x == 4);                     \
416                                                 \
417         orig = __sync_fetch_and_sub(&x, 2);     \
418         ovs_assert(orig == 4);                  \
419         __sync_synchronize();                   \
420         ovs_assert(x == 2);                     \
421                                                 \
422         orig = __sync_fetch_and_or(&x, 6);      \
423         ovs_assert(orig == 2);                  \
424         __sync_synchronize();                   \
425         ovs_assert(x == 6);                     \
426                                                 \
427         orig = __sync_fetch_and_and(&x, 10);    \
428         ovs_assert(orig == 6);                  \
429         __sync_synchronize();                   \
430         ovs_assert(x == 2);                     \
431                                                 \
432         orig = __sync_fetch_and_xor(&x, 10);    \
433         ovs_assert(orig == 2);                  \
434         __sync_synchronize();                   \
435         ovs_assert(x == 8);                     \
436     }]], [dnl
437 TEST_ATOMIC_TYPE(char);
438 TEST_ATOMIC_TYPE(unsigned char);
439 TEST_ATOMIC_TYPE(signed char);
440 TEST_ATOMIC_TYPE(short);
441 TEST_ATOMIC_TYPE(unsigned short);
442 TEST_ATOMIC_TYPE(int);
443 TEST_ATOMIC_TYPE(unsigned int);
444 TEST_ATOMIC_TYPE(long int);
445 TEST_ATOMIC_TYPE(unsigned long int);
446 TEST_ATOMIC_TYPE(long long int);
447 TEST_ATOMIC_TYPE(unsigned long long int);
448 ])],
449         [ovs_cv_gcc4_atomics=yes],
450         [ovs_cv_gcc4_atomics=no])])
451    if test $ovs_cv_gcc4_atomics = yes; then
452      AC_DEFINE([HAVE_GCC4_ATOMICS], [1],
453                [Define to 1 if the C compiler and linker supports the GCC 4.0+
454                 atomic built-ins.])
455    fi])
456
457 dnl OVS_CHECK_ATOMIC_ALWAYS_LOCK_FREE(SIZE)
458 dnl
459 dnl Checks __atomic_always_lock_free(SIZE, 0)
460 AC_DEFUN([OVS_CHECK_ATOMIC_ALWAYS_LOCK_FREE], 
461   [AC_CACHE_CHECK(
462     [value of __atomic_always_lock_free($1)],
463     [ovs_cv_atomic_always_lock_free_$1],
464     [AC_COMPUTE_INT(
465         [ovs_cv_atomic_always_lock_free_$1],
466         [__atomic_always_lock_free($1, 0)],
467         [],
468         [ovs_cv_atomic_always_lock_free_$1=unsupported])])
469    if test ovs_cv_atomic_always_lock_free_$1 != unsupported; then
470      AC_DEFINE_UNQUOTED(
471        [ATOMIC_ALWAYS_LOCK_FREE_$1B],
472        [$ovs_cv_atomic_always_lock_free_$1],
473        [If the C compiler is GCC 4.7 or later, define to the return value of
474         __atomic_always_lock_free($1, 0).  If the C compiler is not GCC or is
475         an older version of GCC, the value does not matter.])
476    fi])
477
478 dnl OVS_CHECK_POSIX_AIO
479 AC_DEFUN([OVS_CHECK_POSIX_AIO],
480   [AC_SEARCH_LIBS([aio_write], [rt])
481    AM_CONDITIONAL([HAVE_POSIX_AIO], [test "$ac_cv_search_aio_write" != no])])
482
483 dnl OVS_CHECK_INCLUDE_NEXT
484 AC_DEFUN([OVS_CHECK_INCLUDE_NEXT],
485   [AC_REQUIRE([gl_CHECK_NEXT_HEADERS])
486    gl_CHECK_NEXT_HEADERS([$1])])
487
488 dnl OVS_CHECK_PRAGMA_MESSAGE
489 AC_DEFUN([OVS_CHECK_PRAGMA_MESSAGE],
490   [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
491    [[_Pragma("message(\"Checking for pragma message\")")
492    ]])],
493      [AC_DEFINE(HAVE_PRAGMA_MESSAGE,1,[Define if compiler supports #pragma
494      message directive])])
495   ])