cmGeneratorExpression.cxx 14 KB

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