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