cmGeneratorExpressionEvaluator.cxx 27 KB

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