cmGeneratorExpressionNode.cxx 91 KB

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