Parcourir la source

Merge topic 'custom-command-comment-genex'

26d813092b add_custom_{command,target}: add genex support for COMMENT
60a5a39022 cmCustomCommandGenerator: refactor GetComment to return std::string

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !7887
Brad King il y a 3 ans
Parent
commit
62172b4ab7

+ 4 - 0
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 <cmake-generator-expressions(7)>`.
+
 ``DEPENDS``
   Specify files on which the command depends.  Each argument is converted
   to a dependency as follows:

+ 4 - 0
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 <cmake-generator-expressions(7)>`.
+
 ``DEPENDS``
   Reference files and outputs of custom commands created with
   :command:`add_custom_command` command calls in the same directory

+ 6 - 0
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 <cmake-generator-expressions(7)>`
+  in their ``COMMENT`` option.

+ 20 - 2
Source/cmCustomCommandGenerator.cxx

@@ -148,6 +148,14 @@ std::string EvaluateDepfile(std::string const& path,
   std::unique_ptr<cmCompiledGeneratorExpression> 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<cmCompiledGeneratorExpression> cge = ge.Parse(comment);
+  return cge->Evaluate(lg, config);
+}
 }
 
 cmCustomCommandGenerator::cmCustomCommandGenerator(
@@ -463,9 +471,19 @@ std::string cmCustomCommandGenerator::GetInternalDepfile() const
   return this->ComputeInternalDepfile(this->OutputConfig, depfile);
 }
 
-const char* cmCustomCommandGenerator::GetComment() const
+cm::optional<std::string> cmCustomCommandGenerator::GetComment() const
 {
-  return this->CC->GetComment();
+  const char* comment = this->CC->GetComment();
+  if (!comment) {
+    return cm::nullopt;
+  }
+  if (!*comment) {
+    return std::string();
+  }
+
+  cmGeneratorExpression ge(*this->LG->GetCMakeInstance(),
+                           this->CC->GetBacktrace());
+  return EvaluateComment(comment, ge, this->LG, this->OutputConfig);
 }
 
 std::string cmCustomCommandGenerator::GetWorkingDirectory() const

+ 1 - 1
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<std::string> GetComment() const;
   std::string GetWorkingDirectory() const;
   std::vector<std::string> const& GetOutputs() const;
   std::vector<std::string> const& GetByproducts() const;

+ 4 - 3
Source/cmGhsMultiTargetGenerator.cxx

@@ -9,6 +9,8 @@
 #include <utility>
 #include <vector>
 
+#include <cm/optional>
+
 #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<std::string> comment = ccg.GetComment()) {
+    std::string echocmd = cmStrCat("echo ", *comment);
     cmdLines.push_back(std::move(echocmd));
   }
 

+ 3 - 2
Source/cmGlobalXCodeGenerator.cxx

@@ -13,6 +13,7 @@
 #include <utility>
 
 #include <cm/memory>
+#include <cm/optional>
 #include <cmext/algorithm>
 #include <cmext/string_view>
 
@@ -2280,11 +2281,11 @@ void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
       }
       makefileStream << "\n";
 
-      if (const char* comment = ccg.GetComment()) {
+      if (cm::optional<std::string> 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";
       }
 

+ 2 - 2
Source/cmLocalGenerator.cxx

@@ -3474,8 +3474,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<std::string> comment = ccg.GetComment()) {
+    return *comment;
   }
 
   // Construct a reasonable default comment if possible.

+ 3 - 3
Source/cmLocalUnixMakefileGenerator3.cxx

@@ -10,6 +10,7 @@
 #include <utility>
 
 #include <cm/memory>
+#include <cm/optional>
 #include <cm/string_view>
 #include <cm/vector>
 #include <cmext/algorithm>
@@ -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<std::string> comment = ccg.GetComment()) {
+      this->AppendEcho(commands, *comment,
                        cmLocalUnixMakefileGenerator3::EchoGenerate);
     }
   }

+ 3 - 3
Source/cmLocalVisualStudio7Generator.cxx

@@ -10,6 +10,7 @@
 #include <utility>
 
 #include <cm/memory>
+#include <cm/optional>
 #include <cmext/algorithm>
 
 #include <windows.h>
@@ -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<std::string> comment = ccg.GetComment()) {
+        this->Stream << "\nDescription=\"" << this->LG->EscapeForXML(*comment)
                      << "\"";
       }
       this->Stream << "\nCommandLine=\"";

+ 1 - 0
Tests/RunCMake/add_custom_command/CommentGenex-build-stdout.txt

@@ -0,0 +1 @@
+lorem ipsum, 01

+ 9 - 0
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 "$<TARGET_PROPERTY:helper,MY_TEXT>$<COMMA> $<STREQUAL:foo,bar>$<EQUAL:42,42>"
+)
+set_property(SOURCE out.txt PROPERTY SYMBOLIC 1)
+add_custom_target(main ALL DEPENDS out.txt)

+ 9 - 0
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()

+ 1 - 0
Tests/RunCMake/add_custom_target/CommentGenex-build-stdout.txt

@@ -0,0 +1 @@
+lorem ipsum, 01

+ 6 - 0
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 "$<TARGET_PROPERTY:helper,MY_TEXT>$<COMMA> $<STREQUAL:foo,bar>$<EQUAL:42,42>"
+)

+ 9 - 0
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()