0b024379fd581dfa7653bc266602fb21e61ad40b
[gnulib.git] / m4 / ansi-c++.m4
1 # ansi-c++.m4 serial 5
2 dnl Copyright (C) 2002-2003, 2005, 2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Bruno Haible.
8
9 # Sets CXX_CHOICE to 'yes' or 'no', depending on the preferred use of C++.
10 # The default is 'yes'. If the configure.ac contains a definition of the
11 # macro gl_CXX_CHOICE_DEFAULT_NO, then the default is 'no'. In both cases,
12 # the user can change the value by passing the option --disable-cxx or
13 # --enable-cxx, respectively.
14
15 AC_DEFUN([gl_CXX_CHOICE],
16 [
17   AC_MSG_CHECKING([whether to use C++])
18   dnl It would be so nice if plus signs were supported in AC_ARG_ENABLE.
19   dnl Feature request submitted on 2010-03-13.
20   m4_ifdef([gl_CXX_CHOICE_DEFAULT_NO],
21     [AC_ARG_ENABLE([cxx],
22        [  --enable-cxx            also build C++ sources],
23        [CXX_CHOICE="$enableval"],
24        [CXX_CHOICE=no])],
25     [AC_ARG_ENABLE([cxx],
26        [  --disable-cxx           do not build C++ sources],
27        [CXX_CHOICE="$enableval"],
28        [CXX_CHOICE=yes])])
29   AC_MSG_RESULT([$CXX_CHOICE])
30   AC_SUBST([CXX_CHOICE])
31 ])
32
33 # gl_PROG_ANSI_CXX([ANSICXX_VARIABLE], [ANSICXX_CONDITIONAL])
34 # Sets ANSICXX_VARIABLE to the name of a sufficiently ANSI C++ compliant
35 # compiler, or to ":" if none is found.
36 # Defines the Automake condition ANSICXX_CONDITIONAL to true if such a compiler
37 # was found, or to false if not.
38
39 AC_DEFUN([gl_PROG_ANSI_CXX],
40 [
41   AC_REQUIRE([gl_CXX_CHOICE])
42   m4_if([$1], [CXX], [],
43     [gl_save_CXX="$CXX"])
44   if test "$CXX_CHOICE" = no; then
45     CXX=":"
46   fi
47   if test -z "$CXX"; then
48     if test -n "$CCC"; then
49       CXX="$CCC"
50     else
51       AC_CHECK_TOOLS([CXX],
52                      [g++ c++ gpp aCC CC cxx cc++ cl FCC KCC RCC xlC_r xlC],
53                      [:])
54     fi
55   fi
56   if test "$CXX" != ":"; then
57     dnl Use a modified version of AC_PROG_CXX_WORKS that does not exit
58     dnl upon failure.
59     AC_MSG_CHECKING([whether the C++ compiler ($CXX $CXXFLAGS $LDFLAGS) works])
60     AC_LANG_PUSH([C++])
61     AC_ARG_VAR([CXX], [C++ compiler command])
62     AC_ARG_VAR([CXXFLAGS], [C++ compiler flags])
63     echo 'int main () { return 0; }' > conftest.$ac_ext
64     if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
65       gl_cv_prog_ansicxx_works=yes
66       if (./conftest; exit) 2>/dev/null; then
67         gl_cv_prog_ansicxx_cross=no
68       else
69         gl_cv_prog_ansicxx_cross=yes
70       fi
71     else
72       gl_cv_prog_ansicxx_works=no
73     fi
74     rm -fr conftest*
75     AC_LANG_POP([C++])
76     AC_MSG_RESULT([$gl_cv_prog_ansicxx_works])
77     if test $gl_cv_prog_ansicxx_works = no; then
78       CXX=:
79     else
80       dnl Test for namespaces.
81       dnl We don't bother supporting pre-ANSI-C++ compilers.
82       AC_MSG_CHECKING([whether the C++ compiler supports namespaces])
83       AC_LANG_PUSH([C++])
84       cat <<EOF > conftest.$ac_ext
85 #include <iostream>
86 namespace test { using namespace std; }
87 std::ostream* ptr;
88 int main () { return 0; }
89 EOF
90       if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
91         gl_cv_prog_ansicxx_namespaces=yes
92       else
93         gl_cv_prog_ansicxx_namespaces=no
94       fi
95       rm -fr conftest*
96       AC_LANG_POP([C++])
97       AC_MSG_RESULT([$gl_cv_prog_ansicxx_namespaces])
98       if test $gl_cv_prog_ansicxx_namespaces = no; then
99         CXX=:
100       fi
101     fi
102   fi
103   m4_if([$1], [CXX], [],
104     [$1="$CXX"
105      CXX="$gl_save_CXX"])
106   AC_SUBST([$1])
107
108   AM_CONDITIONAL([$2], [test "$$1" != ":"])
109
110   dnl This macro invocation resolves an automake error:
111   dnl /usr/local/share/automake-1.11/am/depend2.am: am__fastdepCXX does not appear in AM_CONDITIONAL
112   dnl /usr/local/share/automake-1.11/am/depend2.am:   The usual way to define `am__fastdepCXX' is to add `AC_PROG_CXX'
113   dnl /usr/local/share/automake-1.11/am/depend2.am:   to `configure.ac' and run `aclocal' and `autoconf' again.
114   _AM_DEPENDENCIES([CXX])
115 ])