From 60a5a39022c8c2504173d635732110b2550b7f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20W=C3=BCrth?= Date: Thu, 17 Nov 2022 12:12:26 +0100 Subject: cmCustomCommandGenerator: refactor GetComment to return std::string Refactoring was done because EvaluateComment leaked memory. --- Source/cmCustomCommandGenerator.cxx | 7 +++++-- Source/cmCustomCommandGenerator.h | 2 +- Source/cmGhsMultiTargetGenerator.cxx | 7 ++++--- Source/cmGlobalXCodeGenerator.cxx | 5 +++-- Source/cmLocalGenerator.cxx | 4 ++-- Source/cmLocalUnixMakefileGenerator3.cxx | 6 +++--- Source/cmLocalVisualStudio7Generator.cxx | 6 +++--- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index 57b009a..54f0bab 100644 --- a/Source/cmCustomCommandGenerator.cxx +++ b/Source/cmCustomCommandGenerator.cxx @@ -463,9 +463,12 @@ std::string cmCustomCommandGenerator::GetInternalDepfile() const return this->ComputeInternalDepfile(this->OutputConfig, depfile); } -const char* cmCustomCommandGenerator::GetComment() const +cm::optional cmCustomCommandGenerator::GetComment() const { - return this->CC->GetComment(); + if (const char* comment = this->CC->GetComment()) { + return comment; + } + return cm::nullopt; } std::string cmCustomCommandGenerator::GetWorkingDirectory() const diff --git a/Source/cmCustomCommandGenerator.h b/Source/cmCustomCommandGenerator.h index 73a8d38..4453654 100644 --- a/Source/cmCustomCommandGenerator.h +++ b/Source/cmCustomCommandGenerator.h @@ -58,7 +58,7 @@ public: unsigned int GetNumberOfCommands() const; std::string GetCommand(unsigned int c) const; void AppendArguments(unsigned int c, std::string& cmd) const; - const char* GetComment() const; + cm::optional GetComment() const; std::string GetWorkingDirectory() const; std::vector const& GetOutputs() const; std::vector const& GetByproducts() const; diff --git a/Source/cmGhsMultiTargetGenerator.cxx b/Source/cmGhsMultiTargetGenerator.cxx index 138d3f1..8471dfe 100644 --- a/Source/cmGhsMultiTargetGenerator.cxx +++ b/Source/cmGhsMultiTargetGenerator.cxx @@ -9,6 +9,8 @@ #include #include +#include + #include "cmCustomCommand.h" #include "cmCustomCommandGenerator.h" #include "cmGeneratedFileStream.h" @@ -411,9 +413,8 @@ void cmGhsMultiTargetGenerator::WriteCustomCommandsHelper( cmdLines.push_back("@echo off"); #endif // Echo the custom command's comment text. - const char* comment = ccg.GetComment(); - if (comment && *comment) { - std::string echocmd = cmStrCat("echo ", comment); + if (cm::optional comment = ccg.GetComment()) { + std::string echocmd = cmStrCat("echo ", *comment); cmdLines.push_back(std::move(echocmd)); } diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 0e94de5..d57e2d1 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -2280,11 +2281,11 @@ void cmGlobalXCodeGenerator::CreateCustomRulesMakefile( } makefileStream << "\n"; - if (const char* comment = ccg.GetComment()) { + if (cm::optional comment = ccg.GetComment()) { std::string echo_cmd = cmStrCat("echo ", (this->CurrentLocalGenerator->EscapeForShell( - comment, ccg.GetCC().GetEscapeAllowMakeVars()))); + *comment, ccg.GetCC().GetEscapeAllowMakeVars()))); makefileStream << "\t" << echo_cmd << "\n"; } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 9745142..af9abe5 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -3484,8 +3484,8 @@ std::string cmLocalGenerator::ConstructComment( cmCustomCommandGenerator const& ccg, const char* default_comment) const { // Check for a comment provided with the command. - if (ccg.GetComment()) { - return ccg.GetComment(); + if (cm::optional comment = ccg.GetComment()) { + return *comment; } // Construct a reasonable default comment if possible. diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index de1d3cd..7172d34 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -945,9 +946,8 @@ void cmLocalUnixMakefileGenerator3::AppendCustomCommand( // post-build command comments. Custom build step commands have // their comments generated elsewhere. if (echo_comment) { - const char* comment = ccg.GetComment(); - if (comment && *comment) { - this->AppendEcho(commands, comment, + if (cm::optional comment = ccg.GetComment()) { + this->AppendEcho(commands, *comment, cmLocalUnixMakefileGenerator3::EchoGenerate); } } diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index 383045d..538c036 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -592,9 +593,8 @@ public: { cmCustomCommandGenerator ccg(cc, this->Config, this->LG); if (this->First) { - const char* comment = ccg.GetComment(); - if (comment && *comment) { - this->Stream << "\nDescription=\"" << this->LG->EscapeForXML(comment) + if (cm::optional comment = ccg.GetComment()) { + this->Stream << "\nDescription=\"" << this->LG->EscapeForXML(*comment) << "\""; } this->Stream << "\nCommandLine=\""; -- cgit v0.12 From 26d813092bffdcda77976ab5ba59c114e3e2fda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20W=C3=BCrth?= Date: Fri, 18 Nov 2022 12:29:47 +0100 Subject: add_custom_{command,target}: add genex support for COMMENT Evaluate and expand generator expressions in the `COMMENT` argument of the `add_custom_command()` and `add_custom_target()` commands. This allows to include generator expressions, e.g. a targets location $ or the current configuration $, in the build-time messages. Fixes #22507 --- Help/command/add_custom_command.rst | 4 ++++ Help/command/add_custom_target.rst | 4 ++++ Help/release/dev/custom-command-comment-genex.rst | 6 ++++++ Source/cmCustomCommandGenerator.cxx | 21 ++++++++++++++++++--- .../CommentGenex-build-stdout.txt | 1 + .../RunCMake/add_custom_command/CommentGenex.cmake | 9 +++++++++ .../RunCMake/add_custom_command/RunCMakeTest.cmake | 9 +++++++++ .../add_custom_target/CommentGenex-build-stdout.txt | 1 + Tests/RunCMake/add_custom_target/CommentGenex.cmake | 6 ++++++ Tests/RunCMake/add_custom_target/RunCMakeTest.cmake | 9 +++++++++ 10 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 Help/release/dev/custom-command-comment-genex.rst create mode 100644 Tests/RunCMake/add_custom_command/CommentGenex-build-stdout.txt create mode 100644 Tests/RunCMake/add_custom_command/CommentGenex.cmake create mode 100644 Tests/RunCMake/add_custom_target/CommentGenex-build-stdout.txt create mode 100644 Tests/RunCMake/add_custom_target/CommentGenex.cmake diff --git a/Help/command/add_custom_command.rst b/Help/command/add_custom_command.rst index a999c2d..5878997 100644 --- a/Help/command/add_custom_command.rst +++ b/Help/command/add_custom_command.rst @@ -140,6 +140,10 @@ The options are: Display the given message before the commands are executed at build time. + .. versionadded:: 3.26 + Arguments to ``COMMENT`` may use + :manual:`generator expressions `. + ``DEPENDS`` Specify files on which the command depends. Each argument is converted to a dependency as follows: diff --git a/Help/command/add_custom_target.rst b/Help/command/add_custom_target.rst index ec02ee2..545b9a5 100644 --- a/Help/command/add_custom_target.rst +++ b/Help/command/add_custom_target.rst @@ -109,6 +109,10 @@ The options are: Display the given message before the commands are executed at build time. + .. versionadded:: 3.26 + Arguments to ``COMMENT`` may use + :manual:`generator expressions `. + ``DEPENDS`` Reference files and outputs of custom commands created with :command:`add_custom_command` command calls in the same directory diff --git a/Help/release/dev/custom-command-comment-genex.rst b/Help/release/dev/custom-command-comment-genex.rst new file mode 100644 index 0000000..f9402f2 --- /dev/null +++ b/Help/release/dev/custom-command-comment-genex.rst @@ -0,0 +1,6 @@ +custom-command-comment-genex +---------------------------- + +* :command:`add_custom_command` and :command:`add_custom_target` now + support :manual:`generator expressions ` + in their ``COMMENT`` option. diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index 54f0bab..14c22e3 100644 --- a/Source/cmCustomCommandGenerator.cxx +++ b/Source/cmCustomCommandGenerator.cxx @@ -148,6 +148,14 @@ std::string EvaluateDepfile(std::string const& path, std::unique_ptr cge = ge.Parse(path); return cge->Evaluate(lg, config); } + +std::string EvaluateComment(const char* comment, + cmGeneratorExpression const& ge, + cmLocalGenerator* lg, std::string const& config) +{ + std::unique_ptr cge = ge.Parse(comment); + return cge->Evaluate(lg, config); +} } cmCustomCommandGenerator::cmCustomCommandGenerator( @@ -465,10 +473,17 @@ std::string cmCustomCommandGenerator::GetInternalDepfile() const cm::optional cmCustomCommandGenerator::GetComment() const { - if (const char* comment = this->CC->GetComment()) { - return comment; + const char* comment = this->CC->GetComment(); + if (!comment) { + return cm::nullopt; + } + if (!*comment) { + return std::string(); } - return cm::nullopt; + + cmGeneratorExpression ge(*this->LG->GetCMakeInstance(), + this->CC->GetBacktrace()); + return EvaluateComment(comment, ge, this->LG, this->OutputConfig); } std::string cmCustomCommandGenerator::GetWorkingDirectory() const diff --git a/Tests/RunCMake/add_custom_command/CommentGenex-build-stdout.txt b/Tests/RunCMake/add_custom_command/CommentGenex-build-stdout.txt new file mode 100644 index 0000000..bf49657 --- /dev/null +++ b/Tests/RunCMake/add_custom_command/CommentGenex-build-stdout.txt @@ -0,0 +1 @@ +lorem ipsum, 01 diff --git a/Tests/RunCMake/add_custom_command/CommentGenex.cmake b/Tests/RunCMake/add_custom_command/CommentGenex.cmake new file mode 100644 index 0000000..f517392 --- /dev/null +++ b/Tests/RunCMake/add_custom_command/CommentGenex.cmake @@ -0,0 +1,9 @@ +add_custom_target(helper) +set_property(TARGET helper PROPERTY MY_TEXT "lorem ipsum") +add_custom_command( + OUTPUT out.txt + COMMAND ${CMAKE_COMMAND} -E echo true + COMMENT "$$ $$" +) +set_property(SOURCE out.txt PROPERTY SYMBOLIC 1) +add_custom_target(main ALL DEPENDS out.txt) diff --git a/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake b/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake index ad6b258..6c677c0 100644 --- a/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake +++ b/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake @@ -60,3 +60,12 @@ function(test_genex name) endfunction() test_genex(TargetGenexEvent) + +if(NOT RunCMake_GENERATOR STREQUAL "Xcode") + block() + run_cmake(CommentGenex) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CommentGenex-build) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(CommentGenex-build ${CMAKE_COMMAND} --build .) + endblock() +endif() diff --git a/Tests/RunCMake/add_custom_target/CommentGenex-build-stdout.txt b/Tests/RunCMake/add_custom_target/CommentGenex-build-stdout.txt new file mode 100644 index 0000000..bf49657 --- /dev/null +++ b/Tests/RunCMake/add_custom_target/CommentGenex-build-stdout.txt @@ -0,0 +1 @@ +lorem ipsum, 01 diff --git a/Tests/RunCMake/add_custom_target/CommentGenex.cmake b/Tests/RunCMake/add_custom_target/CommentGenex.cmake new file mode 100644 index 0000000..6e6706e --- /dev/null +++ b/Tests/RunCMake/add_custom_target/CommentGenex.cmake @@ -0,0 +1,6 @@ +add_custom_target(helper) +set_property(TARGET helper PROPERTY MY_TEXT "lorem ipsum") +add_custom_target(main ALL + COMMAND ${CMAKE_COMMAND} -E true + COMMENT "$$ $$" +) diff --git a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake index 22a9ed4..f43779b 100644 --- a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake +++ b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake @@ -23,3 +23,12 @@ function(run_TargetOrder) run_cmake_command(TargetOrder-build ${CMAKE_COMMAND} --build . -- ${build_flags}) endfunction() run_TargetOrder() + +if(NOT RunCMake_GENERATOR STREQUAL "Xcode") + block() + run_cmake(CommentGenex) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CommentGenex-build) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(CommentGenex-build ${CMAKE_COMMAND} --build .) + endblock() +endif() -- cgit v0.12