Commit c5e82ed2 authored by Steffen Müthing's avatar Steffen Müthing

[CMake] Add function to allow modules to require a minimum C++ standard

This patch adds a new function dune_require_cxx_standard() that any Dune
module can call to require support for at least a given C++
standard. If the compiler doesn't meet the requirements, the function
will report the required and the actually available standard versions
and abort with a fatal error.

Moreover, it knows about CXX_MAX_STANDARD and will tell the user if the
value of that variable is below the requirements. This avoids desperate
users that have a shiny beta of GCC 6 with C++17 support wondering why
their own module using shiny C++17 concepts stubbornly fails to
build... ;-)
parent daa2e9f4
......@@ -92,11 +92,36 @@ set(CXX_VERSIONS_FLAGS "17\;1z" "14\;1y" "11\;1x")
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)
......@@ -162,11 +187,8 @@ CMAKE_CXX_FLAGS."
endif()
# make sure we have at least C++11
if(CXX_MAX_SUPPORTED_STANDARD LESS 11)
message(FATAL_ERROR "Your compiler does not seem to support C++11. If it does, please add any required flags to your CMAKE_CXX_FLAGS.")
endif()
dune_require_cxx_standard(MODULE "DUNE" VERSION 11)
# perform tests
......
  • Probably you want to use the stuff CMake already provides, at least in the case CMake is recent enough. Since CMake 3.1 CMAKE_CXX_STANDARD can be set to 98, 11 or 14 and it will error out if the required version is not met. CMake has a list for a lot of compilers with the right flags.
    Documentation: https://cmake.org/cmake/help/v3.4/prop_tgt/CXX_STANDARD.html

  • Unfortunately, that isn't enough for some of our use cases. We have some weird situations that CMake doesn't typically assume, like the issue that first cropped up when you added C++14 detection support and Marian's build broke because he had installed a recent Clang (which had C++14 support) into his home directory that was using the libstdc++ from his system (which didn't). Kaboom!

    So we really need to make sure that the compiler is paired with a matching library, because there are quite a few people with weird compiler setups to work around their old Linux distribution etc. And AFAIK CMake just uses a pre-built table for this which won't catch problems with those oddball configurations.

    PS: And I want to be able to specify C++17 😉

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment