Просмотр исходного кода

Merge topic 'ninja-acc-target-comments'

0122e02293 Ninja: Add COMMENT to custom command and target descriptions
30fb5b1b22 Ninja: add COMMENT to build statement descriptions

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !9484
Brad King 1 год назад
Родитель
Сommit
ba3ad7c9e3

+ 32 - 6
Source/cmNinjaNormalTargetGenerator.cxx

@@ -18,6 +18,7 @@
 #include "cmCustomCommand.h" // IWYU pragma: keep
 #include "cmCustomCommand.h" // IWYU pragma: keep
 #include "cmCustomCommandGenerator.h"
 #include "cmCustomCommandGenerator.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorExpression.h"
 #include "cmGeneratorOptions.h"
 #include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalNinjaGenerator.h"
 #include "cmGlobalNinjaGenerator.h"
@@ -446,8 +447,10 @@ void cmNinjaNormalTargetGenerator::WriteDeviceLinkRules(
   this->GetGlobalGenerator()->AddRule(rule);
   this->GetGlobalGenerator()->AddRule(rule);
 }
 }
 
 
-void cmNinjaNormalTargetGenerator::WriteLinkRule(bool useResponseFile,
-                                                 std::string const& config)
+void cmNinjaNormalTargetGenerator::WriteLinkRule(
+  bool useResponseFile, std::string const& config,
+  std::vector<std::string> const& preLinkComments,
+  std::vector<std::string> const& postBuildComments)
 {
 {
   cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
   cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
 
 
@@ -604,9 +607,19 @@ void cmNinjaNormalTargetGenerator::WriteLinkRule(bool useResponseFile,
     rule.Comment =
     rule.Comment =
       cmStrCat("Rule for linking ", this->TargetLinkLanguage(config), ' ',
       cmStrCat("Rule for linking ", this->TargetLinkLanguage(config), ' ',
                this->GetVisibleTypeName(), '.');
                this->GetVisibleTypeName(), '.');
-    rule.Description =
-      cmStrCat("Linking ", this->TargetLinkLanguage(config), ' ',
-               this->GetVisibleTypeName(), " $TARGET_FILE");
+    char const* presep = "";
+    char const* postsep = "";
+    auto prelink = cmJoin(preLinkComments, "; ");
+    if (!prelink.empty()) {
+      presep = "; ";
+    }
+    auto postbuild = cmJoin(postBuildComments, "; ");
+    if (!postbuild.empty()) {
+      postsep = "; ";
+    }
+    rule.Description = cmStrCat(
+      prelink, presep, "Linking ", this->TargetLinkLanguage(config), ' ',
+      this->GetVisibleTypeName(), " $TARGET_FILE", postsep, postbuild);
     rule.Restat = "$RESTAT";
     rule.Restat = "$RESTAT";
     this->GetGlobalGenerator()->AddRule(rule);
     this->GetGlobalGenerator()->AddRule(rule);
   }
   }
@@ -1398,12 +1411,19 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
     &gt->GetPostBuildCommands()
     &gt->GetPostBuildCommands()
   };
   };
 
 
+  std::vector<std::string> preLinkComments;
+  std::vector<std::string> postBuildComments;
+
   std::vector<std::string> preLinkCmdLines;
   std::vector<std::string> preLinkCmdLines;
   std::vector<std::string> postBuildCmdLines;
   std::vector<std::string> postBuildCmdLines;
 
 
+  std::vector<std::string>* cmdComments[3] = { &preLinkComments,
+                                               &preLinkComments,
+                                               &postBuildComments };
   std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
   std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
                                                 &preLinkCmdLines,
                                                 &preLinkCmdLines,
                                                 &postBuildCmdLines };
                                                 &postBuildCmdLines };
