diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index e04f27044c..2c526f13bb 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/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 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 = "; "; } diff --git a/Tests/RunCMake/Ninja/CommentsWithDollars.cmake b/Tests/RunCMake/Ninja/CommentsWithDollars.cmake new file mode 100644 index 0000000000..8e3253cf66 --- /dev/null +++ b/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") diff --git a/Tests/RunCMake/Ninja/CommentsWithNewlines.cmake b/Tests/RunCMake/Ninja/CommentsWithNewlines.cmake new file mode 100644 index 0000000000..a2a84e8ffc --- /dev/null +++ b/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") diff --git a/Tests/RunCMake/Ninja/RunCMakeTest.cmake b/Tests/RunCMake/Ninja/RunCMakeTest.cmake index cbf7f264df..9e9caf11f5 100644 --- a/Tests/RunCMake/Ninja/RunCMakeTest.cmake +++ b/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)