/usr/share/dune/cmake/modules/CheckCXX11Features.cmake is in libdune-common-dev 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Module that checks for supported C++11 (former C++0x) features.
#
# Sets the follwing variables:
#
# HAVE_NULLPTR True if nullptr is available
# HAS_ATTRIBUTE_UNUSED True if attribute unused is supported
# HAS_ATTRIBUTE_DEPRECATED True if attribute deprecated is supported
# HAS_ATTRIBUTE_DEPRECATED_MSG True if attribute deprecated("msg") is supported
# HAVE_CONSTEXPR True if constexpr is supported
# HAVE_KEYWORD_FINAL True if final is supported.
# HAVE_RANGE_BASED_FOR True if range-based for is supported and working.
# HAVE_NOEXCEPT_SPECIFIER True if nonexcept specifier is supported.
include(CMakePushCheckState)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
# C++ standard versions that this test knows about
set(CXX_VERSIONS 17 14 11)
# Compile tests for the different standard revisions; these test both the compiler
# and the associated library to avoid problems like using a C++14 user-installed
# compiler together with a non C++14-compliant stdlib from the system compiler.
# we need to escape semicolons in the tests to be able to stick them into a list
string(REPLACE ";" "\;" cxx_17_test
"
#include <type_traits>
// nested namespaces are a C++17 compiler feature
namespace A::B {
using T = int;
}
int main() {
// std::void_t is a C++17 library feature
return not std::is_same<void,std::void_t<A::B::T> >{};
}
")
string(REPLACE ";" "\;" cxx_14_test
"
#include <memory>
int main() {
// lambdas with auto parameters are C++14 - so this checks the compiler
auto l = [](auto x) { return x; };
// std::make_unique() is a C++14 library feature - this checks whether the
// compiler uses a C++14 compliant library.
auto v = std::make_unique<int>(l(0));
return *v;
}
")
string(REPLACE ";" "\;" cxx_11_test
"
#include <memory>
int main() {
// this checks both the compiler (by using auto) and the library (by using
// std::make_shared() for C++11 compliance at GCC 4.4 level.
auto v = std::make_shared<int>(0);
return *v;
}
")
# build a list out of the pre-escaped tests
set(CXX_VERSIONS_TEST "${cxx_17_test}" "${cxx_14_test}" "${cxx_11_test}")
# these are appended to "-std=c++" and tried in this order
# note the escaped semicolons; that's necessary to create a nested list
set(CXX_VERSIONS_FLAGS "17\;1z" "14\;1y" "11\;0x")
# by default, we enable C++14 for now, but not C++17
# The user can override this choice by explicitly setting this variable
set(CXX_MAX_STANDARD 14 CACHE STRING "highest version of the C++ standard to enable")
function(dune_require_cxx_standard)
include(CMakeParseArguments)
cmake_parse_arguments("" "" "MODULE;VERSION" "" ${ARGN})
if(_UNPARSED_ARGUMENTS)
message(WARNING "Unknown arguments in call to dune_require_cxx_standard(${ARGN})")
endif()
if(${_VERSION} GREATER ${CXX_MAX_SUPPORTED_STANDARD})
if(NOT _MODULE)
set(_MODULE "This module")
endif()
if(${_VERSION} GREATER ${CXX_MAX_STANDARD})
message(FATAL_ERROR "\
${_MODULE} requires compiler support for C++${_VERSION}, but the build system is currently \
set up to not allow newer language standards than C++${CXX_MAX_STANDARD}. Try setting the \
CMake variable CXX_MAX_STANDARD to at least ${_VERSION}."
)
else()
message(FATAL_ERROR "
${_MODULE} requires compiler support for C++${_VERSION}, but your compiler only supports \
C++${CXX_MAX_SUPPORTED_STANDARD}."
)
endif()
endif()
endfunction()
# try to enable all of the C++ standards that we know about, in descending order
if(NOT DISABLE_CXX_VERSION_CHECK)
foreach(version ${CXX_VERSIONS})
# skip versions that are newer than allowed
if(NOT(version GREATER CXX_MAX_STANDARD))
list(FIND CXX_VERSIONS ${version} version_index)
list(GET CXX_VERSIONS_FLAGS ${version_index} version_flags)
# First try whether the compiler accepts one of the command line flags for this standard
foreach(flag ${version_flags})
set(cxx_std_flag_works "cxx_std_flag_${flag}")
check_cxx_compiler_flag("-std=c++${flag}" ${cxx_std_flag_works})
if(${cxx_std_flag_works})
set(cxx_std_flag "-std=c++${flag}")
break()
endif()
endforeach()
# and if it did, run the compile test
if(cxx_std_flag)
list(GET CXX_VERSIONS_TEST ${version_index} version_test)
set(test_compiler_output "compiler_supports_cxx${version}")
cmake_push_check_state()
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${cxx_std_flag}")
check_cxx_source_compiles("${version_test}" ${test_compiler_output})
cmake_pop_check_state()
if(${test_compiler_output})
set(CXX_MAX_SUPPORTED_STANDARD ${version})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${cxx_std_flag} ")
break()
else()
# Wipe the variable, as this version of the standard doesn't seem to work
unset(cxx_std_flag)
endif()
endif()
endif()
endforeach()
if(NOT DEFINED CXX_MAX_SUPPORTED_STANDARD)
# Let's just assume every compiler at least claims C++03 compliance by now
message(WARNING "\
Unable to determine C++ standard support for your compiler, falling back to C++03. \
If you know that your compiler supports a newer version of the standard, please set the CMake \
variable DISABLE_CXX_VERSION_CHECK to true and the CMake variable CXX_MAX_SUPPORTED_STANDARD \
to the highest version of the standard supported by your compiler (e.g. 14). If your compiler \
needs custom flags to switch to that standard version, you have to manually add them to \
CMAKE_CXX_FLAGS."
)
set(CXX_MAX_SUPPORTED_STANDARD 3)
endif()
endif()
# make sure we have at least C++11
dune_require_cxx_standard(MODULE "DUNE" VERSION 11)
# perform tests
# nullptr
check_cxx_source_compiles("
int main(void)
{
char* ch = nullptr;
return 0;
}
" HAVE_NULLPTR
)
if(HAVE_NULLPTR)
check_cxx_source_compiles("
#include <cstddef>
int main(void)
{
std::nullptr_t npt = nullptr;
return 0;
}
" HAVE_NULLPTR_T
)
if (NOT HAVE_NULLPTR_T)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
message(FATAL_ERROR "Your compiler supports the 'nullptr' keyword, but not the type std::nullptr_t.
You are using an Intel compiler, where this error is typically caused by an outdated underlying system GCC.
Check if 'gcc --version' gives you at least GCC 4.6, otherwise please install a newer GCC and point your Intel compiler at it using the '-gcc-name' or '-gxx-name' options (see 'man icc' for further details)."
)
else()
message(FATAL_ERROR "Your compiler supports the 'nullptr' keyword, but not the type std::nullptr_t.
THIS SHOULD NOT HAPPEN FOR YOUR COMPILER!
Please submit a bug at https://dune-project.org/flyspray/"
)
endif()
endif()
endif()
# __attribute__((unused))
check_cxx_source_compiles("
int main(void)
{
int __attribute__((unused)) foo;
return 0;
};
" HAS_ATTRIBUTE_UNUSED
)
# __attribute__((deprecated))
check_cxx_source_compiles("
#define DEP __attribute__((deprecated))
class bar
{
bar() DEP;
};
class peng { } DEP;
template <class T>
class t_bar
{
t_bar() DEP;
};
template <class T>
class t_peng {
t_peng() {};
} DEP;
void foo() DEP;
void foo() {}
int main(void)
{
return 0;
};
" HAS_ATTRIBUTE_DEPRECATED
)
# __attribute__((deprecated("msg")))
check_cxx_source_compiles("
#define DEP __attribute__((deprecated(\"message\")))
class bar {
bar() DEP;
};
class peng { } DEP;
template <class T>
class t_bar
{
t_bar() DEP;
};
template <class T>
class t_peng
{
t_peng() {};
} DEP;
void foo() DEP;
void foo() {}
int main(void)
{
return 0;
};
" HAS_ATTRIBUTE_DEPRECATED_MSG
)
# constexpr
check_cxx_source_compiles("
constexpr int foo()
{ return 0; }
template<int v>
struct A
{
static const int value = v;
};
int main(void)
{
return A<foo()>::value;
}
" HAVE_CONSTEXPR
)
# keyword final
check_cxx_source_compiles("
struct Foo
{
virtual void foo() final;
};
int main(void)
{
return 0;
}
" HAVE_KEYWORD_FINAL
)
# range-based for
check_cxx_source_compiles("
int main(void)
{
int arr[3];
for(int &val : arr)
val = 0;
}
" HAVE_RANGE_BASED_FOR
)
# nonexcept specifier
check_cxx_source_compiles("
void func1() noexcept {}
void func2() noexcept(true) {}
template <class T>
void func3() noexcept(noexcept(T())) {}
int main(void)
{
func1();
func2();
func3<int>();
}
" HAVE_NOEXCEPT_SPECIFIER
)
# std::declval()
check_cxx_source_compiles("
#include <utility>
template<typename T>
struct check;
template<>
struct check<int&&>
{
int pass() { return 0; }
};
int main(void)
{
return check<decltype(std::declval<int>())>().pass();
}
" HAVE_STD_DECLVAL
)
# full support for is_indexable (checking whether a type supports operator[])
check_cxx_source_compiles("
#include <utility>
#include <type_traits>
#include <array>
template <class T>
typename std::add_rvalue_reference<T>::type declval();
namespace detail {
template<typename T, typename I, typename = int>
struct _is_indexable
: public std::false_type
{};
template<typename T, typename I>
struct _is_indexable<T,I,typename std::enable_if<(sizeof(declval<T>()[declval<I>()]) > 0),int>::type>
: public std::true_type
{};
}
template<typename T, typename I = std::size_t>
struct is_indexable
: public detail::_is_indexable<T,I>
{};
struct foo_type {};
int main()
{
double x;
std::array<double,4> y;
double z[5];
foo_type f;
static_assert(not is_indexable<decltype(x)>::value,\"scalar type\");
static_assert(is_indexable<decltype(y)>::value,\"indexable class\");
static_assert(is_indexable<decltype(z)>::value,\"array\");
static_assert(not is_indexable<decltype(f)>::value,\"not indexable class\");
static_assert(not is_indexable<decltype(y),foo_type>::value,\"custom index type\");
return 0;
}
" HAVE_IS_INDEXABLE_SUPPORT
)
# find the threading library
# Use a copy FindThreads from CMake 3.1 due to its support of pthread
if(NOT DEFINED THREADS_PREFER_PTHREAD_FLAG)
set(THREADS_PREFER_PTHREAD_FLAG 1)
endif()
if(${CMAKE_VERSION} VERSION_LESS "3.1")
find_package(ThreadsCMake31)
else()
find_package(Threads)
endif()
# see whether threading needs -no-as-needed
if(EXISTS /etc/dpkg/origins/ubuntu)
set(NO_AS_NEEDED "-Wl,-no-as-needed ")
else(EXISTS /etc/dpkg/origins/ubuntu)
set(NO_AS_NEEDED "")
endif(EXISTS /etc/dpkg/origins/ubuntu)
set(STDTHREAD_LINK_FLAGS "${NO_AS_NEEDED}${CMAKE_THREAD_LIBS_INIT}"
CACHE STRING "Linker flags needed to get working C++11 threads support. On Ubuntu it may be necessary to include -Wl,-no-as-needed (see FS#1650).")
# set linker flags
#
# in all implementations I know it is sufficient to set the linker flags when
# linking the final executable, so this should work. In cmake, this appears
# to only work when building the project however, not for later config tests
# (contrary to CMAKE_CXX_FLAGS). Luckily, later tests don't seem to use any
# threading... (except for our own sanity check)
if(NOT STDTHREAD_LINK_FLAGS STREQUAL "")
#set(vars CMAKE_EXE_LINKER_FLAGS ${CMAKE_CONFIGURATION_TYPES})
# CMAKE_CONFIGURATION_TYPES seems to be empty. Use the configurations from
# adding -std=c++11 above instead.
set(vars CMAKE_EXE_LINKER_FLAGS DEBUG MINSIZEREL RELEASE RELWITHDEBINFO)
string(REPLACE ";" ";CMAKE_EXE_LINKER_FLAGS_" vars "${vars}")
string(TOUPPER "${vars}" vars)
foreach(var ${vars})
if(NOT var STREQUAL "")
set(${var} "${${var}} ${STDTHREAD_LINK_FLAGS}")
endif()
endforeach(var ${vars})
endif(NOT STDTHREAD_LINK_FLAGS STREQUAL "")
include(CheckCXXSourceRuns)
# check that the found configuration works
if(CMAKE_CROSSCOMPILING)
message(WARNING "Crosscompiling, cannot run test program to see whether "
"std::thread works. Assuming that the found configuration does indeed "
"work.")
endif(CMAKE_CROSSCOMPILING)
if(NOT DEFINED STDTHREAD_WORKS)
if(NOT CMAKE_CROSSCOMPILING)
# The value is not in the cache, so run check
cmake_push_check_state()
# tests seem to ignore CMAKE_EXE_LINKER_FLAGS
set(CMAKE_REQUIRED_LIBRARIES "${STDTHREAD_LINK_FLAGS} ${CMAKE_REQUIRED_LIBRARIES}")
check_cxx_source_runs("
#include <thread>
void dummy() {}
int main() {
std::thread t(dummy);
t.join();
}
" STDTHREAD_WORKS)
cmake_pop_check_state()
endif(NOT CMAKE_CROSSCOMPILING)
# put the found value into the cache. Put it there even if we're
# cross-compiling, so the user can find it. Use FORCE:
# check_cxx_source_runs() already puts the value in the cache but without
# documentation; also the "if(NOT DEFINED STDTHREAD_WORKS)" will prevent us
# from overwriting a value set by the user.
set(STDTHREAD_WORKS "${STDTHREAD_WORKS}"
CACHE BOOL "Whether std::thread works." FORCE)
endif(NOT DEFINED STDTHREAD_WORKS)
if(NOT STDTHREAD_WORKS)
# Working C++11 threading support is required for dune. In particular to
# make things like lazyly initialized caches thread safe
# (e.g. QuadratureRules::rule(), which needs std::call_once()). If we don't
# include the correct options during linking, there will be very funny
# errors at runtime, ranging from segfaults to
#
# terminate called after throwing an instance of 'std::system_error'
# what(): Unknown error 18446744073709551615
message(FATAL_ERROR "Your system does not seem to have a working "
"implementation of std::thread. If it does, please set the linker flags "
"required to get std::thread working in the cache variable "
"STDTHREAD_LINK_FLAGS. If you think this test is wrong, set the cache "
"variable STDTHREAD_WORKS.")
endif(NOT STDTHREAD_WORKS)
|