+  cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance());
 
 
   for (unsigned i = 0; i != 3; ++i) {
   for (unsigned i = 0; i != 3; ++i) {
     for (cmCustomCommand const& cc : *cmdLists[i]) {
     for (cmCustomCommand const& cc : *cmdLists[i]) {
@@ -1413,6 +1433,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
         cmCustomCommandGenerator ccg(cc, fileConfig, this->GetLocalGenerator(),
         cmCustomCommandGenerator ccg(cc, fileConfig, this->GetLocalGenerator(),
                                      true, config);
                                      true, config);
         localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
         localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
+        if (cc.GetComment()) {
+          auto cge = ge.Parse(cc.GetComment());
+          cmdComments[i]->emplace_back(
+            cge->Evaluate(this->GetLocalGenerator(), config));
+        }
         std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
         std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
         byproducts.Add(ccByproducts);
         byproducts.Add(ccByproducts);
         std::transform(
         std::transform(
@@ -1566,7 +1591,8 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
   bool usedResponseFile = false;
   bool usedResponseFile = false;
   globalGen->WriteBuild(this->GetImplFileStream(fileConfig), linkBuild,
   globalGen->WriteBuild(this->GetImplFileStream(fileConfig), linkBuild,
                         commandLineLengthLimit, &usedResponseFile);
                         commandLineLengthLimit, &usedResponseFile);
-  this->WriteLinkRule(usedResponseFile, config);
+  this->WriteLinkRule(usedResponseFile, config, preLinkComments,
+                      postBuildComments);
 
 
   if (symlinkNeeded) {
   if (symlinkNeeded) {
     if (targetType == cmStateEnums::EXECUTABLE) {
     if (targetType == cmStateEnums::EXECUTABLE) {

+ 3 - 1
Source/cmNinjaNormalTargetGenerator.h

@@ -30,7 +30,9 @@ private:
   char const* GetVisibleTypeName() const;
   char const* GetVisibleTypeName() const;
   void WriteLanguagesRules(std::string const& config);
   void WriteLanguagesRules(std::string const& config);
 
 
-  void WriteLinkRule(bool useResponseFile, std::string const& config);
+  void WriteLinkRule(bool useResponseFile, std::string const& config,
+                     std::vector<std::string> const& preLinkComments,
+                     std::vector<std::string> const& postBuildComments);
   void WriteDeviceLinkRules(std::string const& config);
   void WriteDeviceLinkRules(std::string const& config);
   void WriteNvidiaDeviceLinkRule(bool useResponseFile,
   void WriteNvidiaDeviceLinkRule(bool useResponseFile,
                                  std::string const& config);
                                  std::string const& config);

+ 13 - 0
Source/cmNinjaUtilityTargetGenerator.cxx

@@ -5,6 +5,7 @@
 #include <algorithm>
 #include <algorithm>
 #include <array>
 #include <array>
 #include <iterator>
 #include <iterator>
+#include <memory>
 #include <set>
 #include <set>
 #include <string>
 #include <string>
 #include <utility>
 #include <utility>
@@ -13,6 +14,7 @@
 #include "cmCustomCommand.h"
 #include "cmCustomCommand.h"
 #include "cmCustomCommandGenerator.h"
 #include "cmCustomCommandGenerator.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorExpression.h"
 #include "cmGeneratorTarget.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalNinjaGenerator.h"
 #include "cmGlobalNinjaGenerator.h"
 #include "cmLocalNinjaGenerator.h"
 #include "cmLocalNinjaGenerator.h"
@@ -76,6 +78,8 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements(
   cmGlobalNinjaGenerator::CCOutputs util_outputs(gg);
   cmGlobalNinjaGenerator::CCOutputs util_outputs(gg);
   util_outputs.ExplicitOuts.emplace_back(utilCommandName);
   util_outputs.ExplicitOuts.emplace_back(utilCommandName);
 
 
+  std::string commandDesc;
+  cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance());
   bool uses_terminal = false;
   bool uses_terminal = false;
   {
   {
     std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = {
     std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = {
@@ -87,6 +91,13 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements(
         cmCustomCommandGenerator ccg(ci, fileConfig, lg);
         cmCustomCommandGenerator ccg(ci, fileConfig, lg);
         lg->AppendCustomCommandDeps(ccg, deps, fileConfig);
         lg->AppendCustomCommandDeps(ccg, deps, fileConfig);
         lg->AppendCustomCommandLines(ccg, commands);
         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());
         util_outputs.Add(ccg.GetByproducts());
         if (ci.GetUsesTerminal()) {
         if (ci.GetUsesTerminal()) {
           uses_terminal = true;
           uses_terminal = true;
@@ -144,6 +155,8 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements(
     cmValue echoStr = genTarget->GetProperty("EchoString");
     cmValue echoStr = genTarget->GetProperty("EchoString");
     if (echoStr) {
     if (echoStr) {
       desc = *echoStr;
       desc = *echoStr;
+    } else if (!commandDesc.empty()) {
+      desc = commandDesc;
     } else {
     } else {
       desc = "Running utility command for " + this->GetTargetName();
       desc = "Running utility command for " + this->GetTargetName();
     }
     }

+ 12 - 0
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 "$<TARGET_FILE:hello>")
+add_custom_command(TARGET hello PRE_LINK
+  COMMENT "pre-link: $<1:genex>"
+  COMMAND "${CMAKE_COMMAND}" -E echo "$<TARGET_FILE:hello>")
+add_custom_command(TARGET hello POST_BUILD
+  COMMENT "post-build: $<1:genex>"
+  COMMAND "${CMAKE_COMMAND}" -E echo "$<TARGET_FILE:hello>")

+ 15 - 0
Tests/RunCMake/Ninja/RunCMakeTest.cmake

@@ -389,6 +389,21 @@ function (run_ChangeBuildType)
 endfunction()
 endfunction()
 run_ChangeBuildType()
 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)
 function(run_QtAutoMocSkipPch)
   set(QtX Qt${CMake_TEST_Qt_version})
   set(QtX Qt${CMake_TEST_Qt_version})
   if(CMake_TEST_${QtX}Core_Version VERSION_GREATER_EQUAL 5.15.0)
   if(CMake_TEST_${QtX}Core_Version VERSION_GREATER_EQUAL 5.15.0)