cmGeneratorExpressionNode.cxx 74 KB

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