diff options
author | Asit Dhal <dhal.asitk@gmail.com> | 2020-12-22 12:29:34 (GMT) |
---|---|---|
committer | Asit Dhal <dhal.asitk@gmail.com> | 2021-02-01 00:38:19 (GMT) |
commit | 255df8622bc42c62bd8bc81d2ff2964ef8d6a803 (patch) | |
tree | e4540fb3853c457c190117a6a7542f807c0c1094 /Source/cmGeneratorExpressionEvaluationFile.cxx | |
parent | 93eef927779c14214d7730ccc6cbb94a04126af7 (diff) | |
download | CMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.zip CMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.tar.gz CMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.tar.bz2 |
file(GENERATE): Support new line style
Fixes: #19198
Diffstat (limited to 'Source/cmGeneratorExpressionEvaluationFile.cxx')
-rw-r--r-- | Source/cmGeneratorExpressionEvaluationFile.cxx | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/Source/cmGeneratorExpressionEvaluationFile.cxx b/Source/cmGeneratorExpressionEvaluationFile.cxx index ec44df3..9fae15a 100644 --- a/Source/cmGeneratorExpressionEvaluationFile.cxx +++ b/Source/cmGeneratorExpressionEvaluationFile.cxx @@ -21,13 +21,14 @@ cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile( std::string input, std::string target, std::unique_ptr<cmCompiledGeneratorExpression> outputFileExpr, std::unique_ptr<cmCompiledGeneratorExpression> condition, - bool inputIsContent, mode_t permissions, + bool inputIsContent, std::string newLineCharacter, mode_t permissions, cmPolicies::PolicyStatus policyStatusCMP0070) : Input(std::move(input)) , Target(std::move(target)) , OutputFileExpr(std::move(outputFileExpr)) , Condition(std::move(condition)) , InputIsContent(inputIsContent) + , NewLineCharacter(std::move(newLineCharacter)) , PolicyStatusCMP0070(policyStatusCMP0070) , Permissions(permissions) { @@ -82,9 +83,33 @@ void cmGeneratorExpressionEvaluationFile::Generate( this->Files.push_back(outputFileName); outputFiles[outputFileName] = outputContent; - cmGeneratedFileStream fout(outputFileName); + bool openWithBinaryFlag = false; + if (!this->NewLineCharacter.empty()) { + openWithBinaryFlag = true; + } + cmGeneratedFileStream fout; + fout.Open(outputFileName, false, openWithBinaryFlag); + if (!fout) { + lg->IssueMessage(MessageType::FATAL_ERROR, + "Could not open file for write in copy operation " + + outputFileName); + return; + } fout.SetCopyIfDifferent(true); - fout << outputContent; + std::istringstream iss(outputContent); + std::string line; + bool hasNewLine = false; + while (cmSystemTools::GetLineFromStream(iss, line, &hasNewLine)) { + fout << line; + if (!this->NewLineCharacter.empty()) { + fout << this->NewLineCharacter; + } else if (hasNewLine) { + // if new line character is not specified, the file will be opened in + // text mode. So, "\n" will be translated to the correct newline + // ending based on the platform. + fout << "\n"; + } + } if (fout.Close() && perm) { cmSystemTools::SetPermissions(outputFileName.c_str(), perm); } |