cmGeneratorExpressionEvaluator.cxx 71 KB

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