Hi, I noticed that in the recently released CMake v3.20 the WriteCompilerDetectionHeader module is now deprecated, but the cmake-compile-features(7) manual continues to recommend it for achieving feature depended implementations (section Optional Compile Features). The write_compiler_detection_header()
command is used to generate a header with preprocessor defines that can be used for detecting features in the code.
Is suggest to remove the parts using write_compiler_detection_header()
or at least warning about the deprecation and suggesting some alternative solutions instead. Those are the ones I could think about:
-
Generator Expressions
With theCOMPILE_FEATURES
generator expression the preprocessor define can easily be set manually:target_compile_definitions(dummy PRIVATE $<$<COMPILE_FEATURES:cxx_variadic_templates>:SUPPORTS_VARIADIC_TEMPLATES> )
This also has the advantage that you don’t have to include some generate header in your code.
-
CMAKE_<LANG>_COMPILE_FEATURES
An alternative are variables likeCMAKE_CXX_COMPILE_FEATURES
, since they contain a list of all features the compiler supports.if(cxx_variadic_templates IN_LIST CMAKE_CXX_COMPILE_FEATURES) target_sources(dummy PRIVATE impl_with_variadic_templates.cpp) else() target_sources(dummy PRIVATE impl_without_variadic_templates.cpp) endif()
They may also be used wherever generator expressions are not available. One might even use it to set some variables and then use
configure_file()
to generate your own header with preprocessor variables to achieve something similar towrite_compiler_detection_header()
.
@craig.scott, the same would apply to your book Professional CMake: A Practical Guide (section 16.2.1 of the 8th edition).