cmGeneratorExpressionNode.cxx 88 KB

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