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