cmake/Source/cmGeneratorExpressionEvaluationFile.cxx

228 lines
7.3 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"
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <utility>
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"
2015-04-27 22:25:09 +02:00
#include "cmSourceFile.h"
2018-04-23 21:13:27 +02:00
#include "cmSourceFileLocationKind.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
#include "cmake.h"
2013-11-03 12:27:13 +02:00
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
2016-07-09 11:21:54 +02:00
const std::string& input,
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCompiledGeneratorExpression> outputFileExpr,
std::unique_ptr<cmCompiledGeneratorExpression> condition,
bool inputIsContent, cmPolicies::PolicyStatus policyStatusCMP0070)
2016-07-09 11:21:54 +02:00
: Input(input)
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)
2018-01-26 17:06:56 +01:00
, PolicyStatusCMP0070(policyStatusCMP0070)
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();
2016-07-09 11:21:54 +02:00
if (!rawCondition.empty()) {
2016-10-30 18:24:19 +01:00
std::string condResult = this->Condition->Evaluate(
2018-01-26 17:06:56 +01:00
lg, config, false, nullptr, 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 << "\".";
2015-11-17 17:22:37 +01:00
lg->IssueMessage(cmake::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
2018-01-26 17:06:56 +01:00
std::string outputFileName = this->OutputFileExpr->Evaluate(
lg, config, false, nullptr, nullptr, nullptr, lang);
2016-10-30 18:24:19 +01:00
const std::string outputContent = inputExpression->Evaluate(
2018-01-26 17:06:56 +01:00
lg, config, false, nullptr, nullptr, nullptr, lang);
if (cmSystemTools::FileIsFullPath(outputFileName)) {
outputFileName = cmSystemTools::CollapseFullPath(outputFileName);
} else {
outputFileName = this->FixRelativePath(outputFileName, PathForOutput, lg);
}
2016-07-09 11:21:54 +02:00
std::map<std::string, std::string>::iterator it =
outputFiles.find(outputFileName);
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;
2015-11-17 17:22:37 +01:00
lg->IssueMessage(cmake::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;
2015-04-27 22:25:09 +02:00
cmGeneratedFileStream fout(outputFileName.c_str());
fout.SetCopyIfDifferent(true);
fout << outputContent;
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();
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) {
2016-10-30 18:24:19 +01:00
std::string name = this->OutputFileExpr->Evaluate(
2018-01-26 17:06:56 +01:00
lg, config, false, nullptr, nullptr, nullptr, le);
2018-04-23 21:13:27 +02:00
cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(
name, false, cmSourceFileLocationKind::Known);
2015-08-17 11:37:30 +02:00
sf->SetProperty("GENERATED", "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
{
2015-04-27 22:25:09 +02:00
mode_t perm = 0;
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 {
2018-01-26 17:06:56 +01:00
std::string inputFileName = this->Input;
if (cmSystemTools::FileIsFullPath(inputFileName)) {
inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
} else {
inputFileName = this->FixRelativePath(inputFileName, PathForInput, lg);
}
lg->GetMakefile()->AddCMakeDependFile(inputFileName);
cmSystemTools::GetPermissions(inputFileName.c_str(), perm);
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.";
2016-03-13 13:35:51 +01:00
lg->IssueMessage(cmake::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;
std::vector<std::string> allConfigs;
2015-11-17 17:22:37 +01:00
lg->GetMakefile()->GetConfigurations(allConfigs);
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
if (allConfigs.empty()) {
2018-04-23 21:13:27 +02:00
allConfigs.emplace_back();
2016-07-09 11:21:54 +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();
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) {
this->Generate(lg, li, le, inputExpression.get(), outputFiles, perm);
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
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 */
lg->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
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;
}