cmGeneratorExpressionEvaluator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. //----------------------------------------------------------------------------
  17. #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x510
  18. static
  19. #endif
  20. void reportError(cmGeneratorExpressionContext *context,
  21. const std::string &expr, const std::string &result)
  22. {
  23. context->HadError = true;
  24. if (context->Quiet)
  25. {
  26. return;
  27. }
  28. cmOStringStream e;
  29. e << "Error evaluating generator expression:\n"
  30. << " " << expr << "\n"
  31. << result;
  32. context->Makefile->GetCMakeInstance()
  33. ->IssueMessage(cmake::FATAL_ERROR, e.str().c_str(),
  34. context->Backtrace);
  35. }
  36. //----------------------------------------------------------------------------
  37. struct cmGeneratorExpressionNode
  38. {
  39. virtual ~cmGeneratorExpressionNode() {}
  40. virtual bool GeneratesContent() const { return true; }
  41. virtual bool AcceptsSingleArbitraryContentParameter() const
  42. { return false; }
  43. virtual int NumExpectedParameters() const { return 1; }
  44. virtual std::string Evaluate(const std::vector<std::string> &parameters,
  45. cmGeneratorExpressionContext *context,
  46. const GeneratorExpressionContent *content,
  47. cmGeneratorExpressionDAGChecker *dagChecker
  48. ) const = 0;
  49. };
  50. //----------------------------------------------------------------------------
  51. static const struct ZeroNode : public cmGeneratorExpressionNode
  52. {
  53. ZeroNode() {}
  54. virtual bool GeneratesContent() const { return false; }
  55. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  56. std::string Evaluate(const std::vector<std::string> &,
  57. cmGeneratorExpressionContext *,
  58. const GeneratorExpressionContent *,
  59. cmGeneratorExpressionDAGChecker *) const
  60. {
  61. // Unreachable
  62. return std::string();
  63. }
  64. } zeroNode;
  65. //----------------------------------------------------------------------------
  66. static const struct OneNode : public cmGeneratorExpressionNode
  67. {
  68. OneNode() {}
  69. virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  70. std::string Evaluate(const std::vector<std::string> &,
  71. cmGeneratorExpressionContext *,
  72. const GeneratorExpressionContent *,
  73. cmGeneratorExpressionDAGChecker *) const
  74. {
  75. // Unreachable
  76. return std::string();
  77. }
  78. } oneNode;
  79. //----------------------------------------------------------------------------
  80. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  81. static const struct OP ## Node : public cmGeneratorExpressionNode \
  82. { \
  83. OP ## Node () {} \
  84. /* We let -1 carry the meaning 'at least one' */ \
  85. virtual int NumExpectedParameters() const { return -1; } \
  86. \
  87. std::string Evaluate(const std::vector<std::string> &parameters, \
  88. cmGeneratorExpressionContext *context, \
  89. const GeneratorExpressionContent *content, \
  90. cmGeneratorExpressionDAGChecker *) const \
  91. { \
  92. std::vector<std::string>::const_iterator it = parameters.begin(); \
  93. const std::vector<std::string>::const_iterator end = parameters.end(); \
  94. for ( ; it != end; ++it) \
  95. { \
  96. if (*it == #FAILURE_VALUE) \
  97. { \
  98. return #FAILURE_VALUE; \
  99. } \
  100. else if (*it != #SUCCESS_VALUE) \
  101. { \
  102. reportError(context, content->GetOriginalExpression(), \
  103. "Parameters to $<" #OP "> must resolve to either '0' or '1'."); \
  104. return std::string(); \
  105. } \
  106. } \
  107. return #SUCCESS_VALUE; \
  108. } \
  109. } OPNAME;
  110. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  111. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  112. #undef BOOLEAN_OP_NODE
  113. //----------------------------------------------------------------------------
  114. static const struct NotNode : public cmGeneratorExpressionNode
  115. {
  116. NotNode() {}
  117. std::string Evaluate(const std::vector<std::string> &parameters,
  118. cmGeneratorExpressionContext *context,
  119. const GeneratorExpressionContent *content,
  120. cmGeneratorExpressionDAGChecker *) const
  121. {
  122. if (*parameters.begin() != "0" && *parameters.begin() != "1")
  123. {
  124. reportError(context, content->GetOriginalExpression(),
  125. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  126. return std::string();
  127. }
  128. return *parameters.begin() == "0" ? "1" : "0";
  129. }
  130. } notNode;
  131. //----------------------------------------------------------------------------
  132. static const struct BoolNode : public cmGeneratorExpressionNode
  133. {
  134. BoolNode() {}
  135. virtual int NumExpectedParameters() const { return 1; }
  136. std::string Evaluate(const std::vector<std::string> &parameters,
  137. cmGeneratorExpressionContext *,
  138. const GeneratorExpressionContent *,
  139. cmGeneratorExpressionDAGChecker *) const
  140. {
  141. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  142. }
  143. } boolNode;
  144. //----------------------------------------------------------------------------
  145. static const struct StrEqualNode : public cmGeneratorExpressionNode
  146. {
  147. StrEqualNode() {}
  148. virtual int NumExpectedParameters() const { return 2; }
  149. std::string Evaluate(const std::vector<std::string> &parameters,
  150. cmGeneratorExpressionContext *,
  151. const GeneratorExpressionContent *,
  152. cmGeneratorExpressionDAGChecker *) const
  153. {
  154. return *parameters.begin() == parameters[1] ? "1" : "0";
  155. }
  156. } strEqualNode;
  157. //----------------------------------------------------------------------------
  158. static const struct Angle_RNode : public cmGeneratorExpressionNode
  159. {
  160. Angle_RNode() {}
  161. virtual int NumExpectedParameters() const { return 0; }
  162. std::string Evaluate(const std::vector<std::string> &,
  163. cmGeneratorExpressionContext *,
  164. const GeneratorExpressionContent *,
  165. cmGeneratorExpressionDAGChecker *) const
  166. {
  167. return ">";
  168. }
  169. } angle_rNode;
  170. //----------------------------------------------------------------------------
  171. static const struct CommaNode : public cmGeneratorExpressionNode
  172. {
  173. CommaNode() {}
  174. virtual int NumExpectedParameters() const { return 0; }
  175. std::string Evaluate(const std::vector<std::string> &,
  176. cmGeneratorExpressionContext *,
  177. const GeneratorExpressionContent *,
  178. cmGeneratorExpressionDAGChecker *) const
  179. {
  180. return ",";
  181. }
  182. } commaNode;
  183. //----------------------------------------------------------------------------
  184. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  185. {
  186. ConfigurationNode() {}
  187. virtual int NumExpectedParameters() const { return 0; }
  188. std::string Evaluate(const std::vector<std::string> &,
  189. cmGeneratorExpressionContext *context,
  190. const GeneratorExpressionContent *,
  191. cmGeneratorExpressionDAGChecker *) const
  192. {
  193. return context->Config ? context->Config : "";
  194. }
  195. } configurationNode;
  196. //----------------------------------------------------------------------------
  197. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  198. {
  199. ConfigurationTestNode() {}
  200. virtual int NumExpectedParameters() const { return 1; }
  201. std::string Evaluate(const std::vector<std::string> &parameters,
  202. cmGeneratorExpressionContext *context,
  203. const GeneratorExpressionContent *content,
  204. cmGeneratorExpressionDAGChecker *) const
  205. {
  206. cmsys::RegularExpression configValidator;
  207. configValidator.compile("^[A-Za-z0-9_]*$");
  208. if (!configValidator.find(parameters.begin()->c_str()))
  209. {
  210. reportError(context, content->GetOriginalExpression(),
  211. "Expression syntax not recognized.");
  212. return std::string();
  213. }
  214. if (!context->Config)
  215. {
  216. return parameters.front().empty() ? "1" : "0";
  217. }
  218. return cmsysString_strcasecmp(parameters.begin()->c_str(),
  219. context->Config) == 0 ? "1" : "0";
  220. }
  221. } configurationTestNode;
  222. //----------------------------------------------------------------------------
  223. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  224. {
  225. TargetPropertyNode() {}
  226. // This node handles errors on parameter count itself.
  227. virtual int NumExpectedParameters() const { return -1; }
  228. std::string Evaluate(const std::vector<std::string> &parameters,
  229. cmGeneratorExpressionContext *context,
  230. const GeneratorExpressionContent *content,
  231. cmGeneratorExpressionDAGChecker *dagCheckerParent
  232. ) const
  233. {
  234. if (parameters.size() != 1 && parameters.size() != 2)
  235. {
  236. reportError(context, content->GetOriginalExpression(),
  237. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  238. return std::string();
  239. }
  240. cmsys::RegularExpression targetNameValidator;
  241. // The ':' is supported to allow use with IMPORTED targets. At least
  242. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  243. targetNameValidator.compile("^[A-Za-z0-9_.:-]+$");
  244. cmsys::RegularExpression propertyNameValidator;
  245. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  246. cmGeneratorTarget* target = context->Target;
  247. std::string propertyName = *parameters.begin();
  248. if (parameters.size() == 2)
  249. {
  250. if (parameters.begin()->empty() && parameters[1].empty())
  251. {
  252. reportError(context, content->GetOriginalExpression(),
  253. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  254. "target name and property name.");
  255. return std::string();
  256. }
  257. if (parameters.begin()->empty())
  258. {
  259. reportError(context, content->GetOriginalExpression(),
  260. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  261. "target name.");
  262. return std::string();
  263. }
  264. std::string targetName = parameters.front();
  265. propertyName = parameters[1];
  266. if (!targetNameValidator.find(targetName.c_str()))
  267. {
  268. if (!propertyNameValidator.find(propertyName.c_str()))
  269. {
  270. ::reportError(context, content->GetOriginalExpression(),
  271. "Target name and property name not supported.");
  272. return std::string();
  273. }
  274. ::reportError(context, content->GetOriginalExpression(),
  275. "Target name not supported.");
  276. return std::string();
  277. }
  278. target = context->Makefile->FindGeneratorTargetToUse(
  279. targetName.c_str());
  280. if (!target)
  281. {
  282. cmOStringStream e;
  283. e << "Target \""
  284. << targetName
  285. << "\" not found.";
  286. reportError(context, content->GetOriginalExpression(), e.str());
  287. return std::string();
  288. }
  289. }
  290. if (propertyName.empty())
  291. {
  292. reportError(context, content->GetOriginalExpression(),
  293. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  294. "name.");
  295. return std::string();
  296. }
  297. if (!propertyNameValidator.find(propertyName.c_str()))
  298. {
  299. ::reportError(context, content->GetOriginalExpression(),
  300. "Property name not supported.");
  301. return std::string();
  302. }
  303. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  304. target->GetName(),
  305. propertyName,
  306. content,
  307. dagCheckerParent);
  308. if (!dagChecker.check())
  309. {
  310. dagChecker.reportError(context, content->GetOriginalExpression());
  311. return std::string();
  312. }
  313. const char *prop = target->GetProperty(propertyName.c_str());
  314. return prop ? prop : "";
  315. }
  316. } targetPropertyNode;
  317. //----------------------------------------------------------------------------
  318. template<bool linker, bool soname>
  319. struct TargetFilesystemArtifactResultCreator
  320. {
  321. static std::string Create(cmTarget* target,
  322. cmGeneratorExpressionContext *context,
  323. const GeneratorExpressionContent *content);
  324. };
  325. //----------------------------------------------------------------------------
  326. template<>
  327. struct TargetFilesystemArtifactResultCreator<false, true>
  328. {
  329. static std::string Create(cmTarget* target,
  330. cmGeneratorExpressionContext *context,
  331. const GeneratorExpressionContent *content)
  332. {
  333. // The target soname file (.so.1).
  334. if(target->IsDLLPlatform())
  335. {
  336. ::reportError(context, content->GetOriginalExpression(),
  337. "TARGET_SONAME_FILE is not allowed "
  338. "for DLL target platforms.");
  339. return std::string();
  340. }
  341. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  342. {
  343. ::reportError(context, content->GetOriginalExpression(),
  344. "TARGET_SONAME_FILE is allowed only for "
  345. "SHARED libraries.");
  346. return std::string();
  347. }
  348. std::string result = target->GetDirectory(context->Config);
  349. result += "/";
  350. result += target->GetSOName(context->Config);
  351. return result;
  352. }
  353. };
  354. //----------------------------------------------------------------------------
  355. template<>
  356. struct TargetFilesystemArtifactResultCreator<true, false>
  357. {
  358. static std::string Create(cmTarget* target,
  359. cmGeneratorExpressionContext *context,
  360. const GeneratorExpressionContent *content)
  361. {
  362. // The file used to link to the target (.so, .lib, .a).
  363. if(!target->IsLinkable())
  364. {
  365. ::reportError(context, content->GetOriginalExpression(),
  366. "TARGET_LINKER_FILE is allowed only for libraries and "
  367. "executables with ENABLE_EXPORTS.");
  368. return std::string();
  369. }
  370. return target->GetFullPath(context->Config,
  371. target->HasImportLibrary());
  372. }
  373. };
  374. //----------------------------------------------------------------------------
  375. template<>
  376. struct TargetFilesystemArtifactResultCreator<false, false>
  377. {
  378. static std::string Create(cmTarget* target,
  379. cmGeneratorExpressionContext *context,
  380. const GeneratorExpressionContent *)
  381. {
  382. return target->GetFullPath(context->Config, false, true);
  383. }
  384. };
  385. //----------------------------------------------------------------------------
  386. template<bool dirQual, bool nameQual>
  387. struct TargetFilesystemArtifactResultGetter
  388. {
  389. static std::string Get(const std::string &result);
  390. };
  391. //----------------------------------------------------------------------------
  392. template<>
  393. struct TargetFilesystemArtifactResultGetter<false, true>
  394. {
  395. static std::string Get(const std::string &result)
  396. { return cmSystemTools::GetFilenameName(result); }
  397. };
  398. //----------------------------------------------------------------------------
  399. template<>
  400. struct TargetFilesystemArtifactResultGetter<true, false>
  401. {
  402. static std::string Get(const std::string &result)
  403. { return cmSystemTools::GetFilenamePath(result); }
  404. };
  405. //----------------------------------------------------------------------------
  406. template<>
  407. struct TargetFilesystemArtifactResultGetter<false, false>
  408. {
  409. static std::string Get(const std::string &result)
  410. { return result; }
  411. };
  412. //----------------------------------------------------------------------------
  413. template<bool linker, bool soname, bool dirQual, bool nameQual>
  414. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  415. {
  416. TargetFilesystemArtifact() {}
  417. virtual int NumExpectedParameters() const { return 1; }
  418. std::string Evaluate(const std::vector<std::string> &parameters,
  419. cmGeneratorExpressionContext *context,
  420. const GeneratorExpressionContent *content,
  421. cmGeneratorExpressionDAGChecker *) const
  422. {
  423. // Lookup the referenced target.
  424. std::string name = *parameters.begin();
  425. cmsys::RegularExpression targetValidator;
  426. // The ':' is supported to allow use with IMPORTED targets.
  427. targetValidator.compile("^[A-Za-z0-9_.:-]+$");
  428. if (!targetValidator.find(name.c_str()))
  429. {
  430. ::reportError(context, content->GetOriginalExpression(),
  431. "Expression syntax not recognized.");
  432. return std::string();
  433. }
  434. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  435. if(!target)
  436. {
  437. ::reportError(context, content->GetOriginalExpression(),
  438. "No target \"" + name + "\"");
  439. return std::string();
  440. }
  441. if(target->GetType() >= cmTarget::UTILITY &&
  442. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  443. {
  444. ::reportError(context, content->GetOriginalExpression(),
  445. "Target \"" + name + "\" is not an executable or library.");
  446. return std::string();
  447. }
  448. context->Targets.insert(target);
  449. std::string result =
  450. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  451. target,
  452. context,
  453. content);
  454. if (context->HadError)
  455. {
  456. return std::string();
  457. }
  458. return
  459. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  460. }
  461. };
  462. //----------------------------------------------------------------------------
  463. static const
  464. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  465. static const
  466. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  467. static const
  468. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  469. static const
  470. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  471. static const
  472. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  473. static const
  474. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  475. static const
  476. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  477. static const
  478. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  479. static const
  480. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  481. //----------------------------------------------------------------------------
  482. static const
  483. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  484. {
  485. if (identifier == "0")
  486. return &zeroNode;
  487. if (identifier == "1")
  488. return &oneNode;
  489. if (identifier == "AND")
  490. return &andNode;
  491. if (identifier == "OR")
  492. return &orNode;
  493. if (identifier == "NOT")
  494. return &notNode;
  495. else if (identifier == "CONFIGURATION")
  496. return &configurationNode;
  497. else if (identifier == "CONFIG")
  498. return &configurationTestNode;
  499. else if (identifier == "TARGET_FILE")
  500. return &targetFileNode;
  501. else if (identifier == "TARGET_LINKER_FILE")
  502. return &targetLinkerFileNode;
  503. else if (identifier == "TARGET_SONAME_FILE")
  504. return &targetSoNameFileNode;
  505. else if (identifier == "TARGET_FILE_NAME")
  506. return &targetFileNameNode;
  507. else if (identifier == "TARGET_LINKER_FILE_NAME")
  508. return &targetLinkerFileNameNode;
  509. else if (identifier == "TARGET_SONAME_FILE_NAME")
  510. return &targetSoNameFileNameNode;
  511. else if (identifier == "TARGET_FILE_DIR")
  512. return &targetFileDirNode;
  513. else if (identifier == "TARGET_LINKER_FILE_DIR")
  514. return &targetLinkerFileDirNode;
  515. else if (identifier == "TARGET_SONAME_FILE_DIR")
  516. return &targetSoNameFileDirNode;
  517. else if (identifier == "STREQUAL")
  518. return &strEqualNode;
  519. else if (identifier == "BOOL")
  520. return &boolNode;
  521. else if (identifier == "ANGLE-R")
  522. return &angle_rNode;
  523. else if (identifier == "COMMA")
  524. return &commaNode;
  525. else if (identifier == "TARGET_PROPERTY")
  526. return &targetPropertyNode;
  527. return 0;
  528. }
  529. //----------------------------------------------------------------------------
  530. GeneratorExpressionContent::GeneratorExpressionContent(
  531. const char *startContent,
  532. unsigned int length)
  533. : StartContent(startContent), ContentLength(length)
  534. {
  535. }
  536. //----------------------------------------------------------------------------
  537. std::string GeneratorExpressionContent::GetOriginalExpression() const
  538. {
  539. return std::string(this->StartContent, this->ContentLength);
  540. }
  541. //----------------------------------------------------------------------------
  542. std::string GeneratorExpressionContent::Evaluate(
  543. cmGeneratorExpressionContext *context,
  544. cmGeneratorExpressionDAGChecker *dagChecker) const
  545. {
  546. std::string identifier;
  547. {
  548. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  549. = this->IdentifierChildren.begin();
  550. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  551. = this->IdentifierChildren.end();
  552. for ( ; it != end; ++it)
  553. {
  554. identifier += (*it)->Evaluate(context, dagChecker);
  555. if (context->HadError)
  556. {
  557. return std::string();
  558. }
  559. }
  560. }
  561. const cmGeneratorExpressionNode *node = GetNode(identifier);
  562. if (!node)
  563. {
  564. reportError(context, this->GetOriginalExpression(),
  565. "Expression did not evaluate to a known generator expression");
  566. return std::string();
  567. }
  568. if (!node->GeneratesContent())
  569. {
  570. if (node->AcceptsSingleArbitraryContentParameter())
  571. {
  572. if (this->ParamChildren.empty())
  573. {
  574. reportError(context, this->GetOriginalExpression(),
  575. "$<" + identifier + "> expression requires a parameter.");
  576. }
  577. }
  578. else
  579. {
  580. std::vector<std::string> parameters;
  581. this->EvaluateParameters(node, identifier, context, dagChecker,
  582. parameters);
  583. }
  584. return std::string();
  585. }
  586. if (node->AcceptsSingleArbitraryContentParameter())
  587. {
  588. std::string result;
  589. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  590. pit = this->ParamChildren.begin();
  591. const
  592. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  593. pend = this->ParamChildren.end();
  594. for ( ; pit != pend; ++pit)
  595. {
  596. if (!result.empty())
  597. {
  598. result += ",";
  599. }
  600. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  601. = pit->begin();
  602. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  603. = pit->end();
  604. for ( ; it != end; ++it)
  605. {
  606. result += (*it)->Evaluate(context, dagChecker);
  607. if (context->HadError)
  608. {
  609. return std::string();
  610. }
  611. }
  612. }
  613. return result;
  614. }
  615. std::vector<std::string> parameters;
  616. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  617. if (context->HadError)
  618. {
  619. return std::string();
  620. }
  621. return node->Evaluate(parameters, context, this, dagChecker);
  622. }
  623. //----------------------------------------------------------------------------
  624. std::string GeneratorExpressionContent::EvaluateParameters(
  625. const cmGeneratorExpressionNode *node,
  626. const std::string &identifier,
  627. cmGeneratorExpressionContext *context,
  628. cmGeneratorExpressionDAGChecker *dagChecker,
  629. std::vector<std::string> &parameters) const
  630. {
  631. {
  632. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  633. pit = this->ParamChildren.begin();
  634. const
  635. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  636. pend = this->ParamChildren.end();
  637. for ( ; pit != pend; ++pit)
  638. {
  639. std::string parameter;
  640. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  641. pit->begin();
  642. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  643. pit->end();
  644. for ( ; it != end; ++it)
  645. {
  646. parameter += (*it)->Evaluate(context, dagChecker);
  647. if (context->HadError)
  648. {
  649. return std::string();
  650. }
  651. }
  652. parameters.push_back(parameter);
  653. }
  654. }
  655. int numExpected = node->NumExpectedParameters();
  656. if ((numExpected != -1 && (unsigned int)numExpected != parameters.size()))
  657. {
  658. if (numExpected == 0)
  659. {
  660. reportError(context, this->GetOriginalExpression(),
  661. "$<" + identifier + "> expression requires no parameters.");
  662. }
  663. else if (numExpected == 1)
  664. {
  665. reportError(context, this->GetOriginalExpression(),
  666. "$<" + identifier + "> expression requires "
  667. "exactly one parameter.");
  668. }
  669. else
  670. {
  671. cmOStringStream e;
  672. e << "$<" + identifier + "> expression requires "
  673. << numExpected
  674. << " comma separated parameters, but got "
  675. << parameters.size() << " instead.";
  676. reportError(context, this->GetOriginalExpression(), e.str());
  677. }
  678. return std::string();
  679. }
  680. if (numExpected == -1 && parameters.empty())
  681. {
  682. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  683. + "> expression requires at least one parameter.");
  684. }
  685. return std::string();
  686. }
  687. //----------------------------------------------------------------------------
  688. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  689. {
  690. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  691. = c.begin();
  692. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  693. = c.end();
  694. for ( ; it != end; ++it)
  695. {
  696. delete *it;
  697. }
  698. }
  699. //----------------------------------------------------------------------------
  700. GeneratorExpressionContent::~GeneratorExpressionContent()
  701. {
  702. deleteAll(this->IdentifierChildren);
  703. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  704. typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  705. std::vector<EvaluatorVector>::const_iterator pit =
  706. this->ParamChildren.begin();
  707. const std::vector<EvaluatorVector>::const_iterator pend =
  708. this->ParamChildren.end();
  709. for ( ; pit != pend; ++pit)
  710. {
  711. deleteAll(*pit);
  712. }
  713. }