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

Ninja: escape special characters in custom command comments

Considerations of Ninja's control sequences was not considered in
30fb5b1b22 (Ninja: add COMMENT to build statement descriptions,
2024-05-01) via !9484. Escape both newlines and dollar signs.

Fixes: #27181
Ben Boeckel 5 месяцев назад
Родитель
Сommit
1bf48e34f4

+ 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)