cmGeneratorExpression.cxx 14 KB

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