cmGeneratorExpressionEvaluator.cxx 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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 ConfigurationNode : public cmGeneratorExpressionNode
  191. {
  192. ConfigurationNode() {}
  193. virtual int NumExpectedParameters() const { return 0; }
  194. std::string Evaluate(const std::vector<std::string> &,
  195. cmGeneratorExpressionContext *context,
  196. const GeneratorExpressionContent *,
  197. cmGeneratorExpressionDAGChecker *) const
  198. {
  199. context->HadContextSensitiveCondition = true;
  200. return context->Config ? context->Config : "";
  201. }
  202. } configurationNode;
  203. //----------------------------------------------------------------------------
  204. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  205. {
  206. ConfigurationTestNode() {}
  207. virtual int NumExpectedParameters() const { return 1; }
  208. std::string Evaluate(const std::vector<std::string> &parameters,
  209. cmGeneratorExpressionContext *context,
  210. const GeneratorExpressionContent *content,
  211. cmGeneratorExpressionDAGChecker *) const
  212. {
  213. cmsys::RegularExpression configValidator;
  214. configValidator.compile("^[A-Za-z0-9_]*$");
  215. if (!configValidator.find(parameters.begin()->c_str()))
  216. {
  217. reportError(context, content->GetOriginalExpression(),
  218. "Expression syntax not recognized.");
  219. return std::string();
  220. }
  221. context->HadContextSensitiveCondition = true;
  222. if (!context->Config)
  223. {
  224. return parameters.front().empty() ? "1" : "0";
  225. }
  226. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  227. context->Config) == 0)
  228. {
  229. return "1";
  230. }
  231. if (context->CurrentTarget
  232. && context->CurrentTarget->IsImported())
  233. {
  234. const char* loc = 0;
  235. const char* imp = 0;
  236. std::string suffix;
  237. return context->CurrentTarget->GetMappedConfig(context->Config,
  238. &loc,
  239. &imp,
  240. suffix) ? "1" : "0";
  241. }
  242. return "0";
  243. }
  244. } configurationTestNode;
  245. static const struct TargetDefinedNode : public cmGeneratorExpressionNode
  246. {
  247. TargetDefinedNode() {}
  248. virtual int NumExpectedParameters() const { return 1; }
  249. std::string Evaluate(const std::vector<std::string> &parameters,
  250. cmGeneratorExpressionContext *context,
  251. const GeneratorExpressionContent *,
  252. cmGeneratorExpressionDAGChecker *) const
  253. {
  254. return context->Makefile->FindTargetToUse(parameters.front().c_str())
  255. ? "1" : "0";
  256. }
  257. } targetDefinedNode;
  258. //----------------------------------------------------------------------------
  259. static const char* targetPropertyTransitiveWhitelist[] = {
  260. "INTERFACE_INCLUDE_DIRECTORIES"
  261. , "INTERFACE_COMPILE_DEFINITIONS"
  262. };
  263. //----------------------------------------------------------------------------
  264. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  265. {
  266. TargetPropertyNode() {}
  267. // This node handles errors on parameter count itself.
  268. virtual int NumExpectedParameters() const { return -1; }
  269. std::string Evaluate(const std::vector<std::string> &parameters,
  270. cmGeneratorExpressionContext *context,
  271. const GeneratorExpressionContent *content,
  272. cmGeneratorExpressionDAGChecker *dagCheckerParent
  273. ) const
  274. {
  275. if (parameters.size() != 1 && parameters.size() != 2)
  276. {
  277. reportError(context, content->GetOriginalExpression(),
  278. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  279. return std::string();
  280. }
  281. cmsys::RegularExpression propertyNameValidator;
  282. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  283. cmTarget* target = context->HeadTarget;
  284. std::string propertyName = *parameters.begin();
  285. if (!target && parameters.size() == 1)
  286. {
  287. reportError(context, content->GetOriginalExpression(),
  288. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  289. "be used with add_custom_command. Specify the target to read a "
  290. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  291. "instead.");
  292. return std::string();
  293. }
  294. if (parameters.size() == 2)
  295. {
  296. if (parameters.begin()->empty() && parameters[1].empty())
  297. {
  298. reportError(context, content->GetOriginalExpression(),
  299. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  300. "target name and property name.");
  301. return std::string();
  302. }
  303. if (parameters.begin()->empty())
  304. {
  305. reportError(context, content->GetOriginalExpression(),
  306. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  307. "target name.");
  308. return std::string();
  309. }
  310. std::string targetName = parameters.front();
  311. propertyName = parameters[1];
  312. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  313. {
  314. if (!propertyNameValidator.find(propertyName.c_str()))
  315. {
  316. ::reportError(context, content->GetOriginalExpression(),
  317. "Target name and property name not supported.");
  318. return std::string();
  319. }
  320. ::reportError(context, content->GetOriginalExpression(),
  321. "Target name not supported.");
  322. return std::string();
  323. }
  324. target = context->Makefile->FindTargetToUse(
  325. targetName.c_str());
  326. if (!target)
  327. {
  328. cmOStringStream e;
  329. e << "Target \""
  330. << targetName
  331. << "\" not found.";
  332. reportError(context, content->GetOriginalExpression(), e.str());
  333. return std::string();
  334. }
  335. }
  336. if (target == context->HeadTarget)
  337. {
  338. // Keep track of the properties seen while processing.
  339. // The evaluation of the LINK_LIBRARIES generator expressions
  340. // will check this to ensure that properties have one consistent
  341. // value for all evaluations.
  342. context->SeenTargetProperties.insert(propertyName);
  343. }
  344. if (propertyName.empty())
  345. {
  346. reportError(context, content->GetOriginalExpression(),
  347. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  348. "name.");
  349. return std::string();
  350. }
  351. if (!propertyNameValidator.find(propertyName.c_str()))
  352. {
  353. ::reportError(context, content->GetOriginalExpression(),
  354. "Property name not supported.");
  355. return std::string();
  356. }
  357. assert(target);
  358. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  359. target->GetName(),
  360. propertyName,
  361. content,
  362. dagCheckerParent);
  363. switch (dagChecker.check())
  364. {
  365. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  366. dagChecker.reportError(context, content->GetOriginalExpression());
  367. return std::string();
  368. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  369. // No error. We just skip cyclic references.
  370. return std::string();
  371. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  372. for (size_t i = 0;
  373. i < (sizeof(targetPropertyTransitiveWhitelist) /
  374. sizeof(*targetPropertyTransitiveWhitelist));
  375. ++i)
  376. {
  377. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  378. {
  379. // No error. We're not going to find anything new here.
  380. return std::string();
  381. }
  382. }
  383. case cmGeneratorExpressionDAGChecker::DAG:
  384. break;
  385. }
  386. const char *prop = target->GetProperty(propertyName.c_str());
  387. std::string linkedTargetsContent;
  388. if (dagCheckerParent)
  389. {
  390. if (dagCheckerParent->EvaluatingLinkLibraries())
  391. {
  392. if(!prop)
  393. {
  394. return std::string();
  395. }
  396. }
  397. else
  398. {
  399. assert(dagCheckerParent->EvaluatingIncludeDirectories()
  400. || dagCheckerParent->EvaluatingCompileDefinitions());
  401. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  402. || propertyName == "INTERFACE_COMPILE_DEFINITIONS")
  403. {
  404. const cmTarget::LinkInterface *iface = target->GetLinkInterface(
  405. context->Config,
  406. context->HeadTarget);
  407. if(iface)
  408. {
  409. cmGeneratorExpression ge(context->Backtrace);
  410. std::string sep;
  411. std::string depString;
  412. for (std::vector<std::string>::const_iterator
  413. it = iface->Libraries.begin();
  414. it != iface->Libraries.end(); ++it)
  415. {
  416. if (context->Makefile->FindTargetToUse(it->c_str()))
  417. {
  418. depString +=
  419. sep + "$<TARGET_PROPERTY:" + *it + "," + propertyName + ">";
  420. sep = ";";
  421. }
  422. }
  423. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
  424. ge.Parse(depString);
  425. linkedTargetsContent = cge->Evaluate(context->Makefile,
  426. context->Config,
  427. context->Quiet,
  428. context->HeadTarget,
  429. target,
  430. &dagChecker);
  431. if (cge->GetHadContextSensitiveCondition())
  432. {
  433. context->HadContextSensitiveCondition = true;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. if (!prop)
  440. {
  441. if (target->IsImported())
  442. {
  443. return linkedTargetsContent;
  444. }
  445. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  446. context->Config))
  447. {
  448. context->HadContextSensitiveCondition = true;
  449. return target->GetLinkInterfaceDependentBoolProperty(
  450. propertyName,
  451. context->Config) ? "1" : "0";
  452. }
  453. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  454. context->Config))
  455. {
  456. context->HadContextSensitiveCondition = true;
  457. const char *propContent =
  458. target->GetLinkInterfaceDependentStringProperty(
  459. propertyName,
  460. context->Config);
  461. return propContent ? propContent : "";
  462. }
  463. return linkedTargetsContent;
  464. }
  465. for (size_t i = 0;
  466. i < (sizeof(targetPropertyTransitiveWhitelist) /
  467. sizeof(*targetPropertyTransitiveWhitelist));
  468. ++i)
  469. {
  470. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  471. {
  472. cmGeneratorExpression ge(context->Backtrace);
  473. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  474. std::string result = cge->Evaluate(context->Makefile,
  475. context->Config,
  476. context->Quiet,
  477. context->HeadTarget,
  478. target,
  479. &dagChecker);
  480. if (cge->GetHadContextSensitiveCondition())
  481. {
  482. context->HadContextSensitiveCondition = true;
  483. }
  484. if (!linkedTargetsContent.empty())
  485. {
  486. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  487. }
  488. return result;
  489. }
  490. }
  491. return prop;
  492. }
  493. } targetPropertyNode;
  494. //----------------------------------------------------------------------------
  495. static const struct TargetNameNode : public cmGeneratorExpressionNode
  496. {
  497. TargetNameNode() {}
  498. virtual bool GeneratesContent() const { return true; }
  499. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  500. virtual bool RequiresLiteralInput() const { return true; }
  501. std::string Evaluate(const std::vector<std::string> &parameters,
  502. cmGeneratorExpressionContext *,
  503. const GeneratorExpressionContent *,
  504. cmGeneratorExpressionDAGChecker *) const
  505. {
  506. return parameters.front();
  507. }
  508. virtual int NumExpectedParameters() const { return 1; }
  509. } targetNameNode;
  510. //----------------------------------------------------------------------------
  511. static const char* targetPolicyWhitelist[] = {
  512. "CMP0003"
  513. , "CMP0004"
  514. , "CMP0008"
  515. , "CMP0020"
  516. };
  517. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  518. {
  519. #define RETURN_POLICY(POLICY) \
  520. if (strcmp(policy, #POLICY) == 0) \
  521. { \
  522. return tgt->GetPolicyStatus ## POLICY (); \
  523. } \
  524. RETURN_POLICY(CMP0003)
  525. RETURN_POLICY(CMP0004)
  526. RETURN_POLICY(CMP0008)
  527. RETURN_POLICY(CMP0020)
  528. #undef RETURN_POLICY
  529. assert("!Unreachable code. Not a valid policy");
  530. return cmPolicies::WARN;
  531. }
  532. cmPolicies::PolicyID policyForString(const char *policy_id)
  533. {
  534. #define RETURN_POLICY_ID(POLICY_ID) \
  535. if (strcmp(policy_id, #POLICY_ID) == 0) \
  536. { \
  537. return cmPolicies:: POLICY_ID; \
  538. } \
  539. RETURN_POLICY_ID(CMP0003)
  540. RETURN_POLICY_ID(CMP0004)
  541. RETURN_POLICY_ID(CMP0008)
  542. RETURN_POLICY_ID(CMP0020)
  543. #undef RETURN_POLICY_ID
  544. assert("!Unreachable code. Not a valid policy");
  545. return cmPolicies::CMP0002;
  546. }
  547. //----------------------------------------------------------------------------
  548. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  549. {
  550. TargetPolicyNode() {}
  551. virtual int NumExpectedParameters() const { return 1; }
  552. std::string Evaluate(const std::vector<std::string> &parameters,
  553. cmGeneratorExpressionContext *context ,
  554. const GeneratorExpressionContent *content,
  555. cmGeneratorExpressionDAGChecker *) const
  556. {
  557. if (!context->HeadTarget)
  558. {
  559. reportError(context, content->GetOriginalExpression(),
  560. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  561. "be used with add_custom_command.");
  562. return std::string();
  563. }
  564. context->HadContextSensitiveCondition = true;
  565. for (size_t i = 0;
  566. i < (sizeof(targetPolicyWhitelist) /
  567. sizeof(*targetPolicyWhitelist));
  568. ++i)
  569. {
  570. const char *policy = targetPolicyWhitelist[i];
  571. if (parameters.front() == policy)
  572. {
  573. cmMakefile *mf = context->HeadTarget->GetMakefile();
  574. switch(statusForTarget(context->HeadTarget, policy))
  575. {
  576. case cmPolicies::WARN:
  577. mf->IssueMessage(cmake::AUTHOR_WARNING,
  578. mf->GetPolicies()->
  579. GetPolicyWarning(policyForString(policy)));
  580. case cmPolicies::REQUIRED_IF_USED:
  581. case cmPolicies::REQUIRED_ALWAYS:
  582. case cmPolicies::OLD:
  583. return "0";
  584. case cmPolicies::NEW:
  585. return "1";
  586. }
  587. }
  588. }
  589. reportError(context, content->GetOriginalExpression(),
  590. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  591. "policies. Currently it may be used with policies CMP0003, CMP0004, "
  592. "CMP0008 and CMP0020."
  593. );
  594. return std::string();
  595. }
  596. } targetPolicyNode;
  597. //----------------------------------------------------------------------------
  598. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  599. {
  600. InstallPrefixNode() {}
  601. virtual bool GeneratesContent() const { return true; }
  602. virtual int NumExpectedParameters() const { return 0; }
  603. std::string Evaluate(const std::vector<std::string> &,
  604. cmGeneratorExpressionContext *context,
  605. const GeneratorExpressionContent *content,
  606. cmGeneratorExpressionDAGChecker *) const
  607. {
  608. reportError(context, content->GetOriginalExpression(),
  609. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  610. "should never be evaluated.");
  611. return std::string();
  612. }
  613. } installPrefixNode;
  614. //----------------------------------------------------------------------------
  615. template<bool linker, bool soname>
  616. struct TargetFilesystemArtifactResultCreator
  617. {
  618. static std::string Create(cmTarget* target,
  619. cmGeneratorExpressionContext *context,
  620. const GeneratorExpressionContent *content);
  621. };
  622. //----------------------------------------------------------------------------
  623. template<>
  624. struct TargetFilesystemArtifactResultCreator<false, true>
  625. {
  626. static std::string Create(cmTarget* target,
  627. cmGeneratorExpressionContext *context,
  628. const GeneratorExpressionContent *content)
  629. {
  630. // The target soname file (.so.1).
  631. if(target->IsDLLPlatform())
  632. {
  633. ::reportError(context, content->GetOriginalExpression(),
  634. "TARGET_SONAME_FILE is not allowed "
  635. "for DLL target platforms.");
  636. return std::string();
  637. }
  638. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  639. {
  640. ::reportError(context, content->GetOriginalExpression(),
  641. "TARGET_SONAME_FILE is allowed only for "
  642. "SHARED libraries.");
  643. return std::string();
  644. }
  645. std::string result = target->GetDirectory(context->Config);
  646. result += "/";
  647. result += target->GetSOName(context->Config);
  648. return result;
  649. }
  650. };
  651. //----------------------------------------------------------------------------
  652. template<>
  653. struct TargetFilesystemArtifactResultCreator<true, false>
  654. {
  655. static std::string Create(cmTarget* target,
  656. cmGeneratorExpressionContext *context,
  657. const GeneratorExpressionContent *content)
  658. {
  659. // The file used to link to the target (.so, .lib, .a).
  660. if(!target->IsLinkable())
  661. {
  662. ::reportError(context, content->GetOriginalExpression(),
  663. "TARGET_LINKER_FILE is allowed only for libraries and "
  664. "executables with ENABLE_EXPORTS.");
  665. return std::string();
  666. }
  667. return target->GetFullPath(context->Config,
  668. target->HasImportLibrary());
  669. }
  670. };
  671. //----------------------------------------------------------------------------
  672. template<>
  673. struct TargetFilesystemArtifactResultCreator<false, false>
  674. {
  675. static std::string Create(cmTarget* target,
  676. cmGeneratorExpressionContext *context,
  677. const GeneratorExpressionContent *)
  678. {
  679. return target->GetFullPath(context->Config, false, true);
  680. }
  681. };
  682. //----------------------------------------------------------------------------
  683. template<bool dirQual, bool nameQual>
  684. struct TargetFilesystemArtifactResultGetter
  685. {
  686. static std::string Get(const std::string &result);
  687. };
  688. //----------------------------------------------------------------------------
  689. template<>
  690. struct TargetFilesystemArtifactResultGetter<false, true>
  691. {
  692. static std::string Get(const std::string &result)
  693. { return cmSystemTools::GetFilenameName(result); }
  694. };
  695. //----------------------------------------------------------------------------
  696. template<>
  697. struct TargetFilesystemArtifactResultGetter<true, false>
  698. {
  699. static std::string Get(const std::string &result)
  700. { return cmSystemTools::GetFilenamePath(result); }
  701. };
  702. //----------------------------------------------------------------------------
  703. template<>
  704. struct TargetFilesystemArtifactResultGetter<false, false>
  705. {
  706. static std::string Get(const std::string &result)
  707. { return result; }
  708. };
  709. //----------------------------------------------------------------------------
  710. template<bool linker, bool soname, bool dirQual, bool nameQual>
  711. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  712. {
  713. TargetFilesystemArtifact() {}
  714. virtual int NumExpectedParameters() const { return 1; }
  715. std::string Evaluate(const std::vector<std::string> &parameters,
  716. cmGeneratorExpressionContext *context,
  717. const GeneratorExpressionContent *content,
  718. cmGeneratorExpressionDAGChecker *) const
  719. {
  720. // Lookup the referenced target.
  721. std::string name = *parameters.begin();
  722. if (!cmGeneratorExpression::IsValidTargetName(name))
  723. {
  724. ::reportError(context, content->GetOriginalExpression(),
  725. "Expression syntax not recognized.");
  726. return std::string();
  727. }
  728. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  729. if(!target)
  730. {
  731. ::reportError(context, content->GetOriginalExpression(),
  732. "No target \"" + name + "\"");
  733. return std::string();
  734. }
  735. if(target->GetType() >= cmTarget::UTILITY &&
  736. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  737. {
  738. ::reportError(context, content->GetOriginalExpression(),
  739. "Target \"" + name + "\" is not an executable or library.");
  740. return std::string();
  741. }
  742. context->Targets.insert(target);
  743. std::string result =
  744. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  745. target,
  746. context,
  747. content);
  748. if (context->HadError)
  749. {
  750. return std::string();
  751. }
  752. return
  753. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  754. }
  755. };
  756. //----------------------------------------------------------------------------
  757. static const
  758. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  759. static const
  760. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  761. static const
  762. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  763. static const
  764. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  765. static const
  766. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  767. static const
  768. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  769. static const
  770. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  771. static const
  772. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  773. static const
  774. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  775. //----------------------------------------------------------------------------
  776. static const
  777. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  778. {
  779. if (identifier == "0")
  780. return &zeroNode;
  781. else if (identifier == "1")
  782. return &oneNode;
  783. else if (identifier == "AND")
  784. return &andNode;
  785. else if (identifier == "OR")
  786. return &orNode;
  787. else if (identifier == "NOT")
  788. return &notNode;
  789. else if (identifier == "CONFIGURATION")
  790. return &configurationNode;
  791. else if (identifier == "CONFIG")
  792. return &configurationTestNode;
  793. else if (identifier == "TARGET_FILE")
  794. return &targetFileNode;
  795. else if (identifier == "TARGET_LINKER_FILE")
  796. return &targetLinkerFileNode;
  797. else if (identifier == "TARGET_SONAME_FILE")
  798. return &targetSoNameFileNode;
  799. else if (identifier == "TARGET_FILE_NAME")
  800. return &targetFileNameNode;
  801. else if (identifier == "TARGET_LINKER_FILE_NAME")
  802. return &targetLinkerFileNameNode;
  803. else if (identifier == "TARGET_SONAME_FILE_NAME")
  804. return &targetSoNameFileNameNode;
  805. else if (identifier == "TARGET_FILE_DIR")
  806. return &targetFileDirNode;
  807. else if (identifier == "TARGET_LINKER_FILE_DIR")
  808. return &targetLinkerFileDirNode;
  809. else if (identifier == "TARGET_SONAME_FILE_DIR")
  810. return &targetSoNameFileDirNode;
  811. else if (identifier == "STREQUAL")
  812. return &strEqualNode;
  813. else if (identifier == "BOOL")
  814. return &boolNode;
  815. else if (identifier == "ANGLE-R")
  816. return &angle_rNode;
  817. else if (identifier == "COMMA")
  818. return &commaNode;
  819. else if (identifier == "TARGET_PROPERTY")
  820. return &targetPropertyNode;
  821. else if (identifier == "TARGET_NAME")
  822. return &targetNameNode;
  823. else if (identifier == "TARGET_POLICY")
  824. return &targetPolicyNode;
  825. else if (identifier == "BUILD_INTERFACE")
  826. return &buildInterfaceNode;
  827. else if (identifier == "INSTALL_INTERFACE")
  828. return &installInterfaceNode;
  829. else if (identifier == "TARGET_DEFINED")
  830. return &targetDefinedNode;
  831. else if (identifier == "INSTALL_PREFIX")
  832. return &installPrefixNode;
  833. return 0;
  834. }
  835. //----------------------------------------------------------------------------
  836. GeneratorExpressionContent::GeneratorExpressionContent(
  837. const char *startContent,
  838. unsigned int length)
  839. : StartContent(startContent), ContentLength(length)
  840. {
  841. }
  842. //----------------------------------------------------------------------------
  843. std::string GeneratorExpressionContent::GetOriginalExpression() const
  844. {
  845. return std::string(this->StartContent, this->ContentLength);
  846. }
  847. //----------------------------------------------------------------------------
  848. std::string GeneratorExpressionContent::Evaluate(
  849. cmGeneratorExpressionContext *context,
  850. cmGeneratorExpressionDAGChecker *dagChecker) const
  851. {
  852. std::string identifier;
  853. {
  854. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  855. = this->IdentifierChildren.begin();
  856. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  857. = this->IdentifierChildren.end();
  858. for ( ; it != end; ++it)
  859. {
  860. identifier += (*it)->Evaluate(context, dagChecker);
  861. if (context->HadError)
  862. {
  863. return std::string();
  864. }
  865. }
  866. }
  867. const cmGeneratorExpressionNode *node = GetNode(identifier);
  868. if (!node)
  869. {
  870. reportError(context, this->GetOriginalExpression(),
  871. "Expression did not evaluate to a known generator expression");
  872. return std::string();
  873. }
  874. if (!node->GeneratesContent())
  875. {
  876. if (node->AcceptsSingleArbitraryContentParameter())
  877. {
  878. if (this->ParamChildren.empty())
  879. {
  880. reportError(context, this->GetOriginalExpression(),
  881. "$<" + identifier + "> expression requires a parameter.");
  882. }
  883. }
  884. else
  885. {
  886. std::vector<std::string> parameters;
  887. this->EvaluateParameters(node, identifier, context, dagChecker,
  888. parameters);
  889. }
  890. return std::string();
  891. }
  892. if (node->AcceptsSingleArbitraryContentParameter())
  893. {
  894. std::string result;
  895. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  896. pit = this->ParamChildren.begin();
  897. const
  898. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  899. pend = this->ParamChildren.end();
  900. for ( ; pit != pend; ++pit)
  901. {
  902. if (!result.empty())
  903. {
  904. result += ",";
  905. }
  906. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  907. = pit->begin();
  908. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  909. = pit->end();
  910. for ( ; it != end; ++it)
  911. {
  912. if (node->RequiresLiteralInput())
  913. {
  914. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  915. {
  916. reportError(context, this->GetOriginalExpression(),
  917. "$<" + identifier + "> expression requires literal input.");
  918. return std::string();
  919. }
  920. }
  921. result += (*it)->Evaluate(context, dagChecker);
  922. if (context->HadError)
  923. {
  924. return std::string();
  925. }
  926. }
  927. }
  928. if (node->RequiresLiteralInput())
  929. {
  930. std::vector<std::string> parameters;
  931. parameters.push_back(result);
  932. return node->Evaluate(parameters, context, this, dagChecker);
  933. }
  934. return result;
  935. }
  936. std::vector<std::string> parameters;
  937. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  938. if (context->HadError)
  939. {
  940. return std::string();
  941. }
  942. return node->Evaluate(parameters, context, this, dagChecker);
  943. }
  944. //----------------------------------------------------------------------------
  945. std::string GeneratorExpressionContent::EvaluateParameters(
  946. const cmGeneratorExpressionNode *node,
  947. const std::string &identifier,
  948. cmGeneratorExpressionContext *context,
  949. cmGeneratorExpressionDAGChecker *dagChecker,
  950. std::vector<std::string> &parameters) const
  951. {
  952. {
  953. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  954. pit = this->ParamChildren.begin();
  955. const
  956. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  957. pend = this->ParamChildren.end();
  958. for ( ; pit != pend; ++pit)
  959. {
  960. std::string parameter;
  961. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  962. pit->begin();
  963. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  964. pit->end();
  965. for ( ; it != end; ++it)
  966. {
  967. parameter += (*it)->Evaluate(context, dagChecker);
  968. if (context->HadError)
  969. {
  970. return std::string();
  971. }
  972. }
  973. parameters.push_back(parameter);
  974. }
  975. }
  976. int numExpected = node->NumExpectedParameters();
  977. if ((numExpected != -1 && (unsigned int)numExpected != parameters.size()))
  978. {
  979. if (numExpected == 0)
  980. {
  981. reportError(context, this->GetOriginalExpression(),
  982. "$<" + identifier + "> expression requires no parameters.");
  983. }
  984. else if (numExpected == 1)
  985. {
  986. reportError(context, this->GetOriginalExpression(),
  987. "$<" + identifier + "> expression requires "
  988. "exactly one parameter.");
  989. }
  990. else
  991. {
  992. cmOStringStream e;
  993. e << "$<" + identifier + "> expression requires "
  994. << numExpected
  995. << " comma separated parameters, but got "
  996. << parameters.size() << " instead.";
  997. reportError(context, this->GetOriginalExpression(), e.str());
  998. }
  999. return std::string();
  1000. }
  1001. if (numExpected == -1 && parameters.empty())
  1002. {
  1003. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1004. + "> expression requires at least one parameter.");
  1005. }
  1006. return std::string();
  1007. }
  1008. //----------------------------------------------------------------------------
  1009. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1010. {
  1011. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1012. = c.begin();
  1013. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1014. = c.end();
  1015. for ( ; it != end; ++it)
  1016. {
  1017. delete *it;
  1018. }
  1019. }
  1020. //----------------------------------------------------------------------------
  1021. GeneratorExpressionContent::~GeneratorExpressionContent()
  1022. {
  1023. deleteAll(this->IdentifierChildren);
  1024. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1025. typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  1026. std::vector<EvaluatorVector>::const_iterator pit =
  1027. this->ParamChildren.begin();
  1028. const std::vector<EvaluatorVector>::const_iterator pend =
  1029. this->ParamChildren.end();
  1030. for ( ; pit != pend; ++pit)
  1031. {
  1032. deleteAll(*pit);
  1033. }
  1034. }