added m4 macros for boost C++ headers and libs, hopefully making better autoconf...
[m4-moved-to-github.git] / ax_boost_thread.m4
1 ##### http://autoconf-archive.cryp.to/ax_boost_thread.html
2 #
3 # SYNOPSIS
4 #
5 #   AX_BOOST_THREAD
6 #
7 # DESCRIPTION
8 #
9 #   Test for Thread library from the Boost C++ libraries. The macro
10 #   requires a preceding call to AX_BOOST_BASE. Further documentation
11 #   is available at <http://randspringer.de/boost/index.html>.
12 #
13 #   This macro calls:
14 #
15 #     AC_SUBST(BOOST_THREAD_LIB)
16 #
17 #   And sets:
18 #
19 #     HAVE_BOOST_THREAD
20 #
21 # LAST MODIFICATION
22 #
23 #   2006-12-28
24 #
25 # COPYLEFT
26 #
27 #   Copyright (c) 2006 Thomas Porschberg <thomas@randspringer.de>
28 #   Copyright (c) 2006 Michael Tindal <mtindal@paradoxpoint.com>
29 #
30 #   Copying and distribution of this file, with or without
31 #   modification, are permitted in any medium without royalty provided
32 #   the copyright notice and this notice are preserved.
33
34 AC_DEFUN([AX_BOOST_THREAD],
35 [
36         AC_ARG_WITH([boost-thread],
37         AS_HELP_STRING([--with-boost-thread@<:@=special-lib@:>@],
38                    [use the Thread library from boost - it is possible to specify a certain library for the linker
39                         e.g. --with-boost-thread=boost_thread-gcc-mt ]),
40         [
41         if test "$withval" = "no"; then
42                         want_boost="no"
43         elif test "$withval" = "yes"; then
44             want_boost="yes"
45             ax_boost_user_thread_lib=""
46         else
47                     want_boost="yes"
48                 ax_boost_user_thread_lib="$withval"
49                 fi
50         ],
51         [want_boost="yes"]
52         )
53
54         if test "x$want_boost" = "xyes"; then
55         AC_REQUIRE([AC_PROG_CC])
56         AC_REQUIRE([AC_CANONICAL_BUILD])
57                 CPPFLAGS_SAVED="$CPPFLAGS"
58                 CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
59                 export CPPFLAGS
60
61                 LDFLAGS_SAVED="$LDFLAGS"
62                 LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
63                 export LDFLAGS
64
65         AC_CACHE_CHECK(whether the Boost::Thread library is available,
66                                            ax_cv_boost_thread,
67         [AC_LANG_PUSH([C++])
68                          CXXFLAGS_SAVE=$CXXFLAGS
69
70                          if test "x$build_os" = "xsolaris" ; then
71                                  CXXFLAGS="-pthreads $CXXFLAGS"
72                          elif test "x$build_os" = "xming32" ; then
73                                  CXXFLAGS="-mthreads $CXXFLAGS"
74                          else
75                                 CXXFLAGS="-pthread $CXXFLAGS"
76                          fi
77                          AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[@%:@include <boost/thread/thread.hpp>]],
78                                    [[boost::thread_group thrds;
79                                    return 0;]]),
80                    ax_cv_boost_thread=yes, ax_cv_boost_thread=no)
81                          CXXFLAGS=$CXXFLAGS_SAVE
82              AC_LANG_POP([C++])
83                 ])
84                 if test "x$ax_cv_boost_thread" = "xyes"; then
85            if test "x$build_os" = "xsolaris" ; then
86                           BOOST_CPPFLAGS="-pthreads $BOOST_CPPFLAGS"
87                    elif test "x$build_os" = "xming32" ; then
88                           BOOST_CPPFLAGS="-mthreads $BOOST_CPPFLAGS"
89                    else
90                           BOOST_CPPFLAGS="-pthread $BOOST_CPPFLAGS"
91                    fi
92
93                         AC_SUBST(BOOST_CPPFLAGS)
94
95                         AC_DEFINE(HAVE_BOOST_THREAD,,[define if the Boost::Thread library is available])
96                         BN=boost_thread
97
98                         LDFLAGS_SAVE=$LDFLAGS
99                         case "x$build_os" in
100                           *bsd* )
101                                LDFLAGS="-pthread $LDFLAGS"
102                           break;
103                           ;;
104                         esac
105             if test "x$ax_boost_user_thread_lib" = "x"; then
106                                 for ax_lib in $BN $BN-$CC $BN-$CC-mt $BN-$CC-mt-s $BN-$CC-s \
107                               lib$BN lib$BN-$CC lib$BN-$CC-mt lib$BN-$CC-mt-s lib$BN-$CC-s \
108                               $BN-mgw $BN-mgw $BN-mgw-mt $BN-mgw-mt-s $BN-mgw-s ; do
109                                     AC_CHECK_LIB($ax_lib, main, [BOOST_THREAD_LIB="-l$ax_lib" AC_SUBST(BOOST_THREAD_LIB) link_thread="yes" break],
110                                  [link_thread="no"])
111                                 done
112             else
113                for ax_lib in $ax_boost_user_thread_lib $BN-$ax_boost_user_thread_lib; do
114                                       AC_CHECK_LIB($ax_lib, main,
115                                    [BOOST_THREAD_LIB="-l$ax_lib" AC_SUBST(BOOST_THREAD_LIB) link_thread="yes" break],
116                                    [link_thread="no"])
117                   done
118
119             fi
120                         if test "x$link_thread" = "xno"; then
121                                 AC_MSG_ERROR(Could not link against $ax_lib !)
122                         else
123                            case "x$build_os" in
124                               *bsd* )
125                                 BOOST_LDFLAGS="-pthread $BOOST_LDFLAGS"
126                               break;
127                               ;;
128                            esac
129
130                         fi
131                 fi
132
133                 CPPFLAGS="$CPPFLAGS_SAVED"
134         LDFLAGS="$LDFLAGS_SAVED"
135         fi
136 ])