cmGeneratorExpressionNode.cxx 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  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 <algorithm>
  5. #include <cassert>
  6. #include <cerrno>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <map>
  10. #include <memory>
  11. #include <set>
  12. #include <sstream>
  13. #include <utility>
  14. #include <cm/iterator>
  15. #include <cm/optional>
  16. #include <cm/string_view>
  17. #include <cm/vector>
  18. #include <cmext/algorithm>
  19. #include <cmext/string_view>
  20. #include "cmsys/RegularExpression.hxx"
  21. #include "cmsys/String.h"
  22. #include "cmAlgorithms.h"
  23. #include "cmComputeLinkInformation.h"
  24. #include "cmGeneratorExpression.h"
  25. #include "cmGeneratorExpressionContext.h"
  26. #include "cmGeneratorExpressionDAGChecker.h"
  27. #include "cmGeneratorExpressionEvaluator.h"
  28. #include "cmGeneratorTarget.h"
  29. #include "cmGlobalGenerator.h"
  30. #include "cmLinkItem.h"
  31. #include "cmLocalGenerator.h"
  32. #include "cmMakefile.h"
  33. #include "cmMessageType.h"
  34. #include "cmOutputConverter.h"
  35. #include "cmPolicies.h"
  36. #include "cmRange.h"
  37. #include "cmStandardLevelResolver.h"
  38. #include "cmState.h"
  39. #include "cmStateSnapshot.h"
  40. #include "cmStateTypes.h"
  41. #include "cmStringAlgorithms.h"
  42. #include "cmSystemTools.h"
  43. #include "cmTarget.h"
  44. #include "cmValue.h"
  45. #include "cmake.h"
  46. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  47. std::string const& prop, cmLocalGenerator* lg,
  48. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  49. cmGeneratorExpressionDAGChecker* dagChecker,
  50. cmGeneratorTarget const* currentTarget)
  51. {
  52. cmGeneratorExpression ge(context->Backtrace);
  53. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  54. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  55. cge->SetQuiet(context->Quiet);
  56. std::string result =
  57. cge->Evaluate(lg, context->Config, headTarget, dagChecker, currentTarget,
  58. context->Language);
  59. if (cge->GetHadContextSensitiveCondition()) {
  60. context->HadContextSensitiveCondition = true;
  61. }
  62. if (cge->GetHadHeadSensitiveCondition()) {
  63. context->HadHeadSensitiveCondition = true;
  64. }
  65. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  66. context->HadLinkLanguageSensitiveCondition = true;
  67. }
  68. return result;
  69. }
  70. static const struct ZeroNode : public cmGeneratorExpressionNode
  71. {
  72. ZeroNode() {} // NOLINT(modernize-use-equals-default)
  73. bool GeneratesContent() const override { return false; }
  74. bool AcceptsArbitraryContentParameter() const override { return true; }
  75. std::string Evaluate(
  76. const std::vector<std::string>& /*parameters*/,
  77. cmGeneratorExpressionContext* /*context*/,
  78. const GeneratorExpressionContent* /*content*/,
  79. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  80. {
  81. return std::string();
  82. }
  83. } zeroNode;
  84. static const struct OneNode : public cmGeneratorExpressionNode
  85. {
  86. OneNode() {} // NOLINT(modernize-use-equals-default)
  87. bool AcceptsArbitraryContentParameter() const override { return true; }
  88. std::string Evaluate(
  89. const std::vector<std::string>& parameters,
  90. cmGeneratorExpressionContext* /*context*/,
  91. const GeneratorExpressionContent* /*content*/,
  92. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  93. {
  94. return parameters.front();
  95. }
  96. } oneNode;
  97. static const struct OneNode buildInterfaceNode;
  98. static const struct ZeroNode installInterfaceNode;
  99. struct BooleanOpNode : public cmGeneratorExpressionNode
  100. {
  101. BooleanOpNode(const char* op_, const char* successVal_,
  102. const char* failureVal_)
  103. : op(op_)
  104. , successVal(successVal_)
  105. , failureVal(failureVal_)
  106. {
  107. }
  108. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  109. std::string Evaluate(const std::vector<std::string>& parameters,
  110. cmGeneratorExpressionContext* context,
  111. const GeneratorExpressionContent* content,
  112. cmGeneratorExpressionDAGChecker*) const override
  113. {
  114. for (std::string const& param : parameters) {
  115. if (param == this->failureVal) {
  116. return this->failureVal;
  117. }
  118. if (param != this->successVal) {
  119. std::ostringstream e;
  120. e << "Parameters to $<" << this->op;
  121. e << "> must resolve to either '0' or '1'.";
  122. reportError(context, content->GetOriginalExpression(), e.str());
  123. return std::string();
  124. }
  125. }
  126. return this->successVal;
  127. }
  128. const char *const op, *const successVal, *const failureVal;
  129. };
  130. static const BooleanOpNode andNode("AND", "1", "0"), orNode("OR", "0", "1");
  131. static const struct NotNode : public cmGeneratorExpressionNode
  132. {
  133. NotNode() {} // NOLINT(modernize-use-equals-default)
  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. if (parameters.front() != "0" && parameters.front() != "1") {
  141. reportError(
  142. context, content->GetOriginalExpression(),
  143. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  144. return std::string();
  145. }
  146. return parameters.front() == "0" ? "1" : "0";
  147. }
  148. } notNode;
  149. static const struct BoolNode : public cmGeneratorExpressionNode
  150. {
  151. BoolNode() {} // NOLINT(modernize-use-equals-default)
  152. int NumExpectedParameters() const override { return 1; }
  153. std::string Evaluate(
  154. const std::vector<std::string>& parameters,
  155. cmGeneratorExpressionContext* /*context*/,
  156. const GeneratorExpressionContent* /*content*/,
  157. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  158. {
  159. return !cmIsOff(parameters.front()) ? "1" : "0";
  160. }
  161. } boolNode;
  162. static const struct IfNode : public cmGeneratorExpressionNode
  163. {
  164. IfNode() {} // NOLINT(modernize-use-equals-default)
  165. int NumExpectedParameters() const override { return 3; }
  166. std::string Evaluate(const std::vector<std::string>& parameters,
  167. cmGeneratorExpressionContext* context,
  168. const GeneratorExpressionContent* content,
  169. cmGeneratorExpressionDAGChecker*) const override
  170. {
  171. if (parameters[0] != "1" && parameters[0] != "0") {
  172. reportError(context, content->GetOriginalExpression(),
  173. "First parameter to $<IF> must resolve to exactly one '0' "
  174. "or '1' value.");
  175. return std::string();
  176. }
  177. return parameters[0] == "1" ? parameters[1] : parameters[2];
  178. }
  179. } ifNode;
  180. static const struct StrEqualNode : public cmGeneratorExpressionNode
  181. {
  182. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  183. int NumExpectedParameters() const override { return 2; }
  184. std::string Evaluate(
  185. const std::vector<std::string>& parameters,
  186. cmGeneratorExpressionContext* /*context*/,
  187. const GeneratorExpressionContent* /*content*/,
  188. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  189. {
  190. return parameters.front() == parameters[1] ? "1" : "0";
  191. }
  192. } strEqualNode;
  193. static const struct EqualNode : public cmGeneratorExpressionNode
  194. {
  195. EqualNode() {} // NOLINT(modernize-use-equals-default)
  196. int NumExpectedParameters() const override { return 2; }
  197. std::string Evaluate(
  198. const std::vector<std::string>& parameters,
  199. cmGeneratorExpressionContext* context,
  200. const GeneratorExpressionContent* content,
  201. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  202. {
  203. long numbers[2];
  204. for (int i = 0; i < 2; ++i) {
  205. if (!ParameterToLong(parameters[i].c_str(), &numbers[i])) {
  206. reportError(context, content->GetOriginalExpression(),
  207. "$<EQUAL> parameter " + parameters[i] +
  208. " is not a valid integer.");
  209. return {};
  210. }
  211. }
  212. return numbers[0] == numbers[1] ? "1" : "0";
  213. }
  214. static bool ParameterToLong(const char* param, long* outResult)
  215. {
  216. const char isNegative = param[0] == '-';
  217. int base = 0;
  218. if (cmHasLiteralPrefix(param, "0b") || cmHasLiteralPrefix(param, "0B")) {
  219. base = 2;
  220. param += 2;
  221. } else if (cmHasLiteralPrefix(param, "-0b") ||
  222. cmHasLiteralPrefix(param, "-0B") ||
  223. cmHasLiteralPrefix(param, "+0b") ||
  224. cmHasLiteralPrefix(param, "+0B")) {
  225. base = 2;
  226. param += 3;
  227. }
  228. char* pEnd;
  229. long result = strtol(param, &pEnd, base);
  230. if (pEnd == param || *pEnd != '\0' || errno == ERANGE) {
  231. return false;
  232. }
  233. if (isNegative && result > 0) {
  234. result *= -1;
  235. }
  236. *outResult = result;
  237. return true;
  238. }
  239. } equalNode;
  240. static const struct InListNode : public cmGeneratorExpressionNode
  241. {
  242. InListNode() {} // NOLINT(modernize-use-equals-default)
  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;
  251. std::vector<std::string> checkValues;
  252. bool check = false;
  253. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  254. case cmPolicies::WARN:
  255. if (parameters.front().empty()) {
  256. check = true;
  257. cmExpandList(parameters[1], checkValues, true);
  258. }
  259. CM_FALLTHROUGH;
  260. case cmPolicies::OLD:
  261. cmExpandList(parameters[1], values);
  262. if (check && values != checkValues) {
  263. std::ostringstream e;
  264. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  265. << "\nSearch Item:\n \"" << parameters.front()
  266. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  267. context->LG->GetCMakeInstance()->IssueMessage(
  268. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  269. return "0";
  270. }
  271. if (values.empty()) {
  272. return "0";
  273. }
  274. break;
  275. case cmPolicies::REQUIRED_IF_USED:
  276. case cmPolicies::REQUIRED_ALWAYS:
  277. case cmPolicies::NEW:
  278. cmExpandList(parameters[1], values, true);
  279. break;
  280. }
  281. return cm::contains(values, parameters.front()) ? "1" : "0";
  282. }
  283. } inListNode;
  284. static const struct FilterNode : public cmGeneratorExpressionNode
  285. {
  286. FilterNode() {} // NOLINT(modernize-use-equals-default)
  287. int NumExpectedParameters() const override { return 3; }
  288. std::string Evaluate(
  289. const std::vector<std::string>& parameters,
  290. cmGeneratorExpressionContext* context,
  291. const GeneratorExpressionContent* content,
  292. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  293. {
  294. if (parameters.size() != 3) {
  295. reportError(context, content->GetOriginalExpression(),
  296. "$<FILTER:...> expression requires three parameters");
  297. return {};
  298. }
  299. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  300. reportError(
  301. context, content->GetOriginalExpression(),
  302. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  303. return {};
  304. }
  305. const bool exclude = parameters[1] == "EXCLUDE";
  306. cmsys::RegularExpression re;
  307. if (!re.compile(parameters[2])) {
  308. reportError(context, content->GetOriginalExpression(),
  309. "$<FILTER:...> failed to compile regex");
  310. return {};
  311. }
  312. std::vector<std::string> values;
  313. std::vector<std::string> result;
  314. cmExpandList(parameters.front(), values, true);
  315. std::copy_if(values.cbegin(), values.cend(), std::back_inserter(result),
  316. [&re, exclude](std::string const& input) {
  317. return exclude ^ re.find(input);
  318. });
  319. return cmJoin(cmMakeRange(result.cbegin(), result.cend()), ";");
  320. }
  321. } filterNode;
  322. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  323. {
  324. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  325. int NumExpectedParameters() const override { return 1; }
  326. std::string Evaluate(
  327. const std::vector<std::string>& parameters,
  328. cmGeneratorExpressionContext* context,
  329. const GeneratorExpressionContent* content,
  330. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  331. {
  332. if (parameters.size() != 1) {
  333. reportError(
  334. context, content->GetOriginalExpression(),
  335. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  336. }
  337. std::vector<std::string> values = cmExpandedList(parameters.front(), true);
  338. auto valuesEnd = cmRemoveDuplicates(values);
  339. auto valuesBegin = values.cbegin();
  340. return cmJoin(cmMakeRange(valuesBegin, valuesEnd), ";");
  341. }
  342. } removeDuplicatesNode;
  343. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  344. {
  345. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  346. int NumExpectedParameters() const override { return 1; }
  347. std::string Evaluate(
  348. const std::vector<std::string>& parameters,
  349. cmGeneratorExpressionContext* context,
  350. const GeneratorExpressionContent* content,
  351. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  352. {
  353. if (parameters.size() != 1) {
  354. reportError(context, content->GetOriginalExpression(),
  355. "$<TARGET_EXISTS:...> expression requires one parameter");
  356. return std::string();
  357. }
  358. std::string targetName = parameters.front();
  359. if (targetName.empty() ||
  360. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  361. reportError(context, content->GetOriginalExpression(),
  362. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  363. "valid target name.");
  364. return std::string();
  365. }
  366. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  367. }
  368. } targetExistsNode;
  369. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  370. {
  371. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  372. int NumExpectedParameters() const override { return 1; }
  373. std::string Evaluate(
  374. const std::vector<std::string>& parameters,
  375. cmGeneratorExpressionContext* context,
  376. const GeneratorExpressionContent* content,
  377. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  378. {
  379. if (parameters.size() != 1) {
  380. reportError(context, content->GetOriginalExpression(),
  381. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  382. "parameter");
  383. return std::string();
  384. }
  385. std::string targetName = parameters.front();
  386. if (targetName.empty() ||
  387. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  388. reportError(context, content->GetOriginalExpression(),
  389. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  390. "non-empty valid target name.");
  391. return std::string();
  392. }
  393. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  394. ? targetName
  395. : std::string();
  396. }
  397. } targetNameIfExistsNode;
  398. struct GenexEvaluator : public cmGeneratorExpressionNode
  399. {
  400. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  401. protected:
  402. std::string EvaluateExpression(
  403. const std::string& genexOperator, const std::string& expression,
  404. cmGeneratorExpressionContext* context,
  405. const GeneratorExpressionContent* content,
  406. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  407. {
  408. if (context->HeadTarget) {
  409. cmGeneratorExpressionDAGChecker dagChecker(
  410. context->Backtrace, context->HeadTarget,
  411. genexOperator + ":" + expression, content, dagCheckerParent);
  412. switch (dagChecker.Check()) {
  413. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  414. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  415. dagChecker.ReportError(context, content->GetOriginalExpression());
  416. return std::string();
  417. }
  418. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  419. case cmGeneratorExpressionDAGChecker::DAG:
  420. break;
  421. }
  422. return this->EvaluateDependentExpression(
  423. expression, context->LG, context, context->HeadTarget, &dagChecker,
  424. context->CurrentTarget);
  425. }
  426. return this->EvaluateDependentExpression(
  427. expression, context->LG, context, context->HeadTarget, dagCheckerParent,
  428. context->CurrentTarget);
  429. }
  430. };
  431. static const struct TargetGenexEvalNode : public GenexEvaluator
  432. {
  433. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  434. int NumExpectedParameters() const override { return 2; }
  435. bool AcceptsArbitraryContentParameter() const override { return true; }
  436. std::string Evaluate(
  437. const std::vector<std::string>& parameters,
  438. cmGeneratorExpressionContext* context,
  439. const GeneratorExpressionContent* content,
  440. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  441. {
  442. const std::string& targetName = parameters.front();
  443. if (targetName.empty() ||
  444. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  445. reportError(context, content->GetOriginalExpression(),
  446. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  447. "non-empty valid target name.");
  448. return std::string();
  449. }
  450. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  451. if (!target) {
  452. std::ostringstream e;
  453. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  454. << "\" not found.";
  455. reportError(context, content->GetOriginalExpression(), e.str());
  456. return std::string();
  457. }
  458. const std::string& expression = parameters[1];
  459. if (expression.empty()) {
  460. return expression;
  461. }
  462. cmGeneratorExpressionContext targetContext(
  463. context->LG, context->Config, context->Quiet, target, target,
  464. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  465. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  466. &targetContext, content, dagCheckerParent);
  467. }
  468. } targetGenexEvalNode;
  469. static const struct GenexEvalNode : public GenexEvaluator
  470. {
  471. GenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  472. int NumExpectedParameters() const override { return 1; }
  473. bool AcceptsArbitraryContentParameter() const override { return true; }
  474. std::string Evaluate(
  475. const std::vector<std::string>& parameters,
  476. cmGeneratorExpressionContext* context,
  477. const GeneratorExpressionContent* content,
  478. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  479. {
  480. const std::string& expression = parameters[0];
  481. if (expression.empty()) {
  482. return expression;
  483. }
  484. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  485. dagCheckerParent);
  486. }
  487. } genexEvalNode;
  488. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  489. {
  490. LowerCaseNode() {} // NOLINT(modernize-use-equals-default)
  491. bool AcceptsArbitraryContentParameter() const override { return true; }
  492. std::string Evaluate(
  493. const std::vector<std::string>& parameters,
  494. cmGeneratorExpressionContext* /*context*/,
  495. const GeneratorExpressionContent* /*content*/,
  496. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  497. {
  498. return cmSystemTools::LowerCase(parameters.front());
  499. }
  500. } lowerCaseNode;
  501. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  502. {
  503. UpperCaseNode() {} // NOLINT(modernize-use-equals-default)
  504. bool AcceptsArbitraryContentParameter() const override { return true; }
  505. std::string Evaluate(
  506. const std::vector<std::string>& parameters,
  507. cmGeneratorExpressionContext* /*context*/,
  508. const GeneratorExpressionContent* /*content*/,
  509. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  510. {
  511. return cmSystemTools::UpperCase(parameters.front());
  512. }
  513. } upperCaseNode;
  514. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  515. {
  516. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  517. bool AcceptsArbitraryContentParameter() const override { return true; }
  518. std::string Evaluate(
  519. const std::vector<std::string>& parameters,
  520. cmGeneratorExpressionContext* /*context*/,
  521. const GeneratorExpressionContent* /*content*/,
  522. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  523. {
  524. return cmSystemTools::MakeCidentifier(parameters.front());
  525. }
  526. } makeCIdentifierNode;
  527. template <char C>
  528. struct CharacterNode : public cmGeneratorExpressionNode
  529. {
  530. CharacterNode() {} // NOLINT(modernize-use-equals-default)
  531. int NumExpectedParameters() const override { return 0; }
  532. std::string Evaluate(
  533. const std::vector<std::string>& /*parameters*/,
  534. cmGeneratorExpressionContext* /*context*/,
  535. const GeneratorExpressionContent* /*content*/,
  536. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  537. {
  538. return { C };
  539. }
  540. };
  541. static const CharacterNode<'>'> angle_rNode;
  542. static const CharacterNode<','> commaNode;
  543. static const CharacterNode<';'> semicolonNode;
  544. struct CompilerIdNode : public cmGeneratorExpressionNode
  545. {
  546. CompilerIdNode(const char* compilerLang)
  547. : CompilerLanguage(compilerLang)
  548. {
  549. }
  550. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  551. std::string Evaluate(
  552. const std::vector<std::string>& parameters,
  553. cmGeneratorExpressionContext* context,
  554. const GeneratorExpressionContent* content,
  555. cmGeneratorExpressionDAGChecker* dagChecker) const override
  556. {
  557. if (!context->HeadTarget) {
  558. std::ostringstream e;
  559. e << "$<" << this->CompilerLanguage
  560. << "_COMPILER_ID> may only be used with binary targets. It may "
  561. "not be used with add_custom_command or add_custom_target.";
  562. reportError(context, content->GetOriginalExpression(), e.str());
  563. return {};
  564. }
  565. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  566. this->CompilerLanguage);
  567. }
  568. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  569. cmGeneratorExpressionContext* context,
  570. const GeneratorExpressionContent* content,
  571. cmGeneratorExpressionDAGChecker* /*unused*/,
  572. const std::string& lang) const
  573. {
  574. std::string const& compilerId =
  575. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  576. "_COMPILER_ID");
  577. if (parameters.empty()) {
  578. return compilerId;
  579. }
  580. if (compilerId.empty()) {
  581. return parameters.front().empty() ? "1" : "0";
  582. }
  583. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  584. for (auto const& param : parameters) {
  585. if (!compilerIdValidator.find(param)) {
  586. reportError(context, content->GetOriginalExpression(),
  587. "Expression syntax not recognized.");
  588. return std::string();
  589. }
  590. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  591. return "1";
  592. }
  593. if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
  594. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  595. case cmPolicies::WARN: {
  596. context->LG->GetCMakeInstance()->IssueMessage(
  597. MessageType::AUTHOR_WARNING,
  598. cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
  599. context->Backtrace);
  600. CM_FALLTHROUGH;
  601. }
  602. case cmPolicies::OLD:
  603. return "1";
  604. case cmPolicies::NEW:
  605. case cmPolicies::REQUIRED_ALWAYS:
  606. case cmPolicies::REQUIRED_IF_USED:
  607. break;
  608. }
  609. }
  610. }
  611. return "0";
  612. }
  613. const char* const CompilerLanguage;
  614. };
  615. static const CompilerIdNode cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  616. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  617. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  618. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  619. struct CompilerVersionNode : public cmGeneratorExpressionNode
  620. {
  621. CompilerVersionNode(const char* compilerLang)
  622. : CompilerLanguage(compilerLang)
  623. {
  624. }
  625. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  626. std::string Evaluate(
  627. const std::vector<std::string>& parameters,
  628. cmGeneratorExpressionContext* context,
  629. const GeneratorExpressionContent* content,
  630. cmGeneratorExpressionDAGChecker* dagChecker) const override
  631. {
  632. if (!context->HeadTarget) {
  633. std::ostringstream e;
  634. e << "$<" << this->CompilerLanguage
  635. << "_COMPILER_VERSION> may only be used with binary targets. It "
  636. "may not be used with add_custom_command or add_custom_target.";
  637. reportError(context, content->GetOriginalExpression(), e.str());
  638. return {};
  639. }
  640. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  641. this->CompilerLanguage);
  642. }
  643. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  644. cmGeneratorExpressionContext* context,
  645. const GeneratorExpressionContent* content,
  646. cmGeneratorExpressionDAGChecker* /*unused*/,
  647. const std::string& lang) const
  648. {
  649. std::string const& compilerVersion =
  650. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  651. "_COMPILER_VERSION");
  652. if (parameters.empty()) {
  653. return compilerVersion;
  654. }
  655. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  656. if (!compilerIdValidator.find(parameters.front())) {
  657. reportError(context, content->GetOriginalExpression(),
  658. "Expression syntax not recognized.");
  659. return {};
  660. }
  661. if (compilerVersion.empty()) {
  662. return parameters.front().empty() ? "1" : "0";
  663. }
  664. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  665. parameters.front(), compilerVersion)
  666. ? "1"
  667. : "0";
  668. }
  669. const char* const CompilerLanguage;
  670. };
  671. static const CompilerVersionNode cCompilerVersionNode("C"),
  672. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  673. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  674. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  675. hipCompilerVersionNode("HIP");
  676. struct PlatformIdNode : public cmGeneratorExpressionNode
  677. {
  678. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  679. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  680. std::string Evaluate(
  681. const std::vector<std::string>& parameters,
  682. cmGeneratorExpressionContext* context,
  683. const GeneratorExpressionContent* /*content*/,
  684. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  685. {
  686. std::string const& platformId =
  687. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  688. if (parameters.empty()) {
  689. return platformId;
  690. }
  691. if (platformId.empty()) {
  692. return parameters.front().empty() ? "1" : "0";
  693. }
  694. for (auto const& param : parameters) {
  695. if (param == platformId) {
  696. return "1";
  697. }
  698. }
  699. return "0";
  700. }
  701. };
  702. static struct PlatformIdNode platformIdNode;
  703. template <cmSystemTools::CompareOp Op>
  704. struct VersionNode : public cmGeneratorExpressionNode
  705. {
  706. VersionNode() {} // NOLINT(modernize-use-equals-default)
  707. int NumExpectedParameters() const override { return 2; }
  708. std::string Evaluate(
  709. const std::vector<std::string>& parameters,
  710. cmGeneratorExpressionContext* /*context*/,
  711. const GeneratorExpressionContent* /*content*/,
  712. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  713. {
  714. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  715. ? "1"
  716. : "0";
  717. }
  718. };
  719. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  720. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  721. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  722. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  723. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  724. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  725. {
  726. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  727. std::string Evaluate(
  728. const std::vector<std::string>& parameters,
  729. cmGeneratorExpressionContext* context,
  730. const GeneratorExpressionContent* content,
  731. cmGeneratorExpressionDAGChecker* dagChecker) const override
  732. {
  733. if (!dagChecker) {
  734. reportError(context, content->GetOriginalExpression(),
  735. "$<LINK_ONLY:...> may only be used for linking");
  736. return std::string();
  737. }
  738. if (!dagChecker->GetTransitivePropertiesOnly()) {
  739. return parameters.front();
  740. }
  741. return std::string();
  742. }
  743. } linkOnlyNode;
  744. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  745. {
  746. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  747. int NumExpectedParameters() const override { return 0; }
  748. std::string Evaluate(
  749. const std::vector<std::string>& /*parameters*/,
  750. cmGeneratorExpressionContext* context,
  751. const GeneratorExpressionContent* /*content*/,
  752. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  753. {
  754. context->HadContextSensitiveCondition = true;
  755. return context->Config;
  756. }
  757. } configurationNode;
  758. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  759. {
  760. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  761. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  762. std::string Evaluate(
  763. const std::vector<std::string>& parameters,
  764. cmGeneratorExpressionContext* context,
  765. const GeneratorExpressionContent* content,
  766. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  767. {
  768. if (parameters.empty()) {
  769. return configurationNode.Evaluate(parameters, context, content, nullptr);
  770. }
  771. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  772. if (!configValidator.find(parameters.front())) {
  773. reportError(context, content->GetOriginalExpression(),
  774. "Expression syntax not recognized.");
  775. return std::string();
  776. }
  777. context->HadContextSensitiveCondition = true;
  778. for (auto const& param : parameters) {
  779. if (context->Config.empty()) {
  780. if (param.empty()) {
  781. return "1";
  782. }
  783. } else if (cmsysString_strcasecmp(param.c_str(),
  784. context->Config.c_str()) == 0) {
  785. return "1";
  786. }
  787. }
  788. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  789. cmValue loc = nullptr;
  790. cmValue imp = nullptr;
  791. std::string suffix;
  792. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  793. imp, suffix)) {
  794. // This imported target has an appropriate location
  795. // for this (possibly mapped) config.
  796. // Check if there is a proper config mapping for the tested config.
  797. std::vector<std::string> mappedConfigs;
  798. std::string mapProp = cmStrCat(
  799. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  800. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  801. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  802. for (auto const& param : parameters) {
  803. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  804. return "1";
  805. }
  806. }
  807. }
  808. }
  809. }
  810. return "0";
  811. }
  812. } configurationTestNode;
  813. static const struct JoinNode : public cmGeneratorExpressionNode
  814. {
  815. JoinNode() {} // NOLINT(modernize-use-equals-default)
  816. int NumExpectedParameters() const override { return 2; }
  817. bool AcceptsArbitraryContentParameter() const override { return true; }
  818. std::string Evaluate(
  819. const std::vector<std::string>& parameters,
  820. cmGeneratorExpressionContext* /*context*/,
  821. const GeneratorExpressionContent* /*content*/,
  822. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  823. {
  824. std::vector<std::string> list = cmExpandedList(parameters.front());
  825. return cmJoin(list, parameters[1]);
  826. }
  827. } joinNode;
  828. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  829. {
  830. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  831. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  832. std::string Evaluate(
  833. const std::vector<std::string>& parameters,
  834. cmGeneratorExpressionContext* context,
  835. const GeneratorExpressionContent* content,
  836. cmGeneratorExpressionDAGChecker* dagChecker) const override
  837. {
  838. if (context->Language.empty() &&
  839. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  840. reportError(
  841. context, content->GetOriginalExpression(),
  842. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  843. "directories, compile definitions, compile options, and to evaluate "
  844. "components of the file(GENERATE) command.");
  845. return std::string();
  846. }
  847. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  848. std::string genName = gg->GetName();
  849. if (genName.find("Makefiles") == std::string::npos &&
  850. genName.find("Ninja") == std::string::npos &&
  851. genName.find("Visual Studio") == std::string::npos &&
  852. genName.find("Xcode") == std::string::npos &&
  853. genName.find("Watcom WMake") == std::string::npos) {
  854. reportError(context, content->GetOriginalExpression(),
  855. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  856. return std::string();
  857. }
  858. if (parameters.empty()) {
  859. return context->Language;
  860. }
  861. for (auto const& param : parameters) {
  862. if (context->Language == param) {
  863. return "1";
  864. }
  865. }
  866. return "0";
  867. }
  868. } languageNode;
  869. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  870. {
  871. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  872. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  873. std::string Evaluate(
  874. const std::vector<std::string>& parameters,
  875. cmGeneratorExpressionContext* context,
  876. const GeneratorExpressionContent* content,
  877. cmGeneratorExpressionDAGChecker* dagChecker) const override
  878. {
  879. if (!context->HeadTarget ||
  880. (context->Language.empty() &&
  881. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  882. // reportError(context, content->GetOriginalExpression(), "");
  883. reportError(
  884. context, content->GetOriginalExpression(),
  885. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  886. "to specify include directories, compile definitions, and compile "
  887. "options. It may not be used with the add_custom_command, "
  888. "add_custom_target, or file(GENERATE) commands.");
  889. return std::string();
  890. }
  891. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  892. std::string genName = gg->GetName();
  893. if (genName.find("Makefiles") == std::string::npos &&
  894. genName.find("Ninja") == std::string::npos &&
  895. genName.find("Visual Studio") == std::string::npos &&
  896. genName.find("Xcode") == std::string::npos &&
  897. genName.find("Watcom WMake") == std::string::npos) {
  898. reportError(
  899. context, content->GetOriginalExpression(),
  900. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  901. return std::string();
  902. }
  903. const std::string& lang = context->Language;
  904. if (lang == parameters.front()) {
  905. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  906. parameters.cend());
  907. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  908. idParameter, context, content, dagChecker, lang);
  909. }
  910. return "0";
  911. }
  912. } languageAndIdNode;
  913. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  914. {
  915. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  916. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  917. std::string Evaluate(
  918. const std::vector<std::string>& parameters,
  919. cmGeneratorExpressionContext* context,
  920. const GeneratorExpressionContent* content,
  921. cmGeneratorExpressionDAGChecker* dagChecker) const override
  922. {
  923. if (!context->HeadTarget || !dagChecker ||
  924. !(dagChecker->EvaluatingLinkExpression() ||
  925. dagChecker->EvaluatingLinkLibraries())) {
  926. reportError(context, content->GetOriginalExpression(),
  927. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  928. "to specify link libraries, link directories, link options "
  929. "and link depends.");
  930. return std::string();
  931. }
  932. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  933. reportError(
  934. context, content->GetOriginalExpression(),
  935. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  936. return std::string();
  937. }
  938. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  939. std::string genName = gg->GetName();
  940. if (genName.find("Makefiles") == std::string::npos &&
  941. genName.find("Ninja") == std::string::npos &&
  942. genName.find("Visual Studio") == std::string::npos &&
  943. genName.find("Xcode") == std::string::npos &&
  944. genName.find("Watcom WMake") == std::string::npos) {
  945. reportError(context, content->GetOriginalExpression(),
  946. "$<LINK_LANGUAGE:...> not supported for this generator.");
  947. return std::string();
  948. }
  949. if (dagChecker->EvaluatingLinkLibraries()) {
  950. context->HadHeadSensitiveCondition = true;
  951. context->HadLinkLanguageSensitiveCondition = true;
  952. }
  953. if (parameters.empty()) {
  954. return context->Language;
  955. }
  956. for (auto const& param : parameters) {
  957. if (context->Language == param) {
  958. return "1";
  959. }
  960. }
  961. return "0";
  962. }
  963. } linkLanguageNode;
  964. namespace {
  965. struct LinkerId
  966. {
  967. static std::string Evaluate(const std::vector<std::string>& parameters,
  968. cmGeneratorExpressionContext* context,
  969. const GeneratorExpressionContent* content,
  970. const std::string& lang)
  971. {
  972. std::string const& linkerId =
  973. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  974. "_COMPILER_ID");
  975. if (parameters.empty()) {
  976. return linkerId;
  977. }
  978. if (linkerId.empty()) {
  979. return parameters.front().empty() ? "1" : "0";
  980. }
  981. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  982. for (auto const& param : parameters) {
  983. if (!linkerIdValidator.find(param)) {
  984. reportError(context, content->GetOriginalExpression(),
  985. "Expression syntax not recognized.");
  986. return std::string();
  987. }
  988. if (param == linkerId) {
  989. return "1";
  990. }
  991. }
  992. return "0";
  993. }
  994. };
  995. }
  996. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  997. {
  998. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  999. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1000. std::string Evaluate(
  1001. const std::vector<std::string>& parameters,
  1002. cmGeneratorExpressionContext* context,
  1003. const GeneratorExpressionContent* content,
  1004. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1005. {
  1006. if (!context->HeadTarget || !dagChecker ||
  1007. !(dagChecker->EvaluatingLinkExpression() ||
  1008. dagChecker->EvaluatingLinkLibraries())) {
  1009. reportError(
  1010. context, content->GetOriginalExpression(),
  1011. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1012. "to specify link libraries, link directories, link options, and link "
  1013. "depends.");
  1014. return std::string();
  1015. }
  1016. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1017. std::string genName = gg->GetName();
  1018. if (genName.find("Makefiles") == std::string::npos &&
  1019. genName.find("Ninja") == std::string::npos &&
  1020. genName.find("Visual Studio") == std::string::npos &&
  1021. genName.find("Xcode") == std::string::npos &&
  1022. genName.find("Watcom WMake") == std::string::npos) {
  1023. reportError(
  1024. context, content->GetOriginalExpression(),
  1025. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1026. return std::string();
  1027. }
  1028. if (dagChecker->EvaluatingLinkLibraries()) {
  1029. context->HadHeadSensitiveCondition = true;
  1030. context->HadLinkLanguageSensitiveCondition = true;
  1031. }
  1032. const std::string& lang = context->Language;
  1033. if (lang == parameters.front()) {
  1034. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1035. parameters.cend());
  1036. return LinkerId::Evaluate(idParameter, context, content, lang);
  1037. }
  1038. return "0";
  1039. }
  1040. } linkLanguageAndIdNode;
  1041. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  1042. {
  1043. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  1044. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1045. std::string Evaluate(
  1046. const std::vector<std::string>& parameters,
  1047. cmGeneratorExpressionContext* context,
  1048. const GeneratorExpressionContent* content,
  1049. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1050. {
  1051. if (!context->HeadTarget || !dagChecker ||
  1052. !dagChecker->EvaluatingLinkLibraries()) {
  1053. reportError(context, content->GetOriginalExpression(),
  1054. "$<LINK_LIBRARY:...> may only be used with binary targets "
  1055. "to specify link libraries.");
  1056. return std::string();
  1057. }
  1058. std::vector<std::string> list;
  1059. cmExpandLists(parameters.begin(), parameters.end(), list);
  1060. if (list.empty()) {
  1061. reportError(
  1062. context, content->GetOriginalExpression(),
  1063. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  1064. return std::string();
  1065. }
  1066. if (list.size() == 1) {
  1067. // no libraries specified, ignore this genex
  1068. return std::string();
  1069. }
  1070. auto const& feature = list.front();
  1071. const auto LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  1072. const auto LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  1073. // filter out $<LINK_LIBRARY:..> tags with same feature
  1074. // and raise an error for any different feature
  1075. cm::erase_if(list, [&](const std::string& item) -> bool {
  1076. return item == LL_BEGIN || item == LL_END;
  1077. });
  1078. auto it =
  1079. std::find_if(list.cbegin() + 1, list.cend(),
  1080. [&feature](const std::string& item) -> bool {
  1081. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  1082. item.substr(14, item.find('>', 14) - 14) != feature;
  1083. });
  1084. if (it != list.cend()) {
  1085. reportError(
  1086. context, content->GetOriginalExpression(),
  1087. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  1088. return std::string();
  1089. }
  1090. list.front() = LL_BEGIN;
  1091. list.push_back(LL_END);
  1092. return cmJoin(list, ";"_s);
  1093. }
  1094. } linkLibraryNode;
  1095. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1096. {
  1097. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1098. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1099. std::string Evaluate(
  1100. const std::vector<std::string>& parameters,
  1101. cmGeneratorExpressionContext* context,
  1102. const GeneratorExpressionContent* content,
  1103. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1104. {
  1105. if (!context->HeadTarget || !dagChecker ||
  1106. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1107. reportError(context, content->GetOriginalExpression(),
  1108. "$<HOST_LINK:...> may only be used with binary targets "
  1109. "to specify link options.");
  1110. return std::string();
  1111. }
  1112. return context->HeadTarget->IsDeviceLink() ? std::string()
  1113. : cmJoin(parameters, ";");
  1114. }
  1115. } hostLinkNode;
  1116. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1117. {
  1118. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1119. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1120. std::string Evaluate(
  1121. const std::vector<std::string>& parameters,
  1122. cmGeneratorExpressionContext* context,
  1123. const GeneratorExpressionContent* content,
  1124. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1125. {
  1126. if (!context->HeadTarget || !dagChecker ||
  1127. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1128. reportError(context, content->GetOriginalExpression(),
  1129. "$<DEVICE_LINK:...> may only be used with binary targets "
  1130. "to specify link options.");
  1131. return std::string();
  1132. }
  1133. if (context->HeadTarget->IsDeviceLink()) {
  1134. std::vector<std::string> list;
  1135. cmExpandLists(parameters.begin(), parameters.end(), list);
  1136. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1137. const auto DL_END = "</DEVICE_LINK>"_s;
  1138. cm::erase_if(list, [&](const std::string& item) {
  1139. return item == DL_BEGIN || item == DL_END;
  1140. });
  1141. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1142. list.push_back(static_cast<std::string>(DL_END));
  1143. return cmJoin(list, ";");
  1144. }
  1145. return std::string();
  1146. }
  1147. } deviceLinkNode;
  1148. static std::string getLinkedTargetsContent(
  1149. cmGeneratorTarget const* target, std::string const& prop,
  1150. cmGeneratorExpressionContext* context,
  1151. cmGeneratorExpressionDAGChecker* dagChecker)
  1152. {
  1153. std::string result;
  1154. if (cmLinkImplementationLibraries const* impl =
  1155. target->GetLinkImplementationLibraries(context->Config)) {
  1156. for (cmLinkImplItem const& lib : impl->Libraries) {
  1157. if (lib.Target) {
  1158. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1159. // caller's property and hand-evaluate it as if it were compiled.
  1160. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1161. cmGeneratorExpressionContext libContext(
  1162. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1163. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1164. context->Language);
  1165. std::string libResult =
  1166. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1167. if (!libResult.empty()) {
  1168. if (result.empty()) {
  1169. result = std::move(libResult);
  1170. } else {
  1171. result.reserve(result.size() + 1 + libResult.size());
  1172. result += ";";
  1173. result += libResult;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. return result;
  1180. }
  1181. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1182. {
  1183. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1184. // This node handles errors on parameter count itself.
  1185. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1186. static const char* GetErrorText(std::string const& targetName,
  1187. std::string const& propertyName)
  1188. {
  1189. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1190. if (targetName.empty() && propertyName.empty()) {
  1191. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1192. "target name and property name.";
  1193. }
  1194. if (targetName.empty()) {
  1195. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1196. "target name.";
  1197. }
  1198. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1199. if (!propertyNameValidator.find(propertyName)) {
  1200. return "Target name and property name not supported.";
  1201. }
  1202. return "Target name not supported.";
  1203. }
  1204. return nullptr;
  1205. }
  1206. std::string Evaluate(
  1207. const std::vector<std::string>& parameters,
  1208. cmGeneratorExpressionContext* context,
  1209. const GeneratorExpressionContent* content,
  1210. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1211. {
  1212. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1213. cmGeneratorTarget const* target = nullptr;
  1214. std::string targetName;
  1215. std::string propertyName;
  1216. if (parameters.size() == 2) {
  1217. targetName = parameters[0];
  1218. propertyName = parameters[1];
  1219. if (const char* e = GetErrorText(targetName, propertyName)) {
  1220. reportError(context, content->GetOriginalExpression(), e);
  1221. return std::string();
  1222. }
  1223. if (propertyName == "ALIASED_TARGET"_s) {
  1224. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1225. if (cmGeneratorTarget* tgt =
  1226. context->LG->FindGeneratorTargetToUse(targetName)) {
  1227. return tgt->GetName();
  1228. }
  1229. }
  1230. return std::string();
  1231. }
  1232. if (propertyName == "ALIAS_GLOBAL"_s) {
  1233. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1234. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1235. ? "TRUE"
  1236. : "FALSE";
  1237. }
  1238. return std::string();
  1239. }
  1240. target = context->LG->FindGeneratorTargetToUse(targetName);
  1241. if (!target) {
  1242. std::ostringstream e;
  1243. e << "Target \"" << targetName << "\" not found.";
  1244. reportError(context, content->GetOriginalExpression(), e.str());
  1245. return std::string();
  1246. }
  1247. context->AllTargets.insert(target);
  1248. } else if (parameters.size() == 1) {
  1249. target = context->HeadTarget;
  1250. propertyName = parameters[0];
  1251. // Keep track of the properties seen while processing.
  1252. // The evaluation of the LINK_LIBRARIES generator expressions
  1253. // will check this to ensure that properties have one consistent
  1254. // value for all evaluations.
  1255. context->SeenTargetProperties.insert(propertyName);
  1256. context->HadHeadSensitiveCondition = true;
  1257. if (!target) {
  1258. reportError(
  1259. context, content->GetOriginalExpression(),
  1260. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1261. "It may not be used with add_custom_command or add_custom_target. "
  1262. " "
  1263. "Specify the target to read a property from using the "
  1264. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1265. return std::string();
  1266. }
  1267. } else {
  1268. reportError(
  1269. context, content->GetOriginalExpression(),
  1270. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1271. return std::string();
  1272. }
  1273. if (propertyName == "SOURCES") {
  1274. context->SourceSensitiveTargets.insert(target);
  1275. }
  1276. if (propertyName.empty()) {
  1277. reportError(
  1278. context, content->GetOriginalExpression(),
  1279. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1280. "name.");
  1281. return std::string();
  1282. }
  1283. if (!propertyNameValidator.find(propertyName)) {
  1284. ::reportError(context, content->GetOriginalExpression(),
  1285. "Property name not supported.");
  1286. return std::string();
  1287. }
  1288. assert(target);
  1289. if (propertyName == "LINKER_LANGUAGE") {
  1290. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1291. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1292. dagCheckerParent->EvaluatingSources())) {
  1293. reportError(
  1294. context, content->GetOriginalExpression(),
  1295. "LINKER_LANGUAGE target property can not be used while evaluating "
  1296. "link libraries for a static library");
  1297. return std::string();
  1298. }
  1299. return target->GetLinkerLanguage(context->Config);
  1300. }
  1301. std::string interfacePropertyName;
  1302. bool isInterfaceProperty = false;
  1303. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1304. if (propertyName == #prop) { \
  1305. interfacePropertyName = "INTERFACE_" #prop; \
  1306. } else if (propertyName == "INTERFACE_" #prop) { \
  1307. interfacePropertyName = "INTERFACE_" #prop; \
  1308. isInterfaceProperty = true; \
  1309. } else
  1310. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1311. // Note that the above macro terminates with an else
  1312. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1313. cmPolicies::PolicyStatus polSt =
  1314. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1315. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1316. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1317. }
  1318. }
  1319. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1320. bool evaluatingLinkLibraries = false;
  1321. if (dagCheckerParent) {
  1322. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1323. dagCheckerParent->EvaluatingPICExpression()) {
  1324. // No check required.
  1325. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1326. evaluatingLinkLibraries = true;
  1327. if (!interfacePropertyName.empty()) {
  1328. reportError(
  1329. context, content->GetOriginalExpression(),
  1330. "$<TARGET_PROPERTY:...> expression in link libraries "
  1331. "evaluation depends on target property which is transitive "
  1332. "over the link libraries, creating a recursion.");
  1333. return std::string();
  1334. }
  1335. } else {
  1336. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1337. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1338. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1339. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1340. }
  1341. }
  1342. if (isInterfaceProperty) {
  1343. return cmGeneratorExpression::StripEmptyListElements(
  1344. target->EvaluateInterfaceProperty(propertyName, context,
  1345. dagCheckerParent));
  1346. }
  1347. cmGeneratorExpressionDAGChecker dagChecker(
  1348. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1349. switch (dagChecker.Check()) {
  1350. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1351. dagChecker.ReportError(context, content->GetOriginalExpression());
  1352. return std::string();
  1353. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1354. // No error. We just skip cyclic references.
  1355. return std::string();
  1356. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1357. // We handle transitive properties above. For non-transitive
  1358. // properties we accept repeats anyway.
  1359. case cmGeneratorExpressionDAGChecker::DAG:
  1360. break;
  1361. }
  1362. std::string result;
  1363. bool haveProp = false;
  1364. if (cmValue p = target->GetProperty(propertyName)) {
  1365. result = *p;
  1366. haveProp = true;
  1367. } else if (evaluatingLinkLibraries) {
  1368. return std::string();
  1369. }
  1370. if (!haveProp && !target->IsImported() &&
  1371. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1372. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1373. context->Config)) {
  1374. context->HadContextSensitiveCondition = true;
  1375. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1376. context->Config)
  1377. ? "1"
  1378. : "0";
  1379. }
  1380. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1381. context->Config)) {
  1382. context->HadContextSensitiveCondition = true;
  1383. const char* propContent =
  1384. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1385. context->Config);
  1386. return propContent ? propContent : "";
  1387. }
  1388. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1389. context->Config)) {
  1390. context->HadContextSensitiveCondition = true;
  1391. const char* propContent =
  1392. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1393. context->Config);
  1394. return propContent ? propContent : "";
  1395. }
  1396. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1397. context->Config)) {
  1398. context->HadContextSensitiveCondition = true;
  1399. const char* propContent =
  1400. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1401. context->Config);
  1402. return propContent ? propContent : "";
  1403. }
  1404. }
  1405. if (!target->IsImported() && dagCheckerParent &&
  1406. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1407. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1408. context->Config)) {
  1409. context->HadContextSensitiveCondition = true;
  1410. const char* propContent =
  1411. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1412. context->Config);
  1413. return propContent ? propContent : "";
  1414. }
  1415. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1416. context->Config)) {
  1417. context->HadContextSensitiveCondition = true;
  1418. const char* propContent =
  1419. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1420. context->Config);
  1421. return propContent ? propContent : "";
  1422. }
  1423. }
  1424. if (!interfacePropertyName.empty()) {
  1425. result = cmGeneratorExpression::StripEmptyListElements(
  1426. this->EvaluateDependentExpression(result, context->LG, context, target,
  1427. &dagChecker, target));
  1428. std::string linkedTargetsContent = getLinkedTargetsContent(
  1429. target, interfacePropertyName, context, &dagChecker);
  1430. if (!linkedTargetsContent.empty()) {
  1431. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1432. }
  1433. }
  1434. return result;
  1435. }
  1436. } targetPropertyNode;
  1437. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1438. {
  1439. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1440. bool GeneratesContent() const override { return true; }
  1441. bool AcceptsArbitraryContentParameter() const override { return true; }
  1442. bool RequiresLiteralInput() const override { return true; }
  1443. std::string Evaluate(
  1444. const std::vector<std::string>& parameters,
  1445. cmGeneratorExpressionContext* /*context*/,
  1446. const GeneratorExpressionContent* /*content*/,
  1447. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1448. {
  1449. return parameters.front();
  1450. }
  1451. int NumExpectedParameters() const override { return 1; }
  1452. } targetNameNode;
  1453. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1454. {
  1455. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1456. std::string Evaluate(
  1457. const std::vector<std::string>& parameters,
  1458. cmGeneratorExpressionContext* context,
  1459. const GeneratorExpressionContent* content,
  1460. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1461. {
  1462. std::string tgtName = parameters.front();
  1463. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1464. if (!gt) {
  1465. std::ostringstream e;
  1466. e << "Objects of target \"" << tgtName
  1467. << "\" referenced but no such target exists.";
  1468. reportError(context, content->GetOriginalExpression(), e.str());
  1469. return std::string();
  1470. }
  1471. cmStateEnums::TargetType type = gt->GetType();
  1472. if (type != cmStateEnums::EXECUTABLE &&
  1473. type != cmStateEnums::STATIC_LIBRARY &&
  1474. type != cmStateEnums::SHARED_LIBRARY &&
  1475. type != cmStateEnums::MODULE_LIBRARY &&
  1476. type != cmStateEnums::OBJECT_LIBRARY) {
  1477. std::ostringstream e;
  1478. e << "Objects of target \"" << tgtName
  1479. << "\" referenced but is not one of the allowed target types "
  1480. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  1481. reportError(context, content->GetOriginalExpression(), e.str());
  1482. return std::string();
  1483. }
  1484. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1485. {
  1486. std::string reason;
  1487. if (!context->EvaluateForBuildsystem &&
  1488. !gg->HasKnownObjectFileLocation(&reason)) {
  1489. std::ostringstream e;
  1490. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1491. "is only suitable for consumption by CMake (limited"
  1492. << reason
  1493. << "). "
  1494. "It is not suitable for writing out elsewhere.";
  1495. reportError(context, content->GetOriginalExpression(), e.str());
  1496. return std::string();
  1497. }
  1498. }
  1499. std::vector<std::string> objects;
  1500. if (gt->IsImported()) {
  1501. cmValue loc = nullptr;
  1502. cmValue imp = nullptr;
  1503. std::string suffix;
  1504. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  1505. cmExpandList(*loc, objects);
  1506. }
  1507. context->HadContextSensitiveCondition = true;
  1508. } else {
  1509. gt->GetTargetObjectNames(context->Config, objects);
  1510. std::string obj_dir;
  1511. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  1512. // Use object file directory with buildsystem placeholder.
  1513. obj_dir = gt->ObjectDirectory;
  1514. context->HadContextSensitiveCondition =
  1515. gt->HasContextDependentSources();
  1516. } else {
  1517. // Use object file directory with per-config location.
  1518. obj_dir = gt->GetObjectDirectory(context->Config);
  1519. context->HadContextSensitiveCondition = true;
  1520. }
  1521. for (std::string& o : objects) {
  1522. o = cmStrCat(obj_dir, o);
  1523. }
  1524. }
  1525. // Create the cmSourceFile instances in the referencing directory.
  1526. cmMakefile* mf = context->LG->GetMakefile();
  1527. for (std::string const& o : objects) {
  1528. mf->AddTargetObject(tgtName, o);
  1529. }
  1530. return cmJoin(objects, ";");
  1531. }
  1532. } targetObjectsNode;
  1533. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  1534. {
  1535. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  1536. std::string Evaluate(
  1537. const std::vector<std::string>& parameters,
  1538. cmGeneratorExpressionContext* context,
  1539. const GeneratorExpressionContent* content,
  1540. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1541. {
  1542. std::string tgtName = parameters.front();
  1543. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1544. if (!gt) {
  1545. std::ostringstream e;
  1546. e << "Objects of target \"" << tgtName
  1547. << "\" referenced but no such target exists.";
  1548. reportError(context, content->GetOriginalExpression(), e.str());
  1549. return std::string();
  1550. }
  1551. cmStateEnums::TargetType type = gt->GetType();
  1552. if (type != cmStateEnums::EXECUTABLE &&
  1553. type != cmStateEnums::SHARED_LIBRARY &&
  1554. type != cmStateEnums::MODULE_LIBRARY) {
  1555. std::ostringstream e;
  1556. e << "Objects of target \"" << tgtName
  1557. << "\" referenced but is not one of the allowed target types "
  1558. << "(EXECUTABLE, SHARED, MODULE).";
  1559. reportError(context, content->GetOriginalExpression(), e.str());
  1560. return std::string();
  1561. }
  1562. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  1563. std::vector<std::string> dllPaths;
  1564. auto const& dlls = cli->GetRuntimeDLLs();
  1565. for (auto const& dll : dlls) {
  1566. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  1567. dllPaths.emplace_back(*loc);
  1568. }
  1569. }
  1570. return cmJoin(dllPaths, ";");
  1571. }
  1572. return "";
  1573. }
  1574. } targetRuntimeDllsNode;
  1575. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1576. {
  1577. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1578. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1579. std::string Evaluate(
  1580. const std::vector<std::string>& parameters,
  1581. cmGeneratorExpressionContext* context,
  1582. const GeneratorExpressionContent* content,
  1583. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1584. {
  1585. cmGeneratorTarget const* target = context->HeadTarget;
  1586. if (!target) {
  1587. reportError(
  1588. context, content->GetOriginalExpression(),
  1589. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1590. "not be used with add_custom_command or add_custom_target.");
  1591. return std::string();
  1592. }
  1593. context->HadHeadSensitiveCondition = true;
  1594. using LangMap = std::map<std::string, std::vector<std::string>>;
  1595. static LangMap availableFeatures;
  1596. LangMap testedFeatures;
  1597. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  1598. for (std::string const& p : parameters) {
  1599. std::string error;
  1600. std::string lang;
  1601. if (!standardResolver.CompileFeatureKnown(
  1602. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  1603. reportError(context, content->GetOriginalExpression(), error);
  1604. return std::string();
  1605. }
  1606. testedFeatures[lang].push_back(p);
  1607. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1608. cmValue featuresKnown =
  1609. standardResolver.CompileFeaturesAvailable(lang, &error);
  1610. if (!featuresKnown) {
  1611. reportError(context, content->GetOriginalExpression(), error);
  1612. return std::string();
  1613. }
  1614. cmExpandList(featuresKnown, availableFeatures[lang]);
  1615. }
  1616. }
  1617. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1618. for (auto const& lit : testedFeatures) {
  1619. std::vector<std::string> const& langAvailable =
  1620. availableFeatures[lit.first];
  1621. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  1622. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1623. for (std::string const& it : lit.second) {
  1624. if (!cm::contains(langAvailable, it)) {
  1625. return "0";
  1626. }
  1627. if (standardDefault && standardDefault->empty()) {
  1628. // This compiler has no notion of language standard levels.
  1629. // All features known for the language are always available.
  1630. continue;
  1631. }
  1632. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  1633. context->Config, it)) {
  1634. if (evalLL) {
  1635. cmValue l =
  1636. target->GetLanguageStandard(lit.first, context->Config);
  1637. if (!l) {
  1638. l = standardDefault;
  1639. }
  1640. assert(l);
  1641. context->MaxLanguageStandard[target][lit.first] = *l;
  1642. } else {
  1643. return "0";
  1644. }
  1645. }
  1646. }
  1647. }
  1648. return "1";
  1649. }
  1650. } compileFeaturesNode;
  1651. static const char* targetPolicyWhitelist[] = {
  1652. nullptr
  1653. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1654. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1655. #undef TARGET_POLICY_STRING
  1656. };
  1657. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1658. const char* policy)
  1659. {
  1660. #define RETURN_POLICY(POLICY) \
  1661. if (strcmp(policy, #POLICY) == 0) { \
  1662. return tgt->GetPolicyStatus##POLICY(); \
  1663. }
  1664. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1665. #undef RETURN_POLICY
  1666. assert(false && "Unreachable code. Not a valid policy");
  1667. return cmPolicies::WARN;
  1668. }
  1669. static cmPolicies::PolicyID policyForString(const char* policy_id)
  1670. {
  1671. #define RETURN_POLICY_ID(POLICY_ID) \
  1672. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1673. return cmPolicies::POLICY_ID; \
  1674. }
  1675. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1676. #undef RETURN_POLICY_ID
  1677. assert(false && "Unreachable code. Not a valid policy");
  1678. return cmPolicies::CMP0002;
  1679. }
  1680. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1681. {
  1682. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1683. int NumExpectedParameters() const override { return 1; }
  1684. std::string Evaluate(
  1685. const std::vector<std::string>& parameters,
  1686. cmGeneratorExpressionContext* context,
  1687. const GeneratorExpressionContent* content,
  1688. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1689. {
  1690. if (!context->HeadTarget) {
  1691. reportError(
  1692. context, content->GetOriginalExpression(),
  1693. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1694. "may not be used with add_custom_command or add_custom_target.");
  1695. return std::string();
  1696. }
  1697. context->HadContextSensitiveCondition = true;
  1698. context->HadHeadSensitiveCondition = true;
  1699. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1700. const char* policy = targetPolicyWhitelist[i];
  1701. if (parameters.front() == policy) {
  1702. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1703. switch (statusForTarget(context->HeadTarget, policy)) {
  1704. case cmPolicies::WARN:
  1705. lg->IssueMessage(
  1706. MessageType::AUTHOR_WARNING,
  1707. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1708. CM_FALLTHROUGH;
  1709. case cmPolicies::REQUIRED_IF_USED:
  1710. case cmPolicies::REQUIRED_ALWAYS:
  1711. case cmPolicies::OLD:
  1712. return "0";
  1713. case cmPolicies::NEW:
  1714. return "1";
  1715. }
  1716. }
  1717. }
  1718. reportError(
  1719. context, content->GetOriginalExpression(),
  1720. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1721. "policies. Currently it may be used with the following policies:\n"
  1722. #define STRINGIFY_HELPER(X) #X
  1723. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1724. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1725. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1726. #undef TARGET_POLICY_LIST_ITEM
  1727. );
  1728. return std::string();
  1729. }
  1730. } targetPolicyNode;
  1731. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1732. {
  1733. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1734. bool GeneratesContent() const override { return true; }
  1735. int NumExpectedParameters() const override { return 0; }
  1736. std::string Evaluate(
  1737. const std::vector<std::string>& /*parameters*/,
  1738. cmGeneratorExpressionContext* context,
  1739. const GeneratorExpressionContent* content,
  1740. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1741. {
  1742. reportError(context, content->GetOriginalExpression(),
  1743. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1744. "should never be evaluated.");
  1745. return std::string();
  1746. }
  1747. } installPrefixNode;
  1748. class ArtifactDirTag;
  1749. class ArtifactLinkerTag;
  1750. class ArtifactNameTag;
  1751. class ArtifactPathTag;
  1752. class ArtifactPdbTag;
  1753. class ArtifactSonameTag;
  1754. class ArtifactBundleDirTag;
  1755. class ArtifactBundleContentDirTag;
  1756. template <typename ArtifactT, typename ComponentT>
  1757. struct TargetFilesystemArtifactDependency
  1758. {
  1759. static void AddDependency(cmGeneratorTarget* target,
  1760. cmGeneratorExpressionContext* context)
  1761. {
  1762. context->DependTargets.insert(target);
  1763. context->AllTargets.insert(target);
  1764. }
  1765. };
  1766. struct TargetFilesystemArtifactDependencyCMP0112
  1767. {
  1768. static void AddDependency(cmGeneratorTarget* target,
  1769. cmGeneratorExpressionContext* context)
  1770. {
  1771. context->AllTargets.insert(target);
  1772. cmLocalGenerator* lg = context->LG;
  1773. switch (target->GetPolicyStatusCMP0112()) {
  1774. case cmPolicies::WARN:
  1775. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  1776. "CMAKE_POLICY_WARNING_CMP0112")) {
  1777. std::string err =
  1778. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  1779. "\nDependency being added to target:\n \"",
  1780. target->GetName(), "\"\n");
  1781. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  1782. err, context->Backtrace);
  1783. }
  1784. CM_FALLTHROUGH;
  1785. case cmPolicies::OLD:
  1786. context->DependTargets.insert(target);
  1787. break;
  1788. case cmPolicies::REQUIRED_IF_USED:
  1789. case cmPolicies::REQUIRED_ALWAYS:
  1790. case cmPolicies::NEW:
  1791. break;
  1792. }
  1793. }
  1794. };
  1795. template <typename ArtifactT>
  1796. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  1797. : TargetFilesystemArtifactDependencyCMP0112
  1798. {
  1799. };
  1800. template <typename ArtifactT>
  1801. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  1802. : TargetFilesystemArtifactDependencyCMP0112
  1803. {
  1804. };
  1805. template <>
  1806. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  1807. ArtifactPathTag>
  1808. : TargetFilesystemArtifactDependencyCMP0112
  1809. {
  1810. };
  1811. template <>
  1812. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  1813. ArtifactPathTag>
  1814. : TargetFilesystemArtifactDependencyCMP0112
  1815. {
  1816. };
  1817. template <typename ArtifactT>
  1818. struct TargetFilesystemArtifactResultCreator
  1819. {
  1820. static std::string Create(cmGeneratorTarget* target,
  1821. cmGeneratorExpressionContext* context,
  1822. const GeneratorExpressionContent* content);
  1823. };
  1824. template <>
  1825. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1826. {
  1827. static std::string Create(cmGeneratorTarget* target,
  1828. cmGeneratorExpressionContext* context,
  1829. const GeneratorExpressionContent* content)
  1830. {
  1831. // The target soname file (.so.1).
  1832. if (target->IsDLLPlatform()) {
  1833. ::reportError(context, content->GetOriginalExpression(),
  1834. "TARGET_SONAME_FILE is not allowed "
  1835. "for DLL target platforms.");
  1836. return std::string();
  1837. }
  1838. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1839. ::reportError(context, content->GetOriginalExpression(),
  1840. "TARGET_SONAME_FILE is allowed only for "
  1841. "SHARED libraries.");
  1842. return std::string();
  1843. }
  1844. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  1845. target->GetSOName(context->Config));
  1846. return result;
  1847. }
  1848. };
  1849. template <>
  1850. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1851. {
  1852. static std::string Create(cmGeneratorTarget* target,
  1853. cmGeneratorExpressionContext* context,
  1854. const GeneratorExpressionContent* content)
  1855. {
  1856. if (target->IsImported()) {
  1857. ::reportError(context, content->GetOriginalExpression(),
  1858. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1859. return std::string();
  1860. }
  1861. std::string language = target->GetLinkerLanguage(context->Config);
  1862. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1863. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1864. ::reportError(context, content->GetOriginalExpression(),
  1865. "TARGET_PDB_FILE is not supported by the target linker.");
  1866. return std::string();
  1867. }
  1868. cmStateEnums::TargetType targetType = target->GetType();
  1869. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1870. targetType != cmStateEnums::MODULE_LIBRARY &&
  1871. targetType != cmStateEnums::EXECUTABLE) {
  1872. ::reportError(context, content->GetOriginalExpression(),
  1873. "TARGET_PDB_FILE is allowed only for "
  1874. "targets with linker created artifacts.");
  1875. return std::string();
  1876. }
  1877. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  1878. '/', target->GetPDBName(context->Config));
  1879. return result;
  1880. }
  1881. };
  1882. template <>
  1883. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1884. {
  1885. static std::string Create(cmGeneratorTarget* target,
  1886. cmGeneratorExpressionContext* context,
  1887. const GeneratorExpressionContent* content)
  1888. {
  1889. // The file used to link to the target (.so, .lib, .a).
  1890. if (!target->IsLinkable()) {
  1891. ::reportError(context, content->GetOriginalExpression(),
  1892. "TARGET_LINKER_FILE is allowed only for libraries and "
  1893. "executables with ENABLE_EXPORTS.");
  1894. return std::string();
  1895. }
  1896. cmStateEnums::ArtifactType artifact =
  1897. target->HasImportLibrary(context->Config)
  1898. ? cmStateEnums::ImportLibraryArtifact
  1899. : cmStateEnums::RuntimeBinaryArtifact;
  1900. return target->GetFullPath(context->Config, artifact);
  1901. }
  1902. };
  1903. template <>
  1904. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1905. {
  1906. static std::string Create(cmGeneratorTarget* target,
  1907. cmGeneratorExpressionContext* context,
  1908. const GeneratorExpressionContent* content)
  1909. {
  1910. if (target->IsImported()) {
  1911. ::reportError(context, content->GetOriginalExpression(),
  1912. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1913. return std::string();
  1914. }
  1915. if (!target->IsBundleOnApple()) {
  1916. ::reportError(context, content->GetOriginalExpression(),
  1917. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1918. return std::string();
  1919. }
  1920. std::string outpath = target->GetDirectory(context->Config) + '/';
  1921. return target->BuildBundleDirectory(outpath, context->Config,
  1922. cmGeneratorTarget::BundleDirLevel);
  1923. }
  1924. };
  1925. template <>
  1926. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1927. {
  1928. static std::string Create(cmGeneratorTarget* target,
  1929. cmGeneratorExpressionContext* context,
  1930. const GeneratorExpressionContent* content)
  1931. {
  1932. if (target->IsImported()) {
  1933. ::reportError(
  1934. context, content->GetOriginalExpression(),
  1935. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1936. return std::string();
  1937. }
  1938. if (!target->IsBundleOnApple()) {
  1939. ::reportError(
  1940. context, content->GetOriginalExpression(),
  1941. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1942. return std::string();
  1943. }
  1944. std::string outpath = target->GetDirectory(context->Config) + '/';
  1945. return target->BuildBundleDirectory(outpath, context->Config,
  1946. cmGeneratorTarget::ContentLevel);
  1947. }
  1948. };
  1949. template <>
  1950. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1951. {
  1952. static std::string Create(cmGeneratorTarget* target,
  1953. cmGeneratorExpressionContext* context,
  1954. const GeneratorExpressionContent* /*unused*/)
  1955. {
  1956. return target->GetFullPath(context->Config,
  1957. cmStateEnums::RuntimeBinaryArtifact, true);
  1958. }
  1959. };
  1960. template <typename ArtifactT>
  1961. struct TargetFilesystemArtifactResultGetter
  1962. {
  1963. static std::string Get(const std::string& result);
  1964. };
  1965. template <>
  1966. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1967. {
  1968. static std::string Get(const std::string& result)
  1969. {
  1970. return cmSystemTools::GetFilenameName(result);
  1971. }
  1972. };
  1973. template <>
  1974. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1975. {
  1976. static std::string Get(const std::string& result)
  1977. {
  1978. return cmSystemTools::GetFilenamePath(result);
  1979. }
  1980. };
  1981. template <>
  1982. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1983. {
  1984. static std::string Get(const std::string& result) { return result; }
  1985. };
  1986. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1987. {
  1988. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1989. protected:
  1990. cmGeneratorTarget* GetTarget(
  1991. const std::vector<std::string>& parameters,
  1992. cmGeneratorExpressionContext* context,
  1993. const GeneratorExpressionContent* content,
  1994. cmGeneratorExpressionDAGChecker* dagChecker) const
  1995. {
  1996. // Lookup the referenced target.
  1997. std::string name = parameters.front();
  1998. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1999. ::reportError(context, content->GetOriginalExpression(),
  2000. "Expression syntax not recognized.");
  2001. return nullptr;
  2002. }
  2003. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  2004. if (!target) {
  2005. ::reportError(context, content->GetOriginalExpression(),
  2006. "No target \"" + name + "\"");
  2007. return nullptr;
  2008. }
  2009. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  2010. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  2011. ::reportError(context, content->GetOriginalExpression(),
  2012. "Target \"" + name +
  2013. "\" is not an executable or library.");
  2014. return nullptr;
  2015. }
  2016. if (dagChecker &&
  2017. (dagChecker->EvaluatingLinkLibraries(target) ||
  2018. (dagChecker->EvaluatingSources() &&
  2019. target == dagChecker->TopTarget()))) {
  2020. ::reportError(context, content->GetOriginalExpression(),
  2021. "Expressions which require the linker language may not "
  2022. "be used while evaluating link libraries");
  2023. return nullptr;
  2024. }
  2025. return target;
  2026. }
  2027. };
  2028. template <typename ArtifactT, typename ComponentT>
  2029. struct TargetFilesystemArtifact : public TargetArtifactBase
  2030. {
  2031. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  2032. int NumExpectedParameters() const override { return 1; }
  2033. std::string Evaluate(
  2034. const std::vector<std::string>& parameters,
  2035. cmGeneratorExpressionContext* context,
  2036. const GeneratorExpressionContent* content,
  2037. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2038. {
  2039. cmGeneratorTarget* target =
  2040. this->GetTarget(parameters, context, content, dagChecker);
  2041. if (!target) {
  2042. return std::string();
  2043. }
  2044. // Not a dependent target if we are querying for ArtifactDirTag,
  2045. // ArtifactNameTag, ArtifactBundleDirTag, and ArtifactBundleContentDirTag
  2046. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  2047. target, context);
  2048. std::string result =
  2049. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  2050. content);
  2051. if (context->HadError) {
  2052. return std::string();
  2053. }
  2054. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2055. }
  2056. };
  2057. template <typename ArtifactT>
  2058. struct TargetFilesystemArtifactNodeGroup
  2059. {
  2060. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2061. {
  2062. }
  2063. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2064. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2065. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2066. };
  2067. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2068. targetNodeGroup;
  2069. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2070. targetLinkerNodeGroup;
  2071. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2072. targetSoNameNodeGroup;
  2073. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2074. targetPdbNodeGroup;
  2075. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2076. targetBundleDirNode;
  2077. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2078. ArtifactPathTag>
  2079. targetBundleContentDirNode;
  2080. //
  2081. // To retrieve base name for various artifacts
  2082. //
  2083. template <typename ArtifactT>
  2084. struct TargetOutputNameArtifactResultGetter
  2085. {
  2086. static std::string Get(cmGeneratorTarget* target,
  2087. cmGeneratorExpressionContext* context,
  2088. const GeneratorExpressionContent* content);
  2089. };
  2090. template <>
  2091. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2092. {
  2093. static std::string Get(cmGeneratorTarget* target,
  2094. cmGeneratorExpressionContext* context,
  2095. const GeneratorExpressionContent* /*unused*/)
  2096. {
  2097. return target->GetOutputName(context->Config,
  2098. cmStateEnums::RuntimeBinaryArtifact) +
  2099. target->GetFilePostfix(context->Config);
  2100. }
  2101. };
  2102. template <>
  2103. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2104. {
  2105. static std::string Get(cmGeneratorTarget* target,
  2106. cmGeneratorExpressionContext* context,
  2107. const GeneratorExpressionContent* content)
  2108. {
  2109. // The file used to link to the target (.so, .lib, .a).
  2110. if (!target->IsLinkable()) {
  2111. ::reportError(context, content->GetOriginalExpression(),
  2112. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2113. "libraries and executables with ENABLE_EXPORTS.");
  2114. return std::string();
  2115. }
  2116. cmStateEnums::ArtifactType artifact =
  2117. target->HasImportLibrary(context->Config)
  2118. ? cmStateEnums::ImportLibraryArtifact
  2119. : cmStateEnums::RuntimeBinaryArtifact;
  2120. return target->GetOutputName(context->Config, artifact) +
  2121. target->GetFilePostfix(context->Config);
  2122. }
  2123. };
  2124. template <>
  2125. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2126. {
  2127. static std::string Get(cmGeneratorTarget* target,
  2128. cmGeneratorExpressionContext* context,
  2129. const GeneratorExpressionContent* content)
  2130. {
  2131. if (target->IsImported()) {
  2132. ::reportError(
  2133. context, content->GetOriginalExpression(),
  2134. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2135. return std::string();
  2136. }
  2137. std::string language = target->GetLinkerLanguage(context->Config);
  2138. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2139. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2140. ::reportError(
  2141. context, content->GetOriginalExpression(),
  2142. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2143. return std::string();
  2144. }
  2145. cmStateEnums::TargetType targetType = target->GetType();
  2146. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2147. targetType != cmStateEnums::MODULE_LIBRARY &&
  2148. targetType != cmStateEnums::EXECUTABLE) {
  2149. ::reportError(context, content->GetOriginalExpression(),
  2150. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2151. "targets with linker created artifacts.");
  2152. return std::string();
  2153. }
  2154. return target->GetPDBOutputName(context->Config) +
  2155. target->GetFilePostfix(context->Config);
  2156. }
  2157. };
  2158. template <typename ArtifactT>
  2159. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2160. {
  2161. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2162. int NumExpectedParameters() const override { return 1; }
  2163. std::string Evaluate(
  2164. const std::vector<std::string>& parameters,
  2165. cmGeneratorExpressionContext* context,
  2166. const GeneratorExpressionContent* content,
  2167. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2168. {
  2169. cmGeneratorTarget* target =
  2170. this->GetTarget(parameters, context, content, dagChecker);
  2171. if (!target) {
  2172. return std::string();
  2173. }
  2174. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2175. target, context, content);
  2176. if (context->HadError) {
  2177. return std::string();
  2178. }
  2179. return result;
  2180. }
  2181. };
  2182. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2183. targetFileBaseNameNode;
  2184. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2185. targetLinkerFileBaseNameNode;
  2186. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2187. targetPdbFileBaseNameNode;
  2188. class ArtifactFilePrefixTag;
  2189. class ArtifactLinkerFilePrefixTag;
  2190. class ArtifactFileSuffixTag;
  2191. class ArtifactLinkerFileSuffixTag;
  2192. template <typename ArtifactT>
  2193. struct TargetFileArtifactResultGetter
  2194. {
  2195. static std::string Get(cmGeneratorTarget* target,
  2196. cmGeneratorExpressionContext* context,
  2197. const GeneratorExpressionContent* content);
  2198. };
  2199. template <>
  2200. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2201. {
  2202. static std::string Get(cmGeneratorTarget* target,
  2203. cmGeneratorExpressionContext* context,
  2204. const GeneratorExpressionContent*)
  2205. {
  2206. return target->GetFilePrefix(context->Config);
  2207. }
  2208. };
  2209. template <>
  2210. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2211. {
  2212. static std::string Get(cmGeneratorTarget* target,
  2213. cmGeneratorExpressionContext* context,
  2214. const GeneratorExpressionContent* content)
  2215. {
  2216. if (!target->IsLinkable()) {
  2217. ::reportError(context, content->GetOriginalExpression(),
  2218. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2219. "executables with ENABLE_EXPORTS.");
  2220. return std::string();
  2221. }
  2222. cmStateEnums::ArtifactType artifact =
  2223. target->HasImportLibrary(context->Config)
  2224. ? cmStateEnums::ImportLibraryArtifact
  2225. : cmStateEnums::RuntimeBinaryArtifact;
  2226. return target->GetFilePrefix(context->Config, artifact);
  2227. }
  2228. };
  2229. template <>
  2230. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2231. {
  2232. static std::string Get(cmGeneratorTarget* target,
  2233. cmGeneratorExpressionContext* context,
  2234. const GeneratorExpressionContent*)
  2235. {
  2236. return target->GetFileSuffix(context->Config);
  2237. }
  2238. };
  2239. template <>
  2240. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2241. {
  2242. static std::string Get(cmGeneratorTarget* target,
  2243. cmGeneratorExpressionContext* context,
  2244. const GeneratorExpressionContent* content)
  2245. {
  2246. if (!target->IsLinkable()) {
  2247. ::reportError(context, content->GetOriginalExpression(),
  2248. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2249. "executables with ENABLE_EXPORTS.");
  2250. return std::string();
  2251. }
  2252. cmStateEnums::ArtifactType artifact =
  2253. target->HasImportLibrary(context->Config)
  2254. ? cmStateEnums::ImportLibraryArtifact
  2255. : cmStateEnums::RuntimeBinaryArtifact;
  2256. return target->GetFileSuffix(context->Config, artifact);
  2257. }
  2258. };
  2259. template <typename ArtifactT>
  2260. struct TargetFileArtifact : public TargetArtifactBase
  2261. {
  2262. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2263. int NumExpectedParameters() const override { return 1; }
  2264. std::string Evaluate(
  2265. const std::vector<std::string>& parameters,
  2266. cmGeneratorExpressionContext* context,
  2267. const GeneratorExpressionContent* content,
  2268. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2269. {
  2270. cmGeneratorTarget* target =
  2271. this->GetTarget(parameters, context, content, dagChecker);
  2272. if (!target) {
  2273. return std::string();
  2274. }
  2275. std::string result =
  2276. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2277. if (context->HadError) {
  2278. return std::string();
  2279. }
  2280. return result;
  2281. }
  2282. };
  2283. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2284. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2285. targetLinkerFilePrefixNode;
  2286. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2287. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2288. targetLinkerFileSuffixNode;
  2289. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2290. {
  2291. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2292. std::string Evaluate(
  2293. const std::vector<std::string>& parameters,
  2294. cmGeneratorExpressionContext* context,
  2295. const GeneratorExpressionContent* content,
  2296. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2297. {
  2298. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2299. if (listIn.empty()) {
  2300. reportError(context, content->GetOriginalExpression(),
  2301. "\"\" is not an absolute path.");
  2302. return std::string();
  2303. }
  2304. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2305. cmOutputConverter converter(snapshot);
  2306. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2307. std::vector<std::string> listOut;
  2308. listOut.reserve(listIn.size());
  2309. for (auto const& in : listIn) {
  2310. if (!cmSystemTools::FileIsFullPath(in)) {
  2311. reportError(context, content->GetOriginalExpression(),
  2312. "\"" + in + "\" is not an absolute path.");
  2313. return std::string();
  2314. }
  2315. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2316. }
  2317. return cmJoin(listOut, separator);
  2318. }
  2319. } shellPathNode;
  2320. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2321. const std::string& identifier)
  2322. {
  2323. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2324. { "0", &zeroNode },
  2325. { "1", &oneNode },
  2326. { "AND", &andNode },
  2327. { "OR", &orNode },
  2328. { "NOT", &notNode },
  2329. { "C_COMPILER_ID", &cCompilerIdNode },
  2330. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2331. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2332. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2333. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2334. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2335. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2336. { "VERSION_GREATER", &versionGreaterNode },
  2337. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2338. { "VERSION_LESS", &versionLessNode },
  2339. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2340. { "VERSION_EQUAL", &versionEqualNode },
  2341. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2342. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2343. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2344. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2345. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2346. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2347. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2348. { "PLATFORM_ID", &platformIdNode },
  2349. { "COMPILE_FEATURES", &compileFeaturesNode },
  2350. { "CONFIGURATION", &configurationNode },
  2351. { "CONFIG", &configurationTestNode },
  2352. { "TARGET_FILE", &targetNodeGroup.File },
  2353. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2354. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2355. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2356. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2357. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2358. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2359. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2360. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2361. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2362. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2363. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2364. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2365. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2366. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2367. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2368. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2369. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2370. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2371. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2372. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2373. { "STREQUAL", &strEqualNode },
  2374. { "EQUAL", &equalNode },
  2375. { "IN_LIST", &inListNode },
  2376. { "FILTER", &filterNode },
  2377. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2378. { "LOWER_CASE", &lowerCaseNode },
  2379. { "UPPER_CASE", &upperCaseNode },
  2380. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2381. { "BOOL", &boolNode },
  2382. { "IF", &ifNode },
  2383. { "ANGLE-R", &angle_rNode },
  2384. { "COMMA", &commaNode },
  2385. { "SEMICOLON", &semicolonNode },
  2386. { "TARGET_PROPERTY", &targetPropertyNode },
  2387. { "TARGET_NAME", &targetNameNode },
  2388. { "TARGET_OBJECTS", &targetObjectsNode },
  2389. { "TARGET_POLICY", &targetPolicyNode },
  2390. { "TARGET_EXISTS", &targetExistsNode },
  2391. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2392. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2393. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2394. { "GENEX_EVAL", &genexEvalNode },
  2395. { "BUILD_INTERFACE", &buildInterfaceNode },
  2396. { "INSTALL_INTERFACE", &installInterfaceNode },
  2397. { "INSTALL_PREFIX", &installPrefixNode },
  2398. { "JOIN", &joinNode },
  2399. { "LINK_ONLY", &linkOnlyNode },
  2400. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2401. { "COMPILE_LANGUAGE", &languageNode },
  2402. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2403. { "LINK_LANGUAGE", &linkLanguageNode },
  2404. { "LINK_LIBRARY", &linkLibraryNode },
  2405. { "HOST_LINK", &hostLinkNode },
  2406. { "DEVICE_LINK", &deviceLinkNode },
  2407. { "SHELL_PATH", &shellPathNode }
  2408. };
  2409. {
  2410. auto itr = nodeMap.find(identifier);
  2411. if (itr != nodeMap.end()) {
  2412. return itr->second;
  2413. }
  2414. }
  2415. return nullptr;
  2416. }
  2417. void reportError(cmGeneratorExpressionContext* context,
  2418. const std::string& expr, const std::string& result)
  2419. {
  2420. context->HadError = true;
  2421. if (context->Quiet) {
  2422. return;
  2423. }
  2424. std::ostringstream e;
  2425. /* clang-format off */
  2426. e << "Error evaluating generator expression:\n"
  2427. << " " << expr << "\n"
  2428. << result;
  2429. /* clang-format on */
  2430. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2431. e.str(), context->Backtrace);
  2432. }