cmGeneratorExpressionEvaluator.cxx 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2012 Stephen Kelly <[email protected]>
  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 "cmMakefile.h"
  11. #include "cmGeneratorExpressionEvaluator.h"
  12. #include "cmGeneratorExpressionParser.h"
  13. #include "cmGeneratorExpressionDAGChecker.h"
  14. #include "cmGeneratorExpression.h"
  15. #include <cmsys/String.h>
  16. #include <assert.h>
  17. //----------------------------------------------------------------------------
  18. #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x510
  19. static
  20. #endif
  21. void reportError(cmGeneratorExpressionContext *context,
  22. const std::string &expr, const std::string &result)
  23. {
  24. context->HadError = true;
  25. if (context->Quiet)
  26. {
  27. return;
  28. }
  29. cmOStringStream e;
  30. e << "Error evaluating generator expression:\n"
  31. << " " << expr << "\n"
  32. << result;
  33. context->Makefile->GetCMakeInstance()
  34. ->IssueMessage(cmake::FATAL_ERROR, e.str().c_str(),
  35. context->Backtrace);
  36. }
  37. //----------------------------------------------------------------------------
  38. struct cmGeneratorExpressionNode
  39. {
  40. virtual ~cmGeneratorExpressionNode() {}
  41. virtual bool GeneratesContent() const { return true; }
  42. virtual bool RequiresLiteralInput() const { return false; }
  43. virtual bool AcceptsSingleArbitraryContentParameter() const
  44. { return false; }
  45. virtual int NumExpectedParameters() const { return 1; }
  46. virtual std::string Evaluate(const std::vector<std::string> &parameters,
  47. cmGeneratorExpressionContext *context,
  48. const GeneratorExpressionContent *content,
  49. cmGeneratorExpressionDAGChecker *dagChecker
  50. ) const = 0;
  51. };
  52. //----------------------------------------------------------------------------
  53. static const struct ZeroNode : public cmGeneratorExpressionNode
  54. {
  55. ZeroNode() {}
  56. virtual bool GeneratesContent() const { return false; }
  57. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  58. std::string Evaluate(const std::vector<std::string> &,
  59. cmGeneratorExpressionContext *,
  60. const GeneratorExpressionContent *,
  61. cmGeneratorExpressionDAGChecker *) const
  62. {
  63. // Unreachable
  64. return std::string();
  65. }
  66. } zeroNode;
  67. //----------------------------------------------------------------------------
  68. static const struct OneNode : public cmGeneratorExpressionNode
  69. {
  70. OneNode() {}
  71. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  72. std::string Evaluate(const std::vector<std::string> &,
  73. cmGeneratorExpressionContext *,
  74. const GeneratorExpressionContent *,
  75. cmGeneratorExpressionDAGChecker *) const
  76. {
  77. // Unreachable
  78. return std::string();
  79. }
  80. } oneNode;
  81. //----------------------------------------------------------------------------
  82. static const struct OneNode buildInterfaceNode;
  83. //----------------------------------------------------------------------------
  84. static const struct ZeroNode installInterfaceNode;
  85. //----------------------------------------------------------------------------
  86. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  87. static const struct OP ## Node : public cmGeneratorExpressionNode \
  88. { \
  89. OP ## Node () {} \
  90. /* We let -1 carry the meaning 'at least one' */ \
  91. virtual int NumExpectedParameters() const { return -1; } \
  92. \
  93. std::string Evaluate(const std::vector<std::string> &parameters, \
  94. cmGeneratorExpressionContext *context, \
  95. const GeneratorExpressionContent *content, \
  96. cmGeneratorExpressionDAGChecker *) const \
  97. { \
  98. std::vector<std::string>::const_iterator it = parameters.begin(); \
  99. const std::vector<std::string>::const_iterator end = parameters.end(); \
  100. for ( ; it != end; ++it) \
  101. { \
  102. if (*it == #FAILURE_VALUE) \
  103. { \
  104. return #FAILURE_VALUE; \
  105. } \
  106. else if (*it != #SUCCESS_VALUE) \
  107. { \
  108. reportError(context, content->GetOriginalExpression(), \
  109. "Parameters to $<" #OP "> must resolve to either '0' or '1'."); \
  110. return std::string(); \
  111. } \
  112. } \
  113. return #SUCCESS_VALUE; \
  114. } \
  115. } OPNAME;
  116. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  117. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  118. #undef BOOLEAN_OP_NODE
  119. //----------------------------------------------------------------------------
  120. static const struct NotNode : public cmGeneratorExpressionNode
  121. {
  122. NotNode() {}
  123. std::string Evaluate(const std::vector<std::string> &parameters,
  124. cmGeneratorExpressionContext *context,
  125. const GeneratorExpressionContent *content,
  126. cmGeneratorExpressionDAGChecker *) const
  127. {
  128. if (*parameters.begin() != "0" && *parameters.begin() != "1")
  129. {
  130. reportError(context, content->GetOriginalExpression(),
  131. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  132. return std::string();
  133. }
  134. return *parameters.begin() == "0" ? "1" : "0";
  135. }
  136. } notNode;
  137. //----------------------------------------------------------------------------
  138. static const struct BoolNode : public cmGeneratorExpressionNode
  139. {
  140. BoolNode() {}
  141. virtual int NumExpectedParameters() const { return 1; }
  142. std::string Evaluate(const std::vector<std::string> &parameters,
  143. cmGeneratorExpressionContext *,
  144. const GeneratorExpressionContent *,
  145. cmGeneratorExpressionDAGChecker *) const
  146. {
  147. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  148. }
  149. } boolNode;
  150. //----------------------------------------------------------------------------
  151. static const struct StrEqualNode : public cmGeneratorExpressionNode
  152. {
  153. StrEqualNode() {}
  154. virtual int NumExpectedParameters() const { return 2; }
  155. std::string Evaluate(const std::vector<std::string> &parameters,
  156. cmGeneratorExpressionContext *,
  157. const GeneratorExpressionContent *,
  158. cmGeneratorExpressionDAGChecker *) const
  159. {
  160. return *parameters.begin() == parameters[1] ? "1" : "0";
  161. }
  162. } strEqualNode;
  163. //----------------------------------------------------------------------------
  164. static const struct Angle_RNode : public cmGeneratorExpressionNode
  165. {
  166. Angle_RNode() {}
  167. virtual int NumExpectedParameters() const { return 0; }
  168. std::string Evaluate(const std::vector<std::string> &,
  169. cmGeneratorExpressionContext *,
  170. const GeneratorExpressionContent *,
  171. cmGeneratorExpressionDAGChecker *) const
  172. {
  173. return ">";
  174. }
  175. } angle_rNode;
  176. //----------------------------------------------------------------------------
  177. static const struct CommaNode : public cmGeneratorExpressionNode
  178. {
  179. CommaNode() {}
  180. virtual int NumExpectedParameters() const { return 0; }
  181. std::string Evaluate(const std::vector<std::string> &,
  182. cmGeneratorExpressionContext *,
  183. const GeneratorExpressionContent *,
  184. cmGeneratorExpressionDAGChecker *) const
  185. {
  186. return ",";
  187. }
  188. } commaNode;
  189. //----------------------------------------------------------------------------
  190. static const struct SemicolonNode : public cmGeneratorExpressionNode
  191. {
  192. SemicolonNode() {}
  193. virtual int NumExpectedParameters() const { return 0; }
  194. std::string Evaluate(const std::vector<std::string> &,
  195. cmGeneratorExpressionContext *,
  196. const GeneratorExpressionContent *,
  197. cmGeneratorExpressionDAGChecker *) const
  198. {
  199. return ";";
  200. }
  201. } semicolonNode;
  202. //----------------------------------------------------------------------------
  203. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  204. {
  205. ConfigurationNode() {}
  206. virtual int NumExpectedParameters() const { return 0; }
  207. std::string Evaluate(const std::vector<std::string> &,
  208. cmGeneratorExpressionContext *context,
  209. const GeneratorExpressionContent *,
  210. cmGeneratorExpressionDAGChecker *) const
  211. {
  212. context->HadContextSensitiveCondition = true;
  213. return context->Config ? context->Config : "";
  214. }
  215. } configurationNode;
  216. //----------------------------------------------------------------------------
  217. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  218. {
  219. ConfigurationTestNode() {}
  220. virtual int NumExpectedParameters() const { return 1; }
  221. std::string Evaluate(const std::vector<std::string> &parameters,
  222. cmGeneratorExpressionContext *context,
  223. const GeneratorExpressionContent *content,
  224. cmGeneratorExpressionDAGChecker *) const
  225. {
  226. cmsys::RegularExpression configValidator;
  227. configValidator.compile("^[A-Za-z0-9_]*$");
  228. if (!configValidator.find(parameters.begin()->c_str()))
  229. {
  230. reportError(context, content->GetOriginalExpression(),
  231. "Expression syntax not recognized.");
  232. return std::string();
  233. }
  234. context->HadContextSensitiveCondition = true;
  235. if (!context->Config)
  236. {
  237. return parameters.front().empty() ? "1" : "0";
  238. }
  239. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  240. context->Config) == 0)
  241. {
  242. return "1";
  243. }
  244. if (context->CurrentTarget
  245. && context->CurrentTarget->IsImported())
  246. {
  247. const char* loc = 0;
  248. const char* imp = 0;
  249. std::string suffix;
  250. return context->CurrentTarget->GetMappedConfig(context->Config,
  251. &loc,
  252. &imp,
  253. suffix) ? "1" : "0";
  254. }
  255. return "0";
  256. }
  257. } configurationTestNode;
  258. //----------------------------------------------------------------------------
  259. static const char* targetPropertyTransitiveWhitelist[] = {
  260. "INTERFACE_INCLUDE_DIRECTORIES"
  261. , "INTERFACE_COMPILE_DEFINITIONS"
  262. };
  263. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  264. cmTarget *target,
  265. cmGeneratorExpressionContext *context,
  266. cmGeneratorExpressionDAGChecker *dagChecker,
  267. const std::string &interfacePropertyName)
  268. {
  269. cmGeneratorExpression ge(context->Backtrace);
  270. std::string sep;
  271. std::string depString;
  272. for (std::vector<std::string>::const_iterator
  273. it = libraries.begin();
  274. it != libraries.end(); ++it)
  275. {
  276. if (*it == target->GetName())
  277. {
  278. // Broken code can have a target in its own link interface.
  279. // Don't follow such link interface entries so as not to create a
  280. // self-referencing loop.
  281. continue;
  282. }
  283. if (context->Makefile->FindTargetToUse(it->c_str()))
  284. {
  285. depString +=
  286. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  287. sep = ";";
  288. }
  289. }
  290. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  291. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  292. context->Config,
  293. context->Quiet,
  294. context->HeadTarget,
  295. target,
  296. dagChecker);
  297. if (cge->GetHadContextSensitiveCondition())
  298. {
  299. context->HadContextSensitiveCondition = true;
  300. }
  301. return linkedTargetsContent;
  302. }
  303. //----------------------------------------------------------------------------
  304. struct TransitiveWhitelistCompare
  305. {
  306. explicit TransitiveWhitelistCompare(const std::string &needle)
  307. : Needle(needle) {}
  308. bool operator() (const char *item)
  309. { return strcmp(item, this->Needle.c_str()) == 0; }
  310. private:
  311. std::string Needle;
  312. };
  313. //----------------------------------------------------------------------------
  314. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  315. {
  316. TargetPropertyNode() {}
  317. // This node handles errors on parameter count itself.
  318. virtual int NumExpectedParameters() const { return -1; }
  319. std::string Evaluate(const std::vector<std::string> &parameters,
  320. cmGeneratorExpressionContext *context,
  321. const GeneratorExpressionContent *content,
  322. cmGeneratorExpressionDAGChecker *dagCheckerParent
  323. ) const
  324. {
  325. if (parameters.size() != 1 && parameters.size() != 2)
  326. {
  327. reportError(context, content->GetOriginalExpression(),
  328. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  329. return std::string();
  330. }
  331. cmsys::RegularExpression propertyNameValidator;
  332. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  333. cmTarget* target = context->HeadTarget;
  334. std::string propertyName = *parameters.begin();
  335. if (!target && parameters.size() == 1)
  336. {
  337. reportError(context, content->GetOriginalExpression(),
  338. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  339. "be used with add_custom_command. Specify the target to read a "
  340. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  341. "instead.");
  342. return std::string();
  343. }
  344. if (parameters.size() == 2)
  345. {
  346. if (parameters.begin()->empty() && parameters[1].empty())
  347. {
  348. reportError(context, content->GetOriginalExpression(),
  349. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  350. "target name and property name.");
  351. return std::string();
  352. }
  353. if (parameters.begin()->empty())
  354. {
  355. reportError(context, content->GetOriginalExpression(),
  356. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  357. "target name.");
  358. return std::string();
  359. }
  360. std::string targetName = parameters.front();
  361. propertyName = parameters[1];
  362. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  363. {
  364. if (!propertyNameValidator.find(propertyName.c_str()))
  365. {
  366. ::reportError(context, content->GetOriginalExpression(),
  367. "Target name and property name not supported.");
  368. return std::string();
  369. }
  370. ::reportError(context, content->GetOriginalExpression(),
  371. "Target name not supported.");
  372. return std::string();
  373. }
  374. target = context->Makefile->FindTargetToUse(
  375. targetName.c_str());
  376. if (!target)
  377. {
  378. cmOStringStream e;
  379. e << "Target \""
  380. << targetName
  381. << "\" not found.";
  382. reportError(context, content->GetOriginalExpression(), e.str());
  383. return std::string();
  384. }
  385. context->AllTargets.insert(target);
  386. }
  387. if (target == context->HeadTarget)
  388. {
  389. // Keep track of the properties seen while processing.
  390. // The evaluation of the LINK_LIBRARIES generator expressions
  391. // will check this to ensure that properties have one consistent
  392. // value for all evaluations.
  393. context->SeenTargetProperties.insert(propertyName);
  394. }
  395. if (propertyName.empty())
  396. {
  397. reportError(context, content->GetOriginalExpression(),
  398. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  399. "name.");
  400. return std::string();
  401. }
  402. if (!propertyNameValidator.find(propertyName.c_str()))
  403. {
  404. ::reportError(context, content->GetOriginalExpression(),
  405. "Property name not supported.");
  406. return std::string();
  407. }
  408. assert(target);
  409. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  410. target->GetName(),
  411. propertyName,
  412. content,
  413. dagCheckerParent);
  414. switch (dagChecker.check())
  415. {
  416. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  417. dagChecker.reportError(context, content->GetOriginalExpression());
  418. return std::string();
  419. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  420. // No error. We just skip cyclic references.
  421. return std::string();
  422. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  423. for (size_t i = 0;
  424. i < (sizeof(targetPropertyTransitiveWhitelist) /
  425. sizeof(*targetPropertyTransitiveWhitelist));
  426. ++i)
  427. {
  428. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  429. {
  430. // No error. We're not going to find anything new here.
  431. return std::string();
  432. }
  433. }
  434. case cmGeneratorExpressionDAGChecker::DAG:
  435. break;
  436. }
  437. const char *prop = target->GetProperty(propertyName.c_str());
  438. if (dagCheckerParent)
  439. {
  440. if (dagCheckerParent->EvaluatingLinkLibraries())
  441. {
  442. if(!prop)
  443. {
  444. return std::string();
  445. }
  446. }
  447. else
  448. {
  449. assert(dagCheckerParent->EvaluatingIncludeDirectories()
  450. || dagCheckerParent->EvaluatingCompileDefinitions());
  451. }
  452. }
  453. std::string linkedTargetsContent;
  454. std::string interfacePropertyName;
  455. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  456. || propertyName == "INCLUDE_DIRECTORIES")
  457. {
  458. interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
  459. }
  460. else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
  461. || propertyName == "COMPILE_DEFINITIONS"
  462. || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
  463. {
  464. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  465. }
  466. const char **transBegin = targetPropertyTransitiveWhitelist;
  467. const char **transEnd = targetPropertyTransitiveWhitelist
  468. + (sizeof(targetPropertyTransitiveWhitelist) /
  469. sizeof(*targetPropertyTransitiveWhitelist));
  470. if (std::find_if(transBegin, transEnd,
  471. TransitiveWhitelistCompare(interfacePropertyName)) != transEnd)
  472. {
  473. const cmTarget::LinkInterface *iface = target->GetLinkInterface(
  474. context->Config,
  475. context->HeadTarget);
  476. if(iface)
  477. {
  478. linkedTargetsContent =
  479. getLinkedTargetsContent(iface->Libraries, target,
  480. context, &dagChecker,
  481. interfacePropertyName);
  482. }
  483. }
  484. linkedTargetsContent =
  485. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  486. if (!prop)
  487. {
  488. if (target->IsImported())
  489. {
  490. return linkedTargetsContent;
  491. }
  492. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  493. context->Config))
  494. {
  495. context->HadContextSensitiveCondition = true;
  496. return target->GetLinkInterfaceDependentBoolProperty(
  497. propertyName,
  498. context->Config) ? "1" : "0";
  499. }
  500. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  501. context->Config))
  502. {
  503. context->HadContextSensitiveCondition = true;
  504. const char *propContent =
  505. target->GetLinkInterfaceDependentStringProperty(
  506. propertyName,
  507. context->Config);
  508. return propContent ? propContent : "";
  509. }
  510. return linkedTargetsContent;
  511. }
  512. for (size_t i = 0;
  513. i < (sizeof(targetPropertyTransitiveWhitelist) /
  514. sizeof(*targetPropertyTransitiveWhitelist));
  515. ++i)
  516. {
  517. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  518. {
  519. cmGeneratorExpression ge(context->Backtrace);
  520. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  521. std::string result = cge->Evaluate(context->Makefile,
  522. context->Config,
  523. context->Quiet,
  524. context->HeadTarget,
  525. target,
  526. &dagChecker);
  527. if (cge->GetHadContextSensitiveCondition())
  528. {
  529. context->HadContextSensitiveCondition = true;
  530. }
  531. if (!linkedTargetsContent.empty())
  532. {
  533. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  534. }
  535. return result;
  536. }
  537. }
  538. return prop;
  539. }
  540. } targetPropertyNode;
  541. //----------------------------------------------------------------------------
  542. static const struct TargetNameNode : public cmGeneratorExpressionNode
  543. {
  544. TargetNameNode() {}
  545. virtual bool GeneratesContent() const { return true; }
  546. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  547. virtual bool RequiresLiteralInput() const { return true; }
  548. std::string Evaluate(const std::vector<std::string> &parameters,
  549. cmGeneratorExpressionContext *,
  550. const GeneratorExpressionContent *,
  551. cmGeneratorExpressionDAGChecker *) const
  552. {
  553. return parameters.front();
  554. }
  555. virtual int NumExpectedParameters() const { return 1; }
  556. } targetNameNode;
  557. //----------------------------------------------------------------------------
  558. static const char* targetPolicyWhitelist[] = {
  559. "CMP0003"
  560. , "CMP0004"
  561. , "CMP0008"
  562. , "CMP0020"
  563. };
  564. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  565. {
  566. #define RETURN_POLICY(POLICY) \
  567. if (strcmp(policy, #POLICY) == 0) \
  568. { \
  569. return tgt->GetPolicyStatus ## POLICY (); \
  570. } \
  571. RETURN_POLICY(CMP0003)
  572. RETURN_POLICY(CMP0004)
  573. RETURN_POLICY(CMP0008)
  574. RETURN_POLICY(CMP0020)
  575. #undef RETURN_POLICY
  576. assert("!Unreachable code. Not a valid policy");
  577. return cmPolicies::WARN;
  578. }
  579. cmPolicies::PolicyID policyForString(const char *policy_id)
  580. {
  581. #define RETURN_POLICY_ID(POLICY_ID) \
  582. if (strcmp(policy_id, #POLICY_ID) == 0) \
  583. { \
  584. return cmPolicies:: POLICY_ID; \
  585. } \
  586. RETURN_POLICY_ID(CMP0003)
  587. RETURN_POLICY_ID(CMP0004)
  588. RETURN_POLICY_ID(CMP0008)
  589. RETURN_POLICY_ID(CMP0020)
  590. #undef RETURN_POLICY_ID
  591. assert("!Unreachable code. Not a valid policy");
  592. return cmPolicies::CMP0002;
  593. }
  594. //----------------------------------------------------------------------------
  595. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  596. {
  597. TargetPolicyNode() {}
  598. virtual int NumExpectedParameters() const { return 1; }
  599. std::string Evaluate(const std::vector<std::string> &parameters,
  600. cmGeneratorExpressionContext *context ,
  601. const GeneratorExpressionContent *content,
  602. cmGeneratorExpressionDAGChecker *) const
  603. {
  604. if (!context->HeadTarget)
  605. {
  606. reportError(context, content->GetOriginalExpression(),
  607. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  608. "be used with add_custom_command.");
  609. return std::string();
  610. }
  611. context->HadContextSensitiveCondition = true;
  612. for (size_t i = 0;
  613. i < (sizeof(targetPolicyWhitelist) /
  614. sizeof(*targetPolicyWhitelist));
  615. ++i)
  616. {
  617. const char *policy = targetPolicyWhitelist[i];
  618. if (parameters.front() == policy)
  619. {
  620. cmMakefile *mf = context->HeadTarget->GetMakefile();
  621. switch(statusForTarget(context->HeadTarget, policy))
  622. {
  623. case cmPolicies::WARN:
  624. mf->IssueMessage(cmake::AUTHOR_WARNING,
  625. mf->GetPolicies()->
  626. GetPolicyWarning(policyForString(policy)));
  627. case cmPolicies::REQUIRED_IF_USED:
  628. case cmPolicies::REQUIRED_ALWAYS:
  629. case cmPolicies::OLD:
  630. return "0";
  631. case cmPolicies::NEW:
  632. return "1";
  633. }
  634. }
  635. }
  636. reportError(context, content->GetOriginalExpression(),
  637. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  638. "policies. Currently it may be used with policies CMP0003, CMP0004, "
  639. "CMP0008 and CMP0020."
  640. );
  641. return std::string();
  642. }
  643. } targetPolicyNode;
  644. //----------------------------------------------------------------------------
  645. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  646. {
  647. InstallPrefixNode() {}
  648. virtual bool GeneratesContent() const { return true; }
  649. virtual int NumExpectedParameters() const { return 0; }
  650. std::string Evaluate(const std::vector<std::string> &,
  651. cmGeneratorExpressionContext *context,
  652. const GeneratorExpressionContent *content,
  653. cmGeneratorExpressionDAGChecker *) const
  654. {
  655. reportError(context, content->GetOriginalExpression(),
  656. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  657. "should never be evaluated.");
  658. return std::string();
  659. }
  660. } installPrefixNode;
  661. //----------------------------------------------------------------------------
  662. template<bool linker, bool soname>
  663. struct TargetFilesystemArtifactResultCreator
  664. {
  665. static std::string Create(cmTarget* target,
  666. cmGeneratorExpressionContext *context,
  667. const GeneratorExpressionContent *content);
  668. };
  669. //----------------------------------------------------------------------------
  670. template<>
  671. struct TargetFilesystemArtifactResultCreator<false, true>
  672. {
  673. static std::string Create(cmTarget* target,
  674. cmGeneratorExpressionContext *context,
  675. const GeneratorExpressionContent *content)
  676. {
  677. // The target soname file (.so.1).
  678. if(target->IsDLLPlatform())
  679. {
  680. ::reportError(context, content->GetOriginalExpression(),
  681. "TARGET_SONAME_FILE is not allowed "
  682. "for DLL target platforms.");
  683. return std::string();
  684. }
  685. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  686. {
  687. ::reportError(context, content->GetOriginalExpression(),
  688. "TARGET_SONAME_FILE is allowed only for "
  689. "SHARED libraries.");
  690. return std::string();
  691. }
  692. std::string result = target->GetDirectory(context->Config);
  693. result += "/";
  694. result += target->GetSOName(context->Config);
  695. return result;
  696. }
  697. };
  698. //----------------------------------------------------------------------------
  699. template<>
  700. struct TargetFilesystemArtifactResultCreator<true, false>
  701. {
  702. static std::string Create(cmTarget* target,
  703. cmGeneratorExpressionContext *context,
  704. const GeneratorExpressionContent *content)
  705. {
  706. // The file used to link to the target (.so, .lib, .a).
  707. if(!target->IsLinkable())
  708. {
  709. ::reportError(context, content->GetOriginalExpression(),
  710. "TARGET_LINKER_FILE is allowed only for libraries and "
  711. "executables with ENABLE_EXPORTS.");
  712. return std::string();
  713. }
  714. return target->GetFullPath(context->Config,
  715. target->HasImportLibrary());
  716. }
  717. };
  718. //----------------------------------------------------------------------------
  719. template<>
  720. struct TargetFilesystemArtifactResultCreator<false, false>
  721. {
  722. static std::string Create(cmTarget* target,
  723. cmGeneratorExpressionContext *context,
  724. const GeneratorExpressionContent *)
  725. {
  726. return target->GetFullPath(context->Config, false, true);
  727. }
  728. };
  729. //----------------------------------------------------------------------------
  730. template<bool dirQual, bool nameQual>
  731. struct TargetFilesystemArtifactResultGetter
  732. {
  733. static std::string Get(const std::string &result);
  734. };
  735. //----------------------------------------------------------------------------
  736. template<>
  737. struct TargetFilesystemArtifactResultGetter<false, true>
  738. {
  739. static std::string Get(const std::string &result)
  740. { return cmSystemTools::GetFilenameName(result); }
  741. };
  742. //----------------------------------------------------------------------------
  743. template<>
  744. struct TargetFilesystemArtifactResultGetter<true, false>
  745. {
  746. static std::string Get(const std::string &result)
  747. { return cmSystemTools::GetFilenamePath(result); }
  748. };
  749. //----------------------------------------------------------------------------
  750. template<>
  751. struct TargetFilesystemArtifactResultGetter<false, false>
  752. {
  753. static std::string Get(const std::string &result)
  754. { return result; }
  755. };
  756. //----------------------------------------------------------------------------
  757. template<bool linker, bool soname, bool dirQual, bool nameQual>
  758. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  759. {
  760. TargetFilesystemArtifact() {}
  761. virtual int NumExpectedParameters() const { return 1; }
  762. std::string Evaluate(const std::vector<std::string> &parameters,
  763. cmGeneratorExpressionContext *context,
  764. const GeneratorExpressionContent *content,
  765. cmGeneratorExpressionDAGChecker *) const
  766. {
  767. // Lookup the referenced target.
  768. std::string name = *parameters.begin();
  769. if (!cmGeneratorExpression::IsValidTargetName(name))
  770. {
  771. ::reportError(context, content->GetOriginalExpression(),
  772. "Expression syntax not recognized.");
  773. return std::string();
  774. }
  775. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  776. if(!target)
  777. {
  778. ::reportError(context, content->GetOriginalExpression(),
  779. "No target \"" + name + "\"");
  780. return std::string();
  781. }
  782. if(target->GetType() >= cmTarget::UTILITY &&
  783. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  784. {
  785. ::reportError(context, content->GetOriginalExpression(),
  786. "Target \"" + name + "\" is not an executable or library.");
  787. return std::string();
  788. }
  789. context->DependTargets.insert(target);
  790. context->AllTargets.insert(target);
  791. std::string result =
  792. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  793. target,
  794. context,
  795. content);
  796. if (context->HadError)
  797. {
  798. return std::string();
  799. }
  800. return
  801. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  802. }
  803. };
  804. //----------------------------------------------------------------------------
  805. static const
  806. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  807. static const
  808. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  809. static const
  810. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  811. static const
  812. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  813. static const
  814. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  815. static const
  816. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  817. static const
  818. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  819. static const
  820. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  821. static const
  822. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  823. //----------------------------------------------------------------------------
  824. static const
  825. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  826. {
  827. if (identifier == "0")
  828. return &zeroNode;
  829. else if (identifier == "1")
  830. return &oneNode;
  831. else if (identifier == "AND")
  832. return &andNode;
  833. else if (identifier == "OR")
  834. return &orNode;
  835. else if (identifier == "NOT")
  836. return &notNode;
  837. else if (identifier == "CONFIGURATION")
  838. return &configurationNode;
  839. else if (identifier == "CONFIG")
  840. return &configurationTestNode;
  841. else if (identifier == "TARGET_FILE")
  842. return &targetFileNode;
  843. else if (identifier == "TARGET_LINKER_FILE")
  844. return &targetLinkerFileNode;
  845. else if (identifier == "TARGET_SONAME_FILE")
  846. return &targetSoNameFileNode;
  847. else if (identifier == "TARGET_FILE_NAME")
  848. return &targetFileNameNode;
  849. else if (identifier == "TARGET_LINKER_FILE_NAME")
  850. return &targetLinkerFileNameNode;
  851. else if (identifier == "TARGET_SONAME_FILE_NAME")
  852. return &targetSoNameFileNameNode;
  853. else if (identifier == "TARGET_FILE_DIR")
  854. return &targetFileDirNode;
  855. else if (identifier == "TARGET_LINKER_FILE_DIR")
  856. return &targetLinkerFileDirNode;
  857. else if (identifier == "TARGET_SONAME_FILE_DIR")
  858. return &targetSoNameFileDirNode;
  859. else if (identifier == "STREQUAL")
  860. return &strEqualNode;
  861. else if (identifier == "BOOL")
  862. return &boolNode;
  863. else if (identifier == "ANGLE-R")
  864. return &angle_rNode;
  865. else if (identifier == "COMMA")
  866. return &commaNode;
  867. else if (identifier == "SEMICOLON")
  868. return &semicolonNode;
  869. else if (identifier == "TARGET_PROPERTY")
  870. return &targetPropertyNode;
  871. else if (identifier == "TARGET_NAME")
  872. return &targetNameNode;
  873. else if (identifier == "TARGET_POLICY")
  874. return &targetPolicyNode;
  875. else if (identifier == "BUILD_INTERFACE")
  876. return &buildInterfaceNode;
  877. else if (identifier == "INSTALL_INTERFACE")
  878. return &installInterfaceNode;
  879. else if (identifier == "INSTALL_PREFIX")
  880. return &installPrefixNode;
  881. return 0;
  882. }
  883. //----------------------------------------------------------------------------
  884. GeneratorExpressionContent::GeneratorExpressionContent(
  885. const char *startContent,
  886. unsigned int length)
  887. : StartContent(startContent), ContentLength(length)
  888. {
  889. }
  890. //----------------------------------------------------------------------------
  891. std::string GeneratorExpressionContent::GetOriginalExpression() const
  892. {
  893. return std::string(this->StartContent, this->ContentLength);
  894. }
  895. //----------------------------------------------------------------------------
  896. std::string GeneratorExpressionContent::Evaluate(
  897. cmGeneratorExpressionContext *context,
  898. cmGeneratorExpressionDAGChecker *dagChecker) const
  899. {
  900. std::string identifier;
  901. {
  902. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  903. = this->IdentifierChildren.begin();
  904. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  905. = this->IdentifierChildren.end();
  906. for ( ; it != end; ++it)
  907. {
  908. identifier += (*it)->Evaluate(context, dagChecker);
  909. if (context->HadError)
  910. {
  911. return std::string();
  912. }
  913. }
  914. }
  915. const cmGeneratorExpressionNode *node = GetNode(identifier);
  916. if (!node)
  917. {
  918. reportError(context, this->GetOriginalExpression(),
  919. "Expression did not evaluate to a known generator expression");
  920. return std::string();
  921. }
  922. if (!node->GeneratesContent())
  923. {
  924. if (node->AcceptsSingleArbitraryContentParameter())
  925. {
  926. if (this->ParamChildren.empty())
  927. {
  928. reportError(context, this->GetOriginalExpression(),
  929. "$<" + identifier + "> expression requires a parameter.");
  930. }
  931. }
  932. else
  933. {
  934. std::vector<std::string> parameters;
  935. this->EvaluateParameters(node, identifier, context, dagChecker,
  936. parameters);
  937. }
  938. return std::string();
  939. }
  940. if (node->AcceptsSingleArbitraryContentParameter())
  941. {
  942. std::string result;
  943. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  944. pit = this->ParamChildren.begin();
  945. const
  946. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  947. pend = this->ParamChildren.end();
  948. for ( ; pit != pend; ++pit)
  949. {
  950. if (!result.empty())
  951. {
  952. result += ",";
  953. }
  954. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  955. = pit->begin();
  956. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  957. = pit->end();
  958. for ( ; it != end; ++it)
  959. {
  960. if (node->RequiresLiteralInput())
  961. {
  962. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  963. {
  964. reportError(context, this->GetOriginalExpression(),
  965. "$<" + identifier + "> expression requires literal input.");
  966. return std::string();
  967. }
  968. }
  969. result += (*it)->Evaluate(context, dagChecker);
  970. if (context->HadError)
  971. {
  972. return std::string();
  973. }
  974. }
  975. }
  976. if (node->RequiresLiteralInput())
  977. {
  978. std::vector<std::string> parameters;
  979. parameters.push_back(result);
  980. return node->Evaluate(parameters, context, this, dagChecker);
  981. }
  982. return result;
  983. }
  984. std::vector<std::string> parameters;
  985. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  986. if (context->HadError)
  987. {
  988. return std::string();
  989. }
  990. return node->Evaluate(parameters, context, this, dagChecker);
  991. }
  992. //----------------------------------------------------------------------------
  993. std::string GeneratorExpressionContent::EvaluateParameters(
  994. const cmGeneratorExpressionNode *node,
  995. const std::string &identifier,
  996. cmGeneratorExpressionContext *context,
  997. cmGeneratorExpressionDAGChecker *dagChecker,
  998. std::vector<std::string> &parameters) const
  999. {
  1000. {
  1001. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1002. pit = this->ParamChildren.begin();
  1003. const
  1004. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1005. pend = this->ParamChildren.end();
  1006. for ( ; pit != pend; ++pit)
  1007. {
  1008. std::string parameter;
  1009. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1010. pit->begin();
  1011. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1012. pit->end();
  1013. for ( ; it != end; ++it)
  1014. {
  1015. parameter += (*it)->Evaluate(context, dagChecker);
  1016. if (context->HadError)
  1017. {
  1018. return std::string();
  1019. }
  1020. }
  1021. parameters.push_back(parameter);
  1022. }
  1023. }
  1024. int numExpected = node->NumExpectedParameters();
  1025. if ((numExpected != -1 && (unsigned int)numExpected != parameters.size()))
  1026. {
  1027. if (numExpected == 0)
  1028. {
  1029. reportError(context, this->GetOriginalExpression(),
  1030. "$<" + identifier + "> expression requires no parameters.");
  1031. }
  1032. else if (numExpected == 1)
  1033. {
  1034. reportError(context, this->GetOriginalExpression(),
  1035. "$<" + identifier + "> expression requires "
  1036. "exactly one parameter.");
  1037. }
  1038. else
  1039. {
  1040. cmOStringStream e;
  1041. e << "$<" + identifier + "> expression requires "
  1042. << numExpected
  1043. << " comma separated parameters, but got "
  1044. << parameters.size() << " instead.";
  1045. reportError(context, this->GetOriginalExpression(), e.str());
  1046. }
  1047. return std::string();
  1048. }
  1049. if (numExpected == -1 && parameters.empty())
  1050. {
  1051. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1052. + "> expression requires at least one parameter.");
  1053. }
  1054. return std::string();
  1055. }
  1056. //----------------------------------------------------------------------------
  1057. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1058. {
  1059. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1060. = c.begin();
  1061. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1062. = c.end();
  1063. for ( ; it != end; ++it)
  1064. {
  1065. delete *it;
  1066. }
  1067. }
  1068. //----------------------------------------------------------------------------
  1069. GeneratorExpressionContent::~GeneratorExpressionContent()
  1070. {
  1071. deleteAll(this->IdentifierChildren);
  1072. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1073. typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  1074. std::vector<EvaluatorVector>::const_iterator pit =
  1075. this->ParamChildren.begin();
  1076. const std::vector<EvaluatorVector>::const_iterator pend =
  1077. this->ParamChildren.end();
  1078. for ( ; pit != pend; ++pit)
  1079. {
  1080. deleteAll(*pit);
  1081. }
  1082. }