Merge branch 'release-3.31'
[kiteware-cmake.git] / Source / cmGeneratorExpression.h
blob134930804f226022b8f47e8bd2e29199255f8674
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #pragma once
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include <cm/string_view>
16 #include "cmListFileCache.h"
17 #include "cmLocalGenerator.h"
19 class cmake;
20 class cmCompiledGeneratorExpression;
21 class cmGeneratorTarget;
22 struct cmGeneratorExpressionDAGChecker;
23 struct cmGeneratorExpressionEvaluator;
25 /** \class cmGeneratorExpression
26 * \brief Evaluate generate-time query expression syntax.
28 * cmGeneratorExpression instances are used by build system generator
29 * implementations to evaluate the $<> generator expression syntax.
30 * Generator expressions are evaluated just before the generate step
31 * writes strings into the build system. They have knowledge of the
32 * build configuration which is not available at configure time.
34 class cmGeneratorExpression
36 public:
37 /** Construct. */
38 cmGeneratorExpression(cmake& cmakeInstance,
39 cmListFileBacktrace backtrace = cmListFileBacktrace());
40 ~cmGeneratorExpression();
42 cmGeneratorExpression(cmGeneratorExpression const&) = delete;
43 cmGeneratorExpression& operator=(cmGeneratorExpression const&) = delete;
45 std::unique_ptr<cmCompiledGeneratorExpression> Parse(
46 std::string input) const;
48 static std::string Evaluate(
49 std::string input, cmLocalGenerator* lg, const std::string& config,
50 cmGeneratorTarget const* headTarget = nullptr,
51 cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
52 cmGeneratorTarget const* currentTarget = nullptr,
53 std::string const& language = std::string());
55 enum PreprocessContext
57 StripAllGeneratorExpressions,
58 BuildInterface,
59 InstallInterface
62 static std::string Preprocess(const std::string& input,
63 PreprocessContext context,
64 cm::string_view importPrefix = {});
66 static void Split(const std::string& input,
67 std::vector<std::string>& output);
69 static cm::string_view::size_type Find(const cm::string_view& input);
71 static bool IsValidTargetName(const std::string& input);
73 static std::string StripEmptyListElements(const std::string& input);
75 static inline bool StartsWithGeneratorExpression(const std::string& input)
77 return input.length() >= 2 && input[0] == '$' && input[1] == '<';
79 static inline bool StartsWithGeneratorExpression(const char* input)
81 return input && input[0] == '$' && input[1] == '<';
84 static void ReplaceInstallPrefix(std::string& input,
85 const std::string& replacement);
87 private:
88 cmake& CMakeInstance;
89 cmListFileBacktrace Backtrace;
92 class cmCompiledGeneratorExpression
94 public:
95 ~cmCompiledGeneratorExpression();
97 cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
98 cmCompiledGeneratorExpression& operator=(
99 cmCompiledGeneratorExpression const&) = delete;
101 const std::string& Evaluate(
102 cmLocalGenerator* lg, const std::string& config,
103 cmGeneratorTarget const* headTarget = nullptr,
104 cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
105 cmGeneratorTarget const* currentTarget = nullptr,
106 std::string const& language = std::string()) const;
108 /** Get set of targets found during evaluations. */
109 std::set<cmGeneratorTarget*> const& GetTargets() const
111 return this->DependTargets;
114 std::set<std::string> const& GetSeenTargetProperties() const
116 return this->SeenTargetProperties;
119 std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
121 return this->AllTargetsSeen;
124 std::string const& GetInput() const { return this->Input; }
126 cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
127 bool GetHadContextSensitiveCondition() const
129 return this->HadContextSensitiveCondition;
131 bool GetHadHeadSensitiveCondition() const
133 return this->HadHeadSensitiveCondition;
135 bool GetHadLinkLanguageSensitiveCondition() const
137 return this->HadLinkLanguageSensitiveCondition;
139 std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
141 return this->SourceSensitiveTargets;
144 void SetEvaluateForBuildsystem(bool eval)
146 this->EvaluateForBuildsystem = eval;
149 void SetQuiet(bool quiet) { this->Quiet = quiet; }
151 void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
152 std::map<std::string, std::string>& mapping);
154 private:
155 cmCompiledGeneratorExpression(cmake& cmakeInstance,
156 cmListFileBacktrace backtrace,
157 std::string input);
159 friend class cmGeneratorExpression;
161 cmListFileBacktrace Backtrace;
162 std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
163 const std::string Input;
164 bool NeedsEvaluation;
165 bool EvaluateForBuildsystem = false;
166 bool Quiet = false;
168 mutable std::set<cmGeneratorTarget*> DependTargets;
169 mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
170 mutable std::set<std::string> SeenTargetProperties;
171 mutable std::map<cmGeneratorTarget const*,
172 std::map<std::string, std::string>>
173 MaxLanguageStandard;
174 mutable std::string Output;
175 mutable bool HadContextSensitiveCondition = false;
176 mutable bool HadHeadSensitiveCondition = false;
177 mutable bool HadLinkLanguageSensitiveCondition = false;
178 mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
181 class cmGeneratorExpressionInterpreter
183 public:
184 cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
185 std::string config,
186 cmGeneratorTarget const* headTarget,
187 std::string language = std::string())
188 : GeneratorExpression(*localGenerator->GetCMakeInstance())
189 , LocalGenerator(localGenerator)
190 , Config(std::move(config))
191 , HeadTarget(headTarget)
192 , Language(std::move(language))
196 cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
197 delete;
198 cmGeneratorExpressionInterpreter& operator=(
199 cmGeneratorExpressionInterpreter const&) = delete;
201 const std::string& Evaluate(std::string expression,
202 const std::string& property);
204 protected:
205 cmGeneratorExpression GeneratorExpression;
206 std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
207 cmLocalGenerator* LocalGenerator = nullptr;
208 std::string Config;
209 cmGeneratorTarget const* HeadTarget = nullptr;
210 std::string Language;