cmGeneratorExpressionEvaluator.cxx 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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. struct CompilerVersionNode : public cmGeneratorExpressionNode
  295. {
  296. CompilerVersionNode() {}
  297. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  298. std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
  299. cmGeneratorExpressionContext *context,
  300. const GeneratorExpressionContent *content,
  301. cmGeneratorExpressionDAGChecker *,
  302. const std::string &lang) const
  303. {
  304. const char *compilerVersion = context->Makefile ?
  305. context->Makefile->GetSafeDefinition((
  306. "CMAKE_" + lang + "_COMPILER_VERSION").c_str()) : "";
  307. if (parameters.size() == 0)
  308. {
  309. return compilerVersion ? compilerVersion : "";
  310. }
  311. cmsys::RegularExpression compilerIdValidator;
  312. compilerIdValidator.compile("^[0-9\\.]*$");
  313. if (!compilerIdValidator.find(parameters.begin()->c_str()))
  314. {
  315. reportError(context, content->GetOriginalExpression(),
  316. "Expression syntax not recognized.");
  317. return std::string();
  318. }
  319. if (!compilerVersion)
  320. {
  321. return parameters.front().empty() ? "1" : "0";
  322. }
  323. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  324. parameters.begin()->c_str(),
  325. compilerVersion) ? "1" : "0";
  326. }
  327. };
  328. //----------------------------------------------------------------------------
  329. static const struct CCompilerVersionNode : public CompilerVersionNode
  330. {
  331. CCompilerVersionNode() {}
  332. std::string Evaluate(const std::vector<std::string> &parameters,
  333. cmGeneratorExpressionContext *context,
  334. const GeneratorExpressionContent *content,
  335. cmGeneratorExpressionDAGChecker *dagChecker) const
  336. {
  337. if (parameters.size() != 0 && parameters.size() != 1)
  338. {
  339. reportError(context, content->GetOriginalExpression(),
  340. "$<C_COMPILER_VERSION> expression requires one or two parameters");
  341. return std::string();
  342. }
  343. if (!context->HeadTarget)
  344. {
  345. reportError(context, content->GetOriginalExpression(),
  346. "$<C_COMPILER_VERSION> may only be used with targets. It may not "
  347. "be used with add_custom_command.");
  348. }
  349. return this->EvaluateWithLanguage(parameters, context, content,
  350. dagChecker, "C");
  351. }
  352. } cCompilerVersionNode;
  353. //----------------------------------------------------------------------------
  354. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  355. {
  356. CxxCompilerVersionNode() {}
  357. std::string Evaluate(const std::vector<std::string> &parameters,
  358. cmGeneratorExpressionContext *context,
  359. const GeneratorExpressionContent *content,
  360. cmGeneratorExpressionDAGChecker *dagChecker) const
  361. {
  362. if (parameters.size() != 0 && parameters.size() != 1)
  363. {
  364. reportError(context, content->GetOriginalExpression(),
  365. "$<CXX_COMPILER_VERSION> expression requires one or two "
  366. "parameters");
  367. return std::string();
  368. }
  369. if (!context->HeadTarget)
  370. {
  371. reportError(context, content->GetOriginalExpression(),
  372. "$<CXX_COMPILER_VERSION> may only be used with targets. It may "
  373. "not be used with add_custom_command.");
  374. }
  375. return this->EvaluateWithLanguage(parameters, context, content,
  376. dagChecker, "CXX");
  377. }
  378. } cxxCompilerVersionNode;
  379. //----------------------------------------------------------------------------
  380. struct PlatformIdNode : public cmGeneratorExpressionNode
  381. {
  382. PlatformIdNode() {}
  383. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  384. std::string Evaluate(const std::vector<std::string> &parameters,
  385. cmGeneratorExpressionContext *context,
  386. const GeneratorExpressionContent *,
  387. cmGeneratorExpressionDAGChecker *) const
  388. {
  389. const char *platformId = context->Makefile ?
  390. context->Makefile->GetSafeDefinition(
  391. "CMAKE_SYSTEM_NAME") : "";
  392. if (parameters.size() == 0)
  393. {
  394. return platformId ? platformId : "";
  395. }
  396. if (!platformId)
  397. {
  398. return parameters.front().empty() ? "1" : "0";
  399. }
  400. if (cmsysString_strcasecmp(parameters.begin()->c_str(), platformId) == 0)
  401. {
  402. return "1";
  403. }
  404. return "0";
  405. }
  406. } platformIdNode;
  407. //----------------------------------------------------------------------------
  408. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  409. {
  410. VersionGreaterNode() {}
  411. virtual int NumExpectedParameters() const { return 2; }
  412. std::string Evaluate(const std::vector<std::string> &parameters,
  413. cmGeneratorExpressionContext *,
  414. const GeneratorExpressionContent *,
  415. cmGeneratorExpressionDAGChecker *) const
  416. {
  417. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  418. parameters.front().c_str(),
  419. parameters[1].c_str()) ? "1" : "0";
  420. }
  421. } versionGreaterNode;
  422. //----------------------------------------------------------------------------
  423. static const struct VersionLessNode : public cmGeneratorExpressionNode
  424. {
  425. VersionLessNode() {}
  426. virtual int NumExpectedParameters() const { return 2; }
  427. std::string Evaluate(const std::vector<std::string> &parameters,
  428. cmGeneratorExpressionContext *,
  429. const GeneratorExpressionContent *,
  430. cmGeneratorExpressionDAGChecker *) const
  431. {
  432. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  433. parameters.front().c_str(),
  434. parameters[1].c_str()) ? "1" : "0";
  435. }
  436. } versionLessNode;
  437. //----------------------------------------------------------------------------
  438. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  439. {
  440. VersionEqualNode() {}
  441. virtual int NumExpectedParameters() const { return 2; }
  442. std::string Evaluate(const std::vector<std::string> &parameters,
  443. cmGeneratorExpressionContext *,
  444. const GeneratorExpressionContent *,
  445. cmGeneratorExpressionDAGChecker *) const
  446. {
  447. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  448. parameters.front().c_str(),
  449. parameters[1].c_str()) ? "1" : "0";
  450. }
  451. } versionEqualNode;
  452. //----------------------------------------------------------------------------
  453. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  454. {
  455. LinkOnlyNode() {}
  456. std::string Evaluate(const std::vector<std::string> &parameters,
  457. cmGeneratorExpressionContext *,
  458. const GeneratorExpressionContent *,
  459. cmGeneratorExpressionDAGChecker *dagChecker) const
  460. {
  461. if(!dagChecker->GetTransitivePropertiesOnly())
  462. {
  463. return parameters.front();
  464. }
  465. return "";
  466. }
  467. } linkOnlyNode;
  468. //----------------------------------------------------------------------------
  469. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  470. {
  471. ConfigurationNode() {}
  472. virtual int NumExpectedParameters() const { return 0; }
  473. std::string Evaluate(const std::vector<std::string> &,
  474. cmGeneratorExpressionContext *context,
  475. const GeneratorExpressionContent *,
  476. cmGeneratorExpressionDAGChecker *) const
  477. {
  478. context->HadContextSensitiveCondition = true;
  479. return context->Config ? context->Config : "";
  480. }
  481. } configurationNode;
  482. //----------------------------------------------------------------------------
  483. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  484. {
  485. ConfigurationTestNode() {}
  486. virtual int NumExpectedParameters() const { return 1; }
  487. std::string Evaluate(const std::vector<std::string> &parameters,
  488. cmGeneratorExpressionContext *context,
  489. const GeneratorExpressionContent *content,
  490. cmGeneratorExpressionDAGChecker *) const
  491. {
  492. cmsys::RegularExpression configValidator;
  493. configValidator.compile("^[A-Za-z0-9_]*$");
  494. if (!configValidator.find(parameters.begin()->c_str()))
  495. {
  496. reportError(context, content->GetOriginalExpression(),
  497. "Expression syntax not recognized.");
  498. return std::string();
  499. }
  500. context->HadContextSensitiveCondition = true;
  501. if (!context->Config)
  502. {
  503. return parameters.front().empty() ? "1" : "0";
  504. }
  505. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  506. context->Config) == 0)
  507. {
  508. return "1";
  509. }
  510. if (context->CurrentTarget
  511. && context->CurrentTarget->IsImported())
  512. {
  513. const char* loc = 0;
  514. const char* imp = 0;
  515. std::string suffix;
  516. if (context->CurrentTarget->GetMappedConfig(context->Config,
  517. &loc,
  518. &imp,
  519. suffix))
  520. {
  521. // This imported target has an appropriate location
  522. // for this (possibly mapped) config.
  523. // Check if there is a proper config mapping for the tested config.
  524. std::vector<std::string> mappedConfigs;
  525. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  526. mapProp += cmSystemTools::UpperCase(context->Config);
  527. if(const char* mapValue =
  528. context->CurrentTarget->GetProperty(mapProp.c_str()))
  529. {
  530. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  531. mappedConfigs);
  532. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  533. cmSystemTools::UpperCase(parameters.front()))
  534. != mappedConfigs.end() ? "1" : "0";
  535. }
  536. }
  537. }
  538. return "0";
  539. }
  540. } configurationTestNode;
  541. static const struct JoinNode : public cmGeneratorExpressionNode
  542. {
  543. JoinNode() {}
  544. virtual int NumExpectedParameters() const { return 2; }
  545. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  546. std::string Evaluate(const std::vector<std::string> &parameters,
  547. cmGeneratorExpressionContext *,
  548. const GeneratorExpressionContent *,
  549. cmGeneratorExpressionDAGChecker *) const
  550. {
  551. std::string result;
  552. std::vector<std::string> list;
  553. cmSystemTools::ExpandListArgument(parameters.front(), list);
  554. std::string sep;
  555. for(std::vector<std::string>::const_iterator li = list.begin();
  556. li != list.end(); ++li)
  557. {
  558. result += sep + *li;
  559. sep = parameters[1];
  560. }
  561. return result;
  562. }
  563. } joinNode;
  564. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) \
  565. , "INTERFACE_" #PROPERTY
  566. //----------------------------------------------------------------------------
  567. static const char* targetPropertyTransitiveWhitelist[] = {
  568. 0
  569. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  570. };
  571. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  572. cmTarget const* target,
  573. cmTarget const* headTarget,
  574. cmGeneratorExpressionContext *context,
  575. cmGeneratorExpressionDAGChecker *dagChecker,
  576. const std::string &interfacePropertyName)
  577. {
  578. cmGeneratorExpression ge(context->Backtrace);
  579. std::string sep;
  580. std::string depString;
  581. for (std::vector<std::string>::const_iterator
  582. it = libraries.begin();
  583. it != libraries.end(); ++it)
  584. {
  585. if (*it == target->GetName())
  586. {
  587. // Broken code can have a target in its own link interface.
  588. // Don't follow such link interface entries so as not to create a
  589. // self-referencing loop.
  590. continue;
  591. }
  592. if (context->Makefile->FindTargetToUse(it->c_str()))
  593. {
  594. depString +=
  595. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  596. sep = ";";
  597. }
  598. }
  599. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  600. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  601. context->Config,
  602. context->Quiet,
  603. headTarget,
  604. target,
  605. dagChecker);
  606. if (cge->GetHadContextSensitiveCondition())
  607. {
  608. context->HadContextSensitiveCondition = true;
  609. }
  610. return linkedTargetsContent;
  611. }
  612. //----------------------------------------------------------------------------
  613. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  614. {
  615. TargetPropertyNode() {}
  616. // This node handles errors on parameter count itself.
  617. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  618. std::string Evaluate(const std::vector<std::string> &parameters,
  619. cmGeneratorExpressionContext *context,
  620. const GeneratorExpressionContent *content,
  621. cmGeneratorExpressionDAGChecker *dagCheckerParent
  622. ) const
  623. {
  624. if (parameters.size() != 1 && parameters.size() != 2)
  625. {
  626. reportError(context, content->GetOriginalExpression(),
  627. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  628. return std::string();
  629. }
  630. cmsys::RegularExpression propertyNameValidator;
  631. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  632. cmTarget const* target = context->HeadTarget;
  633. std::string propertyName = *parameters.begin();
  634. if (!target && parameters.size() == 1)
  635. {
  636. reportError(context, content->GetOriginalExpression(),
  637. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  638. "be used with add_custom_command. Specify the target to read a "
  639. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  640. "instead.");
  641. return std::string();
  642. }
  643. if (parameters.size() == 2)
  644. {
  645. if (parameters.begin()->empty() && parameters[1].empty())
  646. {
  647. reportError(context, content->GetOriginalExpression(),
  648. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  649. "target name and property name.");
  650. return std::string();
  651. }
  652. if (parameters.begin()->empty())
  653. {
  654. reportError(context, content->GetOriginalExpression(),
  655. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  656. "target name.");
  657. return std::string();
  658. }
  659. std::string targetName = parameters.front();
  660. propertyName = parameters[1];
  661. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  662. {
  663. if (!propertyNameValidator.find(propertyName.c_str()))
  664. {
  665. ::reportError(context, content->GetOriginalExpression(),
  666. "Target name and property name not supported.");
  667. return std::string();
  668. }
  669. ::reportError(context, content->GetOriginalExpression(),
  670. "Target name not supported.");
  671. return std::string();
  672. }
  673. if(propertyName == "ALIASED_TARGET")
  674. {
  675. if(context->Makefile->IsAlias(targetName.c_str()))
  676. {
  677. if(cmTarget* tgt =
  678. context->Makefile->FindTargetToUse(targetName.c_str()))
  679. {
  680. return tgt->GetName();
  681. }
  682. }
  683. return "";
  684. }
  685. target = context->Makefile->FindTargetToUse(
  686. targetName.c_str());
  687. if (!target)
  688. {
  689. cmOStringStream e;
  690. e << "Target \""
  691. << targetName
  692. << "\" not found.";
  693. reportError(context, content->GetOriginalExpression(), e.str());
  694. return std::string();
  695. }
  696. context->AllTargets.insert(target);
  697. }
  698. if (target == context->HeadTarget)
  699. {
  700. // Keep track of the properties seen while processing.
  701. // The evaluation of the LINK_LIBRARIES generator expressions
  702. // will check this to ensure that properties have one consistent
  703. // value for all evaluations.
  704. context->SeenTargetProperties.insert(propertyName);
  705. }
  706. if (propertyName.empty())
  707. {
  708. reportError(context, content->GetOriginalExpression(),
  709. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  710. "name.");
  711. return std::string();
  712. }
  713. if (!propertyNameValidator.find(propertyName.c_str()))
  714. {
  715. ::reportError(context, content->GetOriginalExpression(),
  716. "Property name not supported.");
  717. return std::string();
  718. }
  719. assert(target);
  720. if (propertyName == "LINKER_LANGUAGE")
  721. {
  722. if (target->LinkLanguagePropagatesToDependents() &&
  723. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries())
  724. {
  725. reportError(context, content->GetOriginalExpression(),
  726. "LINKER_LANGUAGE target property can not be used while evaluating "
  727. "link libraries for a static library");
  728. return std::string();
  729. }
  730. const char *lang = target->GetLinkerLanguage(context->Config);
  731. return lang ? lang : "";
  732. }
  733. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  734. target->GetName(),
  735. propertyName,
  736. content,
  737. dagCheckerParent);
  738. switch (dagChecker.check())
  739. {
  740. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  741. dagChecker.reportError(context, content->GetOriginalExpression());
  742. return std::string();
  743. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  744. // No error. We just skip cyclic references.
  745. return std::string();
  746. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  747. for (size_t i = 1;
  748. i < cmArraySize(targetPropertyTransitiveWhitelist);
  749. ++i)
  750. {
  751. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  752. {
  753. // No error. We're not going to find anything new here.
  754. return std::string();
  755. }
  756. }
  757. case cmGeneratorExpressionDAGChecker::DAG:
  758. break;
  759. }
  760. const char *prop = target->GetProperty(propertyName.c_str());
  761. if (dagCheckerParent)
  762. {
  763. if (dagCheckerParent->EvaluatingLinkLibraries())
  764. {
  765. if(!prop)
  766. {
  767. return std::string();
  768. }
  769. }
  770. else
  771. {
  772. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  773. dagCheckerParent->METHOD () ||
  774. assert(
  775. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  776. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  777. false);
  778. }
  779. }
  780. std::string linkedTargetsContent;
  781. std::string interfacePropertyName;
  782. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  783. if (propertyName == #prop || propertyName == "INTERFACE_" #prop) \
  784. { \
  785. interfacePropertyName = "INTERFACE_" #prop; \
  786. } \
  787. else
  788. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  789. // Note that the above macro terminates with an else
  790. /* else */ if (cmHasLiteralPrefix(propertyName.c_str(),
  791. "COMPILE_DEFINITIONS_"))
  792. {
  793. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  794. }
  795. #undef POPULATE_INTERFACE_PROPERTY_NAME
  796. cmTarget const* headTarget = context->HeadTarget
  797. ? context->HeadTarget : target;
  798. const char * const *transBegin =
  799. cmArrayBegin(targetPropertyTransitiveWhitelist) + 1;
  800. const char * const *transEnd =
  801. cmArrayEnd(targetPropertyTransitiveWhitelist);
  802. if (std::find_if(transBegin, transEnd,
  803. cmStrCmp(propertyName)) != transEnd)
  804. {
  805. std::vector<std::string> libs;
  806. target->GetTransitivePropertyLinkLibraries(context->Config,
  807. headTarget, libs);
  808. if (!libs.empty())
  809. {
  810. linkedTargetsContent =
  811. getLinkedTargetsContent(libs, target,
  812. headTarget,
  813. context, &dagChecker,
  814. interfacePropertyName);
  815. }
  816. }
  817. else if (std::find_if(transBegin, transEnd,
  818. cmStrCmp(interfacePropertyName)) != transEnd)
  819. {
  820. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  821. context->Config,
  822. headTarget);
  823. if(impl)
  824. {
  825. linkedTargetsContent =
  826. getLinkedTargetsContent(impl->Libraries, target,
  827. headTarget,
  828. context, &dagChecker,
  829. interfacePropertyName);
  830. }
  831. }
  832. linkedTargetsContent =
  833. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  834. if (!prop)
  835. {
  836. if (target->IsImported()
  837. || target->GetType() == cmTarget::INTERFACE_LIBRARY)
  838. {
  839. return linkedTargetsContent;
  840. }
  841. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  842. context->Config))
  843. {
  844. context->HadContextSensitiveCondition = true;
  845. return target->GetLinkInterfaceDependentBoolProperty(
  846. propertyName,
  847. context->Config) ? "1" : "0";
  848. }
  849. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  850. context->Config))
  851. {
  852. context->HadContextSensitiveCondition = true;
  853. const char *propContent =
  854. target->GetLinkInterfaceDependentStringProperty(
  855. propertyName,
  856. context->Config);
  857. return propContent ? propContent : "";
  858. }
  859. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  860. context->Config))
  861. {
  862. context->HadContextSensitiveCondition = true;
  863. const char *propContent =
  864. target->GetLinkInterfaceDependentNumberMinProperty(
  865. propertyName,
  866. context->Config);
  867. return propContent ? propContent : "";
  868. }
  869. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  870. context->Config))
  871. {
  872. context->HadContextSensitiveCondition = true;
  873. const char *propContent =
  874. target->GetLinkInterfaceDependentNumberMaxProperty(
  875. propertyName,
  876. context->Config);
  877. return propContent ? propContent : "";
  878. }
  879. return linkedTargetsContent;
  880. }
  881. if (!target->IsImported()
  882. && dagCheckerParent && !dagCheckerParent->EvaluatingLinkLibraries())
  883. {
  884. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  885. context->Config))
  886. {
  887. context->HadContextSensitiveCondition = true;
  888. const char *propContent =
  889. target->GetLinkInterfaceDependentNumberMinProperty(
  890. propertyName,
  891. context->Config);
  892. return propContent ? propContent : "";
  893. }
  894. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  895. context->Config))
  896. {
  897. context->HadContextSensitiveCondition = true;
  898. const char *propContent =
  899. target->GetLinkInterfaceDependentNumberMaxProperty(
  900. propertyName,
  901. context->Config);
  902. return propContent ? propContent : "";
  903. }
  904. }
  905. for (size_t i = 1;
  906. i < cmArraySize(targetPropertyTransitiveWhitelist);
  907. ++i)
  908. {
  909. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  910. {
  911. cmGeneratorExpression ge(context->Backtrace);
  912. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  913. std::string result = cge->Evaluate(context->Makefile,
  914. context->Config,
  915. context->Quiet,
  916. headTarget,
  917. target,
  918. &dagChecker);
  919. if (cge->GetHadContextSensitiveCondition())
  920. {
  921. context->HadContextSensitiveCondition = true;
  922. }
  923. if (!linkedTargetsContent.empty())
  924. {
  925. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  926. }
  927. return result;
  928. }
  929. }
  930. return prop;
  931. }
  932. } targetPropertyNode;
  933. //----------------------------------------------------------------------------
  934. static const struct TargetNameNode : public cmGeneratorExpressionNode
  935. {
  936. TargetNameNode() {}
  937. virtual bool GeneratesContent() const { return true; }
  938. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  939. virtual bool RequiresLiteralInput() const { return true; }
  940. std::string Evaluate(const std::vector<std::string> &parameters,
  941. cmGeneratorExpressionContext *,
  942. const GeneratorExpressionContent *,
  943. cmGeneratorExpressionDAGChecker *) const
  944. {
  945. return parameters.front();
  946. }
  947. virtual int NumExpectedParameters() const { return 1; }
  948. } targetNameNode;
  949. //----------------------------------------------------------------------------
  950. static const char* targetPolicyWhitelist[] = {
  951. 0
  952. #define TARGET_POLICY_STRING(POLICY) \
  953. , #POLICY
  954. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  955. #undef TARGET_POLICY_STRING
  956. };
  957. cmPolicies::PolicyStatus statusForTarget(cmTarget const* tgt,
  958. const char *policy)
  959. {
  960. #define RETURN_POLICY(POLICY) \
  961. if (strcmp(policy, #POLICY) == 0) \
  962. { \
  963. return tgt->GetPolicyStatus ## POLICY (); \
  964. } \
  965. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  966. #undef RETURN_POLICY
  967. assert("!Unreachable code. Not a valid policy");
  968. return cmPolicies::WARN;
  969. }
  970. cmPolicies::PolicyID policyForString(const char *policy_id)
  971. {
  972. #define RETURN_POLICY_ID(POLICY_ID) \
  973. if (strcmp(policy_id, #POLICY_ID) == 0) \
  974. { \
  975. return cmPolicies:: POLICY_ID; \
  976. } \
  977. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  978. #undef RETURN_POLICY_ID
  979. assert("!Unreachable code. Not a valid policy");
  980. return cmPolicies::CMP0002;
  981. }
  982. //----------------------------------------------------------------------------
  983. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  984. {
  985. TargetPolicyNode() {}
  986. virtual int NumExpectedParameters() const { return 1; }
  987. std::string Evaluate(const std::vector<std::string> &parameters,
  988. cmGeneratorExpressionContext *context ,
  989. const GeneratorExpressionContent *content,
  990. cmGeneratorExpressionDAGChecker *) const
  991. {
  992. if (!context->HeadTarget)
  993. {
  994. reportError(context, content->GetOriginalExpression(),
  995. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  996. "be used with add_custom_command.");
  997. return std::string();
  998. }
  999. context->HadContextSensitiveCondition = true;
  1000. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i)
  1001. {
  1002. const char *policy = targetPolicyWhitelist[i];
  1003. if (parameters.front() == policy)
  1004. {
  1005. cmMakefile *mf = context->HeadTarget->GetMakefile();
  1006. switch(statusForTarget(context->HeadTarget, policy))
  1007. {
  1008. case cmPolicies::WARN:
  1009. mf->IssueMessage(cmake::AUTHOR_WARNING,
  1010. mf->GetPolicies()->
  1011. GetPolicyWarning(policyForString(policy)));
  1012. case cmPolicies::REQUIRED_IF_USED:
  1013. case cmPolicies::REQUIRED_ALWAYS:
  1014. case cmPolicies::OLD:
  1015. return "0";
  1016. case cmPolicies::NEW:
  1017. return "1";
  1018. }
  1019. }
  1020. }
  1021. reportError(context, content->GetOriginalExpression(),
  1022. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1023. "policies. Currently it may be used with the following policies:\n"
  1024. #define STRINGIFY_HELPER(X) #X
  1025. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1026. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  1027. " * " STRINGIFY(POLICY) "\n"
  1028. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1029. #undef TARGET_POLICY_LIST_ITEM
  1030. );
  1031. return std::string();
  1032. }
  1033. } targetPolicyNode;
  1034. //----------------------------------------------------------------------------
  1035. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1036. {
  1037. InstallPrefixNode() {}
  1038. virtual bool GeneratesContent() const { return true; }
  1039. virtual int NumExpectedParameters() const { return 0; }
  1040. std::string Evaluate(const std::vector<std::string> &,
  1041. cmGeneratorExpressionContext *context,
  1042. const GeneratorExpressionContent *content,
  1043. cmGeneratorExpressionDAGChecker *) const
  1044. {
  1045. reportError(context, content->GetOriginalExpression(),
  1046. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1047. "should never be evaluated.");
  1048. return std::string();
  1049. }
  1050. } installPrefixNode;
  1051. //----------------------------------------------------------------------------
  1052. template<bool linker, bool soname>
  1053. struct TargetFilesystemArtifactResultCreator
  1054. {
  1055. static std::string Create(cmTarget* target,
  1056. cmGeneratorExpressionContext *context,
  1057. const GeneratorExpressionContent *content);
  1058. };
  1059. //----------------------------------------------------------------------------
  1060. template<>
  1061. struct TargetFilesystemArtifactResultCreator<false, true>
  1062. {
  1063. static std::string Create(cmTarget* target,
  1064. cmGeneratorExpressionContext *context,
  1065. const GeneratorExpressionContent *content)
  1066. {
  1067. // The target soname file (.so.1).
  1068. if(target->IsDLLPlatform())
  1069. {
  1070. ::reportError(context, content->GetOriginalExpression(),
  1071. "TARGET_SONAME_FILE is not allowed "
  1072. "for DLL target platforms.");
  1073. return std::string();
  1074. }
  1075. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1076. {
  1077. ::reportError(context, content->GetOriginalExpression(),
  1078. "TARGET_SONAME_FILE is allowed only for "
  1079. "SHARED libraries.");
  1080. return std::string();
  1081. }
  1082. std::string result = target->GetDirectory(context->Config);
  1083. result += "/";
  1084. result += target->GetSOName(context->Config);
  1085. return result;
  1086. }
  1087. };
  1088. //----------------------------------------------------------------------------
  1089. template<>
  1090. struct TargetFilesystemArtifactResultCreator<true, false>
  1091. {
  1092. static std::string Create(cmTarget* target,
  1093. cmGeneratorExpressionContext *context,
  1094. const GeneratorExpressionContent *content)
  1095. {
  1096. // The file used to link to the target (.so, .lib, .a).
  1097. if(!target->IsLinkable())
  1098. {
  1099. ::reportError(context, content->GetOriginalExpression(),
  1100. "TARGET_LINKER_FILE is allowed only for libraries and "
  1101. "executables with ENABLE_EXPORTS.");
  1102. return std::string();
  1103. }
  1104. return target->GetFullPath(context->Config,
  1105. target->HasImportLibrary());
  1106. }
  1107. };
  1108. //----------------------------------------------------------------------------
  1109. template<>
  1110. struct TargetFilesystemArtifactResultCreator<false, false>
  1111. {
  1112. static std::string Create(cmTarget* target,
  1113. cmGeneratorExpressionContext *context,
  1114. const GeneratorExpressionContent *)
  1115. {
  1116. return target->GetFullPath(context->Config, false, true);
  1117. }
  1118. };
  1119. //----------------------------------------------------------------------------
  1120. template<bool dirQual, bool nameQual>
  1121. struct TargetFilesystemArtifactResultGetter
  1122. {
  1123. static std::string Get(const std::string &result);
  1124. };
  1125. //----------------------------------------------------------------------------
  1126. template<>
  1127. struct TargetFilesystemArtifactResultGetter<false, true>
  1128. {
  1129. static std::string Get(const std::string &result)
  1130. { return cmSystemTools::GetFilenameName(result); }
  1131. };
  1132. //----------------------------------------------------------------------------
  1133. template<>
  1134. struct TargetFilesystemArtifactResultGetter<true, false>
  1135. {
  1136. static std::string Get(const std::string &result)
  1137. { return cmSystemTools::GetFilenamePath(result); }
  1138. };
  1139. //----------------------------------------------------------------------------
  1140. template<>
  1141. struct TargetFilesystemArtifactResultGetter<false, false>
  1142. {
  1143. static std::string Get(const std::string &result)
  1144. { return result; }
  1145. };
  1146. //----------------------------------------------------------------------------
  1147. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1148. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1149. {
  1150. TargetFilesystemArtifact() {}
  1151. virtual int NumExpectedParameters() const { return 1; }
  1152. std::string Evaluate(const std::vector<std::string> &parameters,
  1153. cmGeneratorExpressionContext *context,
  1154. const GeneratorExpressionContent *content,
  1155. cmGeneratorExpressionDAGChecker *dagChecker) const
  1156. {
  1157. // Lookup the referenced target.
  1158. std::string name = *parameters.begin();
  1159. if (!cmGeneratorExpression::IsValidTargetName(name))
  1160. {
  1161. ::reportError(context, content->GetOriginalExpression(),
  1162. "Expression syntax not recognized.");
  1163. return std::string();
  1164. }
  1165. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1166. if(!target)
  1167. {
  1168. ::reportError(context, content->GetOriginalExpression(),
  1169. "No target \"" + name + "\"");
  1170. return std::string();
  1171. }
  1172. if(target->GetType() >= cmTarget::OBJECT_LIBRARY &&
  1173. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1174. {
  1175. ::reportError(context, content->GetOriginalExpression(),
  1176. "Target \"" + name + "\" is not an executable or library.");
  1177. return std::string();
  1178. }
  1179. if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
  1180. {
  1181. ::reportError(context, content->GetOriginalExpression(),
  1182. "Expressions which require the linker language may not "
  1183. "be used while evaluating link libraries");
  1184. return std::string();
  1185. }
  1186. context->DependTargets.insert(target);
  1187. context->AllTargets.insert(target);
  1188. std::string result =
  1189. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1190. target,
  1191. context,
  1192. content);
  1193. if (context->HadError)
  1194. {
  1195. return std::string();
  1196. }
  1197. return
  1198. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1199. }
  1200. };
  1201. //----------------------------------------------------------------------------
  1202. static const
  1203. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1204. static const
  1205. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1206. static const
  1207. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1208. static const
  1209. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1210. static const
  1211. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1212. static const
  1213. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1214. static const
  1215. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1216. static const
  1217. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1218. static const
  1219. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1220. //----------------------------------------------------------------------------
  1221. static const
  1222. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1223. {
  1224. if (identifier == "0")
  1225. return &zeroNode;
  1226. else if (identifier == "1")
  1227. return &oneNode;
  1228. else if (identifier == "AND")
  1229. return &andNode;
  1230. else if (identifier == "OR")
  1231. return &orNode;
  1232. else if (identifier == "NOT")
  1233. return &notNode;
  1234. else if (identifier == "C_COMPILER_ID")
  1235. return &cCompilerIdNode;
  1236. else if (identifier == "CXX_COMPILER_ID")
  1237. return &cxxCompilerIdNode;
  1238. else if (identifier == "VERSION_GREATER")
  1239. return &versionGreaterNode;
  1240. else if (identifier == "VERSION_LESS")
  1241. return &versionLessNode;
  1242. else if (identifier == "VERSION_EQUAL")
  1243. return &versionEqualNode;
  1244. else if (identifier == "C_COMPILER_VERSION")
  1245. return &cCompilerVersionNode;
  1246. else if (identifier == "CXX_COMPILER_VERSION")
  1247. return &cxxCompilerVersionNode;
  1248. else if (identifier == "PLATFORM_ID")
  1249. return &platformIdNode;
  1250. else if (identifier == "CONFIGURATION")
  1251. return &configurationNode;
  1252. else if (identifier == "CONFIG")
  1253. return &configurationTestNode;
  1254. else if (identifier == "TARGET_FILE")
  1255. return &targetFileNode;
  1256. else if (identifier == "TARGET_LINKER_FILE")
  1257. return &targetLinkerFileNode;
  1258. else if (identifier == "TARGET_SONAME_FILE")
  1259. return &targetSoNameFileNode;
  1260. else if (identifier == "TARGET_FILE_NAME")
  1261. return &targetFileNameNode;
  1262. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1263. return &targetLinkerFileNameNode;
  1264. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1265. return &targetSoNameFileNameNode;
  1266. else if (identifier == "TARGET_FILE_DIR")
  1267. return &targetFileDirNode;
  1268. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1269. return &targetLinkerFileDirNode;
  1270. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1271. return &targetSoNameFileDirNode;
  1272. else if (identifier == "STREQUAL")
  1273. return &strEqualNode;
  1274. else if (identifier == "BOOL")
  1275. return &boolNode;
  1276. else if (identifier == "ANGLE-R")
  1277. return &angle_rNode;
  1278. else if (identifier == "COMMA")
  1279. return &commaNode;
  1280. else if (identifier == "SEMICOLON")
  1281. return &semicolonNode;
  1282. else if (identifier == "TARGET_PROPERTY")
  1283. return &targetPropertyNode;
  1284. else if (identifier == "TARGET_NAME")
  1285. return &targetNameNode;
  1286. else if (identifier == "TARGET_POLICY")
  1287. return &targetPolicyNode;
  1288. else if (identifier == "BUILD_INTERFACE")
  1289. return &buildInterfaceNode;
  1290. else if (identifier == "INSTALL_INTERFACE")
  1291. return &installInterfaceNode;
  1292. else if (identifier == "INSTALL_PREFIX")
  1293. return &installPrefixNode;
  1294. else if (identifier == "JOIN")
  1295. return &joinNode;
  1296. else if (identifier == "LINK_ONLY")
  1297. return &linkOnlyNode;
  1298. return 0;
  1299. }
  1300. //----------------------------------------------------------------------------
  1301. GeneratorExpressionContent::GeneratorExpressionContent(
  1302. const char *startContent,
  1303. size_t length)
  1304. : StartContent(startContent), ContentLength(length)
  1305. {
  1306. }
  1307. //----------------------------------------------------------------------------
  1308. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1309. {
  1310. return std::string(this->StartContent, this->ContentLength);
  1311. }
  1312. //----------------------------------------------------------------------------
  1313. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1314. const cmGeneratorExpressionNode *node,
  1315. const std::string &identifier,
  1316. cmGeneratorExpressionContext *context,
  1317. cmGeneratorExpressionDAGChecker *dagChecker,
  1318. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1319. pit) const
  1320. {
  1321. std::string result;
  1322. const
  1323. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1324. pend = this->ParamChildren.end();
  1325. for ( ; pit != pend; ++pit)
  1326. {
  1327. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1328. = pit->begin();
  1329. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1330. = pit->end();
  1331. for ( ; it != end; ++it)
  1332. {
  1333. if (node->RequiresLiteralInput())
  1334. {
  1335. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1336. {
  1337. reportError(context, this->GetOriginalExpression(),
  1338. "$<" + identifier + "> expression requires literal input.");
  1339. return std::string();
  1340. }
  1341. }
  1342. result += (*it)->Evaluate(context, dagChecker);
  1343. if (context->HadError)
  1344. {
  1345. return std::string();
  1346. }
  1347. }
  1348. if ((pit + 1) != pend)
  1349. {
  1350. result += ",";
  1351. }
  1352. }
  1353. if (node->RequiresLiteralInput())
  1354. {
  1355. std::vector<std::string> parameters;
  1356. parameters.push_back(result);
  1357. return node->Evaluate(parameters, context, this, dagChecker);
  1358. }
  1359. return result;
  1360. }
  1361. //----------------------------------------------------------------------------
  1362. std::string GeneratorExpressionContent::Evaluate(
  1363. cmGeneratorExpressionContext *context,
  1364. cmGeneratorExpressionDAGChecker *dagChecker) const
  1365. {
  1366. std::string identifier;
  1367. {
  1368. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1369. = this->IdentifierChildren.begin();
  1370. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1371. = this->IdentifierChildren.end();
  1372. for ( ; it != end; ++it)
  1373. {
  1374. identifier += (*it)->Evaluate(context, dagChecker);
  1375. if (context->HadError)
  1376. {
  1377. return std::string();
  1378. }
  1379. }
  1380. }
  1381. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1382. if (!node)
  1383. {
  1384. reportError(context, this->GetOriginalExpression(),
  1385. "Expression did not evaluate to a known generator expression");
  1386. return std::string();
  1387. }
  1388. if (!node->GeneratesContent())
  1389. {
  1390. if (node->NumExpectedParameters() == 1
  1391. && node->AcceptsArbitraryContentParameter())
  1392. {
  1393. if (this->ParamChildren.empty())
  1394. {
  1395. reportError(context, this->GetOriginalExpression(),
  1396. "$<" + identifier + "> expression requires a parameter.");
  1397. }
  1398. }
  1399. else
  1400. {
  1401. std::vector<std::string> parameters;
  1402. this->EvaluateParameters(node, identifier, context, dagChecker,
  1403. parameters);
  1404. }
  1405. return std::string();
  1406. }
  1407. if (node->NumExpectedParameters() == 1
  1408. && node->AcceptsArbitraryContentParameter())
  1409. {
  1410. return this->ProcessArbitraryContent(node, identifier, context,
  1411. dagChecker,
  1412. this->ParamChildren.begin());
  1413. }
  1414. std::vector<std::string> parameters;
  1415. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1416. if (context->HadError)
  1417. {
  1418. return std::string();
  1419. }
  1420. return node->Evaluate(parameters, context, this, dagChecker);
  1421. }
  1422. //----------------------------------------------------------------------------
  1423. std::string GeneratorExpressionContent::EvaluateParameters(
  1424. const cmGeneratorExpressionNode *node,
  1425. const std::string &identifier,
  1426. cmGeneratorExpressionContext *context,
  1427. cmGeneratorExpressionDAGChecker *dagChecker,
  1428. std::vector<std::string> &parameters) const
  1429. {
  1430. const int numExpected = node->NumExpectedParameters();
  1431. {
  1432. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1433. pit = this->ParamChildren.begin();
  1434. const
  1435. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1436. pend = this->ParamChildren.end();
  1437. const bool acceptsArbitraryContent
  1438. = node->AcceptsArbitraryContentParameter();
  1439. for ( ; pit != pend; ++pit)
  1440. {
  1441. std::string parameter;
  1442. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1443. pit->begin();
  1444. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1445. pit->end();
  1446. for ( ; it != end; ++it)
  1447. {
  1448. parameter += (*it)->Evaluate(context, dagChecker);
  1449. if (context->HadError)
  1450. {
  1451. return std::string();
  1452. }
  1453. }
  1454. parameters.push_back(parameter);
  1455. if (acceptsArbitraryContent
  1456. && parameters.size() == (unsigned int)numExpected - 1)
  1457. {
  1458. assert(pit != pend);
  1459. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1460. context,
  1461. dagChecker,
  1462. pit + 1);
  1463. parameters.push_back(lastParam);
  1464. return std::string();
  1465. }
  1466. }
  1467. }
  1468. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1469. && (unsigned int)numExpected != parameters.size()))
  1470. {
  1471. if (numExpected == 0)
  1472. {
  1473. reportError(context, this->GetOriginalExpression(),
  1474. "$<" + identifier + "> expression requires no parameters.");
  1475. }
  1476. else if (numExpected == 1)
  1477. {
  1478. reportError(context, this->GetOriginalExpression(),
  1479. "$<" + identifier + "> expression requires "
  1480. "exactly one parameter.");
  1481. }
  1482. else
  1483. {
  1484. cmOStringStream e;
  1485. e << "$<" + identifier + "> expression requires "
  1486. << numExpected
  1487. << " comma separated parameters, but got "
  1488. << parameters.size() << " instead.";
  1489. reportError(context, this->GetOriginalExpression(), e.str());
  1490. }
  1491. return std::string();
  1492. }
  1493. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1494. && parameters.empty())
  1495. {
  1496. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1497. + "> expression requires at least one parameter.");
  1498. }
  1499. return std::string();
  1500. }
  1501. //----------------------------------------------------------------------------
  1502. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1503. {
  1504. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1505. = c.begin();
  1506. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1507. = c.end();
  1508. for ( ; it != end; ++it)
  1509. {
  1510. delete *it;
  1511. }
  1512. }
  1513. //----------------------------------------------------------------------------
  1514. GeneratorExpressionContent::~GeneratorExpressionContent()
  1515. {
  1516. deleteAll(this->IdentifierChildren);
  1517. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1518. std::vector<EvaluatorVector>::const_iterator pit =
  1519. this->ParamChildren.begin();
  1520. const std::vector<EvaluatorVector>::const_iterator pend =
  1521. this->ParamChildren.end();
  1522. for ( ; pit != pend; ++pit)
  1523. {
  1524. deleteAll(*pit);
  1525. }
  1526. }