cmGeneratorExpressionEvaluator.cxx 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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. , #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 *target,
  573. cmTarget *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* 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. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  783. || propertyName == "INCLUDE_DIRECTORIES")
  784. {
  785. interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
  786. }
  787. else if (propertyName == "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")
  788. {
  789. interfacePropertyName = "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES";
  790. }
  791. else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
  792. || propertyName == "COMPILE_DEFINITIONS"
  793. || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
  794. {
  795. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  796. }
  797. else if (propertyName == "INTERFACE_COMPILE_OPTIONS"
  798. || propertyName == "COMPILE_OPTIONS")
  799. {
  800. interfacePropertyName = "INTERFACE_COMPILE_OPTIONS";
  801. }
  802. cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;
  803. const char * const *transBegin =
  804. cmArrayBegin(targetPropertyTransitiveWhitelist) + 1;
  805. const char * const *transEnd =
  806. cmArrayEnd(targetPropertyTransitiveWhitelist);
  807. if (std::find_if(transBegin, transEnd,
  808. cmStrCmp(propertyName)) != transEnd)
  809. {
  810. std::vector<std::string> libs;
  811. target->GetTransitivePropertyLinkLibraries(context->Config,
  812. headTarget, libs);
  813. if (!libs.empty())
  814. {
  815. linkedTargetsContent =
  816. getLinkedTargetsContent(libs, target,
  817. headTarget,
  818. context, &dagChecker,
  819. interfacePropertyName);
  820. }
  821. }
  822. else if (std::find_if(transBegin, transEnd,
  823. cmStrCmp(interfacePropertyName)) != transEnd)
  824. {
  825. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  826. context->Config,
  827. headTarget);
  828. if(impl)
  829. {
  830. linkedTargetsContent =
  831. getLinkedTargetsContent(impl->Libraries, target,
  832. headTarget,
  833. context, &dagChecker,
  834. interfacePropertyName);
  835. }
  836. }
  837. linkedTargetsContent =
  838. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  839. if (!prop)
  840. {
  841. if (target->IsImported())
  842. {
  843. return linkedTargetsContent;
  844. }
  845. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  846. context->Config))
  847. {
  848. context->HadContextSensitiveCondition = true;
  849. return target->GetLinkInterfaceDependentBoolProperty(
  850. propertyName,
  851. context->Config) ? "1" : "0";
  852. }
  853. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  854. context->Config))
  855. {
  856. context->HadContextSensitiveCondition = true;
  857. const char *propContent =
  858. target->GetLinkInterfaceDependentStringProperty(
  859. propertyName,
  860. context->Config);
  861. return propContent ? propContent : "";
  862. }
  863. return linkedTargetsContent;
  864. }
  865. for (size_t i = 1;
  866. i < cmArraySize(targetPropertyTransitiveWhitelist);
  867. ++i)
  868. {
  869. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  870. {
  871. cmGeneratorExpression ge(context->Backtrace);
  872. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  873. std::string result = cge->Evaluate(context->Makefile,
  874. context->Config,
  875. context->Quiet,
  876. headTarget,
  877. target,
  878. &dagChecker);
  879. if (cge->GetHadContextSensitiveCondition())
  880. {
  881. context->HadContextSensitiveCondition = true;
  882. }
  883. if (!linkedTargetsContent.empty())
  884. {
  885. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  886. }
  887. return result;
  888. }
  889. }
  890. return prop;
  891. }
  892. } targetPropertyNode;
  893. //----------------------------------------------------------------------------
  894. static const struct TargetNameNode : public cmGeneratorExpressionNode
  895. {
  896. TargetNameNode() {}
  897. virtual bool GeneratesContent() const { return true; }
  898. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  899. virtual bool RequiresLiteralInput() const { return true; }
  900. std::string Evaluate(const std::vector<std::string> &parameters,
  901. cmGeneratorExpressionContext *,
  902. const GeneratorExpressionContent *,
  903. cmGeneratorExpressionDAGChecker *) const
  904. {
  905. return parameters.front();
  906. }
  907. virtual int NumExpectedParameters() const { return 1; }
  908. } targetNameNode;
  909. //----------------------------------------------------------------------------
  910. static const char* targetPolicyWhitelist[] = {
  911. 0
  912. #define TARGET_POLICY_STRING(POLICY) \
  913. , #POLICY
  914. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  915. #undef TARGET_POLICY_STRING
  916. };
  917. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  918. {
  919. #define RETURN_POLICY(POLICY) \
  920. if (strcmp(policy, #POLICY) == 0) \
  921. { \
  922. return tgt->GetPolicyStatus ## POLICY (); \
  923. } \
  924. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  925. #undef RETURN_POLICY
  926. assert("!Unreachable code. Not a valid policy");
  927. return cmPolicies::WARN;
  928. }
  929. cmPolicies::PolicyID policyForString(const char *policy_id)
  930. {
  931. #define RETURN_POLICY_ID(POLICY_ID) \
  932. if (strcmp(policy_id, #POLICY_ID) == 0) \
  933. { \
  934. return cmPolicies:: POLICY_ID; \
  935. } \
  936. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  937. #undef RETURN_POLICY_ID
  938. assert("!Unreachable code. Not a valid policy");
  939. return cmPolicies::CMP0002;
  940. }
  941. //----------------------------------------------------------------------------
  942. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  943. {
  944. TargetPolicyNode() {}
  945. virtual int NumExpectedParameters() const { return 1; }
  946. std::string Evaluate(const std::vector<std::string> &parameters,
  947. cmGeneratorExpressionContext *context ,
  948. const GeneratorExpressionContent *content,
  949. cmGeneratorExpressionDAGChecker *) const
  950. {
  951. if (!context->HeadTarget)
  952. {
  953. reportError(context, content->GetOriginalExpression(),
  954. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  955. "be used with add_custom_command.");
  956. return std::string();
  957. }
  958. context->HadContextSensitiveCondition = true;
  959. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i)
  960. {
  961. const char *policy = targetPolicyWhitelist[i];
  962. if (parameters.front() == policy)
  963. {
  964. cmMakefile *mf = context->HeadTarget->GetMakefile();
  965. switch(statusForTarget(context->HeadTarget, policy))
  966. {
  967. case cmPolicies::WARN:
  968. mf->IssueMessage(cmake::AUTHOR_WARNING,
  969. mf->GetPolicies()->
  970. GetPolicyWarning(policyForString(policy)));
  971. case cmPolicies::REQUIRED_IF_USED:
  972. case cmPolicies::REQUIRED_ALWAYS:
  973. case cmPolicies::OLD:
  974. return "0";
  975. case cmPolicies::NEW:
  976. return "1";
  977. }
  978. }
  979. }
  980. reportError(context, content->GetOriginalExpression(),
  981. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  982. "policies. Currently it may be used with the following policies:\n"
  983. #define STRINGIFY_HELPER(X) #X
  984. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  985. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  986. " * " STRINGIFY(POLICY) "\n"
  987. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  988. #undef TARGET_POLICY_LIST_ITEM
  989. );
  990. return std::string();
  991. }
  992. } targetPolicyNode;
  993. //----------------------------------------------------------------------------
  994. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  995. {
  996. InstallPrefixNode() {}
  997. virtual bool GeneratesContent() const { return true; }
  998. virtual int NumExpectedParameters() const { return 0; }
  999. std::string Evaluate(const std::vector<std::string> &,
  1000. cmGeneratorExpressionContext *context,
  1001. const GeneratorExpressionContent *content,
  1002. cmGeneratorExpressionDAGChecker *) const
  1003. {
  1004. reportError(context, content->GetOriginalExpression(),
  1005. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1006. "should never be evaluated.");
  1007. return std::string();
  1008. }
  1009. } installPrefixNode;
  1010. //----------------------------------------------------------------------------
  1011. template<bool linker, bool soname>
  1012. struct TargetFilesystemArtifactResultCreator
  1013. {
  1014. static std::string Create(cmTarget* target,
  1015. cmGeneratorExpressionContext *context,
  1016. const GeneratorExpressionContent *content);
  1017. };
  1018. //----------------------------------------------------------------------------
  1019. template<>
  1020. struct TargetFilesystemArtifactResultCreator<false, true>
  1021. {
  1022. static std::string Create(cmTarget* target,
  1023. cmGeneratorExpressionContext *context,
  1024. const GeneratorExpressionContent *content)
  1025. {
  1026. // The target soname file (.so.1).
  1027. if(target->IsDLLPlatform())
  1028. {
  1029. ::reportError(context, content->GetOriginalExpression(),
  1030. "TARGET_SONAME_FILE is not allowed "
  1031. "for DLL target platforms.");
  1032. return std::string();
  1033. }
  1034. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1035. {
  1036. ::reportError(context, content->GetOriginalExpression(),
  1037. "TARGET_SONAME_FILE is allowed only for "
  1038. "SHARED libraries.");
  1039. return std::string();
  1040. }
  1041. std::string result = target->GetDirectory(context->Config);
  1042. result += "/";
  1043. result += target->GetSOName(context->Config);
  1044. return result;
  1045. }
  1046. };
  1047. //----------------------------------------------------------------------------
  1048. template<>
  1049. struct TargetFilesystemArtifactResultCreator<true, false>
  1050. {
  1051. static std::string Create(cmTarget* target,
  1052. cmGeneratorExpressionContext *context,
  1053. const GeneratorExpressionContent *content)
  1054. {
  1055. // The file used to link to the target (.so, .lib, .a).
  1056. if(!target->IsLinkable())
  1057. {
  1058. ::reportError(context, content->GetOriginalExpression(),
  1059. "TARGET_LINKER_FILE is allowed only for libraries and "
  1060. "executables with ENABLE_EXPORTS.");
  1061. return std::string();
  1062. }
  1063. return target->GetFullPath(context->Config,
  1064. target->HasImportLibrary());
  1065. }
  1066. };
  1067. //----------------------------------------------------------------------------
  1068. template<>
  1069. struct TargetFilesystemArtifactResultCreator<false, false>
  1070. {
  1071. static std::string Create(cmTarget* target,
  1072. cmGeneratorExpressionContext *context,
  1073. const GeneratorExpressionContent *)
  1074. {
  1075. return target->GetFullPath(context->Config, false, true);
  1076. }
  1077. };
  1078. //----------------------------------------------------------------------------
  1079. template<bool dirQual, bool nameQual>
  1080. struct TargetFilesystemArtifactResultGetter
  1081. {
  1082. static std::string Get(const std::string &result);
  1083. };
  1084. //----------------------------------------------------------------------------
  1085. template<>
  1086. struct TargetFilesystemArtifactResultGetter<false, true>
  1087. {
  1088. static std::string Get(const std::string &result)
  1089. { return cmSystemTools::GetFilenameName(result); }
  1090. };
  1091. //----------------------------------------------------------------------------
  1092. template<>
  1093. struct TargetFilesystemArtifactResultGetter<true, false>
  1094. {
  1095. static std::string Get(const std::string &result)
  1096. { return cmSystemTools::GetFilenamePath(result); }
  1097. };
  1098. //----------------------------------------------------------------------------
  1099. template<>
  1100. struct TargetFilesystemArtifactResultGetter<false, false>
  1101. {
  1102. static std::string Get(const std::string &result)
  1103. { return result; }
  1104. };
  1105. //----------------------------------------------------------------------------
  1106. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1107. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1108. {
  1109. TargetFilesystemArtifact() {}
  1110. virtual int NumExpectedParameters() const { return 1; }
  1111. std::string Evaluate(const std::vector<std::string> &parameters,
  1112. cmGeneratorExpressionContext *context,
  1113. const GeneratorExpressionContent *content,
  1114. cmGeneratorExpressionDAGChecker *dagChecker) const
  1115. {
  1116. // Lookup the referenced target.
  1117. std::string name = *parameters.begin();
  1118. if (!cmGeneratorExpression::IsValidTargetName(name))
  1119. {
  1120. ::reportError(context, content->GetOriginalExpression(),
  1121. "Expression syntax not recognized.");
  1122. return std::string();
  1123. }
  1124. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1125. if(!target)
  1126. {
  1127. ::reportError(context, content->GetOriginalExpression(),
  1128. "No target \"" + name + "\"");
  1129. return std::string();
  1130. }
  1131. if(target->GetType() >= cmTarget::UTILITY &&
  1132. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1133. {
  1134. ::reportError(context, content->GetOriginalExpression(),
  1135. "Target \"" + name + "\" is not an executable or library.");
  1136. return std::string();
  1137. }
  1138. if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
  1139. {
  1140. ::reportError(context, content->GetOriginalExpression(),
  1141. "Expressions which require the linker language may not "
  1142. "be used while evaluating link libraries");
  1143. return std::string();
  1144. }
  1145. context->DependTargets.insert(target);
  1146. context->AllTargets.insert(target);
  1147. std::string result =
  1148. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1149. target,
  1150. context,
  1151. content);
  1152. if (context->HadError)
  1153. {
  1154. return std::string();
  1155. }
  1156. return
  1157. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1158. }
  1159. };
  1160. //----------------------------------------------------------------------------
  1161. static const
  1162. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1163. static const
  1164. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1165. static const
  1166. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1167. static const
  1168. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1169. static const
  1170. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1171. static const
  1172. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1173. static const
  1174. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1175. static const
  1176. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1177. static const
  1178. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1179. //----------------------------------------------------------------------------
  1180. static const
  1181. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1182. {
  1183. if (identifier == "0")
  1184. return &zeroNode;
  1185. else if (identifier == "1")
  1186. return &oneNode;
  1187. else if (identifier == "AND")
  1188. return &andNode;
  1189. else if (identifier == "OR")
  1190. return &orNode;
  1191. else if (identifier == "NOT")
  1192. return &notNode;
  1193. else if (identifier == "C_COMPILER_ID")
  1194. return &cCompilerIdNode;
  1195. else if (identifier == "CXX_COMPILER_ID")
  1196. return &cxxCompilerIdNode;
  1197. else if (identifier == "VERSION_GREATER")
  1198. return &versionGreaterNode;
  1199. else if (identifier == "VERSION_LESS")
  1200. return &versionLessNode;
  1201. else if (identifier == "VERSION_EQUAL")
  1202. return &versionEqualNode;
  1203. else if (identifier == "C_COMPILER_VERSION")
  1204. return &cCompilerVersionNode;
  1205. else if (identifier == "CXX_COMPILER_VERSION")
  1206. return &cxxCompilerVersionNode;
  1207. else if (identifier == "PLATFORM_ID")
  1208. return &platformIdNode;
  1209. else if (identifier == "CONFIGURATION")
  1210. return &configurationNode;
  1211. else if (identifier == "CONFIG")
  1212. return &configurationTestNode;
  1213. else if (identifier == "TARGET_FILE")
  1214. return &targetFileNode;
  1215. else if (identifier == "TARGET_LINKER_FILE")
  1216. return &targetLinkerFileNode;
  1217. else if (identifier == "TARGET_SONAME_FILE")
  1218. return &targetSoNameFileNode;
  1219. else if (identifier == "TARGET_FILE_NAME")
  1220. return &targetFileNameNode;
  1221. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1222. return &targetLinkerFileNameNode;
  1223. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1224. return &targetSoNameFileNameNode;
  1225. else if (identifier == "TARGET_FILE_DIR")
  1226. return &targetFileDirNode;
  1227. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1228. return &targetLinkerFileDirNode;
  1229. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1230. return &targetSoNameFileDirNode;
  1231. else if (identifier == "STREQUAL")
  1232. return &strEqualNode;
  1233. else if (identifier == "BOOL")
  1234. return &boolNode;
  1235. else if (identifier == "ANGLE-R")
  1236. return &angle_rNode;
  1237. else if (identifier == "COMMA")
  1238. return &commaNode;
  1239. else if (identifier == "SEMICOLON")
  1240. return &semicolonNode;
  1241. else if (identifier == "TARGET_PROPERTY")
  1242. return &targetPropertyNode;
  1243. else if (identifier == "TARGET_NAME")
  1244. return &targetNameNode;
  1245. else if (identifier == "TARGET_POLICY")
  1246. return &targetPolicyNode;
  1247. else if (identifier == "BUILD_INTERFACE")
  1248. return &buildInterfaceNode;
  1249. else if (identifier == "INSTALL_INTERFACE")
  1250. return &installInterfaceNode;
  1251. else if (identifier == "INSTALL_PREFIX")
  1252. return &installPrefixNode;
  1253. else if (identifier == "JOIN")
  1254. return &joinNode;
  1255. else if (identifier == "LINK_ONLY")
  1256. return &linkOnlyNode;
  1257. return 0;
  1258. }
  1259. //----------------------------------------------------------------------------
  1260. GeneratorExpressionContent::GeneratorExpressionContent(
  1261. const char *startContent,
  1262. size_t length)
  1263. : StartContent(startContent), ContentLength(length)
  1264. {
  1265. }
  1266. //----------------------------------------------------------------------------
  1267. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1268. {
  1269. return std::string(this->StartContent, this->ContentLength);
  1270. }
  1271. //----------------------------------------------------------------------------
  1272. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1273. const cmGeneratorExpressionNode *node,
  1274. const std::string &identifier,
  1275. cmGeneratorExpressionContext *context,
  1276. cmGeneratorExpressionDAGChecker *dagChecker,
  1277. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1278. pit) const
  1279. {
  1280. std::string result;
  1281. const
  1282. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1283. pend = this->ParamChildren.end();
  1284. for ( ; pit != pend; ++pit)
  1285. {
  1286. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1287. = pit->begin();
  1288. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1289. = pit->end();
  1290. for ( ; it != end; ++it)
  1291. {
  1292. if (node->RequiresLiteralInput())
  1293. {
  1294. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1295. {
  1296. reportError(context, this->GetOriginalExpression(),
  1297. "$<" + identifier + "> expression requires literal input.");
  1298. return std::string();
  1299. }
  1300. }
  1301. result += (*it)->Evaluate(context, dagChecker);
  1302. if (context->HadError)
  1303. {
  1304. return std::string();
  1305. }
  1306. }
  1307. if ((pit + 1) != pend)
  1308. {
  1309. result += ",";
  1310. }
  1311. }
  1312. if (node->RequiresLiteralInput())
  1313. {
  1314. std::vector<std::string> parameters;
  1315. parameters.push_back(result);
  1316. return node->Evaluate(parameters, context, this, dagChecker);
  1317. }
  1318. return result;
  1319. }
  1320. //----------------------------------------------------------------------------
  1321. std::string GeneratorExpressionContent::Evaluate(
  1322. cmGeneratorExpressionContext *context,
  1323. cmGeneratorExpressionDAGChecker *dagChecker) const
  1324. {
  1325. std::string identifier;
  1326. {
  1327. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1328. = this->IdentifierChildren.begin();
  1329. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1330. = this->IdentifierChildren.end();
  1331. for ( ; it != end; ++it)
  1332. {
  1333. identifier += (*it)->Evaluate(context, dagChecker);
  1334. if (context->HadError)
  1335. {
  1336. return std::string();
  1337. }
  1338. }
  1339. }
  1340. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1341. if (!node)
  1342. {
  1343. reportError(context, this->GetOriginalExpression(),
  1344. "Expression did not evaluate to a known generator expression");
  1345. return std::string();
  1346. }
  1347. if (!node->GeneratesContent())
  1348. {
  1349. if (node->NumExpectedParameters() == 1
  1350. && node->AcceptsArbitraryContentParameter())
  1351. {
  1352. if (this->ParamChildren.empty())
  1353. {
  1354. reportError(context, this->GetOriginalExpression(),
  1355. "$<" + identifier + "> expression requires a parameter.");
  1356. }
  1357. }
  1358. else
  1359. {
  1360. std::vector<std::string> parameters;
  1361. this->EvaluateParameters(node, identifier, context, dagChecker,
  1362. parameters);
  1363. }
  1364. return std::string();
  1365. }
  1366. if (node->NumExpectedParameters() == 1
  1367. && node->AcceptsArbitraryContentParameter())
  1368. {
  1369. return this->ProcessArbitraryContent(node, identifier, context,
  1370. dagChecker,
  1371. this->ParamChildren.begin());
  1372. }
  1373. std::vector<std::string> parameters;
  1374. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1375. if (context->HadError)
  1376. {
  1377. return std::string();
  1378. }
  1379. return node->Evaluate(parameters, context, this, dagChecker);
  1380. }
  1381. //----------------------------------------------------------------------------
  1382. std::string GeneratorExpressionContent::EvaluateParameters(
  1383. const cmGeneratorExpressionNode *node,
  1384. const std::string &identifier,
  1385. cmGeneratorExpressionContext *context,
  1386. cmGeneratorExpressionDAGChecker *dagChecker,
  1387. std::vector<std::string> &parameters) const
  1388. {
  1389. const int numExpected = node->NumExpectedParameters();
  1390. {
  1391. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1392. pit = this->ParamChildren.begin();
  1393. const
  1394. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1395. pend = this->ParamChildren.end();
  1396. const bool acceptsArbitraryContent
  1397. = node->AcceptsArbitraryContentParameter();
  1398. for ( ; pit != pend; ++pit)
  1399. {
  1400. std::string parameter;
  1401. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1402. pit->begin();
  1403. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1404. pit->end();
  1405. for ( ; it != end; ++it)
  1406. {
  1407. parameter += (*it)->Evaluate(context, dagChecker);
  1408. if (context->HadError)
  1409. {
  1410. return std::string();
  1411. }
  1412. }
  1413. parameters.push_back(parameter);
  1414. if (acceptsArbitraryContent
  1415. && parameters.size() == (unsigned int)numExpected - 1)
  1416. {
  1417. assert(pit != pend);
  1418. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1419. context,
  1420. dagChecker,
  1421. pit + 1);
  1422. parameters.push_back(lastParam);
  1423. return std::string();
  1424. }
  1425. }
  1426. }
  1427. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1428. && (unsigned int)numExpected != parameters.size()))
  1429. {
  1430. if (numExpected == 0)
  1431. {
  1432. reportError(context, this->GetOriginalExpression(),
  1433. "$<" + identifier + "> expression requires no parameters.");
  1434. }
  1435. else if (numExpected == 1)
  1436. {
  1437. reportError(context, this->GetOriginalExpression(),
  1438. "$<" + identifier + "> expression requires "
  1439. "exactly one parameter.");
  1440. }
  1441. else
  1442. {
  1443. cmOStringStream e;
  1444. e << "$<" + identifier + "> expression requires "
  1445. << numExpected
  1446. << " comma separated parameters, but got "
  1447. << parameters.size() << " instead.";
  1448. reportError(context, this->GetOriginalExpression(), e.str());
  1449. }
  1450. return std::string();
  1451. }
  1452. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1453. && parameters.empty())
  1454. {
  1455. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1456. + "> expression requires at least one parameter.");
  1457. }
  1458. return std::string();
  1459. }
  1460. //----------------------------------------------------------------------------
  1461. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1462. {
  1463. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1464. = c.begin();
  1465. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1466. = c.end();
  1467. for ( ; it != end; ++it)
  1468. {
  1469. delete *it;
  1470. }
  1471. }
  1472. //----------------------------------------------------------------------------
  1473. GeneratorExpressionContent::~GeneratorExpressionContent()
  1474. {
  1475. deleteAll(this->IdentifierChildren);
  1476. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1477. std::vector<EvaluatorVector>::const_iterator pit =
  1478. this->ParamChildren.begin();
  1479. const std::vector<EvaluatorVector>::const_iterator pend =
  1480. this->ParamChildren.end();
  1481. for ( ; pit != pend; ++pit)
  1482. {
  1483. deleteAll(*pit);
  1484. }
  1485. }