cmGeneratorExpressionNode.cxx 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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. depString += sep + "$<TARGET_PROPERTY:" + l.Target->GetName() + "," +
  911. interfacePropertyName + ">";
  912. sep = ";";
  913. }
  914. }
  915. if (!depString.empty()) {
  916. linkedTargetsContent =
  917. cmGeneratorExpressionNode::EvaluateDependentExpression(
  918. depString, target->GetLocalGenerator(), context, headTarget, target,
  919. dagChecker);
  920. }
  921. linkedTargetsContent =
  922. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  923. return linkedTargetsContent;
  924. }
  925. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  926. {
  927. TargetPropertyNode() {}
  928. // This node handles errors on parameter count itself.
  929. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  930. std::string Evaluate(
  931. const std::vector<std::string>& parameters,
  932. cmGeneratorExpressionContext* context,
  933. const GeneratorExpressionContent* content,
  934. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  935. {
  936. if (parameters.size() != 1 && parameters.size() != 2) {
  937. reportError(
  938. context, content->GetOriginalExpression(),
  939. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  940. return std::string();
  941. }
  942. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  943. cmGeneratorTarget const* target = context->HeadTarget;
  944. std::string propertyName = *parameters.begin();
  945. if (parameters.size() == 1) {
  946. context->HadHeadSensitiveCondition = true;
  947. }
  948. if (!target && parameters.size() == 1) {
  949. reportError(
  950. context, content->GetOriginalExpression(),
  951. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  952. "It may not be used with add_custom_command or add_custom_target. "
  953. "Specify the target to read a property from using the "
  954. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  955. return std::string();
  956. }
  957. if (parameters.size() == 2) {
  958. if (parameters.begin()->empty() && parameters[1].empty()) {
  959. reportError(
  960. context, content->GetOriginalExpression(),
  961. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  962. "target name and property name.");
  963. return std::string();
  964. }
  965. if (parameters.begin()->empty()) {
  966. reportError(
  967. context, content->GetOriginalExpression(),
  968. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  969. "target name.");
  970. return std::string();
  971. }
  972. std::string targetName = parameters.front();
  973. propertyName = parameters[1];
  974. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  975. if (!propertyNameValidator.find(propertyName)) {
  976. ::reportError(context, content->GetOriginalExpression(),
  977. "Target name and property name not supported.");
  978. return std::string();
  979. }
  980. ::reportError(context, content->GetOriginalExpression(),
  981. "Target name not supported.");
  982. return std::string();
  983. }
  984. static const std::string propALIASED_TARGET = "ALIASED_TARGET";
  985. if (propertyName == propALIASED_TARGET) {
  986. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  987. if (cmGeneratorTarget* tgt =
  988. context->LG->FindGeneratorTargetToUse(targetName)) {
  989. return tgt->GetName();
  990. }
  991. }
  992. return "";
  993. }
  994. target = context->LG->FindGeneratorTargetToUse(targetName);
  995. if (!target) {
  996. std::ostringstream e;
  997. e << "Target \"" << targetName << "\" not found.";
  998. reportError(context, content->GetOriginalExpression(), e.str());
  999. return std::string();
  1000. }
  1001. context->AllTargets.insert(target);
  1002. }
  1003. if (target == context->HeadTarget) {
  1004. // Keep track of the properties seen while processing.
  1005. // The evaluation of the LINK_LIBRARIES generator expressions
  1006. // will check this to ensure that properties have one consistent
  1007. // value for all evaluations.
  1008. context->SeenTargetProperties.insert(propertyName);
  1009. }
  1010. if (propertyName == "SOURCES") {
  1011. context->SourceSensitiveTargets.insert(target);
  1012. }
  1013. if (propertyName.empty()) {
  1014. reportError(
  1015. context, content->GetOriginalExpression(),
  1016. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1017. "name.");
  1018. return std::string();
  1019. }
  1020. if (!propertyNameValidator.find(propertyName)) {
  1021. ::reportError(context, content->GetOriginalExpression(),
  1022. "Property name not supported.");
  1023. return std::string();
  1024. }
  1025. assert(target);
  1026. if (propertyName == "LINKER_LANGUAGE") {
  1027. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1028. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1029. dagCheckerParent->EvaluatingSources())) {
  1030. reportError(
  1031. context, content->GetOriginalExpression(),
  1032. "LINKER_LANGUAGE target property can not be used while evaluating "
  1033. "link libraries for a static library");
  1034. return std::string();
  1035. }
  1036. return target->GetLinkerLanguage(context->Config);
  1037. }
  1038. cmGeneratorExpressionDAGChecker dagChecker(
  1039. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1040. switch (dagChecker.Check()) {
  1041. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1042. dagChecker.ReportError(context, content->GetOriginalExpression());
  1043. return std::string();
  1044. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1045. // No error. We just skip cyclic references.
  1046. return std::string();
  1047. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1048. for (size_t i = 1; i < cm::size(targetPropertyTransitiveWhitelist);
  1049. ++i) {
  1050. if (targetPropertyTransitiveWhitelist[i] == propertyName) {
  1051. // No error. We're not going to find anything new here.
  1052. return std::string();
  1053. }
  1054. }
  1055. case cmGeneratorExpressionDAGChecker::DAG:
  1056. break;
  1057. }
  1058. const char* prop = target->GetProperty(propertyName);
  1059. if (dagCheckerParent) {
  1060. if (dagCheckerParent->EvaluatingGenexExpression()) {
  1061. // No check required.
  1062. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1063. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  1064. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  1065. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(
  1066. TRANSITIVE_PROPERTY_COMPARE) false) { // NOLINT(clang-tidy)
  1067. reportError(
  1068. context, content->GetOriginalExpression(),
  1069. "$<TARGET_PROPERTY:...> expression in link libraries "
  1070. "evaluation depends on target property which is transitive "
  1071. "over the link libraries, creating a recursion.");
  1072. return std::string();
  1073. }
  1074. #undef TRANSITIVE_PROPERTY_COMPARE
  1075. if (!prop) {
  1076. return std::string();
  1077. }
  1078. } else {
  1079. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1080. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1081. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1082. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1083. }
  1084. }
  1085. std::string linkedTargetsContent;
  1086. std::string interfacePropertyName;
  1087. bool isInterfaceProperty = false;
  1088. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1089. if (propertyName == #prop) { \
  1090. interfacePropertyName = "INTERFACE_" #prop; \
  1091. } else if (propertyName == "INTERFACE_" #prop) { \
  1092. interfacePropertyName = "INTERFACE_" #prop; \
  1093. isInterfaceProperty = true; \
  1094. } else
  1095. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1096. // Note that the above macro terminates with an else
  1097. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1098. cmPolicies::PolicyStatus polSt =
  1099. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1100. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1101. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1102. }
  1103. }
  1104. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1105. cmGeneratorTarget const* headTarget =
  1106. context->HeadTarget && isInterfaceProperty ? context->HeadTarget
  1107. : target;
  1108. if (isInterfaceProperty) {
  1109. if (cmLinkInterfaceLibraries const* iface =
  1110. target->GetLinkInterfaceLibraries(context->Config, headTarget,
  1111. true)) {
  1112. linkedTargetsContent =
  1113. getLinkedTargetsContent(iface->Libraries, target, headTarget,
  1114. context, &dagChecker, interfacePropertyName);
  1115. }
  1116. } else if (!interfacePropertyName.empty()) {
  1117. if (cmLinkImplementationLibraries const* impl =
  1118. target->GetLinkImplementationLibraries(context->Config)) {
  1119. linkedTargetsContent =
  1120. getLinkedTargetsContent(impl->Libraries, target, target, context,
  1121. &dagChecker, interfacePropertyName);
  1122. }
  1123. }
  1124. if (!prop) {
  1125. if (target->IsImported() ||
  1126. target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1127. return linkedTargetsContent;
  1128. }
  1129. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1130. context->Config)) {
  1131. context->HadContextSensitiveCondition = true;
  1132. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1133. context->Config)
  1134. ? "1"
  1135. : "0";
  1136. }
  1137. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1138. context->Config)) {
  1139. context->HadContextSensitiveCondition = true;
  1140. const char* propContent =
  1141. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1142. context->Config);
  1143. return propContent ? propContent : "";
  1144. }
  1145. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1146. context->Config)) {
  1147. context->HadContextSensitiveCondition = true;
  1148. const char* propContent =
  1149. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1150. context->Config);
  1151. return propContent ? propContent : "";
  1152. }
  1153. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1154. context->Config)) {
  1155. context->HadContextSensitiveCondition = true;
  1156. const char* propContent =
  1157. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1158. context->Config);
  1159. return propContent ? propContent : "";
  1160. }
  1161. return linkedTargetsContent;
  1162. }
  1163. if (!target->IsImported() && dagCheckerParent &&
  1164. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1165. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1166. context->Config)) {
  1167. context->HadContextSensitiveCondition = true;
  1168. const char* propContent =
  1169. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1170. context->Config);
  1171. return propContent ? propContent : "";
  1172. }
  1173. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1174. context->Config)) {
  1175. context->HadContextSensitiveCondition = true;
  1176. const char* propContent =
  1177. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1178. context->Config);
  1179. return propContent ? propContent : "";
  1180. }
  1181. }
  1182. if (!interfacePropertyName.empty()) {
  1183. std::string result = this->EvaluateDependentExpression(
  1184. prop, context->LG, context, headTarget, target, &dagChecker);
  1185. if (!linkedTargetsContent.empty()) {
  1186. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1187. }
  1188. return result;
  1189. }
  1190. return prop;
  1191. }
  1192. } targetPropertyNode;
  1193. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1194. {
  1195. TargetNameNode() {}
  1196. bool GeneratesContent() const override { return true; }
  1197. bool AcceptsArbitraryContentParameter() const override { return true; }
  1198. bool RequiresLiteralInput() const override { return true; }
  1199. std::string Evaluate(
  1200. const std::vector<std::string>& parameters,
  1201. cmGeneratorExpressionContext* /*context*/,
  1202. const GeneratorExpressionContent* /*content*/,
  1203. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1204. {
  1205. return parameters.front();
  1206. }
  1207. int NumExpectedParameters() const override { return 1; }
  1208. } targetNameNode;
  1209. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1210. {
  1211. TargetObjectsNode() {}
  1212. std::string Evaluate(
  1213. const std::vector<std::string>& parameters,
  1214. cmGeneratorExpressionContext* context,
  1215. const GeneratorExpressionContent* content,
  1216. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1217. {
  1218. std::string tgtName = parameters.front();
  1219. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1220. if (!gt) {
  1221. std::ostringstream e;
  1222. e << "Objects of target \"" << tgtName
  1223. << "\" referenced but no such target exists.";
  1224. reportError(context, content->GetOriginalExpression(), e.str());
  1225. return std::string();
  1226. }
  1227. if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1228. std::ostringstream e;
  1229. e << "Objects of target \"" << tgtName
  1230. << "\" referenced but is not an OBJECT library.";
  1231. reportError(context, content->GetOriginalExpression(), e.str());
  1232. return std::string();
  1233. }
  1234. if (!context->EvaluateForBuildsystem) {
  1235. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1236. std::string reason;
  1237. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1238. std::ostringstream e;
  1239. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1240. "is only suitable for consumption by CMake (limited"
  1241. << reason
  1242. << "). "
  1243. "It is not suitable for writing out elsewhere.";
  1244. reportError(context, content->GetOriginalExpression(), e.str());
  1245. return std::string();
  1246. }
  1247. }
  1248. std::vector<std::string> objects;
  1249. if (gt->IsImported()) {
  1250. const char* loc = nullptr;
  1251. const char* imp = nullptr;
  1252. std::string suffix;
  1253. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1254. cmSystemTools::ExpandListArgument(loc, objects);
  1255. }
  1256. context->HadContextSensitiveCondition = true;
  1257. } else {
  1258. gt->GetTargetObjectNames(context->Config, objects);
  1259. std::string obj_dir;
  1260. if (context->EvaluateForBuildsystem) {
  1261. // Use object file directory with buildsystem placeholder.
  1262. obj_dir = gt->ObjectDirectory;
  1263. // Here we assume that the set of object files produced
  1264. // by an object library does not vary with configuration
  1265. // and do not set HadContextSensitiveCondition to true.
  1266. } else {
  1267. // Use object file directory with per-config location.
  1268. obj_dir = gt->GetObjectDirectory(context->Config);
  1269. context->HadContextSensitiveCondition = true;
  1270. }
  1271. for (std::string& o : objects) {
  1272. o = obj_dir + o;
  1273. }
  1274. }
  1275. // Create the cmSourceFile instances in the referencing directory.
  1276. cmMakefile* mf = context->LG->GetMakefile();
  1277. for (std::string& o : objects) {
  1278. mf->AddTargetObject(tgtName, o);
  1279. }
  1280. return cmJoin(objects, ";");
  1281. }
  1282. } targetObjectsNode;
  1283. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1284. {
  1285. CompileFeaturesNode() {}
  1286. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1287. std::string Evaluate(
  1288. const std::vector<std::string>& parameters,
  1289. cmGeneratorExpressionContext* context,
  1290. const GeneratorExpressionContent* content,
  1291. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1292. {
  1293. cmGeneratorTarget const* target = context->HeadTarget;
  1294. if (!target) {
  1295. reportError(
  1296. context, content->GetOriginalExpression(),
  1297. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1298. "not be used with add_custom_command or add_custom_target.");
  1299. return std::string();
  1300. }
  1301. context->HadHeadSensitiveCondition = true;
  1302. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1303. static LangMap availableFeatures;
  1304. LangMap testedFeatures;
  1305. for (std::string const& p : parameters) {
  1306. std::string error;
  1307. std::string lang;
  1308. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1309. context->HeadTarget->Target, p, lang, &error)) {
  1310. reportError(context, content->GetOriginalExpression(), error);
  1311. return std::string();
  1312. }
  1313. testedFeatures[lang].push_back(p);
  1314. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1315. const char* featuresKnown =
  1316. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1317. if (!featuresKnown) {
  1318. reportError(context, content->GetOriginalExpression(), error);
  1319. return std::string();
  1320. }
  1321. cmSystemTools::ExpandListArgument(featuresKnown,
  1322. availableFeatures[lang]);
  1323. }
  1324. }
  1325. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1326. for (auto const& lit : testedFeatures) {
  1327. std::vector<std::string> const& langAvailable =
  1328. availableFeatures[lit.first];
  1329. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1330. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1331. for (std::string const& it : lit.second) {
  1332. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1333. langAvailable.end()) {
  1334. return "0";
  1335. }
  1336. if (standardDefault && !*standardDefault) {
  1337. // This compiler has no notion of language standard levels.
  1338. // All features known for the language are always available.
  1339. continue;
  1340. }
  1341. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1342. target->Target, lit.first, it)) {
  1343. if (evalLL) {
  1344. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1345. if (!l) {
  1346. l = standardDefault;
  1347. }
  1348. assert(l);
  1349. context->MaxLanguageStandard[target][lit.first] = l;
  1350. } else {
  1351. return "0";
  1352. }
  1353. }
  1354. }
  1355. }
  1356. return "1";
  1357. }
  1358. } compileFeaturesNode;
  1359. static const char* targetPolicyWhitelist[] = {
  1360. nullptr
  1361. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1362. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1363. #undef TARGET_POLICY_STRING
  1364. };
  1365. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1366. const char* policy)
  1367. {
  1368. #define RETURN_POLICY(POLICY) \
  1369. if (strcmp(policy, #POLICY) == 0) { \
  1370. return tgt->GetPolicyStatus##POLICY(); \
  1371. }
  1372. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1373. #undef RETURN_POLICY
  1374. assert(false && "Unreachable code. Not a valid policy");
  1375. return cmPolicies::WARN;
  1376. }
  1377. cmPolicies::PolicyID policyForString(const char* policy_id)
  1378. {
  1379. #define RETURN_POLICY_ID(POLICY_ID) \
  1380. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1381. return cmPolicies::POLICY_ID; \
  1382. }
  1383. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1384. #undef RETURN_POLICY_ID
  1385. assert(false && "Unreachable code. Not a valid policy");
  1386. return cmPolicies::CMP0002;
  1387. }
  1388. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1389. {
  1390. TargetPolicyNode() {}
  1391. int NumExpectedParameters() const override { return 1; }
  1392. std::string Evaluate(
  1393. const std::vector<std::string>& parameters,
  1394. cmGeneratorExpressionContext* context,
  1395. const GeneratorExpressionContent* content,
  1396. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1397. {
  1398. if (!context->HeadTarget) {
  1399. reportError(
  1400. context, content->GetOriginalExpression(),
  1401. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1402. "may not be used with add_custom_command or add_custom_target.");
  1403. return std::string();
  1404. }
  1405. context->HadContextSensitiveCondition = true;
  1406. context->HadHeadSensitiveCondition = true;
  1407. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1408. const char* policy = targetPolicyWhitelist[i];
  1409. if (parameters.front() == policy) {
  1410. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1411. switch (statusForTarget(context->HeadTarget, policy)) {
  1412. case cmPolicies::WARN:
  1413. lg->IssueMessage(
  1414. cmake::AUTHOR_WARNING,
  1415. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1416. CM_FALLTHROUGH;
  1417. case cmPolicies::REQUIRED_IF_USED:
  1418. case cmPolicies::REQUIRED_ALWAYS:
  1419. case cmPolicies::OLD:
  1420. return "0";
  1421. case cmPolicies::NEW:
  1422. return "1";
  1423. }
  1424. }
  1425. }
  1426. reportError(
  1427. context, content->GetOriginalExpression(),
  1428. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1429. "policies. Currently it may be used with the following policies:\n"
  1430. #define STRINGIFY_HELPER(X) #X
  1431. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1432. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1433. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1434. #undef TARGET_POLICY_LIST_ITEM
  1435. );
  1436. return std::string();
  1437. }
  1438. } targetPolicyNode;
  1439. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1440. {
  1441. InstallPrefixNode() {}
  1442. bool GeneratesContent() const override { return true; }
  1443. int NumExpectedParameters() const override { return 0; }
  1444. std::string Evaluate(
  1445. const std::vector<std::string>& /*parameters*/,
  1446. cmGeneratorExpressionContext* context,
  1447. const GeneratorExpressionContent* content,
  1448. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1449. {
  1450. reportError(context, content->GetOriginalExpression(),
  1451. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1452. "should never be evaluated.");
  1453. return std::string();
  1454. }
  1455. } installPrefixNode;
  1456. class ArtifactDirTag;
  1457. class ArtifactLinkerTag;
  1458. class ArtifactNameTag;
  1459. class ArtifactPathTag;
  1460. class ArtifactPdbTag;
  1461. class ArtifactSonameTag;
  1462. class ArtifactBundleDirTag;
  1463. class ArtifactBundleContentDirTag;
  1464. template <typename ArtifactT>
  1465. struct TargetFilesystemArtifactResultCreator
  1466. {
  1467. static std::string Create(cmGeneratorTarget* target,
  1468. cmGeneratorExpressionContext* context,
  1469. const GeneratorExpressionContent* content);
  1470. };
  1471. template <>
  1472. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1473. {
  1474. static std::string Create(cmGeneratorTarget* target,
  1475. cmGeneratorExpressionContext* context,
  1476. const GeneratorExpressionContent* content)
  1477. {
  1478. // The target soname file (.so.1).
  1479. if (target->IsDLLPlatform()) {
  1480. ::reportError(context, content->GetOriginalExpression(),
  1481. "TARGET_SONAME_FILE is not allowed "
  1482. "for DLL target platforms.");
  1483. return std::string();
  1484. }
  1485. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1486. ::reportError(context, content->GetOriginalExpression(),
  1487. "TARGET_SONAME_FILE is allowed only for "
  1488. "SHARED libraries.");
  1489. return std::string();
  1490. }
  1491. std::string result = target->GetDirectory(context->Config);
  1492. result += "/";
  1493. result += target->GetSOName(context->Config);
  1494. return result;
  1495. }
  1496. };
  1497. template <>
  1498. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1499. {
  1500. static std::string Create(cmGeneratorTarget* target,
  1501. cmGeneratorExpressionContext* context,
  1502. const GeneratorExpressionContent* content)
  1503. {
  1504. if (target->IsImported()) {
  1505. ::reportError(context, content->GetOriginalExpression(),
  1506. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1507. return std::string();
  1508. }
  1509. std::string language = target->GetLinkerLanguage(context->Config);
  1510. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1511. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1512. ::reportError(context, content->GetOriginalExpression(),
  1513. "TARGET_PDB_FILE is not supported by the target linker.");
  1514. return std::string();
  1515. }
  1516. cmStateEnums::TargetType targetType = target->GetType();
  1517. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1518. targetType != cmStateEnums::MODULE_LIBRARY &&
  1519. targetType != cmStateEnums::EXECUTABLE) {
  1520. ::reportError(context, content->GetOriginalExpression(),
  1521. "TARGET_PDB_FILE is allowed only for "
  1522. "targets with linker created artifacts.");
  1523. return std::string();
  1524. }
  1525. std::string result = target->GetPDBDirectory(context->Config);
  1526. result += "/";
  1527. result += target->GetPDBName(context->Config);
  1528. return result;
  1529. }
  1530. };
  1531. template <>
  1532. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1533. {
  1534. static std::string Create(cmGeneratorTarget* target,
  1535. cmGeneratorExpressionContext* context,
  1536. const GeneratorExpressionContent* content)
  1537. {
  1538. // The file used to link to the target (.so, .lib, .a).
  1539. if (!target->IsLinkable()) {
  1540. ::reportError(context, content->GetOriginalExpression(),
  1541. "TARGET_LINKER_FILE is allowed only for libraries and "
  1542. "executables with ENABLE_EXPORTS.");
  1543. return std::string();
  1544. }
  1545. cmStateEnums::ArtifactType artifact =
  1546. target->HasImportLibrary(context->Config)
  1547. ? cmStateEnums::ImportLibraryArtifact
  1548. : cmStateEnums::RuntimeBinaryArtifact;
  1549. return target->GetFullPath(context->Config, artifact);
  1550. }
  1551. };
  1552. template <>
  1553. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1554. {
  1555. static std::string Create(cmGeneratorTarget* target,
  1556. cmGeneratorExpressionContext* context,
  1557. const GeneratorExpressionContent* content)
  1558. {
  1559. if (target->IsImported()) {
  1560. ::reportError(context, content->GetOriginalExpression(),
  1561. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1562. return std::string();
  1563. }
  1564. if (!target->IsBundleOnApple()) {
  1565. ::reportError(context, content->GetOriginalExpression(),
  1566. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1567. return std::string();
  1568. }
  1569. std::string outpath = target->GetDirectory(context->Config) + '/';
  1570. return target->BuildBundleDirectory(outpath, context->Config,
  1571. cmGeneratorTarget::BundleDirLevel);
  1572. }
  1573. };
  1574. template <>
  1575. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1576. {
  1577. static std::string Create(cmGeneratorTarget* target,
  1578. cmGeneratorExpressionContext* context,
  1579. const GeneratorExpressionContent* content)
  1580. {
  1581. if (target->IsImported()) {
  1582. ::reportError(
  1583. context, content->GetOriginalExpression(),
  1584. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1585. return std::string();
  1586. }
  1587. if (!target->IsBundleOnApple()) {
  1588. ::reportError(
  1589. context, content->GetOriginalExpression(),
  1590. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1591. return std::string();
  1592. }
  1593. std::string outpath = target->GetDirectory(context->Config) + '/';
  1594. return target->BuildBundleDirectory(outpath, context->Config,
  1595. cmGeneratorTarget::ContentLevel);
  1596. }
  1597. };
  1598. template <>
  1599. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1600. {
  1601. static std::string Create(cmGeneratorTarget* target,
  1602. cmGeneratorExpressionContext* context,
  1603. const GeneratorExpressionContent* /*unused*/)
  1604. {
  1605. return target->GetFullPath(context->Config,
  1606. cmStateEnums::RuntimeBinaryArtifact, true);
  1607. }
  1608. };
  1609. template <typename ArtifactT>
  1610. struct TargetFilesystemArtifactResultGetter
  1611. {
  1612. static std::string Get(const std::string& result);
  1613. };
  1614. template <>
  1615. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1616. {
  1617. static std::string Get(const std::string& result)
  1618. {
  1619. return cmSystemTools::GetFilenameName(result);
  1620. }
  1621. };
  1622. template <>
  1623. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1624. {
  1625. static std::string Get(const std::string& result)
  1626. {
  1627. return cmSystemTools::GetFilenamePath(result);
  1628. }
  1629. };
  1630. template <>
  1631. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1632. {
  1633. static std::string Get(const std::string& result) { return result; }
  1634. };
  1635. template <typename ArtifactT, typename ComponentT>
  1636. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1637. {
  1638. TargetFilesystemArtifact() {}
  1639. int NumExpectedParameters() const override { return 1; }
  1640. std::string Evaluate(
  1641. const std::vector<std::string>& parameters,
  1642. cmGeneratorExpressionContext* context,
  1643. const GeneratorExpressionContent* content,
  1644. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1645. {
  1646. // Lookup the referenced target.
  1647. std::string name = *parameters.begin();
  1648. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1649. ::reportError(context, content->GetOriginalExpression(),
  1650. "Expression syntax not recognized.");
  1651. return std::string();
  1652. }
  1653. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1654. if (!target) {
  1655. ::reportError(context, content->GetOriginalExpression(),
  1656. "No target \"" + name + "\"");
  1657. return std::string();
  1658. }
  1659. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1660. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1661. ::reportError(context, content->GetOriginalExpression(),
  1662. "Target \"" + name +
  1663. "\" is not an executable or library.");
  1664. return std::string();
  1665. }
  1666. if (dagChecker &&
  1667. (dagChecker->EvaluatingLinkLibraries(target) ||
  1668. (dagChecker->EvaluatingSources() &&
  1669. target == dagChecker->TopTarget()))) {
  1670. ::reportError(context, content->GetOriginalExpression(),
  1671. "Expressions which require the linker language may not "
  1672. "be used while evaluating link libraries");
  1673. return std::string();
  1674. }
  1675. context->DependTargets.insert(target);
  1676. context->AllTargets.insert(target);
  1677. std::string result =
  1678. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1679. content);
  1680. if (context->HadError) {
  1681. return std::string();
  1682. }
  1683. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1684. }
  1685. };
  1686. template <typename ArtifactT>
  1687. struct TargetFilesystemArtifactNodeGroup
  1688. {
  1689. TargetFilesystemArtifactNodeGroup() {}
  1690. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1691. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1692. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1693. };
  1694. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1695. targetNodeGroup;
  1696. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1697. targetLinkerNodeGroup;
  1698. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1699. targetSoNameNodeGroup;
  1700. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1701. targetPdbNodeGroup;
  1702. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1703. targetBundleDirNode;
  1704. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1705. ArtifactPathTag>
  1706. targetBundleContentDirNode;
  1707. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1708. {
  1709. ShellPathNode() {}
  1710. std::string Evaluate(
  1711. const std::vector<std::string>& parameters,
  1712. cmGeneratorExpressionContext* context,
  1713. const GeneratorExpressionContent* content,
  1714. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1715. {
  1716. if (!cmSystemTools::FileIsFullPath(parameters.front())) {
  1717. reportError(context, content->GetOriginalExpression(),
  1718. "\"" + parameters.front() + "\" is not an absolute path.");
  1719. return std::string();
  1720. }
  1721. cmOutputConverter converter(context->LG->GetStateSnapshot());
  1722. return converter.ConvertDirectorySeparatorsForShell(parameters.front());
  1723. }
  1724. } shellPathNode;
  1725. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1726. const std::string& identifier)
  1727. {
  1728. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1729. static NodeMap nodeMap;
  1730. if (nodeMap.empty()) {
  1731. nodeMap["0"] = &zeroNode;
  1732. nodeMap["1"] = &oneNode;
  1733. nodeMap["AND"] = &andNode;
  1734. nodeMap["OR"] = &orNode;
  1735. nodeMap["NOT"] = &notNode;
  1736. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1737. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1738. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1739. nodeMap["VERSION_GREATER_EQUAL"] = &versionGreaterEqNode;
  1740. nodeMap["VERSION_LESS"] = &versionLessNode;
  1741. nodeMap["VERSION_LESS_EQUAL"] = &versionLessEqNode;
  1742. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1743. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1744. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1745. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1746. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1747. nodeMap["CONFIGURATION"] = &configurationNode;
  1748. nodeMap["CONFIG"] = &configurationTestNode;
  1749. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1750. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1751. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1752. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1753. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1754. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1755. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1756. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1757. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1758. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1759. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1760. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1761. nodeMap["TARGET_BUNDLE_DIR"] = &targetBundleDirNode;
  1762. nodeMap["TARGET_BUNDLE_CONTENT_DIR"] = &targetBundleContentDirNode;
  1763. nodeMap["STREQUAL"] = &strEqualNode;
  1764. nodeMap["EQUAL"] = &equalNode;
  1765. nodeMap["IN_LIST"] = &inListNode;
  1766. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1767. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1768. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1769. nodeMap["BOOL"] = &boolNode;
  1770. nodeMap["IF"] = &ifNode;
  1771. nodeMap["ANGLE-R"] = &angle_rNode;
  1772. nodeMap["COMMA"] = &commaNode;
  1773. nodeMap["SEMICOLON"] = &semicolonNode;
  1774. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1775. nodeMap["TARGET_NAME"] = &targetNameNode;
  1776. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1777. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1778. nodeMap["TARGET_EXISTS"] = &targetExistsNode;
  1779. nodeMap["TARGET_NAME_IF_EXISTS"] = &targetNameIfExistsNode;
  1780. nodeMap["TARGET_GENEX_EVAL"] = &targetGenexEvalNode;
  1781. nodeMap["GENEX_EVAL"] = &genexEvalNode;
  1782. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1783. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1784. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1785. nodeMap["JOIN"] = &joinNode;
  1786. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1787. nodeMap["COMPILE_LANGUAGE"] = &languageNode;
  1788. nodeMap["SHELL_PATH"] = &shellPathNode;
  1789. }
  1790. NodeMap::const_iterator i = nodeMap.find(identifier);
  1791. if (i == nodeMap.end()) {
  1792. return nullptr;
  1793. }
  1794. return i->second;
  1795. }
  1796. void reportError(cmGeneratorExpressionContext* context,
  1797. const std::string& expr, const std::string& result)
  1798. {
  1799. context->HadError = true;
  1800. if (context->Quiet) {
  1801. return;
  1802. }
  1803. std::ostringstream e;
  1804. /* clang-format off */
  1805. e << "Error evaluating generator expression:\n"
  1806. << " " << expr << "\n"
  1807. << result;
  1808. /* clang-format on */
  1809. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1810. context->Backtrace);
  1811. }