cmGeneratorExpression.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorExpression.h"
  11. #include "cmMakefile.h"
  12. #include "cmTarget.h"
  13. #include "assert.h"
  14. #include <cmsys/String.h>
  15. #include "cmGeneratorExpressionEvaluator.h"
  16. #include "cmGeneratorExpressionLexer.h"
  17. #include "cmGeneratorExpressionParser.h"
  18. #include "cmGeneratorExpressionDAGChecker.h"
  19. //----------------------------------------------------------------------------
  20. cmGeneratorExpression::cmGeneratorExpression(
  21. cmListFileBacktrace const& backtrace):
  22. Backtrace(backtrace)
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. cmsys::auto_ptr<cmCompiledGeneratorExpression>
  27. cmGeneratorExpression::Parse(std::string const& input)
  28. {
  29. return this->Parse(input.c_str());
  30. }
  31. //----------------------------------------------------------------------------
  32. cmsys::auto_ptr<cmCompiledGeneratorExpression>
  33. cmGeneratorExpression::Parse(const char* input)
  34. {
  35. return cmsys::auto_ptr<cmCompiledGeneratorExpression>(
  36. new cmCompiledGeneratorExpression(
  37. this->Backtrace,
  38. input));
  39. }
  40. cmGeneratorExpression::~cmGeneratorExpression()
  41. {
  42. }
  43. //----------------------------------------------------------------------------
  44. const char *cmCompiledGeneratorExpression::Evaluate(
  45. cmMakefile* mf, const char* config, bool quiet,
  46. cmTarget *headTarget,
  47. cmGeneratorExpressionDAGChecker *dagChecker) const
  48. {
  49. return this->Evaluate(mf,
  50. config,
  51. quiet,
  52. headTarget,
  53. headTarget,
  54. dagChecker);
  55. }
  56. //----------------------------------------------------------------------------
  57. const char *cmCompiledGeneratorExpression::Evaluate(
  58. cmMakefile* mf, const char* config, bool quiet,
  59. cmTarget *headTarget,
  60. cmTarget *currentTarget,
  61. cmGeneratorExpressionDAGChecker *dagChecker) const
  62. {
  63. if (!this->NeedsParsing)
  64. {
  65. return this->Input.c_str();
  66. }
  67. this->Output = "";
  68. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  69. = this->Evaluators.begin();
  70. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  71. = this->Evaluators.end();
  72. cmGeneratorExpressionContext context;
  73. context.Makefile = mf;
  74. context.Config = config;
  75. context.Quiet = quiet;
  76. context.HadError = false;
  77. context.HeadTarget = headTarget;
  78. context.CurrentTarget = currentTarget ? currentTarget : headTarget;
  79. context.Backtrace = this->Backtrace;
  80. for ( ; it != end; ++it)
  81. {
  82. const std::string result = (*it)->Evaluate(&context, dagChecker);
  83. this->Output += result;
  84. for(std::set<cmStdString>::const_iterator
  85. p = context.SeenTargetProperties.begin();
  86. p != context.SeenTargetProperties.end(); ++p)
  87. {
  88. this->SeenTargetProperties[*p] += result + ";";
  89. }
  90. if (context.HadError)
  91. {
  92. this->Output = "";
  93. break;
  94. }
  95. }
  96. this->Targets = context.Targets;
  97. // TODO: Return a std::string from here instead?
  98. return this->Output.c_str();
  99. }
  100. cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
  101. cmListFileBacktrace const& backtrace,
  102. const char *input)
  103. : Backtrace(backtrace), Input(input ? input : "")
  104. {
  105. cmGeneratorExpressionLexer l;
  106. std::vector<cmGeneratorExpressionToken> tokens =
  107. l.Tokenize(this->Input.c_str());
  108. this->NeedsParsing = l.GetSawGeneratorExpression();
  109. if (this->NeedsParsing)
  110. {
  111. cmGeneratorExpressionParser p(tokens);
  112. p.Parse(this->Evaluators);
  113. }
  114. }
  115. //----------------------------------------------------------------------------
  116. cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
  117. {
  118. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  119. = this->Evaluators.begin();
  120. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  121. = this->Evaluators.end();
  122. for ( ; it != end; ++it)
  123. {
  124. delete *it;
  125. }
  126. }
  127. //----------------------------------------------------------------------------
  128. static std::string stripAllGeneratorExpressions(const std::string &input)
  129. {
  130. std::string result;
  131. std::string::size_type pos = 0;
  132. std::string::size_type lastPos = pos;
  133. while((pos = input.find("$<", lastPos)) != input.npos)
  134. {
  135. result += input.substr(lastPos, pos - lastPos);
  136. pos += 2;
  137. int nestingLevel = 1;
  138. const char *c = input.c_str() + pos;
  139. const char * const cStart = c;
  140. for ( ; *c; ++c)
  141. {
  142. if(c[0] == '$' && c[1] == '<')
  143. {
  144. ++nestingLevel;
  145. ++c;
  146. continue;
  147. }
  148. if(c[0] == '>')
  149. {
  150. --nestingLevel;
  151. if (nestingLevel == 0)
  152. {
  153. break;
  154. }
  155. }
  156. }
  157. const std::string::size_type traversed = (c - cStart) + 1;
  158. if (!*c)
  159. {
  160. result += "$<" + input.substr(pos, traversed);
  161. }
  162. pos += traversed;
  163. lastPos = pos;
  164. }
  165. result += input.substr(lastPos);
  166. return result;
  167. }
  168. //----------------------------------------------------------------------------
  169. static std::string stripExportInterface(const std::string &input,
  170. cmGeneratorExpression::PreprocessContext context)
  171. {
  172. std::string result;
  173. std::string::size_type pos = 0;
  174. std::string::size_type lastPos = pos;
  175. while((pos = input.find("$<BUILD_INTERFACE:", lastPos)) != input.npos
  176. || (pos = input.find("$<INSTALL_INTERFACE:", lastPos)) != input.npos)
  177. {
  178. result += input.substr(lastPos, pos - lastPos);
  179. const bool gotInstallInterface = input[pos + 2] == 'I';
  180. pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
  181. : sizeof("$<BUILD_INTERFACE:") - 1;
  182. int nestingLevel = 1;
  183. const char *c = input.c_str() + pos;
  184. const char * const cStart = c;
  185. for ( ; *c; ++c)
  186. {
  187. if(c[0] == '$' && c[1] == '<')
  188. {
  189. ++nestingLevel;
  190. ++c;
  191. continue;
  192. }
  193. if(c[0] == '>')
  194. {
  195. --nestingLevel;
  196. if (nestingLevel != 0)
  197. {
  198. continue;
  199. }
  200. if(context == cmGeneratorExpression::BuildInterface
  201. && !gotInstallInterface)
  202. {
  203. result += input.substr(pos, c - cStart);
  204. }
  205. else if(context == cmGeneratorExpression::InstallInterface
  206. && gotInstallInterface)
  207. {
  208. result += input.substr(pos, c - cStart);
  209. }
  210. break;
  211. }
  212. }
  213. const std::string::size_type traversed = (c - cStart) + 1;
  214. if (!*c)
  215. {
  216. result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
  217. : "$<BUILD_INTERFACE:")
  218. + input.substr(pos, traversed);
  219. }
  220. pos += traversed;
  221. lastPos = pos;
  222. }
  223. result += input.substr(lastPos);
  224. return result;
  225. }
  226. //----------------------------------------------------------------------------
  227. std::string cmGeneratorExpression::Preprocess(const std::string &input,
  228. PreprocessContext context)
  229. {
  230. if (context == StripAllGeneratorExpressions)
  231. {
  232. return stripAllGeneratorExpressions(input);
  233. }
  234. else if (context == BuildInterface || context == InstallInterface)
  235. {
  236. return stripExportInterface(input, context);
  237. }
  238. assert(!"cmGeneratorExpression::Preprocess called with invalid args");
  239. return std::string();
  240. }