cmGeneratorExpressionEvaluator.cxx 69 KB

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