source: trunk/m4/yat_cxx11.m4 @ 3730

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

new function print_what() to print error message from std::nested_exception

  • 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 3730 2018-04-10 07:36:08Z peter $
2#
3# serial 4  (yat 0.15)
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: 2018-04-10 07:36:08 +0000 (Tue, 10 Apr 2018) $
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  void foo(void);
113  ],[ dnl body
114  try {
115    foo();
116  }
117  catch (std::runtime_error& e) {
118    std::throw_with_nested(std::runtime_error("foo"), e);
119  }
120  catch (std::exception& e) {
121    std::rethrow_if_nested(e);
122  }
123  ], [$1], [$2], [$3])
124])
125
126
127# YAT_CXX_TRY_CXX11(feature, prologue, body, [action-if-success],
128#                   [action-if-failed], [search-options])
129#
130# Try c++11 feature, feature, by using code with prologue and body. If
131# successful, issue shell code action-if-success, otherwise if
132# search-options is 'yes' retry with different CXX options as detailed
133# in YAT_CXX_SEARCH_CXX11; if none works, issue shell code action-if-failed.
134AC_DEFUN([YAT_CXX_TRY_CXX11],
135[
136AS_IF([test x"$6" = x"yes"], [
137  YAT_CXX_SEARCH_CXX11([$1], [$2], [$3], [$4], [$5])
138], [
139  AS_VAR_PUSHDEF([my_CACHE], [yat_cv_cxx_try_$1])dnl
140  AC_CACHE_CHECK([if $CXX supports $1],
141    [my_CACHE],
142    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$2], [$3])],
143                       [my_CACHE=yes],
144                       [my_CACHE=no])])
145  AS_VAR_IF([my_CACHE], [yes], [$4], [$5])
146  AS_VAR_POPDEF([my_CACHE])
147])
148]) #YAT_CXX_TRY_CXX11
149
150
151# YAT_CXX_SEARCH_CXX11([feature], [prologue], [body], [action-if-found],
152#                      [action-if-not-found])
153#
154# Try feature by using code with prologue and body, first with CXX and
155# then by adding switches to CXX. If a switch is found with which code
156# compiles, issue action-if-found; otherwise issue
157# action-if-not-found.
158AC_DEFUN([YAT_CXX_SEARCH_CXX11],
159[
160AS_VAR_PUSHDEF([my_CACHE], [yat_cv_cxx_search_$1])dnl
161AC_CACHE_CHECK([for $CXX option to enable $1],
162  [my_CACHE],
163  [my_CACHE=no
164   AS_VAR_PUSHDEF([my_save_CXX], [yat_cxx_$1_save_CXX])dnl
165   my_save_CXX="$CXX"
166# From autoconf 'c.m4' we have
167# GCC -std=c++11 -std=c++0x
168# IBM XL C -qlanglvl=stdcxx11
169# HP aC++ -AA
170# INTEL -std=c++11 -std=c++0x
171# from ax_cxx_compile_stdcxx.m4 we have
172# HP's aCC needs +std=c++11
173# Cray's crayCC '-h std=c++11'
174# but first we try with empty string, hoping compiler is modern.
175   for yat_arg in '' -std=c++11 -std=c++0x -qlanglvl=stdcxx11 -AA +std=c++11 "-h std=c++11" ; do
176     CXX="$my_save_CXX $yat_arg"
177     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$2], [$3])],
178                       [my_CACHE="$yat_arg"],
179                       [])
180     AS_VAR_IF([my_CACHE], [no], [], [break])
181   done
182   # restore CXX
183   CXX="$my_save_CXX"
184
185   AS_CASE([x"$my_CACHE"],
186           [x], [my_CACHE="none needed"],
187           [x"no"], [my_CACHE=unsupported])
188
189   AS_VAR_POPDEF([my_save_CXX])
190  ])
191
192AS_CASE([$my_CACHE],
193        ["none needed"], [$4],
194        [unsupported], [$5],
195        [CXX="$CXX $my_CACHE"
196         $4])
197AS_VAR_POPDEF([my_CACHE])
198])
Note: See TracBrowser for help on using the repository browser.