cmGeneratorExpressionEvaluator.cxx 62 KB

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