From 09a3fdec9b0a51a985f74c7dcab5a0f993019859 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Mon, 5 May 2025 20:28:03 +1000 Subject: [PATCH] Add CODEGEN keyword to add_custom_command() if supported If using CMake 3.31 or later, add_custom_command() supports a CODEGEN keyword. It requires policy CMP0171 to be set to NEW to be recognized. Add the CODEGEN keyword to these calls if we can so that consumers can build the `codegen` CMake target to ensure code generation has been performed without having to actually compile the generated code. This is useful for linting CI jobs like clang-tidy. --- CMakeRC.cmake | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CMakeRC.cmake b/CMakeRC.cmake index 6a5147a..1167281 100644 --- a/CMakeRC.cmake +++ b/CMakeRC.cmake @@ -37,6 +37,11 @@ set(_version 2.0.0) cmake_minimum_required(VERSION 3.12) include(CMakeParseArguments) +# Enable use of CODEGEN keyword in add_custom_command() calls if available +if(POLICY CMP0171) + cmake_policy(SET CMP0171 NEW) +endif() + if(COMMAND cmrc_add_resource_library) if(NOT DEFINED _CMRC_VERSION OR NOT (_version STREQUAL _CMRC_VERSION)) message(WARNING "More than one CMakeRC version has been included in this project.") @@ -487,10 +492,16 @@ function(cmrc_add_resource_library name) string(REPLACE "\n " "\n" cpp_content "${cpp_content}") file(GENERATE OUTPUT "${lib_tmp_cpp}" CONTENT "${cpp_content}") get_filename_component(libcpp "${libdir}/lib.cpp" ABSOLUTE) + if(POLICY CMP0171) + set(maybe_CODEGEN CODEGEN) + else() + set(maybe_CODEGEN "") + endif() add_custom_command(OUTPUT "${libcpp}" DEPENDS "${lib_tmp_cpp}" "${cmrc_hpp}" COMMAND ${CMAKE_COMMAND} -E copy_if_different "${lib_tmp_cpp}" "${libcpp}" COMMENT "Generating ${name} resource loader" + ${maybe_CODEGEN} ) # Generate the actual static library. Each source file is just a single file # with a character array compiled in containing the contents of the @@ -619,6 +630,11 @@ function(cmrc_add_resources name) endfunction() function(_cmrc_generate_intermediate_cpp lib_ns symbol outfile infile) + if(POLICY CMP0171) + set(maybe_CODEGEN CODEGEN) + else() + set(maybe_CODEGEN "") + endif() add_custom_command( # This is the file we will generate OUTPUT "${outfile}" @@ -633,6 +649,7 @@ function(_cmrc_generate_intermediate_cpp lib_ns symbol outfile infile) "-DOUTPUT_FILE=${outfile}" -P "${_CMRC_SCRIPT}" COMMENT "Generating intermediate file for ${infile}" + ${maybe_CODEGEN} ) endfunction()