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

1bf48e34f4 Ninja: escape special characters in custom command comments

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !11136
This commit is contained in:
Brad King
2025-09-03 14:20:01 +00:00
committed by Kitware Robot
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)