Skip to content

[CMake] Rewrite C++ standard detection

This is a complete rewrite of the existing C++ standard version test that tries the various -std=c++xy flags and tries to figure out which version of the standard to enable.

The existing version has a few problems:

  • it is not really extensible to newer standards, those will just lead to a deeper and deeper nesting of if statements.

  • More importantly, there is no way to say: I want C++11, but not C++14 (or, in the future, I want C++14, but not C++17). Being able to do so is important for testing and compatibility reason.

  • There is no easy way for downstream modules to require a newer version of the standard than the core modules.

Test logic

The new version of the test separates the logic from the data (versions, flag names, compiler test sources) and just iterates over lists of that data, in descending order to make sure the newest available standard gets picked. The numerical value of that standard is stored in the variable CXX_MAX_SUPPORTED_STANDARD and the test stops. If the test fails to find a working combination, it issues a warning to the user and only records support for C++03.

The test can be influenced by two CMake variables:

  • DISABLE_CXX_VERSION_CHECK already existed in the old version of the test. It completely disables all testing and requires the user to manually set CXX_MAX_SUPPORTED_STANDARD to the correct value. Moreover, any required compiler command line switches have to be manually added to CMAKE_CXX_FLAGS.

  • CXX_MAX_STANDARD defines the maximum version of the standard that the build system will try to enable. With this new switch, it becomes possible to limit the compiler to an older version of the standard than what it could theoretically support. For now, this defaults to C++14.

Requirements check with a new CMake function

In order to allow module authors to easily state their minimum version requirements, there is a new CMake function dune_require_cxx_standard() that any Dune module can call to require support for at least a given C++ standard:

dune_require_cxx_standard(MODULE "dune-functions" VERSION 14)

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...

Backporting

I would very much like to backport this to 2.4.1 as it would get rid of the dirty hackery currently required in dune-functions and PDELab. For the backported version, I'm not sure about the best value for CXX_MAX_STANDARD: 11 or 14?

This fixes #16 (closed), and a backport to 2.4 would also be a fix for #15 (closed).

Merge request reports