2013-01-02 20:10:04 +04: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"
|
2014-11-05 01:24:54 +03:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmSourceFile.h"
|
2014-04-25 22:11:05 +04:00
|
|
|
#include "cmGeneratedFileStream.h"
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2013-01-02 20:10:04 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
|
|
|
|
const std::string &input,
|
|
|
|
cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
|
|
|
|
cmMakefile *makefile,
|
|
|
|
cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
|
|
|
|
bool inputIsContent)
|
|
|
|
: Input(input),
|
|
|
|
OutputFileExpr(outputFileExpr),
|
|
|
|
Makefile(makefile),
|
|
|
|
Condition(condition),
|
|
|
|
InputIsContent(inputIsContent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2014-02-10 07:48:34 +04:00
|
|
|
void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config,
|
2013-01-02 20:10:04 +04:00
|
|
|
cmCompiledGeneratorExpression* inputExpression,
|
2014-07-22 17:10:57 +04:00
|
|
|
std::map<std::string, std::string> &outputFiles, mode_t perm)
|
2013-01-02 20:10:04 +04:00
|
|
|
{
|
|
|
|
std::string rawCondition = this->Condition->GetInput();
|
|
|
|
if (!rawCondition.empty())
|
|
|
|
{
|
|
|
|
std::string condResult = this->Condition->Evaluate(this->Makefile, config);
|
|
|
|
if (condResult == "0")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (condResult != "1")
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "Evaluation file condition \"" << rawCondition << "\" did "
|
|
|
|
"not evaluate to valid content. Got \"" << condResult << "\".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string outputFileName
|
|
|
|
= this->OutputFileExpr->Evaluate(this->Makefile, config);
|
|
|
|
const std::string outputContent
|
|
|
|
= inputExpression->Evaluate(this->Makefile, config);
|
|
|
|
|
|
|
|
std::map<std::string, std::string>::iterator it
|
|
|
|
= outputFiles.find(outputFileName);
|
|
|
|
|
|
|
|
if(it != outputFiles.end())
|
|
|
|
{
|
|
|
|
if (it->second == outputContent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "Evaluation file to be written multiple times for different "
|
|
|
|
"configurations with different content:\n " << outputFileName;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-22 17:07:39 +04:00
|
|
|
this->Makefile->AddCMakeOutputFile(outputFileName.c_str());
|
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;
|
2014-07-22 17:10:57 +04:00
|
|
|
if (fout.Close() && perm)
|
|
|
|
{
|
|
|
|
cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
|
|
|
|
}
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|
|
|
|
|
2014-11-05 01:24:54 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
|
|
|
|
std::string const& config)
|
|
|
|
{
|
|
|
|
std::string name = this->OutputFileExpr->Evaluate(this->Makefile, config);
|
|
|
|
cmSourceFile* sf = this->Makefile->GetOrCreateSource(name);
|
|
|
|
sf->SetProperty("GENERATED", "1");
|
|
|
|
|
|
|
|
cmGlobalGenerator *gg
|
|
|
|
= this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
|
|
|
|
gg->SetFilenameTargetDepends(sf,
|
|
|
|
this->OutputFileExpr->GetSourceSensitiveTargets());
|
|
|
|
}
|
|
|
|
|
2013-01-02 20:10:04 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratorExpressionEvaluationFile::Generate()
|
|
|
|
{
|
2014-07-22 17:10:57 +04:00
|
|
|
mode_t perm = 0;
|
2013-01-02 20:10:04 +04:00
|
|
|
std::string inputContent;
|
|
|
|
if (this->InputIsContent)
|
|
|
|
{
|
|
|
|
inputContent = this->Input;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-22 17:07:39 +04:00
|
|
|
this->Makefile->AddCMakeDependFile(this->Input.c_str());
|
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());
|
2013-01-02 20:10:04 +04:00
|
|
|
if(!fin)
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "Evaluation file \"" << this->Input << "\" cannot be read.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
2013-01-02 20:10:04 +04: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();
|
2014-05-23 22:59:11 +04:00
|
|
|
cmGeneratorExpression contentGE(&lfbt);
|
2013-01-02 20:10:04 +04:00
|
|
|
cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
|
|
|
|
= contentGE.Parse(inputContent);
|
|
|
|
|
|
|
|
std::map<std::string, std::string> outputFiles;
|
|
|
|
|
|
|
|
std::vector<std::string> allConfigs;
|
|
|
|
this->Makefile->GetConfigurations(allConfigs);
|
|
|
|
|
|
|
|
if (allConfigs.empty())
|
|
|
|
{
|
2014-03-27 12:46:58 +04:00
|
|
|
allConfigs.push_back("");
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|
2014-03-27 12:46:58 +04:00
|
|
|
for(std::vector<std::string>::const_iterator li = allConfigs.begin();
|
|
|
|
li != allConfigs.end(); ++li)
|
2013-01-02 20:10:04 +04:00
|
|
|
{
|
2014-07-22 17:10:57 +04:00
|
|
|
this->Generate(*li, inputExpression.get(), outputFiles, perm);
|
2014-03-27 12:46:58 +04:00
|
|
|
if(cmSystemTools::GetFatalErrorOccured())
|
2013-01-02 20:10:04 +04:00
|
|
|
{
|
2014-03-27 12:46:58 +04:00
|
|
|
return;
|
2013-01-02 20:10:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|