ソースを参照

Merge topic 'ninja-custom-command-comment-syntax' into release-4.1

1bf48e34f4 Ninja: escape special characters in custom command comments

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !11136
Brad King 2 ヶ月 前
コミット
a4dd4f3dad

+ 9 - 0
Source/cmNinjaNormalTargetGenerator.cxx

@@ -445,6 +445,13 @@ void cmNinjaNormalTargetGenerator::WriteDeviceLinkRules(
   this->GetGlobalGenerator()->AddRule(rule);
 }
 
+static void NinjaSafeComment(std::string& comment)
+{
+  // Replace control characters in comments.
+  cmSystemTools::ReplaceString(comment, "\n", " / ");
+  cmSystemTools::ReplaceString(comment, "$", "$$");
+}
+
 void cmNinjaNormalTargetGenerator::WriteLinkRule(
   bool useResponseFile, std::string const& config,
   std::vector<std::string> const& preLinkComments,
@@ -607,10 +614,12 @@ void cmNinjaNormalTargetGenerator::WriteLinkRule(
     char const* presep = "";
     char const* postsep = "";
     auto prelink = cmJoin(preLinkComments, "; ");
+    NinjaSafeComment(prelink);
     if (!prelink.empty()) {
       presep = "; ";
     }
     auto postbuild = cmJoin(postBuildComments, "; ");
+    NinjaSafeComment(postbuild);
     if (!postbuild.empty()) {
       postsep = "; ";
     }

+ 11 - 0
Tests/RunCMake/Ninja/CommentsWithDollars.cmake

@@ -0,0 +1,11 @@
+enable_language(C)
+
+add_executable(comments_with_dollars hello.c)
+add_custom_command(TARGET comments_with_dollars PRE_LINK
+        COMMAND "${CMAKE_COMMAND}" -E echo prelink
+        COMMENT "prelink with
+\$dollars")
+add_custom_command(TARGET comments_with_dollars POST_BUILD
+        COMMAND "${CMAKE_COMMAND}" -E echo postbuild
+        COMMENT "postbuild with
+\$dollars")

+ 11 - 0
Tests/RunCMake/Ninja/CommentsWithNewlines.cmake

@@ -0,0 +1,11 @@
+enable_language(C)
+
+add_executable(comments_with_newlines hello.c)
+add_custom_command(TARGET comments_with_newlines PRE_LINK
+        COMMAND "${CMAKE_COMMAND}" -E echo prelink
+        COMMENT "prelink with
+newline")
+add_custom_command(TARGET comments_with_newlines POST_BUILD
+        COMMAND "${CMAKE_COMMAND}" -E echo postbuild
+        COMMENT "postbuild with
+newline")

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

@@ -73,6 +73,15 @@ function(run_NoWorkToDo)
 endfunction()
 run_NoWorkToDo()
 
+function(run_WithBuild name)
+  run_cmake("${name}")
+  set(RunCMake_TEST_NO_CLEAN 1)
+  set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/${name}-build")
+  run_cmake_command("${name}-build" ${CMAKE_COMMAND} --build .)
+endfunction()
+run_WithBuild(CommentsWithDollars)
+run_WithBuild(CommentsWithNewlines)
+
 function(run_VerboseBuild)
   run_cmake(VerboseBuild)
   set(RunCMake_TEST_NO_CLEAN 1)