cmGeneratorExpressionNode.cxx 71 KB

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