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
This commit is contained in:
Ben Boeckel
2025-09-02 15:00:53 -04:00
parent ba8c4a15f1
commit 1bf48e34f4
4 changed files with 40 additions and 0 deletions

View File

@@ -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 = "; ";
}

View File

@@ -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")

View File

@@ -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")

View File

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