summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorAsit Dhal <dhal.asitk@gmail.com>2020-12-22 12:29:34 (GMT)
committerAsit Dhal <dhal.asitk@gmail.com>2021-02-01 00:38:19 (GMT)
commit255df8622bc42c62bd8bc81d2ff2964ef8d6a803 (patch)
treee4540fb3853c457c190117a6a7542f807c0c1094 /Source
parent93eef927779c14214d7730ccc6cbb94a04126af7 (diff)
downloadCMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.zip
CMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.tar.gz
CMake-255df8622bc42c62bd8bc81d2ff2964ef8d6a803.tar.bz2
file(GENERATE): Support new line style
Fixes: #19198
Diffstat (limited to 'Source')
-rw-r--r--Source/cmFileCommand.cxx24
-rw-r--r--Source/cmGeneratorExpressionEvaluationFile.cxx31
-rw-r--r--Source/cmGeneratorExpressionEvaluationFile.h3
-rw-r--r--Source/cmMakefile.cxx6
-rw-r--r--Source/cmMakefile.h3
5 files changed, 55 insertions, 12 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx
index 576f015..ab83548 100644
--- a/Source/cmFileCommand.cxx
+++ b/Source/cmFileCommand.cxx
@@ -2290,7 +2290,8 @@ void AddEvaluationFile(const std::string& inputName,
const std::string& targetName,
const std::string& outputExpr,
const std::string& condition, bool inputIsContent,
- mode_t permissions, cmExecutionStatus& status)
+ const std::string& newLineCharacter, mode_t permissions,
+ cmExecutionStatus& status)
{
cmListFileBacktrace lfbt = status.GetMakefile().GetBacktrace();
@@ -2304,7 +2305,7 @@ void AddEvaluationFile(const std::string& inputName,
status.GetMakefile().AddEvaluationFile(
inputName, targetName, std::move(outputCge), std::move(conditionCge),
- permissions, inputIsContent);
+ newLineCharacter, permissions, inputIsContent);
}
bool HandleGenerateCommand(std::vector<std::string> const& args,
@@ -2322,6 +2323,7 @@ bool HandleGenerateCommand(std::vector<std::string> const& args,
std::string Content;
std::string Condition;
std::string Target;
+ std::string NewLineStyle;
bool NoSourcePermissions = false;
bool UseSourcePermissions = false;
std::vector<std::string> FilePermissions;
@@ -2336,7 +2338,8 @@ bool HandleGenerateCommand(std::vector<std::string> const& args,
.Bind("TARGET"_s, &Arguments::Target)
.Bind("NO_SOURCE_PERMISSIONS"_s, &Arguments::NoSourcePermissions)
.Bind("USE_SOURCE_PERMISSIONS"_s, &Arguments::UseSourcePermissions)
- .Bind("FILE_PERMISSIONS"_s, &Arguments::FilePermissions);
+ .Bind("FILE_PERMISSIONS"_s, &Arguments::FilePermissions)
+ .Bind("NEWLINE_STYLE"_s, &Arguments::NewLineStyle);
std::vector<std::string> unparsedArguments;
std::vector<std::string> keywordsMissingValues;
@@ -2400,6 +2403,18 @@ bool HandleGenerateCommand(std::vector<std::string> const& args,
status.SetError("Unknown argument to GENERATE subcommand.");
}
+ const bool newLineStyleSpecified =
+ std::find(parsedKeywords.begin(), parsedKeywords.end(),
+ "NEWLINE_STYLE"_s) != parsedKeywords.end();
+ cmNewLineStyle newLineStyle;
+ if (newLineStyleSpecified) {
+ std::string errorMessage;
+ if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
+ status.SetError(cmStrCat("GENERATE ", errorMessage));
+ return false;
+ }
+ }
+
std::string input = arguments.Input;
if (inputIsContent) {
input = arguments.Content;
@@ -2463,7 +2478,8 @@ bool HandleGenerateCommand(std::vector<std::string> const& args,
}
AddEvaluationFile(input, arguments.Target, arguments.Output,
- arguments.Condition, inputIsContent, permisiions, status);
+ arguments.Condition, inputIsContent,
+ newLineStyle.GetCharacters(), permisiions, status);
return true;
}
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);
}
diff --git a/Source/cmGeneratorExpressionEvaluationFile.h b/Source/cmGeneratorExpressionEvaluationFile.h
index 9a870ac..0eb78ac 100644
--- a/Source/cmGeneratorExpressionEvaluationFile.h
+++ b/Source/cmGeneratorExpressionEvaluationFile.h
@@ -24,7 +24,7 @@ public:
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);
void Generate(cmLocalGenerator* lg);
@@ -58,6 +58,7 @@ private:
const std::unique_ptr<cmCompiledGeneratorExpression> Condition;
std::vector<std::string> Files;
const bool InputIsContent;
+ const std::string NewLineCharacter;
cmPolicies::PolicyStatus PolicyStatusCMP0070;
mode_t Permissions;
};
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 0fc8fa3..d6e60d0 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -865,13 +865,13 @@ void cmMakefile::EnforceDirectoryLevelRules() const
void cmMakefile::AddEvaluationFile(
const std::string& inputFile, const std::string& targetName,
std::unique_ptr<cmCompiledGeneratorExpression> outputName,
- std::unique_ptr<cmCompiledGeneratorExpression> condition, mode_t permissions,
- bool inputIsContent)
+ std::unique_ptr<cmCompiledGeneratorExpression> condition,
+ const std::string& newLineCharacter, mode_t permissions, bool inputIsContent)
{
this->EvaluationFiles.push_back(
cm::make_unique<cmGeneratorExpressionEvaluationFile>(
inputFile, targetName, std::move(outputName), std::move(condition),
- inputIsContent, permissions,
+ inputIsContent, newLineCharacter, permissions,
this->GetPolicyStatus(cmPolicies::CMP0070)));
}
diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h
index 60b66a2..6341abc 100644
--- a/Source/cmMakefile.h
+++ b/Source/cmMakefile.h
@@ -899,7 +899,8 @@ public:
const std::string& inputFile, const std::string& targetName,
std::unique_ptr<cmCompiledGeneratorExpression> outputName,
std::unique_ptr<cmCompiledGeneratorExpression> condition,
- mode_t permissions, bool inputIsContent);
+ const std::string& newLineCharacter, mode_t permissions,
+ bool inputIsContent);
const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
GetEvaluationFiles() const;