blob: 97ec37d4c98246cabfbce6d663c29e19b9b2e9ce [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001From ccb3eb7b4ff65414a56e2294080885b8966da52b Mon Sep 17 00:00:00 2001
2From: Arun Raghavan <git@arunraghavan.net>
3Date: Tue, 2 Feb 2016 17:01:47 +0530
4Subject: [PATCH 2/2] build-sys: Add m4 file for AX_CXX_COMPILE_STDCXX_11
5
6---
7 m4/ax_cxx_compile_stdcxx.m4 | 558 +++++++++++++++++++++++++++++++++++++++++
8 m4/ax_cxx_compile_stdcxx_11.m4 | 39 +++
9 2 files changed, 597 insertions(+)
10 create mode 100644 m4/ax_cxx_compile_stdcxx.m4
11 create mode 100644 m4/ax_cxx_compile_stdcxx_11.m4
12
13diff --git a/m4/ax_cxx_compile_stdcxx.m4 b/m4/ax_cxx_compile_stdcxx.m4
14new file mode 100644
15index 0000000..079e17d
16--- /dev/null
17+++ b/m4/ax_cxx_compile_stdcxx.m4
18@@ -0,0 +1,558 @@
19+# ===========================================================================
20+# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
21+# ===========================================================================
22+#
23+# SYNOPSIS
24+#
25+# AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
26+#
27+# DESCRIPTION
28+#
29+# Check for baseline language coverage in the compiler for the specified
30+# version of the C++ standard. If necessary, add switches to CXXFLAGS to
31+# enable support. VERSION may be '11' (for the C++11 standard) or '14'
32+# (for the C++14 standard).
33+#
34+# The second argument, if specified, indicates whether you insist on an
35+# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
36+# -std=c++11). If neither is specified, you get whatever works, with
37+# preference for an extended mode.
38+#
39+# The third argument, if specified 'mandatory' or if left unspecified,
40+# indicates that baseline support for the specified C++ standard is
41+# required and that the macro should error out if no mode with that
42+# support is found. If specified 'optional', then configuration proceeds
43+# regardless, after defining HAVE_CXX${VERSION} if and only if a
44+# supporting mode is found.
45+#
46+# LICENSE
47+#
48+# Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
49+# Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
50+# Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
51+# Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
52+# Copyright (c) 2015 Paul Norman <penorman@mac.com>
53+# Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
54+#
55+# Copying and distribution of this file, with or without modification, are
56+# permitted in any medium without royalty provided the copyright notice
57+# and this notice are preserved. This file is offered as-is, without any
58+# warranty.
59+
60+#serial 1
61+
62+dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
63+dnl (serial version number 13).
64+
65+AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
66+ m4_if([$1], [11], [],
67+ [$1], [14], [],
68+ [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],
69+ [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
70+ m4_if([$2], [], [],
71+ [$2], [ext], [],
72+ [$2], [noext], [],
73+ [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
74+ m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
75+ [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
76+ [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
77+ [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
78+ AC_LANG_PUSH([C++])dnl
79+ ac_success=no
80+ AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
81+ ax_cv_cxx_compile_cxx$1,
82+ [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
83+ [ax_cv_cxx_compile_cxx$1=yes],
84+ [ax_cv_cxx_compile_cxx$1=no])])
85+ if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
86+ ac_success=yes
87+ fi
88+
89+ m4_if([$2], [noext], [], [dnl
90+ if test x$ac_success = xno; then
91+ for switch in -std=gnu++$1 -std=gnu++0x; do
92+ cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
93+ AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
94+ $cachevar,
95+ [ac_save_CXXFLAGS="$CXXFLAGS"
96+ CXXFLAGS="$CXXFLAGS $switch"
97+ AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
98+ [eval $cachevar=yes],
99+ [eval $cachevar=no])
100+ CXXFLAGS="$ac_save_CXXFLAGS"])
101+ if eval test x\$$cachevar = xyes; then
102+ CXXFLAGS="$CXXFLAGS $switch"
103+ ac_success=yes
104+ break
105+ fi
106+ done
107+ fi])
108+
109+ m4_if([$2], [ext], [], [dnl
110+ if test x$ac_success = xno; then
111+ dnl HP's aCC needs +std=c++11 according to:
112+ dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
113+ dnl Cray's crayCC needs "-h std=c++11"
114+ for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do
115+ cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
116+ AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
117+ $cachevar,
118+ [ac_save_CXXFLAGS="$CXXFLAGS"
119+ CXXFLAGS="$CXXFLAGS $switch"
120+ AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
121+ [eval $cachevar=yes],
122+ [eval $cachevar=no])
123+ CXXFLAGS="$ac_save_CXXFLAGS"])
124+ if eval test x\$$cachevar = xyes; then
125+ CXXFLAGS="$CXXFLAGS $switch"
126+ ac_success=yes
127+ break
128+ fi
129+ done
130+ fi])
131+ AC_LANG_POP([C++])
132+ if test x$ax_cxx_compile_cxx$1_required = xtrue; then
133+ if test x$ac_success = xno; then
134+ AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
135+ fi
136+ else
137+ if test x$ac_success = xno; then
138+ HAVE_CXX$1=0
139+ AC_MSG_NOTICE([No compiler with C++$1 support was found])
140+ else
141+ HAVE_CXX$1=1
142+ AC_DEFINE(HAVE_CXX$1,1,
143+ [define if the compiler supports basic C++$1 syntax])
144+ fi
145+
146+ AC_SUBST(HAVE_CXX$1)
147+ fi
148+])
149+
150+
151+dnl Test body for checking C++11 support
152+
153+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
154+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
155+)
156+
157+
158+dnl Test body for checking C++14 support
159+
160+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
161+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
162+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
163+)
164+
165+
166+dnl Tests for new features in C++11
167+
168+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
169+
170+// If the compiler admits that it is not ready for C++11, why torture it?
171+// Hopefully, this will speed up the test.
172+
173+#ifndef __cplusplus
174+
175+#error "This is not a C++ compiler"
176+
177+#elif __cplusplus < 201103L
178+
179+#error "This is not a C++11 compiler"
180+
181+#else
182+
183+namespace cxx11
184+{
185+
186+ namespace test_static_assert
187+ {
188+
189+ template <typename T>
190+ struct check
191+ {
192+ static_assert(sizeof(int) <= sizeof(T), "not big enough");
193+ };
194+
195+ }
196+
197+ namespace test_final_override
198+ {
199+
200+ struct Base
201+ {
202+ virtual void f() {}
203+ };
204+
205+ struct Derived : public Base
206+ {
207+ virtual void f() override {}
208+ };
209+
210+ }
211+
212+ namespace test_double_right_angle_brackets
213+ {
214+
215+ template < typename T >
216+ struct check {};
217+
218+ typedef check<void> single_type;
219+ typedef check<check<void>> double_type;
220+ typedef check<check<check<void>>> triple_type;
221+ typedef check<check<check<check<void>>>> quadruple_type;
222+
223+ }
224+
225+ namespace test_decltype
226+ {
227+
228+ int
229+ f()
230+ {
231+ int a = 1;
232+ decltype(a) b = 2;
233+ return a + b;
234+ }
235+
236+ }
237+
238+ namespace test_type_deduction
239+ {
240+
241+ template < typename T1, typename T2 >
242+ struct is_same
243+ {
244+ static const bool value = false;
245+ };
246+
247+ template < typename T >
248+ struct is_same<T, T>
249+ {
250+ static const bool value = true;
251+ };
252+
253+ template < typename T1, typename T2 >
254+ auto
255+ add(T1 a1, T2 a2) -> decltype(a1 + a2)
256+ {
257+ return a1 + a2;
258+ }
259+
260+ int
261+ test(const int c, volatile int v)
262+ {
263+ static_assert(is_same<int, decltype(0)>::value == true, "");
264+ static_assert(is_same<int, decltype(c)>::value == false, "");
265+ static_assert(is_same<int, decltype(v)>::value == false, "");
266+ auto ac = c;
267+ auto av = v;
268+ auto sumi = ac + av + 'x';
269+ auto sumf = ac + av + 1.0;
270+ static_assert(is_same<int, decltype(ac)>::value == true, "");
271+ static_assert(is_same<int, decltype(av)>::value == true, "");
272+ static_assert(is_same<int, decltype(sumi)>::value == true, "");
273+ static_assert(is_same<int, decltype(sumf)>::value == false, "");
274+ static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
275+ return (sumf > 0.0) ? sumi : add(c, v);
276+ }
277+
278+ }
279+
280+ namespace test_noexcept
281+ {
282+
283+ int f() { return 0; }
284+ int g() noexcept { return 0; }
285+
286+ static_assert(noexcept(f()) == false, "");
287+ static_assert(noexcept(g()) == true, "");
288+
289+ }
290+
291+ namespace test_constexpr
292+ {
293+
294+ template < typename CharT >
295+ unsigned long constexpr
296+ strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
297+ {
298+ return *s ? strlen_c_r(s + 1, acc + 1) : acc;
299+ }
300+
301+ template < typename CharT >
302+ unsigned long constexpr
303+ strlen_c(const CharT *const s) noexcept
304+ {
305+ return strlen_c_r(s, 0UL);
306+ }
307+
308+ static_assert(strlen_c("") == 0UL, "");
309+ static_assert(strlen_c("1") == 1UL, "");
310+ static_assert(strlen_c("example") == 7UL, "");
311+ static_assert(strlen_c("another\0example") == 7UL, "");
312+
313+ }
314+
315+ namespace test_rvalue_references
316+ {
317+
318+ template < int N >
319+ struct answer
320+ {
321+ static constexpr int value = N;
322+ };
323+
324+ answer<1> f(int&) { return answer<1>(); }
325+ answer<2> f(const int&) { return answer<2>(); }
326+ answer<3> f(int&&) { return answer<3>(); }
327+
328+ void
329+ test()
330+ {
331+ int i = 0;
332+ const int c = 0;
333+ static_assert(decltype(f(i))::value == 1, "");
334+ static_assert(decltype(f(c))::value == 2, "");
335+ static_assert(decltype(f(0))::value == 3, "");
336+ }
337+
338+ }
339+
340+ namespace test_uniform_initialization
341+ {
342+
343+ struct test
344+ {
345+ static const int zero {};
346+ static const int one {1};
347+ };
348+
349+ static_assert(test::zero == 0, "");
350+ static_assert(test::one == 1, "");
351+
352+ }
353+
354+ namespace test_lambdas
355+ {
356+
357+ void
358+ test1()
359+ {
360+ auto lambda1 = [](){};
361+ auto lambda2 = lambda1;
362+ lambda1();
363+ lambda2();
364+ }
365+
366+ int
367+ test2()
368+ {
369+ auto a = [](int i, int j){ return i + j; }(1, 2);
370+ auto b = []() -> int { return '0'; }();
371+ auto c = [=](){ return a + b; }();
372+ auto d = [&](){ return c; }();
373+ auto e = [a, &b](int x) mutable {
374+ const auto identity = [](int y){ return y; };
375+ for (auto i = 0; i < a; ++i)
376+ a += b--;
377+ return x + identity(a + b);
378+ }(0);
379+ return a + b + c + d + e;
380+ }
381+
382+ int
383+ test3()
384+ {
385+ const auto nullary = [](){ return 0; };
386+ const auto unary = [](int x){ return x; };
387+ using nullary_t = decltype(nullary);
388+ using unary_t = decltype(unary);
389+ const auto higher1st = [](nullary_t f){ return f(); };
390+ const auto higher2nd = [unary](nullary_t f1){
391+ return [unary, f1](unary_t f2){ return f2(unary(f1())); };
392+ };
393+ return higher1st(nullary) + higher2nd(nullary)(unary);
394+ }
395+
396+ }
397+
398+ namespace test_variadic_templates
399+ {
400+
401+ template <int...>
402+ struct sum;
403+
404+ template <int N0, int... N1toN>
405+ struct sum<N0, N1toN...>
406+ {
407+ static constexpr auto value = N0 + sum<N1toN...>::value;
408+ };
409+
410+ template <>
411+ struct sum<>
412+ {
413+ static constexpr auto value = 0;
414+ };
415+
416+ static_assert(sum<>::value == 0, "");
417+ static_assert(sum<1>::value == 1, "");
418+ static_assert(sum<23>::value == 23, "");
419+ static_assert(sum<1, 2>::value == 3, "");
420+ static_assert(sum<5, 5, 11>::value == 21, "");
421+ static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
422+
423+ }
424+
425+ // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
426+ // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
427+ // because of this.
428+ namespace test_template_alias_sfinae
429+ {
430+
431+ struct foo {};
432+
433+ template<typename T>
434+ using member = typename T::member_type;
435+
436+ template<typename T>
437+ void func(...) {}
438+
439+ template<typename T>
440+ void func(member<T>*) {}
441+
442+ void test();
443+
444+ void test() { func<foo>(0); }
445+
446+ }
447+
448+} // namespace cxx11
449+
450+#endif // __cplusplus >= 201103L
451+
452+]])
453+
454+
455+dnl Tests for new features in C++14
456+
457+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
458+
459+// If the compiler admits that it is not ready for C++14, why torture it?
460+// Hopefully, this will speed up the test.
461+
462+#ifndef __cplusplus
463+
464+#error "This is not a C++ compiler"
465+
466+#elif __cplusplus < 201402L
467+
468+#error "This is not a C++14 compiler"
469+
470+#else
471+
472+namespace cxx14
473+{
474+
475+ namespace test_polymorphic_lambdas
476+ {
477+
478+ int
479+ test()
480+ {
481+ const auto lambda = [](auto&&... args){
482+ const auto istiny = [](auto x){
483+ return (sizeof(x) == 1UL) ? 1 : 0;
484+ };
485+ const int aretiny[] = { istiny(args)... };
486+ return aretiny[0];
487+ };
488+ return lambda(1, 1L, 1.0f, '1');
489+ }
490+
491+ }
492+
493+ namespace test_binary_literals
494+ {
495+
496+ constexpr auto ivii = 0b0000000000101010;
497+ static_assert(ivii == 42, "wrong value");
498+
499+ }
500+
501+ namespace test_generalized_constexpr
502+ {
503+
504+ template < typename CharT >
505+ constexpr unsigned long
506+ strlen_c(const CharT *const s) noexcept
507+ {
508+ auto length = 0UL;
509+ for (auto p = s; *p; ++p)
510+ ++length;
511+ return length;
512+ }
513+
514+ static_assert(strlen_c("") == 0UL, "");
515+ static_assert(strlen_c("x") == 1UL, "");
516+ static_assert(strlen_c("test") == 4UL, "");
517+ static_assert(strlen_c("another\0test") == 7UL, "");
518+
519+ }
520+
521+ namespace test_lambda_init_capture
522+ {
523+
524+ int
525+ test()
526+ {
527+ auto x = 0;
528+ const auto lambda1 = [a = x](int b){ return a + b; };
529+ const auto lambda2 = [a = lambda1(x)](){ return a; };
530+ return lambda2();
531+ }
532+
533+ }
534+
535+ namespace test_digit_seperators
536+ {
537+
538+ constexpr auto ten_million = 100'000'000;
539+ static_assert(ten_million == 100000000, "");
540+
541+ }
542+
543+ namespace test_return_type_deduction
544+ {
545+
546+ auto f(int& x) { return x; }
547+ decltype(auto) g(int& x) { return x; }
548+
549+ template < typename T1, typename T2 >
550+ struct is_same
551+ {
552+ static constexpr auto value = false;
553+ };
554+
555+ template < typename T >
556+ struct is_same<T, T>
557+ {
558+ static constexpr auto value = true;
559+ };
560+
561+ int
562+ test()
563+ {
564+ auto x = 0;
565+ static_assert(is_same<int, decltype(f(x))>::value, "");
566+ static_assert(is_same<int&, decltype(g(x))>::value, "");
567+ return x;
568+ }
569+
570+ }
571+
572+} // namespace cxx14
573+
574+#endif // __cplusplus >= 201402L
575+
576+]])
577diff --git a/m4/ax_cxx_compile_stdcxx_11.m4 b/m4/ax_cxx_compile_stdcxx_11.m4
578new file mode 100644
579index 0000000..09db383
580--- /dev/null
581+++ b/m4/ax_cxx_compile_stdcxx_11.m4
582@@ -0,0 +1,39 @@
583+# ============================================================================
584+# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html
585+# ============================================================================
586+#
587+# SYNOPSIS
588+#
589+# AX_CXX_COMPILE_STDCXX_11([ext|noext], [mandatory|optional])
590+#
591+# DESCRIPTION
592+#
593+# Check for baseline language coverage in the compiler for the C++11
594+# standard; if necessary, add switches to CXXFLAGS to enable support.
595+#
596+# This macro is a convenience alias for calling the AX_CXX_COMPILE_STDCXX
597+# macro with the version set to C++11. The two optional arguments are
598+# forwarded literally as the second and third argument respectively.
599+# Please see the documentation for the AX_CXX_COMPILE_STDCXX macro for
600+# more information. If you want to use this macro, you also need to
601+# download the ax_cxx_compile_stdcxx.m4 file.
602+#
603+# LICENSE
604+#
605+# Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
606+# Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
607+# Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
608+# Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
609+# Copyright (c) 2015 Paul Norman <penorman@mac.com>
610+# Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
611+#
612+# Copying and distribution of this file, with or without modification, are
613+# permitted in any medium without royalty provided the copyright notice
614+# and this notice are preserved. This file is offered as-is, without any
615+# warranty.
616+
617+#serial 14
618+
619+include([ax_cxx_compile_stdcxx.m4])
620+
621+AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [AX_CXX_COMPILE_STDCXX([11], [$1], [$2])])
622--
6232.12.1
624