Update cmake-compile-features(7) manual after deprecation of WriteCompilerDetectionHeader

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 the COMPILE_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 like CMAKE_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 to write_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).

Thanks @petwu, I’ve already updated the relevant parts in the upcoming 9th edition of the book.

@brad.king Since you were the most keen on the removal of the WriteCompilerDetectionHeader module, I’m assuming you had already formed a view on how projects should be updated to no longer use it. Not sure how you want to update the docs to reflect your alternative methods.

I’ve opened CMake MR 6037 to update the cmake-compile-features(7) manual to account for the removal of WCDH.

The policy CMP0120 documentation suggests alternatives to WCDH. The simplest solution, at least in the short term, is to run WCDH once by hand locally and then bundle the generated header with the project source.