cmGeneratorExpression.cxx 14 KB

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