From 0122e0229309ea9e726f399cbea1038a386e5f63 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 1 May 2024 17:31:35 -0400 Subject: [PATCH] Ninja: Add COMMENT to custom command and target descriptions Fixes: #15461 --- Source/cmNinjaUtilityTargetGenerator.cxx | 13 +++++++++++++ .../Ninja/CustomCommandTargetComments.cmake | 12 ++++++++++++ Tests/RunCMake/Ninja/RunCMakeTest.cmake | 15 +++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 Tests/RunCMake/Ninja/CustomCommandTargetComments.cmake diff --git a/Source/cmNinjaUtilityTargetGenerator.cxx b/Source/cmNinjaUtilityTargetGenerator.cxx index 11ebfb237f..54e6c0c169 100644 --- a/Source/cmNinjaUtilityTargetGenerator.cxx +++ b/Source/cmNinjaUtilityTargetGenerator.cxx @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,7 @@ #include "cmCustomCommand.h" #include "cmCustomCommandGenerator.h" #include "cmGeneratedFileStream.h" +#include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmGlobalNinjaGenerator.h" #include "cmLocalNinjaGenerator.h" @@ -76,6 +78,8 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements( cmGlobalNinjaGenerator::CCOutputs util_outputs(gg); util_outputs.ExplicitOuts.emplace_back(utilCommandName); + std::string commandDesc; + cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance()); bool uses_terminal = false; { std::array const*, 2> const cmdLists = { @@ -87,6 +91,13 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements( cmCustomCommandGenerator ccg(ci, fileConfig, lg); lg->AppendCustomCommandDeps(ccg, deps, fileConfig); lg->AppendCustomCommandLines(ccg, commands); + if (ci.GetComment()) { + if (!commandDesc.empty()) { + commandDesc += "; "; + } + auto cge = ge.Parse(ci.GetComment()); + commandDesc += cge->Evaluate(this->GetLocalGenerator(), config); + } util_outputs.Add(ccg.GetByproducts()); if (ci.GetUsesTerminal()) { uses_terminal = true; @@ -144,6 +155,8 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements( cmValue echoStr = genTarget->GetProperty("EchoString"); if (echoStr) { desc = *echoStr; + } else if (!commandDesc.empty()) { + desc = commandDesc; } else { desc = "Running utility command for " + this->GetTargetName(); } diff --git a/Tests/RunCMake/Ninja/CustomCommandTargetComments.cmake b/Tests/RunCMake/Ninja/CustomCommandTargetComments.cmake new file mode 100644 index 0000000000..cfadcb3096 --- /dev/null +++ b/Tests/RunCMake/Ninja/CustomCommandTargetComments.cmake @@ -0,0 +1,12 @@ +enable_language(C) + +add_executable(hello hello.c) +add_custom_command(TARGET hello PRE_BUILD + COMMENT "pre-build: $<1:genex>" + COMMAND "${CMAKE_COMMAND}" -E echo "$") +add_custom_command(TARGET hello PRE_LINK + COMMENT "pre-link: $<1:genex>" + COMMAND "${CMAKE_COMMAND}" -E echo "$") +add_custom_command(TARGET hello POST_BUILD + COMMENT "post-build: $<1:genex>" + COMMAND "${CMAKE_COMMAND}" -E echo "$") diff --git a/Tests/RunCMake/Ninja/RunCMakeTest.cmake b/Tests/RunCMake/Ninja/RunCMakeTest.cmake index a0e59ee66d..cbf7f264df 100644 --- a/Tests/RunCMake/Ninja/RunCMakeTest.cmake +++ b/Tests/RunCMake/Ninja/RunCMakeTest.cmake @@ -389,6 +389,21 @@ function (run_ChangeBuildType) endfunction() run_ChangeBuildType() +function (run_CustomCommandTargetComments) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CustomCommandTargetComments-build) + run_cmake(CustomCommandTargetComments) + unset(RunCMake_TEST_OPTIONS) + run_ninja("${RunCMake_TEST_BINARY_DIR}" ${maybe_w_dupbuild_err}) + if (NOT ninja_stdout MATCHES [[pre-build: genex; pre-link: genex; Linking C executable hello(\.exe)?; post-build: genex]]) + string(REPLACE "\n" "\n " ninja_stdout "${ninja_stdout}") + message(SEND_ERROR + "Custom command comments are not part of the description:\n" + " ${ninja_stdout}" + ) + endif () +endfunction() +run_CustomCommandTargetComments() + function(run_QtAutoMocSkipPch) set(QtX Qt${CMake_TEST_Qt_version}) if(CMake_TEST_${QtX}Core_Version VERSION_GREATER_EQUAL 5.15.0)