cmake/Source/cmGeneratorExpression.h

215 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. */
2021-09-14 00:13:48 +02:00
#pragma once
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <map>
2020-02-01 23:06:01 +01:00
#include <memory>
2016-10-30 18:24:19 +01:00
#include <set>
#include <string>
2019-11-11 23:01:05 +01:00
#include <utility>
2016-10-30 18:24:19 +01:00
#include <vector>
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
#include "cmListFileCache.h"
2023-05-23 16:38:00 +02:00
#include "cmLocalGenerator.h"
2020-02-01 23:06:01 +01:00
2023-05-23 16:38:00 +02:00
class cmake;
2016-10-30 18:24:19 +01:00
class cmCompiledGeneratorExpression;
2016-03-13 13:35:51 +01:00
class cmGeneratorTarget;
2015-08-17 11:37:30 +02:00
struct cmGeneratorExpressionContext;
2013-03-16 19:13:01 +02:00
struct cmGeneratorExpressionDAGChecker;
2016-10-30 18:24:19 +01:00
struct cmGeneratorExpressionEvaluator;
2013-03-16 19:13:01 +02:00
2009-10-04 10:30:41 +03:00
/** \class cmGeneratorExpression
* \brief Evaluate generate-time query expression syntax.
*
* cmGeneratorExpression instances are used by build system generator
* implementations to evaluate the $<> generator expression syntax.
* Generator expressions are evaluated just before the generate step
* writes strings into the build system. They have knowledge of the
* build configuration which is not available at configure time.
*/
class cmGeneratorExpression
{
public:
2013-03-16 19:13:01 +02:00
/** Construct. */
2023-05-23 16:38:00 +02:00
cmGeneratorExpression(cmake& cmakeInstance,
cmListFileBacktrace backtrace = cmListFileBacktrace());
2013-03-16 19:13:01 +02:00
~cmGeneratorExpression();
2019-11-11 23:01:05 +01:00
cmGeneratorExpression(cmGeneratorExpression const&) = delete;
cmGeneratorExpression& operator=(cmGeneratorExpression const&) = delete;
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmCompiledGeneratorExpression> Parse(
2020-02-01 23:06:01 +01:00
std::string input) const;
static std::string Evaluate(
std::string input, cmLocalGenerator* lg, const std::string& config,
cmGeneratorTarget const* headTarget = nullptr,
cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
cmGeneratorTarget const* currentTarget = nullptr,
std::string const& language = std::string());
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
enum PreprocessContext
{
2013-03-16 19:13:01 +02:00
StripAllGeneratorExpressions,
BuildInterface,
InstallInterface
};
2016-07-09 11:21:54 +02:00
static std::string Preprocess(const std::string& input,
2013-11-03 12:27:13 +02:00
PreprocessContext context,
bool resolveRelative = false);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
static void Split(const std::string& input,
std::vector<std::string>& output);
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
static std::string::size_type Find(const std::string& input);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
static bool IsValidTargetName(const std::string& input);
static std::string StripEmptyListElements(const std::string& input);
2013-03-16 19:13:01 +02:00
2018-10-28 12:09:07 +01:00
static inline bool StartsWithGeneratorExpression(const std::string& input)
{
return input.length() >= 2 && input[0] == '$' && input[1] == '<';
}
static inline bool StartsWithGeneratorExpression(const char* input)
{
return input != nullptr && input[0] == '$' && input[1] == '<';
}
2020-08-30 11:54:41 +02:00
static void ReplaceInstallPrefix(std::string& input,
const std::string& replacement);
2013-03-16 19:13:01 +02:00
private:
2023-05-23 16:38:00 +02:00
cmake& CMakeInstance;
2015-11-17 17:22:37 +01:00
cmListFileBacktrace Backtrace;
2013-03-16 19:13:01 +02:00
};
class cmCompiledGeneratorExpression
{
public:
2019-11-11 23:01:05 +01:00
~cmCompiledGeneratorExpression();
cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
cmCompiledGeneratorExpression& operator=(
cmCompiledGeneratorExpression const&) = delete;
2018-10-28 12:09:07 +01:00
const std::string& Evaluate(
2020-02-01 23:06:01 +01:00
cmLocalGenerator* lg, const std::string& config,
2018-10-28 12:09:07 +01:00
cmGeneratorTarget const* headTarget = nullptr,
cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
2020-02-01 23:06:01 +01:00
cmGeneratorTarget const* currentTarget = nullptr,
2018-10-28 12:09:07 +01:00
std::string const& language = std::string()) const;
2011-01-16 11:35:12 +01:00
/** Get set of targets found during evaluations. */
2016-03-13 13:35:51 +01:00
std::set<cmGeneratorTarget*> const& GetTargets() const
2016-07-09 11:21:54 +02:00
{
return this->DependTargets;
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
std::set<std::string> const& GetSeenTargetProperties() const
2016-07-09 11:21:54 +02:00
{
return this->SeenTargetProperties;
}
2013-03-16 19:13:01 +02:00
2016-03-13 13:35:51 +01:00
std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
2016-07-09 11:21:54 +02:00
{
return this->AllTargetsSeen;
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
std::string const& GetInput() const { return this->Input; }
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
2013-03-16 19:13:01 +02:00
bool GetHadContextSensitiveCondition() const
{
return this->HadContextSensitiveCondition;
}
2015-04-27 22:25:09 +02:00
bool GetHadHeadSensitiveCondition() const
{
return this->HadHeadSensitiveCondition;
}
2020-08-30 11:54:41 +02:00
bool GetHadLinkLanguageSensitiveCondition() const
{
return this->HadLinkLanguageSensitiveCondition;
}
2016-03-13 13:35:51 +01:00
std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
2015-04-27 22:25:09 +02:00
{
return this->SourceSensitiveTargets;
}
void SetEvaluateForBuildsystem(bool eval)
{
this->EvaluateForBuildsystem = eval;
}
2020-02-01 23:06:01 +01:00
void SetQuiet(bool quiet) { this->Quiet = quiet; }
2016-03-13 13:35:51 +01:00
void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
2016-07-09 11:21:54 +02:00
std::map<std::string, std::string>& mapping);
2013-03-16 19:13:01 +02:00
2009-10-04 10:30:41 +03:00
private:
2018-10-28 12:09:07 +01:00
const std::string& EvaluateWithContext(
2016-07-09 11:21:54 +02:00
cmGeneratorExpressionContext& context,
cmGeneratorExpressionDAGChecker* dagChecker) const;
2015-08-17 11:37:30 +02:00
2023-05-23 16:38:00 +02:00
cmCompiledGeneratorExpression(cmake& cmakeInstance,
cmListFileBacktrace backtrace,
2019-11-11 23:01:05 +01:00
std::string input);
2013-03-16 19:13:01 +02:00
friend class cmGeneratorExpression;
cmListFileBacktrace Backtrace;
2020-08-30 11:54:41 +02:00
std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
2013-03-16 19:13:01 +02:00
const std::string Input;
2014-08-03 19:52:23 +02:00
bool NeedsEvaluation;
2022-08-04 22:12:04 +02:00
bool EvaluateForBuildsystem = false;
bool Quiet = false;
2013-03-16 19:13:01 +02:00
2016-03-13 13:35:51 +01:00
mutable std::set<cmGeneratorTarget*> DependTargets;
mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
2015-04-27 22:25:09 +02:00
mutable std::set<std::string> SeenTargetProperties;
2016-03-13 13:35:51 +01:00
mutable std::map<cmGeneratorTarget const*,
2018-01-26 17:06:56 +01:00
std::map<std::string, std::string>>
2016-07-09 11:21:54 +02:00
MaxLanguageStandard;
2013-03-16 19:13:01 +02:00
mutable std::string Output;
2022-08-04 22:12:04 +02:00
mutable bool HadContextSensitiveCondition = false;
mutable bool HadHeadSensitiveCondition = false;
mutable bool HadLinkLanguageSensitiveCondition = false;
2016-07-09 11:21:54 +02:00
mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
2009-10-04 10:30:41 +03:00
};
2013-03-16 19:13:01 +02:00
2018-04-23 21:13:27 +02:00
class cmGeneratorExpressionInterpreter
{
public:
cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
2019-11-11 23:01:05 +01:00
std::string config,
2018-10-28 12:09:07 +01:00
cmGeneratorTarget const* headTarget,
2020-02-01 23:06:01 +01:00
std::string language = std::string())
2023-05-23 16:38:00 +02:00
: GeneratorExpression(*localGenerator->GetCMakeInstance())
, LocalGenerator(localGenerator)
2019-11-11 23:01:05 +01:00
, Config(std::move(config))
2018-10-28 12:09:07 +01:00
, HeadTarget(headTarget)
2020-02-01 23:06:01 +01:00
, Language(std::move(language))
2018-04-23 21:13:27 +02:00
{
}
2019-11-11 23:01:05 +01:00
cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
delete;
cmGeneratorExpressionInterpreter& operator=(
cmGeneratorExpressionInterpreter const&) = delete;
2020-02-01 23:06:01 +01:00
const std::string& Evaluate(std::string expression,
const std::string& property);
2018-04-23 21:13:27 +02:00
protected:
cmGeneratorExpression GeneratorExpression;
std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
cmLocalGenerator* LocalGenerator = nullptr;
std::string Config;
2018-10-28 12:09:07 +01:00
cmGeneratorTarget const* HeadTarget = nullptr;
2018-04-23 21:13:27 +02:00
std::string Language;
};