cmGeneratorExpressionEvaluator.cxx 36 KB

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