1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmGeneratorExpression.h"
9 #include "cmsys/RegularExpression.hxx"
11 #include "cmGeneratorExpressionContext.h"
12 #include "cmGeneratorExpressionDAGChecker.h"
13 #include "cmGeneratorExpressionEvaluator.h"
14 #include "cmGeneratorExpressionLexer.h"
15 #include "cmGeneratorExpressionParser.h"
16 #include "cmStringAlgorithms.h"
17 #include "cmSystemTools.h"
19 cmGeneratorExpression::cmGeneratorExpression(cmListFileBacktrace backtrace)
20 : Backtrace(std::move(backtrace))
24 cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression() = default;
26 cmGeneratorExpression::~cmGeneratorExpression() = default;
28 std::unique_ptr<cmCompiledGeneratorExpression> cmGeneratorExpression::Parse(
29 std::string input) const
31 return std::unique_ptr<cmCompiledGeneratorExpression>(
32 new cmCompiledGeneratorExpression(this->Backtrace, std::move(input)));
35 std::string cmGeneratorExpression::Evaluate(
36 std::string input, cmLocalGenerator* lg, const std::string& config,
37 cmGeneratorTarget const* headTarget,
38 cmGeneratorExpressionDAGChecker* dagChecker,
39 cmGeneratorTarget const* currentTarget, std::string const& language)
41 if (Find(input) != std::string::npos) {
42 cmCompiledGeneratorExpression cge(cmListFileBacktrace(), std::move(input));
43 return cge.Evaluate(lg, config, headTarget, dagChecker, currentTarget,
49 const std::string& cmCompiledGeneratorExpression::Evaluate(
50 cmLocalGenerator* lg, const std::string& config,
51 const cmGeneratorTarget* headTarget,
52 cmGeneratorExpressionDAGChecker* dagChecker,
53 const cmGeneratorTarget* currentTarget, std::string const& language) const
55 cmGeneratorExpressionContext context(
56 lg, config, this->Quiet, headTarget,
57 currentTarget ? currentTarget : headTarget, this->EvaluateForBuildsystem,
58 this->Backtrace, language);
60 return this->EvaluateWithContext(context, dagChecker);
63 const std::string& cmCompiledGeneratorExpression::EvaluateWithContext(
64 cmGeneratorExpressionContext& context,
65 cmGeneratorExpressionDAGChecker* dagChecker) const
67 if (!this->NeedsEvaluation) {
73 for (const auto& it : this->Evaluators) {
74 this->Output += it->Evaluate(&context, dagChecker);
76 this->SeenTargetProperties.insert(context.SeenTargetProperties.cbegin(),
77 context.SeenTargetProperties.cend());
78 if (context.HadError) {
84 this->MaxLanguageStandard = context.MaxLanguageStandard;
86 if (!context.HadError) {
87 this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
88 this->HadHeadSensitiveCondition = context.HadHeadSensitiveCondition;
89 this->HadLinkLanguageSensitiveCondition =
90 context.HadLinkLanguageSensitiveCondition;
91 this->SourceSensitiveTargets = context.SourceSensitiveTargets;
94 this->DependTargets = context.DependTargets;
95 this->AllTargetsSeen = context.AllTargets;
99 cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
100 cmListFileBacktrace backtrace, std::string input)
101 : Backtrace(std::move(backtrace))
102 , Input(std::move(input))
104 cmGeneratorExpressionLexer l;
105 std::vector<cmGeneratorExpressionToken> tokens = l.Tokenize(this->Input);
106 this->NeedsEvaluation = l.GetSawGeneratorExpression();
108 if (this->NeedsEvaluation) {
109 cmGeneratorExpressionParser p(tokens);
110 p.Parse(this->Evaluators);
114 std::string cmGeneratorExpression::StripEmptyListElements(
115 const std::string& input)
117 if (input.find(';') == std::string::npos) {
121 result.reserve(input.size());
123 const char* c = input.c_str();
124 const char* last = c;
125 bool skipSemiColons = true;
128 if (skipSemiColons) {
129 result.append(last, c - last);
132 skipSemiColons = true;
134 skipSemiColons = false;
139 if (!result.empty() && *(result.end() - 1) == ';') {
140 result.resize(result.size() - 1);
146 static std::string stripAllGeneratorExpressions(const std::string& input)
149 std::string::size_type pos = 0;
150 std::string::size_type lastPos = pos;
151 int nestingLevel = 0;
152 while ((pos = input.find("$<", lastPos)) != std::string::npos) {
153 result += input.substr(lastPos, pos - lastPos);
156 const char* c = input.c_str() + pos;
157 const char* const cStart = c;
159 if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
166 if (nestingLevel == 0) {
171 const std::string::size_type traversed = (c - cStart) + 1;
173 result += "$<" + input.substr(pos, traversed);
178 if (nestingLevel == 0) {
179 result += input.substr(lastPos);
181 return cmGeneratorExpression::StripEmptyListElements(result);
184 static void prefixItems(const std::string& content, std::string& result,
185 const std::string& prefix)
187 std::vector<std::string> entries;
188 cmGeneratorExpression::Split(content, entries);
189 const char* sep = "";
190 for (std::string const& e : entries) {
193 if (!cmSystemTools::FileIsFullPath(e) &&
194 cmGeneratorExpression::Find(e) != 0) {
201 static std::string stripExportInterface(
202 const std::string& input, cmGeneratorExpression::PreprocessContext context,
203 bool resolveRelative)
207 int nestingLevel = 0;
208 std::string::size_type pos = 0;
209 std::string::size_type lastPos = pos;
211 std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
212 std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
214 if (bPos == std::string::npos && iPos == std::string::npos) {
218 if (bPos == std::string::npos) {
220 } else if (iPos == std::string::npos) {
223 pos = (bPos < iPos) ? bPos : iPos;
226 result += input.substr(lastPos, pos - lastPos);
227 const bool gotInstallInterface = input[pos + 2] == 'I';
228 pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
229 : sizeof("$<BUILD_INTERFACE:") - 1;
231 const char* c = input.c_str() + pos;
232 const char* const cStart = c;
234 if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
241 if (nestingLevel != 0) {
244 if (context == cmGeneratorExpression::BuildInterface &&
245 !gotInstallInterface) {
246 result += input.substr(pos, c - cStart);
247 } else if (context == cmGeneratorExpression::InstallInterface &&
248 gotInstallInterface) {
249 const std::string content = input.substr(pos, c - cStart);
250 if (resolveRelative) {
251 prefixItems(content, result, "${_IMPORT_PREFIX}/");
259 const std::string::size_type traversed = (c - cStart) + 1;
261 result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
262 : "$<BUILD_INTERFACE:") +
263 input.substr(pos, traversed);
268 if (nestingLevel == 0) {
269 result += input.substr(lastPos);
272 return cmGeneratorExpression::StripEmptyListElements(result);
275 void cmGeneratorExpression::Split(const std::string& input,
276 std::vector<std::string>& output)
278 std::string::size_type pos = 0;
279 std::string::size_type lastPos = pos;
280 while ((pos = input.find("$<", lastPos)) != std::string::npos) {
281 std::string part = input.substr(lastPos, pos - lastPos);
282 std::string preGenex;
284 std::string::size_type startPos = input.rfind(';', pos);
285 if (startPos == std::string::npos) {
288 } else if (startPos != pos - 1 && startPos >= lastPos) {
289 part = input.substr(lastPos, startPos - lastPos);
290 preGenex = input.substr(startPos + 1, pos - startPos - 1);
293 cmExpandList(part, output);
297 int nestingLevel = 1;
298 const char* c = input.c_str() + pos;
299 const char* const cStart = c;
301 if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
308 if (nestingLevel == 0) {
314 // Capture the part after the genex and before the next ';'
320 const std::string::size_type traversed = (c - cStart) + 1;
321 output.push_back(preGenex + "$<" + input.substr(pos, traversed));
325 if (lastPos < input.size()) {
326 cmExpandList(input.substr(lastPos), output);
330 std::string cmGeneratorExpression::Preprocess(const std::string& input,
331 PreprocessContext context,
332 bool resolveRelative)
334 if (context == StripAllGeneratorExpressions) {
335 return stripAllGeneratorExpressions(input);
337 if (context == BuildInterface || context == InstallInterface) {
338 return stripExportInterface(input, context, resolveRelative);
342 "cmGeneratorExpression::Preprocess called with invalid args");
343 return std::string();
346 std::string::size_type cmGeneratorExpression::Find(const std::string& input)
348 const std::string::size_type openpos = input.find("$<");
349 if (openpos != std::string::npos &&
350 input.find('>', openpos) != std::string::npos) {
353 return std::string::npos;
356 bool cmGeneratorExpression::IsValidTargetName(const std::string& input)
358 // The ':' is supported to allow use with IMPORTED targets. At least
359 // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
360 static cmsys::RegularExpression targetNameValidator("^[A-Za-z0-9_.:+-]+$");
362 return targetNameValidator.find(input);
365 void cmGeneratorExpression::ReplaceInstallPrefix(
366 std::string& input, const std::string& replacement)
368 std::string::size_type pos = 0;
369 std::string::size_type lastPos = pos;
371 while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) !=
373 std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
374 input.replace(pos, endPos - pos, replacement);
379 void cmCompiledGeneratorExpression::GetMaxLanguageStandard(
380 const cmGeneratorTarget* tgt, std::map<std::string, std::string>& mapping)
382 auto it = this->MaxLanguageStandard.find(tgt);
383 if (it != this->MaxLanguageStandard.end()) {
384 mapping = it->second;
388 const std::string& cmGeneratorExpressionInterpreter::Evaluate(
389 std::string expression, const std::string& property)
391 this->CompiledGeneratorExpression =
392 this->GeneratorExpression.Parse(std::move(expression));
394 // Specify COMPILE_OPTIONS to DAGchecker, same semantic as COMPILE_FLAGS
395 cmGeneratorExpressionDAGChecker dagChecker(
397 property == "COMPILE_FLAGS" ? "COMPILE_OPTIONS" : property, nullptr,
400 return this->CompiledGeneratorExpression->Evaluate(
401 this->LocalGenerator, this->Config, this->HeadTarget, &dagChecker, nullptr,