cmGeneratorExpression.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.HadContextSensitiveCondition = false;
  78. context.HeadTarget = headTarget;
  79. context.CurrentTarget = currentTarget ? currentTarget : headTarget;
  80. context.Backtrace = this->Backtrace;
  81. for ( ; it != end; ++it)
  82. {
  83. const std::string result = (*it)->Evaluate(&context, dagChecker);
  84. this->Output += result;
  85. for(std::set<cmStdString>::const_iterator
  86. p = context.SeenTargetProperties.begin();
  87. p != context.SeenTargetProperties.end(); ++p)
  88. {
  89. this->SeenTargetProperties[*p] += result + ";";
  90. }
  91. if (context.HadError)
  92. {
  93. this->Output = "";
  94. break;
  95. }
  96. }
  97. if (!context.HadError)
  98. {
  99. this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
  100. }
  101. this->Targets = context.Targets;
  102. // TODO: Return a std::string from here instead?
  103. return this->Output.c_str();
  104. }
  105. cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
  106. cmListFileBacktrace const& backtrace,
  107. const char *input)
  108. : Backtrace(backtrace), Input(input ? input : ""),
  109. HadContextSensitiveCondition(false)
  110. {
  111. cmGeneratorExpressionLexer l;
  112. std::vector<cmGeneratorExpressionToken> tokens =
  113. l.Tokenize(this->Input.c_str());
  114. this->NeedsParsing = l.GetSawGeneratorExpression();
  115. if (this->NeedsParsing)
  116. {
  117. cmGeneratorExpressionParser p(tokens);
  118. p.Parse(this->Evaluators);
  119. }
  120. }
  121. //----------------------------------------------------------------------------
  122. cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
  123. {
  124. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  125. = this->Evaluators.begin();
  126. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  127. = this->Evaluators.end();
  128. for ( ; it != end; ++it)
  129. {
  130. delete *it;
  131. }
  132. }
  133. //----------------------------------------------------------------------------
  134. static std::string stripEmptyListElements(const std::string &input)
  135. {
  136. std::string result;
  137. const char *c = input.c_str();
  138. bool skipSemiColons = true;
  139. for ( ; *c; ++c)
  140. {
  141. if(c[0] == ';')
  142. {
  143. if(skipSemiColons)
  144. {
  145. continue;
  146. }
  147. skipSemiColons = true;
  148. }
  149. else
  150. {
  151. skipSemiColons = false;
  152. }
  153. result += *c;
  154. }
  155. if (!result.empty() && *(result.end() - 1) == ';')
  156. {
  157. result.resize(result.size() - 1);
  158. }
  159. return result;
  160. }
  161. //----------------------------------------------------------------------------
  162. static std::string stripAllGeneratorExpressions(const std::string &input)
  163. {
  164. std::string result;
  165. std::string::size_type pos = 0;
  166. std::string::size_type lastPos = pos;
  167. while((pos = input.find("$<", lastPos)) != input.npos)
  168. {
  169. result += input.substr(lastPos, pos - lastPos);
  170. pos += 2;
  171. int nestingLevel = 1;
  172. const char *c = input.c_str() + pos;
  173. const char * const cStart = c;
  174. for ( ; *c; ++c)
  175. {
  176. if(c[0] == '$' && c[1] == '<')
  177. {
  178. ++nestingLevel;
  179. ++c;
  180. continue;
  181. }
  182. if(c[0] == '>')
  183. {
  184. --nestingLevel;
  185. if (nestingLevel == 0)
  186. {
  187. break;
  188. }
  189. }
  190. }
  191. const std::string::size_type traversed = (c - cStart) + 1;
  192. if (!*c)
  193. {
  194. result += "$<" + input.substr(pos, traversed);
  195. }
  196. pos += traversed;
  197. lastPos = pos;
  198. }
  199. result += input.substr(lastPos);
  200. return stripEmptyListElements(result);
  201. }
  202. //----------------------------------------------------------------------------
  203. static std::string stripExportInterface(const std::string &input,
  204. cmGeneratorExpression::PreprocessContext context)
  205. {
  206. std::string result;
  207. std::string::size_type pos = 0;
  208. std::string::size_type lastPos = pos;
  209. while((pos = input.find("$<BUILD_INTERFACE:", lastPos)) != input.npos
  210. || (pos = input.find("$<INSTALL_INTERFACE:", lastPos)) != input.npos)
  211. {
  212. result += input.substr(lastPos, pos - lastPos);
  213. const bool gotInstallInterface = input[pos + 2] == 'I';
  214. pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
  215. : sizeof("$<BUILD_INTERFACE:") - 1;
  216. int nestingLevel = 1;
  217. const char *c = input.c_str() + pos;
  218. const char * const cStart = c;
  219. for ( ; *c; ++c)
  220. {
  221. if(c[0] == '$' && c[1] == '<')
  222. {
  223. ++nestingLevel;
  224. ++c;
  225. continue;
  226. }
  227. if(c[0] == '>')
  228. {
  229. --nestingLevel;
  230. if (nestingLevel != 0)
  231. {
  232. continue;
  233. }
  234. if(context == cmGeneratorExpression::BuildInterface
  235. && !gotInstallInterface)
  236. {
  237. result += input.substr(pos, c - cStart);
  238. }
  239. else if(context == cmGeneratorExpression::InstallInterface
  240. && gotInstallInterface)
  241. {
  242. result += input.substr(pos, c - cStart);
  243. }
  244. break;
  245. }
  246. }
  247. const std::string::size_type traversed = (c - cStart) + 1;
  248. if (!*c)
  249. {
  250. result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
  251. : "$<BUILD_INTERFACE:")
  252. + input.substr(pos, traversed);
  253. }
  254. pos += traversed;
  255. lastPos = pos;
  256. }
  257. result += input.substr(lastPos);
  258. return stripEmptyListElements(result);
  259. }
  260. //----------------------------------------------------------------------------
  261. void cmGeneratorExpression::Split(const std::string &input,
  262. std::vector<std::string> &output)
  263. {
  264. std::string::size_type pos = 0;
  265. std::string::size_type lastPos = pos;
  266. while((pos = input.find("$<", lastPos)) != input.npos)
  267. {
  268. std::string part = input.substr(lastPos, pos - lastPos);
  269. std::string preGenex;
  270. if (!part.empty())
  271. {
  272. std::string::size_type startPos = input.rfind(";", pos);
  273. if (startPos != pos - 1 && startPos >= lastPos)
  274. {
  275. part = input.substr(lastPos, startPos - lastPos);
  276. preGenex = input.substr(startPos + 1, pos - startPos - 1);
  277. }
  278. cmSystemTools::ExpandListArgument(part.c_str(), output);
  279. }
  280. pos += 2;
  281. int nestingLevel = 1;
  282. const char *c = input.c_str() + pos;
  283. const char * const cStart = c;
  284. for ( ; *c; ++c)
  285. {
  286. if(c[0] == '$' && c[1] == '<')
  287. {
  288. ++nestingLevel;
  289. ++c;
  290. continue;
  291. }
  292. if(c[0] == '>')
  293. {
  294. --nestingLevel;
  295. if (nestingLevel == 0)
  296. {
  297. break;
  298. }
  299. }
  300. }
  301. for ( ; *c; ++c)
  302. {
  303. // Capture the part after the genex and before the next ';'
  304. if(c[0] == ';')
  305. {
  306. --c;
  307. break;
  308. }
  309. }
  310. const std::string::size_type traversed = (c - cStart) + 1;
  311. output.push_back(preGenex + "$<" + input.substr(pos, traversed));
  312. pos += traversed;
  313. lastPos = pos;
  314. }
  315. if (lastPos < input.size())
  316. {
  317. cmSystemTools::ExpandListArgument(input.substr(lastPos), output);
  318. }
  319. }
  320. //----------------------------------------------------------------------------
  321. std::string cmGeneratorExpression::Preprocess(const std::string &input,
  322. PreprocessContext context)
  323. {
  324. if (context == StripAllGeneratorExpressions)
  325. {
  326. return stripAllGeneratorExpressions(input);
  327. }
  328. else if (context == BuildInterface || context == InstallInterface)
  329. {
  330. return stripExportInterface(input, context);
  331. }
  332. assert(!"cmGeneratorExpression::Preprocess called with invalid args");
  333. return std::string();
  334. }
  335. //----------------------------------------------------------------------------
  336. std::string::size_type cmGeneratorExpression::Find(const std::string &input)
  337. {
  338. const std::string::size_type openpos = input.find("$<");
  339. if (openpos != std::string::npos
  340. && input.find(">", openpos) != std::string::npos)
  341. {
  342. return openpos;
  343. }
  344. return std::string::npos;
  345. }
  346. //----------------------------------------------------------------------------
  347. bool cmGeneratorExpression::IsValidTargetName(const std::string &input)
  348. {
  349. cmsys::RegularExpression targetNameValidator;
  350. // The ':' is supported to allow use with IMPORTED targets. At least
  351. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  352. targetNameValidator.compile("^[A-Za-z0-9_.:-]+$");
  353. return targetNameValidator.find(input.c_str());
  354. }