cmGeneratorExpressionEvaluator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. //----------------------------------------------------------------------------
  244. static const char* targetPropertyTransitiveWhitelist[] = {
  245. "INTERFACE_INCLUDE_DIRECTORIES"
  246. , "INTERFACE_COMPILE_DEFINITIONS"
  247. };
  248. //----------------------------------------------------------------------------
  249. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  250. {
  251. TargetPropertyNode() {}
  252. // This node handles errors on parameter count itself.
  253. virtual int NumExpectedParameters() const { return -1; }
  254. std::string Evaluate(const std::vector<std::string> &parameters,
  255. cmGeneratorExpressionContext *context,
  256. const GeneratorExpressionContent *content,
  257. cmGeneratorExpressionDAGChecker *dagCheckerParent
  258. ) const
  259. {
  260. if (parameters.size() != 1 && parameters.size() != 2)
  261. {
  262. reportError(context, content->GetOriginalExpression(),
  263. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  264. return std::string();
  265. }
  266. cmsys::RegularExpression targetNameValidator;
  267. // The ':' is supported to allow use with IMPORTED targets. At least
  268. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  269. targetNameValidator.compile("^[A-Za-z0-9_.:-]+$");
  270. cmsys::RegularExpression propertyNameValidator;
  271. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  272. cmTarget* target = context->HeadTarget;
  273. std::string propertyName = *parameters.begin();
  274. if (!target && parameters.size() == 1)
  275. {
  276. reportError(context, content->GetOriginalExpression(),
  277. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  278. "be used with add_custom_command. Specify the target to read a "
  279. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  280. "instead.");
  281. return std::string();
  282. }
  283. if (parameters.size() == 2)
  284. {
  285. if (parameters.begin()->empty() && parameters[1].empty())
  286. {
  287. reportError(context, content->GetOriginalExpression(),
  288. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  289. "target name and property name.");
  290. return std::string();
  291. }
  292. if (parameters.begin()->empty())
  293. {
  294. reportError(context, content->GetOriginalExpression(),
  295. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  296. "target name.");
  297. return std::string();
  298. }
  299. std::string targetName = parameters.front();
  300. propertyName = parameters[1];
  301. if (!targetNameValidator.find(targetName.c_str()))
  302. {
  303. if (!propertyNameValidator.find(propertyName.c_str()))
  304. {
  305. ::reportError(context, content->GetOriginalExpression(),
  306. "Target name and property name not supported.");
  307. return std::string();
  308. }
  309. ::reportError(context, content->GetOriginalExpression(),
  310. "Target name not supported.");
  311. return std::string();
  312. }
  313. target = context->Makefile->FindTargetToUse(
  314. targetName.c_str());
  315. if (!target)
  316. {
  317. cmOStringStream e;
  318. e << "Target \""
  319. << targetName
  320. << "\" not found.";
  321. reportError(context, content->GetOriginalExpression(), e.str());
  322. return std::string();
  323. }
  324. }
  325. if (target == context->HeadTarget)
  326. {
  327. // Keep track of the properties seen while processing.
  328. // The evaluation of the LINK_LIBRARIES generator expressions
  329. // will check this to ensure that properties form a DAG.
  330. context->SeenTargetProperties.insert(propertyName);
  331. }
  332. if (propertyName.empty())
  333. {
  334. reportError(context, content->GetOriginalExpression(),
  335. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  336. "name.");
  337. return std::string();
  338. }
  339. if (!propertyNameValidator.find(propertyName.c_str()))
  340. {
  341. ::reportError(context, content->GetOriginalExpression(),
  342. "Property name not supported.");
  343. return std::string();
  344. }
  345. assert(target);
  346. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  347. target->GetName(),
  348. propertyName,
  349. content,
  350. dagCheckerParent);
  351. switch (dagChecker.check())
  352. {
  353. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  354. dagChecker.reportError(context, content->GetOriginalExpression());
  355. return std::string();
  356. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  357. // No error. We just skip cyclic references.
  358. return std::string();
  359. case cmGeneratorExpressionDAGChecker::DAG:
  360. break;
  361. }
  362. const char *prop = target->GetProperty(propertyName.c_str());
  363. if (!prop)
  364. {
  365. return std::string();
  366. }
  367. for (size_t i = 0;
  368. i < (sizeof(targetPropertyTransitiveWhitelist) /
  369. sizeof(*targetPropertyTransitiveWhitelist));
  370. ++i)
  371. {
  372. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  373. {
  374. cmGeneratorExpression ge(context->Backtrace);
  375. return ge.Parse(prop)->Evaluate(context->Makefile,
  376. context->Config,
  377. context->Quiet,
  378. context->HeadTarget,
  379. target,
  380. &dagChecker);
  381. }
  382. }
  383. return prop;
  384. }
  385. } targetPropertyNode;
  386. //----------------------------------------------------------------------------
  387. static const struct TargetNameNode : public cmGeneratorExpressionNode
  388. {
  389. TargetNameNode() {}
  390. virtual bool GeneratesContent() const { return true; }
  391. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  392. virtual bool RequiresLiteralInput() const { return true; }
  393. std::string Evaluate(const std::vector<std::string> &parameters,
  394. cmGeneratorExpressionContext *,
  395. const GeneratorExpressionContent *,
  396. cmGeneratorExpressionDAGChecker *) const
  397. {
  398. return parameters.front();
  399. }
  400. virtual int NumExpectedParameters() const { return 1; }
  401. } targetNameNode;
  402. //----------------------------------------------------------------------------
  403. template<bool linker, bool soname>
  404. struct TargetFilesystemArtifactResultCreator
  405. {
  406. static std::string Create(cmTarget* target,
  407. cmGeneratorExpressionContext *context,
  408. const GeneratorExpressionContent *content);
  409. };
  410. //----------------------------------------------------------------------------
  411. template<>
  412. struct TargetFilesystemArtifactResultCreator<false, true>
  413. {
  414. static std::string Create(cmTarget* target,
  415. cmGeneratorExpressionContext *context,
  416. const GeneratorExpressionContent *content)
  417. {
  418. // The target soname file (.so.1).
  419. if(target->IsDLLPlatform())
  420. {
  421. ::reportError(context, content->GetOriginalExpression(),
  422. "TARGET_SONAME_FILE is not allowed "
  423. "for DLL target platforms.");
  424. return std::string();
  425. }
  426. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  427. {
  428. ::reportError(context, content->GetOriginalExpression(),
  429. "TARGET_SONAME_FILE is allowed only for "
  430. "SHARED libraries.");
  431. return std::string();
  432. }
  433. std::string result = target->GetDirectory(context->Config);
  434. result += "/";
  435. result += target->GetSOName(context->Config);
  436. return result;
  437. }
  438. };
  439. //----------------------------------------------------------------------------
  440. template<>
  441. struct TargetFilesystemArtifactResultCreator<true, false>
  442. {
  443. static std::string Create(cmTarget* target,
  444. cmGeneratorExpressionContext *context,
  445. const GeneratorExpressionContent *content)
  446. {
  447. // The file used to link to the target (.so, .lib, .a).
  448. if(!target->IsLinkable())
  449. {
  450. ::reportError(context, content->GetOriginalExpression(),
  451. "TARGET_LINKER_FILE is allowed only for libraries and "
  452. "executables with ENABLE_EXPORTS.");
  453. return std::string();
  454. }
  455. return target->GetFullPath(context->Config,
  456. target->HasImportLibrary());
  457. }
  458. };
  459. //----------------------------------------------------------------------------
  460. template<>
  461. struct TargetFilesystemArtifactResultCreator<false, false>
  462. {
  463. static std::string Create(cmTarget* target,
  464. cmGeneratorExpressionContext *context,
  465. const GeneratorExpressionContent *)
  466. {
  467. return target->GetFullPath(context->Config, false, true);
  468. }
  469. };
  470. //----------------------------------------------------------------------------
  471. template<bool dirQual, bool nameQual>
  472. struct TargetFilesystemArtifactResultGetter
  473. {
  474. static std::string Get(const std::string &result);
  475. };
  476. //----------------------------------------------------------------------------
  477. template<>
  478. struct TargetFilesystemArtifactResultGetter<false, true>
  479. {
  480. static std::string Get(const std::string &result)
  481. { return cmSystemTools::GetFilenameName(result); }
  482. };
  483. //----------------------------------------------------------------------------
  484. template<>
  485. struct TargetFilesystemArtifactResultGetter<true, false>
  486. {
  487. static std::string Get(const std::string &result)
  488. { return cmSystemTools::GetFilenamePath(result); }
  489. };
  490. //----------------------------------------------------------------------------
  491. template<>
  492. struct TargetFilesystemArtifactResultGetter<false, false>
  493. {
  494. static std::string Get(const std::string &result)
  495. { return result; }
  496. };
  497. //----------------------------------------------------------------------------
  498. template<bool linker, bool soname, bool dirQual, bool nameQual>
  499. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  500. {
  501. TargetFilesystemArtifact() {}
  502. virtual int NumExpectedParameters() const { return 1; }
  503. std::string Evaluate(const std::vector<std::string> &parameters,
  504. cmGeneratorExpressionContext *context,
  505. const GeneratorExpressionContent *content,
  506. cmGeneratorExpressionDAGChecker *) const
  507. {
  508. // Lookup the referenced target.
  509. std::string name = *parameters.begin();
  510. cmsys::RegularExpression targetValidator;
  511. // The ':' is supported to allow use with IMPORTED targets.
  512. targetValidator.compile("^[A-Za-z0-9_.:-]+$");
  513. if (!targetValidator.find(name.c_str()))
  514. {
  515. ::reportError(context, content->GetOriginalExpression(),
  516. "Expression syntax not recognized.");
  517. return std::string();
  518. }
  519. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  520. if(!target)
  521. {
  522. ::reportError(context, content->GetOriginalExpression(),
  523. "No target \"" + name + "\"");
  524. return std::string();
  525. }
  526. if(target->GetType() >= cmTarget::UTILITY &&
  527. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  528. {
  529. ::reportError(context, content->GetOriginalExpression(),
  530. "Target \"" + name + "\" is not an executable or library.");
  531. return std::string();
  532. }
  533. context->Targets.insert(target);
  534. std::string result =
  535. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  536. target,
  537. context,
  538. content);
  539. if (context->HadError)
  540. {
  541. return std::string();
  542. }
  543. return
  544. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  545. }
  546. };
  547. //----------------------------------------------------------------------------
  548. static const
  549. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  550. static const
  551. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  552. static const
  553. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  554. static const
  555. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  556. static const
  557. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  558. static const
  559. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  560. static const
  561. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  562. static const
  563. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  564. static const
  565. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  566. //----------------------------------------------------------------------------
  567. static const
  568. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  569. {
  570. if (identifier == "0")
  571. return &zeroNode;
  572. else if (identifier == "1")
  573. return &oneNode;
  574. else if (identifier == "AND")
  575. return &andNode;
  576. else if (identifier == "OR")
  577. return &orNode;
  578. else if (identifier == "NOT")
  579. return &notNode;
  580. else if (identifier == "CONFIGURATION")
  581. return &configurationNode;
  582. else if (identifier == "CONFIG")
  583. return &configurationTestNode;
  584. else if (identifier == "TARGET_FILE")
  585. return &targetFileNode;
  586. else if (identifier == "TARGET_LINKER_FILE")
  587. return &targetLinkerFileNode;
  588. else if (identifier == "TARGET_SONAME_FILE")
  589. return &targetSoNameFileNode;
  590. else if (identifier == "TARGET_FILE_NAME")
  591. return &targetFileNameNode;
  592. else if (identifier == "TARGET_LINKER_FILE_NAME")
  593. return &targetLinkerFileNameNode;
  594. else if (identifier == "TARGET_SONAME_FILE_NAME")
  595. return &targetSoNameFileNameNode;
  596. else if (identifier == "TARGET_FILE_DIR")
  597. return &targetFileDirNode;
  598. else if (identifier == "TARGET_LINKER_FILE_DIR")
  599. return &targetLinkerFileDirNode;
  600. else if (identifier == "TARGET_SONAME_FILE_DIR")
  601. return &targetSoNameFileDirNode;
  602. else if (identifier == "STREQUAL")
  603. return &strEqualNode;
  604. else if (identifier == "BOOL")
  605. return &boolNode;
  606. else if (identifier == "ANGLE-R")
  607. return &angle_rNode;
  608. else if (identifier == "COMMA")
  609. return &commaNode;
  610. else if (identifier == "TARGET_PROPERTY")
  611. return &targetPropertyNode;
  612. else if (identifier == "TARGET_NAME")
  613. return &targetNameNode;
  614. else if (identifier == "BUILD_INTERFACE")
  615. return &buildInterfaceNode;
  616. else if (identifier == "INSTALL_INTERFACE")
  617. return &installInterfaceNode;
  618. return 0;
  619. }
  620. //----------------------------------------------------------------------------
  621. GeneratorExpressionContent::GeneratorExpressionContent(
  622. const char *startContent,
  623. unsigned int length)
  624. : StartContent(startContent), ContentLength(length)
  625. {
  626. }
  627. //----------------------------------------------------------------------------
  628. std::string GeneratorExpressionContent::GetOriginalExpression() const
  629. {
  630. return std::string(this->StartContent, this->ContentLength);
  631. }
  632. //----------------------------------------------------------------------------
  633. std::string GeneratorExpressionContent::Evaluate(
  634. cmGeneratorExpressionContext *context,
  635. cmGeneratorExpressionDAGChecker *dagChecker) const
  636. {
  637. std::string identifier;
  638. {
  639. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  640. = this->IdentifierChildren.begin();
  641. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  642. = this->IdentifierChildren.end();
  643. for ( ; it != end; ++it)
  644. {
  645. identifier += (*it)->Evaluate(context, dagChecker);
  646. if (context->HadError)
  647. {
  648. return std::string();
  649. }
  650. }
  651. }
  652. const cmGeneratorExpressionNode *node = GetNode(identifier);
  653. if (!node)
  654. {
  655. reportError(context, this->GetOriginalExpression(),
  656. "Expression did not evaluate to a known generator expression");
  657. return std::string();
  658. }
  659. if (!node->GeneratesContent())
  660. {
  661. if (node->AcceptsSingleArbitraryContentParameter())
  662. {
  663. if (this->ParamChildren.empty())
  664. {
  665. reportError(context, this->GetOriginalExpression(),
  666. "$<" + identifier + "> expression requires a parameter.");
  667. }
  668. }
  669. else
  670. {
  671. std::vector<std::string> parameters;
  672. this->EvaluateParameters(node, identifier, context, dagChecker,
  673. parameters);
  674. }
  675. return std::string();
  676. }
  677. if (node->AcceptsSingleArbitraryContentParameter())
  678. {
  679. std::string result;
  680. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  681. pit = this->ParamChildren.begin();
  682. const
  683. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  684. pend = this->ParamChildren.end();
  685. for ( ; pit != pend; ++pit)
  686. {
  687. if (!result.empty())
  688. {
  689. result += ",";
  690. }
  691. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  692. = pit->begin();
  693. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  694. = pit->end();
  695. for ( ; it != end; ++it)
  696. {
  697. if (node->RequiresLiteralInput())
  698. {
  699. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  700. {
  701. reportError(context, this->GetOriginalExpression(),
  702. "$<" + identifier + "> expression requires literal input.");
  703. return std::string();
  704. }
  705. }
  706. result += (*it)->Evaluate(context, dagChecker);
  707. if (context->HadError)
  708. {
  709. return std::string();
  710. }
  711. }
  712. }
  713. if (node->RequiresLiteralInput())
  714. {
  715. std::vector<std::string> parameters;
  716. parameters.push_back(result);
  717. return node->Evaluate(parameters, context, this, dagChecker);
  718. }
  719. return result;
  720. }
  721. std::vector<std::string> parameters;
  722. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  723. if (context->HadError)
  724. {
  725. return std::string();
  726. }
  727. return node->Evaluate(parameters, context, this, dagChecker);
  728. }
  729. //----------------------------------------------------------------------------
  730. std::string GeneratorExpressionContent::EvaluateParameters(
  731. const cmGeneratorExpressionNode *node,
  732. const std::string &identifier,
  733. cmGeneratorExpressionContext *context,
  734. cmGeneratorExpressionDAGChecker *dagChecker,
  735. std::vector<std::string> &parameters) const
  736. {
  737. {
  738. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  739. pit = this->ParamChildren.begin();
  740. const
  741. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  742. pend = this->ParamChildren.end();
  743. for ( ; pit != pend; ++pit)
  744. {
  745. std::string parameter;
  746. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  747. pit->begin();
  748. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  749. pit->end();
  750. for ( ; it != end; ++it)
  751. {
  752. parameter += (*it)->Evaluate(context, dagChecker);
  753. if (context->HadError)
  754. {
  755. return std::string();
  756. }
  757. }
  758. parameters.push_back(parameter);
  759. }
  760. }
  761. int numExpected = node->NumExpectedParameters();
  762. if ((numExpected != -1 && (unsigned int)numExpected != parameters.size()))
  763. {
  764. if (numExpected == 0)
  765. {
  766. reportError(context, this->GetOriginalExpression(),
  767. "$<" + identifier + "> expression requires no parameters.");
  768. }
  769. else if (numExpected == 1)
  770. {
  771. reportError(context, this->GetOriginalExpression(),
  772. "$<" + identifier + "> expression requires "
  773. "exactly one parameter.");
  774. }
  775. else
  776. {
  777. cmOStringStream e;
  778. e << "$<" + identifier + "> expression requires "
  779. << numExpected
  780. << " comma separated parameters, but got "
  781. << parameters.size() << " instead.";
  782. reportError(context, this->GetOriginalExpression(), e.str());
  783. }
  784. return std::string();
  785. }
  786. if (numExpected == -1 && parameters.empty())
  787. {
  788. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  789. + "> expression requires at least one parameter.");
  790. }
  791. return std::string();
  792. }
  793. //----------------------------------------------------------------------------
  794. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  795. {
  796. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  797. = c.begin();
  798. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  799. = c.end();
  800. for ( ; it != end; ++it)
  801. {
  802. delete *it;
  803. }
  804. }
  805. //----------------------------------------------------------------------------
  806. GeneratorExpressionContent::~GeneratorExpressionContent()
  807. {
  808. deleteAll(this->IdentifierChildren);
  809. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  810. typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  811. std::vector<EvaluatorVector>::const_iterator pit =
  812. this->ParamChildren.begin();
  813. const std::vector<EvaluatorVector>::const_iterator pend =
  814. this->ParamChildren.end();
  815. for ( ; pit != pend; ++pit)
  816. {
  817. deleteAll(*pit);
  818. }
  819. }