cmGeneratorExpressionEvaluator.cxx 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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. enum {
  41. DynamicParameters = 0,
  42. OneOrMoreParameters = -1,
  43. ZeroOrMoreParameters = -2
  44. };
  45. virtual ~cmGeneratorExpressionNode() {}
  46. virtual bool GeneratesContent() const { return true; }
  47. virtual bool RequiresLiteralInput() const { return false; }
  48. virtual bool AcceptsArbitraryContentParameter() const
  49. { return false; }
  50. virtual int NumExpectedParameters() const { return 1; }
  51. virtual std::string Evaluate(const std::vector<std::string> &parameters,
  52. cmGeneratorExpressionContext *context,
  53. const GeneratorExpressionContent *content,
  54. cmGeneratorExpressionDAGChecker *dagChecker
  55. ) const = 0;
  56. };
  57. //----------------------------------------------------------------------------
  58. static const struct ZeroNode : public cmGeneratorExpressionNode
  59. {
  60. ZeroNode() {}
  61. virtual bool GeneratesContent() const { return false; }
  62. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  63. std::string Evaluate(const std::vector<std::string> &,
  64. cmGeneratorExpressionContext *,
  65. const GeneratorExpressionContent *,
  66. cmGeneratorExpressionDAGChecker *) const
  67. {
  68. // Unreachable
  69. return std::string();
  70. }
  71. } zeroNode;
  72. //----------------------------------------------------------------------------
  73. static const struct OneNode : public cmGeneratorExpressionNode
  74. {
  75. OneNode() {}
  76. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  77. std::string Evaluate(const std::vector<std::string> &,
  78. cmGeneratorExpressionContext *,
  79. const GeneratorExpressionContent *,
  80. cmGeneratorExpressionDAGChecker *) const
  81. {
  82. // Unreachable
  83. return std::string();
  84. }
  85. } oneNode;
  86. //----------------------------------------------------------------------------
  87. static const struct OneNode buildInterfaceNode;
  88. //----------------------------------------------------------------------------
  89. static const struct ZeroNode installInterfaceNode;
  90. //----------------------------------------------------------------------------
  91. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  92. static const struct OP ## Node : public cmGeneratorExpressionNode \
  93. { \
  94. OP ## Node () {} \
  95. virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
  96. \
  97. std::string Evaluate(const std::vector<std::string> &parameters, \
  98. cmGeneratorExpressionContext *context, \
  99. const GeneratorExpressionContent *content, \
  100. cmGeneratorExpressionDAGChecker *) const \
  101. { \
  102. std::vector<std::string>::const_iterator it = parameters.begin(); \
  103. const std::vector<std::string>::const_iterator end = parameters.end(); \
  104. for ( ; it != end; ++it) \
  105. { \
  106. if (*it == #FAILURE_VALUE) \
  107. { \
  108. return #FAILURE_VALUE; \
  109. } \
  110. else if (*it != #SUCCESS_VALUE) \
  111. { \
  112. reportError(context, content->GetOriginalExpression(), \
  113. "Parameters to $<" #OP "> must resolve to either '0' or '1'."); \
  114. return std::string(); \
  115. } \
  116. } \
  117. return #SUCCESS_VALUE; \
  118. } \
  119. } OPNAME;
  120. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  121. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  122. #undef BOOLEAN_OP_NODE
  123. //----------------------------------------------------------------------------
  124. static const struct NotNode : public cmGeneratorExpressionNode
  125. {
  126. NotNode() {}
  127. std::string Evaluate(const std::vector<std::string> &parameters,
  128. cmGeneratorExpressionContext *context,
  129. const GeneratorExpressionContent *content,
  130. cmGeneratorExpressionDAGChecker *) const
  131. {
  132. if (*parameters.begin() != "0" && *parameters.begin() != "1")
  133. {
  134. reportError(context, content->GetOriginalExpression(),
  135. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  136. return std::string();
  137. }
  138. return *parameters.begin() == "0" ? "1" : "0";
  139. }
  140. } notNode;
  141. //----------------------------------------------------------------------------
  142. static const struct BoolNode : public cmGeneratorExpressionNode
  143. {
  144. BoolNode() {}
  145. virtual int NumExpectedParameters() const { return 1; }
  146. std::string Evaluate(const std::vector<std::string> &parameters,
  147. cmGeneratorExpressionContext *,
  148. const GeneratorExpressionContent *,
  149. cmGeneratorExpressionDAGChecker *) const
  150. {
  151. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  152. }
  153. } boolNode;
  154. //----------------------------------------------------------------------------
  155. static const struct StrEqualNode : public cmGeneratorExpressionNode
  156. {
  157. StrEqualNode() {}
  158. virtual int NumExpectedParameters() const { return 2; }
  159. std::string Evaluate(const std::vector<std::string> &parameters,
  160. cmGeneratorExpressionContext *,
  161. const GeneratorExpressionContent *,
  162. cmGeneratorExpressionDAGChecker *) const
  163. {
  164. return *parameters.begin() == parameters[1] ? "1" : "0";
  165. }
  166. } strEqualNode;
  167. //----------------------------------------------------------------------------
  168. static const struct Angle_RNode : public cmGeneratorExpressionNode
  169. {
  170. Angle_RNode() {}
  171. virtual int NumExpectedParameters() const { return 0; }
  172. std::string Evaluate(const std::vector<std::string> &,
  173. cmGeneratorExpressionContext *,
  174. const GeneratorExpressionContent *,
  175. cmGeneratorExpressionDAGChecker *) const
  176. {
  177. return ">";
  178. }
  179. } angle_rNode;
  180. //----------------------------------------------------------------------------
  181. static const struct CommaNode : public cmGeneratorExpressionNode
  182. {
  183. CommaNode() {}
  184. virtual int NumExpectedParameters() const { return 0; }
  185. std::string Evaluate(const std::vector<std::string> &,
  186. cmGeneratorExpressionContext *,
  187. const GeneratorExpressionContent *,
  188. cmGeneratorExpressionDAGChecker *) const
  189. {
  190. return ",";
  191. }
  192. } commaNode;
  193. //----------------------------------------------------------------------------
  194. static const struct SemicolonNode : public cmGeneratorExpressionNode
  195. {
  196. SemicolonNode() {}
  197. virtual int NumExpectedParameters() const { return 0; }
  198. std::string Evaluate(const std::vector<std::string> &,
  199. cmGeneratorExpressionContext *,
  200. const GeneratorExpressionContent *,
  201. cmGeneratorExpressionDAGChecker *) const
  202. {
  203. return ";";
  204. }
  205. } semicolonNode;
  206. //----------------------------------------------------------------------------
  207. struct CompilerIdNode : public cmGeneratorExpressionNode
  208. {
  209. CompilerIdNode() {}
  210. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  211. std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
  212. cmGeneratorExpressionContext *context,
  213. const GeneratorExpressionContent *content,
  214. cmGeneratorExpressionDAGChecker *,
  215. const std::string &lang) const
  216. {
  217. const char *compilerId = context->Makefile ?
  218. context->Makefile->GetSafeDefinition((
  219. "CMAKE_" + lang + "_COMPILER_ID").c_str()) : "";
  220. if (parameters.size() == 0)
  221. {
  222. return compilerId ? compilerId : "";
  223. }
  224. cmsys::RegularExpression compilerIdValidator;
  225. compilerIdValidator.compile("^[A-Za-z0-9_]*$");
  226. if (!compilerIdValidator.find(parameters.begin()->c_str()))
  227. {
  228. reportError(context, content->GetOriginalExpression(),
  229. "Expression syntax not recognized.");
  230. return std::string();
  231. }
  232. if (!compilerId)
  233. {
  234. return parameters.front().empty() ? "1" : "0";
  235. }
  236. if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0)
  237. {
  238. return "1";
  239. }
  240. return "0";
  241. }
  242. };
  243. //----------------------------------------------------------------------------
  244. static const struct CCompilerIdNode : public CompilerIdNode
  245. {
  246. CCompilerIdNode() {}
  247. std::string Evaluate(const std::vector<std::string> &parameters,
  248. cmGeneratorExpressionContext *context,
  249. const GeneratorExpressionContent *content,
  250. cmGeneratorExpressionDAGChecker *dagChecker) const
  251. {
  252. if (parameters.size() != 0 && parameters.size() != 1)
  253. {
  254. reportError(context, content->GetOriginalExpression(),
  255. "$<C_COMPILER_ID> expression requires one or two parameters");
  256. return std::string();
  257. }
  258. if (!context->HeadTarget)
  259. {
  260. reportError(context, content->GetOriginalExpression(),
  261. "$<C_COMPILER_ID> may only be used with targets. It may not "
  262. "be used with add_custom_command.");
  263. }
  264. return this->EvaluateWithLanguage(parameters, context, content,
  265. dagChecker, "C");
  266. }
  267. } cCompilerIdNode;
  268. //----------------------------------------------------------------------------
  269. static const struct CXXCompilerIdNode : public CompilerIdNode
  270. {
  271. CXXCompilerIdNode() {}
  272. std::string Evaluate(const std::vector<std::string> &parameters,
  273. cmGeneratorExpressionContext *context,
  274. const GeneratorExpressionContent *content,
  275. cmGeneratorExpressionDAGChecker *dagChecker) const
  276. {
  277. if (parameters.size() != 0 && parameters.size() != 1)
  278. {
  279. reportError(context, content->GetOriginalExpression(),
  280. "$<CXX_COMPILER_ID> expression requires one or two parameters");
  281. return std::string();
  282. }
  283. if (!context->HeadTarget)
  284. {
  285. reportError(context, content->GetOriginalExpression(),
  286. "$<CXX_COMPILER_ID> may only be used with targets. It may not "
  287. "be used with add_custom_command.");
  288. }
  289. return this->EvaluateWithLanguage(parameters, context, content,
  290. dagChecker, "CXX");
  291. }
  292. } cxxCompilerIdNode;
  293. //----------------------------------------------------------------------------
  294. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  295. {
  296. VersionGreaterNode() {}
  297. virtual int NumExpectedParameters() const { return 2; }
  298. std::string Evaluate(const std::vector<std::string> &parameters,
  299. cmGeneratorExpressionContext *,
  300. const GeneratorExpressionContent *,
  301. cmGeneratorExpressionDAGChecker *) const
  302. {
  303. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  304. parameters.front().c_str(),
  305. parameters[1].c_str()) ? "1" : "0";
  306. }
  307. } versionGreaterNode;
  308. //----------------------------------------------------------------------------
  309. static const struct VersionLessNode : public cmGeneratorExpressionNode
  310. {
  311. VersionLessNode() {}
  312. virtual int NumExpectedParameters() const { return 2; }
  313. std::string Evaluate(const std::vector<std::string> &parameters,
  314. cmGeneratorExpressionContext *,
  315. const GeneratorExpressionContent *,
  316. cmGeneratorExpressionDAGChecker *) const
  317. {
  318. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  319. parameters.front().c_str(),
  320. parameters[1].c_str()) ? "1" : "0";
  321. }
  322. } versionLessNode;
  323. //----------------------------------------------------------------------------
  324. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  325. {
  326. VersionEqualNode() {}
  327. virtual int NumExpectedParameters() const { return 2; }
  328. std::string Evaluate(const std::vector<std::string> &parameters,
  329. cmGeneratorExpressionContext *,
  330. const GeneratorExpressionContent *,
  331. cmGeneratorExpressionDAGChecker *) const
  332. {
  333. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  334. parameters.front().c_str(),
  335. parameters[1].c_str()) ? "1" : "0";
  336. }
  337. } versionEqualNode;
  338. //----------------------------------------------------------------------------
  339. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  340. {
  341. ConfigurationNode() {}
  342. virtual int NumExpectedParameters() const { return 0; }
  343. std::string Evaluate(const std::vector<std::string> &,
  344. cmGeneratorExpressionContext *context,
  345. const GeneratorExpressionContent *,
  346. cmGeneratorExpressionDAGChecker *) const
  347. {
  348. context->HadContextSensitiveCondition = true;
  349. return context->Config ? context->Config : "";
  350. }
  351. } configurationNode;
  352. //----------------------------------------------------------------------------
  353. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  354. {
  355. ConfigurationTestNode() {}
  356. virtual int NumExpectedParameters() const { return 1; }
  357. std::string Evaluate(const std::vector<std::string> &parameters,
  358. cmGeneratorExpressionContext *context,
  359. const GeneratorExpressionContent *content,
  360. cmGeneratorExpressionDAGChecker *) const
  361. {
  362. cmsys::RegularExpression configValidator;
  363. configValidator.compile("^[A-Za-z0-9_]*$");
  364. if (!configValidator.find(parameters.begin()->c_str()))
  365. {
  366. reportError(context, content->GetOriginalExpression(),
  367. "Expression syntax not recognized.");
  368. return std::string();
  369. }
  370. context->HadContextSensitiveCondition = true;
  371. if (!context->Config)
  372. {
  373. return parameters.front().empty() ? "1" : "0";
  374. }
  375. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  376. context->Config) == 0)
  377. {
  378. return "1";
  379. }
  380. if (context->CurrentTarget
  381. && context->CurrentTarget->IsImported())
  382. {
  383. const char* loc = 0;
  384. const char* imp = 0;
  385. std::string suffix;
  386. return context->CurrentTarget->GetMappedConfig(context->Config,
  387. &loc,
  388. &imp,
  389. suffix) ? "1" : "0";
  390. }
  391. return "0";
  392. }
  393. } configurationTestNode;
  394. //----------------------------------------------------------------------------
  395. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  396. {
  397. LinkLanguageNode() {}
  398. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  399. std::string Evaluate(const std::vector<std::string> &parameters,
  400. cmGeneratorExpressionContext *context,
  401. const GeneratorExpressionContent *content,
  402. cmGeneratorExpressionDAGChecker *) const
  403. {
  404. if (parameters.size() != 0 && parameters.size() != 1)
  405. {
  406. reportError(context, content->GetOriginalExpression(),
  407. "$<LINK_LANGUAGE> expression requires one or two parameters");
  408. return std::string();
  409. }
  410. cmTarget* target = context->HeadTarget;
  411. if (!target)
  412. {
  413. reportError(context, content->GetOriginalExpression(),
  414. "$<LINK_LANGUAGE> may only be used with targets. It may not "
  415. "be used with add_custom_command.");
  416. }
  417. const char *lang = target->GetLinkerLanguage(context->Config);
  418. if (parameters.size() == 0)
  419. {
  420. return lang ? lang : "";
  421. }
  422. else
  423. {
  424. cmsys::RegularExpression langValidator;
  425. langValidator.compile("^[A-Za-z0-9_]*$");
  426. if (!langValidator.find(parameters.begin()->c_str()))
  427. {
  428. reportError(context, content->GetOriginalExpression(),
  429. "Expression syntax not recognized.");
  430. return std::string();
  431. }
  432. if (!lang)
  433. {
  434. return parameters.front().empty() ? "1" : "0";
  435. }
  436. if (strcmp(parameters.begin()->c_str(), lang) == 0)
  437. {
  438. return "1";
  439. }
  440. return "0";
  441. }
  442. }
  443. } linkLanguageNode;
  444. static const struct JoinNode : public cmGeneratorExpressionNode
  445. {
  446. JoinNode() {}
  447. virtual int NumExpectedParameters() const { return 2; }
  448. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  449. std::string Evaluate(const std::vector<std::string> &parameters,
  450. cmGeneratorExpressionContext *,
  451. const GeneratorExpressionContent *,
  452. cmGeneratorExpressionDAGChecker *) const
  453. {
  454. std::string result;
  455. std::vector<std::string> list;
  456. cmSystemTools::ExpandListArgument(parameters.front(), list);
  457. std::string sep;
  458. for(std::vector<std::string>::const_iterator li = list.begin();
  459. li != list.end(); ++li)
  460. {
  461. result += sep + *li;
  462. sep = parameters[1];
  463. }
  464. return result;
  465. }
  466. } joinNode;
  467. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) \
  468. , #PROPERTY
  469. //----------------------------------------------------------------------------
  470. static const char* targetPropertyTransitiveWhitelist[] = {
  471. 0
  472. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  473. };
  474. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  475. cmTarget *target,
  476. cmTarget *headTarget,
  477. cmGeneratorExpressionContext *context,
  478. cmGeneratorExpressionDAGChecker *dagChecker,
  479. const std::string &interfacePropertyName)
  480. {
  481. cmGeneratorExpression ge(context->Backtrace);
  482. std::string sep;
  483. std::string depString;
  484. for (std::vector<std::string>::const_iterator
  485. it = libraries.begin();
  486. it != libraries.end(); ++it)
  487. {
  488. if (*it == target->GetName())
  489. {
  490. // Broken code can have a target in its own link interface.
  491. // Don't follow such link interface entries so as not to create a
  492. // self-referencing loop.
  493. continue;
  494. }
  495. if (context->Makefile->FindTargetToUse(it->c_str()))
  496. {
  497. depString +=
  498. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  499. sep = ";";
  500. }
  501. }
  502. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  503. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  504. context->Config,
  505. context->Quiet,
  506. headTarget,
  507. target,
  508. dagChecker);
  509. if (cge->GetHadContextSensitiveCondition())
  510. {
  511. context->HadContextSensitiveCondition = true;
  512. }
  513. return linkedTargetsContent;
  514. }
  515. //----------------------------------------------------------------------------
  516. struct TransitiveWhitelistCompare
  517. {
  518. explicit TransitiveWhitelistCompare(const std::string &needle)
  519. : Needle(needle) {}
  520. bool operator() (const char *item)
  521. { return strcmp(item, this->Needle.c_str()) == 0; }
  522. private:
  523. std::string Needle;
  524. };
  525. //----------------------------------------------------------------------------
  526. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  527. {
  528. TargetPropertyNode() {}
  529. // This node handles errors on parameter count itself.
  530. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  531. std::string Evaluate(const std::vector<std::string> &parameters,
  532. cmGeneratorExpressionContext *context,
  533. const GeneratorExpressionContent *content,
  534. cmGeneratorExpressionDAGChecker *dagCheckerParent
  535. ) const
  536. {
  537. if (parameters.size() != 1 && parameters.size() != 2)
  538. {
  539. reportError(context, content->GetOriginalExpression(),
  540. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  541. return std::string();
  542. }
  543. cmsys::RegularExpression propertyNameValidator;
  544. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  545. cmTarget* target = context->HeadTarget;
  546. std::string propertyName = *parameters.begin();
  547. if (!target && parameters.size() == 1)
  548. {
  549. reportError(context, content->GetOriginalExpression(),
  550. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  551. "be used with add_custom_command. Specify the target to read a "
  552. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  553. "instead.");
  554. return std::string();
  555. }
  556. if (parameters.size() == 2)
  557. {
  558. if (parameters.begin()->empty() && parameters[1].empty())
  559. {
  560. reportError(context, content->GetOriginalExpression(),
  561. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  562. "target name and property name.");
  563. return std::string();
  564. }
  565. if (parameters.begin()->empty())
  566. {
  567. reportError(context, content->GetOriginalExpression(),
  568. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  569. "target name.");
  570. return std::string();
  571. }
  572. std::string targetName = parameters.front();
  573. propertyName = parameters[1];
  574. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  575. {
  576. if (!propertyNameValidator.find(propertyName.c_str()))
  577. {
  578. ::reportError(context, content->GetOriginalExpression(),
  579. "Target name and property name not supported.");
  580. return std::string();
  581. }
  582. ::reportError(context, content->GetOriginalExpression(),
  583. "Target name not supported.");
  584. return std::string();
  585. }
  586. target = context->Makefile->FindTargetToUse(
  587. targetName.c_str());
  588. if (!target)
  589. {
  590. cmOStringStream e;
  591. e << "Target \""
  592. << targetName
  593. << "\" not found.";
  594. reportError(context, content->GetOriginalExpression(), e.str());
  595. return std::string();
  596. }
  597. context->AllTargets.insert(target);
  598. }
  599. if (target == context->HeadTarget)
  600. {
  601. // Keep track of the properties seen while processing.
  602. // The evaluation of the LINK_LIBRARIES generator expressions
  603. // will check this to ensure that properties have one consistent
  604. // value for all evaluations.
  605. context->SeenTargetProperties.insert(propertyName);
  606. }
  607. if (propertyName.empty())
  608. {
  609. reportError(context, content->GetOriginalExpression(),
  610. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  611. "name.");
  612. return std::string();
  613. }
  614. if (!propertyNameValidator.find(propertyName.c_str()))
  615. {
  616. ::reportError(context, content->GetOriginalExpression(),
  617. "Property name not supported.");
  618. return std::string();
  619. }
  620. assert(target);
  621. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  622. target->GetName(),
  623. propertyName,
  624. content,
  625. dagCheckerParent);
  626. switch (dagChecker.check())
  627. {
  628. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  629. dagChecker.reportError(context, content->GetOriginalExpression());
  630. return std::string();
  631. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  632. // No error. We just skip cyclic references.
  633. return std::string();
  634. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  635. for (size_t i = 1;
  636. i < (sizeof(targetPropertyTransitiveWhitelist) /
  637. sizeof(*targetPropertyTransitiveWhitelist));
  638. ++i)
  639. {
  640. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  641. {
  642. // No error. We're not going to find anything new here.
  643. return std::string();
  644. }
  645. }
  646. case cmGeneratorExpressionDAGChecker::DAG:
  647. break;
  648. }
  649. const char *prop = target->GetProperty(propertyName.c_str());
  650. if (dagCheckerParent)
  651. {
  652. if (dagCheckerParent->EvaluatingLinkLibraries())
  653. {
  654. if(!prop)
  655. {
  656. return std::string();
  657. }
  658. }
  659. else
  660. {
  661. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  662. dagCheckerParent->METHOD () ||
  663. assert(
  664. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  665. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  666. false);
  667. }
  668. }
  669. std::string linkedTargetsContent;
  670. std::string interfacePropertyName;
  671. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  672. || propertyName == "INCLUDE_DIRECTORIES")
  673. {
  674. interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
  675. }
  676. else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
  677. || propertyName == "COMPILE_DEFINITIONS"
  678. || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
  679. {
  680. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  681. }
  682. else if (propertyName == "INTERFACE_COMPILE_OPTIONS"
  683. || propertyName == "COMPILE_OPTIONS")
  684. {
  685. interfacePropertyName = "INTERFACE_COMPILE_OPTIONS";
  686. }
  687. cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;
  688. const char **transBegin = targetPropertyTransitiveWhitelist + 1;
  689. const char **transEnd = targetPropertyTransitiveWhitelist
  690. + (sizeof(targetPropertyTransitiveWhitelist) /
  691. sizeof(*targetPropertyTransitiveWhitelist));
  692. if (std::find_if(transBegin, transEnd,
  693. TransitiveWhitelistCompare(propertyName)) != transEnd)
  694. {
  695. const cmTarget::LinkInterface *iface = target->GetLinkInterface(
  696. context->Config,
  697. headTarget);
  698. if(iface)
  699. {
  700. linkedTargetsContent =
  701. getLinkedTargetsContent(iface->Libraries, target,
  702. headTarget,
  703. context, &dagChecker,
  704. interfacePropertyName);
  705. }
  706. }
  707. else if (std::find_if(transBegin, transEnd,
  708. TransitiveWhitelistCompare(interfacePropertyName)) != transEnd)
  709. {
  710. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  711. context->Config,
  712. headTarget);
  713. if(impl)
  714. {
  715. linkedTargetsContent =
  716. getLinkedTargetsContent(impl->Libraries, target,
  717. headTarget,
  718. context, &dagChecker,
  719. interfacePropertyName);
  720. }
  721. }
  722. linkedTargetsContent =
  723. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  724. if (!prop)
  725. {
  726. if (target->IsImported())
  727. {
  728. return linkedTargetsContent;
  729. }
  730. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  731. context->Config))
  732. {
  733. context->HadContextSensitiveCondition = true;
  734. return target->GetLinkInterfaceDependentBoolProperty(
  735. propertyName,
  736. context->Config) ? "1" : "0";
  737. }
  738. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  739. context->Config))
  740. {
  741. context->HadContextSensitiveCondition = true;
  742. const char *propContent =
  743. target->GetLinkInterfaceDependentStringProperty(
  744. propertyName,
  745. context->Config);
  746. return propContent ? propContent : "";
  747. }
  748. return linkedTargetsContent;
  749. }
  750. for (size_t i = 1;
  751. i < (sizeof(targetPropertyTransitiveWhitelist) /
  752. sizeof(*targetPropertyTransitiveWhitelist));
  753. ++i)
  754. {
  755. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  756. {
  757. cmGeneratorExpression ge(context->Backtrace);
  758. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  759. std::string result = cge->Evaluate(context->Makefile,
  760. context->Config,
  761. context->Quiet,
  762. headTarget,
  763. target,
  764. &dagChecker);
  765. if (cge->GetHadContextSensitiveCondition())
  766. {
  767. context->HadContextSensitiveCondition = true;
  768. }
  769. if (!linkedTargetsContent.empty())
  770. {
  771. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  772. }
  773. return result;
  774. }
  775. }
  776. return prop;
  777. }
  778. } targetPropertyNode;
  779. //----------------------------------------------------------------------------
  780. static const struct TargetNameNode : public cmGeneratorExpressionNode
  781. {
  782. TargetNameNode() {}
  783. virtual bool GeneratesContent() const { return true; }
  784. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  785. virtual bool RequiresLiteralInput() const { return true; }
  786. std::string Evaluate(const std::vector<std::string> &parameters,
  787. cmGeneratorExpressionContext *,
  788. const GeneratorExpressionContent *,
  789. cmGeneratorExpressionDAGChecker *) const
  790. {
  791. return parameters.front();
  792. }
  793. virtual int NumExpectedParameters() const { return 1; }
  794. } targetNameNode;
  795. //----------------------------------------------------------------------------
  796. static const char* targetPolicyWhitelist[] = {
  797. "CMP0003"
  798. , "CMP0004"
  799. , "CMP0008"
  800. , "CMP0020"
  801. };
  802. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  803. {
  804. #define RETURN_POLICY(POLICY) \
  805. if (strcmp(policy, #POLICY) == 0) \
  806. { \
  807. return tgt->GetPolicyStatus ## POLICY (); \
  808. } \
  809. RETURN_POLICY(CMP0003)
  810. RETURN_POLICY(CMP0004)
  811. RETURN_POLICY(CMP0008)
  812. RETURN_POLICY(CMP0020)
  813. #undef RETURN_POLICY
  814. assert("!Unreachable code. Not a valid policy");
  815. return cmPolicies::WARN;
  816. }
  817. cmPolicies::PolicyID policyForString(const char *policy_id)
  818. {
  819. #define RETURN_POLICY_ID(POLICY_ID) \
  820. if (strcmp(policy_id, #POLICY_ID) == 0) \
  821. { \
  822. return cmPolicies:: POLICY_ID; \
  823. } \
  824. RETURN_POLICY_ID(CMP0003)
  825. RETURN_POLICY_ID(CMP0004)
  826. RETURN_POLICY_ID(CMP0008)
  827. RETURN_POLICY_ID(CMP0020)
  828. #undef RETURN_POLICY_ID
  829. assert("!Unreachable code. Not a valid policy");
  830. return cmPolicies::CMP0002;
  831. }
  832. //----------------------------------------------------------------------------
  833. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  834. {
  835. TargetPolicyNode() {}
  836. virtual int NumExpectedParameters() const { return 1; }
  837. std::string Evaluate(const std::vector<std::string> &parameters,
  838. cmGeneratorExpressionContext *context ,
  839. const GeneratorExpressionContent *content,
  840. cmGeneratorExpressionDAGChecker *) const
  841. {
  842. if (!context->HeadTarget)
  843. {
  844. reportError(context, content->GetOriginalExpression(),
  845. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  846. "be used with add_custom_command.");
  847. return std::string();
  848. }
  849. context->HadContextSensitiveCondition = true;
  850. for (size_t i = 0;
  851. i < (sizeof(targetPolicyWhitelist) /
  852. sizeof(*targetPolicyWhitelist));
  853. ++i)
  854. {
  855. const char *policy = targetPolicyWhitelist[i];
  856. if (parameters.front() == policy)
  857. {
  858. cmMakefile *mf = context->HeadTarget->GetMakefile();
  859. switch(statusForTarget(context->HeadTarget, policy))
  860. {
  861. case cmPolicies::WARN:
  862. mf->IssueMessage(cmake::AUTHOR_WARNING,
  863. mf->GetPolicies()->
  864. GetPolicyWarning(policyForString(policy)));
  865. case cmPolicies::REQUIRED_IF_USED:
  866. case cmPolicies::REQUIRED_ALWAYS:
  867. case cmPolicies::OLD:
  868. return "0";
  869. case cmPolicies::NEW:
  870. return "1";
  871. }
  872. }
  873. }
  874. reportError(context, content->GetOriginalExpression(),
  875. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  876. "policies. Currently it may be used with policies CMP0003, CMP0004, "
  877. "CMP0008 and CMP0020."
  878. );
  879. return std::string();
  880. }
  881. } targetPolicyNode;
  882. //----------------------------------------------------------------------------
  883. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  884. {
  885. InstallPrefixNode() {}
  886. virtual bool GeneratesContent() const { return true; }
  887. virtual int NumExpectedParameters() const { return 0; }
  888. std::string Evaluate(const std::vector<std::string> &,
  889. cmGeneratorExpressionContext *context,
  890. const GeneratorExpressionContent *content,
  891. cmGeneratorExpressionDAGChecker *) const
  892. {
  893. reportError(context, content->GetOriginalExpression(),
  894. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  895. "should never be evaluated.");
  896. return std::string();
  897. }
  898. } installPrefixNode;
  899. //----------------------------------------------------------------------------
  900. template<bool linker, bool soname>
  901. struct TargetFilesystemArtifactResultCreator
  902. {
  903. static std::string Create(cmTarget* target,
  904. cmGeneratorExpressionContext *context,
  905. const GeneratorExpressionContent *content);
  906. };
  907. //----------------------------------------------------------------------------
  908. template<>
  909. struct TargetFilesystemArtifactResultCreator<false, true>
  910. {
  911. static std::string Create(cmTarget* target,
  912. cmGeneratorExpressionContext *context,
  913. const GeneratorExpressionContent *content)
  914. {
  915. // The target soname file (.so.1).
  916. if(target->IsDLLPlatform())
  917. {
  918. ::reportError(context, content->GetOriginalExpression(),
  919. "TARGET_SONAME_FILE is not allowed "
  920. "for DLL target platforms.");
  921. return std::string();
  922. }
  923. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  924. {
  925. ::reportError(context, content->GetOriginalExpression(),
  926. "TARGET_SONAME_FILE is allowed only for "
  927. "SHARED libraries.");
  928. return std::string();
  929. }
  930. std::string result = target->GetDirectory(context->Config);
  931. result += "/";
  932. result += target->GetSOName(context->Config);
  933. return result;
  934. }
  935. };
  936. //----------------------------------------------------------------------------
  937. template<>
  938. struct TargetFilesystemArtifactResultCreator<true, false>
  939. {
  940. static std::string Create(cmTarget* target,
  941. cmGeneratorExpressionContext *context,
  942. const GeneratorExpressionContent *content)
  943. {
  944. // The file used to link to the target (.so, .lib, .a).
  945. if(!target->IsLinkable())
  946. {
  947. ::reportError(context, content->GetOriginalExpression(),
  948. "TARGET_LINKER_FILE is allowed only for libraries and "
  949. "executables with ENABLE_EXPORTS.");
  950. return std::string();
  951. }
  952. return target->GetFullPath(context->Config,
  953. target->HasImportLibrary());
  954. }
  955. };
  956. //----------------------------------------------------------------------------
  957. template<>
  958. struct TargetFilesystemArtifactResultCreator<false, false>
  959. {
  960. static std::string Create(cmTarget* target,
  961. cmGeneratorExpressionContext *context,
  962. const GeneratorExpressionContent *)
  963. {
  964. return target->GetFullPath(context->Config, false, true);
  965. }
  966. };
  967. //----------------------------------------------------------------------------
  968. template<bool dirQual, bool nameQual>
  969. struct TargetFilesystemArtifactResultGetter
  970. {
  971. static std::string Get(const std::string &result);
  972. };
  973. //----------------------------------------------------------------------------
  974. template<>
  975. struct TargetFilesystemArtifactResultGetter<false, true>
  976. {
  977. static std::string Get(const std::string &result)
  978. { return cmSystemTools::GetFilenameName(result); }
  979. };
  980. //----------------------------------------------------------------------------
  981. template<>
  982. struct TargetFilesystemArtifactResultGetter<true, false>
  983. {
  984. static std::string Get(const std::string &result)
  985. { return cmSystemTools::GetFilenamePath(result); }
  986. };
  987. //----------------------------------------------------------------------------
  988. template<>
  989. struct TargetFilesystemArtifactResultGetter<false, false>
  990. {
  991. static std::string Get(const std::string &result)
  992. { return result; }
  993. };
  994. //----------------------------------------------------------------------------
  995. template<bool linker, bool soname, bool dirQual, bool nameQual>
  996. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  997. {
  998. TargetFilesystemArtifact() {}
  999. virtual int NumExpectedParameters() const { return 1; }
  1000. std::string Evaluate(const std::vector<std::string> &parameters,
  1001. cmGeneratorExpressionContext *context,
  1002. const GeneratorExpressionContent *content,
  1003. cmGeneratorExpressionDAGChecker *) const
  1004. {
  1005. // Lookup the referenced target.
  1006. std::string name = *parameters.begin();
  1007. if (!cmGeneratorExpression::IsValidTargetName(name))
  1008. {
  1009. ::reportError(context, content->GetOriginalExpression(),
  1010. "Expression syntax not recognized.");
  1011. return std::string();
  1012. }
  1013. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1014. if(!target)
  1015. {
  1016. ::reportError(context, content->GetOriginalExpression(),
  1017. "No target \"" + name + "\"");
  1018. return std::string();
  1019. }
  1020. if(target->GetType() >= cmTarget::UTILITY &&
  1021. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1022. {
  1023. ::reportError(context, content->GetOriginalExpression(),
  1024. "Target \"" + name + "\" is not an executable or library.");
  1025. return std::string();
  1026. }
  1027. context->DependTargets.insert(target);
  1028. context->AllTargets.insert(target);
  1029. std::string result =
  1030. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1031. target,
  1032. context,
  1033. content);
  1034. if (context->HadError)
  1035. {
  1036. return std::string();
  1037. }
  1038. return
  1039. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1040. }
  1041. };
  1042. //----------------------------------------------------------------------------
  1043. static const
  1044. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1045. static const
  1046. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1047. static const
  1048. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1049. static const
  1050. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1051. static const
  1052. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1053. static const
  1054. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1055. static const
  1056. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1057. static const
  1058. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1059. static const
  1060. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1061. //----------------------------------------------------------------------------
  1062. static const
  1063. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1064. {
  1065. if (identifier == "0")
  1066. return &zeroNode;
  1067. else if (identifier == "1")
  1068. return &oneNode;
  1069. else if (identifier == "AND")
  1070. return &andNode;
  1071. else if (identifier == "OR")
  1072. return &orNode;
  1073. else if (identifier == "NOT")
  1074. return &notNode;
  1075. else if (identifier == "C_COMPILER_ID")
  1076. return &cCompilerIdNode;
  1077. else if (identifier == "CXX_COMPILER_ID")
  1078. return &cxxCompilerIdNode;
  1079. else if (identifier == "VERSION_GREATER")
  1080. return &versionGreaterNode;
  1081. else if (identifier == "VERSION_LESS")
  1082. return &versionLessNode;
  1083. else if (identifier == "VERSION_EQUAL")
  1084. return &versionEqualNode;
  1085. else if (identifier == "CONFIGURATION")
  1086. return &configurationNode;
  1087. else if (identifier == "CONFIG")
  1088. return &configurationTestNode;
  1089. else if (identifier == "LINK_LANGUAGE")
  1090. return &linkLanguageNode;
  1091. else if (identifier == "TARGET_FILE")
  1092. return &targetFileNode;
  1093. else if (identifier == "TARGET_LINKER_FILE")
  1094. return &targetLinkerFileNode;
  1095. else if (identifier == "TARGET_SONAME_FILE")
  1096. return &targetSoNameFileNode;
  1097. else if (identifier == "TARGET_FILE_NAME")
  1098. return &targetFileNameNode;
  1099. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1100. return &targetLinkerFileNameNode;
  1101. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1102. return &targetSoNameFileNameNode;
  1103. else if (identifier == "TARGET_FILE_DIR")
  1104. return &targetFileDirNode;
  1105. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1106. return &targetLinkerFileDirNode;
  1107. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1108. return &targetSoNameFileDirNode;
  1109. else if (identifier == "STREQUAL")
  1110. return &strEqualNode;
  1111. else if (identifier == "BOOL")
  1112. return &boolNode;
  1113. else if (identifier == "ANGLE-R")
  1114. return &angle_rNode;
  1115. else if (identifier == "COMMA")
  1116. return &commaNode;
  1117. else if (identifier == "SEMICOLON")
  1118. return &semicolonNode;
  1119. else if (identifier == "TARGET_PROPERTY")
  1120. return &targetPropertyNode;
  1121. else if (identifier == "TARGET_NAME")
  1122. return &targetNameNode;
  1123. else if (identifier == "TARGET_POLICY")
  1124. return &targetPolicyNode;
  1125. else if (identifier == "BUILD_INTERFACE")
  1126. return &buildInterfaceNode;
  1127. else if (identifier == "INSTALL_INTERFACE")
  1128. return &installInterfaceNode;
  1129. else if (identifier == "INSTALL_PREFIX")
  1130. return &installPrefixNode;
  1131. else if (identifier == "JOIN")
  1132. return &joinNode;
  1133. return 0;
  1134. }
  1135. //----------------------------------------------------------------------------
  1136. GeneratorExpressionContent::GeneratorExpressionContent(
  1137. const char *startContent,
  1138. unsigned int length)
  1139. : StartContent(startContent), ContentLength(length)
  1140. {
  1141. }
  1142. //----------------------------------------------------------------------------
  1143. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1144. {
  1145. return std::string(this->StartContent, this->ContentLength);
  1146. }
  1147. //----------------------------------------------------------------------------
  1148. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1149. const cmGeneratorExpressionNode *node,
  1150. const std::string &identifier,
  1151. cmGeneratorExpressionContext *context,
  1152. cmGeneratorExpressionDAGChecker *dagChecker,
  1153. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1154. pit) const
  1155. {
  1156. std::string result;
  1157. const
  1158. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1159. pend = this->ParamChildren.end();
  1160. for ( ; pit != pend; ++pit)
  1161. {
  1162. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1163. = pit->begin();
  1164. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1165. = pit->end();
  1166. for ( ; it != end; ++it)
  1167. {
  1168. if (node->RequiresLiteralInput())
  1169. {
  1170. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1171. {
  1172. reportError(context, this->GetOriginalExpression(),
  1173. "$<" + identifier + "> expression requires literal input.");
  1174. return std::string();
  1175. }
  1176. }
  1177. result += (*it)->Evaluate(context, dagChecker);
  1178. if (context->HadError)
  1179. {
  1180. return std::string();
  1181. }
  1182. }
  1183. if ((pit + 1) != pend)
  1184. {
  1185. result += ",";
  1186. }
  1187. }
  1188. if (node->RequiresLiteralInput())
  1189. {
  1190. std::vector<std::string> parameters;
  1191. parameters.push_back(result);
  1192. return node->Evaluate(parameters, context, this, dagChecker);
  1193. }
  1194. return result;
  1195. }
  1196. //----------------------------------------------------------------------------
  1197. std::string GeneratorExpressionContent::Evaluate(
  1198. cmGeneratorExpressionContext *context,
  1199. cmGeneratorExpressionDAGChecker *dagChecker) const
  1200. {
  1201. std::string identifier;
  1202. {
  1203. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1204. = this->IdentifierChildren.begin();
  1205. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1206. = this->IdentifierChildren.end();
  1207. for ( ; it != end; ++it)
  1208. {
  1209. identifier += (*it)->Evaluate(context, dagChecker);
  1210. if (context->HadError)
  1211. {
  1212. return std::string();
  1213. }
  1214. }
  1215. }
  1216. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1217. if (!node)
  1218. {
  1219. reportError(context, this->GetOriginalExpression(),
  1220. "Expression did not evaluate to a known generator expression");
  1221. return std::string();
  1222. }
  1223. if (!node->GeneratesContent())
  1224. {
  1225. if (node->NumExpectedParameters() == 1
  1226. && node->AcceptsArbitraryContentParameter())
  1227. {
  1228. if (this->ParamChildren.empty())
  1229. {
  1230. reportError(context, this->GetOriginalExpression(),
  1231. "$<" + identifier + "> expression requires a parameter.");
  1232. }
  1233. }
  1234. else
  1235. {
  1236. std::vector<std::string> parameters;
  1237. this->EvaluateParameters(node, identifier, context, dagChecker,
  1238. parameters);
  1239. }
  1240. return std::string();
  1241. }
  1242. if (node->NumExpectedParameters() == 1
  1243. && node->AcceptsArbitraryContentParameter())
  1244. {
  1245. return this->ProcessArbitraryContent(node, identifier, context,
  1246. dagChecker,
  1247. this->ParamChildren.begin());
  1248. }
  1249. std::vector<std::string> parameters;
  1250. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1251. if (context->HadError)
  1252. {
  1253. return std::string();
  1254. }
  1255. return node->Evaluate(parameters, context, this, dagChecker);
  1256. }
  1257. //----------------------------------------------------------------------------
  1258. std::string GeneratorExpressionContent::EvaluateParameters(
  1259. const cmGeneratorExpressionNode *node,
  1260. const std::string &identifier,
  1261. cmGeneratorExpressionContext *context,
  1262. cmGeneratorExpressionDAGChecker *dagChecker,
  1263. std::vector<std::string> &parameters) const
  1264. {
  1265. const int numExpected = node->NumExpectedParameters();
  1266. {
  1267. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1268. pit = this->ParamChildren.begin();
  1269. const
  1270. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1271. pend = this->ParamChildren.end();
  1272. const bool acceptsArbitraryContent
  1273. = node->AcceptsArbitraryContentParameter();
  1274. for ( ; pit != pend; ++pit)
  1275. {
  1276. std::string parameter;
  1277. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1278. pit->begin();
  1279. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1280. pit->end();
  1281. for ( ; it != end; ++it)
  1282. {
  1283. parameter += (*it)->Evaluate(context, dagChecker);
  1284. if (context->HadError)
  1285. {
  1286. return std::string();
  1287. }
  1288. }
  1289. parameters.push_back(parameter);
  1290. if (acceptsArbitraryContent
  1291. && parameters.size() == (unsigned int)numExpected - 1)
  1292. {
  1293. assert(pit != pend);
  1294. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1295. context,
  1296. dagChecker,
  1297. pit + 1);
  1298. parameters.push_back(lastParam);
  1299. return std::string();
  1300. }
  1301. }
  1302. }
  1303. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1304. && (unsigned int)numExpected != parameters.size()))
  1305. {
  1306. if (numExpected == 0)
  1307. {
  1308. reportError(context, this->GetOriginalExpression(),
  1309. "$<" + identifier + "> expression requires no parameters.");
  1310. }
  1311. else if (numExpected == 1)
  1312. {
  1313. reportError(context, this->GetOriginalExpression(),
  1314. "$<" + identifier + "> expression requires "
  1315. "exactly one parameter.");
  1316. }
  1317. else
  1318. {
  1319. cmOStringStream e;
  1320. e << "$<" + identifier + "> expression requires "
  1321. << numExpected
  1322. << " comma separated parameters, but got "
  1323. << parameters.size() << " instead.";
  1324. reportError(context, this->GetOriginalExpression(), e.str());
  1325. }
  1326. return std::string();
  1327. }
  1328. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1329. && parameters.empty())
  1330. {
  1331. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1332. + "> expression requires at least one parameter.");
  1333. }
  1334. return std::string();
  1335. }
  1336. //----------------------------------------------------------------------------
  1337. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1338. {
  1339. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1340. = c.begin();
  1341. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1342. = c.end();
  1343. for ( ; it != end; ++it)
  1344. {
  1345. delete *it;
  1346. }
  1347. }
  1348. //----------------------------------------------------------------------------
  1349. GeneratorExpressionContent::~GeneratorExpressionContent()
  1350. {
  1351. deleteAll(this->IdentifierChildren);
  1352. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1353. std::vector<EvaluatorVector>::const_iterator pit =
  1354. this->ParamChildren.begin();
  1355. const std::vector<EvaluatorVector>::const_iterator pend =
  1356. this->ParamChildren.end();
  1357. for ( ; pit != pend; ++pit)
  1358. {
  1359. deleteAll(*pit);
  1360. }
  1361. }