cmGeneratorExpression.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 cmsys::auto_ptr<cmCompiledGeneratorExpression>(
  30. new cmCompiledGeneratorExpression(
  31. this->Backtrace,
  32. input));
  33. }
  34. //----------------------------------------------------------------------------
  35. cmsys::auto_ptr<cmCompiledGeneratorExpression>
  36. cmGeneratorExpression::Parse(const char* input)
  37. {
  38. return this->Parse(std::string(input ? input : ""));
  39. }
  40. cmGeneratorExpression::~cmGeneratorExpression()
  41. {
  42. }
  43. //----------------------------------------------------------------------------
  44. const char *cmCompiledGeneratorExpression::Evaluate(
  45. cmMakefile* mf, const std::string& config, bool quiet,
  46. cmTarget const* 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 std::string& config, bool quiet,
  59. cmTarget const* headTarget,
  60. cmTarget const* currentTarget,
  61. cmGeneratorExpressionDAGChecker *dagChecker) const
  62. {
  63. if (!this->NeedsEvaluation)
  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. this->Output += (*it)->Evaluate(&context, dagChecker);
  84. for(std::set<std::string>::const_iterator
  85. p = context.SeenTargetProperties.begin();
  86. p != context.SeenTargetProperties.end(); ++p)
  87. {
  88. this->SeenTargetProperties.insert(*p);
  89. }
  90. if (context.HadError)
  91. {
  92. this->Output = "";
  93. break;
  94. }
  95. }
  96. if (!context.HadError)
  97. {
  98. this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
  99. }
  100. this->DependTargets = context.DependTargets;
  101. this->AllTargetsSeen = context.AllTargets;
  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 std::string& input)
  108. : Backtrace(backtrace), Input(input),
  109. HadContextSensitiveCondition(false)
  110. {
  111. cmGeneratorExpressionLexer l;
  112. std::vector<cmGeneratorExpressionToken> tokens =
  113. l.Tokenize(this->Input);
  114. this->NeedsEvaluation = l.GetSawGeneratorExpression();
  115. if (this->NeedsEvaluation)
  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. std::string cmGeneratorExpression::StripEmptyListElements(
  135. const std::string &input)
  136. {
  137. if (input.find(';') == input.npos)
  138. {
  139. return input;
  140. }
  141. std::string result;
  142. result.reserve(input.size());
  143. const char *c = input.c_str();
  144. const char *last = c;
  145. bool skipSemiColons = true;
  146. for ( ; *c; ++c)
  147. {
  148. if(*c == ';')
  149. {
  150. if(skipSemiColons)
  151. {
  152. result.append(last, c - last);
  153. last = c + 1;
  154. }
  155. skipSemiColons = true;
  156. }
  157. else
  158. {
  159. skipSemiColons = false;
  160. }
  161. }
  162. result.append(last);
  163. if (!result.empty() && *(result.end() - 1) == ';')
  164. {
  165. result.resize(result.size() - 1);
  166. }
  167. return result;
  168. }
  169. //----------------------------------------------------------------------------
  170. static std::string stripAllGeneratorExpressions(const std::string &input)
  171. {
  172. std::string result;
  173. std::string::size_type pos = 0;
  174. std::string::size_type lastPos = pos;
  175. int nestingLevel = 0;
  176. while((pos = input.find("$<", lastPos)) != input.npos)
  177. {
  178. result += input.substr(lastPos, pos - lastPos);
  179. pos += 2;
  180. nestingLevel = 1;
  181. const char *c = input.c_str() + pos;
  182. const char * const cStart = c;
  183. for ( ; *c; ++c)
  184. {
  185. if(c[0] == '$' && c[1] == '<')
  186. {
  187. ++nestingLevel;
  188. ++c;
  189. continue;
  190. }
  191. if(c[0] == '>')
  192. {
  193. --nestingLevel;
  194. if (nestingLevel == 0)
  195. {
  196. break;
  197. }
  198. }
  199. }
  200. const std::string::size_type traversed = (c - cStart) + 1;
  201. if (!*c)
  202. {
  203. result += "$<" + input.substr(pos, traversed);
  204. }
  205. pos += traversed;
  206. lastPos = pos;
  207. }
  208. if (nestingLevel == 0)
  209. {
  210. result += input.substr(lastPos);
  211. }
  212. return cmGeneratorExpression::StripEmptyListElements(result);
  213. }
  214. //----------------------------------------------------------------------------
  215. static void prefixItems(const std::string &content, std::string &result,
  216. const std::string &prefix)
  217. {
  218. std::vector<std::string> entries;
  219. cmGeneratorExpression::Split(content, entries);
  220. const char *sep = "";
  221. for(std::vector<std::string>::const_iterator ei = entries.begin();
  222. ei != entries.end(); ++ei)
  223. {
  224. result += sep;
  225. sep = ";";
  226. if (!cmSystemTools::FileIsFullPath(ei->c_str())
  227. && cmGeneratorExpression::Find(*ei) != 0)
  228. {
  229. result += prefix;
  230. }
  231. result += *ei;
  232. }
  233. }
  234. //----------------------------------------------------------------------------
  235. static std::string stripExportInterface(const std::string &input,
  236. cmGeneratorExpression::PreprocessContext context,
  237. bool resolveRelative)
  238. {
  239. std::string result;
  240. int nestingLevel = 0;
  241. std::string::size_type pos = 0;
  242. std::string::size_type lastPos = pos;
  243. while (true)
  244. {
  245. std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
  246. std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
  247. if (bPos == std::string::npos && iPos == std::string::npos)
  248. {
  249. break;
  250. }
  251. if (bPos == std::string::npos)
  252. {
  253. pos = iPos;
  254. }
  255. else if (iPos == std::string::npos)
  256. {
  257. pos = bPos;
  258. }
  259. else
  260. {
  261. pos = (bPos < iPos) ? bPos : iPos;
  262. }
  263. result += input.substr(lastPos, pos - lastPos);
  264. const bool gotInstallInterface = input[pos + 2] == 'I';
  265. pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
  266. : sizeof("$<BUILD_INTERFACE:") - 1;
  267. nestingLevel = 1;
  268. const char *c = input.c_str() + pos;
  269. const char * const cStart = c;
  270. for ( ; *c; ++c)
  271. {
  272. if(c[0] == '$' && c[1] == '<')
  273. {
  274. ++nestingLevel;
  275. ++c;
  276. continue;
  277. }
  278. if(c[0] == '>')
  279. {
  280. --nestingLevel;
  281. if (nestingLevel != 0)
  282. {
  283. continue;
  284. }
  285. if(context == cmGeneratorExpression::BuildInterface
  286. && !gotInstallInterface)
  287. {
  288. result += input.substr(pos, c - cStart);
  289. }
  290. else if(context == cmGeneratorExpression::InstallInterface
  291. && gotInstallInterface)
  292. {
  293. const std::string content = input.substr(pos, c - cStart);
  294. if (resolveRelative)
  295. {
  296. prefixItems(content, result, "${_IMPORT_PREFIX}/");
  297. }
  298. else
  299. {
  300. result += content;
  301. }
  302. }
  303. break;
  304. }
  305. }
  306. const std::string::size_type traversed = (c - cStart) + 1;
  307. if (!*c)
  308. {
  309. result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
  310. : "$<BUILD_INTERFACE:")
  311. + input.substr(pos, traversed);
  312. }
  313. pos += traversed;
  314. lastPos = pos;
  315. }
  316. if (nestingLevel == 0)
  317. {
  318. result += input.substr(lastPos);
  319. }
  320. return cmGeneratorExpression::StripEmptyListElements(result);
  321. }
  322. //----------------------------------------------------------------------------
  323. void cmGeneratorExpression::Split(const std::string &input,
  324. std::vector<std::string> &output)
  325. {
  326. std::string::size_type pos = 0;
  327. std::string::size_type lastPos = pos;
  328. while((pos = input.find("$<", lastPos)) != input.npos)
  329. {
  330. std::string part = input.substr(lastPos, pos - lastPos);
  331. std::string preGenex;
  332. if (!part.empty())
  333. {
  334. std::string::size_type startPos = input.rfind(";", pos);
  335. if (startPos == std::string::npos)
  336. {
  337. preGenex = part;
  338. part = "";
  339. }
  340. else if (startPos != pos - 1 && startPos >= lastPos)
  341. {
  342. part = input.substr(lastPos, startPos - lastPos);
  343. preGenex = input.substr(startPos + 1, pos - startPos - 1);
  344. }
  345. if(!part.empty())
  346. {
  347. cmSystemTools::ExpandListArgument(part, output);
  348. }
  349. }
  350. pos += 2;
  351. int nestingLevel = 1;
  352. const char *c = input.c_str() + pos;
  353. const char * const cStart = c;
  354. for ( ; *c; ++c)
  355. {
  356. if(c[0] == '$' && c[1] == '<')
  357. {
  358. ++nestingLevel;
  359. ++c;
  360. continue;
  361. }
  362. if(c[0] == '>')
  363. {
  364. --nestingLevel;
  365. if (nestingLevel == 0)
  366. {
  367. break;
  368. }
  369. }
  370. }
  371. for ( ; *c; ++c)
  372. {
  373. // Capture the part after the genex and before the next ';'
  374. if(c[0] == ';')
  375. {
  376. --c;
  377. break;
  378. }
  379. }
  380. const std::string::size_type traversed = (c - cStart) + 1;
  381. output.push_back(preGenex + "$<" + input.substr(pos, traversed));
  382. pos += traversed;
  383. lastPos = pos;
  384. }
  385. if (lastPos < input.size())
  386. {
  387. cmSystemTools::ExpandListArgument(input.substr(lastPos), output);
  388. }
  389. }
  390. //----------------------------------------------------------------------------
  391. std::string cmGeneratorExpression::Preprocess(const std::string &input,
  392. PreprocessContext context,
  393. bool resolveRelative)
  394. {
  395. if (context == StripAllGeneratorExpressions)
  396. {
  397. return stripAllGeneratorExpressions(input);
  398. }
  399. else if (context == BuildInterface || context == InstallInterface)
  400. {
  401. return stripExportInterface(input, context, resolveRelative);
  402. }
  403. assert(!"cmGeneratorExpression::Preprocess called with invalid args");
  404. return std::string();
  405. }
  406. //----------------------------------------------------------------------------
  407. std::string::size_type cmGeneratorExpression::Find(const std::string &input)
  408. {
  409. const std::string::size_type openpos = input.find("$<");
  410. if (openpos != std::string::npos
  411. && input.find(">", openpos) != std::string::npos)
  412. {
  413. return openpos;
  414. }
  415. return std::string::npos;
  416. }
  417. //----------------------------------------------------------------------------
  418. bool cmGeneratorExpression::IsValidTargetName(const std::string &input)
  419. {
  420. cmsys::RegularExpression targetNameValidator;
  421. // The ':' is supported to allow use with IMPORTED targets. At least
  422. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  423. targetNameValidator.compile("^[A-Za-z0-9_.:+-]+$");
  424. return targetNameValidator.find(input.c_str());
  425. }