cmGeneratorExpression.cxx 13 KB

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