cmGeneratorExpressionNode.cxx 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  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 HostLinkNode : public cmGeneratorExpressionNode
  1042. {
  1043. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1044. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  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->EvaluatingLinkOptionsExpression()) {
  1053. reportError(context, content->GetOriginalExpression(),
  1054. "$<HOST_LINK:...> may only be used with binary targets "
  1055. "to specify link options.");
  1056. return std::string();
  1057. }
  1058. return context->HeadTarget->IsDeviceLink() ? std::string()
  1059. : cmJoin(parameters, ";");
  1060. }
  1061. } hostLinkNode;
  1062. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1063. {
  1064. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1065. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1066. std::string Evaluate(
  1067. const std::vector<std::string>& parameters,
  1068. cmGeneratorExpressionContext* context,
  1069. const GeneratorExpressionContent* content,
  1070. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1071. {
  1072. if (!context->HeadTarget || !dagChecker ||
  1073. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1074. reportError(context, content->GetOriginalExpression(),
  1075. "$<DEVICE_LINK:...> may only be used with binary targets "
  1076. "to specify link options.");
  1077. return std::string();
  1078. }
  1079. if (context->HeadTarget->IsDeviceLink()) {
  1080. std::vector<std::string> list;
  1081. cmExpandLists(parameters.begin(), parameters.end(), list);
  1082. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1083. const auto DL_END = "</DEVICE_LINK>"_s;
  1084. cm::erase_if(list, [&](const std::string& item) {
  1085. return item == DL_BEGIN || item == DL_END;
  1086. });
  1087. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1088. list.push_back(static_cast<std::string>(DL_END));
  1089. return cmJoin(list, ";");
  1090. }
  1091. return std::string();
  1092. }
  1093. } deviceLinkNode;
  1094. static std::string getLinkedTargetsContent(
  1095. cmGeneratorTarget const* target, std::string const& prop,
  1096. cmGeneratorExpressionContext* context,
  1097. cmGeneratorExpressionDAGChecker* dagChecker)
  1098. {
  1099. std::string result;
  1100. if (cmLinkImplementationLibraries const* impl =
  1101. target->GetLinkImplementationLibraries(context->Config)) {
  1102. for (cmLinkImplItem const& lib : impl->Libraries) {
  1103. if (lib.Target) {
  1104. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1105. // caller's property and hand-evaluate it as if it were compiled.
  1106. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1107. cmGeneratorExpressionContext libContext(
  1108. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1109. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1110. context->Language);
  1111. std::string libResult =
  1112. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1113. if (!libResult.empty()) {
  1114. if (result.empty()) {
  1115. result = std::move(libResult);
  1116. } else {
  1117. result.reserve(result.size() + 1 + libResult.size());
  1118. result += ";";
  1119. result += libResult;
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. return result;
  1126. }
  1127. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1128. {
  1129. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1130. // This node handles errors on parameter count itself.
  1131. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1132. static const char* GetErrorText(std::string const& targetName,
  1133. std::string const& propertyName)
  1134. {
  1135. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1136. if (targetName.empty() && propertyName.empty()) {
  1137. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1138. "target name and property name.";
  1139. }
  1140. if (targetName.empty()) {
  1141. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1142. "target name.";
  1143. }
  1144. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1145. if (!propertyNameValidator.find(propertyName)) {
  1146. return "Target name and property name not supported.";
  1147. }
  1148. return "Target name not supported.";
  1149. }
  1150. return nullptr;
  1151. }
  1152. std::string Evaluate(
  1153. const std::vector<std::string>& parameters,
  1154. cmGeneratorExpressionContext* context,
  1155. const GeneratorExpressionContent* content,
  1156. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1157. {
  1158. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1159. cmGeneratorTarget const* target = nullptr;
  1160. std::string targetName;
  1161. std::string propertyName;
  1162. if (parameters.size() == 2) {
  1163. targetName = parameters[0];
  1164. propertyName = parameters[1];
  1165. if (const char* e = GetErrorText(targetName, propertyName)) {
  1166. reportError(context, content->GetOriginalExpression(), e);
  1167. return std::string();
  1168. }
  1169. if (propertyName == "ALIASED_TARGET"_s) {
  1170. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1171. if (cmGeneratorTarget* tgt =
  1172. context->LG->FindGeneratorTargetToUse(targetName)) {
  1173. return tgt->GetName();
  1174. }
  1175. }
  1176. return std::string();
  1177. }
  1178. if (propertyName == "ALIAS_GLOBAL"_s) {
  1179. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1180. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1181. ? "TRUE"
  1182. : "FALSE";
  1183. }
  1184. return std::string();
  1185. }
  1186. target = context->LG->FindGeneratorTargetToUse(targetName);
  1187. if (!target) {
  1188. std::ostringstream e;
  1189. e << "Target \"" << targetName << "\" not found.";
  1190. reportError(context, content->GetOriginalExpression(), e.str());
  1191. return std::string();
  1192. }
  1193. context->AllTargets.insert(target);
  1194. } else if (parameters.size() == 1) {
  1195. target = context->HeadTarget;
  1196. propertyName = parameters[0];
  1197. // Keep track of the properties seen while processing.
  1198. // The evaluation of the LINK_LIBRARIES generator expressions
  1199. // will check this to ensure that properties have one consistent
  1200. // value for all evaluations.
  1201. context->SeenTargetProperties.insert(propertyName);
  1202. context->HadHeadSensitiveCondition = true;
  1203. if (!target) {
  1204. reportError(
  1205. context, content->GetOriginalExpression(),
  1206. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1207. "It may not be used with add_custom_command or add_custom_target. "
  1208. " "
  1209. "Specify the target to read a property from using the "
  1210. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1211. return std::string();
  1212. }
  1213. } else {
  1214. reportError(
  1215. context, content->GetOriginalExpression(),
  1216. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1217. return std::string();
  1218. }
  1219. if (propertyName == "SOURCES") {
  1220. context->SourceSensitiveTargets.insert(target);
  1221. }
  1222. if (propertyName.empty()) {
  1223. reportError(
  1224. context, content->GetOriginalExpression(),
  1225. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1226. "name.");
  1227. return std::string();
  1228. }
  1229. if (!propertyNameValidator.find(propertyName)) {
  1230. ::reportError(context, content->GetOriginalExpression(),
  1231. "Property name not supported.");
  1232. return std::string();
  1233. }
  1234. assert(target);
  1235. if (propertyName == "LINKER_LANGUAGE") {
  1236. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1237. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1238. dagCheckerParent->EvaluatingSources())) {
  1239. reportError(
  1240. context, content->GetOriginalExpression(),
  1241. "LINKER_LANGUAGE target property can not be used while evaluating "
  1242. "link libraries for a static library");
  1243. return std::string();
  1244. }
  1245. return target->GetLinkerLanguage(context->Config);
  1246. }
  1247. std::string interfacePropertyName;
  1248. bool isInterfaceProperty = false;
  1249. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1250. if (propertyName == #prop) { \
  1251. interfacePropertyName = "INTERFACE_" #prop; \
  1252. } else if (propertyName == "INTERFACE_" #prop) { \
  1253. interfacePropertyName = "INTERFACE_" #prop; \
  1254. isInterfaceProperty = true; \
  1255. } else
  1256. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1257. // Note that the above macro terminates with an else
  1258. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1259. cmPolicies::PolicyStatus polSt =
  1260. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1261. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1262. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1263. }
  1264. }
  1265. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1266. bool evaluatingLinkLibraries = false;
  1267. if (dagCheckerParent) {
  1268. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1269. dagCheckerParent->EvaluatingPICExpression()) {
  1270. // No check required.
  1271. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1272. evaluatingLinkLibraries = true;
  1273. if (!interfacePropertyName.empty()) {
  1274. reportError(
  1275. context, content->GetOriginalExpression(),
  1276. "$<TARGET_PROPERTY:...> expression in link libraries "
  1277. "evaluation depends on target property which is transitive "
  1278. "over the link libraries, creating a recursion.");
  1279. return std::string();
  1280. }
  1281. } else {
  1282. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1283. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1284. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1285. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1286. }
  1287. }
  1288. if (isInterfaceProperty) {
  1289. return cmGeneratorExpression::StripEmptyListElements(
  1290. target->EvaluateInterfaceProperty(propertyName, context,
  1291. dagCheckerParent));
  1292. }
  1293. cmGeneratorExpressionDAGChecker dagChecker(
  1294. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1295. switch (dagChecker.Check()) {
  1296. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1297. dagChecker.ReportError(context, content->GetOriginalExpression());
  1298. return std::string();
  1299. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1300. // No error. We just skip cyclic references.
  1301. return std::string();
  1302. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1303. // We handle transitive properties above. For non-transitive
  1304. // properties we accept repeats anyway.
  1305. case cmGeneratorExpressionDAGChecker::DAG:
  1306. break;
  1307. }
  1308. std::string result;
  1309. bool haveProp = false;
  1310. if (cmValue p = target->GetProperty(propertyName)) {
  1311. result = *p;
  1312. haveProp = true;
  1313. } else if (evaluatingLinkLibraries) {
  1314. return std::string();
  1315. }
  1316. if (!haveProp && !target->IsImported() &&
  1317. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1318. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1319. context->Config)) {
  1320. context->HadContextSensitiveCondition = true;
  1321. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1322. context->Config)
  1323. ? "1"
  1324. : "0";
  1325. }
  1326. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1327. context->Config)) {
  1328. context->HadContextSensitiveCondition = true;
  1329. const char* propContent =
  1330. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1331. context->Config);
  1332. return propContent ? propContent : "";
  1333. }
  1334. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1335. context->Config)) {
  1336. context->HadContextSensitiveCondition = true;
  1337. const char* propContent =
  1338. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1339. context->Config);
  1340. return propContent ? propContent : "";
  1341. }
  1342. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1343. context->Config)) {
  1344. context->HadContextSensitiveCondition = true;
  1345. const char* propContent =
  1346. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1347. context->Config);
  1348. return propContent ? propContent : "";
  1349. }
  1350. }
  1351. if (!target->IsImported() && dagCheckerParent &&
  1352. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1353. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1354. context->Config)) {
  1355. context->HadContextSensitiveCondition = true;
  1356. const char* propContent =
  1357. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1358. context->Config);
  1359. return propContent ? propContent : "";
  1360. }
  1361. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1362. context->Config)) {
  1363. context->HadContextSensitiveCondition = true;
  1364. const char* propContent =
  1365. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1366. context->Config);
  1367. return propContent ? propContent : "";
  1368. }
  1369. }
  1370. if (!interfacePropertyName.empty()) {
  1371. result = cmGeneratorExpression::StripEmptyListElements(
  1372. this->EvaluateDependentExpression(result, context->LG, context, target,
  1373. &dagChecker, target));
  1374. std::string linkedTargetsContent = getLinkedTargetsContent(
  1375. target, interfacePropertyName, context, &dagChecker);
  1376. if (!linkedTargetsContent.empty()) {
  1377. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1378. }
  1379. }
  1380. return result;
  1381. }
  1382. } targetPropertyNode;
  1383. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1384. {
  1385. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1386. bool GeneratesContent() const override { return true; }
  1387. bool AcceptsArbitraryContentParameter() const override { return true; }
  1388. bool RequiresLiteralInput() const override { return true; }
  1389. std::string Evaluate(
  1390. const std::vector<std::string>& parameters,
  1391. cmGeneratorExpressionContext* /*context*/,
  1392. const GeneratorExpressionContent* /*content*/,
  1393. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1394. {
  1395. return parameters.front();
  1396. }
  1397. int NumExpectedParameters() const override { return 1; }
  1398. } targetNameNode;
  1399. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1400. {
  1401. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1402. std::string Evaluate(
  1403. const std::vector<std::string>& parameters,
  1404. cmGeneratorExpressionContext* context,
  1405. const GeneratorExpressionContent* content,
  1406. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1407. {
  1408. std::string tgtName = parameters.front();
  1409. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1410. if (!gt) {
  1411. std::ostringstream e;
  1412. e << "Objects of target \"" << tgtName
  1413. << "\" referenced but no such target exists.";
  1414. reportError(context, content->GetOriginalExpression(), e.str());
  1415. return std::string();
  1416. }
  1417. cmStateEnums::TargetType type = gt->GetType();
  1418. if (type != cmStateEnums::EXECUTABLE &&
  1419. type != cmStateEnums::STATIC_LIBRARY &&
  1420. type != cmStateEnums::SHARED_LIBRARY &&
  1421. type != cmStateEnums::MODULE_LIBRARY &&
  1422. type != cmStateEnums::OBJECT_LIBRARY) {
  1423. std::ostringstream e;
  1424. e << "Objects of target \"" << tgtName
  1425. << "\" referenced but is not one of the allowed target types "
  1426. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  1427. reportError(context, content->GetOriginalExpression(), e.str());
  1428. return std::string();
  1429. }
  1430. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1431. {
  1432. std::string reason;
  1433. if (!context->EvaluateForBuildsystem &&
  1434. !gg->HasKnownObjectFileLocation(&reason)) {
  1435. std::ostringstream e;
  1436. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1437. "is only suitable for consumption by CMake (limited"
  1438. << reason
  1439. << "). "
  1440. "It is not suitable for writing out elsewhere.";
  1441. reportError(context, content->GetOriginalExpression(), e.str());
  1442. return std::string();
  1443. }
  1444. }
  1445. std::vector<std::string> objects;
  1446. if (gt->IsImported()) {
  1447. cmValue loc = nullptr;
  1448. cmValue imp = nullptr;
  1449. std::string suffix;
  1450. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  1451. cmExpandList(*loc, objects);
  1452. }
  1453. context->HadContextSensitiveCondition = true;
  1454. } else {
  1455. gt->GetTargetObjectNames(context->Config, objects);
  1456. std::string obj_dir;
  1457. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  1458. // Use object file directory with buildsystem placeholder.
  1459. obj_dir = gt->ObjectDirectory;
  1460. context->HadContextSensitiveCondition =
  1461. gt->HasContextDependentSources();
  1462. } else {
  1463. // Use object file directory with per-config location.
  1464. obj_dir = gt->GetObjectDirectory(context->Config);
  1465. context->HadContextSensitiveCondition = true;
  1466. }
  1467. for (std::string& o : objects) {
  1468. o = cmStrCat(obj_dir, o);
  1469. }
  1470. }
  1471. // Create the cmSourceFile instances in the referencing directory.
  1472. cmMakefile* mf = context->LG->GetMakefile();
  1473. for (std::string const& o : objects) {
  1474. mf->AddTargetObject(tgtName, o);
  1475. }
  1476. return cmJoin(objects, ";");
  1477. }
  1478. } targetObjectsNode;
  1479. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  1480. {
  1481. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  1482. std::string Evaluate(
  1483. const std::vector<std::string>& parameters,
  1484. cmGeneratorExpressionContext* context,
  1485. const GeneratorExpressionContent* content,
  1486. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1487. {
  1488. std::string tgtName = parameters.front();
  1489. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1490. if (!gt) {
  1491. std::ostringstream e;
  1492. e << "Objects of target \"" << tgtName
  1493. << "\" referenced but no such target exists.";
  1494. reportError(context, content->GetOriginalExpression(), e.str());
  1495. return std::string();
  1496. }
  1497. cmStateEnums::TargetType type = gt->GetType();
  1498. if (type != cmStateEnums::EXECUTABLE &&
  1499. type != cmStateEnums::SHARED_LIBRARY &&
  1500. type != cmStateEnums::MODULE_LIBRARY) {
  1501. std::ostringstream e;
  1502. e << "Objects of target \"" << tgtName
  1503. << "\" referenced but is not one of the allowed target types "
  1504. << "(EXECUTABLE, SHARED, MODULE).";
  1505. reportError(context, content->GetOriginalExpression(), e.str());
  1506. return std::string();
  1507. }
  1508. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  1509. std::vector<std::string> dllPaths;
  1510. auto const& dlls = cli->GetRuntimeDLLs();
  1511. for (auto const& dll : dlls) {
  1512. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  1513. dllPaths.emplace_back(*loc);
  1514. }
  1515. }
  1516. return cmJoin(dllPaths, ";");
  1517. }
  1518. return "";
  1519. }
  1520. } targetRuntimeDllsNode;
  1521. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1522. {
  1523. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1524. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1525. std::string Evaluate(
  1526. const std::vector<std::string>& parameters,
  1527. cmGeneratorExpressionContext* context,
  1528. const GeneratorExpressionContent* content,
  1529. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1530. {
  1531. cmGeneratorTarget const* target = context->HeadTarget;
  1532. if (!target) {
  1533. reportError(
  1534. context, content->GetOriginalExpression(),
  1535. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1536. "not be used with add_custom_command or add_custom_target.");
  1537. return std::string();
  1538. }
  1539. context->HadHeadSensitiveCondition = true;
  1540. using LangMap = std::map<std::string, std::vector<std::string>>;
  1541. static LangMap availableFeatures;
  1542. LangMap testedFeatures;
  1543. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  1544. for (std::string const& p : parameters) {
  1545. std::string error;
  1546. std::string lang;
  1547. if (!standardResolver.CompileFeatureKnown(
  1548. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  1549. reportError(context, content->GetOriginalExpression(), error);
  1550. return std::string();
  1551. }
  1552. testedFeatures[lang].push_back(p);
  1553. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1554. cmValue featuresKnown =
  1555. standardResolver.CompileFeaturesAvailable(lang, &error);
  1556. if (!featuresKnown) {
  1557. reportError(context, content->GetOriginalExpression(), error);
  1558. return std::string();
  1559. }
  1560. cmExpandList(featuresKnown, availableFeatures[lang]);
  1561. }
  1562. }
  1563. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1564. for (auto const& lit : testedFeatures) {
  1565. std::vector<std::string> const& langAvailable =
  1566. availableFeatures[lit.first];
  1567. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  1568. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1569. for (std::string const& it : lit.second) {
  1570. if (!cm::contains(langAvailable, it)) {
  1571. return "0";
  1572. }
  1573. if (standardDefault && standardDefault->empty()) {
  1574. // This compiler has no notion of language standard levels.
  1575. // All features known for the language are always available.
  1576. continue;
  1577. }
  1578. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  1579. context->Config, it)) {
  1580. if (evalLL) {
  1581. cmValue l =
  1582. target->GetLanguageStandard(lit.first, context->Config);
  1583. if (!l) {
  1584. l = standardDefault;
  1585. }
  1586. assert(l);
  1587. context->MaxLanguageStandard[target][lit.first] = *l;
  1588. } else {
  1589. return "0";
  1590. }
  1591. }
  1592. }
  1593. }
  1594. return "1";
  1595. }
  1596. } compileFeaturesNode;
  1597. static const char* targetPolicyWhitelist[] = {
  1598. nullptr
  1599. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1600. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1601. #undef TARGET_POLICY_STRING
  1602. };
  1603. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1604. const char* policy)
  1605. {
  1606. #define RETURN_POLICY(POLICY) \
  1607. if (strcmp(policy, #POLICY) == 0) { \
  1608. return tgt->GetPolicyStatus##POLICY(); \
  1609. }
  1610. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1611. #undef RETURN_POLICY
  1612. assert(false && "Unreachable code. Not a valid policy");
  1613. return cmPolicies::WARN;
  1614. }
  1615. static cmPolicies::PolicyID policyForString(const char* policy_id)
  1616. {
  1617. #define RETURN_POLICY_ID(POLICY_ID) \
  1618. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1619. return cmPolicies::POLICY_ID; \
  1620. }
  1621. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1622. #undef RETURN_POLICY_ID
  1623. assert(false && "Unreachable code. Not a valid policy");
  1624. return cmPolicies::CMP0002;
  1625. }
  1626. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1627. {
  1628. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1629. int NumExpectedParameters() const override { return 1; }
  1630. std::string Evaluate(
  1631. const std::vector<std::string>& parameters,
  1632. cmGeneratorExpressionContext* context,
  1633. const GeneratorExpressionContent* content,
  1634. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1635. {
  1636. if (!context->HeadTarget) {
  1637. reportError(
  1638. context, content->GetOriginalExpression(),
  1639. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1640. "may not be used with add_custom_command or add_custom_target.");
  1641. return std::string();
  1642. }
  1643. context->HadContextSensitiveCondition = true;
  1644. context->HadHeadSensitiveCondition = true;
  1645. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1646. const char* policy = targetPolicyWhitelist[i];
  1647. if (parameters.front() == policy) {
  1648. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1649. switch (statusForTarget(context->HeadTarget, policy)) {
  1650. case cmPolicies::WARN:
  1651. lg->IssueMessage(
  1652. MessageType::AUTHOR_WARNING,
  1653. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1654. CM_FALLTHROUGH;
  1655. case cmPolicies::REQUIRED_IF_USED:
  1656. case cmPolicies::REQUIRED_ALWAYS:
  1657. case cmPolicies::OLD:
  1658. return "0";
  1659. case cmPolicies::NEW:
  1660. return "1";
  1661. }
  1662. }
  1663. }
  1664. reportError(
  1665. context, content->GetOriginalExpression(),
  1666. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1667. "policies. Currently it may be used with the following policies:\n"
  1668. #define STRINGIFY_HELPER(X) #X
  1669. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1670. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1671. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1672. #undef TARGET_POLICY_LIST_ITEM
  1673. );
  1674. return std::string();
  1675. }
  1676. } targetPolicyNode;
  1677. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1678. {
  1679. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1680. bool GeneratesContent() const override { return true; }
  1681. int NumExpectedParameters() const override { return 0; }
  1682. std::string Evaluate(
  1683. const std::vector<std::string>& /*parameters*/,
  1684. cmGeneratorExpressionContext* context,
  1685. const GeneratorExpressionContent* content,
  1686. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1687. {
  1688. reportError(context, content->GetOriginalExpression(),
  1689. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1690. "should never be evaluated.");
  1691. return std::string();
  1692. }
  1693. } installPrefixNode;
  1694. class ArtifactDirTag;
  1695. class ArtifactLinkerTag;
  1696. class ArtifactNameTag;
  1697. class ArtifactPathTag;
  1698. class ArtifactPdbTag;
  1699. class ArtifactSonameTag;
  1700. class ArtifactBundleDirTag;
  1701. class ArtifactBundleContentDirTag;
  1702. template <typename ArtifactT, typename ComponentT>
  1703. struct TargetFilesystemArtifactDependency
  1704. {
  1705. static void AddDependency(cmGeneratorTarget* target,
  1706. cmGeneratorExpressionContext* context)
  1707. {
  1708. context->DependTargets.insert(target);
  1709. context->AllTargets.insert(target);
  1710. }
  1711. };
  1712. struct TargetFilesystemArtifactDependencyCMP0112
  1713. {
  1714. static void AddDependency(cmGeneratorTarget* target,
  1715. cmGeneratorExpressionContext* context)
  1716. {
  1717. context->AllTargets.insert(target);
  1718. cmLocalGenerator* lg = context->LG;
  1719. switch (target->GetPolicyStatusCMP0112()) {
  1720. case cmPolicies::WARN:
  1721. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  1722. "CMAKE_POLICY_WARNING_CMP0112")) {
  1723. std::string err =
  1724. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  1725. "\nDependency being added to target:\n \"",
  1726. target->GetName(), "\"\n");
  1727. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  1728. err, context->Backtrace);
  1729. }
  1730. CM_FALLTHROUGH;
  1731. case cmPolicies::OLD:
  1732. context->DependTargets.insert(target);
  1733. break;
  1734. case cmPolicies::REQUIRED_IF_USED:
  1735. case cmPolicies::REQUIRED_ALWAYS:
  1736. case cmPolicies::NEW:
  1737. break;
  1738. }
  1739. }
  1740. };
  1741. template <typename ArtifactT>
  1742. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  1743. : TargetFilesystemArtifactDependencyCMP0112
  1744. {
  1745. };
  1746. template <typename ArtifactT>
  1747. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  1748. : TargetFilesystemArtifactDependencyCMP0112
  1749. {
  1750. };
  1751. template <>
  1752. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  1753. ArtifactPathTag>
  1754. : TargetFilesystemArtifactDependencyCMP0112
  1755. {
  1756. };
  1757. template <>
  1758. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  1759. ArtifactPathTag>
  1760. : TargetFilesystemArtifactDependencyCMP0112
  1761. {
  1762. };
  1763. template <typename ArtifactT>
  1764. struct TargetFilesystemArtifactResultCreator
  1765. {
  1766. static std::string Create(cmGeneratorTarget* target,
  1767. cmGeneratorExpressionContext* context,
  1768. const GeneratorExpressionContent* content);
  1769. };
  1770. template <>
  1771. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1772. {
  1773. static std::string Create(cmGeneratorTarget* target,
  1774. cmGeneratorExpressionContext* context,
  1775. const GeneratorExpressionContent* content)
  1776. {
  1777. // The target soname file (.so.1).
  1778. if (target->IsDLLPlatform()) {
  1779. ::reportError(context, content->GetOriginalExpression(),
  1780. "TARGET_SONAME_FILE is not allowed "
  1781. "for DLL target platforms.");
  1782. return std::string();
  1783. }
  1784. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1785. ::reportError(context, content->GetOriginalExpression(),
  1786. "TARGET_SONAME_FILE is allowed only for "
  1787. "SHARED libraries.");
  1788. return std::string();
  1789. }
  1790. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  1791. target->GetSOName(context->Config));
  1792. return result;
  1793. }
  1794. };
  1795. template <>
  1796. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1797. {
  1798. static std::string Create(cmGeneratorTarget* target,
  1799. cmGeneratorExpressionContext* context,
  1800. const GeneratorExpressionContent* content)
  1801. {
  1802. if (target->IsImported()) {
  1803. ::reportError(context, content->GetOriginalExpression(),
  1804. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1805. return std::string();
  1806. }
  1807. std::string language = target->GetLinkerLanguage(context->Config);
  1808. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1809. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1810. ::reportError(context, content->GetOriginalExpression(),
  1811. "TARGET_PDB_FILE is not supported by the target linker.");
  1812. return std::string();
  1813. }
  1814. cmStateEnums::TargetType targetType = target->GetType();
  1815. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1816. targetType != cmStateEnums::MODULE_LIBRARY &&
  1817. targetType != cmStateEnums::EXECUTABLE) {
  1818. ::reportError(context, content->GetOriginalExpression(),
  1819. "TARGET_PDB_FILE is allowed only for "
  1820. "targets with linker created artifacts.");
  1821. return std::string();
  1822. }
  1823. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  1824. '/', target->GetPDBName(context->Config));
  1825. return result;
  1826. }
  1827. };
  1828. template <>
  1829. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1830. {
  1831. static std::string Create(cmGeneratorTarget* target,
  1832. cmGeneratorExpressionContext* context,
  1833. const GeneratorExpressionContent* content)
  1834. {
  1835. // The file used to link to the target (.so, .lib, .a).
  1836. if (!target->IsLinkable()) {
  1837. ::reportError(context, content->GetOriginalExpression(),
  1838. "TARGET_LINKER_FILE is allowed only for libraries and "
  1839. "executables with ENABLE_EXPORTS.");
  1840. return std::string();
  1841. }
  1842. cmStateEnums::ArtifactType artifact =
  1843. target->HasImportLibrary(context->Config)
  1844. ? cmStateEnums::ImportLibraryArtifact
  1845. : cmStateEnums::RuntimeBinaryArtifact;
  1846. return target->GetFullPath(context->Config, artifact);
  1847. }
  1848. };
  1849. template <>
  1850. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  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_BUNDLE_DIR not allowed for IMPORTED targets.");
  1859. return std::string();
  1860. }
  1861. if (!target->IsBundleOnApple()) {
  1862. ::reportError(context, content->GetOriginalExpression(),
  1863. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1864. return std::string();
  1865. }
  1866. std::string outpath = target->GetDirectory(context->Config) + '/';
  1867. return target->BuildBundleDirectory(outpath, context->Config,
  1868. cmGeneratorTarget::BundleDirLevel);
  1869. }
  1870. };
  1871. template <>
  1872. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1873. {
  1874. static std::string Create(cmGeneratorTarget* target,
  1875. cmGeneratorExpressionContext* context,
  1876. const GeneratorExpressionContent* content)
  1877. {
  1878. if (target->IsImported()) {
  1879. ::reportError(
  1880. context, content->GetOriginalExpression(),
  1881. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1882. return std::string();
  1883. }
  1884. if (!target->IsBundleOnApple()) {
  1885. ::reportError(
  1886. context, content->GetOriginalExpression(),
  1887. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1888. return std::string();
  1889. }
  1890. std::string outpath = target->GetDirectory(context->Config) + '/';
  1891. return target->BuildBundleDirectory(outpath, context->Config,
  1892. cmGeneratorTarget::ContentLevel);
  1893. }
  1894. };
  1895. template <>
  1896. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1897. {
  1898. static std::string Create(cmGeneratorTarget* target,
  1899. cmGeneratorExpressionContext* context,
  1900. const GeneratorExpressionContent* /*unused*/)
  1901. {
  1902. return target->GetFullPath(context->Config,
  1903. cmStateEnums::RuntimeBinaryArtifact, true);
  1904. }
  1905. };
  1906. template <typename ArtifactT>
  1907. struct TargetFilesystemArtifactResultGetter
  1908. {
  1909. static std::string Get(const std::string& result);
  1910. };
  1911. template <>
  1912. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1913. {
  1914. static std::string Get(const std::string& result)
  1915. {
  1916. return cmSystemTools::GetFilenameName(result);
  1917. }
  1918. };
  1919. template <>
  1920. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1921. {
  1922. static std::string Get(const std::string& result)
  1923. {
  1924. return cmSystemTools::GetFilenamePath(result);
  1925. }
  1926. };
  1927. template <>
  1928. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1929. {
  1930. static std::string Get(const std::string& result) { return result; }
  1931. };
  1932. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1933. {
  1934. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1935. protected:
  1936. cmGeneratorTarget* GetTarget(
  1937. const std::vector<std::string>& parameters,
  1938. cmGeneratorExpressionContext* context,
  1939. const GeneratorExpressionContent* content,
  1940. cmGeneratorExpressionDAGChecker* dagChecker) const
  1941. {
  1942. // Lookup the referenced target.
  1943. std::string name = parameters.front();
  1944. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1945. ::reportError(context, content->GetOriginalExpression(),
  1946. "Expression syntax not recognized.");
  1947. return nullptr;
  1948. }
  1949. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1950. if (!target) {
  1951. ::reportError(context, content->GetOriginalExpression(),
  1952. "No target \"" + name + "\"");
  1953. return nullptr;
  1954. }
  1955. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1956. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1957. ::reportError(context, content->GetOriginalExpression(),
  1958. "Target \"" + name +
  1959. "\" is not an executable or library.");
  1960. return nullptr;
  1961. }
  1962. if (dagChecker &&
  1963. (dagChecker->EvaluatingLinkLibraries(target) ||
  1964. (dagChecker->EvaluatingSources() &&
  1965. target == dagChecker->TopTarget()))) {
  1966. ::reportError(context, content->GetOriginalExpression(),
  1967. "Expressions which require the linker language may not "
  1968. "be used while evaluating link libraries");
  1969. return nullptr;
  1970. }
  1971. return target;
  1972. }
  1973. };
  1974. template <typename ArtifactT, typename ComponentT>
  1975. struct TargetFilesystemArtifact : public TargetArtifactBase
  1976. {
  1977. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1978. int NumExpectedParameters() const override { return 1; }
  1979. std::string Evaluate(
  1980. const std::vector<std::string>& parameters,
  1981. cmGeneratorExpressionContext* context,
  1982. const GeneratorExpressionContent* content,
  1983. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1984. {
  1985. cmGeneratorTarget* target =
  1986. this->GetTarget(parameters, context, content, dagChecker);
  1987. if (!target) {
  1988. return std::string();
  1989. }
  1990. // Not a dependent target if we are querying for ArtifactDirTag,
  1991. // ArtifactNameTag, ArtifactBundleDirTag, and ArtifactBundleContentDirTag
  1992. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  1993. target, context);
  1994. std::string result =
  1995. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1996. content);
  1997. if (context->HadError) {
  1998. return std::string();
  1999. }
  2000. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2001. }
  2002. };
  2003. template <typename ArtifactT>
  2004. struct TargetFilesystemArtifactNodeGroup
  2005. {
  2006. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2007. {
  2008. }
  2009. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2010. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2011. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2012. };
  2013. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2014. targetNodeGroup;
  2015. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2016. targetLinkerNodeGroup;
  2017. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2018. targetSoNameNodeGroup;
  2019. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2020. targetPdbNodeGroup;
  2021. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2022. targetBundleDirNode;
  2023. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2024. ArtifactPathTag>
  2025. targetBundleContentDirNode;
  2026. //
  2027. // To retrieve base name for various artifacts
  2028. //
  2029. template <typename ArtifactT>
  2030. struct TargetOutputNameArtifactResultGetter
  2031. {
  2032. static std::string Get(cmGeneratorTarget* target,
  2033. cmGeneratorExpressionContext* context,
  2034. const GeneratorExpressionContent* content);
  2035. };
  2036. template <>
  2037. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2038. {
  2039. static std::string Get(cmGeneratorTarget* target,
  2040. cmGeneratorExpressionContext* context,
  2041. const GeneratorExpressionContent* /*unused*/)
  2042. {
  2043. return target->GetOutputName(context->Config,
  2044. cmStateEnums::RuntimeBinaryArtifact) +
  2045. target->GetFilePostfix(context->Config);
  2046. }
  2047. };
  2048. template <>
  2049. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2050. {
  2051. static std::string Get(cmGeneratorTarget* target,
  2052. cmGeneratorExpressionContext* context,
  2053. const GeneratorExpressionContent* content)
  2054. {
  2055. // The file used to link to the target (.so, .lib, .a).
  2056. if (!target->IsLinkable()) {
  2057. ::reportError(context, content->GetOriginalExpression(),
  2058. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2059. "libraries and executables with ENABLE_EXPORTS.");
  2060. return std::string();
  2061. }
  2062. cmStateEnums::ArtifactType artifact =
  2063. target->HasImportLibrary(context->Config)
  2064. ? cmStateEnums::ImportLibraryArtifact
  2065. : cmStateEnums::RuntimeBinaryArtifact;
  2066. return target->GetOutputName(context->Config, artifact) +
  2067. target->GetFilePostfix(context->Config);
  2068. }
  2069. };
  2070. template <>
  2071. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2072. {
  2073. static std::string Get(cmGeneratorTarget* target,
  2074. cmGeneratorExpressionContext* context,
  2075. const GeneratorExpressionContent* content)
  2076. {
  2077. if (target->IsImported()) {
  2078. ::reportError(
  2079. context, content->GetOriginalExpression(),
  2080. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2081. return std::string();
  2082. }
  2083. std::string language = target->GetLinkerLanguage(context->Config);
  2084. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2085. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2086. ::reportError(
  2087. context, content->GetOriginalExpression(),
  2088. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2089. return std::string();
  2090. }
  2091. cmStateEnums::TargetType targetType = target->GetType();
  2092. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2093. targetType != cmStateEnums::MODULE_LIBRARY &&
  2094. targetType != cmStateEnums::EXECUTABLE) {
  2095. ::reportError(context, content->GetOriginalExpression(),
  2096. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2097. "targets with linker created artifacts.");
  2098. return std::string();
  2099. }
  2100. return target->GetPDBOutputName(context->Config) +
  2101. target->GetFilePostfix(context->Config);
  2102. }
  2103. };
  2104. template <typename ArtifactT>
  2105. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2106. {
  2107. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2108. int NumExpectedParameters() const override { return 1; }
  2109. std::string Evaluate(
  2110. const std::vector<std::string>& parameters,
  2111. cmGeneratorExpressionContext* context,
  2112. const GeneratorExpressionContent* content,
  2113. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2114. {
  2115. cmGeneratorTarget* target =
  2116. this->GetTarget(parameters, context, content, dagChecker);
  2117. if (!target) {
  2118. return std::string();
  2119. }
  2120. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2121. target, context, content);
  2122. if (context->HadError) {
  2123. return std::string();
  2124. }
  2125. return result;
  2126. }
  2127. };
  2128. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2129. targetFileBaseNameNode;
  2130. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2131. targetLinkerFileBaseNameNode;
  2132. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2133. targetPdbFileBaseNameNode;
  2134. class ArtifactFilePrefixTag;
  2135. class ArtifactLinkerFilePrefixTag;
  2136. class ArtifactFileSuffixTag;
  2137. class ArtifactLinkerFileSuffixTag;
  2138. template <typename ArtifactT>
  2139. struct TargetFileArtifactResultGetter
  2140. {
  2141. static std::string Get(cmGeneratorTarget* target,
  2142. cmGeneratorExpressionContext* context,
  2143. const GeneratorExpressionContent* content);
  2144. };
  2145. template <>
  2146. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2147. {
  2148. static std::string Get(cmGeneratorTarget* target,
  2149. cmGeneratorExpressionContext* context,
  2150. const GeneratorExpressionContent*)
  2151. {
  2152. return target->GetFilePrefix(context->Config);
  2153. }
  2154. };
  2155. template <>
  2156. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2157. {
  2158. static std::string Get(cmGeneratorTarget* target,
  2159. cmGeneratorExpressionContext* context,
  2160. const GeneratorExpressionContent* content)
  2161. {
  2162. if (!target->IsLinkable()) {
  2163. ::reportError(context, content->GetOriginalExpression(),
  2164. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2165. "executables with ENABLE_EXPORTS.");
  2166. return std::string();
  2167. }
  2168. cmStateEnums::ArtifactType artifact =
  2169. target->HasImportLibrary(context->Config)
  2170. ? cmStateEnums::ImportLibraryArtifact
  2171. : cmStateEnums::RuntimeBinaryArtifact;
  2172. return target->GetFilePrefix(context->Config, artifact);
  2173. }
  2174. };
  2175. template <>
  2176. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2177. {
  2178. static std::string Get(cmGeneratorTarget* target,
  2179. cmGeneratorExpressionContext* context,
  2180. const GeneratorExpressionContent*)
  2181. {
  2182. return target->GetFileSuffix(context->Config);
  2183. }
  2184. };
  2185. template <>
  2186. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2187. {
  2188. static std::string Get(cmGeneratorTarget* target,
  2189. cmGeneratorExpressionContext* context,
  2190. const GeneratorExpressionContent* content)
  2191. {
  2192. if (!target->IsLinkable()) {
  2193. ::reportError(context, content->GetOriginalExpression(),
  2194. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2195. "executables with ENABLE_EXPORTS.");
  2196. return std::string();
  2197. }
  2198. cmStateEnums::ArtifactType artifact =
  2199. target->HasImportLibrary(context->Config)
  2200. ? cmStateEnums::ImportLibraryArtifact
  2201. : cmStateEnums::RuntimeBinaryArtifact;
  2202. return target->GetFileSuffix(context->Config, artifact);
  2203. }
  2204. };
  2205. template <typename ArtifactT>
  2206. struct TargetFileArtifact : public TargetArtifactBase
  2207. {
  2208. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2209. int NumExpectedParameters() const override { return 1; }
  2210. std::string Evaluate(
  2211. const std::vector<std::string>& parameters,
  2212. cmGeneratorExpressionContext* context,
  2213. const GeneratorExpressionContent* content,
  2214. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2215. {
  2216. cmGeneratorTarget* target =
  2217. this->GetTarget(parameters, context, content, dagChecker);
  2218. if (!target) {
  2219. return std::string();
  2220. }
  2221. std::string result =
  2222. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2223. if (context->HadError) {
  2224. return std::string();
  2225. }
  2226. return result;
  2227. }
  2228. };
  2229. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2230. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2231. targetLinkerFilePrefixNode;
  2232. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2233. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2234. targetLinkerFileSuffixNode;
  2235. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2236. {
  2237. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2238. std::string Evaluate(
  2239. const std::vector<std::string>& parameters,
  2240. cmGeneratorExpressionContext* context,
  2241. const GeneratorExpressionContent* content,
  2242. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2243. {
  2244. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2245. if (listIn.empty()) {
  2246. reportError(context, content->GetOriginalExpression(),
  2247. "\"\" is not an absolute path.");
  2248. return std::string();
  2249. }
  2250. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2251. cmOutputConverter converter(snapshot);
  2252. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2253. std::vector<std::string> listOut;
  2254. listOut.reserve(listIn.size());
  2255. for (auto const& in : listIn) {
  2256. if (!cmSystemTools::FileIsFullPath(in)) {
  2257. reportError(context, content->GetOriginalExpression(),
  2258. "\"" + in + "\" is not an absolute path.");
  2259. return std::string();
  2260. }
  2261. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2262. }
  2263. return cmJoin(listOut, separator);
  2264. }
  2265. } shellPathNode;
  2266. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2267. const std::string& identifier)
  2268. {
  2269. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2270. { "0", &zeroNode },
  2271. { "1", &oneNode },
  2272. { "AND", &andNode },
  2273. { "OR", &orNode },
  2274. { "NOT", &notNode },
  2275. { "C_COMPILER_ID", &cCompilerIdNode },
  2276. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2277. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2278. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2279. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2280. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2281. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2282. { "VERSION_GREATER", &versionGreaterNode },
  2283. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2284. { "VERSION_LESS", &versionLessNode },
  2285. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2286. { "VERSION_EQUAL", &versionEqualNode },
  2287. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2288. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2289. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2290. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2291. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2292. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2293. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2294. { "PLATFORM_ID", &platformIdNode },
  2295. { "COMPILE_FEATURES", &compileFeaturesNode },
  2296. { "CONFIGURATION", &configurationNode },
  2297. { "CONFIG", &configurationTestNode },
  2298. { "TARGET_FILE", &targetNodeGroup.File },
  2299. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2300. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2301. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2302. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2303. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2304. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2305. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2306. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2307. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2308. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2309. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2310. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2311. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2312. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2313. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2314. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2315. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2316. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2317. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2318. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2319. { "STREQUAL", &strEqualNode },
  2320. { "EQUAL", &equalNode },
  2321. { "IN_LIST", &inListNode },
  2322. { "FILTER", &filterNode },
  2323. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2324. { "LOWER_CASE", &lowerCaseNode },
  2325. { "UPPER_CASE", &upperCaseNode },
  2326. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2327. { "BOOL", &boolNode },
  2328. { "IF", &ifNode },
  2329. { "ANGLE-R", &angle_rNode },
  2330. { "COMMA", &commaNode },
  2331. { "SEMICOLON", &semicolonNode },
  2332. { "TARGET_PROPERTY", &targetPropertyNode },
  2333. { "TARGET_NAME", &targetNameNode },
  2334. { "TARGET_OBJECTS", &targetObjectsNode },
  2335. { "TARGET_POLICY", &targetPolicyNode },
  2336. { "TARGET_EXISTS", &targetExistsNode },
  2337. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2338. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2339. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2340. { "GENEX_EVAL", &genexEvalNode },
  2341. { "BUILD_INTERFACE", &buildInterfaceNode },
  2342. { "INSTALL_INTERFACE", &installInterfaceNode },
  2343. { "INSTALL_PREFIX", &installPrefixNode },
  2344. { "JOIN", &joinNode },
  2345. { "LINK_ONLY", &linkOnlyNode },
  2346. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2347. { "COMPILE_LANGUAGE", &languageNode },
  2348. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2349. { "LINK_LANGUAGE", &linkLanguageNode },
  2350. { "HOST_LINK", &hostLinkNode },
  2351. { "DEVICE_LINK", &deviceLinkNode },
  2352. { "SHELL_PATH", &shellPathNode }
  2353. };
  2354. {
  2355. auto itr = nodeMap.find(identifier);
  2356. if (itr != nodeMap.end()) {
  2357. return itr->second;
  2358. }
  2359. }
  2360. return nullptr;
  2361. }
  2362. void reportError(cmGeneratorExpressionContext* context,
  2363. const std::string& expr, const std::string& result)
  2364. {
  2365. context->HadError = true;
  2366. if (context->Quiet) {
  2367. return;
  2368. }
  2369. std::ostringstream e;
  2370. /* clang-format off */
  2371. e << "Error evaluating generator expression:\n"
  2372. << " " << expr << "\n"
  2373. << result;
  2374. /* clang-format on */
  2375. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2376. e.str(), context->Backtrace);
  2377. }