cmGeneratorExpressionNode.cxx 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394
  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 <functional>
  10. #include <map>
  11. #include <memory>
  12. #include <set>
  13. #include <sstream>
  14. #include <unordered_map>
  15. #include <utility>
  16. #include <cm/iterator>
  17. #include <cm/optional>
  18. #include <cm/string_view>
  19. #include <cm/vector>
  20. #include <cmext/algorithm>
  21. #include <cmext/string_view>
  22. #include "cmsys/RegularExpression.hxx"
  23. #include "cmsys/String.h"
  24. #include "cmAlgorithms.h"
  25. #include "cmCMakePath.h"
  26. #include "cmComputeLinkInformation.h"
  27. #include "cmGeneratorExpression.h"
  28. #include "cmGeneratorExpressionContext.h"
  29. #include "cmGeneratorExpressionDAGChecker.h"
  30. #include "cmGeneratorExpressionEvaluator.h"
  31. #include "cmGeneratorTarget.h"
  32. #include "cmGlobalGenerator.h"
  33. #include "cmLinkItem.h"
  34. #include "cmLocalGenerator.h"
  35. #include "cmMakefile.h"
  36. #include "cmMessageType.h"
  37. #include "cmOutputConverter.h"
  38. #include "cmPolicies.h"
  39. #include "cmRange.h"
  40. #include "cmStandardLevelResolver.h"
  41. #include "cmState.h"
  42. #include "cmStateSnapshot.h"
  43. #include "cmStateTypes.h"
  44. #include "cmStringAlgorithms.h"
  45. #include "cmSystemTools.h"
  46. #include "cmTarget.h"
  47. #include "cmValue.h"
  48. #include "cmake.h"
  49. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  50. std::string const& prop, cmLocalGenerator* lg,
  51. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  52. cmGeneratorExpressionDAGChecker* dagChecker,
  53. cmGeneratorTarget const* currentTarget)
  54. {
  55. cmGeneratorExpression ge(*lg->GetCMakeInstance(), context->Backtrace);
  56. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  57. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  58. cge->SetQuiet(context->Quiet);
  59. std::string result =
  60. cge->Evaluate(lg, context->Config, headTarget, dagChecker, currentTarget,
  61. context->Language);
  62. if (cge->GetHadContextSensitiveCondition()) {
  63. context->HadContextSensitiveCondition = true;
  64. }
  65. if (cge->GetHadHeadSensitiveCondition()) {
  66. context->HadHeadSensitiveCondition = true;
  67. }
  68. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  69. context->HadLinkLanguageSensitiveCondition = true;
  70. }
  71. return result;
  72. }
  73. static const struct ZeroNode : public cmGeneratorExpressionNode
  74. {
  75. ZeroNode() {} // NOLINT(modernize-use-equals-default)
  76. bool GeneratesContent() const override { return false; }
  77. bool AcceptsArbitraryContentParameter() const override { return true; }
  78. std::string Evaluate(
  79. const std::vector<std::string>& /*parameters*/,
  80. cmGeneratorExpressionContext* /*context*/,
  81. const GeneratorExpressionContent* /*content*/,
  82. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  83. {
  84. return std::string();
  85. }
  86. } zeroNode;
  87. static const struct OneNode : public cmGeneratorExpressionNode
  88. {
  89. OneNode() {} // NOLINT(modernize-use-equals-default)
  90. bool AcceptsArbitraryContentParameter() const override { return true; }
  91. std::string Evaluate(
  92. const std::vector<std::string>& parameters,
  93. cmGeneratorExpressionContext* /*context*/,
  94. const GeneratorExpressionContent* /*content*/,
  95. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  96. {
  97. return parameters.front();
  98. }
  99. } oneNode;
  100. static const struct OneNode buildInterfaceNode;
  101. static const struct ZeroNode installInterfaceNode;
  102. static const struct OneNode buildLocalInterfaceNode;
  103. struct BooleanOpNode : public cmGeneratorExpressionNode
  104. {
  105. BooleanOpNode(const char* op_, const char* successVal_,
  106. const char* failureVal_)
  107. : op(op_)
  108. , successVal(successVal_)
  109. , failureVal(failureVal_)
  110. {
  111. }
  112. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  113. std::string Evaluate(const std::vector<std::string>& parameters,
  114. cmGeneratorExpressionContext* context,
  115. const GeneratorExpressionContent* content,
  116. cmGeneratorExpressionDAGChecker*) const override
  117. {
  118. for (std::string const& param : parameters) {
  119. if (param == this->failureVal) {
  120. return this->failureVal;
  121. }
  122. if (param != this->successVal) {
  123. std::ostringstream e;
  124. e << "Parameters to $<" << this->op;
  125. e << "> must resolve to either '0' or '1'.";
  126. reportError(context, content->GetOriginalExpression(), e.str());
  127. return std::string();
  128. }
  129. }
  130. return this->successVal;
  131. }
  132. const char *const op, *const successVal, *const failureVal;
  133. };
  134. static const BooleanOpNode andNode("AND", "1", "0"), orNode("OR", "0", "1");
  135. static const struct NotNode : public cmGeneratorExpressionNode
  136. {
  137. NotNode() {} // NOLINT(modernize-use-equals-default)
  138. std::string Evaluate(
  139. const std::vector<std::string>& parameters,
  140. cmGeneratorExpressionContext* context,
  141. const GeneratorExpressionContent* content,
  142. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  143. {
  144. if (parameters.front() != "0" && parameters.front() != "1") {
  145. reportError(
  146. context, content->GetOriginalExpression(),
  147. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  148. return std::string();
  149. }
  150. return parameters.front() == "0" ? "1" : "0";
  151. }
  152. } notNode;
  153. static const struct BoolNode : public cmGeneratorExpressionNode
  154. {
  155. BoolNode() {} // NOLINT(modernize-use-equals-default)
  156. int NumExpectedParameters() const override { return 1; }
  157. std::string Evaluate(
  158. const std::vector<std::string>& parameters,
  159. cmGeneratorExpressionContext* /*context*/,
  160. const GeneratorExpressionContent* /*content*/,
  161. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  162. {
  163. return !cmIsOff(parameters.front()) ? "1" : "0";
  164. }
  165. } boolNode;
  166. static const struct IfNode : public cmGeneratorExpressionNode
  167. {
  168. IfNode() {} // NOLINT(modernize-use-equals-default)
  169. int NumExpectedParameters() const override { return 3; }
  170. std::string Evaluate(const std::vector<std::string>& parameters,
  171. cmGeneratorExpressionContext* context,
  172. const GeneratorExpressionContent* content,
  173. cmGeneratorExpressionDAGChecker*) const override
  174. {
  175. if (parameters[0] != "1" && parameters[0] != "0") {
  176. reportError(context, content->GetOriginalExpression(),
  177. "First parameter to $<IF> must resolve to exactly one '0' "
  178. "or '1' value.");
  179. return std::string();
  180. }
  181. return parameters[0] == "1" ? parameters[1] : parameters[2];
  182. }
  183. } ifNode;
  184. static const struct StrEqualNode : public cmGeneratorExpressionNode
  185. {
  186. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  187. int NumExpectedParameters() const override { return 2; }
  188. std::string Evaluate(
  189. const std::vector<std::string>& parameters,
  190. cmGeneratorExpressionContext* /*context*/,
  191. const GeneratorExpressionContent* /*content*/,
  192. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  193. {
  194. return parameters.front() == parameters[1] ? "1" : "0";
  195. }
  196. } strEqualNode;
  197. static const struct EqualNode : public cmGeneratorExpressionNode
  198. {
  199. EqualNode() {} // NOLINT(modernize-use-equals-default)
  200. int NumExpectedParameters() const override { return 2; }
  201. std::string Evaluate(
  202. const std::vector<std::string>& parameters,
  203. cmGeneratorExpressionContext* context,
  204. const GeneratorExpressionContent* content,
  205. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  206. {
  207. long numbers[2];
  208. for (int i = 0; i < 2; ++i) {
  209. if (!ParameterToLong(parameters[i].c_str(), &numbers[i])) {
  210. reportError(context, content->GetOriginalExpression(),
  211. "$<EQUAL> parameter " + parameters[i] +
  212. " is not a valid integer.");
  213. return {};
  214. }
  215. }
  216. return numbers[0] == numbers[1] ? "1" : "0";
  217. }
  218. static bool ParameterToLong(const char* param, long* outResult)
  219. {
  220. const char isNegative = param[0] == '-';
  221. int base = 0;
  222. if (cmHasLiteralPrefix(param, "0b") || cmHasLiteralPrefix(param, "0B")) {
  223. base = 2;
  224. param += 2;
  225. } else if (cmHasLiteralPrefix(param, "-0b") ||
  226. cmHasLiteralPrefix(param, "-0B") ||
  227. cmHasLiteralPrefix(param, "+0b") ||
  228. cmHasLiteralPrefix(param, "+0B")) {
  229. base = 2;
  230. param += 3;
  231. }
  232. char* pEnd;
  233. long result = strtol(param, &pEnd, base);
  234. if (pEnd == param || *pEnd != '\0' || errno == ERANGE) {
  235. return false;
  236. }
  237. if (isNegative && result > 0) {
  238. result *= -1;
  239. }
  240. *outResult = result;
  241. return true;
  242. }
  243. } equalNode;
  244. static const struct InListNode : public cmGeneratorExpressionNode
  245. {
  246. InListNode() {} // NOLINT(modernize-use-equals-default)
  247. int NumExpectedParameters() const override { return 2; }
  248. std::string Evaluate(
  249. const std::vector<std::string>& parameters,
  250. cmGeneratorExpressionContext* context,
  251. const GeneratorExpressionContent* /*content*/,
  252. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  253. {
  254. std::vector<std::string> values;
  255. std::vector<std::string> checkValues;
  256. bool check = false;
  257. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  258. case cmPolicies::WARN:
  259. if (parameters.front().empty()) {
  260. check = true;
  261. cmExpandList(parameters[1], checkValues, true);
  262. }
  263. CM_FALLTHROUGH;
  264. case cmPolicies::OLD:
  265. cmExpandList(parameters[1], values);
  266. if (check && values != checkValues) {
  267. std::ostringstream e;
  268. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  269. << "\nSearch Item:\n \"" << parameters.front()
  270. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  271. context->LG->GetCMakeInstance()->IssueMessage(
  272. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  273. return "0";
  274. }
  275. if (values.empty()) {
  276. return "0";
  277. }
  278. break;
  279. case cmPolicies::REQUIRED_IF_USED:
  280. case cmPolicies::REQUIRED_ALWAYS:
  281. case cmPolicies::NEW:
  282. cmExpandList(parameters[1], values, true);
  283. break;
  284. }
  285. return cm::contains(values, parameters.front()) ? "1" : "0";
  286. }
  287. } inListNode;
  288. static const struct FilterNode : public cmGeneratorExpressionNode
  289. {
  290. FilterNode() {} // NOLINT(modernize-use-equals-default)
  291. int NumExpectedParameters() const override { return 3; }
  292. std::string Evaluate(
  293. const std::vector<std::string>& parameters,
  294. cmGeneratorExpressionContext* context,
  295. const GeneratorExpressionContent* content,
  296. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  297. {
  298. if (parameters.size() != 3) {
  299. reportError(context, content->GetOriginalExpression(),
  300. "$<FILTER:...> expression requires three parameters");
  301. return {};
  302. }
  303. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  304. reportError(
  305. context, content->GetOriginalExpression(),
  306. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  307. return {};
  308. }
  309. const bool exclude = parameters[1] == "EXCLUDE";
  310. cmsys::RegularExpression re;
  311. if (!re.compile(parameters[2])) {
  312. reportError(context, content->GetOriginalExpression(),
  313. "$<FILTER:...> failed to compile regex");
  314. return {};
  315. }
  316. std::vector<std::string> values;
  317. std::vector<std::string> result;
  318. cmExpandList(parameters.front(), values, true);
  319. std::copy_if(values.cbegin(), values.cend(), std::back_inserter(result),
  320. [&re, exclude](std::string const& input) {
  321. return exclude ^ re.find(input);
  322. });
  323. return cmJoin(cmMakeRange(result.cbegin(), result.cend()), ";");
  324. }
  325. } filterNode;
  326. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  327. {
  328. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  329. int NumExpectedParameters() const override { return 1; }
  330. std::string Evaluate(
  331. const std::vector<std::string>& parameters,
  332. cmGeneratorExpressionContext* context,
  333. const GeneratorExpressionContent* content,
  334. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  335. {
  336. if (parameters.size() != 1) {
  337. reportError(
  338. context, content->GetOriginalExpression(),
  339. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  340. }
  341. std::vector<std::string> values = cmExpandedList(parameters.front(), true);
  342. auto valuesEnd = cmRemoveDuplicates(values);
  343. auto valuesBegin = values.cbegin();
  344. return cmJoin(cmMakeRange(valuesBegin, valuesEnd), ";");
  345. }
  346. } removeDuplicatesNode;
  347. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  348. {
  349. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  350. int NumExpectedParameters() const override { return 1; }
  351. std::string Evaluate(
  352. const std::vector<std::string>& parameters,
  353. cmGeneratorExpressionContext* context,
  354. const GeneratorExpressionContent* content,
  355. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  356. {
  357. if (parameters.size() != 1) {
  358. reportError(context, content->GetOriginalExpression(),
  359. "$<TARGET_EXISTS:...> expression requires one parameter");
  360. return std::string();
  361. }
  362. std::string const& targetName = parameters.front();
  363. if (targetName.empty() ||
  364. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  365. reportError(context, content->GetOriginalExpression(),
  366. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  367. "valid target name.");
  368. return std::string();
  369. }
  370. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  371. }
  372. } targetExistsNode;
  373. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  374. {
  375. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  376. int NumExpectedParameters() const override { return 1; }
  377. std::string Evaluate(
  378. const std::vector<std::string>& parameters,
  379. cmGeneratorExpressionContext* context,
  380. const GeneratorExpressionContent* content,
  381. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  382. {
  383. if (parameters.size() != 1) {
  384. reportError(context, content->GetOriginalExpression(),
  385. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  386. "parameter");
  387. return std::string();
  388. }
  389. std::string const& targetName = parameters.front();
  390. if (targetName.empty() ||
  391. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  392. reportError(context, content->GetOriginalExpression(),
  393. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  394. "non-empty valid target name.");
  395. return std::string();
  396. }
  397. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  398. ? targetName
  399. : std::string();
  400. }
  401. } targetNameIfExistsNode;
  402. struct GenexEvaluator : public cmGeneratorExpressionNode
  403. {
  404. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  405. protected:
  406. std::string EvaluateExpression(
  407. const std::string& genexOperator, const std::string& expression,
  408. cmGeneratorExpressionContext* context,
  409. const GeneratorExpressionContent* content,
  410. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  411. {
  412. if (context->HeadTarget) {
  413. cmGeneratorExpressionDAGChecker dagChecker(
  414. context->Backtrace, context->HeadTarget,
  415. genexOperator + ":" + expression, content, dagCheckerParent);
  416. switch (dagChecker.Check()) {
  417. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  418. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  419. dagChecker.ReportError(context, content->GetOriginalExpression());
  420. return std::string();
  421. }
  422. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  423. case cmGeneratorExpressionDAGChecker::DAG:
  424. break;
  425. }
  426. return this->EvaluateDependentExpression(
  427. expression, context->LG, context, context->HeadTarget, &dagChecker,
  428. context->CurrentTarget);
  429. }
  430. return this->EvaluateDependentExpression(
  431. expression, context->LG, context, context->HeadTarget, dagCheckerParent,
  432. context->CurrentTarget);
  433. }
  434. };
  435. static const struct TargetGenexEvalNode : public GenexEvaluator
  436. {
  437. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  438. int NumExpectedParameters() const override { return 2; }
  439. bool AcceptsArbitraryContentParameter() const override { return true; }
  440. std::string Evaluate(
  441. const std::vector<std::string>& parameters,
  442. cmGeneratorExpressionContext* context,
  443. const GeneratorExpressionContent* content,
  444. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  445. {
  446. const std::string& targetName = parameters.front();
  447. if (targetName.empty() ||
  448. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  449. reportError(context, content->GetOriginalExpression(),
  450. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  451. "non-empty valid target name.");
  452. return std::string();
  453. }
  454. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  455. if (!target) {
  456. std::ostringstream e;
  457. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  458. << "\" not found.";
  459. reportError(context, content->GetOriginalExpression(), e.str());
  460. return std::string();
  461. }
  462. const std::string& expression = parameters[1];
  463. if (expression.empty()) {
  464. return expression;
  465. }
  466. cmGeneratorExpressionContext targetContext(
  467. context->LG, context->Config, context->Quiet, target, target,
  468. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  469. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  470. &targetContext, content, dagCheckerParent);
  471. }
  472. } targetGenexEvalNode;
  473. static const struct GenexEvalNode : public GenexEvaluator
  474. {
  475. GenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  476. int NumExpectedParameters() const override { return 1; }
  477. bool AcceptsArbitraryContentParameter() const override { return true; }
  478. std::string Evaluate(
  479. const std::vector<std::string>& parameters,
  480. cmGeneratorExpressionContext* context,
  481. const GeneratorExpressionContent* content,
  482. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  483. {
  484. const std::string& expression = parameters[0];
  485. if (expression.empty()) {
  486. return expression;
  487. }
  488. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  489. dagCheckerParent);
  490. }
  491. } genexEvalNode;
  492. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  493. {
  494. LowerCaseNode() {} // NOLINT(modernize-use-equals-default)
  495. bool AcceptsArbitraryContentParameter() const override { return true; }
  496. std::string Evaluate(
  497. const std::vector<std::string>& parameters,
  498. cmGeneratorExpressionContext* /*context*/,
  499. const GeneratorExpressionContent* /*content*/,
  500. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  501. {
  502. return cmSystemTools::LowerCase(parameters.front());
  503. }
  504. } lowerCaseNode;
  505. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  506. {
  507. UpperCaseNode() {} // NOLINT(modernize-use-equals-default)
  508. bool AcceptsArbitraryContentParameter() const override { return true; }
  509. std::string Evaluate(
  510. const std::vector<std::string>& parameters,
  511. cmGeneratorExpressionContext* /*context*/,
  512. const GeneratorExpressionContent* /*content*/,
  513. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  514. {
  515. return cmSystemTools::UpperCase(parameters.front());
  516. }
  517. } upperCaseNode;
  518. namespace {
  519. template <typename Container>
  520. class Range : public cmRange<typename Container::const_iterator>
  521. {
  522. private:
  523. using Base = cmRange<typename Container::const_iterator>;
  524. public:
  525. using const_iterator = typename Container::const_iterator;
  526. using value_type = typename Container::value_type;
  527. using size_type = typename Container::size_type;
  528. using difference_type = typename Container::difference_type;
  529. using const_reference = typename Container::const_reference;
  530. Range(const Container& container)
  531. : Base(container.begin(), container.end())
  532. {
  533. }
  534. const_reference operator[](size_type pos) const
  535. {
  536. return *(this->begin() + pos);
  537. }
  538. const_reference front() const { return *this->begin(); }
  539. const_reference back() const { return *std::prev(this->end()); }
  540. Range& advance(difference_type amount) &
  541. {
  542. Base::advance(amount);
  543. return *this;
  544. }
  545. Range advance(difference_type amount) &&
  546. {
  547. Base::advance(amount);
  548. return std::move(*this);
  549. }
  550. };
  551. using Arguments = Range<std::vector<std::string>>;
  552. bool CheckPathParametersEx(cmGeneratorExpressionContext* ctx,
  553. const GeneratorExpressionContent* cnt,
  554. cm::string_view option, std::size_t count,
  555. int required = 1, bool exactly = true)
  556. {
  557. if (static_cast<int>(count) < required ||
  558. (exactly && static_cast<int>(count) > required)) {
  559. reportError(ctx, cnt->GetOriginalExpression(),
  560. cmStrCat("$<PATH:", option, "> expression requires ",
  561. (exactly ? "exactly" : "at least"), ' ',
  562. (required == 1 ? "one parameter" : "two parameters"),
  563. '.'));
  564. return false;
  565. }
  566. return true;
  567. };
  568. bool CheckPathParameters(cmGeneratorExpressionContext* ctx,
  569. const GeneratorExpressionContent* cnt,
  570. cm::string_view option, const Arguments& args,
  571. int required = 1)
  572. {
  573. return CheckPathParametersEx(ctx, cnt, option, args.size(), required);
  574. };
  575. std::string ToString(bool isTrue)
  576. {
  577. return isTrue ? "1" : "0";
  578. };
  579. }
  580. static const struct PathNode : public cmGeneratorExpressionNode
  581. {
  582. PathNode() {} // NOLINT(modernize-use-equals-default)
  583. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  584. bool AcceptsArbitraryContentParameter() const override { return true; }
  585. std::string Evaluate(
  586. const std::vector<std::string>& parameters,
  587. cmGeneratorExpressionContext* context,
  588. const GeneratorExpressionContent* content,
  589. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  590. {
  591. static std::unordered_map<
  592. cm::string_view,
  593. std::function<std::string(cmGeneratorExpressionContext*,
  594. const GeneratorExpressionContent*,
  595. Arguments&)>>
  596. pathCommands{
  597. { "GET_ROOT_NAME"_s,
  598. [](cmGeneratorExpressionContext* ctx,
  599. const GeneratorExpressionContent* cnt,
  600. Arguments& args) -> std::string {
  601. return CheckPathParameters(ctx, cnt, "GET_ROOT_NAME"_s, args) &&
  602. !args.front().empty()
  603. ? cmCMakePath{ args.front() }.GetRootName().String()
  604. : std::string{};
  605. } },
  606. { "GET_ROOT_DIRECTORY"_s,
  607. [](cmGeneratorExpressionContext* ctx,
  608. const GeneratorExpressionContent* cnt,
  609. Arguments& args) -> std::string {
  610. return CheckPathParameters(ctx, cnt, "GET_ROOT_DIRECTORY"_s,
  611. args) &&
  612. !args.front().empty()
  613. ? cmCMakePath{ args.front() }.GetRootDirectory().String()
  614. : std::string{};
  615. } },
  616. { "GET_ROOT_PATH"_s,
  617. [](cmGeneratorExpressionContext* ctx,
  618. const GeneratorExpressionContent* cnt,
  619. Arguments& args) -> std::string {
  620. return CheckPathParameters(ctx, cnt, "GET_ROOT_PATH"_s, args) &&
  621. !args.front().empty()
  622. ? cmCMakePath{ args.front() }.GetRootPath().String()
  623. : std::string{};
  624. } },
  625. { "GET_FILENAME"_s,
  626. [](cmGeneratorExpressionContext* ctx,
  627. const GeneratorExpressionContent* cnt,
  628. Arguments& args) -> std::string {
  629. return CheckPathParameters(ctx, cnt, "GET_FILENAME"_s, args) &&
  630. !args.front().empty()
  631. ? cmCMakePath{ args.front() }.GetFileName().String()
  632. : std::string{};
  633. } },
  634. { "GET_EXTENSION"_s,
  635. [](cmGeneratorExpressionContext* ctx,
  636. const GeneratorExpressionContent* cnt,
  637. Arguments& args) -> std::string {
  638. bool lastOnly = args.front() == "LAST_ONLY"_s;
  639. if (lastOnly) {
  640. args.advance(1);
  641. }
  642. if (CheckPathParametersEx(ctx, cnt,
  643. lastOnly ? "GET_EXTENSION,LAST_ONLY"_s
  644. : "GET_EXTENSION"_s,
  645. args.size())) {
  646. if (args.front().empty()) {
  647. return std::string{};
  648. }
  649. return lastOnly
  650. ? cmCMakePath{ args.front() }.GetExtension().String()
  651. : cmCMakePath{ args.front() }.GetWideExtension().String();
  652. }
  653. return std::string{};
  654. } },
  655. { "GET_STEM"_s,
  656. [](cmGeneratorExpressionContext* ctx,
  657. const GeneratorExpressionContent* cnt,
  658. Arguments& args) -> std::string {
  659. bool lastOnly = args.front() == "LAST_ONLY"_s;
  660. if (lastOnly) {
  661. args.advance(1);
  662. }
  663. if (CheckPathParametersEx(
  664. ctx, cnt, lastOnly ? "GET_STEM,LAST_ONLY"_s : "GET_STEM"_s,
  665. args.size())) {
  666. if (args.front().empty()) {
  667. return std::string{};
  668. }
  669. return lastOnly
  670. ? cmCMakePath{ args.front() }.GetStem().String()
  671. : cmCMakePath{ args.front() }.GetNarrowStem().String();
  672. }
  673. return std::string{};
  674. } },
  675. { "GET_RELATIVE_PART"_s,
  676. [](cmGeneratorExpressionContext* ctx,
  677. const GeneratorExpressionContent* cnt,
  678. Arguments& args) -> std::string {
  679. return CheckPathParameters(ctx, cnt, "GET_RELATIVE_PART"_s,
  680. args) &&
  681. !args.front().empty()
  682. ? cmCMakePath{ args.front() }.GetRelativePath().String()
  683. : std::string{};
  684. } },
  685. { "GET_PARENT_PATH"_s,
  686. [](cmGeneratorExpressionContext* ctx,
  687. const GeneratorExpressionContent* cnt,
  688. Arguments& args) -> std::string {
  689. return CheckPathParameters(ctx, cnt, "GET_PARENT_PATH"_s, args)
  690. ? cmCMakePath{ args.front() }.GetParentPath().String()
  691. : std::string{};
  692. } },
  693. { "HAS_ROOT_NAME"_s,
  694. [](cmGeneratorExpressionContext* ctx,
  695. const GeneratorExpressionContent* cnt,
  696. Arguments& args) -> std::string {
  697. return CheckPathParameters(ctx, cnt, "HAS_ROOT_NAME"_s, args)
  698. ? ToString(cmCMakePath{ args.front() }.HasRootName())
  699. : std::string{ "0" };
  700. } },
  701. { "HAS_ROOT_DIRECTORY"_s,
  702. [](cmGeneratorExpressionContext* ctx,
  703. const GeneratorExpressionContent* cnt,
  704. Arguments& args) -> std::string {
  705. return CheckPathParameters(ctx, cnt, "HAS_ROOT_DIRECTORY"_s, args)
  706. ? ToString(cmCMakePath{ args.front() }.HasRootDirectory())
  707. : std::string{ "0" };
  708. } },
  709. { "HAS_ROOT_PATH"_s,
  710. [](cmGeneratorExpressionContext* ctx,
  711. const GeneratorExpressionContent* cnt,
  712. Arguments& args) -> std::string {
  713. return CheckPathParameters(ctx, cnt, "HAS_ROOT_PATH"_s, args)
  714. ? ToString(cmCMakePath{ args.front() }.HasRootPath())
  715. : std::string{ "0" };
  716. } },
  717. { "HAS_FILENAME"_s,
  718. [](cmGeneratorExpressionContext* ctx,
  719. const GeneratorExpressionContent* cnt,
  720. Arguments& args) -> std::string {
  721. return CheckPathParameters(ctx, cnt, "HAS_FILENAME"_s, args)
  722. ? ToString(cmCMakePath{ args.front() }.HasFileName())
  723. : std::string{ "0" };
  724. } },
  725. { "HAS_EXTENSION"_s,
  726. [](cmGeneratorExpressionContext* ctx,
  727. const GeneratorExpressionContent* cnt,
  728. Arguments& args) -> std::string {
  729. return CheckPathParameters(ctx, cnt, "HAS_EXTENSION"_s, args) &&
  730. !args.front().empty()
  731. ? ToString(cmCMakePath{ args.front() }.HasExtension())
  732. : std::string{ "0" };
  733. } },
  734. { "HAS_STEM"_s,
  735. [](cmGeneratorExpressionContext* ctx,
  736. const GeneratorExpressionContent* cnt,
  737. Arguments& args) -> std::string {
  738. return CheckPathParameters(ctx, cnt, "HAS_STEM"_s, args)
  739. ? ToString(cmCMakePath{ args.front() }.HasStem())
  740. : std::string{ "0" };
  741. } },
  742. { "HAS_RELATIVE_PART"_s,
  743. [](cmGeneratorExpressionContext* ctx,
  744. const GeneratorExpressionContent* cnt,
  745. Arguments& args) -> std::string {
  746. return CheckPathParameters(ctx, cnt, "HAS_RELATIVE_PART"_s, args)
  747. ? ToString(cmCMakePath{ args.front() }.HasRelativePath())
  748. : std::string{ "0" };
  749. } },
  750. { "HAS_PARENT_PATH"_s,
  751. [](cmGeneratorExpressionContext* ctx,
  752. const GeneratorExpressionContent* cnt,
  753. Arguments& args) -> std::string {
  754. return CheckPathParameters(ctx, cnt, "HAS_PARENT_PATH"_s, args)
  755. ? ToString(cmCMakePath{ args.front() }.HasParentPath())
  756. : std::string{ "0" };
  757. } },
  758. { "IS_ABSOLUTE"_s,
  759. [](cmGeneratorExpressionContext* ctx,
  760. const GeneratorExpressionContent* cnt,
  761. Arguments& args) -> std::string {
  762. return CheckPathParameters(ctx, cnt, "IS_ABSOLUTE"_s, args)
  763. ? ToString(cmCMakePath{ args.front() }.IsAbsolute())
  764. : std::string{ "0" };
  765. } },
  766. { "IS_RELATIVE"_s,
  767. [](cmGeneratorExpressionContext* ctx,
  768. const GeneratorExpressionContent* cnt,
  769. Arguments& args) -> std::string {
  770. return CheckPathParameters(ctx, cnt, "IS_RELATIVE"_s, args)
  771. ? ToString(cmCMakePath{ args.front() }.IsRelative())
  772. : std::string{ "0" };
  773. } },
  774. { "IS_PREFIX"_s,
  775. [](cmGeneratorExpressionContext* ctx,
  776. const GeneratorExpressionContent* cnt,
  777. Arguments& args) -> std::string {
  778. bool normalize = args.front() == "NORMALIZE"_s;
  779. if (normalize) {
  780. args.advance(1);
  781. }
  782. if (CheckPathParametersEx(ctx, cnt,
  783. normalize ? "IS_PREFIX,NORMALIZE"_s
  784. : "IS_PREFIX"_s,
  785. args.size(), 2)) {
  786. if (normalize) {
  787. return ToString(cmCMakePath{ args[0] }.Normal().IsPrefix(
  788. cmCMakePath{ args[1] }.Normal()));
  789. }
  790. return ToString(
  791. cmCMakePath{ args[0] }.IsPrefix(cmCMakePath{ args[1] }));
  792. }
  793. return std::string{};
  794. } },
  795. { "CMAKE_PATH"_s,
  796. [](cmGeneratorExpressionContext* ctx,
  797. const GeneratorExpressionContent* cnt,
  798. Arguments& args) -> std::string {
  799. bool normalize = args.front() == "NORMALIZE"_s;
  800. if (normalize) {
  801. args.advance(1);
  802. }
  803. if (CheckPathParametersEx(ctx, cnt,
  804. normalize ? "CMAKE_PATH,NORMALIZE"_s
  805. : "CMAKE_PATH"_s,
  806. args.size(), 1)) {
  807. auto path =
  808. cmCMakePath{ args.front(), cmCMakePath::auto_format };
  809. return normalize ? path.Normal().GenericString()
  810. : path.GenericString();
  811. }
  812. return std::string{};
  813. } },
  814. { "APPEND"_s,
  815. [](cmGeneratorExpressionContext* ctx,
  816. const GeneratorExpressionContent* cnt,
  817. Arguments& args) -> std::string {
  818. if (CheckPathParametersEx(ctx, cnt, "APPEND"_s, args.size(), 1,
  819. false)) {
  820. cmCMakePath path;
  821. for (const auto& p : args) {
  822. path /= p;
  823. }
  824. return path.String();
  825. }
  826. return std::string{};
  827. } },
  828. { "REMOVE_FILENAME"_s,
  829. [](cmGeneratorExpressionContext* ctx,
  830. const GeneratorExpressionContent* cnt,
  831. Arguments& args) -> std::string {
  832. return CheckPathParameters(ctx, cnt, "REMOVE_FILENAME"_s, args) &&
  833. !args.front().empty()
  834. ? cmCMakePath{ args.front() }.RemoveFileName().String()
  835. : std::string{};
  836. } },
  837. { "REPLACE_FILENAME"_s,
  838. [](cmGeneratorExpressionContext* ctx,
  839. const GeneratorExpressionContent* cnt,
  840. Arguments& args) -> std::string {
  841. return CheckPathParameters(ctx, cnt, "REPLACE_FILENAME"_s, args, 2)
  842. ? cmCMakePath{ args[0] }
  843. .ReplaceFileName(cmCMakePath{ args[1] })
  844. .String()
  845. : std::string{};
  846. } },
  847. { "REMOVE_EXTENSION"_s,
  848. [](cmGeneratorExpressionContext* ctx,
  849. const GeneratorExpressionContent* cnt,
  850. Arguments& args) -> std::string {
  851. bool lastOnly = args.front() == "LAST_ONLY"_s;
  852. if (lastOnly) {
  853. args.advance(1);
  854. }
  855. if (CheckPathParametersEx(ctx, cnt,
  856. lastOnly ? "REMOVE_EXTENSION,LAST_ONLY"_s
  857. : "REMOVE_EXTENSION"_s,
  858. args.size())) {
  859. if (args.front().empty()) {
  860. return std::string{};
  861. }
  862. return lastOnly
  863. ? cmCMakePath{ args.front() }.RemoveExtension().String()
  864. : cmCMakePath{ args.front() }.RemoveWideExtension().String();
  865. }
  866. return std::string{};
  867. } },
  868. { "REPLACE_EXTENSION"_s,
  869. [](cmGeneratorExpressionContext* ctx,
  870. const GeneratorExpressionContent* cnt,
  871. Arguments& args) -> std::string {
  872. bool lastOnly = args.front() == "LAST_ONLY"_s;
  873. if (lastOnly) {
  874. args.advance(1);
  875. }
  876. if (CheckPathParametersEx(ctx, cnt,
  877. lastOnly
  878. ? "REPLACE_EXTENSION,LAST_ONLY"_s
  879. : "REPLACE_EXTENSION"_s,
  880. args.size(), 2)) {
  881. if (lastOnly) {
  882. return cmCMakePath{ args[0] }
  883. .ReplaceExtension(cmCMakePath{ args[1] })
  884. .String();
  885. }
  886. return cmCMakePath{ args[0] }
  887. .ReplaceWideExtension(cmCMakePath{ args[1] })
  888. .String();
  889. }
  890. return std::string{};
  891. } },
  892. { "NORMAL_PATH"_s,
  893. [](cmGeneratorExpressionContext* ctx,
  894. const GeneratorExpressionContent* cnt,
  895. Arguments& args) -> std::string {
  896. return CheckPathParameters(ctx, cnt, "NORMAL_PATH"_s, args) &&
  897. !args.front().empty()
  898. ? cmCMakePath{ args.front() }.Normal().String()
  899. : std::string{};
  900. } },
  901. { "RELATIVE_PATH"_s,
  902. [](cmGeneratorExpressionContext* ctx,
  903. const GeneratorExpressionContent* cnt,
  904. Arguments& args) -> std::string {
  905. return CheckPathParameters(ctx, cnt, "RELATIVE_PATH"_s, args, 2)
  906. ? cmCMakePath{ args[0] }.Relative(args[1]).String()
  907. : std::string{};
  908. } },
  909. { "ABSOLUTE_PATH"_s,
  910. [](cmGeneratorExpressionContext* ctx,
  911. const GeneratorExpressionContent* cnt,
  912. Arguments& args) -> std::string {
  913. bool normalize = args.front() == "NORMALIZE"_s;
  914. if (normalize) {
  915. args.advance(1);
  916. }
  917. if (CheckPathParametersEx(ctx, cnt,
  918. normalize ? "ABSOLUTE_PATH,NORMALIZE"_s
  919. : "ABSOLUTE_PATH"_s,
  920. args.size(), 2)) {
  921. auto path = cmCMakePath{ args[0] }.Absolute(args[1]);
  922. return normalize ? path.Normal().String() : path.String();
  923. }
  924. return std::string{};
  925. } }
  926. };
  927. if (cm::contains(pathCommands, parameters.front())) {
  928. auto args = Arguments{ parameters }.advance(1);
  929. return pathCommands[parameters.front()](context, content, args);
  930. }
  931. reportError(context, content->GetOriginalExpression(),
  932. cmStrCat(parameters.front(), ": invalid option."));
  933. return std::string{};
  934. }
  935. } pathNode;
  936. static const struct PathEqualNode : public cmGeneratorExpressionNode
  937. {
  938. PathEqualNode() {} // NOLINT(modernize-use-equals-default)
  939. int NumExpectedParameters() const override { return 2; }
  940. std::string Evaluate(
  941. const std::vector<std::string>& parameters,
  942. cmGeneratorExpressionContext* /*context*/,
  943. const GeneratorExpressionContent* /*content*/,
  944. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  945. {
  946. return cmCMakePath{ parameters[0] } == cmCMakePath{ parameters[1] } ? "1"
  947. : "0";
  948. }
  949. } pathEqualNode;
  950. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  951. {
  952. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  953. bool AcceptsArbitraryContentParameter() const override { return true; }
  954. std::string Evaluate(
  955. const std::vector<std::string>& parameters,
  956. cmGeneratorExpressionContext* /*context*/,
  957. const GeneratorExpressionContent* /*content*/,
  958. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  959. {
  960. return cmSystemTools::MakeCidentifier(parameters.front());
  961. }
  962. } makeCIdentifierNode;
  963. template <char C>
  964. struct CharacterNode : public cmGeneratorExpressionNode
  965. {
  966. CharacterNode() {} // NOLINT(modernize-use-equals-default)
  967. int NumExpectedParameters() const override { return 0; }
  968. std::string Evaluate(
  969. const std::vector<std::string>& /*parameters*/,
  970. cmGeneratorExpressionContext* /*context*/,
  971. const GeneratorExpressionContent* /*content*/,
  972. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  973. {
  974. return { C };
  975. }
  976. };
  977. static const CharacterNode<'>'> angle_rNode;
  978. static const CharacterNode<','> commaNode;
  979. static const CharacterNode<';'> semicolonNode;
  980. struct CompilerIdNode : public cmGeneratorExpressionNode
  981. {
  982. CompilerIdNode(const char* compilerLang)
  983. : CompilerLanguage(compilerLang)
  984. {
  985. }
  986. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  987. std::string Evaluate(
  988. const std::vector<std::string>& parameters,
  989. cmGeneratorExpressionContext* context,
  990. const GeneratorExpressionContent* content,
  991. cmGeneratorExpressionDAGChecker* dagChecker) const override
  992. {
  993. if (!context->HeadTarget) {
  994. std::ostringstream e;
  995. e << "$<" << this->CompilerLanguage
  996. << "_COMPILER_ID> may only be used with binary targets. It may "
  997. "not be used with add_custom_command or add_custom_target.";
  998. reportError(context, content->GetOriginalExpression(), e.str());
  999. return {};
  1000. }
  1001. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1002. this->CompilerLanguage);
  1003. }
  1004. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  1005. cmGeneratorExpressionContext* context,
  1006. const GeneratorExpressionContent* content,
  1007. cmGeneratorExpressionDAGChecker* /*unused*/,
  1008. const std::string& lang) const
  1009. {
  1010. std::string const& compilerId =
  1011. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1012. "_COMPILER_ID");
  1013. if (parameters.empty()) {
  1014. return compilerId;
  1015. }
  1016. if (compilerId.empty()) {
  1017. return parameters.front().empty() ? "1" : "0";
  1018. }
  1019. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  1020. for (auto const& param : parameters) {
  1021. if (!compilerIdValidator.find(param)) {
  1022. reportError(context, content->GetOriginalExpression(),
  1023. "Expression syntax not recognized.");
  1024. return std::string();
  1025. }
  1026. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  1027. return "1";
  1028. }
  1029. if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
  1030. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  1031. case cmPolicies::WARN: {
  1032. context->LG->GetCMakeInstance()->IssueMessage(
  1033. MessageType::AUTHOR_WARNING,
  1034. cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
  1035. context->Backtrace);
  1036. CM_FALLTHROUGH;
  1037. }
  1038. case cmPolicies::OLD:
  1039. return "1";
  1040. case cmPolicies::NEW:
  1041. case cmPolicies::REQUIRED_ALWAYS:
  1042. case cmPolicies::REQUIRED_IF_USED:
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. return "0";
  1048. }
  1049. const char* const CompilerLanguage;
  1050. };
  1051. static const CompilerIdNode cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  1052. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  1053. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  1054. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  1055. struct CompilerVersionNode : public cmGeneratorExpressionNode
  1056. {
  1057. CompilerVersionNode(const char* compilerLang)
  1058. : CompilerLanguage(compilerLang)
  1059. {
  1060. }
  1061. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  1062. std::string Evaluate(
  1063. const std::vector<std::string>& parameters,
  1064. cmGeneratorExpressionContext* context,
  1065. const GeneratorExpressionContent* content,
  1066. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1067. {
  1068. if (!context->HeadTarget) {
  1069. std::ostringstream e;
  1070. e << "$<" << this->CompilerLanguage
  1071. << "_COMPILER_VERSION> may only be used with binary targets. It "
  1072. "may not be used with add_custom_command or add_custom_target.";
  1073. reportError(context, content->GetOriginalExpression(), e.str());
  1074. return {};
  1075. }
  1076. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1077. this->CompilerLanguage);
  1078. }
  1079. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  1080. cmGeneratorExpressionContext* context,
  1081. const GeneratorExpressionContent* content,
  1082. cmGeneratorExpressionDAGChecker* /*unused*/,
  1083. const std::string& lang) const
  1084. {
  1085. std::string const& compilerVersion =
  1086. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1087. "_COMPILER_VERSION");
  1088. if (parameters.empty()) {
  1089. return compilerVersion;
  1090. }
  1091. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  1092. if (!compilerIdValidator.find(parameters.front())) {
  1093. reportError(context, content->GetOriginalExpression(),
  1094. "Expression syntax not recognized.");
  1095. return {};
  1096. }
  1097. if (compilerVersion.empty()) {
  1098. return parameters.front().empty() ? "1" : "0";
  1099. }
  1100. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  1101. parameters.front(), compilerVersion)
  1102. ? "1"
  1103. : "0";
  1104. }
  1105. const char* const CompilerLanguage;
  1106. };
  1107. static const CompilerVersionNode cCompilerVersionNode("C"),
  1108. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  1109. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  1110. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  1111. hipCompilerVersionNode("HIP");
  1112. struct PlatformIdNode : public cmGeneratorExpressionNode
  1113. {
  1114. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  1115. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1116. std::string Evaluate(
  1117. const std::vector<std::string>& parameters,
  1118. cmGeneratorExpressionContext* context,
  1119. const GeneratorExpressionContent* /*content*/,
  1120. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1121. {
  1122. std::string const& platformId =
  1123. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  1124. if (parameters.empty()) {
  1125. return platformId;
  1126. }
  1127. if (platformId.empty()) {
  1128. return parameters.front().empty() ? "1" : "0";
  1129. }
  1130. for (auto const& param : parameters) {
  1131. if (param == platformId) {
  1132. return "1";
  1133. }
  1134. }
  1135. return "0";
  1136. }
  1137. };
  1138. static struct PlatformIdNode platformIdNode;
  1139. template <cmSystemTools::CompareOp Op>
  1140. struct VersionNode : public cmGeneratorExpressionNode
  1141. {
  1142. VersionNode() {} // NOLINT(modernize-use-equals-default)
  1143. int NumExpectedParameters() const override { return 2; }
  1144. std::string Evaluate(
  1145. const std::vector<std::string>& parameters,
  1146. cmGeneratorExpressionContext* /*context*/,
  1147. const GeneratorExpressionContent* /*content*/,
  1148. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1149. {
  1150. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  1151. ? "1"
  1152. : "0";
  1153. }
  1154. };
  1155. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  1156. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  1157. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  1158. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  1159. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  1160. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  1161. {
  1162. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1163. std::string Evaluate(
  1164. const std::vector<std::string>& parameters,
  1165. cmGeneratorExpressionContext* context,
  1166. const GeneratorExpressionContent* content,
  1167. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1168. {
  1169. if (!dagChecker) {
  1170. reportError(context, content->GetOriginalExpression(),
  1171. "$<LINK_ONLY:...> may only be used for linking");
  1172. return std::string();
  1173. }
  1174. if (!dagChecker->GetTransitivePropertiesOnly()) {
  1175. return parameters.front();
  1176. }
  1177. return std::string();
  1178. }
  1179. } linkOnlyNode;
  1180. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  1181. {
  1182. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  1183. int NumExpectedParameters() const override { return 0; }
  1184. std::string Evaluate(
  1185. const std::vector<std::string>& /*parameters*/,
  1186. cmGeneratorExpressionContext* context,
  1187. const GeneratorExpressionContent* /*content*/,
  1188. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1189. {
  1190. context->HadContextSensitiveCondition = true;
  1191. return context->Config;
  1192. }
  1193. } configurationNode;
  1194. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  1195. {
  1196. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  1197. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1198. std::string Evaluate(
  1199. const std::vector<std::string>& parameters,
  1200. cmGeneratorExpressionContext* context,
  1201. const GeneratorExpressionContent* content,
  1202. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1203. {
  1204. if (parameters.empty()) {
  1205. return configurationNode.Evaluate(parameters, context, content, nullptr);
  1206. }
  1207. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  1208. if (!configValidator.find(parameters.front())) {
  1209. }
  1210. context->HadContextSensitiveCondition = true;
  1211. bool firstParam = true;
  1212. for (auto const& param : parameters) {
  1213. if (!configValidator.find(param)) {
  1214. if (firstParam) {
  1215. reportError(context, content->GetOriginalExpression(),
  1216. "Expression syntax not recognized.");
  1217. return std::string();
  1218. }
  1219. // for backwards compat invalid config names are only errors as
  1220. // the first parameter
  1221. std::ostringstream e;
  1222. /* clang-format off */
  1223. e << "Warning evaluating generator expression:\n"
  1224. << " " << content->GetOriginalExpression() << "\n"
  1225. << "The config name of \"" << param << "\" is invalid";
  1226. /* clang-format on */
  1227. context->LG->GetCMakeInstance()->IssueMessage(
  1228. MessageType::WARNING, e.str(), context->Backtrace);
  1229. }
  1230. firstParam = false;
  1231. if (context->Config.empty()) {
  1232. if (param.empty()) {
  1233. return "1";
  1234. }
  1235. } else if (cmsysString_strcasecmp(param.c_str(),
  1236. context->Config.c_str()) == 0) {
  1237. return "1";
  1238. }
  1239. }
  1240. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  1241. cmValue loc = nullptr;
  1242. cmValue imp = nullptr;
  1243. std::string suffix;
  1244. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  1245. imp, suffix)) {
  1246. // This imported target has an appropriate location
  1247. // for this (possibly mapped) config.
  1248. // Check if there is a proper config mapping for the tested config.
  1249. std::vector<std::string> mappedConfigs;
  1250. std::string mapProp = cmStrCat(
  1251. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  1252. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  1253. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  1254. for (auto const& param : parameters) {
  1255. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  1256. return "1";
  1257. }
  1258. }
  1259. } else if (!suffix.empty()) {
  1260. // There is no explicit mapping for the tested config, so use
  1261. // the configuration of the imported location that was selected.
  1262. for (auto const& param : parameters) {
  1263. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  1264. return "1";
  1265. }
  1266. }
  1267. }
  1268. }
  1269. }
  1270. return "0";
  1271. }
  1272. } configurationTestNode;
  1273. static const struct JoinNode : public cmGeneratorExpressionNode
  1274. {
  1275. JoinNode() {} // NOLINT(modernize-use-equals-default)
  1276. int NumExpectedParameters() const override { return 2; }
  1277. bool AcceptsArbitraryContentParameter() const override { return true; }
  1278. std::string Evaluate(
  1279. const std::vector<std::string>& parameters,
  1280. cmGeneratorExpressionContext* /*context*/,
  1281. const GeneratorExpressionContent* /*content*/,
  1282. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1283. {
  1284. std::vector<std::string> list = cmExpandedList(parameters.front());
  1285. return cmJoin(list, parameters[1]);
  1286. }
  1287. } joinNode;
  1288. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  1289. {
  1290. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1291. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1292. std::string Evaluate(
  1293. const std::vector<std::string>& parameters,
  1294. cmGeneratorExpressionContext* context,
  1295. const GeneratorExpressionContent* content,
  1296. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1297. {
  1298. if (context->Language.empty() &&
  1299. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  1300. reportError(
  1301. context, content->GetOriginalExpression(),
  1302. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  1303. "directories, compile definitions, compile options, and to evaluate "
  1304. "components of the file(GENERATE) command.");
  1305. return std::string();
  1306. }
  1307. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1308. std::string genName = gg->GetName();
  1309. if (genName.find("Makefiles") == std::string::npos &&
  1310. genName.find("Ninja") == std::string::npos &&
  1311. genName.find("Visual Studio") == std::string::npos &&
  1312. genName.find("Xcode") == std::string::npos &&
  1313. genName.find("Watcom WMake") == std::string::npos) {
  1314. reportError(context, content->GetOriginalExpression(),
  1315. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  1316. return std::string();
  1317. }
  1318. if (parameters.empty()) {
  1319. return context->Language;
  1320. }
  1321. for (auto const& param : parameters) {
  1322. if (context->Language == param) {
  1323. return "1";
  1324. }
  1325. }
  1326. return "0";
  1327. }
  1328. } languageNode;
  1329. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  1330. {
  1331. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1332. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1333. std::string Evaluate(
  1334. const std::vector<std::string>& parameters,
  1335. cmGeneratorExpressionContext* context,
  1336. const GeneratorExpressionContent* content,
  1337. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1338. {
  1339. if (!context->HeadTarget ||
  1340. (context->Language.empty() &&
  1341. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  1342. // reportError(context, content->GetOriginalExpression(), "");
  1343. reportError(
  1344. context, content->GetOriginalExpression(),
  1345. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  1346. "to specify include directories, compile definitions, and compile "
  1347. "options. It may not be used with the add_custom_command, "
  1348. "add_custom_target, or file(GENERATE) commands.");
  1349. return std::string();
  1350. }
  1351. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1352. std::string genName = gg->GetName();
  1353. if (genName.find("Makefiles") == std::string::npos &&
  1354. genName.find("Ninja") == std::string::npos &&
  1355. genName.find("Visual Studio") == std::string::npos &&
  1356. genName.find("Xcode") == std::string::npos &&
  1357. genName.find("Watcom WMake") == std::string::npos) {
  1358. reportError(
  1359. context, content->GetOriginalExpression(),
  1360. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  1361. return std::string();
  1362. }
  1363. const std::string& lang = context->Language;
  1364. if (lang == parameters.front()) {
  1365. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1366. parameters.cend());
  1367. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  1368. idParameter, context, content, dagChecker, lang);
  1369. }
  1370. return "0";
  1371. }
  1372. } languageAndIdNode;
  1373. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  1374. {
  1375. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1376. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1377. std::string Evaluate(
  1378. const std::vector<std::string>& parameters,
  1379. cmGeneratorExpressionContext* context,
  1380. const GeneratorExpressionContent* content,
  1381. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1382. {
  1383. if (!context->HeadTarget || !dagChecker ||
  1384. !(dagChecker->EvaluatingLinkExpression() ||
  1385. dagChecker->EvaluatingLinkLibraries())) {
  1386. reportError(context, content->GetOriginalExpression(),
  1387. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  1388. "to specify link libraries, link directories, link options "
  1389. "and link depends.");
  1390. return std::string();
  1391. }
  1392. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  1393. reportError(
  1394. context, content->GetOriginalExpression(),
  1395. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  1396. return std::string();
  1397. }
  1398. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1399. std::string genName = gg->GetName();
  1400. if (genName.find("Makefiles") == std::string::npos &&
  1401. genName.find("Ninja") == std::string::npos &&
  1402. genName.find("Visual Studio") == std::string::npos &&
  1403. genName.find("Xcode") == std::string::npos &&
  1404. genName.find("Watcom WMake") == std::string::npos) {
  1405. reportError(context, content->GetOriginalExpression(),
  1406. "$<LINK_LANGUAGE:...> not supported for this generator.");
  1407. return std::string();
  1408. }
  1409. if (dagChecker->EvaluatingLinkLibraries()) {
  1410. context->HadHeadSensitiveCondition = true;
  1411. context->HadLinkLanguageSensitiveCondition = true;
  1412. }
  1413. if (parameters.empty()) {
  1414. return context->Language;
  1415. }
  1416. for (auto const& param : parameters) {
  1417. if (context->Language == param) {
  1418. return "1";
  1419. }
  1420. }
  1421. return "0";
  1422. }
  1423. } linkLanguageNode;
  1424. namespace {
  1425. struct LinkerId
  1426. {
  1427. static std::string Evaluate(const std::vector<std::string>& parameters,
  1428. cmGeneratorExpressionContext* context,
  1429. const GeneratorExpressionContent* content,
  1430. const std::string& lang)
  1431. {
  1432. std::string const& linkerId =
  1433. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1434. "_COMPILER_ID");
  1435. if (parameters.empty()) {
  1436. return linkerId;
  1437. }
  1438. if (linkerId.empty()) {
  1439. return parameters.front().empty() ? "1" : "0";
  1440. }
  1441. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  1442. for (auto const& param : parameters) {
  1443. if (!linkerIdValidator.find(param)) {
  1444. reportError(context, content->GetOriginalExpression(),
  1445. "Expression syntax not recognized.");
  1446. return std::string();
  1447. }
  1448. if (param == linkerId) {
  1449. return "1";
  1450. }
  1451. }
  1452. return "0";
  1453. }
  1454. };
  1455. }
  1456. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  1457. {
  1458. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1459. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1460. std::string Evaluate(
  1461. const std::vector<std::string>& parameters,
  1462. cmGeneratorExpressionContext* context,
  1463. const GeneratorExpressionContent* content,
  1464. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1465. {
  1466. if (!context->HeadTarget || !dagChecker ||
  1467. !(dagChecker->EvaluatingLinkExpression() ||
  1468. dagChecker->EvaluatingLinkLibraries())) {
  1469. reportError(
  1470. context, content->GetOriginalExpression(),
  1471. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1472. "to specify link libraries, link directories, link options, and link "
  1473. "depends.");
  1474. return std::string();
  1475. }
  1476. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1477. std::string genName = gg->GetName();
  1478. if (genName.find("Makefiles") == std::string::npos &&
  1479. genName.find("Ninja") == std::string::npos &&
  1480. genName.find("Visual Studio") == std::string::npos &&
  1481. genName.find("Xcode") == std::string::npos &&
  1482. genName.find("Watcom WMake") == std::string::npos) {
  1483. reportError(
  1484. context, content->GetOriginalExpression(),
  1485. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1486. return std::string();
  1487. }
  1488. if (dagChecker->EvaluatingLinkLibraries()) {
  1489. context->HadHeadSensitiveCondition = true;
  1490. context->HadLinkLanguageSensitiveCondition = true;
  1491. }
  1492. const std::string& lang = context->Language;
  1493. if (lang == parameters.front()) {
  1494. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1495. parameters.cend());
  1496. return LinkerId::Evaluate(idParameter, context, content, lang);
  1497. }
  1498. return "0";
  1499. }
  1500. } linkLanguageAndIdNode;
  1501. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  1502. {
  1503. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  1504. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1505. std::string Evaluate(
  1506. const std::vector<std::string>& parameters,
  1507. cmGeneratorExpressionContext* context,
  1508. const GeneratorExpressionContent* content,
  1509. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1510. {
  1511. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  1512. if (!context->HeadTarget || !dagChecker ||
  1513. !dagChecker->EvaluatingLinkLibraries(nullptr,
  1514. ForGenex::LINK_LIBRARY)) {
  1515. reportError(context, content->GetOriginalExpression(),
  1516. "$<LINK_LIBRARY:...> may only be used with binary targets "
  1517. "to specify link libraries through 'LINK_LIBRARIES', "
  1518. "'INTERFACE_LINK_LIBRARIES', and "
  1519. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  1520. return std::string();
  1521. }
  1522. std::vector<std::string> list;
  1523. cmExpandLists(parameters.begin(), parameters.end(), list);
  1524. if (list.empty()) {
  1525. reportError(
  1526. context, content->GetOriginalExpression(),
  1527. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  1528. return std::string();
  1529. }
  1530. if (list.size() == 1) {
  1531. // no libraries specified, ignore this genex
  1532. return std::string();
  1533. }
  1534. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1535. auto const& feature = list.front();
  1536. if (!featureNameValidator.find(feature)) {
  1537. reportError(context, content->GetOriginalExpression(),
  1538. cmStrCat("The feature name '", feature,
  1539. "' contains invalid characters."));
  1540. return std::string();
  1541. }
  1542. const auto LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  1543. const auto LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  1544. // filter out $<LINK_LIBRARY:..> tags with same feature
  1545. // and raise an error for any different feature
  1546. cm::erase_if(list, [&](const std::string& item) -> bool {
  1547. return item == LL_BEGIN || item == LL_END;
  1548. });
  1549. auto it =
  1550. std::find_if(list.cbegin() + 1, list.cend(),
  1551. [&feature](const std::string& item) -> bool {
  1552. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  1553. item.substr(14, item.find('>', 14) - 14) != feature;
  1554. });
  1555. if (it != list.cend()) {
  1556. reportError(
  1557. context, content->GetOriginalExpression(),
  1558. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  1559. return std::string();
  1560. }
  1561. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  1562. it = std::find_if(list.cbegin() + 1, list.cend(),
  1563. [](const std::string& item) -> bool {
  1564. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  1565. });
  1566. if (it != list.cend()) {
  1567. reportError(context, content->GetOriginalExpression(),
  1568. "$<LINK_GROUP:...> cannot be nested inside a "
  1569. "$<LINK_LIBRARY:...> expression.");
  1570. return std::string();
  1571. }
  1572. list.front() = LL_BEGIN;
  1573. list.push_back(LL_END);
  1574. return cmJoin(list, ";"_s);
  1575. }
  1576. } linkLibraryNode;
  1577. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  1578. {
  1579. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  1580. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1581. std::string Evaluate(
  1582. const std::vector<std::string>& parameters,
  1583. cmGeneratorExpressionContext* context,
  1584. const GeneratorExpressionContent* content,
  1585. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1586. {
  1587. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  1588. if (!context->HeadTarget || !dagChecker ||
  1589. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  1590. reportError(
  1591. context, content->GetOriginalExpression(),
  1592. "$<LINK_GROUP:...> may only be used with binary targets "
  1593. "to specify group of link libraries through 'LINK_LIBRARIES', "
  1594. "'INTERFACE_LINK_LIBRARIES', and "
  1595. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  1596. return std::string();
  1597. }
  1598. std::vector<std::string> list;
  1599. cmExpandLists(parameters.begin(), parameters.end(), list);
  1600. if (list.empty()) {
  1601. reportError(
  1602. context, content->GetOriginalExpression(),
  1603. "$<LINK_GROUP:...> expects a feature name as first argument.");
  1604. return std::string();
  1605. }
  1606. // $<LINK_GROUP:..> cannot be nested
  1607. if (std::find_if(list.cbegin(), list.cend(),
  1608. [](const std::string& item) -> bool {
  1609. return cmHasPrefix(item, "<LINK_GROUP"_s);
  1610. }) != list.cend()) {
  1611. reportError(context, content->GetOriginalExpression(),
  1612. "$<LINK_GROUP:...> cannot be nested.");
  1613. return std::string();
  1614. }
  1615. if (list.size() == 1) {
  1616. // no libraries specified, ignore this genex
  1617. return std::string();
  1618. }
  1619. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1620. auto const& feature = list.front();
  1621. if (!featureNameValidator.find(feature)) {
  1622. reportError(context, content->GetOriginalExpression(),
  1623. cmStrCat("The feature name '", feature,
  1624. "' contains invalid characters."));
  1625. return std::string();
  1626. }
  1627. const auto LG_BEGIN = cmStrCat(
  1628. "<LINK_GROUP:", feature, ':',
  1629. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  1630. "|"_s),
  1631. '>');
  1632. const auto LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  1633. list.front() = LG_BEGIN;
  1634. list.push_back(LG_END);
  1635. return cmJoin(list, ";"_s);
  1636. }
  1637. } linkGroupNode;
  1638. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1639. {
  1640. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1641. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1642. std::string Evaluate(
  1643. const std::vector<std::string>& parameters,
  1644. cmGeneratorExpressionContext* context,
  1645. const GeneratorExpressionContent* content,
  1646. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1647. {
  1648. if (!context->HeadTarget || !dagChecker ||
  1649. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1650. reportError(context, content->GetOriginalExpression(),
  1651. "$<HOST_LINK:...> may only be used with binary targets "
  1652. "to specify link options.");
  1653. return std::string();
  1654. }
  1655. return context->HeadTarget->IsDeviceLink() ? std::string()
  1656. : cmJoin(parameters, ";");
  1657. }
  1658. } hostLinkNode;
  1659. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1660. {
  1661. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1662. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1663. std::string Evaluate(
  1664. const std::vector<std::string>& parameters,
  1665. cmGeneratorExpressionContext* context,
  1666. const GeneratorExpressionContent* content,
  1667. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1668. {
  1669. if (!context->HeadTarget || !dagChecker ||
  1670. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1671. reportError(context, content->GetOriginalExpression(),
  1672. "$<DEVICE_LINK:...> may only be used with binary targets "
  1673. "to specify link options.");
  1674. return std::string();
  1675. }
  1676. if (context->HeadTarget->IsDeviceLink()) {
  1677. std::vector<std::string> list;
  1678. cmExpandLists(parameters.begin(), parameters.end(), list);
  1679. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1680. const auto DL_END = "</DEVICE_LINK>"_s;
  1681. cm::erase_if(list, [&](const std::string& item) {
  1682. return item == DL_BEGIN || item == DL_END;
  1683. });
  1684. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1685. list.push_back(static_cast<std::string>(DL_END));
  1686. return cmJoin(list, ";");
  1687. }
  1688. return std::string();
  1689. }
  1690. } deviceLinkNode;
  1691. static std::string getLinkedTargetsContent(
  1692. cmGeneratorTarget const* target, std::string const& prop,
  1693. cmGeneratorExpressionContext* context,
  1694. cmGeneratorExpressionDAGChecker* dagChecker)
  1695. {
  1696. std::string result;
  1697. if (cmLinkImplementationLibraries const* impl =
  1698. target->GetLinkImplementationLibraries(
  1699. context->Config, cmGeneratorTarget::LinkInterfaceFor::Usage)) {
  1700. for (cmLinkImplItem const& lib : impl->Libraries) {
  1701. if (lib.Target) {
  1702. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1703. // caller's property and hand-evaluate it as if it were compiled.
  1704. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1705. cmGeneratorExpressionContext libContext(
  1706. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1707. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1708. context->Language);
  1709. std::string libResult =
  1710. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1711. if (!libResult.empty()) {
  1712. if (result.empty()) {
  1713. result = std::move(libResult);
  1714. } else {
  1715. result.reserve(result.size() + 1 + libResult.size());
  1716. result += ";";
  1717. result += libResult;
  1718. }
  1719. }
  1720. }
  1721. }
  1722. }
  1723. return result;
  1724. }
  1725. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1726. {
  1727. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1728. // This node handles errors on parameter count itself.
  1729. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1730. static const char* GetErrorText(std::string const& targetName,
  1731. std::string const& propertyName)
  1732. {
  1733. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1734. if (targetName.empty() && propertyName.empty()) {
  1735. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1736. "target name and property name.";
  1737. }
  1738. if (targetName.empty()) {
  1739. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1740. "target name.";
  1741. }
  1742. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1743. if (!propertyNameValidator.find(propertyName)) {
  1744. return "Target name and property name not supported.";
  1745. }
  1746. return "Target name not supported.";
  1747. }
  1748. return nullptr;
  1749. }
  1750. std::string Evaluate(
  1751. const std::vector<std::string>& parameters,
  1752. cmGeneratorExpressionContext* context,
  1753. const GeneratorExpressionContent* content,
  1754. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1755. {
  1756. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1757. cmGeneratorTarget const* target = nullptr;
  1758. std::string targetName;
  1759. std::string propertyName;
  1760. if (parameters.size() == 2) {
  1761. targetName = parameters[0];
  1762. propertyName = parameters[1];
  1763. if (const char* e = GetErrorText(targetName, propertyName)) {
  1764. reportError(context, content->GetOriginalExpression(), e);
  1765. return std::string();
  1766. }
  1767. if (propertyName == "ALIASED_TARGET"_s) {
  1768. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1769. if (cmGeneratorTarget* tgt =
  1770. context->LG->FindGeneratorTargetToUse(targetName)) {
  1771. return tgt->GetName();
  1772. }
  1773. }
  1774. return std::string();
  1775. }
  1776. if (propertyName == "ALIAS_GLOBAL"_s) {
  1777. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1778. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1779. ? "TRUE"
  1780. : "FALSE";
  1781. }
  1782. return std::string();
  1783. }
  1784. cmLocalGenerator const* lg = context->CurrentTarget
  1785. ? context->CurrentTarget->GetLocalGenerator()
  1786. : context->LG;
  1787. target = lg->FindGeneratorTargetToUse(targetName);
  1788. if (!target) {
  1789. std::ostringstream e;
  1790. e << "Target \"" << targetName << "\" not found.";
  1791. reportError(context, content->GetOriginalExpression(), e.str());
  1792. return std::string();
  1793. }
  1794. context->AllTargets.insert(target);
  1795. } else if (parameters.size() == 1) {
  1796. target = context->HeadTarget;
  1797. propertyName = parameters[0];
  1798. // Keep track of the properties seen while processing.
  1799. // The evaluation of the LINK_LIBRARIES generator expressions
  1800. // will check this to ensure that properties have one consistent
  1801. // value for all evaluations.
  1802. context->SeenTargetProperties.insert(propertyName);
  1803. context->HadHeadSensitiveCondition = true;
  1804. if (!target) {
  1805. reportError(
  1806. context, content->GetOriginalExpression(),
  1807. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1808. "It may not be used with add_custom_command or add_custom_target. "
  1809. " "
  1810. "Specify the target to read a property from using the "
  1811. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1812. return std::string();
  1813. }
  1814. } else {
  1815. reportError(
  1816. context, content->GetOriginalExpression(),
  1817. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1818. return std::string();
  1819. }
  1820. if (propertyName == "SOURCES") {
  1821. context->SourceSensitiveTargets.insert(target);
  1822. }
  1823. if (propertyName.empty()) {
  1824. reportError(
  1825. context, content->GetOriginalExpression(),
  1826. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1827. "name.");
  1828. return std::string();
  1829. }
  1830. if (!propertyNameValidator.find(propertyName)) {
  1831. ::reportError(context, content->GetOriginalExpression(),
  1832. "Property name not supported.");
  1833. return std::string();
  1834. }
  1835. assert(target);
  1836. if (propertyName == "LINKER_LANGUAGE") {
  1837. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1838. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1839. dagCheckerParent->EvaluatingSources())) {
  1840. reportError(
  1841. context, content->GetOriginalExpression(),
  1842. "LINKER_LANGUAGE target property can not be used while evaluating "
  1843. "link libraries for a static library");
  1844. return std::string();
  1845. }
  1846. return target->GetLinkerLanguage(context->Config);
  1847. }
  1848. std::string interfacePropertyName;
  1849. bool isInterfaceProperty = false;
  1850. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1851. if (propertyName == #prop) { \
  1852. interfacePropertyName = "INTERFACE_" #prop; \
  1853. } else if (propertyName == "INTERFACE_" #prop) { \
  1854. interfacePropertyName = "INTERFACE_" #prop; \
  1855. isInterfaceProperty = true; \
  1856. } else
  1857. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1858. // Note that the above macro terminates with an else
  1859. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1860. cmPolicies::PolicyStatus polSt =
  1861. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1862. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1863. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1864. }
  1865. }
  1866. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1867. bool evaluatingLinkLibraries = false;
  1868. if (dagCheckerParent) {
  1869. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1870. dagCheckerParent->EvaluatingPICExpression()) {
  1871. // No check required.
  1872. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1873. evaluatingLinkLibraries = true;
  1874. if (!interfacePropertyName.empty()) {
  1875. reportError(
  1876. context, content->GetOriginalExpression(),
  1877. "$<TARGET_PROPERTY:...> expression in link libraries "
  1878. "evaluation depends on target property which is transitive "
  1879. "over the link libraries, creating a recursion.");
  1880. return std::string();
  1881. }
  1882. } else {
  1883. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1884. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1885. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1886. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1887. }
  1888. }
  1889. if (isInterfaceProperty) {
  1890. return cmGeneratorExpression::StripEmptyListElements(
  1891. target->EvaluateInterfaceProperty(propertyName, context,
  1892. dagCheckerParent));
  1893. }
  1894. cmGeneratorExpressionDAGChecker dagChecker(
  1895. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1896. switch (dagChecker.Check()) {
  1897. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1898. dagChecker.ReportError(context, content->GetOriginalExpression());
  1899. return std::string();
  1900. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1901. // No error. We just skip cyclic references.
  1902. return std::string();
  1903. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1904. // We handle transitive properties above. For non-transitive
  1905. // properties we accept repeats anyway.
  1906. case cmGeneratorExpressionDAGChecker::DAG:
  1907. break;
  1908. }
  1909. std::string result;
  1910. bool haveProp = false;
  1911. if (cmValue p = target->GetProperty(propertyName)) {
  1912. result = *p;
  1913. haveProp = true;
  1914. } else if (evaluatingLinkLibraries) {
  1915. return std::string();
  1916. }
  1917. if (!haveProp && !target->IsImported() &&
  1918. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1919. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1920. context->Config)) {
  1921. context->HadContextSensitiveCondition = true;
  1922. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1923. context->Config)
  1924. ? "1"
  1925. : "0";
  1926. }
  1927. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1928. context->Config)) {
  1929. context->HadContextSensitiveCondition = true;
  1930. const char* propContent =
  1931. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1932. context->Config);
  1933. return propContent ? propContent : "";
  1934. }
  1935. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1936. context->Config)) {
  1937. context->HadContextSensitiveCondition = true;
  1938. const char* propContent =
  1939. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1940. context->Config);
  1941. return propContent ? propContent : "";
  1942. }
  1943. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1944. context->Config)) {
  1945. context->HadContextSensitiveCondition = true;
  1946. const char* propContent =
  1947. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1948. context->Config);
  1949. return propContent ? propContent : "";
  1950. }
  1951. }
  1952. if (!target->IsImported() && dagCheckerParent &&
  1953. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1954. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1955. context->Config)) {
  1956. context->HadContextSensitiveCondition = true;
  1957. const char* propContent =
  1958. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1959. context->Config);
  1960. return propContent ? propContent : "";
  1961. }
  1962. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1963. context->Config)) {
  1964. context->HadContextSensitiveCondition = true;
  1965. const char* propContent =
  1966. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1967. context->Config);
  1968. return propContent ? propContent : "";
  1969. }
  1970. }
  1971. if (!interfacePropertyName.empty()) {
  1972. result = cmGeneratorExpression::StripEmptyListElements(
  1973. this->EvaluateDependentExpression(result, context->LG, context, target,
  1974. &dagChecker, target));
  1975. std::string linkedTargetsContent = getLinkedTargetsContent(
  1976. target, interfacePropertyName, context, &dagChecker);
  1977. if (!linkedTargetsContent.empty()) {
  1978. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1979. }
  1980. }
  1981. return result;
  1982. }
  1983. } targetPropertyNode;
  1984. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1985. {
  1986. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1987. bool GeneratesContent() const override { return true; }
  1988. bool AcceptsArbitraryContentParameter() const override { return true; }
  1989. bool RequiresLiteralInput() const override { return true; }
  1990. std::string Evaluate(
  1991. const std::vector<std::string>& parameters,
  1992. cmGeneratorExpressionContext* /*context*/,
  1993. const GeneratorExpressionContent* /*content*/,
  1994. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1995. {
  1996. return parameters.front();
  1997. }
  1998. int NumExpectedParameters() const override { return 1; }
  1999. } targetNameNode;
  2000. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  2001. {
  2002. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  2003. std::string Evaluate(
  2004. const std::vector<std::string>& parameters,
  2005. cmGeneratorExpressionContext* context,
  2006. const GeneratorExpressionContent* content,
  2007. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2008. {
  2009. std::string const& tgtName = parameters.front();
  2010. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2011. if (!gt) {
  2012. std::ostringstream e;
  2013. e << "Objects of target \"" << tgtName
  2014. << "\" referenced but no such target exists.";
  2015. reportError(context, content->GetOriginalExpression(), e.str());
  2016. return std::string();
  2017. }
  2018. cmStateEnums::TargetType type = gt->GetType();
  2019. if (type != cmStateEnums::EXECUTABLE &&
  2020. type != cmStateEnums::STATIC_LIBRARY &&
  2021. type != cmStateEnums::SHARED_LIBRARY &&
  2022. type != cmStateEnums::MODULE_LIBRARY &&
  2023. type != cmStateEnums::OBJECT_LIBRARY) {
  2024. std::ostringstream e;
  2025. e << "Objects of target \"" << tgtName
  2026. << "\" referenced but is not one of the allowed target types "
  2027. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2028. reportError(context, content->GetOriginalExpression(), e.str());
  2029. return std::string();
  2030. }
  2031. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2032. {
  2033. std::string reason;
  2034. if (!context->EvaluateForBuildsystem &&
  2035. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2036. std::ostringstream e;
  2037. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2038. "is only suitable for consumption by CMake (limited"
  2039. << reason
  2040. << "). "
  2041. "It is not suitable for writing out elsewhere.";
  2042. reportError(context, content->GetOriginalExpression(), e.str());
  2043. return std::string();
  2044. }
  2045. }
  2046. std::vector<std::string> objects;
  2047. if (gt->IsImported()) {
  2048. cmValue loc = nullptr;
  2049. cmValue imp = nullptr;
  2050. std::string suffix;
  2051. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  2052. cmExpandList(*loc, objects);
  2053. }
  2054. context->HadContextSensitiveCondition = true;
  2055. } else {
  2056. gt->GetTargetObjectNames(context->Config, objects);
  2057. std::string obj_dir;
  2058. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  2059. // Use object file directory with buildsystem placeholder.
  2060. obj_dir = gt->ObjectDirectory;
  2061. context->HadContextSensitiveCondition =
  2062. gt->HasContextDependentSources();
  2063. } else {
  2064. // Use object file directory with per-config location.
  2065. obj_dir = gt->GetObjectDirectory(context->Config);
  2066. context->HadContextSensitiveCondition = true;
  2067. }
  2068. for (std::string& o : objects) {
  2069. o = cmStrCat(obj_dir, o);
  2070. }
  2071. }
  2072. // Create the cmSourceFile instances in the referencing directory.
  2073. cmMakefile* mf = context->LG->GetMakefile();
  2074. for (std::string const& o : objects) {
  2075. mf->AddTargetObject(tgtName, o);
  2076. }
  2077. return cmJoin(objects, ";");
  2078. }
  2079. } targetObjectsNode;
  2080. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  2081. {
  2082. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  2083. std::string Evaluate(
  2084. const std::vector<std::string>& parameters,
  2085. cmGeneratorExpressionContext* context,
  2086. const GeneratorExpressionContent* content,
  2087. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2088. {
  2089. std::string const& tgtName = parameters.front();
  2090. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2091. if (!gt) {
  2092. std::ostringstream e;
  2093. e << "Objects of target \"" << tgtName
  2094. << "\" referenced but no such target exists.";
  2095. reportError(context, content->GetOriginalExpression(), e.str());
  2096. return std::string();
  2097. }
  2098. cmStateEnums::TargetType type = gt->GetType();
  2099. if (type != cmStateEnums::EXECUTABLE &&
  2100. type != cmStateEnums::SHARED_LIBRARY &&
  2101. type != cmStateEnums::MODULE_LIBRARY) {
  2102. std::ostringstream e;
  2103. e << "Objects of target \"" << tgtName
  2104. << "\" referenced but is not one of the allowed target types "
  2105. << "(EXECUTABLE, SHARED, MODULE).";
  2106. reportError(context, content->GetOriginalExpression(), e.str());
  2107. return std::string();
  2108. }
  2109. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  2110. std::vector<std::string> dllPaths;
  2111. auto const& dlls = cli->GetRuntimeDLLs();
  2112. for (auto const& dll : dlls) {
  2113. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  2114. dllPaths.emplace_back(*loc);
  2115. }
  2116. }
  2117. return cmJoin(dllPaths, ";");
  2118. }
  2119. return "";
  2120. }
  2121. } targetRuntimeDllsNode;
  2122. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  2123. {
  2124. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  2125. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2126. std::string Evaluate(
  2127. const std::vector<std::string>& parameters,
  2128. cmGeneratorExpressionContext* context,
  2129. const GeneratorExpressionContent* content,
  2130. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2131. {
  2132. cmGeneratorTarget const* target = context->HeadTarget;
  2133. if (!target) {
  2134. reportError(
  2135. context, content->GetOriginalExpression(),
  2136. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  2137. "not be used with add_custom_command or add_custom_target.");
  2138. return std::string();
  2139. }
  2140. context->HadHeadSensitiveCondition = true;
  2141. using LangMap = std::map<std::string, std::vector<std::string>>;
  2142. static LangMap availableFeatures;
  2143. LangMap testedFeatures;
  2144. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  2145. for (std::string const& p : parameters) {
  2146. std::string error;
  2147. std::string lang;
  2148. if (!standardResolver.CompileFeatureKnown(
  2149. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  2150. reportError(context, content->GetOriginalExpression(), error);
  2151. return std::string();
  2152. }
  2153. testedFeatures[lang].push_back(p);
  2154. if (availableFeatures.find(lang) == availableFeatures.end()) {
  2155. cmValue featuresKnown =
  2156. standardResolver.CompileFeaturesAvailable(lang, &error);
  2157. if (!featuresKnown) {
  2158. reportError(context, content->GetOriginalExpression(), error);
  2159. return std::string();
  2160. }
  2161. cmExpandList(featuresKnown, availableFeatures[lang]);
  2162. }
  2163. }
  2164. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  2165. for (auto const& lit : testedFeatures) {
  2166. std::vector<std::string> const& langAvailable =
  2167. availableFeatures[lit.first];
  2168. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  2169. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  2170. for (std::string const& it : lit.second) {
  2171. if (!cm::contains(langAvailable, it)) {
  2172. return "0";
  2173. }
  2174. if (standardDefault && standardDefault->empty()) {
  2175. // This compiler has no notion of language standard levels.
  2176. // All features known for the language are always available.
  2177. continue;
  2178. }
  2179. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  2180. context->Config, it)) {
  2181. if (evalLL) {
  2182. cmValue l =
  2183. target->GetLanguageStandard(lit.first, context->Config);
  2184. if (!l) {
  2185. l = standardDefault;
  2186. }
  2187. assert(l);
  2188. context->MaxLanguageStandard[target][lit.first] = *l;
  2189. } else {
  2190. return "0";
  2191. }
  2192. }
  2193. }
  2194. }
  2195. return "1";
  2196. }
  2197. } compileFeaturesNode;
  2198. static const char* targetPolicyWhitelist[] = {
  2199. nullptr
  2200. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  2201. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  2202. #undef TARGET_POLICY_STRING
  2203. };
  2204. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  2205. const char* policy)
  2206. {
  2207. #define RETURN_POLICY(POLICY) \
  2208. if (strcmp(policy, #POLICY) == 0) { \
  2209. return tgt->GetPolicyStatus##POLICY(); \
  2210. }
  2211. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  2212. #undef RETURN_POLICY
  2213. assert(false && "Unreachable code. Not a valid policy");
  2214. return cmPolicies::WARN;
  2215. }
  2216. static cmPolicies::PolicyID policyForString(const char* policy_id)
  2217. {
  2218. #define RETURN_POLICY_ID(POLICY_ID) \
  2219. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  2220. return cmPolicies::POLICY_ID; \
  2221. }
  2222. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  2223. #undef RETURN_POLICY_ID
  2224. assert(false && "Unreachable code. Not a valid policy");
  2225. return cmPolicies::CMP0002;
  2226. }
  2227. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  2228. {
  2229. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  2230. int NumExpectedParameters() const override { return 1; }
  2231. std::string Evaluate(
  2232. const std::vector<std::string>& parameters,
  2233. cmGeneratorExpressionContext* context,
  2234. const GeneratorExpressionContent* content,
  2235. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2236. {
  2237. if (!context->HeadTarget) {
  2238. reportError(
  2239. context, content->GetOriginalExpression(),
  2240. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  2241. "may not be used with add_custom_command or add_custom_target.");
  2242. return std::string();
  2243. }
  2244. context->HadContextSensitiveCondition = true;
  2245. context->HadHeadSensitiveCondition = true;
  2246. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  2247. const char* policy = targetPolicyWhitelist[i];
  2248. if (parameters.front() == policy) {
  2249. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  2250. switch (statusForTarget(context->HeadTarget, policy)) {
  2251. case cmPolicies::WARN:
  2252. lg->IssueMessage(
  2253. MessageType::AUTHOR_WARNING,
  2254. cmPolicies::GetPolicyWarning(policyForString(policy)));
  2255. CM_FALLTHROUGH;
  2256. case cmPolicies::REQUIRED_IF_USED:
  2257. case cmPolicies::REQUIRED_ALWAYS:
  2258. case cmPolicies::OLD:
  2259. return "0";
  2260. case cmPolicies::NEW:
  2261. return "1";
  2262. }
  2263. }
  2264. }
  2265. reportError(
  2266. context, content->GetOriginalExpression(),
  2267. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  2268. "policies. Currently it may be used with the following policies:\n"
  2269. #define STRINGIFY_HELPER(X) #X
  2270. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  2271. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  2272. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  2273. #undef TARGET_POLICY_LIST_ITEM
  2274. );
  2275. return std::string();
  2276. }
  2277. } targetPolicyNode;
  2278. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  2279. {
  2280. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  2281. bool GeneratesContent() const override { return true; }
  2282. int NumExpectedParameters() const override { return 0; }
  2283. std::string Evaluate(
  2284. const std::vector<std::string>& /*parameters*/,
  2285. cmGeneratorExpressionContext* context,
  2286. const GeneratorExpressionContent* content,
  2287. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2288. {
  2289. reportError(context, content->GetOriginalExpression(),
  2290. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  2291. "should never be evaluated.");
  2292. return std::string();
  2293. }
  2294. } installPrefixNode;
  2295. class ArtifactDirTag;
  2296. class ArtifactLinkerTag;
  2297. class ArtifactNameTag;
  2298. class ArtifactPathTag;
  2299. class ArtifactPdbTag;
  2300. class ArtifactSonameTag;
  2301. class ArtifactBundleDirTag;
  2302. class ArtifactBundleDirNameTag;
  2303. class ArtifactBundleContentDirTag;
  2304. template <typename ArtifactT, typename ComponentT>
  2305. struct TargetFilesystemArtifactDependency
  2306. {
  2307. static void AddDependency(cmGeneratorTarget* target,
  2308. cmGeneratorExpressionContext* context)
  2309. {
  2310. context->DependTargets.insert(target);
  2311. context->AllTargets.insert(target);
  2312. }
  2313. };
  2314. struct TargetFilesystemArtifactDependencyCMP0112
  2315. {
  2316. static void AddDependency(cmGeneratorTarget* target,
  2317. cmGeneratorExpressionContext* context)
  2318. {
  2319. context->AllTargets.insert(target);
  2320. cmLocalGenerator* lg = context->LG;
  2321. switch (target->GetPolicyStatusCMP0112()) {
  2322. case cmPolicies::WARN:
  2323. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  2324. "CMAKE_POLICY_WARNING_CMP0112")) {
  2325. std::string err =
  2326. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  2327. "\nDependency being added to target:\n \"",
  2328. target->GetName(), "\"\n");
  2329. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  2330. err, context->Backtrace);
  2331. }
  2332. CM_FALLTHROUGH;
  2333. case cmPolicies::OLD:
  2334. context->DependTargets.insert(target);
  2335. break;
  2336. case cmPolicies::REQUIRED_IF_USED:
  2337. case cmPolicies::REQUIRED_ALWAYS:
  2338. case cmPolicies::NEW:
  2339. break;
  2340. }
  2341. }
  2342. };
  2343. template <typename ArtifactT>
  2344. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  2345. : TargetFilesystemArtifactDependencyCMP0112
  2346. {
  2347. };
  2348. template <typename ArtifactT>
  2349. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  2350. : TargetFilesystemArtifactDependencyCMP0112
  2351. {
  2352. };
  2353. template <>
  2354. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  2355. ArtifactPathTag>
  2356. : TargetFilesystemArtifactDependencyCMP0112
  2357. {
  2358. };
  2359. template <>
  2360. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  2361. ArtifactPathTag>
  2362. : TargetFilesystemArtifactDependencyCMP0112
  2363. {
  2364. };
  2365. template <>
  2366. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  2367. ArtifactPathTag>
  2368. : TargetFilesystemArtifactDependencyCMP0112
  2369. {
  2370. };
  2371. template <typename ArtifactT>
  2372. struct TargetFilesystemArtifactResultCreator
  2373. {
  2374. static std::string Create(cmGeneratorTarget* target,
  2375. cmGeneratorExpressionContext* context,
  2376. const GeneratorExpressionContent* content);
  2377. };
  2378. template <>
  2379. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  2380. {
  2381. static std::string Create(cmGeneratorTarget* target,
  2382. cmGeneratorExpressionContext* context,
  2383. const GeneratorExpressionContent* content)
  2384. {
  2385. // The target soname file (.so.1).
  2386. if (target->IsDLLPlatform()) {
  2387. ::reportError(context, content->GetOriginalExpression(),
  2388. "TARGET_SONAME_FILE is not allowed "
  2389. "for DLL target platforms.");
  2390. return std::string();
  2391. }
  2392. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2393. ::reportError(context, content->GetOriginalExpression(),
  2394. "TARGET_SONAME_FILE is allowed only for "
  2395. "SHARED libraries.");
  2396. return std::string();
  2397. }
  2398. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  2399. target->GetSOName(context->Config));
  2400. return result;
  2401. }
  2402. };
  2403. template <>
  2404. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  2405. {
  2406. static std::string Create(cmGeneratorTarget* target,
  2407. cmGeneratorExpressionContext* context,
  2408. const GeneratorExpressionContent* content)
  2409. {
  2410. if (target->IsImported()) {
  2411. ::reportError(context, content->GetOriginalExpression(),
  2412. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  2413. return std::string();
  2414. }
  2415. std::string language = target->GetLinkerLanguage(context->Config);
  2416. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2417. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2418. ::reportError(context, content->GetOriginalExpression(),
  2419. "TARGET_PDB_FILE is not supported by the target linker.");
  2420. return std::string();
  2421. }
  2422. cmStateEnums::TargetType targetType = target->GetType();
  2423. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2424. targetType != cmStateEnums::MODULE_LIBRARY &&
  2425. targetType != cmStateEnums::EXECUTABLE) {
  2426. ::reportError(context, content->GetOriginalExpression(),
  2427. "TARGET_PDB_FILE is allowed only for "
  2428. "targets with linker created artifacts.");
  2429. return std::string();
  2430. }
  2431. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  2432. '/', target->GetPDBName(context->Config));
  2433. return result;
  2434. }
  2435. };
  2436. template <>
  2437. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  2438. {
  2439. static std::string Create(cmGeneratorTarget* target,
  2440. cmGeneratorExpressionContext* context,
  2441. const GeneratorExpressionContent* content)
  2442. {
  2443. // The file used to link to the target (.so, .lib, .a).
  2444. if (!target->IsLinkable()) {
  2445. ::reportError(context, content->GetOriginalExpression(),
  2446. "TARGET_LINKER_FILE is allowed only for libraries and "
  2447. "executables with ENABLE_EXPORTS.");
  2448. return std::string();
  2449. }
  2450. cmStateEnums::ArtifactType artifact =
  2451. target->HasImportLibrary(context->Config)
  2452. ? cmStateEnums::ImportLibraryArtifact
  2453. : cmStateEnums::RuntimeBinaryArtifact;
  2454. return target->GetFullPath(context->Config, artifact);
  2455. }
  2456. };
  2457. template <>
  2458. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  2459. {
  2460. static std::string Create(cmGeneratorTarget* target,
  2461. cmGeneratorExpressionContext* context,
  2462. const GeneratorExpressionContent* content)
  2463. {
  2464. if (target->IsImported()) {
  2465. ::reportError(context, content->GetOriginalExpression(),
  2466. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  2467. return std::string();
  2468. }
  2469. if (!target->IsBundleOnApple()) {
  2470. ::reportError(context, content->GetOriginalExpression(),
  2471. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  2472. return std::string();
  2473. }
  2474. std::string outpath = target->GetDirectory(context->Config) + '/';
  2475. return target->BuildBundleDirectory(outpath, context->Config,
  2476. cmGeneratorTarget::BundleDirLevel);
  2477. }
  2478. };
  2479. template <>
  2480. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  2481. {
  2482. static std::string Create(cmGeneratorTarget* target,
  2483. cmGeneratorExpressionContext* context,
  2484. const GeneratorExpressionContent* content)
  2485. {
  2486. if (target->IsImported()) {
  2487. ::reportError(
  2488. context, content->GetOriginalExpression(),
  2489. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  2490. return std::string();
  2491. }
  2492. if (!target->IsBundleOnApple()) {
  2493. ::reportError(
  2494. context, content->GetOriginalExpression(),
  2495. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  2496. return std::string();
  2497. }
  2498. auto level = cmGeneratorTarget::BundleDirLevel;
  2499. auto config = context->Config;
  2500. if (target->IsAppBundleOnApple()) {
  2501. return target->GetAppBundleDirectory(config, level);
  2502. }
  2503. if (target->IsFrameworkOnApple()) {
  2504. return target->GetFrameworkDirectory(config, level);
  2505. }
  2506. if (target->IsCFBundleOnApple()) {
  2507. return target->GetCFBundleDirectory(config, level);
  2508. }
  2509. return std::string();
  2510. }
  2511. };
  2512. template <>
  2513. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  2514. {
  2515. static std::string Create(cmGeneratorTarget* target,
  2516. cmGeneratorExpressionContext* context,
  2517. const GeneratorExpressionContent* content)
  2518. {
  2519. if (target->IsImported()) {
  2520. ::reportError(
  2521. context, content->GetOriginalExpression(),
  2522. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  2523. return std::string();
  2524. }
  2525. if (!target->IsBundleOnApple()) {
  2526. ::reportError(
  2527. context, content->GetOriginalExpression(),
  2528. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  2529. return std::string();
  2530. }
  2531. std::string outpath = target->GetDirectory(context->Config) + '/';
  2532. return target->BuildBundleDirectory(outpath, context->Config,
  2533. cmGeneratorTarget::ContentLevel);
  2534. }
  2535. };
  2536. template <>
  2537. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  2538. {
  2539. static std::string Create(cmGeneratorTarget* target,
  2540. cmGeneratorExpressionContext* context,
  2541. const GeneratorExpressionContent* /*unused*/)
  2542. {
  2543. return target->GetFullPath(context->Config,
  2544. cmStateEnums::RuntimeBinaryArtifact, true);
  2545. }
  2546. };
  2547. template <typename ArtifactT>
  2548. struct TargetFilesystemArtifactResultGetter
  2549. {
  2550. static std::string Get(const std::string& result);
  2551. };
  2552. template <>
  2553. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  2554. {
  2555. static std::string Get(const std::string& result)
  2556. {
  2557. return cmSystemTools::GetFilenameName(result);
  2558. }
  2559. };
  2560. template <>
  2561. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  2562. {
  2563. static std::string Get(const std::string& result)
  2564. {
  2565. return cmSystemTools::GetFilenamePath(result);
  2566. }
  2567. };
  2568. template <>
  2569. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  2570. {
  2571. static std::string Get(const std::string& result) { return result; }
  2572. };
  2573. struct TargetArtifactBase : public cmGeneratorExpressionNode
  2574. {
  2575. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  2576. protected:
  2577. cmGeneratorTarget* GetTarget(
  2578. const std::vector<std::string>& parameters,
  2579. cmGeneratorExpressionContext* context,
  2580. const GeneratorExpressionContent* content,
  2581. cmGeneratorExpressionDAGChecker* dagChecker) const
  2582. {
  2583. // Lookup the referenced target.
  2584. std::string const& name = parameters.front();
  2585. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  2586. ::reportError(context, content->GetOriginalExpression(),
  2587. "Expression syntax not recognized.");
  2588. return nullptr;
  2589. }
  2590. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  2591. if (!target) {
  2592. ::reportError(context, content->GetOriginalExpression(),
  2593. "No target \"" + name + "\"");
  2594. return nullptr;
  2595. }
  2596. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  2597. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  2598. ::reportError(context, content->GetOriginalExpression(),
  2599. "Target \"" + name +
  2600. "\" is not an executable or library.");
  2601. return nullptr;
  2602. }
  2603. if (dagChecker &&
  2604. (dagChecker->EvaluatingLinkLibraries(target) ||
  2605. (dagChecker->EvaluatingSources() &&
  2606. target == dagChecker->TopTarget()))) {
  2607. ::reportError(context, content->GetOriginalExpression(),
  2608. "Expressions which require the linker language may not "
  2609. "be used while evaluating link libraries");
  2610. return nullptr;
  2611. }
  2612. return target;
  2613. }
  2614. };
  2615. template <typename ArtifactT, typename ComponentT>
  2616. struct TargetFilesystemArtifact : public TargetArtifactBase
  2617. {
  2618. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  2619. int NumExpectedParameters() const override { return 1; }
  2620. std::string Evaluate(
  2621. const std::vector<std::string>& parameters,
  2622. cmGeneratorExpressionContext* context,
  2623. const GeneratorExpressionContent* content,
  2624. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2625. {
  2626. cmGeneratorTarget* target =
  2627. this->GetTarget(parameters, context, content, dagChecker);
  2628. if (!target) {
  2629. return std::string();
  2630. }
  2631. // Not a dependent target if we are querying for ArtifactDirTag,
  2632. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  2633. // and ArtifactBundleContentDirTag
  2634. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  2635. target, context);
  2636. std::string result =
  2637. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  2638. content);
  2639. if (context->HadError) {
  2640. return std::string();
  2641. }
  2642. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2643. }
  2644. };
  2645. template <typename ArtifactT>
  2646. struct TargetFilesystemArtifactNodeGroup
  2647. {
  2648. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2649. {
  2650. }
  2651. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2652. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2653. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2654. };
  2655. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2656. targetNodeGroup;
  2657. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2658. targetLinkerNodeGroup;
  2659. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2660. targetSoNameNodeGroup;
  2661. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2662. targetPdbNodeGroup;
  2663. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2664. targetBundleDirNode;
  2665. static const TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  2666. ArtifactNameTag>
  2667. targetBundleDirNameNode;
  2668. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2669. ArtifactPathTag>
  2670. targetBundleContentDirNode;
  2671. //
  2672. // To retrieve base name for various artifacts
  2673. //
  2674. template <typename ArtifactT>
  2675. struct TargetOutputNameArtifactResultGetter
  2676. {
  2677. static std::string Get(cmGeneratorTarget* target,
  2678. cmGeneratorExpressionContext* context,
  2679. const GeneratorExpressionContent* content);
  2680. };
  2681. template <>
  2682. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2683. {
  2684. static std::string Get(cmGeneratorTarget* target,
  2685. cmGeneratorExpressionContext* context,
  2686. const GeneratorExpressionContent* /*unused*/)
  2687. {
  2688. return target->GetOutputName(context->Config,
  2689. cmStateEnums::RuntimeBinaryArtifact) +
  2690. target->GetFilePostfix(context->Config);
  2691. }
  2692. };
  2693. template <>
  2694. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2695. {
  2696. static std::string Get(cmGeneratorTarget* target,
  2697. cmGeneratorExpressionContext* context,
  2698. const GeneratorExpressionContent* content)
  2699. {
  2700. // The file used to link to the target (.so, .lib, .a).
  2701. if (!target->IsLinkable()) {
  2702. ::reportError(context, content->GetOriginalExpression(),
  2703. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2704. "libraries and executables with ENABLE_EXPORTS.");
  2705. return std::string();
  2706. }
  2707. cmStateEnums::ArtifactType artifact =
  2708. target->HasImportLibrary(context->Config)
  2709. ? cmStateEnums::ImportLibraryArtifact
  2710. : cmStateEnums::RuntimeBinaryArtifact;
  2711. return target->GetOutputName(context->Config, artifact) +
  2712. target->GetFilePostfix(context->Config);
  2713. }
  2714. };
  2715. template <>
  2716. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2717. {
  2718. static std::string Get(cmGeneratorTarget* target,
  2719. cmGeneratorExpressionContext* context,
  2720. const GeneratorExpressionContent* content)
  2721. {
  2722. if (target->IsImported()) {
  2723. ::reportError(
  2724. context, content->GetOriginalExpression(),
  2725. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2726. return std::string();
  2727. }
  2728. std::string language = target->GetLinkerLanguage(context->Config);
  2729. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2730. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2731. ::reportError(
  2732. context, content->GetOriginalExpression(),
  2733. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2734. return std::string();
  2735. }
  2736. cmStateEnums::TargetType targetType = target->GetType();
  2737. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2738. targetType != cmStateEnums::MODULE_LIBRARY &&
  2739. targetType != cmStateEnums::EXECUTABLE) {
  2740. ::reportError(context, content->GetOriginalExpression(),
  2741. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2742. "targets with linker created artifacts.");
  2743. return std::string();
  2744. }
  2745. return target->GetPDBOutputName(context->Config) +
  2746. target->GetFilePostfix(context->Config);
  2747. }
  2748. };
  2749. template <typename ArtifactT>
  2750. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2751. {
  2752. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2753. int NumExpectedParameters() const override { return 1; }
  2754. std::string Evaluate(
  2755. const std::vector<std::string>& parameters,
  2756. cmGeneratorExpressionContext* context,
  2757. const GeneratorExpressionContent* content,
  2758. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2759. {
  2760. cmGeneratorTarget* target =
  2761. this->GetTarget(parameters, context, content, dagChecker);
  2762. if (!target) {
  2763. return std::string();
  2764. }
  2765. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2766. target, context, content);
  2767. if (context->HadError) {
  2768. return std::string();
  2769. }
  2770. return result;
  2771. }
  2772. };
  2773. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2774. targetFileBaseNameNode;
  2775. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2776. targetLinkerFileBaseNameNode;
  2777. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2778. targetPdbFileBaseNameNode;
  2779. class ArtifactFilePrefixTag;
  2780. class ArtifactLinkerFilePrefixTag;
  2781. class ArtifactFileSuffixTag;
  2782. class ArtifactLinkerFileSuffixTag;
  2783. template <typename ArtifactT>
  2784. struct TargetFileArtifactResultGetter
  2785. {
  2786. static std::string Get(cmGeneratorTarget* target,
  2787. cmGeneratorExpressionContext* context,
  2788. const GeneratorExpressionContent* content);
  2789. };
  2790. template <>
  2791. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2792. {
  2793. static std::string Get(cmGeneratorTarget* target,
  2794. cmGeneratorExpressionContext* context,
  2795. const GeneratorExpressionContent*)
  2796. {
  2797. return target->GetFilePrefix(context->Config);
  2798. }
  2799. };
  2800. template <>
  2801. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2802. {
  2803. static std::string Get(cmGeneratorTarget* target,
  2804. cmGeneratorExpressionContext* context,
  2805. const GeneratorExpressionContent* content)
  2806. {
  2807. if (!target->IsLinkable()) {
  2808. ::reportError(context, content->GetOriginalExpression(),
  2809. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2810. "executables with ENABLE_EXPORTS.");
  2811. return std::string();
  2812. }
  2813. cmStateEnums::ArtifactType artifact =
  2814. target->HasImportLibrary(context->Config)
  2815. ? cmStateEnums::ImportLibraryArtifact
  2816. : cmStateEnums::RuntimeBinaryArtifact;
  2817. return target->GetFilePrefix(context->Config, artifact);
  2818. }
  2819. };
  2820. template <>
  2821. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2822. {
  2823. static std::string Get(cmGeneratorTarget* target,
  2824. cmGeneratorExpressionContext* context,
  2825. const GeneratorExpressionContent*)
  2826. {
  2827. return target->GetFileSuffix(context->Config);
  2828. }
  2829. };
  2830. template <>
  2831. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2832. {
  2833. static std::string Get(cmGeneratorTarget* target,
  2834. cmGeneratorExpressionContext* context,
  2835. const GeneratorExpressionContent* content)
  2836. {
  2837. if (!target->IsLinkable()) {
  2838. ::reportError(context, content->GetOriginalExpression(),
  2839. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2840. "executables with ENABLE_EXPORTS.");
  2841. return std::string();
  2842. }
  2843. cmStateEnums::ArtifactType artifact =
  2844. target->HasImportLibrary(context->Config)
  2845. ? cmStateEnums::ImportLibraryArtifact
  2846. : cmStateEnums::RuntimeBinaryArtifact;
  2847. return target->GetFileSuffix(context->Config, artifact);
  2848. }
  2849. };
  2850. template <typename ArtifactT>
  2851. struct TargetFileArtifact : public TargetArtifactBase
  2852. {
  2853. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2854. int NumExpectedParameters() const override { return 1; }
  2855. std::string Evaluate(
  2856. const std::vector<std::string>& parameters,
  2857. cmGeneratorExpressionContext* context,
  2858. const GeneratorExpressionContent* content,
  2859. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2860. {
  2861. cmGeneratorTarget* target =
  2862. this->GetTarget(parameters, context, content, dagChecker);
  2863. if (!target) {
  2864. return std::string();
  2865. }
  2866. std::string result =
  2867. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2868. if (context->HadError) {
  2869. return std::string();
  2870. }
  2871. return result;
  2872. }
  2873. };
  2874. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2875. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2876. targetLinkerFilePrefixNode;
  2877. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2878. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2879. targetLinkerFileSuffixNode;
  2880. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2881. {
  2882. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2883. std::string Evaluate(
  2884. const std::vector<std::string>& parameters,
  2885. cmGeneratorExpressionContext* context,
  2886. const GeneratorExpressionContent* content,
  2887. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2888. {
  2889. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2890. if (listIn.empty()) {
  2891. reportError(context, content->GetOriginalExpression(),
  2892. "\"\" is not an absolute path.");
  2893. return std::string();
  2894. }
  2895. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2896. cmOutputConverter converter(snapshot);
  2897. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2898. std::vector<std::string> listOut;
  2899. listOut.reserve(listIn.size());
  2900. for (auto const& in : listIn) {
  2901. if (!cmSystemTools::FileIsFullPath(in)) {
  2902. reportError(context, content->GetOriginalExpression(),
  2903. "\"" + in + "\" is not an absolute path.");
  2904. return std::string();
  2905. }
  2906. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2907. }
  2908. return cmJoin(listOut, separator);
  2909. }
  2910. } shellPathNode;
  2911. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2912. const std::string& identifier)
  2913. {
  2914. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2915. { "0", &zeroNode },
  2916. { "1", &oneNode },
  2917. { "AND", &andNode },
  2918. { "OR", &orNode },
  2919. { "NOT", &notNode },
  2920. { "C_COMPILER_ID", &cCompilerIdNode },
  2921. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2922. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2923. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2924. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2925. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2926. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2927. { "VERSION_GREATER", &versionGreaterNode },
  2928. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2929. { "VERSION_LESS", &versionLessNode },
  2930. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2931. { "VERSION_EQUAL", &versionEqualNode },
  2932. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2933. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2934. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2935. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2936. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2937. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2938. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2939. { "PLATFORM_ID", &platformIdNode },
  2940. { "COMPILE_FEATURES", &compileFeaturesNode },
  2941. { "CONFIGURATION", &configurationNode },
  2942. { "CONFIG", &configurationTestNode },
  2943. { "TARGET_FILE", &targetNodeGroup.File },
  2944. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2945. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2946. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2947. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2948. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2949. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2950. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2951. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2952. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2953. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2954. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2955. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2956. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2957. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2958. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2959. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2960. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2961. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2962. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2963. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  2964. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2965. { "STREQUAL", &strEqualNode },
  2966. { "EQUAL", &equalNode },
  2967. { "IN_LIST", &inListNode },
  2968. { "FILTER", &filterNode },
  2969. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2970. { "LOWER_CASE", &lowerCaseNode },
  2971. { "UPPER_CASE", &upperCaseNode },
  2972. { "PATH", &pathNode },
  2973. { "PATH_EQUAL", &pathEqualNode },
  2974. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2975. { "BOOL", &boolNode },
  2976. { "IF", &ifNode },
  2977. { "ANGLE-R", &angle_rNode },
  2978. { "COMMA", &commaNode },
  2979. { "SEMICOLON", &semicolonNode },
  2980. { "TARGET_PROPERTY", &targetPropertyNode },
  2981. { "TARGET_NAME", &targetNameNode },
  2982. { "TARGET_OBJECTS", &targetObjectsNode },
  2983. { "TARGET_POLICY", &targetPolicyNode },
  2984. { "TARGET_EXISTS", &targetExistsNode },
  2985. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2986. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2987. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2988. { "GENEX_EVAL", &genexEvalNode },
  2989. { "BUILD_INTERFACE", &buildInterfaceNode },
  2990. { "INSTALL_INTERFACE", &installInterfaceNode },
  2991. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  2992. { "INSTALL_PREFIX", &installPrefixNode },
  2993. { "JOIN", &joinNode },
  2994. { "LINK_ONLY", &linkOnlyNode },
  2995. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2996. { "COMPILE_LANGUAGE", &languageNode },
  2997. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2998. { "LINK_LANGUAGE", &linkLanguageNode },
  2999. { "LINK_LIBRARY", &linkLibraryNode },
  3000. { "LINK_GROUP", &linkGroupNode },
  3001. { "HOST_LINK", &hostLinkNode },
  3002. { "DEVICE_LINK", &deviceLinkNode },
  3003. { "SHELL_PATH", &shellPathNode }
  3004. };
  3005. {
  3006. auto itr = nodeMap.find(identifier);
  3007. if (itr != nodeMap.end()) {
  3008. return itr->second;
  3009. }
  3010. }
  3011. return nullptr;
  3012. }
  3013. void reportError(cmGeneratorExpressionContext* context,
  3014. const std::string& expr, const std::string& result)
  3015. {
  3016. context->HadError = true;
  3017. if (context->Quiet) {
  3018. return;
  3019. }
  3020. std::ostringstream e;
  3021. /* clang-format off */
  3022. e << "Error evaluating generator expression:\n"
  3023. << " " << expr << "\n"
  3024. << result;
  3025. /* clang-format on */
  3026. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  3027. e.str(), context->Backtrace);
  3028. }