1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmGeneratorExpression.h"
14 #include "cmMakefile.h"
18 #include <cmsys/String.h>
20 #include "cmGeneratorExpressionEvaluator.h"
21 #include "cmGeneratorExpressionLexer.h"
22 #include "cmGeneratorExpressionParser.h"
23 #include "cmGeneratorExpressionDAGChecker.h"
25 //----------------------------------------------------------------------------
26 cmGeneratorExpression::cmGeneratorExpression(
27 cmListFileBacktrace const& backtrace):
32 //----------------------------------------------------------------------------
33 cmsys::auto_ptr<cmCompiledGeneratorExpression>
34 cmGeneratorExpression::Parse(std::string const& input)
36 return this->Parse(input.c_str());
39 //----------------------------------------------------------------------------
40 cmsys::auto_ptr<cmCompiledGeneratorExpression>
41 cmGeneratorExpression::Parse(const char* input)
43 return cmsys::auto_ptr<cmCompiledGeneratorExpression>(
44 new cmCompiledGeneratorExpression(
49 cmGeneratorExpression::~cmGeneratorExpression()
53 //----------------------------------------------------------------------------
54 const char *cmCompiledGeneratorExpression::Evaluate(
55 cmMakefile* mf, const char* config, bool quiet,
57 cmGeneratorExpressionDAGChecker *dagChecker) const
59 return this->Evaluate(mf,
67 //----------------------------------------------------------------------------
68 const char *cmCompiledGeneratorExpression::Evaluate(
69 cmMakefile* mf, const char* config, bool quiet,
71 cmTarget *currentTarget,
72 cmGeneratorExpressionDAGChecker *dagChecker) const
74 if (!this->NeedsParsing)
76 return this->Input.c_str();
81 std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
82 = this->Evaluators.begin();
83 const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
84 = this->Evaluators.end();
86 cmGeneratorExpressionContext context;
87 context.Makefile = mf;
88 context.Config = config;
89 context.Quiet = quiet;
90 context.HadError = false;
91 context.HadContextSensitiveCondition = false;
92 context.HeadTarget = headTarget;
93 context.CurrentTarget = currentTarget ? currentTarget : headTarget;
94 context.Backtrace = this->Backtrace;
96 for ( ; it != end; ++it)
98 this->Output += (*it)->Evaluate(&context, dagChecker);
100 for(std::set<cmStdString>::const_iterator
101 p = context.SeenTargetProperties.begin();
102 p != context.SeenTargetProperties.end(); ++p)
104 this->SeenTargetProperties.insert(*p);
106 if (context.HadError)
112 if (!context.HadError)
114 this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
117 this->DependTargets = context.DependTargets;
118 this->AllTargetsSeen = context.AllTargets;
119 // TODO: Return a std::string from here instead?
120 return this->Output.c_str();
123 cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
124 cmListFileBacktrace const& backtrace,
126 : Backtrace(backtrace), Input(input ? input : ""),
127 HadContextSensitiveCondition(false)
129 cmGeneratorExpressionLexer l;
130 std::vector<cmGeneratorExpressionToken> tokens =
131 l.Tokenize(this->Input.c_str());
132 this->NeedsParsing = l.GetSawGeneratorExpression();
134 if (this->NeedsParsing)
136 cmGeneratorExpressionParser p(tokens);
137 p.Parse(this->Evaluators);
142 //----------------------------------------------------------------------------
143 cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
145 std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
146 = this->Evaluators.begin();
147 const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
148 = this->Evaluators.end();
150 for ( ; it != end; ++it)
156 //----------------------------------------------------------------------------
157 std::string cmGeneratorExpression::StripEmptyListElements(
158 const std::string &input)
162 const char *c = input.c_str();
163 bool skipSemiColons = true;
172 skipSemiColons = true;
176 skipSemiColons = false;
181 if (!result.empty() && *(result.end() - 1) == ';')
183 result.resize(result.size() - 1);
189 //----------------------------------------------------------------------------
190 static std::string stripAllGeneratorExpressions(const std::string &input)
193 std::string::size_type pos = 0;
194 std::string::size_type lastPos = pos;
195 int nestingLevel = 0;
196 while((pos = input.find("$<", lastPos)) != input.npos)
198 result += input.substr(lastPos, pos - lastPos);
201 const char *c = input.c_str() + pos;
202 const char * const cStart = c;
205 if(c[0] == '$' && c[1] == '<')
214 if (nestingLevel == 0)
220 const std::string::size_type traversed = (c - cStart) + 1;
223 result += "$<" + input.substr(pos, traversed);
228 if (nestingLevel == 0)
230 result += input.substr(lastPos);
232 return cmGeneratorExpression::StripEmptyListElements(result);
235 //----------------------------------------------------------------------------
236 static void prefixItems(const std::string &content, std::string &result,
237 const std::string &prefix)
239 std::vector<std::string> entries;
240 cmGeneratorExpression::Split(content, entries);
241 const char *sep = "";
242 for(std::vector<std::string>::const_iterator ei = entries.begin();
243 ei != entries.end(); ++ei)
247 if (!cmSystemTools::FileIsFullPath(ei->c_str())
248 && cmGeneratorExpression::Find(*ei) == std::string::npos)
256 //----------------------------------------------------------------------------
257 static std::string stripExportInterface(const std::string &input,
258 cmGeneratorExpression::PreprocessContext context,
259 bool resolveRelative)
263 int nestingLevel = 0;
264 std::string::size_type pos = 0;
265 std::string::size_type lastPos = pos;
268 std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
269 std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
271 if (bPos == std::string::npos && iPos == std::string::npos)
276 if (bPos == std::string::npos)
280 else if (iPos == std::string::npos)
286 pos = (bPos < iPos) ? bPos : iPos;
289 result += input.substr(lastPos, pos - lastPos);
290 const bool gotInstallInterface = input[pos + 2] == 'I';
291 pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
292 : sizeof("$<BUILD_INTERFACE:") - 1;
294 const char *c = input.c_str() + pos;
295 const char * const cStart = c;
298 if(c[0] == '$' && c[1] == '<')
307 if (nestingLevel != 0)
311 if(context == cmGeneratorExpression::BuildInterface
312 && !gotInstallInterface)
314 result += input.substr(pos, c - cStart);
316 else if(context == cmGeneratorExpression::InstallInterface
317 && gotInstallInterface)
319 const std::string content = input.substr(pos, c - cStart);
322 prefixItems(content, result, "${_IMPORT_PREFIX}/");
332 const std::string::size_type traversed = (c - cStart) + 1;
335 result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
336 : "$<BUILD_INTERFACE:")
337 + input.substr(pos, traversed);
342 if (nestingLevel == 0)
344 result += input.substr(lastPos);
347 return cmGeneratorExpression::StripEmptyListElements(result);
350 //----------------------------------------------------------------------------
351 void cmGeneratorExpression::Split(const std::string &input,
352 std::vector<std::string> &output)
354 std::string::size_type pos = 0;
355 std::string::size_type lastPos = pos;
356 while((pos = input.find("$<", lastPos)) != input.npos)
358 std::string part = input.substr(lastPos, pos - lastPos);
359 std::string preGenex;
362 std::string::size_type startPos = input.rfind(";", pos);
363 if (startPos == std::string::npos)
368 else if (startPos != pos - 1 && startPos >= lastPos)
370 part = input.substr(lastPos, startPos - lastPos);
371 preGenex = input.substr(startPos + 1, pos - startPos - 1);
375 cmSystemTools::ExpandListArgument(part.c_str(), output);
379 int nestingLevel = 1;
380 const char *c = input.c_str() + pos;
381 const char * const cStart = c;
384 if(c[0] == '$' && c[1] == '<')
393 if (nestingLevel == 0)
401 // Capture the part after the genex and before the next ';'
408 const std::string::size_type traversed = (c - cStart) + 1;
409 output.push_back(preGenex + "$<" + input.substr(pos, traversed));
413 if (lastPos < input.size())
415 cmSystemTools::ExpandListArgument(input.substr(lastPos), output);
419 //----------------------------------------------------------------------------
420 std::string cmGeneratorExpression::Preprocess(const std::string &input,
421 PreprocessContext context,
422 bool resolveRelative)
424 if (context == StripAllGeneratorExpressions)
426 return stripAllGeneratorExpressions(input);
428 else if (context == BuildInterface || context == InstallInterface)
430 return stripExportInterface(input, context, resolveRelative);
433 assert(!"cmGeneratorExpression::Preprocess called with invalid args");
434 return std::string();
437 //----------------------------------------------------------------------------
438 std::string::size_type cmGeneratorExpression::Find(const std::string &input)
440 const std::string::size_type openpos = input.find("$<");
441 if (openpos != std::string::npos
442 && input.find(">", openpos) != std::string::npos)
446 return std::string::npos;
449 //----------------------------------------------------------------------------
450 bool cmGeneratorExpression::IsValidTargetName(const std::string &input)
452 cmsys::RegularExpression targetNameValidator;
453 // The ':' is supported to allow use with IMPORTED targets. At least
454 // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
455 targetNameValidator.compile("^[A-Za-z0-9_.:+-]+$");
457 return targetNameValidator.find(input.c_str());