1
0

cmGeneratorExpressionNode.cxx 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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. struct CompilerVersionNode : public cmGeneratorExpressionNode
  617. {
  618. CompilerVersionNode(const char* compilerLang)
  619. : CompilerLanguage(compilerLang)
  620. {
  621. }
  622. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  623. std::string Evaluate(
  624. const std::vector<std::string>& parameters,
  625. cmGeneratorExpressionContext* context,
  626. const GeneratorExpressionContent* content,
  627. cmGeneratorExpressionDAGChecker* dagChecker) const override
  628. {
  629. if (!context->HeadTarget) {
  630. std::ostringstream e;
  631. e << "$<" << this->CompilerLanguage
  632. << "_COMPILER_VERSION> may only be used with binary targets. It "
  633. "may not be used with add_custom_command or add_custom_target.";
  634. reportError(context, content->GetOriginalExpression(), e.str());
  635. return {};
  636. }
  637. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  638. this->CompilerLanguage);
  639. }
  640. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  641. cmGeneratorExpressionContext* context,
  642. const GeneratorExpressionContent* content,
  643. cmGeneratorExpressionDAGChecker* /*unused*/,
  644. const std::string& lang) const
  645. {
  646. std::string const& compilerVersion =
  647. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  648. "_COMPILER_VERSION");
  649. if (parameters.empty()) {
  650. return compilerVersion;
  651. }
  652. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  653. if (!compilerIdValidator.find(parameters.front())) {
  654. reportError(context, content->GetOriginalExpression(),
  655. "Expression syntax not recognized.");
  656. return {};
  657. }
  658. if (compilerVersion.empty()) {
  659. return parameters.front().empty() ? "1" : "0";
  660. }
  661. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  662. parameters.front().c_str(),
  663. compilerVersion.c_str())
  664. ? "1"
  665. : "0";
  666. }
  667. const char* const CompilerLanguage;
  668. };
  669. static const CompilerVersionNode cCompilerVersionNode("C"),
  670. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  671. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  672. fortranCompilerVersionNode("Fortran");
  673. struct PlatformIdNode : public cmGeneratorExpressionNode
  674. {
  675. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  676. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  677. std::string Evaluate(
  678. const std::vector<std::string>& parameters,
  679. cmGeneratorExpressionContext* context,
  680. const GeneratorExpressionContent* /*content*/,
  681. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  682. {
  683. std::string const& platformId =
  684. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  685. if (parameters.empty()) {
  686. return platformId;
  687. }
  688. if (platformId.empty()) {
  689. return parameters.front().empty() ? "1" : "0";
  690. }
  691. for (auto& param : parameters) {
  692. if (param == platformId) {
  693. return "1";
  694. }
  695. }
  696. return "0";
  697. }
  698. } platformIdNode;
  699. template <cmSystemTools::CompareOp Op>
  700. struct VersionNode : public cmGeneratorExpressionNode
  701. {
  702. VersionNode() {} // NOLINT(modernize-use-equals-default)
  703. int NumExpectedParameters() const override { return 2; }
  704. std::string Evaluate(
  705. const std::vector<std::string>& parameters,
  706. cmGeneratorExpressionContext* /*context*/,
  707. const GeneratorExpressionContent* /*content*/,
  708. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  709. {
  710. return cmSystemTools::VersionCompare(Op, parameters.front().c_str(),
  711. parameters[1].c_str())
  712. ? "1"
  713. : "0";
  714. }
  715. };
  716. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  717. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  718. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  719. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  720. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  721. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  722. {
  723. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  724. std::string Evaluate(
  725. const std::vector<std::string>& parameters,
  726. cmGeneratorExpressionContext* context,
  727. const GeneratorExpressionContent* content,
  728. cmGeneratorExpressionDAGChecker* dagChecker) const override
  729. {
  730. if (!dagChecker) {
  731. reportError(context, content->GetOriginalExpression(),
  732. "$<LINK_ONLY:...> may only be used for linking");
  733. return std::string();
  734. }
  735. if (!dagChecker->GetTransitivePropertiesOnly()) {
  736. return parameters.front();
  737. }
  738. return std::string();
  739. }
  740. } linkOnlyNode;
  741. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  742. {
  743. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  744. int NumExpectedParameters() const override { return 0; }
  745. std::string Evaluate(
  746. const std::vector<std::string>& /*parameters*/,
  747. cmGeneratorExpressionContext* context,
  748. const GeneratorExpressionContent* /*content*/,
  749. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  750. {
  751. context->HadContextSensitiveCondition = true;
  752. return context->Config;
  753. }
  754. } configurationNode;
  755. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  756. {
  757. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  758. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  759. std::string Evaluate(
  760. const std::vector<std::string>& parameters,
  761. cmGeneratorExpressionContext* context,
  762. const GeneratorExpressionContent* content,
  763. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  764. {
  765. if (parameters.empty()) {
  766. return configurationNode.Evaluate(parameters, context, content, nullptr);
  767. }
  768. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  769. if (!configValidator.find(parameters.front())) {
  770. reportError(context, content->GetOriginalExpression(),
  771. "Expression syntax not recognized.");
  772. return std::string();
  773. }
  774. context->HadContextSensitiveCondition = true;
  775. for (auto& param : parameters) {
  776. if (context->Config.empty()) {
  777. if (param.empty()) {
  778. return "1";
  779. }
  780. } else if (cmsysString_strcasecmp(param.c_str(),
  781. context->Config.c_str()) == 0) {
  782. return "1";
  783. }
  784. }
  785. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  786. cmProp loc = nullptr;
  787. cmProp imp = nullptr;
  788. std::string suffix;
  789. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  790. imp, suffix)) {
  791. // This imported target has an appropriate location
  792. // for this (possibly mapped) config.
  793. // Check if there is a proper config mapping for the tested config.
  794. std::vector<std::string> mappedConfigs;
  795. std::string mapProp = cmStrCat(
  796. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  797. if (cmProp mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  798. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  799. for (auto& param : parameters) {
  800. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  801. return "1";
  802. }
  803. }
  804. }
  805. }
  806. }
  807. return "0";
  808. }
  809. } configurationTestNode;
  810. static const struct JoinNode : public cmGeneratorExpressionNode
  811. {
  812. JoinNode() {} // NOLINT(modernize-use-equals-default)
  813. int NumExpectedParameters() const override { return 2; }
  814. bool AcceptsArbitraryContentParameter() const override { return true; }
  815. std::string Evaluate(
  816. const std::vector<std::string>& parameters,
  817. cmGeneratorExpressionContext* /*context*/,
  818. const GeneratorExpressionContent* /*content*/,
  819. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  820. {
  821. std::vector<std::string> list = cmExpandedList(parameters.front());
  822. return cmJoin(list, parameters[1]);
  823. }
  824. } joinNode;
  825. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  826. {
  827. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  828. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  829. std::string Evaluate(
  830. const std::vector<std::string>& parameters,
  831. cmGeneratorExpressionContext* context,
  832. const GeneratorExpressionContent* content,
  833. cmGeneratorExpressionDAGChecker* dagChecker) const override
  834. {
  835. if (context->Language.empty() &&
  836. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  837. reportError(
  838. context, content->GetOriginalExpression(),
  839. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  840. "directories, compile definitions, compile options, and to evaluate "
  841. "components of the file(GENERATE) command.");
  842. return std::string();
  843. }
  844. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  845. std::string genName = gg->GetName();
  846. if (genName.find("Makefiles") == std::string::npos &&
  847. genName.find("Ninja") == std::string::npos &&
  848. genName.find("Visual Studio") == std::string::npos &&
  849. genName.find("Xcode") == std::string::npos &&
  850. genName.find("Watcom WMake") == std::string::npos) {
  851. reportError(context, content->GetOriginalExpression(),
  852. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  853. return std::string();
  854. }
  855. if (parameters.empty()) {
  856. return context->Language;
  857. }
  858. for (auto& param : parameters) {
  859. if (context->Language == param) {
  860. return "1";
  861. }
  862. }
  863. return "0";
  864. }
  865. } languageNode;
  866. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  867. {
  868. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  869. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  870. std::string Evaluate(
  871. const std::vector<std::string>& parameters,
  872. cmGeneratorExpressionContext* context,
  873. const GeneratorExpressionContent* content,
  874. cmGeneratorExpressionDAGChecker* dagChecker) const override
  875. {
  876. if (!context->HeadTarget ||
  877. (context->Language.empty() &&
  878. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  879. // reportError(context, content->GetOriginalExpression(), "");
  880. reportError(
  881. context, content->GetOriginalExpression(),
  882. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  883. "to specify include directories, compile definitions, and compile "
  884. "options. It may not be used with the add_custom_command, "
  885. "add_custom_target, or file(GENERATE) commands.");
  886. return std::string();
  887. }
  888. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  889. std::string genName = gg->GetName();
  890. if (genName.find("Makefiles") == std::string::npos &&
  891. genName.find("Ninja") == std::string::npos &&
  892. genName.find("Visual Studio") == std::string::npos &&
  893. genName.find("Xcode") == std::string::npos &&
  894. genName.find("Watcom WMake") == std::string::npos) {
  895. reportError(
  896. context, content->GetOriginalExpression(),
  897. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  898. return std::string();
  899. }
  900. const std::string& lang = context->Language;
  901. if (lang == parameters.front()) {
  902. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  903. parameters.cend());
  904. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  905. idParameter, context, content, dagChecker, lang);
  906. }
  907. return "0";
  908. }
  909. } languageAndIdNode;
  910. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  911. {
  912. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  913. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  914. std::string Evaluate(
  915. const std::vector<std::string>& parameters,
  916. cmGeneratorExpressionContext* context,
  917. const GeneratorExpressionContent* content,
  918. cmGeneratorExpressionDAGChecker* dagChecker) const override
  919. {
  920. if (!context->HeadTarget || !dagChecker ||
  921. !(dagChecker->EvaluatingLinkExpression() ||
  922. dagChecker->EvaluatingLinkLibraries())) {
  923. reportError(context, content->GetOriginalExpression(),
  924. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  925. "to specify link libraries, link directories, link options "
  926. "and link depends.");
  927. return std::string();
  928. }
  929. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  930. reportError(
  931. context, content->GetOriginalExpression(),
  932. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  933. return std::string();
  934. }
  935. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  936. std::string genName = gg->GetName();
  937. if (genName.find("Makefiles") == std::string::npos &&
  938. genName.find("Ninja") == std::string::npos &&
  939. genName.find("Visual Studio") == std::string::npos &&
  940. genName.find("Xcode") == std::string::npos &&
  941. genName.find("Watcom WMake") == std::string::npos) {
  942. reportError(context, content->GetOriginalExpression(),
  943. "$<LINK_LANGUAGE:...> not supported for this generator.");
  944. return std::string();
  945. }
  946. if (dagChecker->EvaluatingLinkLibraries()) {
  947. context->HadHeadSensitiveCondition = true;
  948. context->HadLinkLanguageSensitiveCondition = true;
  949. }
  950. if (parameters.empty()) {
  951. return context->Language;
  952. }
  953. for (auto& param : parameters) {
  954. if (context->Language == param) {
  955. return "1";
  956. }
  957. }
  958. return "0";
  959. }
  960. } linkLanguageNode;
  961. namespace {
  962. struct LinkerId
  963. {
  964. static std::string Evaluate(const std::vector<std::string>& parameters,
  965. cmGeneratorExpressionContext* context,
  966. const GeneratorExpressionContent* content,
  967. const std::string& lang)
  968. {
  969. std::string const& linkerId =
  970. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  971. "_COMPILER_ID");
  972. if (parameters.empty()) {
  973. return linkerId;
  974. }
  975. if (linkerId.empty()) {
  976. return parameters.front().empty() ? "1" : "0";
  977. }
  978. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  979. for (auto& param : parameters) {
  980. if (!linkerIdValidator.find(param)) {
  981. reportError(context, content->GetOriginalExpression(),
  982. "Expression syntax not recognized.");
  983. return std::string();
  984. }
  985. if (param == linkerId) {
  986. return "1";
  987. }
  988. }
  989. return "0";
  990. }
  991. };
  992. }
  993. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  994. {
  995. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  996. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  997. std::string Evaluate(
  998. const std::vector<std::string>& parameters,
  999. cmGeneratorExpressionContext* context,
  1000. const GeneratorExpressionContent* content,
  1001. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1002. {
  1003. if (!context->HeadTarget || !dagChecker ||
  1004. !(dagChecker->EvaluatingLinkExpression() ||
  1005. dagChecker->EvaluatingLinkLibraries())) {
  1006. reportError(
  1007. context, content->GetOriginalExpression(),
  1008. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1009. "to specify link libraries, link directories, link options, and link "
  1010. "depends.");
  1011. return std::string();
  1012. }
  1013. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1014. std::string genName = gg->GetName();
  1015. if (genName.find("Makefiles") == std::string::npos &&
  1016. genName.find("Ninja") == std::string::npos &&
  1017. genName.find("Visual Studio") == std::string::npos &&
  1018. genName.find("Xcode") == std::string::npos &&
  1019. genName.find("Watcom WMake") == std::string::npos) {
  1020. reportError(
  1021. context, content->GetOriginalExpression(),
  1022. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1023. return std::string();
  1024. }
  1025. if (dagChecker->EvaluatingLinkLibraries()) {
  1026. context->HadHeadSensitiveCondition = true;
  1027. context->HadLinkLanguageSensitiveCondition = true;
  1028. }
  1029. const std::string& lang = context->Language;
  1030. if (lang == parameters.front()) {
  1031. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1032. parameters.cend());
  1033. return LinkerId::Evaluate(idParameter, context, content, lang);
  1034. }
  1035. return "0";
  1036. }
  1037. } linkLanguageAndIdNode;
  1038. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1039. {
  1040. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1041. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1042. std::string Evaluate(
  1043. const std::vector<std::string>& parameters,
  1044. cmGeneratorExpressionContext* context,
  1045. const GeneratorExpressionContent* content,
  1046. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1047. {
  1048. if (!context->HeadTarget || !dagChecker ||
  1049. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1050. reportError(context, content->GetOriginalExpression(),
  1051. "$<HOST_LINK:...> may only be used with binary targets "
  1052. "to specify link options.");
  1053. return std::string();
  1054. }
  1055. return context->HeadTarget->IsDeviceLink() ? std::string()
  1056. : cmJoin(parameters, ";");
  1057. }
  1058. } hostLinkNode;
  1059. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1060. {
  1061. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1062. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1063. std::string Evaluate(
  1064. const std::vector<std::string>& parameters,
  1065. cmGeneratorExpressionContext* context,
  1066. const GeneratorExpressionContent* content,
  1067. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1068. {
  1069. if (!context->HeadTarget || !dagChecker ||
  1070. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1071. reportError(context, content->GetOriginalExpression(),
  1072. "$<DEVICE_LINK:...> may only be used with binary targets "
  1073. "to specify link options.");
  1074. return std::string();
  1075. }
  1076. if (context->HeadTarget->IsDeviceLink()) {
  1077. std::vector<std::string> list;
  1078. cmExpandLists(parameters.begin(), parameters.end(), list);
  1079. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1080. const auto DL_END = "</DEVICE_LINK>"_s;
  1081. cm::erase_if(list, [&](const std::string& item) {
  1082. return item == DL_BEGIN || item == DL_END;
  1083. });
  1084. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1085. list.push_back(static_cast<std::string>(DL_END));
  1086. return cmJoin(list, ";");
  1087. }
  1088. return std::string();
  1089. }
  1090. } deviceLinkNode;
  1091. std::string getLinkedTargetsContent(
  1092. cmGeneratorTarget const* target, std::string const& prop,
  1093. cmGeneratorExpressionContext* context,
  1094. cmGeneratorExpressionDAGChecker* dagChecker)
  1095. {
  1096. std::string result;
  1097. if (cmLinkImplementationLibraries const* impl =
  1098. target->GetLinkImplementationLibraries(context->Config)) {
  1099. for (cmLinkImplItem const& lib : impl->Libraries) {
  1100. if (lib.Target) {
  1101. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1102. // caller's property and hand-evaluate it as if it were compiled.
  1103. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1104. cmGeneratorExpressionContext libContext(
  1105. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1106. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1107. context->Language);
  1108. std::string libResult =
  1109. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1110. if (!libResult.empty()) {
  1111. if (result.empty()) {
  1112. result = std::move(libResult);
  1113. } else {
  1114. result.reserve(result.size() + 1 + libResult.size());
  1115. result += ";";
  1116. result += libResult;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. return result;
  1123. }
  1124. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1125. {
  1126. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1127. // This node handles errors on parameter count itself.
  1128. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1129. static const char* GetErrorText(std::string const& targetName,
  1130. std::string const& propertyName)
  1131. {
  1132. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1133. if (targetName.empty() && propertyName.empty()) {
  1134. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1135. "target name and property name.";
  1136. }
  1137. if (targetName.empty()) {
  1138. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1139. "target name.";
  1140. }
  1141. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1142. if (!propertyNameValidator.find(propertyName)) {
  1143. return "Target name and property name not supported.";
  1144. }
  1145. return "Target name not supported.";
  1146. }
  1147. return nullptr;
  1148. }
  1149. std::string Evaluate(
  1150. const std::vector<std::string>& parameters,
  1151. cmGeneratorExpressionContext* context,
  1152. const GeneratorExpressionContent* content,
  1153. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1154. {
  1155. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1156. cmGeneratorTarget const* target = nullptr;
  1157. std::string targetName;
  1158. std::string propertyName;
  1159. if (parameters.size() == 2) {
  1160. targetName = parameters[0];
  1161. propertyName = parameters[1];
  1162. if (const char* e = GetErrorText(targetName, propertyName)) {
  1163. reportError(context, content->GetOriginalExpression(), e);
  1164. return std::string();
  1165. }
  1166. if (propertyName == "ALIASED_TARGET"_s) {
  1167. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1168. if (cmGeneratorTarget* tgt =
  1169. context->LG->FindGeneratorTargetToUse(targetName)) {
  1170. return tgt->GetName();
  1171. }
  1172. }
  1173. return std::string();
  1174. }
  1175. if (propertyName == "ALIAS_GLOBAL"_s) {
  1176. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1177. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1178. ? "TRUE"
  1179. : "FALSE";
  1180. }
  1181. return std::string();
  1182. }
  1183. target = context->LG->FindGeneratorTargetToUse(targetName);
  1184. if (!target) {
  1185. std::ostringstream e;
  1186. e << "Target \"" << targetName << "\" not found.";
  1187. reportError(context, content->GetOriginalExpression(), e.str());
  1188. return std::string();
  1189. }
  1190. context->AllTargets.insert(target);
  1191. } else if (parameters.size() == 1) {
  1192. target = context->HeadTarget;
  1193. propertyName = parameters[0];
  1194. // Keep track of the properties seen while processing.
  1195. // The evaluation of the LINK_LIBRARIES generator expressions
  1196. // will check this to ensure that properties have one consistent
  1197. // value for all evaluations.
  1198. context->SeenTargetProperties.insert(propertyName);
  1199. context->HadHeadSensitiveCondition = true;
  1200. if (!target) {
  1201. reportError(
  1202. context, content->GetOriginalExpression(),
  1203. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1204. "It may not be used with add_custom_command or add_custom_target. "
  1205. " "
  1206. "Specify the target to read a property from using the "
  1207. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1208. return std::string();
  1209. }
  1210. } else {
  1211. reportError(
  1212. context, content->GetOriginalExpression(),
  1213. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1214. return std::string();
  1215. }
  1216. if (propertyName == "SOURCES") {
  1217. context->SourceSensitiveTargets.insert(target);
  1218. }
  1219. if (propertyName.empty()) {
  1220. reportError(
  1221. context, content->GetOriginalExpression(),
  1222. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1223. "name.");
  1224. return std::string();
  1225. }
  1226. if (!propertyNameValidator.find(propertyName)) {
  1227. ::reportError(context, content->GetOriginalExpression(),
  1228. "Property name not supported.");
  1229. return std::string();
  1230. }
  1231. assert(target);
  1232. if (propertyName == "LINKER_LANGUAGE") {
  1233. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1234. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1235. dagCheckerParent->EvaluatingSources())) {
  1236. reportError(
  1237. context, content->GetOriginalExpression(),
  1238. "LINKER_LANGUAGE target property can not be used while evaluating "
  1239. "link libraries for a static library");
  1240. return std::string();
  1241. }
  1242. return target->GetLinkerLanguage(context->Config);
  1243. }
  1244. std::string interfacePropertyName;
  1245. bool isInterfaceProperty = false;
  1246. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1247. if (propertyName == #prop) { \
  1248. interfacePropertyName = "INTERFACE_" #prop; \
  1249. } else if (propertyName == "INTERFACE_" #prop) { \
  1250. interfacePropertyName = "INTERFACE_" #prop; \
  1251. isInterfaceProperty = true; \
  1252. } else
  1253. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1254. // Note that the above macro terminates with an else
  1255. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1256. cmPolicies::PolicyStatus polSt =
  1257. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1258. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1259. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1260. }
  1261. }
  1262. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1263. bool evaluatingLinkLibraries = false;
  1264. if (dagCheckerParent) {
  1265. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1266. dagCheckerParent->EvaluatingPICExpression()) {
  1267. // No check required.
  1268. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1269. evaluatingLinkLibraries = true;
  1270. if (!interfacePropertyName.empty()) {
  1271. reportError(
  1272. context, content->GetOriginalExpression(),
  1273. "$<TARGET_PROPERTY:...> expression in link libraries "
  1274. "evaluation depends on target property which is transitive "
  1275. "over the link libraries, creating a recursion.");
  1276. return std::string();
  1277. }
  1278. } else {
  1279. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1280. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1281. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1282. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1283. }
  1284. }
  1285. if (isInterfaceProperty) {
  1286. return target->EvaluateInterfaceProperty(propertyName, context,
  1287. dagCheckerParent);
  1288. }
  1289. cmGeneratorExpressionDAGChecker dagChecker(
  1290. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1291. switch (dagChecker.Check()) {
  1292. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1293. dagChecker.ReportError(context, content->GetOriginalExpression());
  1294. return std::string();
  1295. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1296. // No error. We just skip cyclic references.
  1297. return std::string();
  1298. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1299. // We handle transitive properties above. For non-transitive
  1300. // properties we accept repeats anyway.
  1301. case cmGeneratorExpressionDAGChecker::DAG:
  1302. break;
  1303. }
  1304. std::string result;
  1305. bool haveProp = false;
  1306. if (cmProp p = target->GetProperty(propertyName)) {
  1307. result = *p;
  1308. haveProp = true;
  1309. } else if (evaluatingLinkLibraries) {
  1310. return std::string();
  1311. }
  1312. if (!haveProp && !target->IsImported() &&
  1313. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1314. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1315. context->Config)) {
  1316. context->HadContextSensitiveCondition = true;
  1317. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1318. context->Config)
  1319. ? "1"
  1320. : "0";
  1321. }
  1322. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1323. context->Config)) {
  1324. context->HadContextSensitiveCondition = true;
  1325. const char* propContent =
  1326. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1327. context->Config);
  1328. return propContent ? propContent : "";
  1329. }
  1330. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1331. context->Config)) {
  1332. context->HadContextSensitiveCondition = true;
  1333. const char* propContent =
  1334. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1335. context->Config);
  1336. return propContent ? propContent : "";
  1337. }
  1338. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1339. context->Config)) {
  1340. context->HadContextSensitiveCondition = true;
  1341. const char* propContent =
  1342. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1343. context->Config);
  1344. return propContent ? propContent : "";
  1345. }
  1346. }
  1347. if (!target->IsImported() && dagCheckerParent &&
  1348. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1349. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1350. context->Config)) {
  1351. context->HadContextSensitiveCondition = true;
  1352. const char* propContent =
  1353. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1354. context->Config);
  1355. return propContent ? propContent : "";
  1356. }
  1357. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1358. context->Config)) {
  1359. context->HadContextSensitiveCondition = true;
  1360. const char* propContent =
  1361. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1362. context->Config);
  1363. return propContent ? propContent : "";
  1364. }
  1365. }
  1366. if (!interfacePropertyName.empty()) {
  1367. result = this->EvaluateDependentExpression(result, context->LG, context,
  1368. target, &dagChecker, target);
  1369. std::string linkedTargetsContent = getLinkedTargetsContent(
  1370. target, interfacePropertyName, context, &dagChecker);
  1371. if (!linkedTargetsContent.empty()) {
  1372. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1373. }
  1374. }
  1375. return result;
  1376. }
  1377. } targetPropertyNode;
  1378. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1379. {
  1380. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1381. bool GeneratesContent() const override { return true; }
  1382. bool AcceptsArbitraryContentParameter() const override { return true; }
  1383. bool RequiresLiteralInput() const override { return true; }
  1384. std::string Evaluate(
  1385. const std::vector<std::string>& parameters,
  1386. cmGeneratorExpressionContext* /*context*/,
  1387. const GeneratorExpressionContent* /*content*/,
  1388. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1389. {
  1390. return parameters.front();
  1391. }
  1392. int NumExpectedParameters() const override { return 1; }
  1393. } targetNameNode;
  1394. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1395. {
  1396. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1397. std::string Evaluate(
  1398. const std::vector<std::string>& parameters,
  1399. cmGeneratorExpressionContext* context,
  1400. const GeneratorExpressionContent* content,
  1401. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1402. {
  1403. std::string tgtName = parameters.front();
  1404. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1405. if (!gt) {
  1406. std::ostringstream e;
  1407. e << "Objects of target \"" << tgtName
  1408. << "\" referenced but no such target exists.";
  1409. reportError(context, content->GetOriginalExpression(), e.str());
  1410. return std::string();
  1411. }
  1412. cmStateEnums::TargetType type = gt->GetType();
  1413. if (type != cmStateEnums::EXECUTABLE &&
  1414. type != cmStateEnums::STATIC_LIBRARY &&
  1415. type != cmStateEnums::SHARED_LIBRARY &&
  1416. type != cmStateEnums::MODULE_LIBRARY &&
  1417. type != cmStateEnums::OBJECT_LIBRARY) {
  1418. std::ostringstream e;
  1419. e << "Objects of target \"" << tgtName
  1420. << "\" referenced but is not an allowed library types (EXECUTABLE, "
  1421. << "STATIC, SHARED, MODULE, OBJECT).";
  1422. reportError(context, content->GetOriginalExpression(), e.str());
  1423. return std::string();
  1424. }
  1425. if (!context->EvaluateForBuildsystem) {
  1426. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1427. std::string reason;
  1428. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1429. std::ostringstream e;
  1430. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1431. "is only suitable for consumption by CMake (limited"
  1432. << reason
  1433. << "). "
  1434. "It is not suitable for writing out elsewhere.";
  1435. reportError(context, content->GetOriginalExpression(), e.str());
  1436. return std::string();
  1437. }
  1438. }
  1439. std::vector<std::string> objects;
  1440. if (gt->IsImported()) {
  1441. cmProp loc = nullptr;
  1442. cmProp imp = nullptr;
  1443. std::string suffix;
  1444. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  1445. cmExpandList(*loc, objects);
  1446. }
  1447. context->HadContextSensitiveCondition = true;
  1448. } else {
  1449. gt->GetTargetObjectNames(context->Config, objects);
  1450. std::string obj_dir;
  1451. if (context->EvaluateForBuildsystem) {
  1452. // Use object file directory with buildsystem placeholder.
  1453. obj_dir = gt->ObjectDirectory;
  1454. // Here we assume that the set of object files produced
  1455. // by an object library does not vary with configuration
  1456. // and do not set HadContextSensitiveCondition to true.
  1457. } else {
  1458. // Use object file directory with per-config location.
  1459. obj_dir = gt->GetObjectDirectory(context->Config);
  1460. context->HadContextSensitiveCondition = true;
  1461. }
  1462. for (std::string& o : objects) {
  1463. o = cmStrCat(obj_dir, o);
  1464. }
  1465. }
  1466. // Create the cmSourceFile instances in the referencing directory.
  1467. cmMakefile* mf = context->LG->GetMakefile();
  1468. for (std::string& o : objects) {
  1469. mf->AddTargetObject(tgtName, o);
  1470. }
  1471. return cmJoin(objects, ";");
  1472. }
  1473. } targetObjectsNode;
  1474. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1475. {
  1476. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1477. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1478. std::string Evaluate(
  1479. const std::vector<std::string>& parameters,
  1480. cmGeneratorExpressionContext* context,
  1481. const GeneratorExpressionContent* content,
  1482. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1483. {
  1484. cmGeneratorTarget const* target = context->HeadTarget;
  1485. if (!target) {
  1486. reportError(
  1487. context, content->GetOriginalExpression(),
  1488. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1489. "not be used with add_custom_command or add_custom_target.");
  1490. return std::string();
  1491. }
  1492. context->HadHeadSensitiveCondition = true;
  1493. using LangMap = std::map<std::string, std::vector<std::string>>;
  1494. static LangMap availableFeatures;
  1495. LangMap testedFeatures;
  1496. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  1497. for (std::string const& p : parameters) {
  1498. std::string error;
  1499. std::string lang;
  1500. if (!standardResolver.CompileFeatureKnown(
  1501. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  1502. reportError(context, content->GetOriginalExpression(), error);
  1503. return std::string();
  1504. }
  1505. testedFeatures[lang].push_back(p);
  1506. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1507. const char* featuresKnown =
  1508. standardResolver.CompileFeaturesAvailable(lang, &error);
  1509. if (!featuresKnown) {
  1510. reportError(context, content->GetOriginalExpression(), error);
  1511. return std::string();
  1512. }
  1513. cmExpandList(featuresKnown, availableFeatures[lang]);
  1514. }
  1515. }
  1516. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1517. for (auto const& lit : testedFeatures) {
  1518. std::vector<std::string> const& langAvailable =
  1519. availableFeatures[lit.first];
  1520. cmProp standardDefault = context->LG->GetMakefile()->GetDef(
  1521. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1522. for (std::string const& it : lit.second) {
  1523. if (!cm::contains(langAvailable, it)) {
  1524. return "0";
  1525. }
  1526. if (standardDefault && standardDefault->empty()) {
  1527. // This compiler has no notion of language standard levels.
  1528. // All features known for the language are always available.
  1529. continue;
  1530. }
  1531. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  1532. context->Config, it)) {
  1533. if (evalLL) {
  1534. cmProp l = target->GetLanguageStandard(lit.first, context->Config);
  1535. if (!l) {
  1536. l = standardDefault;
  1537. }
  1538. assert(l);
  1539. context->MaxLanguageStandard[target][lit.first] = *l;
  1540. } else {
  1541. return "0";
  1542. }
  1543. }
  1544. }
  1545. }
  1546. return "1";
  1547. }
  1548. } compileFeaturesNode;
  1549. static const char* targetPolicyWhitelist[] = {
  1550. nullptr
  1551. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1552. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1553. #undef TARGET_POLICY_STRING
  1554. };
  1555. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1556. const char* policy)
  1557. {
  1558. #define RETURN_POLICY(POLICY) \
  1559. if (strcmp(policy, #POLICY) == 0) { \
  1560. return tgt->GetPolicyStatus##POLICY(); \
  1561. }
  1562. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1563. #undef RETURN_POLICY
  1564. assert(false && "Unreachable code. Not a valid policy");
  1565. return cmPolicies::WARN;
  1566. }
  1567. cmPolicies::PolicyID policyForString(const char* policy_id)
  1568. {
  1569. #define RETURN_POLICY_ID(POLICY_ID) \
  1570. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1571. return cmPolicies::POLICY_ID; \
  1572. }
  1573. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1574. #undef RETURN_POLICY_ID
  1575. assert(false && "Unreachable code. Not a valid policy");
  1576. return cmPolicies::CMP0002;
  1577. }
  1578. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1579. {
  1580. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1581. int NumExpectedParameters() const override { return 1; }
  1582. std::string Evaluate(
  1583. const std::vector<std::string>& parameters,
  1584. cmGeneratorExpressionContext* context,
  1585. const GeneratorExpressionContent* content,
  1586. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1587. {
  1588. if (!context->HeadTarget) {
  1589. reportError(
  1590. context, content->GetOriginalExpression(),
  1591. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1592. "may not be used with add_custom_command or add_custom_target.");
  1593. return std::string();
  1594. }
  1595. context->HadContextSensitiveCondition = true;
  1596. context->HadHeadSensitiveCondition = true;
  1597. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1598. const char* policy = targetPolicyWhitelist[i];
  1599. if (parameters.front() == policy) {
  1600. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1601. switch (statusForTarget(context->HeadTarget, policy)) {
  1602. case cmPolicies::WARN:
  1603. lg->IssueMessage(
  1604. MessageType::AUTHOR_WARNING,
  1605. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1606. CM_FALLTHROUGH;
  1607. case cmPolicies::REQUIRED_IF_USED:
  1608. case cmPolicies::REQUIRED_ALWAYS:
  1609. case cmPolicies::OLD:
  1610. return "0";
  1611. case cmPolicies::NEW:
  1612. return "1";
  1613. }
  1614. }
  1615. }
  1616. reportError(
  1617. context, content->GetOriginalExpression(),
  1618. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1619. "policies. Currently it may be used with the following policies:\n"
  1620. #define STRINGIFY_HELPER(X) #X
  1621. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1622. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1623. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1624. #undef TARGET_POLICY_LIST_ITEM
  1625. );
  1626. return std::string();
  1627. }
  1628. } targetPolicyNode;
  1629. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1630. {
  1631. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1632. bool GeneratesContent() const override { return true; }
  1633. int NumExpectedParameters() const override { return 0; }
  1634. std::string Evaluate(
  1635. const std::vector<std::string>& /*parameters*/,
  1636. cmGeneratorExpressionContext* context,
  1637. const GeneratorExpressionContent* content,
  1638. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1639. {
  1640. reportError(context, content->GetOriginalExpression(),
  1641. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1642. "should never be evaluated.");
  1643. return std::string();
  1644. }
  1645. } installPrefixNode;
  1646. class ArtifactDirTag;
  1647. class ArtifactLinkerTag;
  1648. class ArtifactNameTag;
  1649. class ArtifactPathTag;
  1650. class ArtifactPdbTag;
  1651. class ArtifactSonameTag;
  1652. class ArtifactBundleDirTag;
  1653. class ArtifactBundleContentDirTag;
  1654. template <typename ArtifactT>
  1655. struct TargetFilesystemArtifactResultCreator
  1656. {
  1657. static std::string Create(cmGeneratorTarget* target,
  1658. cmGeneratorExpressionContext* context,
  1659. const GeneratorExpressionContent* content);
  1660. };
  1661. template <>
  1662. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1663. {
  1664. static std::string Create(cmGeneratorTarget* target,
  1665. cmGeneratorExpressionContext* context,
  1666. const GeneratorExpressionContent* content)
  1667. {
  1668. // The target soname file (.so.1).
  1669. if (target->IsDLLPlatform()) {
  1670. ::reportError(context, content->GetOriginalExpression(),
  1671. "TARGET_SONAME_FILE is not allowed "
  1672. "for DLL target platforms.");
  1673. return std::string();
  1674. }
  1675. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1676. ::reportError(context, content->GetOriginalExpression(),
  1677. "TARGET_SONAME_FILE is allowed only for "
  1678. "SHARED libraries.");
  1679. return std::string();
  1680. }
  1681. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  1682. target->GetSOName(context->Config));
  1683. return result;
  1684. }
  1685. };
  1686. template <>
  1687. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1688. {
  1689. static std::string Create(cmGeneratorTarget* target,
  1690. cmGeneratorExpressionContext* context,
  1691. const GeneratorExpressionContent* content)
  1692. {
  1693. if (target->IsImported()) {
  1694. ::reportError(context, content->GetOriginalExpression(),
  1695. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1696. return std::string();
  1697. }
  1698. std::string language = target->GetLinkerLanguage(context->Config);
  1699. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1700. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1701. ::reportError(context, content->GetOriginalExpression(),
  1702. "TARGET_PDB_FILE is not supported by the target linker.");
  1703. return std::string();
  1704. }
  1705. cmStateEnums::TargetType targetType = target->GetType();
  1706. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1707. targetType != cmStateEnums::MODULE_LIBRARY &&
  1708. targetType != cmStateEnums::EXECUTABLE) {
  1709. ::reportError(context, content->GetOriginalExpression(),
  1710. "TARGET_PDB_FILE is allowed only for "
  1711. "targets with linker created artifacts.");
  1712. return std::string();
  1713. }
  1714. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  1715. '/', target->GetPDBName(context->Config));
  1716. return result;
  1717. }
  1718. };
  1719. template <>
  1720. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1721. {
  1722. static std::string Create(cmGeneratorTarget* target,
  1723. cmGeneratorExpressionContext* context,
  1724. const GeneratorExpressionContent* content)
  1725. {
  1726. // The file used to link to the target (.so, .lib, .a).
  1727. if (!target->IsLinkable()) {
  1728. ::reportError(context, content->GetOriginalExpression(),
  1729. "TARGET_LINKER_FILE is allowed only for libraries and "
  1730. "executables with ENABLE_EXPORTS.");
  1731. return std::string();
  1732. }
  1733. cmStateEnums::ArtifactType artifact =
  1734. target->HasImportLibrary(context->Config)
  1735. ? cmStateEnums::ImportLibraryArtifact
  1736. : cmStateEnums::RuntimeBinaryArtifact;
  1737. return target->GetFullPath(context->Config, artifact);
  1738. }
  1739. };
  1740. template <>
  1741. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1742. {
  1743. static std::string Create(cmGeneratorTarget* target,
  1744. cmGeneratorExpressionContext* context,
  1745. const GeneratorExpressionContent* content)
  1746. {
  1747. if (target->IsImported()) {
  1748. ::reportError(context, content->GetOriginalExpression(),
  1749. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1750. return std::string();
  1751. }
  1752. if (!target->IsBundleOnApple()) {
  1753. ::reportError(context, content->GetOriginalExpression(),
  1754. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1755. return std::string();
  1756. }
  1757. std::string outpath = target->GetDirectory(context->Config) + '/';
  1758. return target->BuildBundleDirectory(outpath, context->Config,
  1759. cmGeneratorTarget::BundleDirLevel);
  1760. }
  1761. };
  1762. template <>
  1763. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1764. {
  1765. static std::string Create(cmGeneratorTarget* target,
  1766. cmGeneratorExpressionContext* context,
  1767. const GeneratorExpressionContent* content)
  1768. {
  1769. if (target->IsImported()) {
  1770. ::reportError(
  1771. context, content->GetOriginalExpression(),
  1772. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1773. return std::string();
  1774. }
  1775. if (!target->IsBundleOnApple()) {
  1776. ::reportError(
  1777. context, content->GetOriginalExpression(),
  1778. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1779. return std::string();
  1780. }
  1781. std::string outpath = target->GetDirectory(context->Config) + '/';
  1782. return target->BuildBundleDirectory(outpath, context->Config,
  1783. cmGeneratorTarget::ContentLevel);
  1784. }
  1785. };
  1786. template <>
  1787. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1788. {
  1789. static std::string Create(cmGeneratorTarget* target,
  1790. cmGeneratorExpressionContext* context,
  1791. const GeneratorExpressionContent* /*unused*/)
  1792. {
  1793. return target->GetFullPath(context->Config,
  1794. cmStateEnums::RuntimeBinaryArtifact, true);
  1795. }
  1796. };
  1797. template <typename ArtifactT>
  1798. struct TargetFilesystemArtifactResultGetter
  1799. {
  1800. static std::string Get(const std::string& result);
  1801. };
  1802. template <>
  1803. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1804. {
  1805. static std::string Get(const std::string& result)
  1806. {
  1807. return cmSystemTools::GetFilenameName(result);
  1808. }
  1809. };
  1810. template <>
  1811. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1812. {
  1813. static std::string Get(const std::string& result)
  1814. {
  1815. return cmSystemTools::GetFilenamePath(result);
  1816. }
  1817. };
  1818. template <>
  1819. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1820. {
  1821. static std::string Get(const std::string& result) { return result; }
  1822. };
  1823. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1824. {
  1825. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1826. protected:
  1827. cmGeneratorTarget* GetTarget(
  1828. const std::vector<std::string>& parameters,
  1829. cmGeneratorExpressionContext* context,
  1830. const GeneratorExpressionContent* content,
  1831. cmGeneratorExpressionDAGChecker* dagChecker) const
  1832. {
  1833. // Lookup the referenced target.
  1834. std::string name = parameters.front();
  1835. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1836. ::reportError(context, content->GetOriginalExpression(),
  1837. "Expression syntax not recognized.");
  1838. return nullptr;
  1839. }
  1840. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1841. if (!target) {
  1842. ::reportError(context, content->GetOriginalExpression(),
  1843. "No target \"" + name + "\"");
  1844. return nullptr;
  1845. }
  1846. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1847. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1848. ::reportError(context, content->GetOriginalExpression(),
  1849. "Target \"" + name +
  1850. "\" is not an executable or library.");
  1851. return nullptr;
  1852. }
  1853. if (dagChecker &&
  1854. (dagChecker->EvaluatingLinkLibraries(target) ||
  1855. (dagChecker->EvaluatingSources() &&
  1856. target == dagChecker->TopTarget()))) {
  1857. ::reportError(context, content->GetOriginalExpression(),
  1858. "Expressions which require the linker language may not "
  1859. "be used while evaluating link libraries");
  1860. return nullptr;
  1861. }
  1862. return target;
  1863. }
  1864. };
  1865. template <typename ArtifactT, typename ComponentT>
  1866. struct TargetFilesystemArtifact : public TargetArtifactBase
  1867. {
  1868. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1869. int NumExpectedParameters() const override { return 1; }
  1870. std::string Evaluate(
  1871. const std::vector<std::string>& parameters,
  1872. cmGeneratorExpressionContext* context,
  1873. const GeneratorExpressionContent* content,
  1874. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1875. {
  1876. cmGeneratorTarget* target =
  1877. this->GetTarget(parameters, context, content, dagChecker);
  1878. if (!target) {
  1879. return std::string();
  1880. }
  1881. context->DependTargets.insert(target);
  1882. context->AllTargets.insert(target);
  1883. std::string result =
  1884. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1885. content);
  1886. if (context->HadError) {
  1887. return std::string();
  1888. }
  1889. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1890. }
  1891. };
  1892. template <typename ArtifactT>
  1893. struct TargetFilesystemArtifactNodeGroup
  1894. {
  1895. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  1896. {
  1897. }
  1898. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1899. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1900. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1901. };
  1902. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1903. targetNodeGroup;
  1904. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1905. targetLinkerNodeGroup;
  1906. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1907. targetSoNameNodeGroup;
  1908. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1909. targetPdbNodeGroup;
  1910. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1911. targetBundleDirNode;
  1912. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1913. ArtifactPathTag>
  1914. targetBundleContentDirNode;
  1915. //
  1916. // To retrieve base name for various artifacts
  1917. //
  1918. template <typename ArtifactT>
  1919. struct TargetOutputNameArtifactResultGetter
  1920. {
  1921. static std::string Get(cmGeneratorTarget* target,
  1922. cmGeneratorExpressionContext* context,
  1923. const GeneratorExpressionContent* content);
  1924. };
  1925. template <>
  1926. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  1927. {
  1928. static std::string Get(cmGeneratorTarget* target,
  1929. cmGeneratorExpressionContext* context,
  1930. const GeneratorExpressionContent* /*unused*/)
  1931. {
  1932. return target->GetOutputName(context->Config,
  1933. cmStateEnums::RuntimeBinaryArtifact) +
  1934. target->GetFilePostfix(context->Config);
  1935. }
  1936. };
  1937. template <>
  1938. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  1939. {
  1940. static std::string Get(cmGeneratorTarget* target,
  1941. cmGeneratorExpressionContext* context,
  1942. const GeneratorExpressionContent* content)
  1943. {
  1944. // The file used to link to the target (.so, .lib, .a).
  1945. if (!target->IsLinkable()) {
  1946. ::reportError(context, content->GetOriginalExpression(),
  1947. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  1948. "libraries and executables with ENABLE_EXPORTS.");
  1949. return std::string();
  1950. }
  1951. cmStateEnums::ArtifactType artifact =
  1952. target->HasImportLibrary(context->Config)
  1953. ? cmStateEnums::ImportLibraryArtifact
  1954. : cmStateEnums::RuntimeBinaryArtifact;
  1955. return target->GetOutputName(context->Config, artifact) +
  1956. target->GetFilePostfix(context->Config);
  1957. }
  1958. };
  1959. template <>
  1960. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  1961. {
  1962. static std::string Get(cmGeneratorTarget* target,
  1963. cmGeneratorExpressionContext* context,
  1964. const GeneratorExpressionContent* content)
  1965. {
  1966. if (target->IsImported()) {
  1967. ::reportError(
  1968. context, content->GetOriginalExpression(),
  1969. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  1970. return std::string();
  1971. }
  1972. std::string language = target->GetLinkerLanguage(context->Config);
  1973. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1974. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1975. ::reportError(
  1976. context, content->GetOriginalExpression(),
  1977. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  1978. return std::string();
  1979. }
  1980. cmStateEnums::TargetType targetType = target->GetType();
  1981. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1982. targetType != cmStateEnums::MODULE_LIBRARY &&
  1983. targetType != cmStateEnums::EXECUTABLE) {
  1984. ::reportError(context, content->GetOriginalExpression(),
  1985. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  1986. "targets with linker created artifacts.");
  1987. return std::string();
  1988. }
  1989. return target->GetPDBOutputName(context->Config) +
  1990. target->GetFilePostfix(context->Config);
  1991. }
  1992. };
  1993. template <typename ArtifactT>
  1994. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  1995. {
  1996. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  1997. int NumExpectedParameters() const override { return 1; }
  1998. std::string Evaluate(
  1999. const std::vector<std::string>& parameters,
  2000. cmGeneratorExpressionContext* context,
  2001. const GeneratorExpressionContent* content,
  2002. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2003. {
  2004. cmGeneratorTarget* target =
  2005. this->GetTarget(parameters, context, content, dagChecker);
  2006. if (!target) {
  2007. return std::string();
  2008. }
  2009. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2010. target, context, content);
  2011. if (context->HadError) {
  2012. return std::string();
  2013. }
  2014. return result;
  2015. }
  2016. };
  2017. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2018. targetFileBaseNameNode;
  2019. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2020. targetLinkerFileBaseNameNode;
  2021. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2022. targetPdbFileBaseNameNode;
  2023. class ArtifactFilePrefixTag;
  2024. class ArtifactLinkerFilePrefixTag;
  2025. class ArtifactFileSuffixTag;
  2026. class ArtifactLinkerFileSuffixTag;
  2027. template <typename ArtifactT>
  2028. struct TargetFileArtifactResultGetter
  2029. {
  2030. static std::string Get(cmGeneratorTarget* target,
  2031. cmGeneratorExpressionContext* context,
  2032. const GeneratorExpressionContent* content);
  2033. };
  2034. template <>
  2035. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2036. {
  2037. static std::string Get(cmGeneratorTarget* target,
  2038. cmGeneratorExpressionContext* context,
  2039. const GeneratorExpressionContent*)
  2040. {
  2041. return target->GetFilePrefix(context->Config);
  2042. }
  2043. };
  2044. template <>
  2045. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2046. {
  2047. static std::string Get(cmGeneratorTarget* target,
  2048. cmGeneratorExpressionContext* context,
  2049. const GeneratorExpressionContent* content)
  2050. {
  2051. if (!target->IsLinkable()) {
  2052. ::reportError(context, content->GetOriginalExpression(),
  2053. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2054. "executables with ENABLE_EXPORTS.");
  2055. return std::string();
  2056. }
  2057. cmStateEnums::ArtifactType artifact =
  2058. target->HasImportLibrary(context->Config)
  2059. ? cmStateEnums::ImportLibraryArtifact
  2060. : cmStateEnums::RuntimeBinaryArtifact;
  2061. return target->GetFilePrefix(context->Config, artifact);
  2062. }
  2063. };
  2064. template <>
  2065. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2066. {
  2067. static std::string Get(cmGeneratorTarget* target,
  2068. cmGeneratorExpressionContext* context,
  2069. const GeneratorExpressionContent*)
  2070. {
  2071. return target->GetFileSuffix(context->Config);
  2072. }
  2073. };
  2074. template <>
  2075. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2076. {
  2077. static std::string Get(cmGeneratorTarget* target,
  2078. cmGeneratorExpressionContext* context,
  2079. const GeneratorExpressionContent* content)
  2080. {
  2081. if (!target->IsLinkable()) {
  2082. ::reportError(context, content->GetOriginalExpression(),
  2083. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2084. "executables with ENABLE_EXPORTS.");
  2085. return std::string();
  2086. }
  2087. cmStateEnums::ArtifactType artifact =
  2088. target->HasImportLibrary(context->Config)
  2089. ? cmStateEnums::ImportLibraryArtifact
  2090. : cmStateEnums::RuntimeBinaryArtifact;
  2091. return target->GetFileSuffix(context->Config, artifact);
  2092. }
  2093. };
  2094. template <typename ArtifactT>
  2095. struct TargetFileArtifact : public TargetArtifactBase
  2096. {
  2097. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2098. int NumExpectedParameters() const override { return 1; }
  2099. std::string Evaluate(
  2100. const std::vector<std::string>& parameters,
  2101. cmGeneratorExpressionContext* context,
  2102. const GeneratorExpressionContent* content,
  2103. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2104. {
  2105. cmGeneratorTarget* target =
  2106. this->GetTarget(parameters, context, content, dagChecker);
  2107. if (!target) {
  2108. return std::string();
  2109. }
  2110. std::string result =
  2111. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2112. if (context->HadError) {
  2113. return std::string();
  2114. }
  2115. return result;
  2116. }
  2117. };
  2118. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2119. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2120. targetLinkerFilePrefixNode;
  2121. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2122. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2123. targetLinkerFileSuffixNode;
  2124. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2125. {
  2126. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2127. std::string Evaluate(
  2128. const std::vector<std::string>& parameters,
  2129. cmGeneratorExpressionContext* context,
  2130. const GeneratorExpressionContent* content,
  2131. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2132. {
  2133. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2134. if (listIn.empty()) {
  2135. reportError(context, content->GetOriginalExpression(),
  2136. "\"\" is not an absolute path.");
  2137. return std::string();
  2138. }
  2139. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2140. cmOutputConverter converter(snapshot);
  2141. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2142. std::vector<std::string> listOut;
  2143. listOut.reserve(listIn.size());
  2144. for (auto const& in : listIn) {
  2145. if (!cmSystemTools::FileIsFullPath(in)) {
  2146. reportError(context, content->GetOriginalExpression(),
  2147. "\"" + in + "\" is not an absolute path.");
  2148. return std::string();
  2149. }
  2150. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2151. }
  2152. return cmJoin(listOut, separator);
  2153. }
  2154. } shellPathNode;
  2155. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2156. const std::string& identifier)
  2157. {
  2158. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2159. { "0", &zeroNode },
  2160. { "1", &oneNode },
  2161. { "AND", &andNode },
  2162. { "OR", &orNode },
  2163. { "NOT", &notNode },
  2164. { "C_COMPILER_ID", &cCompilerIdNode },
  2165. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2166. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2167. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2168. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2169. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2170. { "VERSION_GREATER", &versionGreaterNode },
  2171. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2172. { "VERSION_LESS", &versionLessNode },
  2173. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2174. { "VERSION_EQUAL", &versionEqualNode },
  2175. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2176. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2177. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2178. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2179. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2180. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2181. { "PLATFORM_ID", &platformIdNode },
  2182. { "COMPILE_FEATURES", &compileFeaturesNode },
  2183. { "CONFIGURATION", &configurationNode },
  2184. { "CONFIG", &configurationTestNode },
  2185. { "TARGET_FILE", &targetNodeGroup.File },
  2186. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2187. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2188. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2189. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2190. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2191. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2192. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2193. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2194. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2195. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2196. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2197. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2198. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2199. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2200. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2201. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2202. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2203. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2204. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2205. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2206. { "STREQUAL", &strEqualNode },
  2207. { "EQUAL", &equalNode },
  2208. { "IN_LIST", &inListNode },
  2209. { "FILTER", &filterNode },
  2210. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2211. { "LOWER_CASE", &lowerCaseNode },
  2212. { "UPPER_CASE", &upperCaseNode },
  2213. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2214. { "BOOL", &boolNode },
  2215. { "IF", &ifNode },
  2216. { "ANGLE-R", &angle_rNode },
  2217. { "COMMA", &commaNode },
  2218. { "SEMICOLON", &semicolonNode },
  2219. { "TARGET_PROPERTY", &targetPropertyNode },
  2220. { "TARGET_NAME", &targetNameNode },
  2221. { "TARGET_OBJECTS", &targetObjectsNode },
  2222. { "TARGET_POLICY", &targetPolicyNode },
  2223. { "TARGET_EXISTS", &targetExistsNode },
  2224. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2225. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2226. { "GENEX_EVAL", &genexEvalNode },
  2227. { "BUILD_INTERFACE", &buildInterfaceNode },
  2228. { "INSTALL_INTERFACE", &installInterfaceNode },
  2229. { "INSTALL_PREFIX", &installPrefixNode },
  2230. { "JOIN", &joinNode },
  2231. { "LINK_ONLY", &linkOnlyNode },
  2232. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2233. { "COMPILE_LANGUAGE", &languageNode },
  2234. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2235. { "LINK_LANGUAGE", &linkLanguageNode },
  2236. { "HOST_LINK", &hostLinkNode },
  2237. { "DEVICE_LINK", &deviceLinkNode },
  2238. { "SHELL_PATH", &shellPathNode }
  2239. };
  2240. {
  2241. auto itr = nodeMap.find(identifier);
  2242. if (itr != nodeMap.end()) {
  2243. return itr->second;
  2244. }
  2245. }
  2246. return nullptr;
  2247. }
  2248. void reportError(cmGeneratorExpressionContext* context,
  2249. const std::string& expr, const std::string& result)
  2250. {
  2251. context->HadError = true;
  2252. if (context->Quiet) {
  2253. return;
  2254. }
  2255. std::ostringstream e;
  2256. /* clang-format off */
  2257. e << "Error evaluating generator expression:\n"
  2258. << " " << expr << "\n"
  2259. << result;
  2260. /* clang-format on */
  2261. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2262. e.str(), context->Backtrace);
  2263. }