cmake/Source/cmGeneratorExpression.cxx

415 lines
12 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. */
2009-10-04 10:30:41 +03:00
#include "cmGeneratorExpression.h"
2020-02-01 23:06:01 +01:00
#include <cassert>
#include <memory>
2017-04-14 19:02:05 +02:00
#include <utility>
2020-02-01 23:06:01 +01:00
#include "cmsys/RegularExpression.hxx"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorExpressionContext.h"
2018-04-23 21:13:27 +02:00
#include "cmGeneratorExpressionDAGChecker.h"
2013-03-16 19:13:01 +02:00
#include "cmGeneratorExpressionEvaluator.h"
#include "cmGeneratorExpressionLexer.h"
#include "cmGeneratorExpressionParser.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2009-10-04 10:30:41 +03:00
2019-11-11 23:01:05 +01:00
cmGeneratorExpression::cmGeneratorExpression(cmListFileBacktrace backtrace)
: Backtrace(std::move(backtrace))
2009-10-04 10:30:41 +03:00
{
}
2020-08-30 11:54:41 +02:00
cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression() = default;
2020-02-01 23:06:01 +01:00
cmGeneratorExpression::~cmGeneratorExpression() = default;
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCompiledGeneratorExpression> cmGeneratorExpression::Parse(
2020-02-01 23:06:01 +01:00
std::string input) const
2009-10-04 10:30:41 +03:00
{
2018-01-26 17:06:56 +01:00
return std::unique_ptr<cmCompiledGeneratorExpression>(
2020-02-01 23:06:01 +01:00
new cmCompiledGeneratorExpression(this->Backtrace, std::move(input)));
2009-10-04 10:30:41 +03:00
}
2020-02-01 23:06:01 +01:00
std::string cmGeneratorExpression::Evaluate(
std::string input, cmLocalGenerator* lg, const std::string& config,
cmGeneratorTarget const* headTarget,
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionDAGChecker* dagChecker,
2020-02-01 23:06:01 +01:00
cmGeneratorTarget const* currentTarget, std::string const& language)
2013-03-16 19:13:01 +02:00
{
2020-02-01 23:06:01 +01:00
if (Find(input) != std::string::npos) {
cmCompiledGeneratorExpression cge(cmListFileBacktrace(), std::move(input));
return cge.Evaluate(lg, config, headTarget, dagChecker, currentTarget,
2015-08-17 11:37:30 +02:00
language);
2020-02-01 23:06:01 +01:00
}
return input;
}
2018-10-28 12:09:07 +01:00
const std::string& cmCompiledGeneratorExpression::Evaluate(
2020-02-01 23:06:01 +01:00
cmLocalGenerator* lg, const std::string& config,
const cmGeneratorTarget* headTarget,
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionDAGChecker* dagChecker,
2020-02-01 23:06:01 +01:00
const cmGeneratorTarget* currentTarget, std::string const& language) const
2015-08-17 11:37:30 +02:00
{
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionContext context(
2020-02-01 23:06:01 +01:00
lg, config, this->Quiet, headTarget,
currentTarget ? currentTarget : headTarget, this->EvaluateForBuildsystem,
this->Backtrace, language);
2015-08-17 11:37:30 +02:00
return this->EvaluateWithContext(context, dagChecker);
}
2018-10-28 12:09:07 +01:00
const std::string& cmCompiledGeneratorExpression::EvaluateWithContext(
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionContext& context,
cmGeneratorExpressionDAGChecker* dagChecker) const
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
if (!this->NeedsEvaluation) {
2018-10-28 12:09:07 +01:00
return this->Input;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
this->Output.clear();
2013-03-16 19:13:01 +02:00
2020-08-30 11:54:41 +02:00
for (const auto& it : this->Evaluators) {
2019-11-11 23:01:05 +01:00
this->Output += it->Evaluate(&context, dagChecker);
2013-03-16 19:13:01 +02:00
2019-11-11 23:01:05 +01:00
this->SeenTargetProperties.insert(context.SeenTargetProperties.cbegin(),
context.SeenTargetProperties.cend());
2016-07-09 11:21:54 +02:00
if (context.HadError) {
2018-01-26 17:06:56 +01:00
this->Output.clear();
2013-03-16 19:13:01 +02:00
break;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->MaxLanguageStandard = context.MaxLanguageStandard;
2016-07-09 11:21:54 +02:00
if (!context.HadError) {
2013-03-16 19:13:01 +02:00
this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
2015-04-27 22:25:09 +02:00
this->HadHeadSensitiveCondition = context.HadHeadSensitiveCondition;
2020-08-30 11:54:41 +02:00
this->HadLinkLanguageSensitiveCondition =
context.HadLinkLanguageSensitiveCondition;
2015-04-27 22:25:09 +02:00
this->SourceSensitiveTargets = context.SourceSensitiveTargets;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
this->DependTargets = context.DependTargets;
this->AllTargetsSeen = context.AllTargets;
2018-10-28 12:09:07 +01:00
return this->Output;
2009-10-04 10:30:41 +03:00
}
2013-03-16 19:13:01 +02:00
cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
2019-11-11 23:01:05 +01:00
cmListFileBacktrace backtrace, std::string input)
: Backtrace(std::move(backtrace))
, Input(std::move(input))
2020-02-01 23:06:01 +01:00
, EvaluateForBuildsystem(false)
, Quiet(false)
2016-07-09 11:21:54 +02:00
, HadContextSensitiveCondition(false)
, HadHeadSensitiveCondition(false)
2020-08-30 11:54:41 +02:00
, HadLinkLanguageSensitiveCondition(false)
2009-10-04 10:30:41 +03:00
{
2013-03-16 19:13:01 +02:00
cmGeneratorExpressionLexer l;
2016-07-09 11:21:54 +02:00
std::vector<cmGeneratorExpressionToken> tokens = l.Tokenize(this->Input);
2014-08-03 19:52:23 +02:00
this->NeedsEvaluation = l.GetSawGeneratorExpression();
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
if (this->NeedsEvaluation) {
2013-03-16 19:13:01 +02:00
cmGeneratorExpressionParser p(tokens);
p.Parse(this->Evaluators);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
std::string cmGeneratorExpression::StripEmptyListElements(
2016-07-09 11:21:54 +02:00
const std::string& input)
2009-10-04 10:30:41 +03:00
{
2017-07-20 19:35:53 +02:00
if (input.find(';') == std::string::npos) {
2015-04-27 22:25:09 +02:00
return input;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::string result;
2015-04-27 22:25:09 +02:00
result.reserve(input.size());
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
const char* c = input.c_str();
const char* last = c;
2013-03-16 19:13:01 +02:00
bool skipSemiColons = true;
2016-07-09 11:21:54 +02:00
for (; *c; ++c) {
if (*c == ';') {
if (skipSemiColons) {
2015-04-27 22:25:09 +02:00
result.append(last, c - last);
last = c + 1;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
skipSemiColons = true;
} else {
2013-03-16 19:13:01 +02:00
skipSemiColons = false;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
result.append(last);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!result.empty() && *(result.end() - 1) == ';') {
2013-03-16 19:13:01 +02:00
result.resize(result.size() - 1);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return result;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
static std::string stripAllGeneratorExpressions(const std::string& input)
2009-10-04 10:30:41 +03:00
{
2013-03-16 19:13:01 +02:00
std::string result;
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
2013-11-03 12:27:13 +02:00
int nestingLevel = 0;
2017-07-20 19:35:53 +02:00
while ((pos = input.find("$<", lastPos)) != std::string::npos) {
2013-03-16 19:13:01 +02:00
result += input.substr(lastPos, pos - lastPos);
pos += 2;
2013-11-03 12:27:13 +02:00
nestingLevel = 1;
2016-07-09 11:21:54 +02:00
const char* c = input.c_str() + pos;
const char* const cStart = c;
for (; *c; ++c) {
2018-10-28 12:09:07 +01:00
if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
2013-03-16 19:13:01 +02:00
++nestingLevel;
++c;
continue;
2016-07-09 11:21:54 +02:00
}
if (c[0] == '>') {
2013-03-16 19:13:01 +02:00
--nestingLevel;
2016-07-09 11:21:54 +02:00
if (nestingLevel == 0) {
2013-03-16 19:13:01 +02:00
break;
}
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
const std::string::size_type traversed = (c - cStart) + 1;
2016-07-09 11:21:54 +02:00
if (!*c) {
2013-03-16 19:13:01 +02:00
result += "$<" + input.substr(pos, traversed);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
pos += traversed;
lastPos = pos;
2016-07-09 11:21:54 +02:00
}
if (nestingLevel == 0) {
2013-11-03 12:27:13 +02:00
result += input.substr(lastPos);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return cmGeneratorExpression::StripEmptyListElements(result);
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
static void prefixItems(const std::string& content, std::string& result,
const std::string& prefix)
2013-11-03 12:27:13 +02:00
{
std::vector<std::string> entries;
cmGeneratorExpression::Split(content, entries);
2016-07-09 11:21:54 +02:00
const char* sep = "";
2018-01-26 17:06:56 +01:00
for (std::string const& e : entries) {
2013-11-03 12:27:13 +02:00
result += sep;
sep = ";";
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(e) &&
2018-01-26 17:06:56 +01:00
cmGeneratorExpression::Find(e) != 0) {
2013-11-03 12:27:13 +02:00
result += prefix;
}
2018-01-26 17:06:56 +01:00
result += e;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
}
2016-07-09 11:21:54 +02:00
static std::string stripExportInterface(
const std::string& input, cmGeneratorExpression::PreprocessContext context,
bool resolveRelative)
2013-03-16 19:13:01 +02:00
{
std::string result;
2013-11-03 12:27:13 +02:00
int nestingLevel = 0;
2013-03-16 19:13:01 +02:00
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
2016-07-09 11:21:54 +02:00
while (true) {
2013-04-21 10:33:41 +03:00
std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
2016-07-09 11:21:54 +02:00
if (bPos == std::string::npos && iPos == std::string::npos) {
2013-04-21 10:33:41 +03:00
break;
2016-07-09 11:21:54 +02:00
}
2013-04-21 10:33:41 +03:00
2016-07-09 11:21:54 +02:00
if (bPos == std::string::npos) {
2013-04-21 10:33:41 +03:00
pos = iPos;
2016-07-09 11:21:54 +02:00
} else if (iPos == std::string::npos) {
2013-04-21 10:33:41 +03:00
pos = bPos;
2016-07-09 11:21:54 +02:00
} else {
2013-04-21 10:33:41 +03:00
pos = (bPos < iPos) ? bPos : iPos;
2016-07-09 11:21:54 +02:00
}
2013-04-21 10:33:41 +03:00
2013-03-16 19:13:01 +02:00
result += input.substr(lastPos, pos - lastPos);
const bool gotInstallInterface = input[pos + 2] == 'I';
pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
: sizeof("$<BUILD_INTERFACE:") - 1;
2013-11-03 12:27:13 +02:00
nestingLevel = 1;
2016-07-09 11:21:54 +02:00
const char* c = input.c_str() + pos;
const char* const cStart = c;
for (; *c; ++c) {
2018-10-28 12:09:07 +01:00
if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
2013-03-16 19:13:01 +02:00
++nestingLevel;
++c;
continue;
2016-07-09 11:21:54 +02:00
}
if (c[0] == '>') {
2013-03-16 19:13:01 +02:00
--nestingLevel;
2016-07-09 11:21:54 +02:00
if (nestingLevel != 0) {
2013-03-16 19:13:01 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
if (context == cmGeneratorExpression::BuildInterface &&
!gotInstallInterface) {
2013-03-16 19:13:01 +02:00
result += input.substr(pos, c - cStart);
2016-07-09 11:21:54 +02:00
} else if (context == cmGeneratorExpression::InstallInterface &&
gotInstallInterface) {
2013-11-03 12:27:13 +02:00
const std::string content = input.substr(pos, c - cStart);
2016-07-09 11:21:54 +02:00
if (resolveRelative) {
2013-11-03 12:27:13 +02:00
prefixItems(content, result, "${_IMPORT_PREFIX}/");
2016-07-09 11:21:54 +02:00
} else {
2013-11-03 12:27:13 +02:00
result += content;
2013-03-16 19:13:01 +02:00
}
}
2016-07-09 11:21:54 +02:00
break;
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
const std::string::size_type traversed = (c - cStart) + 1;
2016-07-09 11:21:54 +02:00
if (!*c) {
2013-03-16 19:13:01 +02:00
result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
2016-07-09 11:21:54 +02:00
: "$<BUILD_INTERFACE:") +
input.substr(pos, traversed);
}
2013-03-16 19:13:01 +02:00
pos += traversed;
lastPos = pos;
2016-07-09 11:21:54 +02:00
}
if (nestingLevel == 0) {
2013-11-03 12:27:13 +02:00
result += input.substr(lastPos);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return cmGeneratorExpression::StripEmptyListElements(result);
}
2016-07-09 11:21:54 +02:00
void cmGeneratorExpression::Split(const std::string& input,
std::vector<std::string>& output)
2013-03-16 19:13:01 +02:00
{
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
2017-07-20 19:35:53 +02:00
while ((pos = input.find("$<", lastPos)) != std::string::npos) {
2013-03-16 19:13:01 +02:00
std::string part = input.substr(lastPos, pos - lastPos);
std::string preGenex;
2016-07-09 11:21:54 +02:00
if (!part.empty()) {
std::string::size_type startPos = input.rfind(';', pos);
if (startPos == std::string::npos) {
2013-03-16 19:13:01 +02:00
preGenex = part;
2018-01-26 17:06:56 +01:00
part.clear();
2016-07-09 11:21:54 +02:00
} else if (startPos != pos - 1 && startPos >= lastPos) {
2013-03-16 19:13:01 +02:00
part = input.substr(lastPos, startPos - lastPos);
preGenex = input.substr(startPos + 1, pos - startPos - 1);
2016-07-09 11:21:54 +02:00
}
if (!part.empty()) {
2020-02-01 23:06:01 +01:00
cmExpandList(part, output);
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
pos += 2;
int nestingLevel = 1;
2016-07-09 11:21:54 +02:00
const char* c = input.c_str() + pos;
const char* const cStart = c;
for (; *c; ++c) {
2018-10-28 12:09:07 +01:00
if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
2013-03-16 19:13:01 +02:00
++nestingLevel;
++c;
continue;
2016-07-09 11:21:54 +02:00
}
if (c[0] == '>') {
2013-03-16 19:13:01 +02:00
--nestingLevel;
2016-07-09 11:21:54 +02:00
if (nestingLevel == 0) {
2013-03-16 19:13:01 +02:00
break;
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
for (; *c; ++c) {
2013-03-16 19:13:01 +02:00
// Capture the part after the genex and before the next ';'
2016-07-09 11:21:54 +02:00
if (c[0] == ';') {
2013-03-16 19:13:01 +02:00
--c;
break;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
const std::string::size_type traversed = (c - cStart) + 1;
output.push_back(preGenex + "$<" + input.substr(pos, traversed));
pos += traversed;
lastPos = pos;
2016-07-09 11:21:54 +02:00
}
if (lastPos < input.size()) {
2020-02-01 23:06:01 +01:00
cmExpandList(input.substr(lastPos), output);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
std::string cmGeneratorExpression::Preprocess(const std::string& input,
2013-11-03 12:27:13 +02:00
PreprocessContext context,
bool resolveRelative)
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
if (context == StripAllGeneratorExpressions) {
2013-03-16 19:13:01 +02:00
return stripAllGeneratorExpressions(input);
2016-10-30 18:24:19 +01:00
}
if (context == BuildInterface || context == InstallInterface) {
2013-11-03 12:27:13 +02:00
return stripExportInterface(input, context, resolveRelative);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2017-04-14 19:02:05 +02:00
assert(false &&
"cmGeneratorExpression::Preprocess called with invalid args");
2013-03-16 19:13:01 +02:00
return std::string();
}
2016-07-09 11:21:54 +02:00
std::string::size_type cmGeneratorExpression::Find(const std::string& input)
2013-03-16 19:13:01 +02:00
{
const std::string::size_type openpos = input.find("$<");
2016-07-09 11:21:54 +02:00
if (openpos != std::string::npos &&
input.find('>', openpos) != std::string::npos) {
2013-03-16 19:13:01 +02:00
return openpos;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return std::string::npos;
}
2016-07-09 11:21:54 +02:00
bool cmGeneratorExpression::IsValidTargetName(const std::string& input)
2013-03-16 19:13:01 +02:00
{
// The ':' is supported to allow use with IMPORTED targets. At least
// Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
2015-04-27 22:25:09 +02:00
static cmsys::RegularExpression targetNameValidator("^[A-Za-z0-9_.:+-]+$");
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
return targetNameValidator.find(input);
}
2020-08-30 11:54:41 +02:00
void cmGeneratorExpression::ReplaceInstallPrefix(
std::string& input, const std::string& replacement)
{
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) !=
std::string::npos) {
std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
input.replace(pos, endPos - pos, replacement);
lastPos = endPos;
}
}
2016-07-09 11:21:54 +02:00
void cmCompiledGeneratorExpression::GetMaxLanguageStandard(
const cmGeneratorTarget* tgt, std::map<std::string, std::string>& mapping)
2015-04-27 22:25:09 +02:00
{
2020-02-01 23:06:01 +01:00
auto it = this->MaxLanguageStandard.find(tgt);
2016-07-09 11:21:54 +02:00
if (it != this->MaxLanguageStandard.end()) {
2015-04-27 22:25:09 +02:00
mapping = it->second;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2018-04-23 21:13:27 +02:00
2018-10-28 12:09:07 +01:00
const std::string& cmGeneratorExpressionInterpreter::Evaluate(
2020-02-01 23:06:01 +01:00
std::string expression, const std::string& property)
2018-04-23 21:13:27 +02:00
{
2018-10-28 12:09:07 +01:00
this->CompiledGeneratorExpression =
2020-02-01 23:06:01 +01:00
this->GeneratorExpression.Parse(std::move(expression));
2018-04-23 21:13:27 +02:00
// Specify COMPILE_OPTIONS to DAGchecker, same semantic as COMPILE_FLAGS
cmGeneratorExpressionDAGChecker dagChecker(
2018-10-28 12:09:07 +01:00
this->HeadTarget,
property == "COMPILE_FLAGS" ? "COMPILE_OPTIONS" : property, nullptr,
nullptr);
2018-04-23 21:13:27 +02:00
2018-10-28 12:09:07 +01:00
return this->CompiledGeneratorExpression->Evaluate(
2020-02-01 23:06:01 +01:00
this->LocalGenerator, this->Config, this->HeadTarget, &dagChecker, nullptr,
2018-10-28 12:09:07 +01:00
this->Language);
2018-04-23 21:13:27 +02:00
}
2020-02-01 23:06:01 +01:00
const std::string& cmGeneratorExpressionInterpreter::Evaluate(
const char* expression, const std::string& property)
{
return this->Evaluate(std::string(expression ? expression : ""), property);
}