cmGeneratorExpression.cxx 14 KB

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