cmake/Source/cmGeneratorExpressionEvaluationFile.cxx

189 lines
6.2 KiB
C++
Raw Normal View History

2013-11-03 12:27:13 +02:00
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2013 Stephen Kelly <steveire@gmail.com>
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmGeneratorExpressionEvaluationFile.h"
#include "cmMakefile.h"
2015-04-27 22:25:09 +02:00
#include "cmLocalGenerator.h"
#include "cmGlobalGenerator.h"
#include "cmSourceFile.h"
#include "cmGeneratedFileStream.h"
2014-08-03 19:52:23 +02:00
#include <cmsys/FStream.hxx>
2013-11-03 12:27:13 +02:00
#include <assert.h>
//----------------------------------------------------------------------------
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
const std::string &input,
cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
bool inputIsContent)
: Input(input),
OutputFileExpr(outputFileExpr),
Condition(condition),
InputIsContent(inputIsContent)
{
}
//----------------------------------------------------------------------------
2015-11-17 17:22:37 +01:00
void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg,
const std::string& config,
2015-08-17 11:37:30 +02:00
const std::string& lang,
2013-11-03 12:27:13 +02:00
cmCompiledGeneratorExpression* inputExpression,
2015-04-27 22:25:09 +02:00
std::map<std::string, std::string> &outputFiles, mode_t perm)
2013-11-03 12:27:13 +02:00
{
std::string rawCondition = this->Condition->GetInput();
if (!rawCondition.empty())
{
2016-03-13 13:35:51 +01:00
std::string condResult = this->Condition->Evaluate(lg,
2015-11-17 17:22:37 +01:00
config,
2015-08-17 11:37:30 +02:00
false, 0, 0, 0, lang);
2013-11-03 12:27:13 +02:00
if (condResult == "0")
{
return;
}
if (condResult != "1")
{
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2013-11-03 12:27:13 +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;
}
}
const std::string outputFileName
2016-03-13 13:35:51 +01:00
= this->OutputFileExpr->Evaluate(lg, config,
2015-08-17 11:37:30 +02:00
false, 0, 0, 0, lang);
2013-11-03 12:27:13 +02:00
const std::string outputContent
2016-03-13 13:35:51 +01:00
= inputExpression->Evaluate(lg,
2015-11-17 17:22:37 +01:00
config,
2015-08-17 11:37:30 +02:00
false, 0, 0, 0, lang);
2013-11-03 12:27:13 +02:00
std::map<std::string, std::string>::iterator it
= outputFiles.find(outputFileName);
if(it != outputFiles.end())
{
if (it->second == outputContent)
{
return;
}
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2013-11-03 12:27:13 +02:00
e << "Evaluation file to be written multiple times for different "
2015-08-17 11:37:30 +02:00
"configurations or languages with different content:\n "
<< 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;
}
2015-11-17 17:22:37 +01:00
lg->GetMakefile()->AddCMakeOutputFile(outputFileName.c_str());
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;
if (fout.Close() && perm)
2013-11-03 12:27:13 +02:00
{
2015-04-27 22:25:09 +02:00
cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
2013-11-03 12:27:13 +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(
2015-11-17 17:22:37 +01: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;
2015-11-17 17:22:37 +01:00
cmGlobalGenerator *gg = lg->GetGlobalGenerator();
2015-08-17 11:37:30 +02:00
gg->GetEnabledLanguages(enabledLanguages);
2015-04-27 22:25:09 +02:00
2015-08-17 11:37:30 +02:00
for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
le != enabledLanguages.end(); ++le)
{
2016-03-13 13:35:51 +01:00
std::string name = this->OutputFileExpr->Evaluate(lg,
2015-11-17 17:22:37 +01:00
config,
2015-08-17 11:37:30 +02:00
false, 0, 0, 0, *le);
2015-11-17 17:22:37 +01:00
cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
2015-08-17 11:37:30 +02:00
sf->SetProperty("GENERATED", "1");
gg->SetFilenameTargetDepends(sf,
2015-04-27 22:25:09 +02:00
this->OutputFileExpr->GetSourceSensitiveTargets());
2015-08-17 11:37:30 +02:00
}
2013-11-03 12:27:13 +02:00
}
//----------------------------------------------------------------------------
2015-11-17 17:22:37 +01: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;
if (this->InputIsContent)
{
inputContent = this->Input;
}
else
{
2015-11-17 17:22:37 +01:00
lg->GetMakefile()->AddCMakeDependFile(this->Input.c_str());
2015-04-27 22:25:09 +02:00
cmSystemTools::GetPermissions(this->Input.c_str(), perm);
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(this->Input.c_str());
2013-11-03 12:27:13 +02:00
if(!fin)
{
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2013-11-03 12:27:13 +02:00
e << "Evaluation file \"" << this->Input << "\" 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;
}
std::string line;
std::string sep;
while(cmSystemTools::GetLineFromStream(fin, line))
{
inputContent += sep + line;
sep = "\n";
}
inputContent += sep;
}
cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
2015-11-17 17:22:37 +01:00
cmGeneratorExpression contentGE(lfbt);
2013-11-03 12:27:13 +02:00
cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
= contentGE.Parse(inputContent);
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
if (allConfigs.empty())
{
2015-04-27 22:25:09 +02:00
allConfigs.push_back("");
2013-11-03 12:27:13 +02:00
}
2015-08-17 11:37:30 +02:00
std::vector<std::string> enabledLanguages;
2015-11-17 17:22:37 +01:00
cmGlobalGenerator *gg = lg->GetGlobalGenerator();
2015-08-17 11:37:30 +02:00
gg->GetEnabledLanguages(enabledLanguages);
for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
le != enabledLanguages.end(); ++le)
2013-11-03 12:27:13 +02:00
{
2015-08-17 11:37:30 +02:00
for(std::vector<std::string>::const_iterator li = allConfigs.begin();
li != allConfigs.end(); ++li)
2013-11-03 12:27:13 +02:00
{
2015-11-17 17:22:37 +01:00
this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
2015-08-17 11:37:30 +02:00
if(cmSystemTools::GetFatalErrorOccured())
{
return;
}
2013-11-03 12:27:13 +02:00
}
}
}