From 6e225efd8cd1c20bd45c062b168d79a6addb78e5 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Fri, 29 Jan 2021 11:57:03 +0100 Subject: file(CONFIGURE): Fix newlines in CONTENT Fixes: #21749 --- Source/cmFileCommand.cxx | 14 ++++++++------ Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake | 7 ++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 576f015..9377baa 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -3105,15 +3105,13 @@ bool HandleConfigureCommand(std::vector const& args, cmSystemTools::MakeDirectory(path); } - std::string newLineCharacters; - bool open_with_binary_flag = false; + std::string newLineCharacters = "\n"; if (newLineStyle.IsValid()) { - open_with_binary_flag = true; newLineCharacters = newLineStyle.GetCharacters(); } cmGeneratedFileStream fout; - fout.Open(outputFile, false, open_with_binary_flag); + fout.Open(outputFile, false, true); if (!fout) { cmSystemTools::Error("Could not open file for write in copy operation " + outputFile); @@ -3126,11 +3124,15 @@ bool HandleConfigureCommand(std::vector const& args, std::stringstream sin(parsedArgs.Content, std::ios::in); std::string inLine; std::string outLine; - while (cmSystemTools::GetLineFromStream(sin, inLine)) { + bool hasNewLine = false; + while (cmSystemTools::GetLineFromStream(sin, inLine, &hasNewLine)) { outLine.clear(); makeFile.ConfigureString(inLine, outLine, parsedArgs.AtOnly, parsedArgs.EscapeQuotes); - fout << outLine << newLineCharacters; + fout << outLine; + if (hasNewLine || newLineStyle.IsValid()) { + fout << newLineCharacters; + } } // close file before attempting to copy diff --git a/Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake b/Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake index e384873..09aec45 100644 --- a/Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake +++ b/Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake @@ -1,10 +1,13 @@ set(file_name ${CMAKE_CURRENT_BINARY_DIR}/NewLineStyle.txt) function(test_eol style in out) + if (style) + set(newline_stle NEWLINE_STYLE ${style}) + endif() file(CONFIGURE OUTPUT ${file_name} CONTENT "@in@" - NEWLINE_STYLE ${style} + ${newline_stle} ) file(READ ${file_name} new HEX) if(NOT "${new}" STREQUAL "${out}") @@ -18,3 +21,5 @@ test_eol(CRLF "c" "630d0a") test_eol(UNIX "d" "640a") test_eol(LF "e" "650a") + +test_eol("" "a\nb" "610a62") -- cgit v0.12