2012-09-11 21:53:38 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2012 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 "cmGeneratorExpressionEvaluator.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmAlgorithms.h"
|
2012-09-18 15:53:39 +04:00
|
|
|
#include "cmGeneratorExpression.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmGeneratorExpressionDAGChecker.h"
|
|
|
|
#include "cmGeneratorExpressionParser.h"
|
2015-02-22 19:43:13 +03:00
|
|
|
#include "cmGlobalGenerator.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2014-02-26 18:59:18 +04:00
|
|
|
#include "cmSourceFile.h"
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2012-11-01 17:21:25 +04:00
|
|
|
#include <cmsys/String.h>
|
|
|
|
|
2013-01-03 23:56:32 +04:00
|
|
|
#include <assert.h>
|
2013-12-27 15:42:37 +04:00
|
|
|
#include <errno.h>
|
2013-01-03 23:56:32 +04:00
|
|
|
|
2015-02-23 01:32:11 +03:00
|
|
|
#include "cmGeneratorExpressionNode.h"
|
2012-09-11 21:53:38 +04:00
|
|
|
|
|
|
|
GeneratorExpressionContent::GeneratorExpressionContent(
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* startContent, size_t length)
|
|
|
|
: StartContent(startContent)
|
|
|
|
, ContentLength(length)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GeneratorExpressionContent::GetOriginalExpression() const
|
|
|
|
{
|
|
|
|
return std::string(this->StartContent, this->ContentLength);
|
|
|
|
}
|
|
|
|
|
2013-04-25 15:54:18 +04:00
|
|
|
std::string GeneratorExpressionContent::ProcessArbitraryContent(
|
2016-05-16 17:34:04 +03:00
|
|
|
const cmGeneratorExpressionNode* node, const std::string& identifier,
|
|
|
|
cmGeneratorExpressionContext* context,
|
|
|
|
cmGeneratorExpressionDAGChecker* dagChecker,
|
|
|
|
std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
|
2013-04-25 15:54:18 +04:00
|
|
|
pit) const
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::vector<
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator pend =
|
|
|
|
this->ParamChildren.end();
|
|
|
|
for (; pit != pend; ++pit) {
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
|
|
|
|
pit->begin();
|
|
|
|
const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
|
|
|
|
pit->end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (node->RequiresLiteralInput()) {
|
|
|
|
if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text) {
|
|
|
|
reportError(context, this->GetOriginalExpression(), "$<" +
|
|
|
|
identifier + "> expression requires literal input.");
|
2013-04-25 15:54:18 +04:00
|
|
|
return std::string();
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-04-25 15:54:18 +04:00
|
|
|
result += (*it)->Evaluate(context, dagChecker);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (context->HadError) {
|
2013-04-25 15:54:18 +04:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((pit + 1) != pend) {
|
|
|
|
result += ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node->RequiresLiteralInput()) {
|
2013-04-25 15:54:18 +04:00
|
|
|
std::vector<std::string> parameters;
|
|
|
|
parameters.push_back(result);
|
|
|
|
return node->Evaluate(parameters, context, this, dagChecker);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-04-25 15:54:18 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-11 21:53:38 +04:00
|
|
|
std::string GeneratorExpressionContent::Evaluate(
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratorExpressionContext* context,
|
|
|
|
cmGeneratorExpressionDAGChecker* dagChecker) const
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
|
|
|
std::string identifier;
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
|
|
|
|
this->IdentifierChildren.begin();
|
|
|
|
const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
|
|
|
|
this->IdentifierChildren.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
identifier += (*it)->Evaluate(context, dagChecker);
|
|
|
|
if (context->HadError) {
|
|
|
|
return std::string();
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const cmGeneratorExpressionNode* node =
|
|
|
|
cmGeneratorExpressionNode::GetNode(identifier);
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!node) {
|
2012-09-11 21:53:38 +04:00
|
|
|
reportError(context, this->GetOriginalExpression(),
|
2016-05-16 17:34:04 +03:00
|
|
|
"Expression did not evaluate to a known generator expression");
|
2012-09-11 21:53:38 +04:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!node->GeneratesContent()) {
|
|
|
|
if (node->NumExpectedParameters() == 1 &&
|
|
|
|
node->AcceptsArbitraryContentParameter()) {
|
|
|
|
if (this->ParamChildren.empty()) {
|
2012-11-27 01:43:13 +04:00
|
|
|
reportError(context, this->GetOriginalExpression(),
|
2016-05-16 17:34:04 +03:00
|
|
|
"$<" + identifier + "> expression requires a parameter.");
|
2012-11-27 01:43:13 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-11-27 01:43:13 +04:00
|
|
|
std::vector<std::string> parameters;
|
|
|
|
this->EvaluateParameters(node, identifier, context, dagChecker,
|
|
|
|
parameters);
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return std::string();
|
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
|
|
|
std::vector<std::string> parameters;
|
2012-11-27 01:42:00 +04:00
|
|
|
this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (context->HadError) {
|
2012-11-27 01:42:00 +04:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-11-27 01:42:00 +04:00
|
|
|
|
|
|
|
return node->Evaluate(parameters, context, this, dagChecker);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GeneratorExpressionContent::EvaluateParameters(
|
2016-05-16 17:34:04 +03:00
|
|
|
const cmGeneratorExpressionNode* node, const std::string& identifier,
|
|
|
|
cmGeneratorExpressionContext* context,
|
|
|
|
cmGeneratorExpressionDAGChecker* dagChecker,
|
|
|
|
std::vector<std::string>& parameters) const
|
2012-11-27 01:42:00 +04:00
|
|
|
{
|
2013-04-25 15:54:57 +04:00
|
|
|
const int numExpected = node->NumExpectedParameters();
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
|
|
|
|
pit = this->ParamChildren.begin();
|
|
|
|
const std::vector<
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator pend =
|
|
|
|
this->ParamChildren.end();
|
|
|
|
const bool acceptsArbitraryContent =
|
|
|
|
node->AcceptsArbitraryContentParameter();
|
|
|
|
int counter = 1;
|
|
|
|
for (; pit != pend; ++pit, ++counter) {
|
|
|
|
if (acceptsArbitraryContent && counter == numExpected) {
|
|
|
|
std::string lastParam = this->ProcessArbitraryContent(
|
|
|
|
node, identifier, context, dagChecker, pit);
|
|
|
|
parameters.push_back(lastParam);
|
|
|
|
return std::string();
|
2016-08-18 21:36:29 +03:00
|
|
|
}
|
|
|
|
std::string parameter;
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
|
|
|
|
pit->begin();
|
|
|
|
const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
|
|
|
|
pit->end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
parameter += (*it)->Evaluate(context, dagChecker);
|
|
|
|
if (context->HadError) {
|
|
|
|
return std::string();
|
2013-12-18 03:32:35 +04:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 21:36:29 +03:00
|
|
|
parameters.push_back(parameter);
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((numExpected > cmGeneratorExpressionNode::DynamicParameters &&
|
|
|
|
(unsigned int)numExpected != parameters.size())) {
|
|
|
|
if (numExpected == 0) {
|
2012-09-11 21:53:38 +04:00
|
|
|
reportError(context, this->GetOriginalExpression(),
|
|
|
|
"$<" + identifier + "> expression requires no parameters.");
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (numExpected == 1) {
|
|
|
|
reportError(context, this->GetOriginalExpression(), "$<" + identifier +
|
|
|
|
"> expression requires "
|
|
|
|
"exactly one parameter.");
|
|
|
|
} else {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2016-05-16 17:34:04 +03:00
|
|
|
e << "$<" + identifier + "> expression requires " << numExpected
|
|
|
|
<< " comma separated parameters, but got " << parameters.size()
|
|
|
|
<< " instead.";
|
2012-09-11 21:53:38 +04:00
|
|
|
reportError(context, this->GetOriginalExpression(), e.str());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return std::string();
|
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters &&
|
|
|
|
parameters.empty()) {
|
|
|
|
reportError(context, this->GetOriginalExpression(), "$<" + identifier +
|
|
|
|
"> expression requires at least one parameter.");
|
|
|
|
}
|
|
|
|
if (numExpected == cmGeneratorExpressionNode::OneOrZeroParameters &&
|
|
|
|
parameters.size() > 1) {
|
|
|
|
reportError(context, this->GetOriginalExpression(), "$<" + identifier +
|
|
|
|
"> expression requires one or zero parameters.");
|
|
|
|
}
|
2012-11-27 01:42:00 +04:00
|
|
|
return std::string();
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GeneratorExpressionContent::~GeneratorExpressionContent()
|
|
|
|
{
|
2015-01-04 18:35:26 +03:00
|
|
|
cmDeleteAll(this->IdentifierChildren);
|
|
|
|
std::for_each(this->ParamChildren.begin(), this->ParamChildren.end(),
|
|
|
|
cmDeleteAll<std::vector<cmGeneratorExpressionEvaluator*> >);
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|