2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2013-01-02 20:10:04 +04:00
|
|
|
#include "cmGeneratorExpressionEvaluationFile.h"
|
|
|
|
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmGeneratedFileStream.h"
|
2014-11-05 01:24:54 +03:00
|
|
|
#include "cmGlobalGenerator.h"
|
2016-09-01 21:59:28 +03:00
|
|
|
#include "cmListFileCache.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2014-11-05 01:24:54 +03:00
|
|
|
#include "cmSourceFile.h"
|
2016-09-01 21:59:28 +03:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmake.h"
|
2013-01-02 20:10:04 +04:00
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
#include <cmsys/FStream.hxx>
|
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
2013-01-02 20:10:04 +04:00
|
|
|
|
|
|
|
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& input,
|
2016-06-28 17:17:52 +03:00
|
|
|
CM_AUTO_PTR<cmCompiledGeneratorExpression> outputFileExpr,
|
|
|
|
CM_AUTO_PTR<cmCompiledGeneratorExpression> condition, bool inputIsContent)
|
2016-05-16 17:34:04 +03:00
|
|
|
: Input(input)
|
|
|
|
, OutputFileExpr(outputFileExpr)
|
|
|
|
, Condition(condition)
|
|
|
|
, InputIsContent(inputIsContent)
|
2013-01-02 20:10:04 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03: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-01-02 20:10:04 +04:00
|
|
|
{
|
|
|
|
std::string rawCondition = this->Condition->GetInput();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!rawCondition.empty()) {
|
2016-06-27 23:44:16 +03:00
|
|
|
std::string condResult = this->Condition->Evaluate(
|
|
|
|
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (condResult == "0") {
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (condResult != "1") {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2016-05-16 17:34:04 +03:00
|
|
|
e << "Evaluation file condition \"" << rawCondition
|
|
|
|
<< "\" did "
|
|
|
|
"not evaluate to valid content. Got \""
|
|
|
|
<< condResult << "\".";
|
2015-08-01 13:19:32 +03:00
|
|
|
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
|
2016-06-27 23:44:16 +03:00
|
|
|
const std::string outputFileName = this->OutputFileExpr->Evaluate(
|
|
|
|
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
|
|
|
|
const std::string outputContent = inputExpression->Evaluate(
|
|
|
|
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
|
2016-05-16 17:34:04 +03:00
|
|
|
|
|
|
|
std::map<std::string, std::string>::iterator it =
|
|
|
|
outputFiles.find(outputFileName);
|
2013-01-02 20:10:04 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (it != outputFiles.end()) {
|
|
|
|
if (it->second == outputContent) {
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2013-01-02 20:10:04 +04:00
|
|
|
e << "Evaluation file to be written multiple times for different "
|
2015-02-22 19:44:59 +03:00
|
|
|
"configurations or languages with different content:\n "
|
|
|
|
<< outputFileName;
|
2015-08-01 13:19:32 +03:00
|
|
|
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
|
2016-05-26 23:52:22 +03:00
|
|
|
lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
|
2013-01-02 20:10:04 +04:00
|
|
|
this->Files.push_back(outputFileName);
|
|
|
|
outputFiles[outputFileName] = outputContent;
|
|
|
|
|
2014-04-25 22:11:05 +04:00
|
|
|
cmGeneratedFileStream fout(outputFileName.c_str());
|
|
|
|
fout.SetCopyIfDifferent(true);
|
2013-01-02 20:10:04 +04:00
|
|
|
fout << outputContent;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fout.Close() && perm) {
|
2014-07-22 17:10:57 +04:00
|
|
|
cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|
|
|
|
|
2014-11-05 01:24:54 +03:00
|
|
|
void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
|
2016-05-16 17:34:04 +03:00
|
|
|
cmLocalGenerator* lg, std::string const& config)
|
2014-11-05 01:24:54 +03:00
|
|
|
{
|
2015-02-22 19:44:59 +03:00
|
|
|
std::vector<std::string> enabledLanguages;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
|
2015-02-22 19:44:59 +03:00
|
|
|
gg->GetEnabledLanguages(enabledLanguages);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
|
|
|
|
le != enabledLanguages.end(); ++le) {
|
2016-06-27 23:44:16 +03:00
|
|
|
std::string name = this->OutputFileExpr->Evaluate(
|
|
|
|
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, *le);
|
2015-08-01 13:19:32 +03:00
|
|
|
cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
|
2015-02-22 19:44:59 +03:00
|
|
|
sf->SetProperty("GENERATED", "1");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
gg->SetFilenameTargetDepends(
|
|
|
|
sf, this->OutputFileExpr->GetSourceSensitiveTargets());
|
|
|
|
}
|
2014-11-05 01:24:54 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
|
2013-01-02 20:10:04 +04:00
|
|
|
{
|
2014-07-22 17:10:57 +04:00
|
|
|
mode_t perm = 0;
|
2013-01-02 20:10:04 +04:00
|
|
|
std::string inputContent;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->InputIsContent) {
|
2013-01-02 20:10:04 +04:00
|
|
|
inputContent = this->Input;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-05-26 23:52:22 +03:00
|
|
|
lg->GetMakefile()->AddCMakeDependFile(this->Input);
|
2014-07-22 17:10:57 +04:00
|
|
|
cmSystemTools::GetPermissions(this->Input.c_str(), perm);
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream fin(this->Input.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fin) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2013-01-02 20:10:04 +04:00
|
|
|
e << "Evaluation file \"" << this->Input << "\" cannot be read.";
|
2015-10-07 20:42:13 +03:00
|
|
|
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
|
|
|
|
std::string line;
|
|
|
|
std::string sep;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (cmSystemTools::GetLineFromStream(fin, line)) {
|
2013-01-02 20:10:04 +04:00
|
|
|
inputContent += sep + line;
|
|
|
|
sep = "\n";
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
inputContent += sep;
|
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
|
|
|
|
cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
|
2015-07-09 00:52:51 +03:00
|
|
|
cmGeneratorExpression contentGE(lfbt);
|
2016-06-28 17:17:52 +03:00
|
|
|
CM_AUTO_PTR<cmCompiledGeneratorExpression> inputExpression =
|
2016-05-16 17:34:04 +03:00
|
|
|
contentGE.Parse(inputContent);
|
2013-01-02 20:10:04 +04:00
|
|
|
|
|
|
|
std::map<std::string, std::string> outputFiles;
|
|
|
|
|
|
|
|
std::vector<std::string> allConfigs;
|
2015-08-01 13:19:32 +03:00
|
|
|
lg->GetMakefile()->GetConfigurations(allConfigs);
|
2013-01-02 20:10:04 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (allConfigs.empty()) {
|
2014-03-27 12:46:58 +04:00
|
|
|
allConfigs.push_back("");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-22 19:44:59 +03:00
|
|
|
|
|
|
|
std::vector<std::string> enabledLanguages;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
|
2015-02-22 19:44:59 +03:00
|
|
|
gg->GetEnabledLanguages(enabledLanguages);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
|
|
|
|
le != enabledLanguages.end(); ++le) {
|
|
|
|
for (std::vector<std::string>::const_iterator li = allConfigs.begin();
|
|
|
|
li != allConfigs.end(); ++li) {
|
2015-08-01 13:19:32 +03:00
|
|
|
this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::GetFatalErrorOccured()) {
|
2015-02-22 19:44:59 +03:00
|
|
|
return;
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|