cmGeneratorExpressionEvaluator.cxx 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. #undef TRANSITIVE_PROPERTY_NAME
  663. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  664. cmTarget const* target,
  665. cmTarget const* headTarget,
  666. cmGeneratorExpressionContext *context,
  667. cmGeneratorExpressionDAGChecker *dagChecker,
  668. const std::string &interfacePropertyName)
  669. {
  670. cmGeneratorExpression ge(context->Backtrace);
  671. std::string sep;
  672. std::string depString;
  673. for (std::vector<std::string>::const_iterator
  674. it = libraries.begin();
  675. it != libraries.end(); ++it)
  676. {
  677. if (*it == target->GetName())
  678. {
  679. // Broken code can have a target in its own link interface.
  680. // Don't follow such link interface entries so as not to create a
  681. // self-referencing loop.
  682. continue;
  683. }
  684. if (context->Makefile->FindTargetToUse(it->c_str()))
  685. {
  686. depString +=
  687. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  688. sep = ";";
  689. }
  690. }
  691. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  692. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  693. context->Config,
  694. context->Quiet,
  695. headTarget,
  696. target,
  697. dagChecker);
  698. if (cge->GetHadContextSensitiveCondition())
  699. {
  700. context->HadContextSensitiveCondition = true;
  701. }
  702. return linkedTargetsContent;
  703. }
  704. //----------------------------------------------------------------------------
  705. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  706. {
  707. TargetPropertyNode() {}
  708. // This node handles errors on parameter count itself.
  709. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  710. std::string Evaluate(const std::vector<std::string> &parameters,
  711. cmGeneratorExpressionContext *context,
  712. const GeneratorExpressionContent *content,
  713. cmGeneratorExpressionDAGChecker *dagCheckerParent
  714. ) const
  715. {
  716. if (parameters.size() != 1 && parameters.size() != 2)
  717. {
  718. reportError(context, content->GetOriginalExpression(),
  719. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  720. return std::string();
  721. }
  722. cmsys::RegularExpression propertyNameValidator;
  723. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  724. cmTarget const* target = context->HeadTarget;
  725. std::string propertyName = *parameters.begin();
  726. if (!target && parameters.size() == 1)
  727. {
  728. reportError(context, content->GetOriginalExpression(),
  729. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  730. "be used with add_custom_command. Specify the target to read a "
  731. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  732. "instead.");
  733. return std::string();
  734. }
  735. if (parameters.size() == 2)
  736. {
  737. if (parameters.begin()->empty() && parameters[1].empty())
  738. {
  739. reportError(context, content->GetOriginalExpression(),
  740. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  741. "target name and property name.");
  742. return std::string();
  743. }
  744. if (parameters.begin()->empty())
  745. {
  746. reportError(context, content->GetOriginalExpression(),
  747. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  748. "target name.");
  749. return std::string();
  750. }
  751. std::string targetName = parameters.front();
  752. propertyName = parameters[1];
  753. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  754. {
  755. if (!propertyNameValidator.find(propertyName.c_str()))
  756. {
  757. ::reportError(context, content->GetOriginalExpression(),
  758. "Target name and property name not supported.");
  759. return std::string();
  760. }
  761. ::reportError(context, content->GetOriginalExpression(),
  762. "Target name not supported.");
  763. return std::string();
  764. }
  765. if(propertyName == "ALIASED_TARGET")
  766. {
  767. if(context->Makefile->IsAlias(targetName.c_str()))
  768. {
  769. if(cmTarget* tgt =
  770. context->Makefile->FindTargetToUse(targetName.c_str()))
  771. {
  772. return tgt->GetName();
  773. }
  774. }
  775. return "";
  776. }
  777. target = context->Makefile->FindTargetToUse(
  778. targetName.c_str());
  779. if (!target)
  780. {
  781. cmOStringStream e;
  782. e << "Target \""
  783. << targetName
  784. << "\" not found.";
  785. reportError(context, content->GetOriginalExpression(), e.str());
  786. return std::string();
  787. }
  788. context->AllTargets.insert(target);
  789. }
  790. if (target == context->HeadTarget)
  791. {
  792. // Keep track of the properties seen while processing.
  793. // The evaluation of the LINK_LIBRARIES generator expressions
  794. // will check this to ensure that properties have one consistent
  795. // value for all evaluations.
  796. context->SeenTargetProperties.insert(propertyName);
  797. }
  798. if (propertyName.empty())
  799. {
  800. reportError(context, content->GetOriginalExpression(),
  801. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  802. "name.");
  803. return std::string();
  804. }
  805. if (!propertyNameValidator.find(propertyName.c_str()))
  806. {
  807. ::reportError(context, content->GetOriginalExpression(),
  808. "Property name not supported.");
  809. return std::string();
  810. }
  811. assert(target);
  812. if (propertyName == "LINKER_LANGUAGE")
  813. {
  814. if (target->LinkLanguagePropagatesToDependents() &&
  815. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries())
  816. {
  817. reportError(context, content->GetOriginalExpression(),
  818. "LINKER_LANGUAGE target property can not be used while evaluating "
  819. "link libraries for a static library");
  820. return std::string();
  821. }
  822. const char *lang = target->GetLinkerLanguage(context->Config);
  823. return lang ? lang : "";
  824. }
  825. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  826. target->GetName(),
  827. propertyName,
  828. content,
  829. dagCheckerParent);
  830. switch (dagChecker.check())
  831. {
  832. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  833. dagChecker.reportError(context, content->GetOriginalExpression());
  834. return std::string();
  835. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  836. // No error. We just skip cyclic references.
  837. return std::string();
  838. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  839. for (size_t i = 1;
  840. i < cmArraySize(targetPropertyTransitiveWhitelist);
  841. ++i)
  842. {
  843. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  844. {
  845. // No error. We're not going to find anything new here.
  846. return std::string();
  847. }
  848. }
  849. case cmGeneratorExpressionDAGChecker::DAG:
  850. break;
  851. }
  852. const char *prop = target->GetProperty(propertyName.c_str());
  853. if (dagCheckerParent)
  854. {
  855. if (dagCheckerParent->EvaluatingLinkLibraries())
  856. {
  857. if(!prop)
  858. {
  859. return std::string();
  860. }
  861. }
  862. else
  863. {
  864. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  865. dagCheckerParent->METHOD () ||
  866. assert(
  867. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  868. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  869. false);
  870. }
  871. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  872. }
  873. std::string linkedTargetsContent;
  874. std::string interfacePropertyName;
  875. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  876. if (propertyName == #prop || propertyName == "INTERFACE_" #prop) \
  877. { \
  878. interfacePropertyName = "INTERFACE_" #prop; \
  879. } \
  880. else
  881. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  882. // Note that the above macro terminates with an else
  883. /* else */ if (cmHasLiteralPrefix(propertyName.c_str(),
  884. "COMPILE_DEFINITIONS_"))
  885. {
  886. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  887. }
  888. #undef POPULATE_INTERFACE_PROPERTY_NAME
  889. cmTarget const* headTarget = context->HeadTarget
  890. ? context->HeadTarget : target;
  891. const char * const *transBegin =
  892. cmArrayBegin(targetPropertyTransitiveWhitelist) + 1;
  893. const char * const *transEnd =
  894. cmArrayEnd(targetPropertyTransitiveWhitelist);
  895. if (std::find_if(transBegin, transEnd,
  896. cmStrCmp(propertyName)) != transEnd)
  897. {
  898. std::vector<std::string> libs;
  899. target->GetTransitivePropertyLinkLibraries(context->Config,
  900. headTarget, libs);
  901. if (!libs.empty())
  902. {
  903. linkedTargetsContent =
  904. getLinkedTargetsContent(libs, target,
  905. headTarget,
  906. context, &dagChecker,
  907. interfacePropertyName);
  908. }
  909. }
  910. else if (std::find_if(transBegin, transEnd,
  911. cmStrCmp(interfacePropertyName)) != transEnd)
  912. {
  913. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  914. context->Config,
  915. headTarget);
  916. if(impl)
  917. {
  918. linkedTargetsContent =
  919. getLinkedTargetsContent(impl->Libraries, target,
  920. headTarget,
  921. context, &dagChecker,
  922. interfacePropertyName);
  923. }
  924. }
  925. linkedTargetsContent =
  926. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  927. if (!prop)
  928. {
  929. if (target->IsImported()
  930. || target->GetType() == cmTarget::INTERFACE_LIBRARY)
  931. {
  932. return linkedTargetsContent;
  933. }
  934. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  935. context->Config))
  936. {
  937. context->HadContextSensitiveCondition = true;
  938. return target->GetLinkInterfaceDependentBoolProperty(
  939. propertyName,
  940. context->Config) ? "1" : "0";
  941. }
  942. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  943. context->Config))
  944. {
  945. context->HadContextSensitiveCondition = true;
  946. const char *propContent =
  947. target->GetLinkInterfaceDependentStringProperty(
  948. propertyName,
  949. context->Config);
  950. return propContent ? propContent : "";
  951. }
  952. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  953. context->Config))
  954. {
  955. context->HadContextSensitiveCondition = true;
  956. const char *propContent =
  957. target->GetLinkInterfaceDependentNumberMinProperty(
  958. propertyName,
  959. context->Config);
  960. return propContent ? propContent : "";
  961. }
  962. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  963. context->Config))
  964. {
  965. context->HadContextSensitiveCondition = true;
  966. const char *propContent =
  967. target->GetLinkInterfaceDependentNumberMaxProperty(
  968. propertyName,
  969. context->Config);
  970. return propContent ? propContent : "";
  971. }
  972. return linkedTargetsContent;
  973. }
  974. if (!target->IsImported()
  975. && dagCheckerParent && !dagCheckerParent->EvaluatingLinkLibraries())
  976. {
  977. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  978. context->Config))
  979. {
  980. context->HadContextSensitiveCondition = true;
  981. const char *propContent =
  982. target->GetLinkInterfaceDependentNumberMinProperty(
  983. propertyName,
  984. context->Config);
  985. return propContent ? propContent : "";
  986. }
  987. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  988. context->Config))
  989. {
  990. context->HadContextSensitiveCondition = true;
  991. const char *propContent =
  992. target->GetLinkInterfaceDependentNumberMaxProperty(
  993. propertyName,
  994. context->Config);
  995. return propContent ? propContent : "";
  996. }
  997. }
  998. for (size_t i = 1;
  999. i < cmArraySize(targetPropertyTransitiveWhitelist);
  1000. ++i)
  1001. {
  1002. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  1003. {
  1004. cmGeneratorExpression ge(context->Backtrace);
  1005. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  1006. std::string result = cge->Evaluate(context->Makefile,
  1007. context->Config,
  1008. context->Quiet,
  1009. headTarget,
  1010. target,
  1011. &dagChecker);
  1012. if (cge->GetHadContextSensitiveCondition())
  1013. {
  1014. context->HadContextSensitiveCondition = true;
  1015. }
  1016. if (!linkedTargetsContent.empty())
  1017. {
  1018. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1019. }
  1020. return result;
  1021. }
  1022. }
  1023. return prop;
  1024. }
  1025. } targetPropertyNode;
  1026. //----------------------------------------------------------------------------
  1027. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1028. {
  1029. TargetNameNode() {}
  1030. virtual bool GeneratesContent() const { return true; }
  1031. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  1032. virtual bool RequiresLiteralInput() const { return true; }
  1033. std::string Evaluate(const std::vector<std::string> &parameters,
  1034. cmGeneratorExpressionContext *,
  1035. const GeneratorExpressionContent *,
  1036. cmGeneratorExpressionDAGChecker *) const
  1037. {
  1038. return parameters.front();
  1039. }
  1040. virtual int NumExpectedParameters() const { return 1; }
  1041. } targetNameNode;
  1042. //----------------------------------------------------------------------------
  1043. static const char* targetPolicyWhitelist[] = {
  1044. 0
  1045. #define TARGET_POLICY_STRING(POLICY) \
  1046. , #POLICY
  1047. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1048. #undef TARGET_POLICY_STRING
  1049. };
  1050. cmPolicies::PolicyStatus statusForTarget(cmTarget const* tgt,
  1051. const char *policy)
  1052. {
  1053. #define RETURN_POLICY(POLICY) \
  1054. if (strcmp(policy, #POLICY) == 0) \
  1055. { \
  1056. return tgt->GetPolicyStatus ## POLICY (); \
  1057. } \
  1058. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1059. #undef RETURN_POLICY
  1060. assert("!Unreachable code. Not a valid policy");
  1061. return cmPolicies::WARN;
  1062. }
  1063. cmPolicies::PolicyID policyForString(const char *policy_id)
  1064. {
  1065. #define RETURN_POLICY_ID(POLICY_ID) \
  1066. if (strcmp(policy_id, #POLICY_ID) == 0) \
  1067. { \
  1068. return cmPolicies:: POLICY_ID; \
  1069. } \
  1070. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1071. #undef RETURN_POLICY_ID
  1072. assert("!Unreachable code. Not a valid policy");
  1073. return cmPolicies::CMP0002;
  1074. }
  1075. //----------------------------------------------------------------------------
  1076. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1077. {
  1078. TargetPolicyNode() {}
  1079. virtual int NumExpectedParameters() const { return 1; }
  1080. std::string Evaluate(const std::vector<std::string> &parameters,
  1081. cmGeneratorExpressionContext *context ,
  1082. const GeneratorExpressionContent *content,
  1083. cmGeneratorExpressionDAGChecker *) const
  1084. {
  1085. if (!context->HeadTarget)
  1086. {
  1087. reportError(context, content->GetOriginalExpression(),
  1088. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  1089. "be used with add_custom_command.");
  1090. return std::string();
  1091. }
  1092. context->HadContextSensitiveCondition = true;
  1093. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i)
  1094. {
  1095. const char *policy = targetPolicyWhitelist[i];
  1096. if (parameters.front() == policy)
  1097. {
  1098. cmMakefile *mf = context->HeadTarget->GetMakefile();
  1099. switch(statusForTarget(context->HeadTarget, policy))
  1100. {
  1101. case cmPolicies::WARN:
  1102. mf->IssueMessage(cmake::AUTHOR_WARNING,
  1103. mf->GetPolicies()->
  1104. GetPolicyWarning(policyForString(policy)));
  1105. case cmPolicies::REQUIRED_IF_USED:
  1106. case cmPolicies::REQUIRED_ALWAYS:
  1107. case cmPolicies::OLD:
  1108. return "0";
  1109. case cmPolicies::NEW:
  1110. return "1";
  1111. }
  1112. }
  1113. }
  1114. reportError(context, content->GetOriginalExpression(),
  1115. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1116. "policies. Currently it may be used with the following policies:\n"
  1117. #define STRINGIFY_HELPER(X) #X
  1118. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1119. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  1120. " * " STRINGIFY(POLICY) "\n"
  1121. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1122. #undef TARGET_POLICY_LIST_ITEM
  1123. );
  1124. return std::string();
  1125. }
  1126. } targetPolicyNode;
  1127. //----------------------------------------------------------------------------
  1128. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1129. {
  1130. InstallPrefixNode() {}
  1131. virtual bool GeneratesContent() const { return true; }
  1132. virtual int NumExpectedParameters() const { return 0; }
  1133. std::string Evaluate(const std::vector<std::string> &,
  1134. cmGeneratorExpressionContext *context,
  1135. const GeneratorExpressionContent *content,
  1136. cmGeneratorExpressionDAGChecker *) const
  1137. {
  1138. reportError(context, content->GetOriginalExpression(),
  1139. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1140. "should never be evaluated.");
  1141. return std::string();
  1142. }
  1143. } installPrefixNode;
  1144. //----------------------------------------------------------------------------
  1145. template<bool linker, bool soname>
  1146. struct TargetFilesystemArtifactResultCreator
  1147. {
  1148. static std::string Create(cmTarget* target,
  1149. cmGeneratorExpressionContext *context,
  1150. const GeneratorExpressionContent *content);
  1151. };
  1152. //----------------------------------------------------------------------------
  1153. template<>
  1154. struct TargetFilesystemArtifactResultCreator<false, true>
  1155. {
  1156. static std::string Create(cmTarget* target,
  1157. cmGeneratorExpressionContext *context,
  1158. const GeneratorExpressionContent *content)
  1159. {
  1160. // The target soname file (.so.1).
  1161. if(target->IsDLLPlatform())
  1162. {
  1163. ::reportError(context, content->GetOriginalExpression(),
  1164. "TARGET_SONAME_FILE is not allowed "
  1165. "for DLL target platforms.");
  1166. return std::string();
  1167. }
  1168. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1169. {
  1170. ::reportError(context, content->GetOriginalExpression(),
  1171. "TARGET_SONAME_FILE is allowed only for "
  1172. "SHARED libraries.");
  1173. return std::string();
  1174. }
  1175. std::string result = target->GetDirectory(context->Config);
  1176. result += "/";
  1177. result += target->GetSOName(context->Config);
  1178. return result;
  1179. }
  1180. };
  1181. //----------------------------------------------------------------------------
  1182. template<>
  1183. struct TargetFilesystemArtifactResultCreator<true, false>
  1184. {
  1185. static std::string Create(cmTarget* target,
  1186. cmGeneratorExpressionContext *context,
  1187. const GeneratorExpressionContent *content)
  1188. {
  1189. // The file used to link to the target (.so, .lib, .a).
  1190. if(!target->IsLinkable())
  1191. {
  1192. ::reportError(context, content->GetOriginalExpression(),
  1193. "TARGET_LINKER_FILE is allowed only for libraries and "
  1194. "executables with ENABLE_EXPORTS.");
  1195. return std::string();
  1196. }
  1197. return target->GetFullPath(context->Config,
  1198. target->HasImportLibrary());
  1199. }
  1200. };
  1201. //----------------------------------------------------------------------------
  1202. template<>
  1203. struct TargetFilesystemArtifactResultCreator<false, false>
  1204. {
  1205. static std::string Create(cmTarget* target,
  1206. cmGeneratorExpressionContext *context,
  1207. const GeneratorExpressionContent *)
  1208. {
  1209. return target->GetFullPath(context->Config, false, true);
  1210. }
  1211. };
  1212. //----------------------------------------------------------------------------
  1213. template<bool dirQual, bool nameQual>
  1214. struct TargetFilesystemArtifactResultGetter
  1215. {
  1216. static std::string Get(const std::string &result);
  1217. };
  1218. //----------------------------------------------------------------------------
  1219. template<>
  1220. struct TargetFilesystemArtifactResultGetter<false, true>
  1221. {
  1222. static std::string Get(const std::string &result)
  1223. { return cmSystemTools::GetFilenameName(result); }
  1224. };
  1225. //----------------------------------------------------------------------------
  1226. template<>
  1227. struct TargetFilesystemArtifactResultGetter<true, false>
  1228. {
  1229. static std::string Get(const std::string &result)
  1230. { return cmSystemTools::GetFilenamePath(result); }
  1231. };
  1232. //----------------------------------------------------------------------------
  1233. template<>
  1234. struct TargetFilesystemArtifactResultGetter<false, false>
  1235. {
  1236. static std::string Get(const std::string &result)
  1237. { return result; }
  1238. };
  1239. //----------------------------------------------------------------------------
  1240. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1241. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1242. {
  1243. TargetFilesystemArtifact() {}
  1244. virtual int NumExpectedParameters() const { return 1; }
  1245. std::string Evaluate(const std::vector<std::string> &parameters,
  1246. cmGeneratorExpressionContext *context,
  1247. const GeneratorExpressionContent *content,
  1248. cmGeneratorExpressionDAGChecker *dagChecker) const
  1249. {
  1250. // Lookup the referenced target.
  1251. std::string name = *parameters.begin();
  1252. if (!cmGeneratorExpression::IsValidTargetName(name))
  1253. {
  1254. ::reportError(context, content->GetOriginalExpression(),
  1255. "Expression syntax not recognized.");
  1256. return std::string();
  1257. }
  1258. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1259. if(!target)
  1260. {
  1261. ::reportError(context, content->GetOriginalExpression(),
  1262. "No target \"" + name + "\"");
  1263. return std::string();
  1264. }
  1265. if(target->GetType() >= cmTarget::OBJECT_LIBRARY &&
  1266. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1267. {
  1268. ::reportError(context, content->GetOriginalExpression(),
  1269. "Target \"" + name + "\" is not an executable or library.");
  1270. return std::string();
  1271. }
  1272. if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
  1273. {
  1274. ::reportError(context, content->GetOriginalExpression(),
  1275. "Expressions which require the linker language may not "
  1276. "be used while evaluating link libraries");
  1277. return std::string();
  1278. }
  1279. context->DependTargets.insert(target);
  1280. context->AllTargets.insert(target);
  1281. std::string result =
  1282. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1283. target,
  1284. context,
  1285. content);
  1286. if (context->HadError)
  1287. {
  1288. return std::string();
  1289. }
  1290. return
  1291. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1292. }
  1293. };
  1294. //----------------------------------------------------------------------------
  1295. static const
  1296. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1297. static const
  1298. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1299. static const
  1300. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1301. static const
  1302. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1303. static const
  1304. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1305. static const
  1306. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1307. static const
  1308. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1309. static const
  1310. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1311. static const
  1312. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1313. //----------------------------------------------------------------------------
  1314. static const
  1315. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1316. {
  1317. if (identifier == "0")
  1318. return &zeroNode;
  1319. else if (identifier == "1")
  1320. return &oneNode;
  1321. else if (identifier == "AND")
  1322. return &andNode;
  1323. else if (identifier == "OR")
  1324. return &orNode;
  1325. else if (identifier == "NOT")
  1326. return &notNode;
  1327. else if (identifier == "C_COMPILER_ID")
  1328. return &cCompilerIdNode;
  1329. else if (identifier == "CXX_COMPILER_ID")
  1330. return &cxxCompilerIdNode;
  1331. else if (identifier == "VERSION_GREATER")
  1332. return &versionGreaterNode;
  1333. else if (identifier == "VERSION_LESS")
  1334. return &versionLessNode;
  1335. else if (identifier == "VERSION_EQUAL")
  1336. return &versionEqualNode;
  1337. else if (identifier == "C_COMPILER_VERSION")
  1338. return &cCompilerVersionNode;
  1339. else if (identifier == "CXX_COMPILER_VERSION")
  1340. return &cxxCompilerVersionNode;
  1341. else if (identifier == "PLATFORM_ID")
  1342. return &platformIdNode;
  1343. else if (identifier == "CONFIGURATION")
  1344. return &configurationNode;
  1345. else if (identifier == "CONFIG")
  1346. return &configurationTestNode;
  1347. else if (identifier == "TARGET_FILE")
  1348. return &targetFileNode;
  1349. else if (identifier == "TARGET_LINKER_FILE")
  1350. return &targetLinkerFileNode;
  1351. else if (identifier == "TARGET_SONAME_FILE")
  1352. return &targetSoNameFileNode;
  1353. else if (identifier == "TARGET_FILE_NAME")
  1354. return &targetFileNameNode;
  1355. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1356. return &targetLinkerFileNameNode;
  1357. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1358. return &targetSoNameFileNameNode;
  1359. else if (identifier == "TARGET_FILE_DIR")
  1360. return &targetFileDirNode;
  1361. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1362. return &targetLinkerFileDirNode;
  1363. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1364. return &targetSoNameFileDirNode;
  1365. else if (identifier == "STREQUAL")
  1366. return &strEqualNode;
  1367. else if (identifier == "EQUAL")
  1368. return &equalNode;
  1369. else if (identifier == "LOWER_CASE")
  1370. return &lowerCaseNode;
  1371. else if (identifier == "UPPER_CASE")
  1372. return &upperCaseNode;
  1373. else if (identifier == "MAKE_C_IDENTIFIER")
  1374. return &makeCIdentifierNode;
  1375. else if (identifier == "BOOL")
  1376. return &boolNode;
  1377. else if (identifier == "ANGLE-R")
  1378. return &angle_rNode;
  1379. else if (identifier == "COMMA")
  1380. return &commaNode;
  1381. else if (identifier == "SEMICOLON")
  1382. return &semicolonNode;
  1383. else if (identifier == "TARGET_PROPERTY")
  1384. return &targetPropertyNode;
  1385. else if (identifier == "TARGET_NAME")
  1386. return &targetNameNode;
  1387. else if (identifier == "TARGET_POLICY")
  1388. return &targetPolicyNode;
  1389. else if (identifier == "BUILD_INTERFACE")
  1390. return &buildInterfaceNode;
  1391. else if (identifier == "INSTALL_INTERFACE")
  1392. return &installInterfaceNode;
  1393. else if (identifier == "INSTALL_PREFIX")
  1394. return &installPrefixNode;
  1395. else if (identifier == "JOIN")
  1396. return &joinNode;
  1397. else if (identifier == "LINK_ONLY")
  1398. return &linkOnlyNode;
  1399. return 0;
  1400. }
  1401. //----------------------------------------------------------------------------
  1402. GeneratorExpressionContent::GeneratorExpressionContent(
  1403. const char *startContent,
  1404. size_t length)
  1405. : StartContent(startContent), ContentLength(length)
  1406. {
  1407. }
  1408. //----------------------------------------------------------------------------
  1409. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1410. {
  1411. return std::string(this->StartContent, this->ContentLength);
  1412. }
  1413. //----------------------------------------------------------------------------
  1414. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1415. const cmGeneratorExpressionNode *node,
  1416. const std::string &identifier,
  1417. cmGeneratorExpressionContext *context,
  1418. cmGeneratorExpressionDAGChecker *dagChecker,
  1419. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1420. pit) const
  1421. {
  1422. std::string result;
  1423. const
  1424. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1425. pend = this->ParamChildren.end();
  1426. for ( ; pit != pend; ++pit)
  1427. {
  1428. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1429. = pit->begin();
  1430. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1431. = pit->end();
  1432. for ( ; it != end; ++it)
  1433. {
  1434. if (node->RequiresLiteralInput())
  1435. {
  1436. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1437. {
  1438. reportError(context, this->GetOriginalExpression(),
  1439. "$<" + identifier + "> expression requires literal input.");
  1440. return std::string();
  1441. }
  1442. }
  1443. result += (*it)->Evaluate(context, dagChecker);
  1444. if (context->HadError)
  1445. {
  1446. return std::string();
  1447. }
  1448. }
  1449. if ((pit + 1) != pend)
  1450. {
  1451. result += ",";
  1452. }
  1453. }
  1454. if (node->RequiresLiteralInput())
  1455. {
  1456. std::vector<std::string> parameters;
  1457. parameters.push_back(result);
  1458. return node->Evaluate(parameters, context, this, dagChecker);
  1459. }
  1460. return result;
  1461. }
  1462. //----------------------------------------------------------------------------
  1463. std::string GeneratorExpressionContent::Evaluate(
  1464. cmGeneratorExpressionContext *context,
  1465. cmGeneratorExpressionDAGChecker *dagChecker) const
  1466. {
  1467. std::string identifier;
  1468. {
  1469. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1470. = this->IdentifierChildren.begin();
  1471. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1472. = this->IdentifierChildren.end();
  1473. for ( ; it != end; ++it)
  1474. {
  1475. identifier += (*it)->Evaluate(context, dagChecker);
  1476. if (context->HadError)
  1477. {
  1478. return std::string();
  1479. }
  1480. }
  1481. }
  1482. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1483. if (!node)
  1484. {
  1485. reportError(context, this->GetOriginalExpression(),
  1486. "Expression did not evaluate to a known generator expression");
  1487. return std::string();
  1488. }
  1489. if (!node->GeneratesContent())
  1490. {
  1491. if (node->NumExpectedParameters() == 1
  1492. && node->AcceptsArbitraryContentParameter())
  1493. {
  1494. if (this->ParamChildren.empty())
  1495. {
  1496. reportError(context, this->GetOriginalExpression(),
  1497. "$<" + identifier + "> expression requires a parameter.");
  1498. }
  1499. }
  1500. else
  1501. {
  1502. std::vector<std::string> parameters;
  1503. this->EvaluateParameters(node, identifier, context, dagChecker,
  1504. parameters);
  1505. }
  1506. return std::string();
  1507. }
  1508. std::vector<std::string> parameters;
  1509. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1510. if (context->HadError)
  1511. {
  1512. return std::string();
  1513. }
  1514. return node->Evaluate(parameters, context, this, dagChecker);
  1515. }
  1516. //----------------------------------------------------------------------------
  1517. std::string GeneratorExpressionContent::EvaluateParameters(
  1518. const cmGeneratorExpressionNode *node,
  1519. const std::string &identifier,
  1520. cmGeneratorExpressionContext *context,
  1521. cmGeneratorExpressionDAGChecker *dagChecker,
  1522. std::vector<std::string> &parameters) const
  1523. {
  1524. const int numExpected = node->NumExpectedParameters();
  1525. {
  1526. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1527. pit = this->ParamChildren.begin();
  1528. const
  1529. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1530. pend = this->ParamChildren.end();
  1531. const bool acceptsArbitraryContent
  1532. = node->AcceptsArbitraryContentParameter();
  1533. int counter = 1;
  1534. for ( ; pit != pend; ++pit, ++counter)
  1535. {
  1536. if (acceptsArbitraryContent && counter == numExpected)
  1537. {
  1538. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1539. context,
  1540. dagChecker,
  1541. pit);
  1542. parameters.push_back(lastParam);
  1543. return std::string();
  1544. }
  1545. else
  1546. {
  1547. std::string parameter;
  1548. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1549. pit->begin();
  1550. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1551. pit->end();
  1552. for ( ; it != end; ++it)
  1553. {
  1554. parameter += (*it)->Evaluate(context, dagChecker);
  1555. if (context->HadError)
  1556. {
  1557. return std::string();
  1558. }
  1559. }
  1560. parameters.push_back(parameter);
  1561. }
  1562. }
  1563. }
  1564. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1565. && (unsigned int)numExpected != parameters.size()))
  1566. {
  1567. if (numExpected == 0)
  1568. {
  1569. reportError(context, this->GetOriginalExpression(),
  1570. "$<" + identifier + "> expression requires no parameters.");
  1571. }
  1572. else if (numExpected == 1)
  1573. {
  1574. reportError(context, this->GetOriginalExpression(),
  1575. "$<" + identifier + "> expression requires "
  1576. "exactly one parameter.");
  1577. }
  1578. else
  1579. {
  1580. cmOStringStream e;
  1581. e << "$<" + identifier + "> expression requires "
  1582. << numExpected
  1583. << " comma separated parameters, but got "
  1584. << parameters.size() << " instead.";
  1585. reportError(context, this->GetOriginalExpression(), e.str());
  1586. }
  1587. return std::string();
  1588. }
  1589. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1590. && parameters.empty())
  1591. {
  1592. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1593. + "> expression requires at least one parameter.");
  1594. }
  1595. if (numExpected == cmGeneratorExpressionNode::OneOrZeroParameters
  1596. && parameters.size() > 2)
  1597. {
  1598. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1599. + "> expression requires one or zero parameters.");
  1600. }
  1601. return std::string();
  1602. }
  1603. //----------------------------------------------------------------------------
  1604. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1605. {
  1606. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1607. = c.begin();
  1608. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1609. = c.end();
  1610. for ( ; it != end; ++it)
  1611. {
  1612. delete *it;
  1613. }
  1614. }
  1615. //----------------------------------------------------------------------------
  1616. GeneratorExpressionContent::~GeneratorExpressionContent()
  1617. {
  1618. deleteAll(this->IdentifierChildren);
  1619. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1620. std::vector<EvaluatorVector>::const_iterator pit =
  1621. this->ParamChildren.begin();
  1622. const std::vector<EvaluatorVector>::const_iterator pend =
  1623. this->ParamChildren.end();
  1624. for ( ; pit != pend; ++pit)
  1625. {
  1626. deleteAll(*pit);
  1627. }
  1628. }