cmake/Source/cmGeneratorExpressionEvaluationFile.cxx

275 lines
8.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2013-11-03 12:27:13 +02:00
#include "cmGeneratorExpressionEvaluationFile.h"
2020-02-01 23:06:01 +01:00
#include <memory>
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <utility>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
2016-07-09 11:21:54 +02:00
#include "cmGeneratedFileStream.h"
2015-04-27 22:25:09 +02:00
#include "cmGlobalGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmListFileCache.h"
2016-07-09 11:21:54 +02:00
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2015-04-27 22:25:09 +02:00
#include "cmSourceFile.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2013-11-03 12:27:13 +02:00
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
2021-09-14 00:13:48 +02:00
std::string input, std::string target,
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCompiledGeneratorExpression> outputFileExpr,
std::unique_ptr<cmCompiledGeneratorExpression> condition,
2021-09-14 00:13:48 +02:00
bool inputIsContent, std::string newLineCharacter, mode_t permissions,
cmPolicies::PolicyStatus policyStatusCMP0070)
2019-11-11 23:01:05 +01:00
: Input(std::move(input))
2021-09-14 00:13:48 +02:00
, Target(std::move(target))
2018-01-26 17:06:56 +01:00
, OutputFileExpr(std::move(outputFileExpr))
, Condition(std::move(condition))
2016-07-09 11:21:54 +02:00
, InputIsContent(inputIsContent)
2021-09-14 00:13:48 +02:00
, NewLineCharacter(std::move(newLineCharacter))
2018-01-26 17:06:56 +01:00
, PolicyStatusCMP0070(policyStatusCMP0070)
2021-09-14 00:13:48 +02:00
, Permissions(permissions)
2013-11-03 12:27:13 +02:00
{
}
2016-07-09 11:21:54 +02:00
void cmGeneratorExpressionEvaluationFile::Generate(
cmLocalGenerator* lg, const std::string& config, const std::string& lang,
cmCompiledGeneratorExpression* inputExpression,
std::map<std::string, std::string>& outputFiles, mode_t perm)
2013-11-03 12:27:13 +02:00
{
std::string rawCondition = this->Condition->GetInput();
2021-09-14 00:13:48 +02:00
cmGeneratorTarget* target = lg->FindGeneratorTargetToUse(this->Target);
2016-07-09 11:21:54 +02:00
if (!rawCondition.empty()) {
2020-02-01 23:06:01 +01:00
std::string condResult =
2021-09-14 00:13:48 +02:00
this->Condition->Evaluate(lg, config, target, nullptr, nullptr, lang);
2016-07-09 11:21:54 +02:00
if (condResult == "0") {
2013-11-03 12:27:13 +02:00
return;
2016-07-09 11:21:54 +02:00
}
if (condResult != "1") {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2016-07-09 11:21:54 +02:00
e << "Evaluation file condition \"" << rawCondition
<< "\" did "
"not evaluate to valid content. Got \""
<< condResult << "\".";
2019-11-11 23:01:05 +01:00
lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
2013-11-03 12:27:13 +02:00
return;
}
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2021-09-14 00:13:48 +02:00
const std::string outputFileName =
this->GetOutputFileName(lg, target, config, lang);
2020-02-01 23:06:01 +01:00
const std::string& outputContent =
2021-09-14 00:13:48 +02:00
inputExpression->Evaluate(lg, config, target, nullptr, nullptr, lang);
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
auto it = outputFiles.find(outputFileName);
2016-07-09 11:21:54 +02:00
if (it != outputFiles.end()) {
if (it->second == outputContent) {
2013-11-03 12:27:13 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2017-07-20 19:35:53 +02:00
e << "Evaluation file to be written multiple times with different "
"content. "
"This is generally caused by the content evaluating the "
"configuration type, language, or location of object files:\n "
2015-08-17 11:37:30 +02:00
<< outputFileName;
2019-11-11 23:01:05 +01:00
lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
2013-11-03 12:27:13 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
2013-11-03 12:27:13 +02:00
this->Files.push_back(outputFileName);
outputFiles[outputFileName] = outputContent;
2021-09-14 00:13:48 +02:00
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;
}
2015-04-27 22:25:09 +02:00
fout.SetCopyIfDifferent(true);
2021-09-14 00:13:48 +02:00
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";
}
}
2016-07-09 11:21:54 +02:00
if (fout.Close() && perm) {
2015-04-27 22:25:09 +02:00
cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2013-11-03 12:27:13 +02:00
2015-04-27 22:25:09 +02:00
void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
2016-07-09 11:21:54 +02:00
cmLocalGenerator* lg, std::string const& config)
2015-04-27 22:25:09 +02:00
{
2015-08-17 11:37:30 +02:00
std::vector<std::string> enabledLanguages;
2016-07-09 11:21:54 +02:00
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2021-09-14 00:13:48 +02:00
cmGeneratorTarget* target = lg->FindGeneratorTargetToUse(this->Target);
2015-08-17 11:37:30 +02:00
gg->GetEnabledLanguages(enabledLanguages);
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& le : enabledLanguages) {
2021-09-14 00:13:48 +02:00
std::string const name = this->GetOutputFileName(lg, target, config, le);
cmSourceFile* sf = lg->GetMakefile()->GetOrCreateGeneratedSource(name);
2015-08-17 11:37:30 +02:00
2018-05-19 10:45:57 +02:00
// Tell the build system generators that there is no build rule
// to generate the file.
sf->SetProperty("__CMAKE_GENERATED_BY_CMAKE", "1");
2016-07-09 11:21:54 +02:00
gg->SetFilenameTargetDepends(
sf, this->OutputFileExpr->GetSourceSensitiveTargets());
}
2013-11-03 12:27:13 +02:00
}
2016-07-09 11:21:54 +02:00
void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
2013-11-03 12:27:13 +02:00
{
std::string inputContent;
2016-07-09 11:21:54 +02:00
if (this->InputIsContent) {
2013-11-03 12:27:13 +02:00
inputContent = this->Input;
2016-07-09 11:21:54 +02:00
} else {
2021-09-14 00:13:48 +02:00
const std::string inputFileName = this->GetInputFileName(lg);
2018-01-26 17:06:56 +01:00
lg->GetMakefile()->AddCMakeDependFile(inputFileName);
2021-09-14 00:13:48 +02:00
if (!this->Permissions) {
cmSystemTools::GetPermissions(inputFileName.c_str(), this->Permissions);
}
2018-01-26 17:06:56 +01:00
cmsys::ifstream fin(inputFileName.c_str());
2016-07-09 11:21:54 +02:00
if (!fin) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2018-01-26 17:06:56 +01:00
e << "Evaluation file \"" << inputFileName << "\" cannot be read.";
2019-11-11 23:01:05 +01:00
lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
2013-11-03 12:27:13 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
std::string line;
std::string sep;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, line)) {
2013-11-03 12:27:13 +02:00
inputContent += sep + line;
sep = "\n";
}
2016-07-09 11:21:54 +02:00
inputContent += sep;
}
2013-11-03 12:27:13 +02:00
cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
2015-11-17 17:22:37 +01:00
cmGeneratorExpression contentGE(lfbt);
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCompiledGeneratorExpression> inputExpression =
2016-07-09 11:21:54 +02:00
contentGE.Parse(inputContent);
2013-11-03 12:27:13 +02:00
std::map<std::string, std::string> outputFiles;
2021-09-14 00:13:48 +02:00
std::vector<std::string> allConfigs =
lg->GetMakefile()->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
2015-08-17 11:37:30 +02:00
std::vector<std::string> enabledLanguages;
2016-07-09 11:21:54 +02:00
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2015-08-17 11:37:30 +02:00
gg->GetEnabledLanguages(enabledLanguages);
2018-01-26 17:06:56 +01:00
for (std::string const& le : enabledLanguages) {
for (std::string const& li : allConfigs) {
2021-09-14 00:13:48 +02:00
this->Generate(lg, li, le, inputExpression.get(), outputFiles,
this->Permissions);
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetFatalErrorOccured()) {
2015-08-17 11:37:30 +02:00
return;
2013-11-03 12:27:13 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
}
2018-01-26 17:06:56 +01:00
2021-09-14 00:13:48 +02:00
std::string cmGeneratorExpressionEvaluationFile::GetInputFileName(
cmLocalGenerator* lg)
{
std::string inputFileName = this->Input;
if (cmSystemTools::FileIsFullPath(inputFileName)) {
inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
} else {
inputFileName = this->FixRelativePath(inputFileName, PathForInput, lg);
}
return inputFileName;
}
std::string cmGeneratorExpressionEvaluationFile::GetOutputFileName(
cmLocalGenerator* lg, cmGeneratorTarget* target, const std::string& config,
const std::string& lang)
{
std::string outputFileName =
this->OutputFileExpr->Evaluate(lg, config, target, nullptr, nullptr, lang);
if (cmSystemTools::FileIsFullPath(outputFileName)) {
outputFileName = cmSystemTools::CollapseFullPath(outputFileName);
} else {
outputFileName = this->FixRelativePath(outputFileName, PathForOutput, lg);
}
return outputFileName;
}
2018-01-26 17:06:56 +01:00
std::string cmGeneratorExpressionEvaluationFile::FixRelativePath(
std::string const& relativePath, PathRole role, cmLocalGenerator* lg)
{
std::string resultPath;
switch (this->PolicyStatusCMP0070) {
case cmPolicies::WARN: {
std::string arg;
switch (role) {
case PathForInput:
arg = "INPUT";
break;
case PathForOutput:
arg = "OUTPUT";
break;
}
std::ostringstream w;
/* clang-format off */
w <<
cmPolicies::GetPolicyWarning(cmPolicies::CMP0070) << "\n"
"file(GENERATE) given relative " << arg << " path:\n"
" " << relativePath << "\n"
"This is not defined behavior unless CMP0070 is set to NEW. "
"For compatibility with older versions of CMake, the previous "
"undefined behavior will be used."
;
/* clang-format on */
2019-11-11 23:01:05 +01:00
lg->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
2018-01-26 17:06:56 +01:00
}
CM_FALLTHROUGH;
case cmPolicies::OLD:
// OLD behavior is to use the relative path unchanged,
// which ends up being used relative to the working dir.
resultPath = relativePath;
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
// NEW behavior is to interpret the relative path with respect
// to the current source or binary directory.
switch (role) {
case PathForInput:
resultPath = cmSystemTools::CollapseFullPath(
relativePath, lg->GetCurrentSourceDirectory());
break;
case PathForOutput:
resultPath = cmSystemTools::CollapseFullPath(
relativePath, lg->GetCurrentBinaryDirectory());
break;
}
break;
}
return resultPath;
}