cmGeneratorExpression.cxx 13 KB

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