Переглянути джерело

file(CONFIGURE): Fix newlines in CONTENT

Fixes: #21749
Cristian Adam 4 роки тому
батько
коміт
6e225efd8c

+ 8 - 6
Source/cmFileCommand.cxx

@@ -3105,15 +3105,13 @@ bool HandleConfigureCommand(std::vector<std::string> 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<std::string> 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

+ 6 - 1
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")