cmGeneratorExpressionNode.cxx 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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. const char* compilerId = context->LG->GetMakefile()->GetSafeDefinition(
  494. "CMAKE_" + lang + "_COMPILER_ID");
  495. if (parameters.empty()) {
  496. return compilerId ? compilerId : "";
  497. }
  498. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  499. if (!compilerIdValidator.find(*parameters.begin())) {
  500. reportError(context, content->GetOriginalExpression(),
  501. "Expression syntax not recognized.");
  502. return std::string();
  503. }
  504. if (!compilerId) {
  505. return parameters.front().empty() ? "1" : "0";
  506. }
  507. if (strcmp(parameters.begin()->c_str(), compilerId) == 0) {
  508. return "1";
  509. }
  510. if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0) {
  511. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  512. case cmPolicies::WARN: {
  513. std::ostringstream e;
  514. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
  515. context->LG->GetCMakeInstance()->IssueMessage(
  516. cmake::AUTHOR_WARNING, e.str(), context->Backtrace);
  517. CM_FALLTHROUGH;
  518. }
  519. case cmPolicies::OLD:
  520. return "1";
  521. case cmPolicies::NEW:
  522. case cmPolicies::REQUIRED_ALWAYS:
  523. case cmPolicies::REQUIRED_IF_USED:
  524. break;
  525. }
  526. }
  527. return "0";
  528. }
  529. };
  530. static const struct CCompilerIdNode : public CompilerIdNode
  531. {
  532. CCompilerIdNode() {}
  533. std::string Evaluate(
  534. const std::vector<std::string>& parameters,
  535. cmGeneratorExpressionContext* context,
  536. const GeneratorExpressionContent* content,
  537. cmGeneratorExpressionDAGChecker* dagChecker) const override
  538. {
  539. if (!context->HeadTarget) {
  540. reportError(
  541. context, content->GetOriginalExpression(),
  542. "$<C_COMPILER_ID> may only be used with binary targets. It may "
  543. "not be used with add_custom_command or add_custom_target.");
  544. return std::string();
  545. }
  546. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  547. "C");
  548. }
  549. } cCompilerIdNode;
  550. static const struct CXXCompilerIdNode : public CompilerIdNode
  551. {
  552. CXXCompilerIdNode() {}
  553. std::string Evaluate(
  554. const std::vector<std::string>& parameters,
  555. cmGeneratorExpressionContext* context,
  556. const GeneratorExpressionContent* content,
  557. cmGeneratorExpressionDAGChecker* dagChecker) const override
  558. {
  559. if (!context->HeadTarget) {
  560. reportError(
  561. context, content->GetOriginalExpression(),
  562. "$<CXX_COMPILER_ID> may only be used with binary targets. It may "
  563. "not be used with add_custom_command or add_custom_target.");
  564. return std::string();
  565. }
  566. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  567. "CXX");
  568. }
  569. } cxxCompilerIdNode;
  570. struct CompilerVersionNode : public cmGeneratorExpressionNode
  571. {
  572. CompilerVersionNode() {}
  573. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  574. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  575. cmGeneratorExpressionContext* context,
  576. const GeneratorExpressionContent* content,
  577. cmGeneratorExpressionDAGChecker* /*unused*/,
  578. const std::string& lang) const
  579. {
  580. const char* compilerVersion =
  581. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  582. "_COMPILER_VERSION");
  583. if (parameters.empty()) {
  584. return compilerVersion ? compilerVersion : "";
  585. }
  586. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  587. if (!compilerIdValidator.find(*parameters.begin())) {
  588. reportError(context, content->GetOriginalExpression(),
  589. "Expression syntax not recognized.");
  590. return std::string();
  591. }
  592. if (!compilerVersion) {
  593. return parameters.front().empty() ? "1" : "0";
  594. }
  595. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  596. parameters.begin()->c_str(),
  597. compilerVersion)
  598. ? "1"
  599. : "0";
  600. }
  601. };
  602. static const struct CCompilerVersionNode : public CompilerVersionNode
  603. {
  604. CCompilerVersionNode() {}
  605. std::string Evaluate(
  606. const std::vector<std::string>& parameters,
  607. cmGeneratorExpressionContext* context,
  608. const GeneratorExpressionContent* content,
  609. cmGeneratorExpressionDAGChecker* dagChecker) const override
  610. {
  611. if (!context->HeadTarget) {
  612. reportError(
  613. context, content->GetOriginalExpression(),
  614. "$<C_COMPILER_VERSION> may only be used with binary targets. It "
  615. "may not be used with add_custom_command or add_custom_target.");
  616. return std::string();
  617. }
  618. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  619. "C");
  620. }
  621. } cCompilerVersionNode;
  622. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  623. {
  624. CxxCompilerVersionNode() {}
  625. std::string Evaluate(
  626. const std::vector<std::string>& parameters,
  627. cmGeneratorExpressionContext* context,
  628. const GeneratorExpressionContent* content,
  629. cmGeneratorExpressionDAGChecker* dagChecker) const override
  630. {
  631. if (!context->HeadTarget) {
  632. reportError(
  633. context, content->GetOriginalExpression(),
  634. "$<CXX_COMPILER_VERSION> may only be used with binary targets. It "
  635. "may not be used with add_custom_command or add_custom_target.");
  636. return std::string();
  637. }
  638. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  639. "CXX");
  640. }
  641. } cxxCompilerVersionNode;
  642. struct PlatformIdNode : public cmGeneratorExpressionNode
  643. {
  644. PlatformIdNode() {}
  645. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  646. std::string Evaluate(
  647. const std::vector<std::string>& parameters,
  648. cmGeneratorExpressionContext* context,
  649. const GeneratorExpressionContent* /*content*/,
  650. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  651. {
  652. const char* platformId =
  653. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  654. if (parameters.empty()) {
  655. return platformId ? platformId : "";
  656. }
  657. if (!platformId) {
  658. return parameters.front().empty() ? "1" : "0";
  659. }
  660. if (strcmp(parameters.begin()->c_str(), platformId) == 0) {
  661. return "1";
  662. }
  663. return "0";
  664. }
  665. } platformIdNode;
  666. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  667. {
  668. VersionGreaterNode() {}
  669. int NumExpectedParameters() const override { return 2; }
  670. std::string Evaluate(
  671. const std::vector<std::string>& parameters,
  672. cmGeneratorExpressionContext* /*context*/,
  673. const GeneratorExpressionContent* /*content*/,
  674. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  675. {
  676. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  677. parameters.front().c_str(),
  678. parameters[1].c_str())
  679. ? "1"
  680. : "0";
  681. }
  682. } versionGreaterNode;
  683. static const struct VersionGreaterEqNode : public cmGeneratorExpressionNode
  684. {
  685. VersionGreaterEqNode() {}
  686. int NumExpectedParameters() const override { return 2; }
  687. std::string Evaluate(
  688. const std::vector<std::string>& parameters,
  689. cmGeneratorExpressionContext* /*context*/,
  690. const GeneratorExpressionContent* /*content*/,
  691. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  692. {
  693. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  694. parameters.front().c_str(),
  695. parameters[1].c_str())
  696. ? "1"
  697. : "0";
  698. }
  699. } versionGreaterEqNode;
  700. static const struct VersionLessNode : public cmGeneratorExpressionNode
  701. {
  702. VersionLessNode() {}
  703. int NumExpectedParameters() const override { return 2; }
  704. std::string Evaluate(
  705. const std::vector<std::string>& parameters,
  706. cmGeneratorExpressionContext* /*context*/,
  707. const GeneratorExpressionContent* /*content*/,
  708. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  709. {
  710. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  711. parameters.front().c_str(),
  712. parameters[1].c_str())
  713. ? "1"
  714. : "0";
  715. }
  716. } versionLessNode;
  717. static const struct VersionLessEqNode : public cmGeneratorExpressionNode
  718. {
  719. VersionLessEqNode() {}
  720. int NumExpectedParameters() const override { return 2; }
  721. std::string Evaluate(
  722. const std::vector<std::string>& parameters,
  723. cmGeneratorExpressionContext* /*context*/,
  724. const GeneratorExpressionContent* /*content*/,
  725. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  726. {
  727. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS_EQUAL,
  728. parameters.front().c_str(),
  729. parameters[1].c_str())
  730. ? "1"
  731. : "0";
  732. }
  733. } versionLessEqNode;
  734. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  735. {
  736. VersionEqualNode() {}
  737. int NumExpectedParameters() const override { return 2; }
  738. std::string Evaluate(
  739. const std::vector<std::string>& parameters,
  740. cmGeneratorExpressionContext* /*context*/,
  741. const GeneratorExpressionContent* /*content*/,
  742. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  743. {
  744. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  745. parameters.front().c_str(),
  746. parameters[1].c_str())
  747. ? "1"
  748. : "0";
  749. }
  750. } versionEqualNode;
  751. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  752. {
  753. LinkOnlyNode() {}
  754. std::string Evaluate(
  755. const std::vector<std::string>& parameters,
  756. cmGeneratorExpressionContext* context,
  757. const GeneratorExpressionContent* content,
  758. cmGeneratorExpressionDAGChecker* dagChecker) const override
  759. {
  760. if (!dagChecker) {
  761. reportError(context, content->GetOriginalExpression(),
  762. "$<LINK_ONLY:...> may only be used for linking");
  763. return std::string();
  764. }
  765. if (!dagChecker->GetTransitivePropertiesOnly()) {
  766. return parameters.front();
  767. }
  768. return std::string();
  769. }
  770. } linkOnlyNode;
  771. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  772. {
  773. ConfigurationNode() {}
  774. int NumExpectedParameters() const override { return 0; }
  775. std::string Evaluate(
  776. const std::vector<std::string>& /*parameters*/,
  777. cmGeneratorExpressionContext* context,
  778. const GeneratorExpressionContent* /*content*/,
  779. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  780. {
  781. context->HadContextSensitiveCondition = true;
  782. return context->Config;
  783. }
  784. } configurationNode;
  785. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  786. {
  787. ConfigurationTestNode() {}
  788. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  789. std::string Evaluate(
  790. const std::vector<std::string>& parameters,
  791. cmGeneratorExpressionContext* context,
  792. const GeneratorExpressionContent* content,
  793. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  794. {
  795. if (parameters.empty()) {
  796. return configurationNode.Evaluate(parameters, context, content, nullptr);
  797. }
  798. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  799. if (!configValidator.find(*parameters.begin())) {
  800. reportError(context, content->GetOriginalExpression(),
  801. "Expression syntax not recognized.");
  802. return std::string();
  803. }
  804. context->HadContextSensitiveCondition = true;
  805. if (context->Config.empty()) {
  806. return parameters.front().empty() ? "1" : "0";
  807. }
  808. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  809. context->Config.c_str()) == 0) {
  810. return "1";
  811. }
  812. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  813. const char* loc = nullptr;
  814. const char* imp = nullptr;
  815. std::string suffix;
  816. if (context->CurrentTarget->Target->GetMappedConfig(
  817. context->Config, &loc, &imp, suffix)) {
  818. // This imported target has an appropriate location
  819. // for this (possibly mapped) config.
  820. // Check if there is a proper config mapping for the tested config.
  821. std::vector<std::string> mappedConfigs;
  822. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  823. mapProp += cmSystemTools::UpperCase(context->Config);
  824. if (const char* mapValue =
  825. context->CurrentTarget->GetProperty(mapProp)) {
  826. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  827. mappedConfigs);
  828. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  829. cmSystemTools::UpperCase(parameters.front())) !=
  830. mappedConfigs.end()
  831. ? "1"
  832. : "0";
  833. }
  834. }
  835. }
  836. return "0";
  837. }
  838. } configurationTestNode;
  839. static const struct JoinNode : public cmGeneratorExpressionNode
  840. {
  841. JoinNode() {}
  842. int NumExpectedParameters() const override { return 2; }
  843. bool AcceptsArbitraryContentParameter() const override { return true; }
  844. std::string Evaluate(
  845. const std::vector<std::string>& parameters,
  846. cmGeneratorExpressionContext* /*context*/,
  847. const GeneratorExpressionContent* /*content*/,
  848. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  849. {
  850. std::vector<std::string> list;
  851. cmSystemTools::ExpandListArgument(parameters.front(), list);
  852. return cmJoin(list, parameters[1]);
  853. }
  854. } joinNode;
  855. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  856. {
  857. CompileLanguageNode() {}
  858. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  859. std::string Evaluate(
  860. const std::vector<std::string>& parameters,
  861. cmGeneratorExpressionContext* context,
  862. const GeneratorExpressionContent* content,
  863. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  864. {
  865. if (context->Language.empty()) {
  866. reportError(
  867. context, content->GetOriginalExpression(),
  868. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  869. "directories, compile definitions, compile options, and to evaluate "
  870. "components of the file(GENERATE) command.");
  871. return std::string();
  872. }
  873. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  874. std::string genName = gg->GetName();
  875. if (genName.find("Makefiles") == std::string::npos &&
  876. genName.find("Ninja") == std::string::npos &&
  877. genName.find("Visual Studio") == std::string::npos &&
  878. genName.find("Xcode") == std::string::npos &&
  879. genName.find("Watcom WMake") == std::string::npos) {
  880. reportError(context, content->GetOriginalExpression(),
  881. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  882. return std::string();
  883. }
  884. if (parameters.empty()) {
  885. return context->Language;
  886. }
  887. return context->Language == parameters.front() ? "1" : "0";
  888. }
  889. } languageNode;
  890. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) , "INTERFACE_" #PROPERTY
  891. static const char* targetPropertyTransitiveWhitelist[] = {
  892. nullptr CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  893. };
  894. #undef TRANSITIVE_PROPERTY_NAME
  895. template <typename T>
  896. std::string getLinkedTargetsContent(
  897. std::vector<T> const& libraries, cmGeneratorTarget const* target,
  898. cmGeneratorTarget const* headTarget, cmGeneratorExpressionContext* context,
  899. cmGeneratorExpressionDAGChecker* dagChecker,
  900. const std::string& interfacePropertyName)
  901. {
  902. std::string linkedTargetsContent;
  903. std::string sep;
  904. std::string depString;
  905. for (T const& l : libraries) {
  906. // Broken code can have a target in its own link interface.
  907. // Don't follow such link interface entries so as not to create a
  908. // self-referencing loop.
  909. if (l.Target && l.Target != target) {
  910. std::string uniqueName =
  911. target->GetGlobalGenerator()->IndexGeneratorTargetUniquely(l.Target);
  912. depString += sep + "$<TARGET_PROPERTY:" + std::move(uniqueName) + "," +
  913. interfacePropertyName + ">";
  914. sep = ";";
  915. }
  916. }
  917. if (!depString.empty()) {
  918. linkedTargetsContent =
  919. cmGeneratorExpressionNode::EvaluateDependentExpression(
  920. depString, target->GetLocalGenerator(), context, headTarget, target,
  921. dagChecker);
  922. }
  923. linkedTargetsContent =
  924. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  925. return linkedTargetsContent;
  926. }
  927. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  928. {
  929. TargetPropertyNode() {}
  930. // This node handles errors on parameter count itself.
  931. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  932. std::string Evaluate(
  933. const std::vector<std::string>& parameters,
  934. cmGeneratorExpressionContext* context,
  935. const GeneratorExpressionContent* content,
  936. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  937. {
  938. if (parameters.size() != 1 && parameters.size() != 2) {
  939. reportError(
  940. context, content->GetOriginalExpression(),
  941. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  942. return std::string();
  943. }
  944. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  945. cmGeneratorTarget const* target = context->HeadTarget;
  946. std::string propertyName = *parameters.begin();
  947. if (parameters.size() == 1) {
  948. context->HadHeadSensitiveCondition = true;
  949. }
  950. if (!target && parameters.size() == 1) {
  951. reportError(
  952. context, content->GetOriginalExpression(),
  953. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  954. "It may not be used with add_custom_command or add_custom_target. "
  955. "Specify the target to read a property from using the "
  956. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  957. return std::string();
  958. }
  959. if (parameters.size() == 2) {
  960. if (parameters.begin()->empty() && parameters[1].empty()) {
  961. reportError(
  962. context, content->GetOriginalExpression(),
  963. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  964. "target name and property name.");
  965. return std::string();
  966. }
  967. if (parameters.begin()->empty()) {
  968. reportError(
  969. context, content->GetOriginalExpression(),
  970. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  971. "target name.");
  972. return std::string();
  973. }
  974. std::string targetName = parameters.front();
  975. propertyName = parameters[1];
  976. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  977. if (!propertyNameValidator.find(propertyName)) {
  978. ::reportError(context, content->GetOriginalExpression(),
  979. "Target name and property name not supported.");
  980. return std::string();
  981. }
  982. ::reportError(context, content->GetOriginalExpression(),
  983. "Target name not supported.");
  984. return std::string();
  985. }
  986. static const std::string propALIASED_TARGET = "ALIASED_TARGET";
  987. if (propertyName == propALIASED_TARGET) {
  988. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  989. if (cmGeneratorTarget* tgt =
  990. context->LG->FindGeneratorTargetToUse(targetName)) {
  991. return tgt->GetName();
  992. }
  993. }
  994. return "";
  995. }
  996. target = context->LG->FindGeneratorTargetToUse(targetName);
  997. if (!target) {
  998. std::ostringstream e;
  999. e << "Target \"" << targetName << "\" not found.";
  1000. reportError(context, content->GetOriginalExpression(), e.str());
  1001. return std::string();
  1002. }
  1003. context->AllTargets.insert(target);
  1004. }
  1005. if (target == context->HeadTarget) {
  1006. // Keep track of the properties seen while processing.
  1007. // The evaluation of the LINK_LIBRARIES generator expressions
  1008. // will check this to ensure that properties have one consistent
  1009. // value for all evaluations.
  1010. context->SeenTargetProperties.insert(propertyName);
  1011. }
  1012. if (propertyName == "SOURCES") {
  1013. context->SourceSensitiveTargets.insert(target);
  1014. }
  1015. if (propertyName.empty()) {
  1016. reportError(
  1017. context, content->GetOriginalExpression(),
  1018. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1019. "name.");
  1020. return std::string();
  1021. }
  1022. if (!propertyNameValidator.find(propertyName)) {
  1023. ::reportError(context, content->GetOriginalExpression(),
  1024. "Property name not supported.");
  1025. return std::string();
  1026. }
  1027. assert(target);
  1028. if (propertyName == "LINKER_LANGUAGE") {
  1029. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1030. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1031. dagCheckerParent->EvaluatingSources())) {
  1032. reportError(
  1033. context, content->GetOriginalExpression(),
  1034. "LINKER_LANGUAGE target property can not be used while evaluating "
  1035. "link libraries for a static library");
  1036. return std::string();
  1037. }
  1038. return target->GetLinkerLanguage(context->Config);
  1039. }
  1040. cmGeneratorExpressionDAGChecker dagChecker(
  1041. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1042. switch (dagChecker.Check()) {
  1043. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1044. dagChecker.ReportError(context, content->GetOriginalExpression());
  1045. return std::string();
  1046. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1047. // No error. We just skip cyclic references.
  1048. return std::string();
  1049. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1050. for (size_t i = 1; i < cm::size(targetPropertyTransitiveWhitelist);
  1051. ++i) {
  1052. if (targetPropertyTransitiveWhitelist[i] == propertyName) {
  1053. // No error. We're not going to find anything new here.
  1054. return std::string();
  1055. }
  1056. }
  1057. case cmGeneratorExpressionDAGChecker::DAG:
  1058. break;
  1059. }
  1060. const char* prop = target->GetProperty(propertyName);
  1061. if (dagCheckerParent) {
  1062. if (dagCheckerParent->EvaluatingGenexExpression()) {
  1063. // No check required.
  1064. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1065. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  1066. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  1067. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(
  1068. TRANSITIVE_PROPERTY_COMPARE) false) { // NOLINT(clang-tidy)
  1069. reportError(
  1070. context, content->GetOriginalExpression(),
  1071. "$<TARGET_PROPERTY:...> expression in link libraries "
  1072. "evaluation depends on target property which is transitive "
  1073. "over the link libraries, creating a recursion.");
  1074. return std::string();
  1075. }
  1076. #undef TRANSITIVE_PROPERTY_COMPARE
  1077. if (!prop) {
  1078. return std::string();
  1079. }
  1080. } else {
  1081. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1082. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1083. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1084. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1085. }
  1086. }
  1087. std::string linkedTargetsContent;
  1088. std::string interfacePropertyName;
  1089. bool isInterfaceProperty = false;
  1090. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1091. if (propertyName == #prop) { \
  1092. interfacePropertyName = "INTERFACE_" #prop; \
  1093. } else if (propertyName == "INTERFACE_" #prop) { \
  1094. interfacePropertyName = "INTERFACE_" #prop; \
  1095. isInterfaceProperty = true; \
  1096. } else
  1097. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1098. // Note that the above macro terminates with an else
  1099. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1100. cmPolicies::PolicyStatus polSt =
  1101. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1102. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1103. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1104. }
  1105. }
  1106. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1107. cmGeneratorTarget const* headTarget =
  1108. context->HeadTarget && isInterfaceProperty ? context->HeadTarget
  1109. : target;
  1110. if (isInterfaceProperty) {
  1111. if (cmLinkInterfaceLibraries const* iface =
  1112. target->GetLinkInterfaceLibraries(context->Config, headTarget,
  1113. true)) {
  1114. linkedTargetsContent =
  1115. getLinkedTargetsContent(iface->Libraries, target, headTarget,
  1116. context, &dagChecker, interfacePropertyName);
  1117. }
  1118. } else if (!interfacePropertyName.empty()) {
  1119. if (cmLinkImplementationLibraries const* impl =
  1120. target->GetLinkImplementationLibraries(context->Config)) {
  1121. linkedTargetsContent =
  1122. getLinkedTargetsContent(impl->Libraries, target, target, context,
  1123. &dagChecker, interfacePropertyName);
  1124. }
  1125. }
  1126. if (!prop) {
  1127. if (target->IsImported() ||
  1128. target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1129. return linkedTargetsContent;
  1130. }
  1131. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1132. context->Config)) {
  1133. context->HadContextSensitiveCondition = true;
  1134. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1135. context->Config)
  1136. ? "1"
  1137. : "0";
  1138. }
  1139. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1140. context->Config)) {
  1141. context->HadContextSensitiveCondition = true;
  1142. const char* propContent =
  1143. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1144. context->Config);
  1145. return propContent ? propContent : "";
  1146. }
  1147. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1148. context->Config)) {
  1149. context->HadContextSensitiveCondition = true;
  1150. const char* propContent =
  1151. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1152. context->Config);
  1153. return propContent ? propContent : "";
  1154. }
  1155. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1156. context->Config)) {
  1157. context->HadContextSensitiveCondition = true;
  1158. const char* propContent =
  1159. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1160. context->Config);
  1161. return propContent ? propContent : "";
  1162. }
  1163. return linkedTargetsContent;
  1164. }
  1165. if (!target->IsImported() && dagCheckerParent &&
  1166. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1167. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1168. context->Config)) {
  1169. context->HadContextSensitiveCondition = true;
  1170. const char* propContent =
  1171. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1172. context->Config);
  1173. return propContent ? propContent : "";
  1174. }
  1175. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1176. context->Config)) {
  1177. context->HadContextSensitiveCondition = true;
  1178. const char* propContent =
  1179. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1180. context->Config);
  1181. return propContent ? propContent : "";
  1182. }
  1183. }
  1184. if (!interfacePropertyName.empty()) {
  1185. std::string result = this->EvaluateDependentExpression(
  1186. prop, context->LG, context, headTarget, target, &dagChecker);
  1187. if (!linkedTargetsContent.empty()) {
  1188. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1189. }
  1190. return result;
  1191. }
  1192. return prop;
  1193. }
  1194. } targetPropertyNode;
  1195. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1196. {
  1197. TargetNameNode() {}
  1198. bool GeneratesContent() const override { return true; }
  1199. bool AcceptsArbitraryContentParameter() const override { return true; }
  1200. bool RequiresLiteralInput() const override { return true; }
  1201. std::string Evaluate(
  1202. const std::vector<std::string>& parameters,
  1203. cmGeneratorExpressionContext* /*context*/,
  1204. const GeneratorExpressionContent* /*content*/,
  1205. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1206. {
  1207. return parameters.front();
  1208. }
  1209. int NumExpectedParameters() const override { return 1; }
  1210. } targetNameNode;
  1211. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1212. {
  1213. TargetObjectsNode() {}
  1214. std::string Evaluate(
  1215. const std::vector<std::string>& parameters,
  1216. cmGeneratorExpressionContext* context,
  1217. const GeneratorExpressionContent* content,
  1218. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1219. {
  1220. std::string tgtName = parameters.front();
  1221. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1222. if (!gt) {
  1223. std::ostringstream e;
  1224. e << "Objects of target \"" << tgtName
  1225. << "\" referenced but no such target exists.";
  1226. reportError(context, content->GetOriginalExpression(), e.str());
  1227. return std::string();
  1228. }
  1229. if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1230. std::ostringstream e;
  1231. e << "Objects of target \"" << tgtName
  1232. << "\" referenced but is not an OBJECT library.";
  1233. reportError(context, content->GetOriginalExpression(), e.str());
  1234. return std::string();
  1235. }
  1236. if (!context->EvaluateForBuildsystem) {
  1237. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1238. std::string reason;
  1239. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1240. std::ostringstream e;
  1241. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1242. "is only suitable for consumption by CMake (limited"
  1243. << reason
  1244. << "). "
  1245. "It is not suitable for writing out elsewhere.";
  1246. reportError(context, content->GetOriginalExpression(), e.str());
  1247. return std::string();
  1248. }
  1249. }
  1250. std::vector<std::string> objects;
  1251. if (gt->IsImported()) {
  1252. const char* loc = nullptr;
  1253. const char* imp = nullptr;
  1254. std::string suffix;
  1255. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1256. cmSystemTools::ExpandListArgument(loc, objects);
  1257. }
  1258. context->HadContextSensitiveCondition = true;
  1259. } else {
  1260. gt->GetTargetObjectNames(context->Config, objects);
  1261. std::string obj_dir;
  1262. if (context->EvaluateForBuildsystem) {
  1263. // Use object file directory with buildsystem placeholder.
  1264. obj_dir = gt->ObjectDirectory;
  1265. // Here we assume that the set of object files produced
  1266. // by an object library does not vary with configuration
  1267. // and do not set HadContextSensitiveCondition to true.
  1268. } else {
  1269. // Use object file directory with per-config location.
  1270. obj_dir = gt->GetObjectDirectory(context->Config);
  1271. context->HadContextSensitiveCondition = true;
  1272. }
  1273. for (std::string& o : objects) {
  1274. o = obj_dir + o;
  1275. }
  1276. }
  1277. // Create the cmSourceFile instances in the referencing directory.
  1278. cmMakefile* mf = context->LG->GetMakefile();
  1279. for (std::string& o : objects) {
  1280. mf->AddTargetObject(tgtName, o);
  1281. }
  1282. return cmJoin(objects, ";");
  1283. }
  1284. } targetObjectsNode;
  1285. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1286. {
  1287. CompileFeaturesNode() {}
  1288. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1289. std::string Evaluate(
  1290. const std::vector<std::string>& parameters,
  1291. cmGeneratorExpressionContext* context,
  1292. const GeneratorExpressionContent* content,
  1293. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1294. {
  1295. cmGeneratorTarget const* target = context->HeadTarget;
  1296. if (!target) {
  1297. reportError(
  1298. context, content->GetOriginalExpression(),
  1299. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1300. "not be used with add_custom_command or add_custom_target.");
  1301. return std::string();
  1302. }
  1303. context->HadHeadSensitiveCondition = true;
  1304. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1305. static LangMap availableFeatures;
  1306. LangMap testedFeatures;
  1307. for (std::string const& p : parameters) {
  1308. std::string error;
  1309. std::string lang;
  1310. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1311. context->HeadTarget->Target, p, lang, &error)) {
  1312. reportError(context, content->GetOriginalExpression(), error);
  1313. return std::string();
  1314. }
  1315. testedFeatures[lang].push_back(p);
  1316. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1317. const char* featuresKnown =
  1318. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1319. if (!featuresKnown) {
  1320. reportError(context, content->GetOriginalExpression(), error);
  1321. return std::string();
  1322. }
  1323. cmSystemTools::ExpandListArgument(featuresKnown,
  1324. availableFeatures[lang]);
  1325. }
  1326. }
  1327. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1328. for (auto const& lit : testedFeatures) {
  1329. std::vector<std::string> const& langAvailable =
  1330. availableFeatures[lit.first];
  1331. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1332. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1333. for (std::string const& it : lit.second) {
  1334. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1335. langAvailable.end()) {
  1336. return "0";
  1337. }
  1338. if (standardDefault && !*standardDefault) {
  1339. // This compiler has no notion of language standard levels.
  1340. // All features known for the language are always available.
  1341. continue;
  1342. }
  1343. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1344. target->Target, lit.first, it)) {
  1345. if (evalLL) {
  1346. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1347. if (!l) {
  1348. l = standardDefault;
  1349. }
  1350. assert(l);
  1351. context->MaxLanguageStandard[target][lit.first] = l;
  1352. } else {
  1353. return "0";
  1354. }
  1355. }
  1356. }
  1357. }
  1358. return "1";
  1359. }
  1360. } compileFeaturesNode;
  1361. static const char* targetPolicyWhitelist[] = {
  1362. nullptr
  1363. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1364. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1365. #undef TARGET_POLICY_STRING
  1366. };
  1367. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1368. const char* policy)
  1369. {
  1370. #define RETURN_POLICY(POLICY) \
  1371. if (strcmp(policy, #POLICY) == 0) { \
  1372. return tgt->GetPolicyStatus##POLICY(); \
  1373. }
  1374. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1375. #undef RETURN_POLICY
  1376. assert(false && "Unreachable code. Not a valid policy");
  1377. return cmPolicies::WARN;
  1378. }
  1379. cmPolicies::PolicyID policyForString(const char* policy_id)
  1380. {
  1381. #define RETURN_POLICY_ID(POLICY_ID) \
  1382. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1383. return cmPolicies::POLICY_ID; \
  1384. }
  1385. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1386. #undef RETURN_POLICY_ID
  1387. assert(false && "Unreachable code. Not a valid policy");
  1388. return cmPolicies::CMP0002;
  1389. }
  1390. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1391. {
  1392. TargetPolicyNode() {}
  1393. int NumExpectedParameters() const override { return 1; }
  1394. std::string Evaluate(
  1395. const std::vector<std::string>& parameters,
  1396. cmGeneratorExpressionContext* context,
  1397. const GeneratorExpressionContent* content,
  1398. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1399. {
  1400. if (!context->HeadTarget) {
  1401. reportError(
  1402. context, content->GetOriginalExpression(),
  1403. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1404. "may not be used with add_custom_command or add_custom_target.");
  1405. return std::string();
  1406. }
  1407. context->HadContextSensitiveCondition = true;
  1408. context->HadHeadSensitiveCondition = true;
  1409. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1410. const char* policy = targetPolicyWhitelist[i];
  1411. if (parameters.front() == policy) {
  1412. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1413. switch (statusForTarget(context->HeadTarget, policy)) {
  1414. case cmPolicies::WARN:
  1415. lg->IssueMessage(
  1416. cmake::AUTHOR_WARNING,
  1417. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1418. CM_FALLTHROUGH;
  1419. case cmPolicies::REQUIRED_IF_USED:
  1420. case cmPolicies::REQUIRED_ALWAYS:
  1421. case cmPolicies::OLD:
  1422. return "0";
  1423. case cmPolicies::NEW:
  1424. return "1";
  1425. }
  1426. }
  1427. }
  1428. reportError(
  1429. context, content->GetOriginalExpression(),
  1430. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1431. "policies. Currently it may be used with the following policies:\n"
  1432. #define STRINGIFY_HELPER(X) #X
  1433. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1434. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1435. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1436. #undef TARGET_POLICY_LIST_ITEM
  1437. );
  1438. return std::string();
  1439. }
  1440. } targetPolicyNode;
  1441. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1442. {
  1443. InstallPrefixNode() {}
  1444. bool GeneratesContent() const override { return true; }
  1445. int NumExpectedParameters() const override { return 0; }
  1446. std::string Evaluate(
  1447. const std::vector<std::string>& /*parameters*/,
  1448. cmGeneratorExpressionContext* context,
  1449. const GeneratorExpressionContent* content,
  1450. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1451. {
  1452. reportError(context, content->GetOriginalExpression(),
  1453. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1454. "should never be evaluated.");
  1455. return std::string();
  1456. }
  1457. } installPrefixNode;
  1458. class ArtifactDirTag;
  1459. class ArtifactLinkerTag;
  1460. class ArtifactNameTag;
  1461. class ArtifactPathTag;
  1462. class ArtifactPdbTag;
  1463. class ArtifactSonameTag;
  1464. class ArtifactBundleDirTag;
  1465. class ArtifactBundleContentDirTag;
  1466. template <typename ArtifactT>
  1467. struct TargetFilesystemArtifactResultCreator
  1468. {
  1469. static std::string Create(cmGeneratorTarget* target,
  1470. cmGeneratorExpressionContext* context,
  1471. const GeneratorExpressionContent* content);
  1472. };
  1473. template <>
  1474. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1475. {
  1476. static std::string Create(cmGeneratorTarget* target,
  1477. cmGeneratorExpressionContext* context,
  1478. const GeneratorExpressionContent* content)
  1479. {
  1480. // The target soname file (.so.1).
  1481. if (target->IsDLLPlatform()) {
  1482. ::reportError(context, content->GetOriginalExpression(),
  1483. "TARGET_SONAME_FILE is not allowed "
  1484. "for DLL target platforms.");
  1485. return std::string();
  1486. }
  1487. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1488. ::reportError(context, content->GetOriginalExpression(),
  1489. "TARGET_SONAME_FILE is allowed only for "
  1490. "SHARED libraries.");
  1491. return std::string();
  1492. }
  1493. std::string result = target->GetDirectory(context->Config);
  1494. result += "/";
  1495. result += target->GetSOName(context->Config);
  1496. return result;
  1497. }
  1498. };
  1499. template <>
  1500. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1501. {
  1502. static std::string Create(cmGeneratorTarget* target,
  1503. cmGeneratorExpressionContext* context,
  1504. const GeneratorExpressionContent* content)
  1505. {
  1506. if (target->IsImported()) {
  1507. ::reportError(context, content->GetOriginalExpression(),
  1508. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1509. return std::string();
  1510. }
  1511. std::string language = target->GetLinkerLanguage(context->Config);
  1512. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1513. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1514. ::reportError(context, content->GetOriginalExpression(),
  1515. "TARGET_PDB_FILE is not supported by the target linker.");
  1516. return std::string();
  1517. }
  1518. cmStateEnums::TargetType targetType = target->GetType();
  1519. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1520. targetType != cmStateEnums::MODULE_LIBRARY &&
  1521. targetType != cmStateEnums::EXECUTABLE) {
  1522. ::reportError(context, content->GetOriginalExpression(),
  1523. "TARGET_PDB_FILE is allowed only for "
  1524. "targets with linker created artifacts.");
  1525. return std::string();
  1526. }
  1527. std::string result = target->GetPDBDirectory(context->Config);
  1528. result += "/";
  1529. result += target->GetPDBName(context->Config);
  1530. return result;
  1531. }
  1532. };
  1533. template <>
  1534. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1535. {
  1536. static std::string Create(cmGeneratorTarget* target,
  1537. cmGeneratorExpressionContext* context,
  1538. const GeneratorExpressionContent* content)
  1539. {
  1540. // The file used to link to the target (.so, .lib, .a).
  1541. if (!target->IsLinkable()) {
  1542. ::reportError(context, content->GetOriginalExpression(),
  1543. "TARGET_LINKER_FILE is allowed only for libraries and "
  1544. "executables with ENABLE_EXPORTS.");
  1545. return std::string();
  1546. }
  1547. cmStateEnums::ArtifactType artifact =
  1548. target->HasImportLibrary(context->Config)
  1549. ? cmStateEnums::ImportLibraryArtifact
  1550. : cmStateEnums::RuntimeBinaryArtifact;
  1551. return target->GetFullPath(context->Config, artifact);
  1552. }
  1553. };
  1554. template <>
  1555. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1556. {
  1557. static std::string Create(cmGeneratorTarget* target,
  1558. cmGeneratorExpressionContext* context,
  1559. const GeneratorExpressionContent* content)
  1560. {
  1561. if (target->IsImported()) {
  1562. ::reportError(context, content->GetOriginalExpression(),
  1563. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1564. return std::string();
  1565. }
  1566. if (!target->IsBundleOnApple()) {
  1567. ::reportError(context, content->GetOriginalExpression(),
  1568. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1569. return std::string();
  1570. }
  1571. std::string outpath = target->GetDirectory(context->Config) + '/';
  1572. return target->BuildBundleDirectory(outpath, context->Config,
  1573. cmGeneratorTarget::BundleDirLevel);
  1574. }
  1575. };
  1576. template <>
  1577. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1578. {
  1579. static std::string Create(cmGeneratorTarget* target,
  1580. cmGeneratorExpressionContext* context,
  1581. const GeneratorExpressionContent* content)
  1582. {
  1583. if (target->IsImported()) {
  1584. ::reportError(
  1585. context, content->GetOriginalExpression(),
  1586. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1587. return std::string();
  1588. }
  1589. if (!target->IsBundleOnApple()) {
  1590. ::reportError(
  1591. context, content->GetOriginalExpression(),
  1592. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1593. return std::string();
  1594. }
  1595. std::string outpath = target->GetDirectory(context->Config) + '/';
  1596. return target->BuildBundleDirectory(outpath, context->Config,
  1597. cmGeneratorTarget::ContentLevel);
  1598. }
  1599. };
  1600. template <>
  1601. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1602. {
  1603. static std::string Create(cmGeneratorTarget* target,
  1604. cmGeneratorExpressionContext* context,
  1605. const GeneratorExpressionContent* /*unused*/)
  1606. {
  1607. return target->GetFullPath(context->Config,
  1608. cmStateEnums::RuntimeBinaryArtifact, true);
  1609. }
  1610. };
  1611. template <typename ArtifactT>
  1612. struct TargetFilesystemArtifactResultGetter
  1613. {
  1614. static std::string Get(const std::string& result);
  1615. };
  1616. template <>
  1617. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1618. {
  1619. static std::string Get(const std::string& result)
  1620. {
  1621. return cmSystemTools::GetFilenameName(result);
  1622. }
  1623. };
  1624. template <>
  1625. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1626. {
  1627. static std::string Get(const std::string& result)
  1628. {
  1629. return cmSystemTools::GetFilenamePath(result);
  1630. }
  1631. };
  1632. template <>
  1633. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1634. {
  1635. static std::string Get(const std::string& result) { return result; }
  1636. };
  1637. template <typename ArtifactT, typename ComponentT>
  1638. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1639. {
  1640. TargetFilesystemArtifact() {}
  1641. int NumExpectedParameters() const override { return 1; }
  1642. std::string Evaluate(
  1643. const std::vector<std::string>& parameters,
  1644. cmGeneratorExpressionContext* context,
  1645. const GeneratorExpressionContent* content,
  1646. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1647. {
  1648. // Lookup the referenced target.
  1649. std::string name = *parameters.begin();
  1650. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1651. ::reportError(context, content->GetOriginalExpression(),
  1652. "Expression syntax not recognized.");
  1653. return std::string();
  1654. }
  1655. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1656. if (!target) {
  1657. ::reportError(context, content->GetOriginalExpression(),
  1658. "No target \"" + name + "\"");
  1659. return std::string();
  1660. }
  1661. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1662. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1663. ::reportError(context, content->GetOriginalExpression(),
  1664. "Target \"" + name +
  1665. "\" is not an executable or library.");
  1666. return std::string();
  1667. }
  1668. if (dagChecker &&
  1669. (dagChecker->EvaluatingLinkLibraries(target) ||
  1670. (dagChecker->EvaluatingSources() &&
  1671. target == dagChecker->TopTarget()))) {
  1672. ::reportError(context, content->GetOriginalExpression(),
  1673. "Expressions which require the linker language may not "
  1674. "be used while evaluating link libraries");
  1675. return std::string();
  1676. }
  1677. context->DependTargets.insert(target);
  1678. context->AllTargets.insert(target);
  1679. std::string result =
  1680. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1681. content);
  1682. if (context->HadError) {
  1683. return std::string();
  1684. }
  1685. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1686. }
  1687. };
  1688. template <typename ArtifactT>
  1689. struct TargetFilesystemArtifactNodeGroup
  1690. {
  1691. TargetFilesystemArtifactNodeGroup() {}
  1692. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1693. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1694. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1695. };
  1696. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1697. targetNodeGroup;
  1698. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1699. targetLinkerNodeGroup;
  1700. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1701. targetSoNameNodeGroup;
  1702. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1703. targetPdbNodeGroup;
  1704. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1705. targetBundleDirNode;
  1706. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1707. ArtifactPathTag>
  1708. targetBundleContentDirNode;
  1709. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1710. {
  1711. ShellPathNode() {}
  1712. std::string Evaluate(
  1713. const std::vector<std::string>& parameters,
  1714. cmGeneratorExpressionContext* context,
  1715. const GeneratorExpressionContent* content,
  1716. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1717. {
  1718. if (!cmSystemTools::FileIsFullPath(parameters.front())) {
  1719. reportError(context, content->GetOriginalExpression(),
  1720. "\"" + parameters.front() + "\" is not an absolute path.");
  1721. return std::string();
  1722. }
  1723. cmOutputConverter converter(context->LG->GetStateSnapshot());
  1724. return converter.ConvertDirectorySeparatorsForShell(parameters.front());
  1725. }
  1726. } shellPathNode;
  1727. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1728. const std::string& identifier)
  1729. {
  1730. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1731. static NodeMap nodeMap;
  1732. if (nodeMap.empty()) {
  1733. nodeMap["0"] = &zeroNode;
  1734. nodeMap["1"] = &oneNode;
  1735. nodeMap["AND"] = &andNode;
  1736. nodeMap["OR"] = &orNode;
  1737. nodeMap["NOT"] = &notNode;
  1738. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1739. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1740. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1741. nodeMap["VERSION_GREATER_EQUAL"] = &versionGreaterEqNode;
  1742. nodeMap["VERSION_LESS"] = &versionLessNode;
  1743. nodeMap["VERSION_LESS_EQUAL"] = &versionLessEqNode;
  1744. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1745. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1746. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1747. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1748. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1749. nodeMap["CONFIGURATION"] = &configurationNode;
  1750. nodeMap["CONFIG"] = &configurationTestNode;
  1751. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1752. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1753. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1754. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1755. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1756. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1757. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1758. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1759. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1760. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1761. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1762. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1763. nodeMap["TARGET_BUNDLE_DIR"] = &targetBundleDirNode;
  1764. nodeMap["TARGET_BUNDLE_CONTENT_DIR"] = &targetBundleContentDirNode;
  1765. nodeMap["STREQUAL"] = &strEqualNode;
  1766. nodeMap["EQUAL"] = &equalNode;
  1767. nodeMap["IN_LIST"] = &inListNode;
  1768. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1769. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1770. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1771. nodeMap["BOOL"] = &boolNode;
  1772. nodeMap["IF"] = &ifNode;
  1773. nodeMap["ANGLE-R"] = &angle_rNode;
  1774. nodeMap["COMMA"] = &commaNode;
  1775. nodeMap["SEMICOLON"] = &semicolonNode;
  1776. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1777. nodeMap["TARGET_NAME"] = &targetNameNode;
  1778. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1779. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1780. nodeMap["TARGET_EXISTS"] = &targetExistsNode;
  1781. nodeMap["TARGET_NAME_IF_EXISTS"] = &targetNameIfExistsNode;
  1782. nodeMap["TARGET_GENEX_EVAL"] = &targetGenexEvalNode;
  1783. nodeMap["GENEX_EVAL"] = &genexEvalNode;
  1784. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1785. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1786. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1787. nodeMap["JOIN"] = &joinNode;
  1788. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1789. nodeMap["COMPILE_LANGUAGE"] = &languageNode;
  1790. nodeMap["SHELL_PATH"] = &shellPathNode;
  1791. }
  1792. NodeMap::const_iterator i = nodeMap.find(identifier);
  1793. if (i == nodeMap.end()) {
  1794. return nullptr;
  1795. }
  1796. return i->second;
  1797. }
  1798. void reportError(cmGeneratorExpressionContext* context,
  1799. const std::string& expr, const std::string& result)
  1800. {
  1801. context->HadError = true;
  1802. if (context->Quiet) {
  1803. return;
  1804. }
  1805. std::ostringstream e;
  1806. /* clang-format off */
  1807. e << "Error evaluating generator expression:\n"
  1808. << " " << expr << "\n"
  1809. << result;
  1810. /* clang-format on */
  1811. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1812. context->Backtrace);
  1813. }