cmGeneratorExpression.cxx 14 KB

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