cmGeneratorExpressionEvaluator.cxx 40 KB

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