1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
14 #include "cmListFileCache.h"
16 class cmCompiledGeneratorExpression;
17 class cmGeneratorTarget;
18 class cmLocalGenerator;
19 struct cmGeneratorExpressionContext;
20 struct cmGeneratorExpressionDAGChecker;
21 struct cmGeneratorExpressionEvaluator;
23 /** \class cmGeneratorExpression
24 * \brief Evaluate generate-time query expression syntax.
26 * cmGeneratorExpression instances are used by build system generator
27 * implementations to evaluate the $<> generator expression syntax.
28 * Generator expressions are evaluated just before the generate step
29 * writes strings into the build system. They have knowledge of the
30 * build configuration which is not available at configure time.
32 class cmGeneratorExpression
36 cmGeneratorExpression(cmListFileBacktrace backtrace = cmListFileBacktrace());
37 ~cmGeneratorExpression();
39 cmGeneratorExpression(cmGeneratorExpression const&) = delete;
40 cmGeneratorExpression& operator=(cmGeneratorExpression const&) = delete;
42 std::unique_ptr<cmCompiledGeneratorExpression> Parse(
43 std::string input) const;
45 static std::string Evaluate(
46 std::string input, cmLocalGenerator* lg, const std::string& config,
47 cmGeneratorTarget const* headTarget = nullptr,
48 cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
49 cmGeneratorTarget const* currentTarget = nullptr,
50 std::string const& language = std::string());
52 enum PreprocessContext
54 StripAllGeneratorExpressions,
59 static std::string Preprocess(const std::string& input,
60 PreprocessContext context,
61 bool resolveRelative = false);
63 static void Split(const std::string& input,
64 std::vector<std::string>& output);
66 static std::string::size_type Find(const std::string& input);
68 static bool IsValidTargetName(const std::string& input);
70 static std::string StripEmptyListElements(const std::string& input);
72 static inline bool StartsWithGeneratorExpression(const std::string& input)
74 return input.length() >= 2 && input[0] == '$' && input[1] == '<';
76 static inline bool StartsWithGeneratorExpression(const char* input)
78 return input != nullptr && input[0] == '$' && input[1] == '<';
81 static void ReplaceInstallPrefix(std::string& input,
82 const std::string& replacement);
85 cmListFileBacktrace Backtrace;
88 class cmCompiledGeneratorExpression
91 ~cmCompiledGeneratorExpression();
93 cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
94 cmCompiledGeneratorExpression& operator=(
95 cmCompiledGeneratorExpression const&) = delete;
97 const std::string& Evaluate(
98 cmLocalGenerator* lg, const std::string& config,
99 cmGeneratorTarget const* headTarget = nullptr,
100 cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
101 cmGeneratorTarget const* currentTarget = nullptr,
102 std::string const& language = std::string()) const;
104 /** Get set of targets found during evaluations. */
105 std::set<cmGeneratorTarget*> const& GetTargets() const
107 return this->DependTargets;
110 std::set<std::string> const& GetSeenTargetProperties() const
112 return this->SeenTargetProperties;
115 std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
117 return this->AllTargetsSeen;
120 std::string const& GetInput() const { return this->Input; }
122 cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
123 bool GetHadContextSensitiveCondition() const
125 return this->HadContextSensitiveCondition;
127 bool GetHadHeadSensitiveCondition() const
129 return this->HadHeadSensitiveCondition;
131 bool GetHadLinkLanguageSensitiveCondition() const
133 return this->HadLinkLanguageSensitiveCondition;
135 std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
137 return this->SourceSensitiveTargets;
140 void SetEvaluateForBuildsystem(bool eval)
142 this->EvaluateForBuildsystem = eval;
145 void SetQuiet(bool quiet) { this->Quiet = quiet; }
147 void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
148 std::map<std::string, std::string>& mapping);
151 const std::string& EvaluateWithContext(
152 cmGeneratorExpressionContext& context,
153 cmGeneratorExpressionDAGChecker* dagChecker) const;
155 cmCompiledGeneratorExpression(cmListFileBacktrace backtrace,
158 friend class cmGeneratorExpression;
160 cmListFileBacktrace Backtrace;
161 std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
162 const std::string Input;
163 bool NeedsEvaluation;
164 bool EvaluateForBuildsystem = false;
167 mutable std::set<cmGeneratorTarget*> DependTargets;
168 mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
169 mutable std::set<std::string> SeenTargetProperties;
170 mutable std::map<cmGeneratorTarget const*,
171 std::map<std::string, std::string>>
173 mutable std::string Output;
174 mutable bool HadContextSensitiveCondition = false;
175 mutable bool HadHeadSensitiveCondition = false;
176 mutable bool HadLinkLanguageSensitiveCondition = false;
177 mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
180 class cmGeneratorExpressionInterpreter
183 cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
185 cmGeneratorTarget const* headTarget,
186 std::string language = std::string())
187 : LocalGenerator(localGenerator)
188 , Config(std::move(config))
189 , HeadTarget(headTarget)
190 , Language(std::move(language))
194 cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
196 cmGeneratorExpressionInterpreter& operator=(
197 cmGeneratorExpressionInterpreter const&) = delete;
199 const std::string& Evaluate(std::string expression,
200 const std::string& property);
203 cmGeneratorExpression GeneratorExpression;
204 std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
205 cmLocalGenerator* LocalGenerator = nullptr;
207 cmGeneratorTarget const* HeadTarget = nullptr;
208 std::string Language;