source: trunk/m4/yat_cxx11.m4

Last change on this file was 3937, checked in by Peter, 11 months ago

refs #949. New macro YAT_CXX11 that tests if compiler is CXX compliant by using the underlying AX_CXX_COMPILE_STDCXX_11 macro

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