capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

ax_cxx_compile_stdcxx_14.m4 (7053B)


      1 # ============================================================================
      2 #  http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html
      3 #  Additionally modified to detect -stdlib by Kenton Varda.
      4 #  Further modified for C++14 by Kenton Varda.
      5 # ============================================================================
      6 #
      7 # SYNOPSIS
      8 #
      9 #   AX_CXX_COMPILE_STDCXX_14([ext|noext])
     10 #
     11 # DESCRIPTION
     12 #
     13 #   Check for baseline language coverage in the compiler for the C++14
     14 #   standard; if necessary, add switches to CXXFLAGS to enable support.
     15 #   Errors out if no mode that supports C++14 baseline syntax can be found.
     16 #   The argument, if specified, indicates whether you insist on an extended
     17 #   mode (e.g. -std=gnu++14) or a strict conformance mode (e.g. -std=c++14).
     18 #   If neither is specified, you get whatever works, with preference for an
     19 #   extended mode.
     20 #
     21 #   Additionally, check if the standard library supports C++11.  If not,
     22 #   try adding -stdlib=libc++ to see if that fixes it.  This is needed e.g.
     23 #   on Mac OSX 10.8, which ships with a very old libstdc++ but a relatively
     24 #   new libc++.
     25 #
     26 #   Both flags are actually added to CXX rather than CXXFLAGS to work around
     27 #   a bug in libtool: -stdlib is stripped from CXXFLAGS when linking dynamic
     28 #   libraries because it is not recognized.  A patch was committed to mainline
     29 #   libtool in February 2012 but as of June 2013 there has not yet been a
     30 #   release containing this patch.
     31 #      http://git.savannah.gnu.org/gitweb/?p=libtool.git;a=commit;h=c0c49f289f22ae670066657c60905986da3b555f
     32 #
     33 # LICENSE
     34 #
     35 #   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
     36 #   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
     37 #   Copyright (c) 2013 Kenton Varda <temporal@gmail.com>
     38 #
     39 #   Copying and distribution of this file, with or without modification, are
     40 #   permitted in any medium without royalty provided the copyright notice
     41 #   and this notice are preserved. This file is offered as-is, without any
     42 #   warranty.
     43 
     44 #serial 1
     45 
     46 m4_define([_AX_CXX_COMPILE_STDCXX_14_testbody], [[
     47   template <typename T>
     48   struct check
     49   {
     50     static_assert(sizeof(int) <= sizeof(T), "not big enough");
     51   };
     52 
     53   typedef check<check<bool>> right_angle_brackets;
     54 
     55   int a;
     56   decltype(a) b;
     57 
     58   typedef check<int> check_type;
     59   check_type c;
     60   check_type&& cr = static_cast<check_type&&>(c);
     61 
     62   // GCC 4.7 introduced __float128 and makes reference to it in type_traits.
     63   // Clang doesn't implement it, so produces an error.  Using -std=c++11
     64   // instead of -std=gnu++11 works around the problem.  But on some
     65   // platforms we need -std=gnu++11.  So we want to make sure the test of
     66   // -std=gnu++11 fails only where this problem is present, and we hope that
     67   // -std=c++11 is always an acceptable fallback in these cases.  Complicating
     68   // matters, though, is that we don't want to fail here if the platform is
     69   // completely missing a C++11 standard library, because we want to probe that
     70   // in a later test.  It happens, though, that Clang allows us to check
     71   // whether a header exists at all before we include it.
     72   //
     73   // So, if we detect that __has_include is available (which it is on Clang),
     74   // and we use it to detect that <type_traits> (a C++11 header) exists, then
     75   // we go ahead and #include it to see if it breaks.  In all other cases, we
     76   // don't #include it at all.
     77   #ifdef __has_include
     78   #if __has_include(<type_traits>)
     79   #include <type_traits>
     80   #endif
     81   #endif
     82 
     83   // C++14 stuff
     84   auto deduceReturnType(int i) { return i; }
     85 
     86   auto genericLambda = [](auto x, auto y) { return x + y; };
     87   auto captureExpressions = [x = 123]() { return x; };
     88 
     89   // Avoid unused variable warnings.
     90   int foo() {
     91     return genericLambda(1, 2) + captureExpressions();
     92   }
     93 ]])
     94 
     95 m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody_lib], [
     96   #include <initializer_list>
     97   #include <unordered_map>
     98   #include <atomic>
     99   #include <thread>
    100 ])
    101 
    102 AC_DEFUN([AX_CXX_COMPILE_STDCXX_14], [dnl
    103   m4_if([$1], [], [],
    104         [$1], [ext], [],
    105         [$1], [noext], [],
    106         [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_14])])dnl
    107   AC_LANG_ASSERT([C++])dnl
    108   ac_success=no
    109   AC_CACHE_CHECK(whether $CXX supports C++14 features by default,
    110   ax_cv_cxx_compile_cxx14,
    111   [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])],
    112     [ax_cv_cxx_compile_cxx14=yes],
    113     [ax_cv_cxx_compile_cxx14=no])])
    114   if test x$ax_cv_cxx_compile_cxx14 = xyes; then
    115     ac_success=yes
    116   fi
    117 
    118   m4_if([$1], [noext], [], [dnl
    119   if test x$ac_success = xno; then
    120     for switch in -std=gnu++14 -std=gnu++1y; do
    121       cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx14_$switch])
    122       AC_CACHE_CHECK(whether $CXX supports C++14 features with $switch,
    123                      $cachevar,
    124         [ac_save_CXX="$CXX"
    125          CXX="$CXX $switch"
    126          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])],
    127           [eval $cachevar=yes],
    128           [eval $cachevar=no])
    129          CXX="$ac_save_CXX"])
    130       if eval test x\$$cachevar = xyes; then
    131         CXX="$CXX $switch"
    132         ac_success=yes
    133         break
    134       fi
    135     done
    136   fi])
    137 
    138   m4_if([$1], [ext], [], [dnl
    139   if test x$ac_success = xno; then
    140     for switch in -std=c++14 -std=c++1y; do
    141       cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx14_$switch])
    142       AC_CACHE_CHECK(whether $CXX supports C++14 features with $switch,
    143                      $cachevar,
    144         [ac_save_CXX="$CXX"
    145          CXX="$CXX $switch"
    146          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])],
    147           [eval $cachevar=yes],
    148           [eval $cachevar=no])
    149          CXX="$ac_save_CXX"])
    150       if eval test x\$$cachevar = xyes; then
    151         CXX="$CXX $switch"
    152         ac_success=yes
    153         break
    154       fi
    155     done
    156   fi])
    157 
    158   if test x$ac_success = xno; then
    159     AC_MSG_ERROR([*** A compiler with support for C++14 language features is required.])
    160   else
    161     ac_success=no
    162     AC_CACHE_CHECK(whether $CXX supports C++11 library features by default,
    163                    ax_cv_cxx_compile_cxx11_lib,
    164       [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody_lib])],
    165          [ax_cv_cxx_compile_cxx11_lib=yes],
    166          [ax_cv_cxx_compile_cxx11_lib=no])
    167       ])
    168     if test x$ax_cv_cxx_compile_cxx11_lib = xyes; then
    169       ac_success=yes
    170     else
    171       # Try with -stdlib=libc++
    172       AC_CACHE_CHECK(whether $CXX supports C++11 library features with -stdlib=libc++,
    173                      ax_cv_cxx_compile_cxx11_lib_libcxx,
    174         [ac_save_CXX="$CXX"
    175          CXX="$CXX -stdlib=libc++"
    176          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody_lib])],
    177           [eval ax_cv_cxx_compile_cxx11_lib_libcxx=yes],
    178           [eval ax_cv_cxx_compile_cxx11_lib_libcxx=no])
    179          CXX="$ac_save_CXX"])
    180       if eval test x$ax_cv_cxx_compile_cxx11_lib_libcxx = xyes; then
    181         CXX="$CXX -stdlib=libc++"
    182         ac_success=yes
    183         break
    184       fi
    185     fi
    186 
    187     if test x$ac_success = xno; then
    188       AC_MSG_ERROR([*** A C++ library with support for C++11 features is required.])
    189     fi
    190   fi
    191 ])