-
Notifications
You must be signed in to change notification settings - Fork 707
/
check_01_cxx_features.cmake
624 lines (538 loc) · 15.2 KB
/
check_01_cxx_features.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
## ---------------------------------------------------------------------
##
## Copyright (C) 2012 - 2020 by the deal.II authors
##
## This file is part of the deal.II library.
##
## The deal.II library is free software; you can use it, redistribute
## it, and/or modify it under the terms of the GNU Lesser General
## Public License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
## The full text of the license can be found in the file LICENSE.md at
## the top level directory of deal.II.
##
## ---------------------------------------------------------------------
#
# Check for various C++ language features
#
# This file sets up
#
# DEAL_II_HAVE_CXX14
# DEAL_II_HAVE_CXX17
# DEAL_II_HAVE_CXX20
#
# DEAL_II_HAVE_FP_EXCEPTIONS
# DEAL_II_HAVE_COMPLEX_OPERATOR_OVERLOADS
# DEAL_II_HAVE_CXX17_BESSEL_FUNCTIONS
# DEAL_II_FALLTHROUGH
# DEAL_II_DEPRECATED
# DEAL_II_CONSTEXPR
#
########################################################################
# #
# C++ Version Support: #
# #
########################################################################
#
# We need compiler flags specified in ${DEAL_II_CXX_FLAGS} for all the
# tests. Create a small macro to easily set CMAKE_REQUIRED_FLAGS
#
MACRO(_set_up_cmake_required)
# Let's put the user supplied `DEAL_II_CXX_FLAGS_SAVED` last so that we
# never override a user supplied -std=c++XY flag in our tests.
RESET_CMAKE_REQUIRED()
SET(CMAKE_REQUIRED_FLAGS "")
ADD_FLAGS(CMAKE_REQUIRED_FLAGS "${DEAL_II_CXX_FLAGS}")
ADD_FLAGS(CMAKE_REQUIRED_FLAGS "${DEAL_II_CXX_FLAGS_SAVED}")
ENDMACRO()
#
# Wrap the following checks into a macro to make it easier to rerun them.
#
MACRO(_test_cxx20_support)
UNSET_IF_CHANGED(CHECK_CXX20_FEATURES_FLAGS_SAVED
"${CMAKE_REQUIRED_FLAGS}"
DEAL_II_HAVE_CXX20_FEATURES
)
# Strictly speaking "201709L" indicates support for a preliminary version
# of C++20 standard (which will have "202002L" when finalized). gcc-10
# exports this version number when configured with C++20 support.
# clang-10 exports the final "202002L" version instead.
CHECK_CXX_SOURCE_COMPILES(
"
#include <cmath>
#include <ranges>
#if __cplusplus < 201709L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
# error \"insufficient support for C++20\"
#endif
#if !(defined __cpp_lib_ranges) || (__cpp_lib_ranges < 201911)
# error \"insufficient support for C++20\"
#endif
int main()
{
}
"
DEAL_II_HAVE_CXX20_FEATURES)
IF(DEAL_II_HAVE_CXX20_FEATURES)
MESSAGE(STATUS "C++20 support is enabled.")
SET(DEAL_II_HAVE_CXX20 TRUE)
ELSE()
MESSAGE(STATUS "C++20 support is disabled.")
SET(DEAL_II_HAVE_CXX20 FALSE)
ENDIF()
ENDMACRO()
#
# Wrap the following checks into a macro to make it easier to rerun them.
#
MACRO(_test_cxx17_support)
UNSET_IF_CHANGED(CHECK_CXX17_FEATURES_FLAGS_SAVED
"${CMAKE_REQUIRED_FLAGS}"
DEAL_II_HAVE_CXX17_FEATURES
DEAL_II_HAVE_CXX17_CONSTEXPR_LAMBDA_BUG_OK
)
CHECK_CXX_SOURCE_COMPILES(
"
#include <iostream>
#include <optional>
#include <tuple>
#if __cplusplus < 201703L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
# error \"insufficient support for C++17\"
#endif
//check for some C++17 features that we use in our headers:
using std::apply;
using std::optional;
[[nodiscard]] int test_nodiscard()
{
return 1;
}
int main()
{
const unsigned int n=1;
switch (n)
{
case 1:
std::cout << n;
[[fallthrough]];
case 2:
std::cout << n;
}
[[maybe_unused]] int i = test_nodiscard();
constexpr bool flag = false;
if constexpr(flag)
return 1;
return 0;
}
"
DEAL_II_HAVE_CXX17_FEATURES)
# Some compilers treat lambdas as constexpr functions when compiling with
# C++17 support even if they don't fulfill all the constexpr function
# requirements. Consequently, these compilers don't allow try-blocks or
# non-literal return types in lambdas. This is a bug.
CHECK_CXX_SOURCE_COMPILES(
"
#include <string>
int main()
{
auto c = []()
{
return std::string{};
}();
(void) c;
return []()
{
try
{}
catch(...)
{}
return 0;
}();
}
"
DEAL_II_HAVE_CXX17_CONSTEXPR_LAMBDA_BUG_OK)
IF(DEAL_II_HAVE_CXX17_FEATURES AND
DEAL_II_HAVE_CXX17_CONSTEXPR_LAMBDA_BUG_OK)
MESSAGE(STATUS "C++17 support is enabled.")
SET(DEAL_II_HAVE_CXX17 TRUE)
ELSE()
MESSAGE(STATUS "C++17 support is disabled.")
SET(DEAL_II_HAVE_CXX17 FALSE)
ENDIF()
ENDMACRO()
#
# Wrap the following checks into a macro to make it easier to rerun them.
#
MACRO(_test_cxx14_support)
UNSET_IF_CHANGED(CHECK_CXX14_FEATURES_FLAGS_SAVED
"${CMAKE_REQUIRED_FLAGS}"
DEAL_II_HAVE_CXX14_FEATURES
DEAL_II_HAVE_CXX14_CLANGAUTODEBUG_BUG_OK
DEAL_II_HAVE_CXX11_FEATURES
DEAL_II_HAVE_CXX11_FUNCTIONAL_LLVMBUG20084_OK
)
# Check some generic C++14 features
CHECK_CXX_SOURCE_COMPILES(
"
#include <memory>
#include <algorithm>
// Check the version language macro, but skip MSVC because
// MSVC reports 199711 even in MSVC 2017.
#if __cplusplus < 201402L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
# error \"insufficient support for C++14\"
#endif
int main()
{
auto ptr = std::make_unique<int>(42);
constexpr int max = std::max(0, 1);
(void) ptr;
(void) max;
return 0;
}
"
DEAL_II_HAVE_CXX14_FEATURES)
# Clang-3.5* or older, bail out with a spurious error message in case
# of an undeduced auto return type.
#
# https://llvm.org/bugs/show_bug.cgi?id=16876
SET(_flags "${DEAL_II_CXX_FLAGS_DEBUG}")
STRIP_FLAG(_flags "-Wa,--compress-debug-sections")
ADD_FLAGS(CMAKE_REQUIRED_FLAGS "${_flags}")
CHECK_CXX_SOURCE_COMPILES(
"
struct foo
{
auto func();
};
int main()
{
foo bar;
(void) bar;
}
"
DEAL_II_HAVE_CXX14_CLANGAUTODEBUG_BUG_OK)
# Check some generic C++11 features
CHECK_CXX_SOURCE_COMPILES(
"
// common C++11 include files
#include <array>
#include <condition_variable>
#include <type_traits>
// thread_local storage specification
static thread_local std::array<int,3> p;
// Check the version language macro, but skip MSVC because
// MSVC reports 199711 even in MSVC 2017.
#if __cplusplus < 201103L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
# error \"insufficient support for C++11\"
#endif
int main()
{
std::condition_variable c;
p[0] = 1;
c.notify_all();
// type traits functionality
constexpr auto m0 = std::is_trivial<double>::value;
(void) m0;
constexpr auto m1 = std::is_standard_layout<double>::value;
(void) m1;
constexpr auto m2 = std::is_pod<double>::value;
(void) m2;
}
"
DEAL_II_HAVE_CXX11_FEATURES)
# clang libc++ bug, see https://llvm.org/bugs/show_bug.cgi?id=20084
CHECK_CXX_SOURCE_COMPILES(
"
#include <functional>
struct A { void foo() const {} };
int main() { A a; std::bind(&A::foo,a)(); return 0; }
"
DEAL_II_HAVE_CXX11_FUNCTIONAL_LLVMBUG20084_OK)
IF(DEAL_II_HAVE_CXX14_FEATURES AND
DEAL_II_HAVE_CXX14_CLANGAUTODEBUG_BUG_OK AND
DEAL_II_HAVE_CXX11_FEATURES AND
DEAL_II_HAVE_CXX11_FUNCTIONAL_LLVMBUG20084_OK)
MESSAGE(STATUS "C++14 support is enabled.")
SET(DEAL_II_HAVE_CXX14 TRUE)
ELSE()
MESSAGE(STATUS "C++14 support is disabled.")
SET(DEAL_II_HAVE_CXX14 FALSE)
ENDIF()
ENDMACRO()
#
# Try to find out what we support:
#
_set_up_cmake_required()
_test_cxx14_support()
IF(NOT DEAL_II_HAVE_CXX14)
MESSAGE(STATUS "C++14 support not available. Try to set -std=c++14 explicitly")
ENABLE_IF_SUPPORTED(DEAL_II_CXX_FLAGS "-std=c++14")
_set_up_cmake_required()
_test_cxx14_support()
ENDIF()
IF(NOT DEAL_II_HAVE_CXX14)
MESSAGE(FATAL_ERROR
"\nThe current version of deal.II requires a compiler with enabled "
"C++14 support. Make sure to use a modern enough compiler (GCC version "
"5 onwards, Clang version 4 onwards, or Microsoft MS VS 2015 onwards) "
"and check that the compiler flag \"-std=\" is either unset, or set to "
"at least c++14.\n\n"
)
ENDIF()
_test_cxx17_support()
_test_cxx20_support()
########################################################################
# #
# Check for various C++ features: #
# #
########################################################################
#
# Some compilers are too generous in accepting some of the language
# features that we test below and do not issue an error but a warning. Set
# -Werror to make the feature detection more reliable.
#
SET(_werror_flag "")
IF(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
ENABLE_IF_SUPPORTED(_werror_flag "/WX /EHsc")
ELSE()
ENABLE_IF_SUPPORTED(_werror_flag "-Werror")
ENABLE_IF_SUPPORTED(_werror_flag "-Wno-unused-command-line-argument")
ENDIF()
ADD_FLAGS(CMAKE_REQUIRED_FLAGS "${_werror_flag}")
UNSET_IF_CHANGED(CHECK_CXX_FEATURES_FLAGS_SAVED
"${CMAKE_REQUIRED_FLAGS}"
DEAL_II_HAVE_FP_EXCEPTIONS
DEAL_II_HAVE_COMPLEX_OPERATOR_OVERLOADS
DEAL_II_HAVE_CXX17_ATTRIBUTE_DEPRECATED
DEAL_II_HAVE_ATTRIBUTE_DEPRECATED
DEAL_II_HAVE_CXX17_ATTRIBUTE_FALLTHROUGH
DEAL_II_HAVE_ATTRIBUTE_FALLTHROUGH
DEAL_II_HAVE_CXX17_BESSEL_FUNCTIONS
DEAL_II_CXX14_CONSTEXPR_BUG_OK
)
#
# Check that we can use feenableexcept through the C++11 header file cfenv:
#
# The test is a bit more complicated because we also check that no garbage
# exception is thrown if we convert -std::numeric_limits<double>::max to a
# string. This sadly happens with some compiler support libraries :-(
#
# - Timo Heister, 2015
#
SET(_snippet
"
#include <cfenv>
#include <limits>
#include <sstream>
int main()
{
feenableexcept(FE_DIVBYZERO|FE_INVALID);
std::ostringstream description;
const double lower_bound = -std::numeric_limits<double>::max();
description << lower_bound;
return 0;
}
"
)
IF(DEAL_II_ALLOW_PLATFORM_INTROSPECTION)
CHECK_CXX_SOURCE_RUNS("${_snippet}" DEAL_II_HAVE_FP_EXCEPTIONS)
ELSE()
#
# If we are not allowed to do platform introspection, just test whether
# we can compile above code.
#
CHECK_CXX_SOURCE_COMPILES("${_snippet}" DEAL_II_HAVE_FP_EXCEPTIONS)
ENDIF()
#
# Check whether the standard library provides operator* overloads for mixed
# floating point multiplication of complex and real valued numbers.
#
# - Matthias Maier, 2015
#
CHECK_CXX_SOURCE_COMPILES(
"
#include <complex>
int main()
{
double() * std::complex<float>();
std::complex<float>() * double();
float() * std::complex<double>();
std::complex<double>() * float();
std::complex<double>() * std::complex<float>();
std::complex<float>() * std::complex<double>();
return 0;
}
"
DEAL_II_HAVE_COMPLEX_OPERATOR_OVERLOADS)
#
# Even though [[deprecated]] is a C++14 feature we have to check
# wether we can actually use the [[deprecated]] attribute in all
# cases we care about; some of the following are C++17 features.
#
CHECK_CXX_SOURCE_COMPILES(
"
[[deprecated]] int old_fn ();
int old_fn () { return 0; }
struct [[deprecated]] bob
{
[[deprecated]] bob(int i);
[[deprecated]] void test();
};
enum color
{
red [[deprecated]]
};
template <int dim>
struct foo {};
using bar [[deprecated]] = foo<2>;
int main () {}
"
DEAL_II_HAVE_CXX17_ATTRIBUTE_DEPRECATED
)
#
# Also test the corresponding GCC extension
#
CHECK_CXX_SOURCE_COMPILES(
"
__attribute__((deprecated)) int old_fn ();
int old_fn () { return 0; }
struct __attribute__((deprecated)) bob
{
__attribute__((deprecated)) bob(int i);
__attribute__((deprecated)) void test();
};
enum color
{
red __attribute__((deprecated))
};
template <int dim>
struct foo {};
using bar __attribute__((deprecated)) = foo<2>;
int main () {}
"
DEAL_II_HAVE_ATTRIBUTE_DEPRECATED
)
IF(DEAL_II_HAVE_CXX17_ATTRIBUTE_DEPRECATED)
SET(DEAL_II_DEPRECATED "[[deprecated]]")
ELSEIF(DEAL_II_HAVE_ATTRIBUTE_DEPRECATED AND NOT DEAL_II_WITH_CUDA)
SET(DEAL_II_DEPRECATED "__attribute__((deprecated))")
ELSE()
SET(DEAL_II_DEPRECATED " ")
ENDIF()
#
# Try to enable a fallthrough attribute. This is a language feature in C++17,
# but a compiler extension in earlier language versions.
#
CHECK_CXX_SOURCE_COMPILES(
"
int main()
{
int i = 42;
int j = 10;
switch(i)
{
case 1:
++j;
[[fallthrough]];
case 2:
++j;
[[fallthrough]];
default:
break;
}
}
"
DEAL_II_HAVE_CXX17_ATTRIBUTE_FALLTHROUGH
)
#
# see if the current compiler configuration supports the GCC extension
# __attribute__((fallthrough)) syntax instead
#
CHECK_CXX_SOURCE_COMPILES(
"
int main()
{
int i = 42;
int j = 10;
switch(i)
{
case 1:
++j;
__attribute__((fallthrough));
case 2:
++j;
__attribute__((fallthrough));
default:
break;
}
}
"
DEAL_II_HAVE_ATTRIBUTE_FALLTHROUGH
)
IF(DEAL_II_HAVE_CXX17_ATTRIBUTE_FALLTHROUGH)
SET(DEAL_II_FALLTHROUGH "[[fallthrough]]")
ELSEIF(DEAL_II_HAVE_ATTRIBUTE_FALLTHROUGH)
SET(DEAL_II_FALLTHROUGH "__attribute__((fallthrough))")
ELSE()
SET(DEAL_II_FALLTHROUGH " ")
ENDIF()
#
# Check for c++17 bessel function support. Unfortunately libc++ version 10
# does not have those.
#
CHECK_CXX_SOURCE_COMPILES(
"
#include <cmath>
using std::cyl_bessel_j;
using std::cyl_bessel_jf;
using std::cyl_bessel_jl;
int main()
{
}
"
DEAL_II_HAVE_CXX17_BESSEL_FUNCTIONS
)
#
# Check for correct c++14 constexpr support.
#
# As long as there exists an argument value such that an invocation of the
# function or constructor could be an evaluated subexpression of a core constant
# expression, C++14 allows to call non-constexpr functions from constexpr
# functions.
#
# Unfortunately, not all compilers obey the standard in this regard. In some
# cases, MSVC 2019 crashes with an internal compiler error when we
# declare the respective functions as 'constexpr' even though the test below
# passes, see #9080.
#
# We only run this check if we have CXX14 support, otherwise the use of constexpr
# is limited (non-const constexpr functions for example).
#
# MSVC has considerable problems with "constexpr", disable unconditionally
# for now
IF(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
SET(DEAL_II_CXX14_CONSTEXPR_BUG true)
ELSE()
CHECK_CXX_COMPILER_BUG(
"
#define Assert(x,y) if (!(x)) throw y;
void bar()
{}
constexpr int
foo(const int n)
{
Assert(n>0, \"hello\");
if(!(n >= 0))
bar();
return n;
}
int main()
{
constexpr unsigned int n=foo(1);
return n;
}
"
DEAL_II_CXX14_CONSTEXPR_BUG)
ENDIF()
SET(DEAL_II_CONSTEXPR "constexpr")
IF(DEAL_II_CXX14_CONSTEXPR_BUG)
SET(DEAL_II_CONSTEXPR " ")
ENDIF()
RESET_CMAKE_REQUIRED()