cmake/Source/cmGeneratorExpressionEvaluator.cxx

204 lines
6.7 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-03-16 19:13:01 +02:00
#include "cmGeneratorExpressionEvaluator.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <sstream>
2023-05-23 16:38:00 +02:00
#ifndef CMAKE_BOOTSTRAP
# include <cm3p/json/value.h>
#endif
2016-10-30 18:24:19 +01:00
#include "cmGeneratorExpressionContext.h"
2015-08-17 11:37:30 +02:00
#include "cmGeneratorExpressionNode.h"
2023-05-23 16:38:00 +02:00
#include "cmLocalGenerator.h"
#include "cmake.h"
2013-03-16 19:13:01 +02:00
GeneratorExpressionContent::GeneratorExpressionContent(
2016-07-09 11:21:54 +02:00
const char* startContent, size_t length)
: StartContent(startContent)
, ContentLength(length)
2013-03-16 19:13:01 +02:00
{
}
2020-08-30 11:54:41 +02:00
GeneratorExpressionContent::~GeneratorExpressionContent() = default;
2013-03-16 19:13:01 +02:00
std::string GeneratorExpressionContent::GetOriginalExpression() const
{
return std::string(this->StartContent, this->ContentLength);
}
2013-11-03 12:27:13 +02:00
std::string GeneratorExpressionContent::ProcessArbitraryContent(
2016-07-09 11:21:54 +02:00
const cmGeneratorExpressionNode* node, const std::string& identifier,
cmGeneratorExpressionContext* context,
cmGeneratorExpressionDAGChecker* dagChecker,
2020-08-30 11:54:41 +02:00
std::vector<cmGeneratorExpressionEvaluatorVector>::const_iterator pit) const
2013-11-03 12:27:13 +02:00
{
std::string result;
2020-02-01 23:06:01 +01:00
const auto pend = this->ParamChildren.end();
2016-07-09 11:21:54 +02:00
for (; pit != pend; ++pit) {
2021-09-14 00:13:48 +02:00
for (const auto& pExprEval : *pit) {
2016-07-09 11:21:54 +02:00
if (node->RequiresLiteralInput()) {
2019-11-11 23:01:05 +01:00
if (pExprEval->GetType() != cmGeneratorExpressionEvaluator::Text) {
2018-08-09 18:06:22 +02:00
reportError(context, this->GetOriginalExpression(),
"$<" + identifier +
"> expression requires literal input.");
2013-11-03 12:27:13 +02:00
return std::string();
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
result += pExprEval->Evaluate(context, dagChecker);
2016-07-09 11:21:54 +02:00
if (context->HadError) {
2013-11-03 12:27:13 +02:00
return std::string();
}
}
2016-07-09 11:21:54 +02:00
if ((pit + 1) != pend) {
result += ",";
}
}
if (node->RequiresLiteralInput()) {
2013-11-03 12:27:13 +02:00
std::vector<std::string> parameters;
parameters.push_back(result);
return node->Evaluate(parameters, context, this, dagChecker);
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
return result;
}
2013-03-16 19:13:01 +02:00
std::string GeneratorExpressionContent::Evaluate(
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionContext* context,
cmGeneratorExpressionDAGChecker* dagChecker) const
2013-03-16 19:13:01 +02:00
{
2023-05-23 16:38:00 +02:00
#ifndef CMAKE_BOOTSTRAP
auto evalProfilingRAII =
context->LG->GetCMakeInstance()->CreateProfilingEntry(
"genex_eval", this->GetOriginalExpression());
#endif
2013-03-16 19:13:01 +02:00
std::string identifier;
{
2021-09-14 00:13:48 +02:00
for (const auto& pExprEval : this->IdentifierChildren) {
2019-11-11 23:01:05 +01:00
identifier += pExprEval->Evaluate(context, dagChecker);
2016-07-09 11:21:54 +02:00
if (context->HadError) {
return std::string();
2013-03-16 19:13:01 +02:00
}
}
}
2016-07-09 11:21:54 +02:00
const cmGeneratorExpressionNode* node =
cmGeneratorExpressionNode::GetNode(identifier);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!node) {
2013-03-16 19:13:01 +02:00
reportError(context, this->GetOriginalExpression(),
2016-07-09 11:21:54 +02:00
"Expression did not evaluate to a known generator expression");
2013-03-16 19:13:01 +02:00
return std::string();
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!node->GeneratesContent()) {
if (node->NumExpectedParameters() == 1 &&
node->AcceptsArbitraryContentParameter()) {
if (this->ParamChildren.empty()) {
2013-03-16 19:13:01 +02:00
reportError(context, this->GetOriginalExpression(),
2016-07-09 11:21:54 +02:00
"$<" + identifier + "> expression requires a parameter.");
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
std::vector<std::string> parameters;
this->EvaluateParameters(node, identifier, context, dagChecker,
parameters);
}
2016-07-09 11:21:54 +02:00
return std::string();
}
2013-03-16 19:13:01 +02:00
std::vector<std::string> parameters;
this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
2016-07-09 11:21:54 +02:00
if (context->HadError) {
2013-03-16 19:13:01 +02:00
return std::string();
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2023-05-23 16:38:00 +02:00
{
#ifndef CMAKE_BOOTSTRAP
auto execProfilingRAII =
context->LG->GetCMakeInstance()->CreateProfilingEntry(
"genex_exec", identifier, [&parameters]() -> Json::Value {
Json::Value args = Json::objectValue;
if (!parameters.empty()) {
args["genexArgs"] = Json::arrayValue;
for (auto const& parameter : parameters) {
args["genexArgs"].append(parameter);
}
}
return args;
});
#endif
return node->Evaluate(parameters, context, this, dagChecker);
}
2013-03-16 19:13:01 +02:00
}
std::string GeneratorExpressionContent::EvaluateParameters(
2016-07-09 11:21:54 +02:00
const cmGeneratorExpressionNode* node, const std::string& identifier,
cmGeneratorExpressionContext* context,
cmGeneratorExpressionDAGChecker* dagChecker,
std::vector<std::string>& parameters) const
2013-03-16 19:13:01 +02:00
{
2013-11-03 12:27:13 +02:00
const int numExpected = node->NumExpectedParameters();
2013-03-16 19:13:01 +02:00
{
2020-02-01 23:06:01 +01:00
auto pit = this->ParamChildren.begin();
const auto pend = this->ParamChildren.end();
2016-07-09 11:21:54 +02:00
const bool acceptsArbitraryContent =
node->AcceptsArbitraryContentParameter();
int counter = 1;
for (; pit != pend; ++pit, ++counter) {
if (acceptsArbitraryContent && counter == numExpected) {
2018-04-23 21:13:27 +02:00
parameters.push_back(this->ProcessArbitraryContent(
node, identifier, context, dagChecker, pit));
2016-07-09 11:21:54 +02:00
return std::string();
2016-10-30 18:24:19 +01:00
}
std::string parameter;
2021-09-14 00:13:48 +02:00
for (const auto& pExprEval : *pit) {
2019-11-11 23:01:05 +01:00
parameter += pExprEval->Evaluate(context, dagChecker);
2016-10-30 18:24:19 +01:00
if (context->HadError) {
return std::string();
2014-08-03 19:52:23 +02:00
}
}
2018-04-23 21:13:27 +02:00
parameters.push_back(std::move(parameter));
2013-03-16 19:13:01 +02:00
}
}
2016-07-09 11:21:54 +02:00
if ((numExpected > cmGeneratorExpressionNode::DynamicParameters &&
2018-01-26 17:06:56 +01:00
static_cast<unsigned int>(numExpected) != parameters.size())) {
2016-07-09 11:21:54 +02:00
if (numExpected == 0) {
2013-03-16 19:13:01 +02:00
reportError(context, this->GetOriginalExpression(),
"$<" + identifier + "> expression requires no parameters.");
2016-07-09 11:21:54 +02:00
} else if (numExpected == 1) {
2018-08-09 18:06:22 +02:00
reportError(context, this->GetOriginalExpression(),
"$<" + identifier +
2016-07-09 11:21:54 +02:00
"> expression requires "
"exactly one parameter.");
} else {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2016-07-09 11:21:54 +02:00
e << "$<" + identifier + "> expression requires " << numExpected
<< " comma separated parameters, but got " << parameters.size()
<< " instead.";
2013-03-16 19:13:01 +02:00
reportError(context, this->GetOriginalExpression(), e.str());
}
2016-07-09 11:21:54 +02:00
return std::string();
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters &&
parameters.empty()) {
2018-08-09 18:06:22 +02:00
reportError(context, this->GetOriginalExpression(),
"$<" + identifier +
2016-07-09 11:21:54 +02:00
"> expression requires at least one parameter.");
2019-11-11 23:01:05 +01:00
} else if (numExpected == cmGeneratorExpressionNode::TwoOrMoreParameters &&
parameters.size() < 2) {
reportError(context, this->GetOriginalExpression(),
"$<" + identifier +
"> expression requires at least two parameters.");
} else if (numExpected == cmGeneratorExpressionNode::OneOrZeroParameters &&
parameters.size() > 1) {
2018-08-09 18:06:22 +02:00
reportError(context, this->GetOriginalExpression(),
"$<" + identifier +
2016-07-09 11:21:54 +02:00
"> expression requires one or zero parameters.");
}
2013-03-16 19:13:01 +02:00
return std::string();
}