Compatibility
This library provides an advanced target_compile_features() and write_compiler_detection_header().
The problem with those is that they are controlled via a CMake internal database, which has to be kept manually up-to-date.
This version uses check_cxx_source_compiles() instead and is such automatically up-to-date - a compiler supports a feature,
if the test code compiles.
Based on the test results, a header file is generated that stores the result and often workaround code,
for example a CONSTEXPR macro that is constexpr, if supported, and const otherwise,
to use the features anyway.
It also provides the automatic standard deduction from target_compile_features() to activate the right standard.
Example
If you only want the C++ standard activation, simply include() comp_base.cmake and replace target_compile_features() by comp_compile_features().
This is a CMakeLists.txt for a more advanced usage:
# suppose we have a target 'tgt'
include(your/dir/to/comp_base.cmake) # only file you need to download, rest is taken as needed
# we want constexpr, noexcept, std::max_align_t and rtti_support
# instead of cpp11_lang/constexpr and cpp11_lang/noexcept, you could also write cxx_constexpr/cxx_noexcept
comp_target_features(tgt PUBLIC cpp11_lang/constexpr cpp11_lang/noexcept cpp11_lib/max_align_t env/rtti_support)
For convenience we include all generated files in a header named config.hpp.
The filenames are made available through macros.
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include COMP_CONSTEXPR_HEADER
#include COMP_NOEXCEPT_HEADER
#include COMP_MAX_ALIGN_T_HEADER
#include COMP_RTTI_SUPPORT_HEADER
#endif
And then we can use it just by including config.hpp in our code:
#include "config.hpp"
// use a workaround macro
COMP_CONSTEXPR int i = 0;
void func() COMP_NOEXCEPT
{
// use a workaround typedef
comp::max_align_t foo;
// or conditional compilation
#if COMP_HAS_RTTI_SUPPORT
do_sth();
#endif
}
If you don't care about the workarounds, but just want a specific standard, simply call:
comp_target_features(tgt PUBLIC CPP11) # or CPP14 or CPP17
This will only activate C++11/14/17 without doing anything else.
Note: The standard activation is always PRIVATE to allow users of a library to have a different (higher) standard than the library.
Usage
The only file needed is comp_base.cmake.
You can either manually download it, use CMakes file(DOWNLOAD facility,
or use git submodules.
The branch git-submodule only contains comp_base.cmake and is thus perfect for this purpose.
Run git submodule add -b "git-submodule" https://github.com/foonathan/compatibility.git to initialize it and fetch the file.
Then you only need to run git submodule update --remote to update it to the newest version.
include() it in your CMakeLists.txt now.
First it generates CMake options - COMP_CPP11_FLAG, COMP_CPP14_FLAG and COMP_CPP17_FLAG - storing the calculated compiler flag for a given standard,
useful if you want to override it, if it can't find one for your compiler.
It also provides the following function:
comp_target_features(<target> <PRIVATE|PUBLIC|INTERFACE> <features...>
[NOPREFIX | PREFIX <prefix] [NAMESPACE <namespace>]
[CMAKE_PATH <path>] [INCLUDE_PATH <include_path>]
[NOFLAGS | CPP11 | CPP14 | CPP17]
[LOG <DEBUG | INFO | QUIET | SILENT | ALL>]
[NO_HEADER_MACROS] [SINGLE_HEADER <name>])
Ignoring all the other options, it is like target_compile_features().
It takes a list of features to activate for a certain target.
A features is a file in this repository without the .cmake extension, e.g. cpp11_lang for all C++11 language features,
or cpp14_lang/deprecated for the C++14 deprecated features.
A feature file with name dir/xxx.cmake belonging to a feature dir/xxx consists of the following,
where dir is the category of the feature, xxx the feature name in lowercase, XXX the feature name in upper case <PREFIX> the prefix as given to the function, <prefix> the prefix in lowercase and without a trailing underscore:
a test testing whether or not the compiler supports this feature
a CMake option with name COMP_HAS_XXX to override its result, useful if you want to act like it doesn't support a feature,
or if the test is poorly written (please contact me in this case!)
a header named <prefix>/xxx.hpp.
The header contains at least a macro <PREFIX>HAS_XXX with the same value as the CMake option
and often workaround macros or functions that can be used instead of the feature.
The workaround uses either the feature, if it is available, or own code.
This allows using many new features already, without support.
If the compiler gets support, you will be automatically using the native feature or the standard library implementation.
a globally available macro named <PREFIX>_XXX_HEADER to include the header file.
To use the generated header files, simply write #include <PREFIX>_XXX_HEADER inside your code, the macro is made available automatically (you could also #incluce the file directly but this is not recommended).
What comp_target_features function actually does is the following:
For each feature, it downloads the latest version of the test file from Github, if it doesn't exist yet.
The remote url can be set via the option COMP_REMOTE_URL.
For each feature, it calls include(feature.cmake) after some set up. This runs the test and generates the header file.
It calls target_include_directories to allow including the generated header files
and target_compile_definitions for the file macro.
The INTERFACE/PUBLIC/PRIVATE specifier are only used in these calls.
Activates the right C++ standard. E.g., if a feature requires C++11, it will be activated if it is available. If it is not, it will only activate the C++ standard required by the workaround code. This activation is always PRIVATE. The flag is exposed through the CACHE variable ${target}_COMP_COMPILE_OPTIONS.
The behavior can be customized with the other options:
NOPREFIX/PREFIX: The prefix of any generated macros or none if NOPREFIX is set. Default is COMP_.
NAMESPACE: The namespace name of any generated code, default is comp. A given prefix must always use the same namespace name!
CMAKE_PATH/INCLUDE_PATH: The download destination for the CMake files/the destination of the generated headers,
default is ${CMAKE_BINARY_DIR}/comp.downloaded for cmake and ${CMAKE_BINARY_DIR}/comp.generated for the headers.
INCLUDE_PATH is also given to target_include_directories(), but note that the generated headers are in a subfolder <prefix> (this cannot be changed).
NOFLAGS/CPP11/CPP14/CPP17: Override for the standard detection, if you want to have a newer standard than deduced from the features,
or a lower (not recommended). They have priority over the deduction, C++17 over C++14 over C++11.
Specify NOFLAGS if you do not want to have any compiler flags set.
The latter is useful for INTERFACE libraries which are only there to run the tests and generate the options and headers.
LOG: Turn down or up the logging for features. There is no default value, but it is fairly verbose by default.
- Possible options:
DEBUG, INFO, QUIET, SILENT, ALL
- Additionally, setting
COMP_LOG_LEVEL at the global level behaves the same way, except any option passed here will override for the current run
NO_HEADER_MACROS: Disables the definition of the macros that contain the header file location.
SINGLE_HEADER: Generate a single header file with that name that will include all generated headers.
Feature Reference
A feature named dir/xxx is tested in xxx.cmake, defines an override CMake option COMP_HAS_XXX and a macro <PREFIX>HAS_XXX in a file named <prefix>/xxx.hpp (where prefix is <PREFIX> in lowercase without a trailing underscore), filename also made available over the global macro <PREFIX>XXX_HEADER.
There are also alternative names for the CMake target_compile_features() and SD-6 Feature Test Recommondations that are automatically translated.
Where appropriate, it will also generate the SD-6 feature macro as specified.
This will override the existing value if the new one is greater or the macro COMP_OVERRIDE_SD6 is defined.
If a feature is not supported, it will not change or define anything.
For some features, macros are generated that can be used instead (i.e. for noexcept), they have the form <PREFIX>XXX.
Those macros often use compiler extensions.
If there is none (or a lacking implementation...), an error message will be emmitted.
To prevent this, simply define the macro as no-op or as you want prior to including the file.
There are often workaround functions for library features. Those are defined in a namespace and either use the own implementation or the standard library implementation, if it is available.
Prefix and namespace name can be controlled via parameters, see above.
A given prefix must always use the same namespace name on each call.
This library currently tests for the following features.
The code below assumes no prefix and a namespace name of comp.
*A feature will only be included (by me) if it is not a pure syntactic feature (like auto or lambdas which can be avoided) but if there is either sensible workaround code, e.g. through to compiler extensions or through reimplementing (small!) standard library functionality, or there can be conditional compilation based on the existense, e.g. optional literal definitions or move constructors.
Note that I'm open to PRs of any kind :)*
C++11 language features
These features are all in the subdirectory cpp11_lang.
| feature name |
alternative name |
example |
workaround, if any |
| alias_template |
cxx_alias_templas |
template <typename T> using my_map = std::map<int, T>; |
no workaround |
| alignas |
cxx_alignas |
alignas(int) char c |
ALIGNAS(x), fallback to compiler extension, if available |
| alignof |
cxx_alignof |
alignof(int) |
ALIGNOF(x), fallback to compiler extension, if available |
| auto_type |
cxx_auto_type |
auto var = 0 |
no workaround |
| constexpr |
cxx_constexpr |
constexpr int foo() |
CONSTEXPR, fallback to const; CONSTEXPR_FNC, fallback to inline |
| decltype |
cxx_decltype |
decltype(a) |
DECLTYPE(x), fallback to typeof extension, if available |
| default_function_template_args |
cxx_default_function_template_args |
template <typename T = int> void foo(); |
no workaround |
| delete_fnc |
cxx_deleted_functions |
void foo() = delete; |
no workaround |
| enum_class |
cxx_strong_enum |
enum class foo {} |
no workaround |
| explicit_conversion_op |
cxx_explicit_conversion |
explicit operator bool() |
no workaround |
| final |
cxx_final |
void bar() final; |
FINAL macro, workaround expands to nothing |
| inline_namespace |
none |
inline namespace foo {...} |
no workaround |
| lambdas |
cxx_lambdas |
[](){} |
no workaround |
| literal_op |
cxx_user_literals |
operator"" |
no workaround |
| noexcept |
cxx_noexcept |
void foo() noexcept; |
NOEXCEPT, fallback to nothing; NOEXCEPT_IF(x), fallback to nothing; NOEXCEPT_OP(x), fallback to false |
| noreturn |
none |
[[noreturn]] void foo(); |
NORETURN, fallback to compiler extension, if available |
| nullptr |
cxx_nullptr |
void* ptr = nullptr; |
NULLPTR, fallback to null pointer idiom; also comp::nullptr_t |
| override |
cxx_override |
void bar() override; |
OVERRIDE, fallback to nothing |
| parameter_pack |
cxx_parameter_pack |
template <typename ... T> void f(T... ts); |
no workaround |
| range_for |
cxx_range_for |
for (auto var : container) |
no workaround |
| right_angle_brackets |
cxx_right_angle_brackets |
std::vector<std::vector<int>> |
no workaround |
| rvalue_ref |
cxx_rvalue_references |
int&& a = 4; |
no workaround |
| static_assert |
cxx_static_assert |
static_assert(std::is_integral<T>::value, ""); |
STATIC_ASSERT(Expr, Msg), fallback to simple undefined struct technique |
| thread_local |
cxx_thread_local |
thread_local int i; |
THREAD_LOCAL, fallback to __thread extension or similar, if available - does not call constructors or destructors! |