Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmGeneratorExpression.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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"
13
14 #include "cmMakefile.h"
15 #include "cmTarget.h"
16 #include "assert.h"
17
18 #include <cmsys/String.h>
19
20 #include "cmGeneratorExpressionEvaluator.h"
21 #include "cmGeneratorExpressionLexer.h"
22 #include "cmGeneratorExpressionParser.h"
23 #include "cmGeneratorExpressionDAGChecker.h"
24
25 //----------------------------------------------------------------------------
26 cmGeneratorExpression::cmGeneratorExpression(
27   cmListFileBacktrace const& backtrace):
28   Backtrace(backtrace)
29 {
30 }
31
32 //----------------------------------------------------------------------------
33 cmsys::auto_ptr<cmCompiledGeneratorExpression>
34 cmGeneratorExpression::Parse(std::string const& input)
35 {
36   return this->Parse(input.c_str());
37 }
38
39 //----------------------------------------------------------------------------
40 cmsys::auto_ptr<cmCompiledGeneratorExpression>
41 cmGeneratorExpression::Parse(const char* input)
42 {
43   return cmsys::auto_ptr<cmCompiledGeneratorExpression>(
44                                       new cmCompiledGeneratorExpression(
45                                         this->Backtrace,
46                                         input));
47 }
48
49 cmGeneratorExpression::~cmGeneratorExpression()
50 {
51 }
52
53 //----------------------------------------------------------------------------
54 const char *cmCompiledGeneratorExpression::Evaluate(
55   cmMakefile* mf, const char* config, bool quiet,
56   cmTarget *headTarget,
57   cmGeneratorExpressionDAGChecker *dagChecker) const
58 {
59   return this->Evaluate(mf,
60                         config,
61                         quiet,
62                         headTarget,
63                         headTarget,
64                         dagChecker);
65 }
66
67 //----------------------------------------------------------------------------
68 const char *cmCompiledGeneratorExpression::Evaluate(
69   cmMakefile* mf, const char* config, bool quiet,
70   cmTarget *headTarget,
71   cmTarget *currentTarget,
72   cmGeneratorExpressionDAGChecker *dagChecker) const
73 {
74   if (!this->NeedsParsing)
75     {
76     return this->Input.c_str();
77     }
78
79   this->Output = "";
80
81   std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
82                                                   = this->Evaluators.begin();
83   const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
84                                                   = this->Evaluators.end();
85
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;
95
96   for ( ; it != end; ++it)
97     {
98     this->Output += (*it)->Evaluate(&context, dagChecker);
99
100     for(std::set<cmStdString>::const_iterator
101           p = context.SeenTargetProperties.begin();
102           p != context.SeenTargetProperties.end(); ++p)
103       {
104       this->SeenTargetProperties.insert(*p);
105       }
106     if (context.HadError)
107       {
108       this->Output = "";
109       break;
110       }
111     }
112   if (!context.HadError)
113     {
114     this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
115     }
116
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();
121 }
122
123 cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
124               cmListFileBacktrace const& backtrace,
125               const char *input)
126   : Backtrace(backtrace), Input(input ? input : ""),
127     HadContextSensitiveCondition(false)
128 {
129   cmGeneratorExpressionLexer l;
130   std::vector<cmGeneratorExpressionToken> tokens =
131                                               l.Tokenize(this->Input.c_str());
132   this->NeedsParsing = l.GetSawGeneratorExpression();
133
134   if (this->NeedsParsing)
135     {
136     cmGeneratorExpressionParser p(tokens);
137     p.Parse(this->Evaluators);
138     }
139 }
140
141
142 //----------------------------------------------------------------------------
143 cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
144 {
145   std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
146                                                   = this->Evaluators.begin();
147   const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
148                                                   = this->Evaluators.end();
149
150   for ( ; it != end; ++it)
151     {
152     delete *it;
153     }
154 }
155
156 //----------------------------------------------------------------------------
157 std::string cmGeneratorExpression::StripEmptyListElements(
158                                                     const std::string &input)
159 {
160   std::string result;
161
162   const char *c = input.c_str();
163   bool skipSemiColons = true;
164   for ( ; *c; ++c)
165     {
166     if(c[0] == ';')
167       {
168       if(skipSemiColons)
169         {
170         continue;
171         }
172       skipSemiColons = true;
173       }
174     else
175       {
176       skipSemiColons = false;
177       }
178     result += *c;
179     }
180
181   if (!result.empty() && *(result.end() - 1) == ';')
182     {
183     result.resize(result.size() - 1);
184     }
185
186   return result;
187 }
188
189 //----------------------------------------------------------------------------
190 static std::string stripAllGeneratorExpressions(const std::string &input)
191 {
192   std::string result;
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)
197     {
198     result += input.substr(lastPos, pos - lastPos);
199     pos += 2;
200     nestingLevel = 1;
201     const char *c = input.c_str() + pos;
202     const char * const cStart = c;
203     for ( ; *c; ++c)
204       {
205       if(c[0] == '$' && c[1] == '<')
206         {
207         ++nestingLevel;
208         ++c;
209         continue;
210         }
211       if(c[0] == '>')
212         {
213         --nestingLevel;
214         if (nestingLevel == 0)
215           {
216           break;
217           }
218         }
219       }
220     const std::string::size_type traversed = (c - cStart) + 1;
221     if (!*c)
222       {
223       result += "$<" + input.substr(pos, traversed);
224       }
225     pos += traversed;
226     lastPos = pos;
227     }
228   if (nestingLevel == 0)
229     {
230     result += input.substr(lastPos);
231     }
232   return cmGeneratorExpression::StripEmptyListElements(result);
233 }
234
235 //----------------------------------------------------------------------------
236 static void prefixItems(const std::string &content, std::string &result,
237                         const std::string &prefix)
238 {
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)
244     {
245     result += sep;
246     sep = ";";
247     if (!cmSystemTools::FileIsFullPath(ei->c_str())
248         && cmGeneratorExpression::Find(*ei) == std::string::npos)
249       {
250       result += prefix;
251       }
252     result += *ei;
253     }
254 }
255
256 //----------------------------------------------------------------------------
257 static std::string stripExportInterface(const std::string &input,
258                           cmGeneratorExpression::PreprocessContext context,
259                           bool resolveRelative)
260 {
261   std::string result;
262
263   int nestingLevel = 0;
264   std::string::size_type pos = 0;
265   std::string::size_type lastPos = pos;
266   while (true)
267     {
268     std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
269     std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
270
271     if (bPos == std::string::npos && iPos == std::string::npos)
272       {
273       break;
274       }
275
276     if (bPos == std::string::npos)
277       {
278       pos = iPos;
279       }
280     else if (iPos == std::string::npos)
281       {
282       pos = bPos;
283       }
284     else
285       {
286       pos = (bPos < iPos) ? bPos : iPos;
287       }
288
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;
293     nestingLevel = 1;
294     const char *c = input.c_str() + pos;
295     const char * const cStart = c;
296     for ( ; *c; ++c)
297       {
298       if(c[0] == '$' && c[1] == '<')
299         {
300         ++nestingLevel;
301         ++c;
302         continue;
303         }
304       if(c[0] == '>')
305         {
306         --nestingLevel;
307         if (nestingLevel != 0)
308           {
309           continue;
310           }
311         if(context == cmGeneratorExpression::BuildInterface
312             && !gotInstallInterface)
313           {
314           result += input.substr(pos, c - cStart);
315           }
316         else if(context == cmGeneratorExpression::InstallInterface
317             && gotInstallInterface)
318           {
319           const std::string content = input.substr(pos, c - cStart);
320           if (resolveRelative)
321             {
322             prefixItems(content, result, "${_IMPORT_PREFIX}/");
323             }
324           else
325             {
326             result += content;
327             }
328           }
329         break;
330         }
331       }
332     const std::string::size_type traversed = (c - cStart) + 1;
333     if (!*c)
334       {
335       result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
336                                                 : "$<BUILD_INTERFACE:")
337              + input.substr(pos, traversed);
338       }
339     pos += traversed;
340     lastPos = pos;
341     }
342   if (nestingLevel == 0)
343     {
344     result += input.substr(lastPos);
345     }
346
347   return cmGeneratorExpression::StripEmptyListElements(result);
348 }
349
350 //----------------------------------------------------------------------------
351 void cmGeneratorExpression::Split(const std::string &input,
352                                   std::vector<std::string> &output)
353 {
354   std::string::size_type pos = 0;
355   std::string::size_type lastPos = pos;
356   while((pos = input.find("$<", lastPos)) != input.npos)
357     {
358     std::string part = input.substr(lastPos, pos - lastPos);
359     std::string preGenex;
360     if (!part.empty())
361       {
362       std::string::size_type startPos = input.rfind(";", pos);
363       if (startPos == std::string::npos)
364         {
365         preGenex = part;
366         part = "";
367         }
368       else if (startPos != pos - 1 && startPos >= lastPos)
369         {
370         part = input.substr(lastPos, startPos - lastPos);
371         preGenex = input.substr(startPos + 1, pos - startPos - 1);
372         }
373       if(!part.empty())
374         {
375         cmSystemTools::ExpandListArgument(part.c_str(), output);
376         }
377       }
378     pos += 2;
379     int nestingLevel = 1;
380     const char *c = input.c_str() + pos;
381     const char * const cStart = c;
382     for ( ; *c; ++c)
383       {
384       if(c[0] == '$' && c[1] == '<')
385         {
386         ++nestingLevel;
387         ++c;
388         continue;
389         }
390       if(c[0] == '>')
391         {
392         --nestingLevel;
393         if (nestingLevel == 0)
394           {
395           break;
396           }
397         }
398       }
399     for ( ; *c; ++c)
400       {
401       // Capture the part after the genex and before the next ';'
402       if(c[0] == ';')
403         {
404         --c;
405         break;
406         }
407       }
408     const std::string::size_type traversed = (c - cStart) + 1;
409     output.push_back(preGenex + "$<" + input.substr(pos, traversed));
410     pos += traversed;
411     lastPos = pos;
412     }
413   if (lastPos < input.size())
414     {
415     cmSystemTools::ExpandListArgument(input.substr(lastPos), output);
416     }
417 }
418
419 //----------------------------------------------------------------------------
420 std::string cmGeneratorExpression::Preprocess(const std::string &input,
421                                               PreprocessContext context,
422                                               bool resolveRelative)
423 {
424   if (context == StripAllGeneratorExpressions)
425     {
426     return stripAllGeneratorExpressions(input);
427     }
428   else if (context == BuildInterface || context == InstallInterface)
429     {
430     return stripExportInterface(input, context, resolveRelative);
431     }
432
433   assert(!"cmGeneratorExpression::Preprocess called with invalid args");
434   return std::string();
435 }
436
437 //----------------------------------------------------------------------------
438 std::string::size_type cmGeneratorExpression::Find(const std::string &input)
439 {
440   const std::string::size_type openpos = input.find("$<");
441   if (openpos != std::string::npos
442       && input.find(">", openpos) != std::string::npos)
443     {
444     return openpos;
445     }
446   return std::string::npos;
447 }
448
449 //----------------------------------------------------------------------------
450 bool cmGeneratorExpression::IsValidTargetName(const std::string &input)
451 {
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_.:+-]+$");
456
457   return targetNameValidator.find(input.c_str());
458 }