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