cmGeneratorExpressionEvaluator.cxx 68 KB

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