cmGeneratorExpressionEvaluator.cxx 64 KB

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