cmGeneratorExpressionNode.cxx 89 KB

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