Merge branch 'upstream' into stable
[gnulib.git] / m4 / ansi-c++.m4
1 # ansi-c++.m4 serial 4
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_PROGS([CXX], [g++ c++ gpp aCC CC cxx cc++ cl FCC KCC RCC xlC_r xlC], [:])
52     fi
53   fi
54   if test "$CXX" != ":"; then
55     dnl Use a modified version of AC_PROG_CXX_WORKS that does not exit
56     dnl upon failure.
57     AC_MSG_CHECKING([whether the C++ compiler ($CXX $CXXFLAGS $LDFLAGS) works])
58     AC_LANG_PUSH([C++])
59     AC_ARG_VAR([CXX], [C++ compiler command])
60     AC_ARG_VAR([CXXFLAGS], [C++ compiler flags])
61     echo 'int main () { return 0; }' > conftest.$ac_ext
62     if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
63       gl_cv_prog_ansicxx_works=yes
64       if (./conftest; exit) 2>/dev/null; then
65         gl_cv_prog_ansicxx_cross=no
66       else
67         gl_cv_prog_ansicxx_cross=yes
68       fi
69     else
70       gl_cv_prog_ansicxx_works=no
71     fi
72     rm -fr conftest*
73     AC_LANG_POP([C++])
74     AC_MSG_RESULT([$gl_cv_prog_ansicxx_works])
75     if test $gl_cv_prog_ansicxx_works = no; then
76       CXX=:
77     else
78       dnl Test for namespaces.
79       dnl We don't bother supporting pre-ANSI-C++ compilers.
80       AC_MSG_CHECKING([whether the C++ compiler supports namespaces])
81       AC_LANG_PUSH([C++])
82       cat <<EOF > conftest.$ac_ext
83 #include <iostream>
84 namespace test { using namespace std; }
85 std::ostream* ptr;
86 int main () { return 0; }
87 EOF
88       if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
89         gl_cv_prog_ansicxx_namespaces=yes
90       else
91         gl_cv_prog_ansicxx_namespaces=no
92       fi
93       rm -fr conftest*
94       AC_LANG_POP([C++])
95       AC_MSG_RESULT([$gl_cv_prog_ansicxx_namespaces])
96       if test $gl_cv_prog_ansicxx_namespaces = no; then
97         CXX=:
98       fi
99     fi
100   fi
101   m4_if([$1], [CXX], [],
102     [$1="$CXX"
103      CXX="$gl_save_CXX"])
104   AC_SUBST([$1])
105
106   AM_CONDITIONAL([$2], [test "$$1" != ":"])
107
108   dnl This macro invocation resolves an automake error:
109   dnl /usr/local/share/automake-1.11/am/depend2.am: am__fastdepCXX does not appear in AM_CONDITIONAL
110   dnl /usr/local/share/automake-1.11/am/depend2.am:   The usual way to define `am__fastdepCXX' is to add `AC_PROG_CXX'
111   dnl /usr/local/share/automake-1.11/am/depend2.am:   to `configure.ac' and run `aclocal' and `autoconf' again.
112   _AM_DEPENDENCIES([CXX])
113 ])