ax_cxx_compile_stdcxx_11.m4 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # ============================================================================
  2. # http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html
  3. # ============================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_CXX_COMPILE_STDCXX_11([ext|noext],[mandatory|optional])
  8. #
  9. # DESCRIPTION
  10. #
  11. # Check for baseline language coverage in the compiler for the C++11
  12. # standard; if necessary, add switches to CXXFLAGS to enable support.
  13. #
  14. # The first argument, if specified, indicates whether you insist on an
  15. # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
  16. # -std=c++11). If neither is specified, you get whatever works, with
  17. # preference for an extended mode.
  18. #
  19. # The second argument, if specified 'mandatory' or if left unspecified,
  20. # indicates that baseline C++11 support is required and that the macro
  21. # should error out if no mode with that support is found. If specified
  22. # 'optional', then configuration proceeds regardless, after defining
  23. # HAVE_CXX11 if and only if a supporting mode is found.
  24. #
  25. # This version of AX_CXX_COMPILE_STDCXX_11 will also check if specifying
  26. # `-stdlib=libc++`` is required, as it is on current OSX systems using
  27. # a clang which defaults to an old libstdc++.
  28. #
  29. # LICENSE
  30. #
  31. # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
  32. # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
  33. # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
  34. # Copyright (c) 2013 Nils Maier <maierman@web.de>
  35. #
  36. # Copying and distribution of this file, with or without modification, are
  37. # permitted in any medium without royalty provided the copyright notice
  38. # and this notice are preserved. This file is offered as-is, without any
  39. # warranty.
  40. #serial 3
  41. #modified to check fo -stdlib=libc++ (required on OSX)
  42. m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody], [
  43. #include <memory>
  44. template <typename T>
  45. struct check
  46. {
  47. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  48. };
  49. typedef check<check<bool>> right_angle_brackets;
  50. int a;
  51. decltype(a) b;
  52. typedef check<int> check_type;
  53. check_type c;
  54. check_type&& cr = static_cast<check_type&&>(c);
  55. auto d = a;
  56. // Check std::shared_ptr is available, which might require -stdlib=libc++.
  57. std::shared_ptr<check_type> ptr;
  58. ])
  59. AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [dnl
  60. m4_if([$1], [], [],
  61. [$1], [ext], [],
  62. [$1], [noext], [],
  63. [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_11])])dnl
  64. m4_if([$2], [], [ax_cxx_compile_cxx11_required=true],
  65. [$2], [mandatory], [ax_cxx_compile_cxx11_required=true],
  66. [$2], [optional], [ax_cxx_compile_cxx11_required=false],
  67. [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX_11])])
  68. AC_LANG_PUSH([C++])dnl
  69. ac_success=no
  70. AC_CACHE_CHECK(whether $CXX supports C++11 features by default,
  71. ax_cv_cxx_compile_cxx11,
  72. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  73. [ax_cv_cxx_compile_cxx11=yes],
  74. [ax_cv_cxx_compile_cxx11=no])])
  75. if test x$ax_cv_cxx_compile_cxx11 = xyes; then
  76. ac_success=yes
  77. fi
  78. m4_if([$1], [noext], [], [dnl
  79. if test x$ac_success = xno; then
  80. for switch in -std=gnu++11 -std=gnu++0x; do
  81. for lib in "" -stdlib=libc++; do
  82. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch$lib])
  83. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch $lib,
  84. $cachevar,
  85. [ac_save_CXXFLAGS="$CXXFLAGS"
  86. CXXFLAGS="$CXXFLAGS $switch $lib"
  87. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  88. [eval $cachevar=yes],
  89. [eval $cachevar=no])
  90. CXXFLAGS="$ac_save_CXXFLAGS"])
  91. if eval test x\$$cachevar = xyes; then
  92. CXXFLAGS="$CXXFLAGS $switch $lib"
  93. ac_success=yes
  94. break 2
  95. fi
  96. done
  97. done
  98. fi])
  99. m4_if([$1], [ext], [], [dnl
  100. if test x$ac_success = xno; then
  101. for switch in -std=c++11 -std=c++0x; do
  102. for lib in "" -stdlib=libc++; do
  103. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch$lib])
  104. AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch $lib,
  105. $cachevar,
  106. [ac_save_CXXFLAGS="$CXXFLAGS"
  107. CXXFLAGS="$CXXFLAGS $switch $lib"
  108. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])],
  109. [eval $cachevar=yes],
  110. [eval $cachevar=no])
  111. CXXFLAGS="$ac_save_CXXFLAGS"])
  112. if eval test x\$$cachevar = xyes; then
  113. CXXFLAGS="$CXXFLAGS $switch $lib"
  114. ac_success=yes
  115. break 2
  116. fi
  117. done
  118. done
  119. fi])
  120. AC_LANG_POP([C++])
  121. if test x$ax_cxx_compile_cxx11_required = xtrue; then
  122. if test x$ac_success = xno; then
  123. AC_MSG_ERROR([*** A compiler with support for C++11 language features is required.])
  124. fi
  125. else
  126. if test x$ac_success = xno; then
  127. HAVE_CXX11=0
  128. AC_MSG_NOTICE([No compiler with C++11 support was found])
  129. else
  130. HAVE_CXX11=1
  131. AC_DEFINE(HAVE_CXX11,1,
  132. [define if the compiler supports basic C++11 syntax])
  133. fi
  134. AC_SUBST(HAVE_CXX11)
  135. fi
  136. ])