cmGeneratorExpression.cxx 14 KB

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