1 # boost.m4: Locate Boost headers and libraries for autoconf-based projects.
2 # Copyright (C) 2007 Benoit Sigoure <tsuna@lrde.epita.fr>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Original sources can be found at http://repo.or.cz/w/boost.m4.git
24 # This file provides several macros to use the various Boost libraries.
25 # The first macro is BOOST_REQUIRE. It will simply check if it's possible to
26 # find the Boost headers of a given (optional) minimum version and it will
27 # define BOOST_CPPFLAGS accordingly. It will add an option --with-boost to
28 # your configure so that users can specify non standard locations.
30 m4_pattern_forbid([^_?BOOST_])
32 # BOOST_REQUIRE([VERSION])
33 # ------------------------
34 # Look for Boost. If version is given, it must either be a literal of the form
35 # "X.Y" where X and Y are integers or a variable "$var".
36 # Defines the value BOOST_CPPFLAGS. This macro only checks for headers with
37 # the required version, it does not check for any of the Boost libraries.
38 # FIXME: Add a 2nd optionnal argument so that it's not fatal if Boost isn't found
39 # and add an AC_DEFINE to tell whether HAVE_BOOST.
40 AC_DEFUN([BOOST_REQUIRE],
41 [dnl First find out what kind of argument we have.
42 dnl If we have an empty argument, there is no constraint on the version of
43 dnl Boost to use. If it's a literal version number, we can split it in M4 (so
44 dnl the resulting configure script will be smaller/faster). Otherwise we do
45 dnl the splitting at runtime.
47 [^ *$], [m4_pushdef([BOOST_VERSION_REQ], [])dnl
50 boost_version_subminor=0
52 [^[0-9]+\([-._][0-9]+\)*$],
53 [m4_pushdef([BOOST_VERSION_REQ], [ version >= $1])dnl
54 boost_version_major=m4_bregexp([$1], [^\([0-9]+\)], [\1])
55 boost_version_minor=m4_bregexp([$1], [^[0-9]+[-._]\([0-9]+\)], [\1])
56 boost_version_subminor=m4_bregexp([$1], [^[0-9]+[-._][0-9]+[-._]\([0-9]+\)], [\1])
59 [m4_pushdef([BOOST_VERSION_REQ], [])dnl
60 boost_version_major=`expr "X$1" : 'X\([[^-._]]*\)'`
61 boost_version_minor=`expr "X$1" : 'X[[0-9]]*[[-._]]\([[^-._]]*\)'`
62 boost_version_subminor=`expr "X$1" : 'X[[0-9]]*[[-._]][[0-9]]*[[-._]]\([[0-9]]*\)'`
63 case $boost_version_major:$boost_version_minor in #(
64 *: | :* | *[[^0-9]]*:* | *:*[[^0-9]]*)
65 AC_MSG_ERROR([[Invalid argument for REQUIRE_BOOST: `$1']])
69 [m4_fatal(Invalid argument: `$1')]
72 [AS_HELP_STRING([--with-boost=DIR],
73 [prefix of Boost]BOOST_VERSION_REQ[ @<:@guess@:>@])])dnl
74 AC_SUBST([DISTCHECK_CONFIGURE_FLAGS],
75 ["$DISTCHECK_CONFIGURE_FLAGS '--with-boost=$with_boost'"])
76 AC_CACHE_CHECK([for Boost headers[]BOOST_VERSION_REQ],
79 AC_LANG_PUSH([C++])dnl
81 test x"$boost_version_subminor" != x \
82 && boost_subminor_chk="|| (B_V_MAJ == $boost_version_major \
83 && B_V_MIN == $boost_version_minor \
84 && B_V_SUB % 100 < $boost_version_subminor)"
85 for boost_inc in "$with_boost/include" '' \
86 /opt/local/include /usr/local/include /opt/include /usr/include \
87 "$with_boost" C:/Boost/include
89 test -e "$boost_inc" || continue
90 boost_save_CPPFLAGS=$CPPFLAGS
91 test x"$boost_inc" != x && CPPFLAGS="$CPPFLAGS -I$boost_inc"
92 m4_pattern_allow([^BOOST_VERSION$])dnl
93 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <boost/version.hpp>
95 # error BOOST_VERSION is not defined
97 #define B_V_MAJ (BOOST_VERSION / 10000)
98 #define B_V_MIN (BOOST_VERSION / 100 % 1000)
99 #define B_V_SUB (BOOST_VERSION % 100)
100 #if (B_V_MAJ < $boost_version_major) \
101 || (B_V_MAJ == $boost_version_major \
102 && B_V_MIN / 100 % 1000 < $boost_version_minor) $boost_subminor_chk
103 # error Boost headers version < $1
105 ]])], [boost_cv_inc_path=yes], [boost_cv_version=no])
106 CPPFLAGS=$boost_save_CPPFLAGS
107 if test x"$boost_cv_inc_path" = xyes; then
108 if test x"$boost_inc" != x; then
109 boost_cv_inc_path=$boost_inc
114 AC_LANG_POP([C++])dnl
116 case $boost_cv_inc_path in #(
118 AC_MSG_ERROR([Could not find Boost headers[]BOOST_VERSION_REQ])
124 BOOST_CPPFLAGS="-I$boost_cv_inc_path"
127 AC_SUBST([BOOST_CPPFLAGS])dnl
128 AC_CACHE_CHECK([for Boost's header version],
129 [boost_cv_lib_version],
130 [m4_pattern_allow([^BOOST_LIB_VERSION$])dnl
131 boost_cv_lib_version=unknown
132 boost_sed_version='/^.*BOOST_LIB_VERSION.*"\([[^"]]*\)".*$/!d;s//\1/'
133 boost_version_hpp="$boost_inc/boost/version.hpp"
134 test -e "$boost_version_hpp" \
135 && boost_cv_lib_version=`sed "$boost_sed_version" "$boost_version_hpp"`
137 m4_popdef([BOOST_VERSION_REQ])dnl
141 # BOOST_FIND_HEADER([HEADER-NAME], [ACTION-IF-NOT-FOUND], [ACTION-IF-FOUND])
142 # --------------------------------------------------------------------------
143 # Wrapper around AC_CHECK_HEADER for Boost headers. Useful to check for
144 # some parts of the Boost library which are only made of headers and don't
145 # require linking (such as Boost.Foreach).
147 # Default ACTION-IF-NOT-FOUND: Fail with a fatal error.
149 # Default ACTION-IF-FOUND: define the preprocessor symbol HAVE_<HEADER-NAME> in
150 # case of success # (where HEADER-NAME is written LIKE_THIS, e.g.,
151 # HAVE_BOOST_FOREACH_HPP).
152 AC_DEFUN([BOOST_FIND_HEADER],
153 [AC_REQUIRE([BOOST_REQUIRE])dnl
154 AC_LANG_PUSH([C++])dnl
155 boost_save_CPPFLAGS=$CPPFLAGS
156 CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
157 AC_CHECK_HEADER([$1],
158 [m4_default([$3], [AC_DEFINE(AS_TR_CPP([HAVE_$1]), [1],
159 [Define to 1 if you have <$1>])])],
160 [m4_default([$2], [AC_MSG_ERROR([cannot find $1])])])
161 CPPFLAGS=$boost_save_CPPFLAGS
162 AC_LANG_POP([C++])dnl
163 ])# BOOST_FIND_HEADER
166 # BOOST_FIND_LIB([LIB-NAME], [PREFERED-RT-OPT], [HEADER-NAME], [CXX-TEST])
167 # ------------------------------------------------------------------------
168 # Look for the Boost library LIB-NAME (e.g., LIB-NAME = `thread', for
169 # libboost_thread). Check that HEADER-NAME works and check that
170 # libboost_LIB-NAME can link with the code CXX-TEST.
172 # Invokes BOOST_FIND_HEADER([HEADER-NAME]) (see above).
174 # Boost libraries typically come compiled with several flavors (with different
175 # runtime options) so PREFERED-RT-OPT is the prefered suffix. A suffix is one
176 # or more of the following letters: sgdpn (in that order). s = static
177 # runtime, d = debug build, g = debug/diagnostic runtime, p = STLPort build,
178 # n = (unsure) STLPort build without iostreams from STLPort (it looks like `n'
179 # must always be used along with `p'). Additionally, PREFERED-RT-OPT can
180 # start with `mt-' to indicate that there is a preference for multi-thread
181 # builds. Some sample values for PREFERED-RT-OPT: (nothing), mt, d, mt-d, gdp
182 # ... If you want to make sure you have a specific version of Boost
183 # (eg, >= 1.33) you *must* invoke BOOST_REQUIRE before this macro.
184 AC_DEFUN([BOOST_FIND_LIB],
185 [AC_REQUIRE([_BOOST_FIND_COMPILER_TAG])dnl
186 AC_REQUIRE([BOOST_REQUIRE])dnl
187 AC_LANG_PUSH([C++])dnl
188 AS_VAR_PUSHDEF([Boost_lib], [boost_cv_lib_$1])dnl
189 AS_VAR_PUSHDEF([Boost_lib_LDFLAGS], [boost_cv_lib_$1_LDFLAGS])dnl
190 AS_VAR_PUSHDEF([Boost_lib_LIBS], [boost_cv_lib_$1_LIBS])dnl
191 BOOST_FIND_HEADER([$3])
192 boost_save_CPPFLAGS=$CPPFLAGS
193 CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
194 # Now let's try to find the library. The algorithm is as follows: first look
195 # for a given library name according to the user's PREFERED-RT-OPT. For each
196 # library name, we prefer to use the ones that carry the tag (toolset name).
197 # Each library is searched through the various standard paths were Boost is
198 # usually installed. If we can't find the standard variants, we try to
199 # enforce -mt (for instance on MacOSX, libboost_threads.dylib doesn't exist
200 # but there's -obviously- libboost_threads-mt.dylib).
201 AC_CACHE_CHECK([for the Boost $1 library], [Boost_lib],
204 mt | mt-) boost_mt=-mt; boost_rtopt=;; #(
205 mt* | mt-*) boost_mt=-mt; boost_rtopt=`expr "X$2" : 'Xmt-*\(.*\)'`;; #(
206 *) boost_mt=; boost_rtopt=$2;;
208 # If the PREFERED-RT-OPT are not empty, prepend a `-'.
209 case $boost_rtopt in #(
210 *[[a-z0-9A-Z]]*) boost_rtopt="-$boost_rtopt";;
212 # Check whether we do better use `mt' even though we weren't ask to.
213 if test x"$boost_mt" = x; then
214 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
215 #if defined _REENTRANT || defined _MT || defined __MT__
218 # error MT not needed
220 ]])], [boost_mt=-mt])
222 # Generate the test file.
223 AC_LANG_CONFTEST([AC_LANG_PROGRAM([#include <$3>], [$4])])
225 # Don't bother to ident the 6 nested for loops, only the 2 insidemost ones
227 for boost_tag_ in -$boost_cv_lib_tag ''; do
228 for boost_ver_ in -$boost_cv_lib_version ''; do
229 for boost_mt_ in $boost_mt -mt ''; do
230 for boost_rtopt_ in $boost_rtopt '' -d; do
232 boost_$1$boost_tag_$boost_mt_$boost_rtopt_$boost_ver_ \
233 boost_$1$boost_tag_$boost_mt_$boost_ver_ \
234 boost_$1$boost_tag_$boost_rtopt_$boost_ver_ \
235 boost_$1$boost_tag_$boost_mt_ \
236 boost_$1$boost_tag_$boost_ver_
238 # Avoid testing twice the same lib
239 case $boost_failed_libs in #(
240 *@$boost_lib@*) continue;;
242 boost_save_LIBS=$LIBS
243 LIBS="-l$boost_lib $LIBS"
244 for boost_ldpath in "$with_boost/lib" '' \
245 /opt/local/lib /usr/local/lib /opt/lib /usr/lib \
246 "$with_boost" C:/Boost/lib /lib /usr/lib64 /lib64
248 test -e "$boost_ldpath" || continue
249 boost_save_LDFLAGS=$LDFLAGS
250 test x"$boost_ldpath" != x && LDFLAGS="$LDFLAGS -L$boost_ldpath"
251 dnl First argument of AC_LINK_IFELSE left empty because the test file is
252 dnl generated only once above (before we start the for loops).
254 [Boost_lib=yes], [Boost_lib=no])
255 if test x"$Boost_lib" = xyes; then
256 Boost_lib_LDFLAGS="-L$boost_ldpath"
257 Boost_lib_LIBS="-l$boost_lib"
260 boost_failed_libs="$boost_failed_libs@$boost_lib@"
262 LDFLAGS=$boost_save_LDFLAGS
264 LIBS=$boost_save_LIBS
271 AC_SUBST(AS_TR_CPP([BOOST_$1_LDFLAGS]), [$Boost_lib_LDFLAGS])
272 AC_SUBST(AS_TR_CPP([BOOST_$1_LIBS]), [$Boost_lib_LIBS])
273 CPPFLAGS=$boost_save_CPPFLAGS
274 AS_VAR_POPDEF([Boost_lib])dnl
275 AS_VAR_POPDEF([Boost_lib_LDFLAGS])dnl
276 AS_VAR_POPDEF([Boost_lib_LIBS])dnl
277 AC_LANG_POP([C++])dnl
281 # --------------------------------------- #
282 # Checks for the various Boost libraries. #
283 # --------------------------------------- #
285 # List of boost libraries: http://www.boost.org/libs/libraries.htm
286 # The page http://beta.boost.org/doc/libs is useful: it gives the first release
287 # version of each library (among other things).
292 # Look for Boost.Conversion (cast / lexical_cast)
293 AC_DEFUN([BOOST_CONVERSION],
294 [BOOST_FIND_HEADER([boost/cast.hpp])
295 BOOST_FIND_HEADER([boost/lexical_cast.hpp])
299 # BOOST_FILESYSTEM([PREFERED-RT-OPT])
300 # -----------------------------------
301 # Look for Boost.Filesystem. For the documentation of PREFERED-RT-OPT, see the
302 # documentation of BOOST_FIND_LIB above.
303 AC_DEFUN([BOOST_FILESYSTEM],
304 [BOOST_FIND_LIB([filesystem], [$1],
305 [boost/filesystem.hpp], [boost::filesystem::path p;])
311 # Look for Boost.Foreach
312 AC_DEFUN([BOOST_FOREACH],
313 [BOOST_FIND_HEADER([boost/foreach.hpp])])
318 # Look for Boost.Format
319 # Note: we can't check for boost/format/format_fwd.hpp because the header isn't
320 # standalone. It can't be compiled because it triggers the following error:
321 # boost/format/detail/config_macros.hpp:88: error: 'locale' in namespace 'std'
322 # does not name a type
323 AC_DEFUN([BOOST_FORMAT],
324 [BOOST_FIND_HEADER([boost/format.hpp])])
327 # BOOST_GRAPH([PREFERED-RT-OPT])
328 # ------------------------------
329 # Look for Boost.Graphs. For the documentation of PREFERED-RT-OPT, see the
330 # documentation of BOOST_FIND_LIB above.
331 AC_DEFUN([BOOST_GRAPH],
332 [BOOST_FIND_LIB([graph], [$1],
333 [boost/graph/adjacency_list.hpp], [boost::adjacency_list g;])
337 # BOOST_THREADS([PREFERED-RT-OPT])
338 # --------------------------------
339 # Look for Boost.Threads. For the documentation of PREFERED-RT-OPT, see the
340 # documentation of BOOST_FIND_LIB above.
341 AC_DEFUN([BOOST_THREADS],
342 [BOOST_FIND_LIB([thread], [$1],
343 [boost/thread.hpp], [boost::thread t; boost::mutex m;])
349 # Look for Boost.Utility (noncopyable, result_of, base-from-member idiom,
351 AC_DEFUN([BOOST_UTILITY],
352 [BOOST_FIND_HEADER([boost/utility.hpp])])
357 # Look for Boost.Variant.
358 AC_DEFUN([BOOST_VARIANT],
359 [BOOST_FIND_HEADER([boost/variant/variant_fwd.hpp])
360 BOOST_FIND_HEADER([boost/variant.hpp])])
362 # _BOOST_gcc_test(MAJOR, MINOR)
363 # -----------------------------
364 # Internal helper for _BOOST_FIND_COMPILER_TAG.
365 m4_define([_BOOST_gcc_test],
366 ["defined __GNUC__ && __GNUC__ == $1 && __GNUC_MINOR__ == $2 && !defined __ICC @ gcc$1$2"])dnl
368 # _BOOST_FIND_COMPILER_TAG()
369 # --------------------------
370 # Internal. When Boost is installed without --layout=system, each library
371 # filename will hold a suffix that encodes the compiler used during the
372 # build. The Boost build system seems to call this a `tag'.
373 AC_DEFUN([_BOOST_FIND_COMPILER_TAG],
374 [AC_REQUIRE([AC_PROG_CXX])dnl
375 AC_CACHE_CHECK([for the toolset name used by Boost for $CXX], [boost_cv_lib_tag],
376 [AC_LANG_PUSH([C++])dnl
377 boost_cv_lib_tag=unknown
378 # The following tests are mostly inspired by boost/config/auto_link.hpp
379 # The list is sorted to most recent/common to oldest compiler (in order
380 # to increase the likelihood of finding the right compiler with the
381 # least number of compilation attempt).
382 # Beware that some tests are sensible to the order (for instance, we must
383 # look for MinGW before looking for GCC3).
384 # I used one compilation test per compiler with a #error to recognize
385 # each compiler so that it works even when cross-compiling (let me know
386 # if you know a better approach).
387 # Known missing tags (known from Boost's tools/build/v2/tools/common.jam):
388 # como, edg, kcc, bck, mp, sw, tru, xlc
389 # I'm not sure about my test for `il' (be careful: Intel's ICC pre-defines
390 # the same defines as GCC's).
391 # TODO: Move the test on GCC 4.3 up once it's released.
393 _BOOST_gcc_test(4, 2) \
394 _BOOST_gcc_test(4, 1) \
395 _BOOST_gcc_test(4, 0) \
396 "defined __GNUC__ && __GNUC__ == 3 && !defined __ICC \
397 && (defined WIN32 || defined WINNT || defined _WIN32 || defined __WIN32 \
398 || defined __WIN32__ || defined __WINNT || defined __WINNT__) @ mgw" \
399 _BOOST_gcc_test(3, 4) \
400 _BOOST_gcc_test(3, 3) \
401 "defined _MSC_VER && _MSC_VER >= 1400 @ vc80" \
402 _BOOST_gcc_test(3, 2) \
403 "defined _MSC_VER && _MSC_VER == 1310 @ vc71" \
404 _BOOST_gcc_test(3, 1) \
405 _BOOST_gcc_test(3, 0) \
406 "defined __BORLANDC__ @ bcb" \
407 "defined __ICC && (defined __unix || defined __unix__) @ il" \
408 "defined __ICL @ iw" \
409 "defined _MSC_VER && _MSC_VER == 1300 @ vc7" \
410 _BOOST_gcc_test(4, 3) \
411 _BOOST_gcc_test(2, 95) \
412 "defined __MWERKS__ && __MWERKS__ <= 0x32FF @ cw9" \
413 "defined _MSC_VER && _MSC_VER < 1300 && !defined UNDER_CE @ vc6" \
414 "defined _MSC_VER && _MSC_VER < 1300 && defined UNDER_CE @ evc4" \
415 "defined __MWERKS__ && __MWERKS__ <= 0x31FF @ cw8"
417 boost_tag_test=`expr "X$i" : 'X\([[^@]]*\) @ '`
418 boost_tag=`expr "X$i" : 'X[[^@]]* @ \(.*\)'`
419 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
423 # error $boost_tag_test
425 ]])], [boost_cv_lib_tag=$boost_tag; break], [])
427 AC_LANG_POP([C++])dnl
429 if test x"$boost_cv_lib_tag" = xunknown; then
430 AC_MSG_WARN([[could not figure out which toolset name to use for $CXX]])
433 ])# _BOOST_FIND_COMPILER_TAG