source: trunk/m4/yat_cxx11.m4 @ 3786

Last change on this file since 3786 was 3786, checked in by Peter, 5 years ago

The code snippet in YAT_CXX_THROW_IF_NESTED didn't compile also when rethrow_if_nested was available causing false negative test results. Fixed here.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date
File size: 5.9 KB
Line 
1## $Id: yat_cxx11.m4 3786 2019-01-29 04:46:04Z peter $
2#
3# serial 5  (yat 0.16)
4#
5# see http://www.gnu.org/software/automake/manual/automake.html#Serials
6#
7# SYNOPSIS
8#
9#   YAT_CXX_ATOMIC([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
10#   YAT_CXX_RVALUE([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
11#   YAT_CXX_LOG2([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
12#
13# DESCRIPTION
14#
15#   Test whether $CXX supports rvalues and other cxx11 features
16#
17# LAST MODIFICATION
18#
19#   $Date: 2019-01-29 04:46:04 +0000 (Tue, 29 Jan 2019) $
20#
21# COPYLEFT
22#
23#   Copyright (C) 2017 Peter Johansson
24#
25#   This file is part of the yat library, http://dev.thep.lu.se/yat
26#
27#   The yat library is free software; you can redistribute it and/or
28#   modify it under the terms of the GNU General Public License as
29#   published by the Free Software Foundation; either version 3 of the
30#   License, or (at your option) any later version.
31#
32#   The yat library is distributed in the hope that it will be useful,
33#   but WITHOUT ANY WARRANTY; without even the implied warranty of
34#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35#   General Public License for more details.
36#
37#   You should have received a copy of the GNU General Public License
38#   along with yat. If not, see <http://www.gnu.org/licenses/>.
39#
40
41# YAT_CXX_ATOMIC([action-if-found], [action-if-not-found], [search-options])
42#
43# Test whether $CXX supports std::atomic. If not and search-options is
44# 'yes', try turning on support with a number of different
45# switches. If a successful switch is found invoke shell
46# action-if-found; else issue action-if-not-found.
47AC_DEFUN([YAT_CXX_ATOMIC],
48[
49YAT_CXX_TRY_CXX11([atomic],
50  [@%:@include <atomic>],
51  [std::atomic<bool> ready(false);],
52  [$1], [$2], [$3])
53])
54
55
56# YAT_CXX_RVALUE([action-if-found], [action-if-not-found], [search-options])
57#
58# Test whether $CXX supports rvalue. If not and search-options is
59# 'yes', try turning on support with a number of different
60# switches. If a successful switch is found invoke shell
61# action-if-found; else issue action-if-not-found.
62AC_DEFUN([YAT_CXX_RVALUE],
63[
64YAT_CXX_TRY_CXX11([rvalue], [ dnl header
65  @%:@include <utility>
66
67  class MyClass
68  {
69  public:
70    MyClass(void);
71    MyClass(const MyClass&); // copy constructor
72    MyClass(MyClass&&); // move constructor
73    MyClass& operator=(const MyClass&); // copy assignment
74    MyClass& operator=(MyClass&&); // move assignment
75  };
76  ],[ dnl body
77  MyClass mc;
78  MyClass mc2 = std::move(mc);
79  ], [$1], [$2], [$3])
80])
81
82
83# YAT_CXX_LOG2([action-if-found], [action-if-not-found], [search-options])
84#
85# Test whether $CXX supports std::log2. If not and search-options is
86# 'yes', try turning on support with a number of different
87# switches. If a successful switch is found invoke shell
88# action-if-found; else issue action-if-not-found.
89AC_DEFUN([YAT_CXX_LOG2],
90[
91YAT_CXX_TRY_CXX11([log2], [ dnl header
92  @%:@include <cmath>
93  void foo(double x);
94  ],[ dnl body
95  double x = std::log2(3.14);
96  foo(x);
97  ], [$1], [$2], [$3])
98])
99
100
101# YAT_CXX_THROW_IF_NESTED([action-if-found], [action-if-not-found],
102#                         [search-options])
103#
104# Test whether $CXX supports std::nested_exception. If not and
105# search-options is 'yes', try turning on support with a number of
106# different switches. If a successful switch is found invoke shell
107# action-if-found; else issue action-if-not-found.
108AC_DEFUN([YAT_CXX_THROW_IF_NESTED],
109[
110YAT_CXX_TRY_CXX11([throw_if_nested], [ dnl header
111  @%:@include <exception>
112  @%:@include <stdexcept>
113  void foo(void);
114  ],[ dnl body
115  try {
116    foo();
117  }
118  catch (std::runtime_error& e) {
119    std::throw_with_nested(std::runtime_error("foo"));
120  }
121  catch (std::exception& e) {
122    std::rethrow_if_nested(e);
123  }
124  ], [$1], [$2], [$3])
125])
126
127
128# YAT_CXX_TRY_CXX11(feature, prologue, body, [action-if-success],
129#                   [action-if-failed], [search-options])
130#
131# Try c++11 feature, feature, by using code with prologue and body. If
132# successful, issue shell code action-if-success, otherwise if
133# search-options is 'yes' retry with different CXX options as detailed
134# in YAT_CXX_SEARCH_CXX11; if none works, issue shell code action-if-failed.
135AC_DEFUN([YAT_CXX_TRY_CXX11],
136[
137AS_IF([test x"$6" = x"yes"], [
138  YAT_CXX_SEARCH_CXX11([$1], [$2], [$3], [$4], [$5])
139], [
140  AS_VAR_PUSHDEF([my_CACHE], [yat_cv_cxx_try_$1])dnl
141  AC_CACHE_CHECK([if $CXX supports $1],
142    [my_CACHE],
143    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$2], [$3])],
144                       [my_CACHE=yes],
145                       [my_CACHE=no])])
146  AS_VAR_IF([my_CACHE], [yes], [$4], [$5])
147  AS_VAR_POPDEF([my_CACHE])
148])
149]) #YAT_CXX_TRY_CXX11
150
151
152# YAT_CXX_SEARCH_CXX11([feature], [prologue], [body], [action-if-found],
153#                      [action-if-not-found])
154#
155# Try feature by using code with prologue and body, first with CXX and
156# then by adding switches to CXX. If a switch is found with which code
157# compiles, issue action-if-found; otherwise issue
158# action-if-not-found.
159AC_DEFUN([YAT_CXX_SEARCH_CXX11],
160[
161AS_VAR_PUSHDEF([my_CACHE], [yat_cv_cxx_search_$1])dnl
162AC_CACHE_CHECK([for $CXX option to enable $1],
163  [my_CACHE],
164  [my_CACHE=no
165   AS_VAR_PUSHDEF([my_save_CXX], [yat_cxx_$1_save_CXX])dnl
166   my_save_CXX="$CXX"
167# From autoconf 'c.m4' we have
168# GCC -std=c++11 -std=c++0x
169# IBM XL C -qlanglvl=stdcxx11
170# HP aC++ -AA
171# INTEL -std=c++11 -std=c++0x
172# from ax_cxx_compile_stdcxx.m4 we have
173# HP's aCC needs +std=c++11
174# Cray's crayCC '-h std=c++11'
175# but first we try with empty string, hoping compiler is modern.
176   for yat_arg in '' -std=c++11 -std=c++0x -qlanglvl=stdcxx11 -AA +std=c++11 "-h std=c++11" ; do
177     CXX="$my_save_CXX $yat_arg"
178     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$2], [$3])],
179                       [my_CACHE="$yat_arg"],
180                       [])
181     AS_VAR_IF([my_CACHE], [no], [], [break])
182   done
183   # restore CXX
184   CXX="$my_save_CXX"
185
186   AS_CASE([x"$my_CACHE"],
187           [x], [my_CACHE="none needed"],
188           [x"no"], [my_CACHE=unsupported])
189
190   AS_VAR_POPDEF([my_save_CXX])
191  ])
192
193AS_CASE([$my_CACHE],
194        ["none needed"], [$4],
195        [unsupported], [$5],
196        [CXX="$CXX $my_CACHE"
197         $4])
198AS_VAR_POPDEF([my_CACHE])
199])
Note: See TracBrowser for help on using the repository browser.