浏览代码

file(CONFIGURE): Fix newlines in CONTENT

Fixes: #21749
Cristian Adam 4 年之前
父节点
当前提交
6e225efd8c
共有 2 个文件被更改,包括 14 次插入7 次删除
  1. 8 6
      Source/cmFileCommand.cxx
  2. 6 1
      Tests/RunCMake/File_Configure/NewLineStyle-ValidArg.cmake

+ 8 - 6
Source/cmFileCommand.cxx

@@ -3105,15 +3105,13 @@ bool HandleConfigureCommand(std::vector<std::string> const& args,
     cmSystemTools::MakeDirectory(path);
     cmSystemTools::MakeDirectory(path);
   }
   }
 
 
-  std::string newLineCharacters;
-  bool open_with_binary_flag = false;
+  std::string newLineCharacters = "\n";
   if (newLineStyle.IsValid()) {
   if (newLineStyle.IsValid()) {
-    open_with_binary_flag = true;
     newLineCharacters = newLineStyle.GetCharacters();
     newLineCharacters = newLineStyle.GetCharacters();
   }
   }
 
 
   cmGeneratedFileStream fout;
   cmGeneratedFileStream fout;
-  fout.Open(outputFile, false, open_with_binary_flag);
+  fout.Open(outputFile, false, true);
   if (!fout) {
   if (!fout) {
     cmSystemTools::Error("Could not open file for write in copy operation " +
     cmSystemTools::Error("Could not open file for write in copy operation " +
                          outputFile);
                          outputFile);
@@ -3126,11 +3124,15 @@ bool HandleConfigureCommand(std::vector<std::string> const& args,
   std::stringstream sin(parsedArgs.Content, std::ios::in);
   std::stringstream sin(parsedArgs.Content, std::ios::in);
   std::string inLine;
   std::string inLine;
   std::string outLine;
   std::string outLine;
-  while (cmSystemTools::GetLineFromStream(sin, inLine)) {
+  bool hasNewLine = false;
+  while (cmSystemTools::GetLineFromStream(sin, inLine, &hasNewLine)) {
     outLine.clear();
     outLine.clear();
     makeFile.ConfigureString(inLine, outLine, parsedArgs.AtOnly,
     makeFile.ConfigureString(inLine, outLine, parsedArgs.AtOnly,
                              parsedArgs.EscapeQuotes);
                              parsedArgs.EscapeQuotes);
-    fout << outLine << newLineCharacters;
+    fout << outLine;
+    if (hasNewLine || newLineStyle.IsValid()) {
+      fout << newLineCharacters;
+    }
   }
   }
 
 
   // close file before attempting to copy
   // 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)
 set(file_name  ${CMAKE_CURRENT_BINARY_DIR}/NewLineStyle.txt)
 
 
 function(test_eol style in out)
 function(test_eol style in out)
+    if (style)
+      set(newline_stle NEWLINE_STYLE ${style})
+    endif()
     file(CONFIGURE
     file(CONFIGURE
         OUTPUT ${file_name}
         OUTPUT ${file_name}
         CONTENT "@in@"
         CONTENT "@in@"
-        NEWLINE_STYLE ${style}
+        ${newline_stle}
     )
     )
     file(READ ${file_name} new HEX)
     file(READ ${file_name} new HEX)
     if(NOT "${new}" STREQUAL "${out}")
     if(NOT "${new}" STREQUAL "${out}")
@@ -18,3 +21,5 @@ test_eol(CRLF  "c" "630d0a")
 
 
 test_eol(UNIX  "d" "640a")
 test_eol(UNIX  "d" "640a")
 test_eol(LF    "e" "650a")
 test_eol(LF    "e" "650a")
+
+test_eol("" "a\nb" "610a62")