cmGeneratorExpressionNode.cxx 178 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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 <stdexcept>
  15. #include <unordered_map>
  16. #include <utility>
  17. #include <cm/iterator>
  18. #include <cm/optional>
  19. #include <cm/string_view>
  20. #include <cmext/algorithm>
  21. #include <cmext/string_view>
  22. #include "cmsys/RegularExpression.hxx"
  23. #include "cmsys/String.h"
  24. #include "cmCMakePath.h"
  25. #include "cmComputeLinkInformation.h"
  26. #include "cmGeneratorExpression.h"
  27. #include "cmGeneratorExpressionContext.h"
  28. #include "cmGeneratorExpressionDAGChecker.h"
  29. #include "cmGeneratorExpressionEvaluator.h"
  30. #include "cmGeneratorTarget.h"
  31. #include "cmGlobalGenerator.h"
  32. #include "cmLinkItem.h"
  33. #include "cmList.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 const* 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. std::vector<std::string> const& /*parameters*/,
  80. cmGeneratorExpressionContext* /*context*/,
  81. GeneratorExpressionContent const* /*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. std::vector<std::string> const& parameters,
  93. cmGeneratorExpressionContext* /*context*/,
  94. GeneratorExpressionContent const* /*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(char const* op_, char const* successVal_,
  106. char const* failureVal_)
  107. : op(op_)
  108. , successVal(successVal_)
  109. , failureVal(failureVal_)
  110. {
  111. }
  112. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  113. bool ShouldEvaluateNextParameter(std::vector<std::string> const& parameters,
  114. std::string& def_value) const override
  115. {
  116. if (!parameters.empty() && parameters.back() == failureVal) {
  117. def_value = failureVal;
  118. return false;
  119. }
  120. return true;
  121. }
  122. std::string Evaluate(std::vector<std::string> const& parameters,
  123. cmGeneratorExpressionContext* context,
  124. GeneratorExpressionContent const* content,
  125. cmGeneratorExpressionDAGChecker*) const override
  126. {
  127. for (std::string const& param : parameters) {
  128. if (param == this->failureVal) {
  129. return this->failureVal;
  130. }
  131. if (param != this->successVal) {
  132. std::ostringstream e;
  133. e << "Parameters to $<" << this->op;
  134. e << "> must resolve to either '0' or '1'.";
  135. reportError(context, content->GetOriginalExpression(), e.str());
  136. return std::string();
  137. }
  138. }
  139. return this->successVal;
  140. }
  141. char const *const op, *const successVal, *const failureVal;
  142. };
  143. static BooleanOpNode const andNode("AND", "1", "0"), orNode("OR", "0", "1");
  144. static const struct NotNode : public cmGeneratorExpressionNode
  145. {
  146. NotNode() {} // NOLINT(modernize-use-equals-default)
  147. std::string Evaluate(
  148. std::vector<std::string> const& parameters,
  149. cmGeneratorExpressionContext* context,
  150. GeneratorExpressionContent const* content,
  151. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  152. {
  153. if (parameters.front() != "0" && parameters.front() != "1") {
  154. reportError(
  155. context, content->GetOriginalExpression(),
  156. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  157. return std::string();
  158. }
  159. return parameters.front() == "0" ? "1" : "0";
  160. }
  161. } notNode;
  162. static const struct BoolNode : public cmGeneratorExpressionNode
  163. {
  164. BoolNode() {} // NOLINT(modernize-use-equals-default)
  165. int NumExpectedParameters() const override { return 1; }
  166. std::string Evaluate(
  167. std::vector<std::string> const& parameters,
  168. cmGeneratorExpressionContext* /*context*/,
  169. GeneratorExpressionContent const* /*content*/,
  170. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  171. {
  172. return !cmIsOff(parameters.front()) ? "1" : "0";
  173. }
  174. } boolNode;
  175. static const struct IfNode : public cmGeneratorExpressionNode
  176. {
  177. IfNode() {} // NOLINT(modernize-use-equals-default)
  178. int NumExpectedParameters() const override { return 3; }
  179. bool ShouldEvaluateNextParameter(std::vector<std::string> const& parameters,
  180. std::string&) const override
  181. {
  182. return (parameters.empty() ||
  183. parameters[0] != cmStrCat(parameters.size() - 1, ""));
  184. }
  185. std::string Evaluate(std::vector<std::string> const& parameters,
  186. cmGeneratorExpressionContext* context,
  187. GeneratorExpressionContent const* content,
  188. cmGeneratorExpressionDAGChecker*) const override
  189. {
  190. if (parameters[0] != "1" && parameters[0] != "0") {
  191. reportError(context, content->GetOriginalExpression(),
  192. "First parameter to $<IF> must resolve to exactly one '0' "
  193. "or '1' value.");
  194. return std::string();
  195. }
  196. return parameters[0] == "1" ? parameters[1] : parameters[2];
  197. }
  198. } ifNode;
  199. static const struct StrEqualNode : public cmGeneratorExpressionNode
  200. {
  201. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  202. int NumExpectedParameters() const override { return 2; }
  203. std::string Evaluate(
  204. std::vector<std::string> const& parameters,
  205. cmGeneratorExpressionContext* /*context*/,
  206. GeneratorExpressionContent const* /*content*/,
  207. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  208. {
  209. return parameters.front() == parameters[1] ? "1" : "0";
  210. }
  211. } strEqualNode;
  212. static const struct EqualNode : public cmGeneratorExpressionNode
  213. {
  214. EqualNode() {} // NOLINT(modernize-use-equals-default)
  215. int NumExpectedParameters() const override { return 2; }
  216. std::string Evaluate(
  217. std::vector<std::string> const& parameters,
  218. cmGeneratorExpressionContext* context,
  219. GeneratorExpressionContent const* content,
  220. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  221. {
  222. long numbers[2];
  223. for (int i = 0; i < 2; ++i) {
  224. if (!ParameterToLong(parameters[i].c_str(), &numbers[i])) {
  225. reportError(context, content->GetOriginalExpression(),
  226. "$<EQUAL> parameter " + parameters[i] +
  227. " is not a valid integer.");
  228. return {};
  229. }
  230. }
  231. return numbers[0] == numbers[1] ? "1" : "0";
  232. }
  233. static bool ParameterToLong(char const* param, long* outResult)
  234. {
  235. char const isNegative = param[0] == '-';
  236. int base = 0;
  237. if (cmHasLiteralPrefix(param, "0b") || cmHasLiteralPrefix(param, "0B")) {
  238. base = 2;
  239. param += 2;
  240. } else if (cmHasLiteralPrefix(param, "-0b") ||
  241. cmHasLiteralPrefix(param, "-0B") ||
  242. cmHasLiteralPrefix(param, "+0b") ||
  243. cmHasLiteralPrefix(param, "+0B")) {
  244. base = 2;
  245. param += 3;
  246. }
  247. char* pEnd;
  248. long result = strtol(param, &pEnd, base);
  249. if (pEnd == param || *pEnd != '\0' || errno == ERANGE) {
  250. return false;
  251. }
  252. if (isNegative && result > 0) {
  253. result *= -1;
  254. }
  255. *outResult = result;
  256. return true;
  257. }
  258. } equalNode;
  259. static const struct InListNode : public cmGeneratorExpressionNode
  260. {
  261. InListNode() {} // NOLINT(modernize-use-equals-default)
  262. int NumExpectedParameters() const override { return 2; }
  263. std::string Evaluate(
  264. std::vector<std::string> const& parameters,
  265. cmGeneratorExpressionContext* context,
  266. GeneratorExpressionContent const* /*content*/,
  267. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  268. {
  269. cmList values;
  270. cmList checkValues;
  271. bool check = false;
  272. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  273. case cmPolicies::WARN:
  274. if (parameters.front().empty()) {
  275. check = true;
  276. checkValues.assign(parameters[1], cmList::EmptyElements::Yes);
  277. }
  278. CM_FALLTHROUGH;
  279. case cmPolicies::OLD:
  280. values.assign(parameters[1]);
  281. if (check && values != checkValues) {
  282. std::ostringstream e;
  283. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  284. << "\nSearch Item:\n \"" << parameters.front()
  285. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  286. context->LG->GetCMakeInstance()->IssueMessage(
  287. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  288. return "0";
  289. }
  290. if (values.empty()) {
  291. return "0";
  292. }
  293. break;
  294. case cmPolicies::NEW:
  295. values.assign(parameters[1], cmList::EmptyElements::Yes);
  296. break;
  297. }
  298. return values.find(parameters.front()) != cmList::npos ? "1" : "0";
  299. }
  300. } inListNode;
  301. static const struct FilterNode : public cmGeneratorExpressionNode
  302. {
  303. FilterNode() {} // NOLINT(modernize-use-equals-default)
  304. int NumExpectedParameters() const override { return 3; }
  305. std::string Evaluate(
  306. std::vector<std::string> const& parameters,
  307. cmGeneratorExpressionContext* context,
  308. GeneratorExpressionContent const* content,
  309. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  310. {
  311. if (parameters.size() != 3) {
  312. reportError(context, content->GetOriginalExpression(),
  313. "$<FILTER:...> expression requires three parameters");
  314. return {};
  315. }
  316. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  317. reportError(
  318. context, content->GetOriginalExpression(),
  319. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  320. return {};
  321. }
  322. try {
  323. return cmList{ parameters.front(), cmList::EmptyElements::Yes }
  324. .filter(parameters[2],
  325. parameters[1] == "EXCLUDE" ? cmList::FilterMode::EXCLUDE
  326. : cmList::FilterMode::INCLUDE)
  327. .to_string();
  328. } catch (std::invalid_argument&) {
  329. reportError(context, content->GetOriginalExpression(),
  330. "$<FILTER:...> failed to compile regex");
  331. return {};
  332. }
  333. }
  334. } filterNode;
  335. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  336. {
  337. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  338. int NumExpectedParameters() const override { return 1; }
  339. std::string Evaluate(
  340. std::vector<std::string> const& parameters,
  341. cmGeneratorExpressionContext* context,
  342. GeneratorExpressionContent const* content,
  343. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  344. {
  345. if (parameters.size() != 1) {
  346. reportError(
  347. context, content->GetOriginalExpression(),
  348. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  349. }
  350. return cmList{ parameters.front(), cmList::EmptyElements::Yes }
  351. .remove_duplicates()
  352. .to_string();
  353. }
  354. } removeDuplicatesNode;
  355. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  356. {
  357. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  358. int NumExpectedParameters() const override { return 1; }
  359. std::string Evaluate(
  360. std::vector<std::string> const& parameters,
  361. cmGeneratorExpressionContext* context,
  362. GeneratorExpressionContent const* content,
  363. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  364. {
  365. if (parameters.size() != 1) {
  366. reportError(context, content->GetOriginalExpression(),
  367. "$<TARGET_EXISTS:...> expression requires one parameter");
  368. return std::string();
  369. }
  370. std::string const& targetName = parameters.front();
  371. if (targetName.empty() ||
  372. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  373. reportError(context, content->GetOriginalExpression(),
  374. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  375. "valid target name.");
  376. return std::string();
  377. }
  378. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  379. }
  380. } targetExistsNode;
  381. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  382. {
  383. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  384. int NumExpectedParameters() const override { return 1; }
  385. std::string Evaluate(
  386. std::vector<std::string> const& parameters,
  387. cmGeneratorExpressionContext* context,
  388. GeneratorExpressionContent const* content,
  389. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  390. {
  391. if (parameters.size() != 1) {
  392. reportError(context, content->GetOriginalExpression(),
  393. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  394. "parameter");
  395. return std::string();
  396. }
  397. std::string const& targetName = parameters.front();
  398. if (targetName.empty() ||
  399. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  400. reportError(context, content->GetOriginalExpression(),
  401. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  402. "non-empty valid target name.");
  403. return std::string();
  404. }
  405. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  406. ? targetName
  407. : std::string();
  408. }
  409. } targetNameIfExistsNode;
  410. struct GenexEvaluator : public cmGeneratorExpressionNode
  411. {
  412. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  413. protected:
  414. std::string EvaluateExpression(
  415. std::string const& genexOperator, std::string const& expression,
  416. cmGeneratorExpressionContext* context,
  417. GeneratorExpressionContent const* content,
  418. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  419. {
  420. if (context->HeadTarget) {
  421. cmGeneratorExpressionDAGChecker dagChecker{
  422. context->HeadTarget,
  423. genexOperator + ":" + expression,
  424. content,
  425. dagCheckerParent,
  426. context->LG,
  427. context->Config,
  428. context->Backtrace,
  429. };
  430. switch (dagChecker.Check()) {
  431. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  432. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  433. dagChecker.ReportError(context, content->GetOriginalExpression());
  434. return std::string();
  435. }
  436. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  437. case cmGeneratorExpressionDAGChecker::DAG:
  438. break;
  439. }
  440. return this->EvaluateDependentExpression(
  441. expression, context->LG, context, context->HeadTarget, &dagChecker,
  442. context->CurrentTarget);
  443. }
  444. return this->EvaluateDependentExpression(
  445. expression, context->LG, context, context->HeadTarget, dagCheckerParent,
  446. context->CurrentTarget);
  447. }
  448. };
  449. static const struct TargetGenexEvalNode : public GenexEvaluator
  450. {
  451. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  452. int NumExpectedParameters() const override { return 2; }
  453. bool AcceptsArbitraryContentParameter() const override { return true; }
  454. std::string Evaluate(
  455. std::vector<std::string> const& parameters,
  456. cmGeneratorExpressionContext* context,
  457. GeneratorExpressionContent const* content,
  458. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  459. {
  460. std::string const& targetName = parameters.front();
  461. if (targetName.empty() ||
  462. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  463. reportError(context, content->GetOriginalExpression(),
  464. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  465. "non-empty valid target name.");
  466. return std::string();
  467. }
  468. auto const* target = context->LG->FindGeneratorTargetToUse(targetName);
  469. if (!target) {
  470. std::ostringstream e;
  471. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  472. << "\" not found.";
  473. reportError(context, content->GetOriginalExpression(), e.str());
  474. return std::string();
  475. }
  476. std::string const& expression = parameters[1];
  477. if (expression.empty()) {
  478. return expression;
  479. }
  480. // Replace the surrounding context with the named target.
  481. cmGeneratorExpressionContext targetContext(
  482. context->LG, context->Config, context->Quiet, target, target,
  483. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  484. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  485. &targetContext, content, dagCheckerParent);
  486. }
  487. } targetGenexEvalNode;
  488. static const struct GenexEvalNode : public GenexEvaluator
  489. {
  490. GenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  491. int NumExpectedParameters() const override { return 1; }
  492. bool AcceptsArbitraryContentParameter() const override { return true; }
  493. std::string Evaluate(
  494. std::vector<std::string> const& parameters,
  495. cmGeneratorExpressionContext* context,
  496. GeneratorExpressionContent const* content,
  497. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  498. {
  499. std::string const& expression = parameters[0];
  500. if (expression.empty()) {
  501. return expression;
  502. }
  503. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  504. dagCheckerParent);
  505. }
  506. } genexEvalNode;
  507. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  508. {
  509. LowerCaseNode() {} // NOLINT(modernize-use-equals-default)
  510. bool AcceptsArbitraryContentParameter() const override { return true; }
  511. std::string Evaluate(
  512. std::vector<std::string> const& parameters,
  513. cmGeneratorExpressionContext* /*context*/,
  514. GeneratorExpressionContent const* /*content*/,
  515. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  516. {
  517. return cmSystemTools::LowerCase(parameters.front());
  518. }
  519. } lowerCaseNode;
  520. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  521. {
  522. UpperCaseNode() {} // NOLINT(modernize-use-equals-default)
  523. bool AcceptsArbitraryContentParameter() const override { return true; }
  524. std::string Evaluate(
  525. std::vector<std::string> const& parameters,
  526. cmGeneratorExpressionContext* /*context*/,
  527. GeneratorExpressionContent const* /*content*/,
  528. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  529. {
  530. return cmSystemTools::UpperCase(parameters.front());
  531. }
  532. } upperCaseNode;
  533. namespace {
  534. template <typename Container>
  535. class Range : public cmRange<typename Container::const_iterator>
  536. {
  537. private:
  538. using Base = cmRange<typename Container::const_iterator>;
  539. public:
  540. using const_iterator = typename Container::const_iterator;
  541. using value_type = typename Container::value_type;
  542. using size_type = typename Container::size_type;
  543. using difference_type = typename Container::difference_type;
  544. using const_reference = typename Container::const_reference;
  545. Range(Container const& container)
  546. : Base(container.begin(), container.end())
  547. {
  548. }
  549. const_reference operator[](size_type pos) const
  550. {
  551. return *(this->begin() + pos);
  552. }
  553. const_reference front() const { return *this->begin(); }
  554. const_reference back() const { return *std::prev(this->end()); }
  555. Range& advance(difference_type amount) &
  556. {
  557. Base::advance(amount);
  558. return *this;
  559. }
  560. Range advance(difference_type amount) &&
  561. {
  562. Base::advance(amount);
  563. return std::move(*this);
  564. }
  565. };
  566. using Arguments = Range<std::vector<std::string>>;
  567. bool CheckGenExParameters(cmGeneratorExpressionContext* ctx,
  568. GeneratorExpressionContent const* cnt,
  569. cm::string_view genex, cm::string_view option,
  570. std::size_t count, int required = 1,
  571. bool exactly = true)
  572. {
  573. if (static_cast<int>(count) < required ||
  574. (exactly && static_cast<int>(count) > required)) {
  575. std::string nbParameters;
  576. switch (required) {
  577. case 1:
  578. nbParameters = "one parameter";
  579. break;
  580. case 2:
  581. nbParameters = "two parameters";
  582. break;
  583. case 3:
  584. nbParameters = "three parameters";
  585. break;
  586. case 4:
  587. nbParameters = "four parameters";
  588. break;
  589. default:
  590. nbParameters = cmStrCat(required, " parameters");
  591. }
  592. reportError(ctx, cnt->GetOriginalExpression(),
  593. cmStrCat("$<", genex, ':', option, "> expression requires ",
  594. (exactly ? "exactly" : "at least"), ' ', nbParameters,
  595. '.'));
  596. return false;
  597. }
  598. return true;
  599. };
  600. bool CheckPathParametersEx(cmGeneratorExpressionContext* ctx,
  601. GeneratorExpressionContent const* cnt,
  602. cm::string_view option, std::size_t count,
  603. int required = 1, bool exactly = true)
  604. {
  605. return CheckGenExParameters(ctx, cnt, "PATH"_s, option, count, required,
  606. exactly);
  607. }
  608. bool CheckPathParameters(cmGeneratorExpressionContext* ctx,
  609. GeneratorExpressionContent const* cnt,
  610. cm::string_view option, Arguments args,
  611. int required = 1)
  612. {
  613. return CheckPathParametersEx(ctx, cnt, option, args.size(), required);
  614. };
  615. std::string ToString(bool isTrue)
  616. {
  617. return isTrue ? "1" : "0";
  618. };
  619. }
  620. static const struct PathNode : public cmGeneratorExpressionNode
  621. {
  622. PathNode() {} // NOLINT(modernize-use-equals-default)
  623. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  624. bool AcceptsArbitraryContentParameter() const override { return true; }
  625. std::string Evaluate(
  626. std::vector<std::string> const& parameters,
  627. cmGeneratorExpressionContext* context,
  628. GeneratorExpressionContent const* content,
  629. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  630. {
  631. static auto processList =
  632. [](std::string const& arg,
  633. std::function<void(std::string&)> transform) -> std::string {
  634. cmList list{ arg };
  635. std::for_each(list.begin(), list.end(), std::move(transform));
  636. return list.to_string();
  637. };
  638. static std::unordered_map<
  639. cm::string_view,
  640. std::function<std::string(cmGeneratorExpressionContext*,
  641. GeneratorExpressionContent const*,
  642. Arguments&)>>
  643. pathCommands{
  644. { "GET_ROOT_NAME"_s,
  645. [](cmGeneratorExpressionContext* ctx,
  646. GeneratorExpressionContent const* cnt,
  647. Arguments& args) -> std::string {
  648. if (CheckPathParameters(ctx, cnt, "GET_ROOT_NAME"_s, args) &&
  649. !args.front().empty()) {
  650. return processList(args.front(), [](std::string& value) {
  651. value = cmCMakePath{ value }.GetRootName().String();
  652. });
  653. }
  654. return std::string{};
  655. } },
  656. { "GET_ROOT_DIRECTORY"_s,
  657. [](cmGeneratorExpressionContext* ctx,
  658. GeneratorExpressionContent const* cnt,
  659. Arguments& args) -> std::string {
  660. if (CheckPathParameters(ctx, cnt, "GET_ROOT_DIRECTORY"_s, args) &&
  661. !args.front().empty()) {
  662. return processList(args.front(), [](std::string& value) {
  663. value = cmCMakePath{ value }.GetRootDirectory().String();
  664. });
  665. }
  666. return std::string{};
  667. } },
  668. { "GET_ROOT_PATH"_s,
  669. [](cmGeneratorExpressionContext* ctx,
  670. GeneratorExpressionContent const* cnt,
  671. Arguments& args) -> std::string {
  672. if (CheckPathParameters(ctx, cnt, "GET_ROOT_PATH"_s, args) &&
  673. !args.front().empty()) {
  674. return processList(args.front(), [](std::string& value) {
  675. value = cmCMakePath{ value }.GetRootPath().String();
  676. });
  677. }
  678. return std::string{};
  679. } },
  680. { "GET_FILENAME"_s,
  681. [](cmGeneratorExpressionContext* ctx,
  682. GeneratorExpressionContent const* cnt,
  683. Arguments& args) -> std::string {
  684. if (CheckPathParameters(ctx, cnt, "GET_FILENAME"_s, args) &&
  685. !args.front().empty()) {
  686. return processList(args.front(), [](std::string& value) {
  687. value = cmCMakePath{ value }.GetFileName().String();
  688. });
  689. }
  690. return std::string{};
  691. } },
  692. { "GET_EXTENSION"_s,
  693. [](cmGeneratorExpressionContext* ctx,
  694. GeneratorExpressionContent const* cnt,
  695. Arguments& args) -> std::string {
  696. bool lastOnly = args.front() == "LAST_ONLY"_s;
  697. if (lastOnly) {
  698. args.advance(1);
  699. }
  700. if (CheckPathParametersEx(ctx, cnt,
  701. lastOnly ? "GET_EXTENSION,LAST_ONLY"_s
  702. : "GET_EXTENSION"_s,
  703. args.size())) {
  704. if (args.front().empty()) {
  705. return std::string{};
  706. }
  707. if (lastOnly) {
  708. return processList(args.front(), [](std::string& value) {
  709. value = cmCMakePath{ value }.GetExtension().String();
  710. });
  711. }
  712. return processList(args.front(), [](std::string& value) {
  713. value = cmCMakePath{ value }.GetWideExtension().String();
  714. });
  715. }
  716. return std::string{};
  717. } },
  718. { "GET_STEM"_s,
  719. [](cmGeneratorExpressionContext* ctx,
  720. GeneratorExpressionContent const* cnt,
  721. Arguments& args) -> std::string {
  722. bool lastOnly = args.front() == "LAST_ONLY"_s;
  723. if (lastOnly) {
  724. args.advance(1);
  725. }
  726. if (CheckPathParametersEx(
  727. ctx, cnt, lastOnly ? "GET_STEM,LAST_ONLY"_s : "GET_STEM"_s,
  728. args.size())) {
  729. if (args.front().empty()) {
  730. return std::string{};
  731. }
  732. if (lastOnly) {
  733. return processList(args.front(), [](std::string& value) {
  734. value = cmCMakePath{ value }.GetStem().String();
  735. });
  736. }
  737. return processList(args.front(), [](std::string& value) {
  738. value = cmCMakePath{ value }.GetNarrowStem().String();
  739. });
  740. }
  741. return std::string{};
  742. } },
  743. { "GET_RELATIVE_PART"_s,
  744. [](cmGeneratorExpressionContext* ctx,
  745. GeneratorExpressionContent const* cnt,
  746. Arguments& args) -> std::string {
  747. if (CheckPathParameters(ctx, cnt, "GET_RELATIVE_PART"_s, args) &&
  748. !args.front().empty()) {
  749. return processList(args.front(), [](std::string& value) {
  750. value = cmCMakePath{ value }.GetRelativePath().String();
  751. });
  752. }
  753. return std::string{};
  754. } },
  755. { "GET_PARENT_PATH"_s,
  756. [](cmGeneratorExpressionContext* ctx,
  757. GeneratorExpressionContent const* cnt,
  758. Arguments& args) -> std::string {
  759. if (CheckPathParameters(ctx, cnt, "GET_PARENT_PATH"_s, args)) {
  760. return processList(args.front(), [](std::string& value) {
  761. value = cmCMakePath{ value }.GetParentPath().String();
  762. });
  763. }
  764. return std::string{};
  765. } },
  766. { "HAS_ROOT_NAME"_s,
  767. [](cmGeneratorExpressionContext* ctx,
  768. GeneratorExpressionContent const* cnt,
  769. Arguments& args) -> std::string {
  770. return CheckPathParameters(ctx, cnt, "HAS_ROOT_NAME"_s, args)
  771. ? ToString(cmCMakePath{ args.front() }.HasRootName())
  772. : std::string{ "0" };
  773. } },
  774. { "HAS_ROOT_DIRECTORY"_s,
  775. [](cmGeneratorExpressionContext* ctx,
  776. GeneratorExpressionContent const* cnt,
  777. Arguments& args) -> std::string {
  778. return CheckPathParameters(ctx, cnt, "HAS_ROOT_DIRECTORY"_s, args)
  779. ? ToString(cmCMakePath{ args.front() }.HasRootDirectory())
  780. : std::string{ "0" };
  781. } },
  782. { "HAS_ROOT_PATH"_s,
  783. [](cmGeneratorExpressionContext* ctx,
  784. GeneratorExpressionContent const* cnt,
  785. Arguments& args) -> std::string {
  786. return CheckPathParameters(ctx, cnt, "HAS_ROOT_PATH"_s, args)
  787. ? ToString(cmCMakePath{ args.front() }.HasRootPath())
  788. : std::string{ "0" };
  789. } },
  790. { "HAS_FILENAME"_s,
  791. [](cmGeneratorExpressionContext* ctx,
  792. GeneratorExpressionContent const* cnt,
  793. Arguments& args) -> std::string {
  794. return CheckPathParameters(ctx, cnt, "HAS_FILENAME"_s, args)
  795. ? ToString(cmCMakePath{ args.front() }.HasFileName())
  796. : std::string{ "0" };
  797. } },
  798. { "HAS_EXTENSION"_s,
  799. [](cmGeneratorExpressionContext* ctx,
  800. GeneratorExpressionContent const* cnt,
  801. Arguments& args) -> std::string {
  802. return CheckPathParameters(ctx, cnt, "HAS_EXTENSION"_s, args) &&
  803. !args.front().empty()
  804. ? ToString(cmCMakePath{ args.front() }.HasExtension())
  805. : std::string{ "0" };
  806. } },
  807. { "HAS_STEM"_s,
  808. [](cmGeneratorExpressionContext* ctx,
  809. GeneratorExpressionContent const* cnt,
  810. Arguments& args) -> std::string {
  811. return CheckPathParameters(ctx, cnt, "HAS_STEM"_s, args)
  812. ? ToString(cmCMakePath{ args.front() }.HasStem())
  813. : std::string{ "0" };
  814. } },
  815. { "HAS_RELATIVE_PART"_s,
  816. [](cmGeneratorExpressionContext* ctx,
  817. GeneratorExpressionContent const* cnt,
  818. Arguments& args) -> std::string {
  819. return CheckPathParameters(ctx, cnt, "HAS_RELATIVE_PART"_s, args)
  820. ? ToString(cmCMakePath{ args.front() }.HasRelativePath())
  821. : std::string{ "0" };
  822. } },
  823. { "HAS_PARENT_PATH"_s,
  824. [](cmGeneratorExpressionContext* ctx,
  825. GeneratorExpressionContent const* cnt,
  826. Arguments& args) -> std::string {
  827. return CheckPathParameters(ctx, cnt, "HAS_PARENT_PATH"_s, args)
  828. ? ToString(cmCMakePath{ args.front() }.HasParentPath())
  829. : std::string{ "0" };
  830. } },
  831. { "IS_ABSOLUTE"_s,
  832. [](cmGeneratorExpressionContext* ctx,
  833. GeneratorExpressionContent const* cnt,
  834. Arguments& args) -> std::string {
  835. return CheckPathParameters(ctx, cnt, "IS_ABSOLUTE"_s, args)
  836. ? ToString(cmCMakePath{ args.front() }.IsAbsolute())
  837. : std::string{ "0" };
  838. } },
  839. { "IS_RELATIVE"_s,
  840. [](cmGeneratorExpressionContext* ctx,
  841. GeneratorExpressionContent const* cnt,
  842. Arguments& args) -> std::string {
  843. return CheckPathParameters(ctx, cnt, "IS_RELATIVE"_s, args)
  844. ? ToString(cmCMakePath{ args.front() }.IsRelative())
  845. : std::string{ "0" };
  846. } },
  847. { "IS_PREFIX"_s,
  848. [](cmGeneratorExpressionContext* ctx,
  849. GeneratorExpressionContent const* cnt,
  850. Arguments& args) -> std::string {
  851. bool normalize = args.front() == "NORMALIZE"_s;
  852. if (normalize) {
  853. args.advance(1);
  854. }
  855. if (CheckPathParametersEx(ctx, cnt,
  856. normalize ? "IS_PREFIX,NORMALIZE"_s
  857. : "IS_PREFIX"_s,
  858. args.size(), 2)) {
  859. if (normalize) {
  860. return ToString(cmCMakePath{ args[0] }.Normal().IsPrefix(
  861. cmCMakePath{ args[1] }.Normal()));
  862. }
  863. return ToString(
  864. cmCMakePath{ args[0] }.IsPrefix(cmCMakePath{ args[1] }));
  865. }
  866. return std::string{};
  867. } },
  868. { "CMAKE_PATH"_s,
  869. [](cmGeneratorExpressionContext* ctx,
  870. GeneratorExpressionContent const* cnt,
  871. Arguments& args) -> std::string {
  872. bool normalize = args.front() == "NORMALIZE"_s;
  873. if (normalize) {
  874. args.advance(1);
  875. }
  876. if (CheckPathParametersEx(ctx, cnt,
  877. normalize ? "CMAKE_PATH,NORMALIZE"_s
  878. : "CMAKE_PATH"_s,
  879. args.size(), 1)) {
  880. return processList(
  881. args.front(), [normalize](std::string& value) {
  882. auto path = cmCMakePath{ value, cmCMakePath::auto_format };
  883. value = normalize ? path.Normal().GenericString()
  884. : path.GenericString();
  885. });
  886. }
  887. return std::string{};
  888. } },
  889. { "NATIVE_PATH"_s,
  890. [](cmGeneratorExpressionContext* ctx,
  891. GeneratorExpressionContent const* cnt,
  892. Arguments& args) -> std::string {
  893. bool normalize = args.front() == "NORMALIZE"_s;
  894. if (normalize) {
  895. args.advance(1);
  896. }
  897. if (CheckPathParametersEx(ctx, cnt,
  898. normalize ? "NATIVE_PATH,NORMALIZE"_s
  899. : "NATIVE_PATH"_s,
  900. args.size(), 1)) {
  901. return processList(
  902. args.front(), [normalize](std::string& value) {
  903. auto path = cmCMakePath{ value };
  904. value = normalize ? path.Normal().NativeString()
  905. : path.NativeString();
  906. });
  907. }
  908. return std::string{};
  909. } },
  910. { "APPEND"_s,
  911. [](cmGeneratorExpressionContext* ctx,
  912. GeneratorExpressionContent const* cnt,
  913. Arguments& args) -> std::string {
  914. if (CheckPathParametersEx(ctx, cnt, "APPEND"_s, args.size(), 1,
  915. false)) {
  916. auto const& list = args.front();
  917. args.advance(1);
  918. return processList(list, [&args](std::string& value) {
  919. cmCMakePath path{ value };
  920. for (auto const& p : args) {
  921. path /= p;
  922. }
  923. value = path.String();
  924. });
  925. }
  926. return std::string{};
  927. } },
  928. { "REMOVE_FILENAME"_s,
  929. [](cmGeneratorExpressionContext* ctx,
  930. GeneratorExpressionContent const* cnt,
  931. Arguments& args) -> std::string {
  932. if (CheckPathParameters(ctx, cnt, "REMOVE_FILENAME"_s, args) &&
  933. !args.front().empty()) {
  934. return processList(args.front(), [](std::string& value) {
  935. value = cmCMakePath{ value }.RemoveFileName().String();
  936. });
  937. }
  938. return std::string{};
  939. } },
  940. { "REPLACE_FILENAME"_s,
  941. [](cmGeneratorExpressionContext* ctx,
  942. GeneratorExpressionContent const* cnt,
  943. Arguments& args) -> std::string {
  944. if (CheckPathParameters(ctx, cnt, "REPLACE_FILENAME"_s, args, 2)) {
  945. return processList(args.front(), [&args](std::string& value) {
  946. value = cmCMakePath{ value }
  947. .ReplaceFileName(cmCMakePath{ args[1] })
  948. .String();
  949. });
  950. }
  951. return std::string{};
  952. } },
  953. { "REMOVE_EXTENSION"_s,
  954. [](cmGeneratorExpressionContext* ctx,
  955. GeneratorExpressionContent const* cnt,
  956. Arguments& args) -> std::string {
  957. bool lastOnly = args.front() == "LAST_ONLY"_s;
  958. if (lastOnly) {
  959. args.advance(1);
  960. }
  961. if (CheckPathParametersEx(ctx, cnt,
  962. lastOnly ? "REMOVE_EXTENSION,LAST_ONLY"_s
  963. : "REMOVE_EXTENSION"_s,
  964. args.size())) {
  965. if (args.front().empty()) {
  966. return std::string{};
  967. }
  968. if (lastOnly) {
  969. return processList(args.front(), [](std::string& value) {
  970. value = cmCMakePath{ value }.RemoveExtension().String();
  971. });
  972. }
  973. return processList(args.front(), [](std::string& value) {
  974. value = cmCMakePath{ value }.RemoveWideExtension().String();
  975. });
  976. }
  977. return std::string{};
  978. } },
  979. { "REPLACE_EXTENSION"_s,
  980. [](cmGeneratorExpressionContext* ctx,
  981. GeneratorExpressionContent const* cnt,
  982. Arguments& args) -> std::string {
  983. bool lastOnly = args.front() == "LAST_ONLY"_s;
  984. if (lastOnly) {
  985. args.advance(1);
  986. }
  987. if (CheckPathParametersEx(ctx, cnt,
  988. lastOnly
  989. ? "REPLACE_EXTENSION,LAST_ONLY"_s
  990. : "REPLACE_EXTENSION"_s,
  991. args.size(), 2)) {
  992. if (lastOnly) {
  993. return processList(args.front(), [&args](std::string& value) {
  994. value = cmCMakePath{ value }
  995. .ReplaceExtension(cmCMakePath{ args[1] })
  996. .String();
  997. });
  998. }
  999. return processList(args.front(), [&args](std::string& value) {
  1000. value = cmCMakePath{ value }
  1001. .ReplaceWideExtension(cmCMakePath{ args[1] })
  1002. .String();
  1003. });
  1004. }
  1005. return std::string{};
  1006. } },
  1007. { "NORMAL_PATH"_s,
  1008. [](cmGeneratorExpressionContext* ctx,
  1009. GeneratorExpressionContent const* cnt,
  1010. Arguments& args) -> std::string {
  1011. if (CheckPathParameters(ctx, cnt, "NORMAL_PATH"_s, args) &&
  1012. !args.front().empty()) {
  1013. return processList(args.front(), [](std::string& value) {
  1014. value = cmCMakePath{ value }.Normal().String();
  1015. });
  1016. }
  1017. return std::string{};
  1018. } },
  1019. { "RELATIVE_PATH"_s,
  1020. [](cmGeneratorExpressionContext* ctx,
  1021. GeneratorExpressionContent const* cnt,
  1022. Arguments& args) -> std::string {
  1023. if (CheckPathParameters(ctx, cnt, "RELATIVE_PATH"_s, args, 2)) {
  1024. return processList(args.front(), [&args](std::string& value) {
  1025. value = cmCMakePath{ value }.Relative(args[1]).String();
  1026. });
  1027. }
  1028. return std::string{};
  1029. } },
  1030. { "ABSOLUTE_PATH"_s,
  1031. [](cmGeneratorExpressionContext* ctx,
  1032. GeneratorExpressionContent const* cnt,
  1033. Arguments& args) -> std::string {
  1034. bool normalize = args.front() == "NORMALIZE"_s;
  1035. if (normalize) {
  1036. args.advance(1);
  1037. }
  1038. if (CheckPathParametersEx(ctx, cnt,
  1039. normalize ? "ABSOLUTE_PATH,NORMALIZE"_s
  1040. : "ABSOLUTE_PATH"_s,
  1041. args.size(), 2)) {
  1042. return processList(
  1043. args.front(), [&args, normalize](std::string& value) {
  1044. auto path = cmCMakePath{ value }.Absolute(args[1]);
  1045. value = normalize ? path.Normal().String() : path.String();
  1046. });
  1047. }
  1048. return std::string{};
  1049. } }
  1050. };
  1051. if (cm::contains(pathCommands, parameters.front())) {
  1052. auto args = Arguments{ parameters }.advance(1);
  1053. return pathCommands[parameters.front()](context, content, args);
  1054. }
  1055. reportError(context, content->GetOriginalExpression(),
  1056. cmStrCat(parameters.front(), ": invalid option."));
  1057. return std::string{};
  1058. }
  1059. } pathNode;
  1060. static const struct PathEqualNode : public cmGeneratorExpressionNode
  1061. {
  1062. PathEqualNode() {} // NOLINT(modernize-use-equals-default)
  1063. int NumExpectedParameters() const override { return 2; }
  1064. std::string Evaluate(
  1065. std::vector<std::string> const& parameters,
  1066. cmGeneratorExpressionContext* /*context*/,
  1067. GeneratorExpressionContent const* /*content*/,
  1068. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1069. {
  1070. return cmCMakePath{ parameters[0] } == cmCMakePath{ parameters[1] } ? "1"
  1071. : "0";
  1072. }
  1073. } pathEqualNode;
  1074. namespace {
  1075. inline bool CheckListParametersEx(cmGeneratorExpressionContext* ctx,
  1076. GeneratorExpressionContent const* cnt,
  1077. cm::string_view option, std::size_t count,
  1078. int required = 1, bool exactly = true)
  1079. {
  1080. return CheckGenExParameters(ctx, cnt, "LIST"_s, option, count, required,
  1081. exactly);
  1082. }
  1083. inline bool CheckListParameters(cmGeneratorExpressionContext* ctx,
  1084. GeneratorExpressionContent const* cnt,
  1085. cm::string_view option, Arguments args,
  1086. int required = 1)
  1087. {
  1088. return CheckListParametersEx(ctx, cnt, option, args.size(), required);
  1089. };
  1090. inline cmList GetList(std::string const& list)
  1091. {
  1092. return list.empty() ? cmList{} : cmList{ list, cmList::EmptyElements::Yes };
  1093. }
  1094. bool GetNumericArgument(std::string const& arg, cmList::index_type& value)
  1095. {
  1096. try {
  1097. std::size_t pos;
  1098. if (sizeof(cmList::index_type) == sizeof(long)) {
  1099. value = std::stol(arg, &pos);
  1100. } else {
  1101. value = std::stoll(arg, &pos);
  1102. }
  1103. if (pos != arg.length()) {
  1104. // this is not a number
  1105. return false;
  1106. }
  1107. } catch (std::invalid_argument const&) {
  1108. return false;
  1109. }
  1110. return true;
  1111. }
  1112. bool GetNumericArguments(
  1113. cmGeneratorExpressionContext* ctx, GeneratorExpressionContent const* cnt,
  1114. Arguments args, std::vector<cmList::index_type>& indexes,
  1115. cmList::ExpandElements expandElements = cmList::ExpandElements::No)
  1116. {
  1117. using IndexRange = cmRange<Arguments::const_iterator>;
  1118. IndexRange arguments(args.begin(), args.end());
  1119. cmList list;
  1120. if (expandElements == cmList::ExpandElements::Yes) {
  1121. list = cmList{ args.begin(), args.end(), expandElements };
  1122. arguments = IndexRange{ list.begin(), list.end() };
  1123. }
  1124. for (auto const& value : arguments) {
  1125. cmList::index_type index;
  1126. if (!GetNumericArgument(value, index)) {
  1127. reportError(ctx, cnt->GetOriginalExpression(),
  1128. cmStrCat("index: \"", value, "\" is not a valid index"));
  1129. return false;
  1130. }
  1131. indexes.push_back(index);
  1132. }
  1133. return true;
  1134. }
  1135. }
  1136. static const struct ListNode : public cmGeneratorExpressionNode
  1137. {
  1138. ListNode() {} // NOLINT(modernize-use-equals-default)
  1139. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1140. bool AcceptsArbitraryContentParameter() const override { return true; }
  1141. std::string Evaluate(
  1142. std::vector<std::string> const& parameters,
  1143. cmGeneratorExpressionContext* context,
  1144. GeneratorExpressionContent const* content,
  1145. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1146. {
  1147. static std::unordered_map<
  1148. cm::string_view,
  1149. std::function<std::string(cmGeneratorExpressionContext*,
  1150. GeneratorExpressionContent const*,
  1151. Arguments&)>>
  1152. listCommands{
  1153. { "LENGTH"_s,
  1154. [](cmGeneratorExpressionContext* ctx,
  1155. GeneratorExpressionContent const* cnt,
  1156. Arguments& args) -> std::string {
  1157. if (CheckListParameters(ctx, cnt, "LENGTH"_s, args)) {
  1158. return std::to_string(GetList(args.front()).size());
  1159. }
  1160. return std::string{};
  1161. } },
  1162. { "GET"_s,
  1163. [](cmGeneratorExpressionContext* ctx,
  1164. GeneratorExpressionContent const* cnt,
  1165. Arguments& args) -> std::string {
  1166. if (CheckListParametersEx(ctx, cnt, "GET"_s, args.size(), 2,
  1167. false)) {
  1168. auto list = GetList(args.front());
  1169. if (list.empty()) {
  1170. reportError(ctx, cnt->GetOriginalExpression(),
  1171. "given empty list");
  1172. return std::string{};
  1173. }
  1174. std::vector<cmList::index_type> indexes;
  1175. if (!GetNumericArguments(ctx, cnt, args.advance(1), indexes,
  1176. cmList::ExpandElements::Yes)) {
  1177. return std::string{};
  1178. }
  1179. try {
  1180. return list.get_items(indexes.begin(), indexes.end())
  1181. .to_string();
  1182. } catch (std::out_of_range& e) {
  1183. reportError(ctx, cnt->GetOriginalExpression(), e.what());
  1184. return std::string{};
  1185. }
  1186. }
  1187. return std::string{};
  1188. } },
  1189. { "JOIN"_s,
  1190. [](cmGeneratorExpressionContext* ctx,
  1191. GeneratorExpressionContent const* cnt,
  1192. Arguments& args) -> std::string {
  1193. if (CheckListParameters(ctx, cnt, "JOIN"_s, args, 2)) {
  1194. return GetList(args.front()).join(args[1]);
  1195. }
  1196. return std::string{};
  1197. } },
  1198. { "SUBLIST"_s,
  1199. [](cmGeneratorExpressionContext* ctx,
  1200. GeneratorExpressionContent const* cnt,
  1201. Arguments& args) -> std::string {
  1202. if (CheckListParameters(ctx, cnt, "SUBLIST"_s, args, 3)) {
  1203. auto list = GetList(args.front());
  1204. if (!list.empty()) {
  1205. std::vector<cmList::index_type> indexes;
  1206. if (!GetNumericArguments(ctx, cnt, args.advance(1), indexes)) {
  1207. return std::string{};
  1208. }
  1209. if (indexes[0] < 0) {
  1210. reportError(ctx, cnt->GetOriginalExpression(),
  1211. cmStrCat("begin index: ", indexes[0],
  1212. " is out of range 0 - ",
  1213. list.size() - 1));
  1214. return std::string{};
  1215. }
  1216. if (indexes[1] < -1) {
  1217. reportError(ctx, cnt->GetOriginalExpression(),
  1218. cmStrCat("length: ", indexes[1],
  1219. " should be -1 or greater"));
  1220. return std::string{};
  1221. }
  1222. try {
  1223. return list
  1224. .sublist(static_cast<cmList::size_type>(indexes[0]),
  1225. static_cast<cmList::size_type>(indexes[1]))
  1226. .to_string();
  1227. } catch (std::out_of_range& e) {
  1228. reportError(ctx, cnt->GetOriginalExpression(), e.what());
  1229. return std::string{};
  1230. }
  1231. }
  1232. }
  1233. return std::string{};
  1234. } },
  1235. { "FIND"_s,
  1236. [](cmGeneratorExpressionContext* ctx,
  1237. GeneratorExpressionContent const* cnt,
  1238. Arguments& args) -> std::string {
  1239. if (CheckListParameters(ctx, cnt, "FIND"_s, args, 2)) {
  1240. auto list = GetList(args.front());
  1241. auto index = list.find(args[1]);
  1242. return index == cmList::npos ? "-1" : std::to_string(index);
  1243. }
  1244. return std::string{};
  1245. } },
  1246. { "APPEND"_s,
  1247. [](cmGeneratorExpressionContext* ctx,
  1248. GeneratorExpressionContent const* cnt,
  1249. Arguments& args) -> std::string {
  1250. if (CheckListParametersEx(ctx, cnt, "APPEND"_s, args.size(), 2,
  1251. false)) {
  1252. auto list = args.front();
  1253. args.advance(1);
  1254. return cmList::append(list, args.begin(), args.end());
  1255. }
  1256. return std::string{};
  1257. } },
  1258. { "PREPEND"_s,
  1259. [](cmGeneratorExpressionContext* ctx,
  1260. GeneratorExpressionContent const* cnt,
  1261. Arguments& args) -> std::string {
  1262. if (CheckListParametersEx(ctx, cnt, "PREPEND"_s, args.size(), 2,
  1263. false)) {
  1264. auto list = args.front();
  1265. args.advance(1);
  1266. return cmList::prepend(list, args.begin(), args.end());
  1267. }
  1268. return std::string{};
  1269. } },
  1270. { "INSERT"_s,
  1271. [](cmGeneratorExpressionContext* ctx,
  1272. GeneratorExpressionContent const* cnt,
  1273. Arguments& args) -> std::string {
  1274. if (CheckListParametersEx(ctx, cnt, "INSERT"_s, args.size(), 3,
  1275. false)) {
  1276. cmList::index_type index;
  1277. if (!GetNumericArgument(args[1], index)) {
  1278. reportError(
  1279. ctx, cnt->GetOriginalExpression(),
  1280. cmStrCat("index: \"", args[1], "\" is not a valid index"));
  1281. return std::string{};
  1282. }
  1283. try {
  1284. auto list = GetList(args.front());
  1285. args.advance(2);
  1286. list.insert_items(index, args.begin(), args.end(),
  1287. cmList::ExpandElements::No,
  1288. cmList::EmptyElements::Yes);
  1289. return list.to_string();
  1290. } catch (std::out_of_range& e) {
  1291. reportError(ctx, cnt->GetOriginalExpression(), e.what());
  1292. return std::string{};
  1293. }
  1294. }
  1295. return std::string{};
  1296. } },
  1297. { "POP_BACK"_s,
  1298. [](cmGeneratorExpressionContext* ctx,
  1299. GeneratorExpressionContent const* cnt,
  1300. Arguments& args) -> std::string {
  1301. if (CheckListParameters(ctx, cnt, "POP_BACK"_s, args)) {
  1302. auto list = GetList(args.front());
  1303. if (!list.empty()) {
  1304. list.pop_back();
  1305. return list.to_string();
  1306. }
  1307. }
  1308. return std::string{};
  1309. } },
  1310. { "POP_FRONT"_s,
  1311. [](cmGeneratorExpressionContext* ctx,
  1312. GeneratorExpressionContent const* cnt,
  1313. Arguments& args) -> std::string {
  1314. if (CheckListParameters(ctx, cnt, "POP_FRONT"_s, args)) {
  1315. auto list = GetList(args.front());
  1316. if (!list.empty()) {
  1317. list.pop_front();
  1318. return list.to_string();
  1319. }
  1320. }
  1321. return std::string{};
  1322. } },
  1323. { "REMOVE_DUPLICATES"_s,
  1324. [](cmGeneratorExpressionContext* ctx,
  1325. GeneratorExpressionContent const* cnt,
  1326. Arguments& args) -> std::string {
  1327. if (CheckListParameters(ctx, cnt, "REMOVE_DUPLICATES"_s, args)) {
  1328. return GetList(args.front()).remove_duplicates().to_string();
  1329. }
  1330. return std::string{};
  1331. } },
  1332. { "REMOVE_ITEM"_s,
  1333. [](cmGeneratorExpressionContext* ctx,
  1334. GeneratorExpressionContent const* cnt,
  1335. Arguments& args) -> std::string {
  1336. if (CheckListParametersEx(ctx, cnt, "REMOVE_ITEM"_s, args.size(),
  1337. 2, false)) {
  1338. auto list = GetList(args.front());
  1339. args.advance(1);
  1340. cmList items{ args.begin(), args.end(),
  1341. cmList::ExpandElements::Yes };
  1342. return list.remove_items(items.begin(), items.end()).to_string();
  1343. }
  1344. return std::string{};
  1345. } },
  1346. { "REMOVE_AT"_s,
  1347. [](cmGeneratorExpressionContext* ctx,
  1348. GeneratorExpressionContent const* cnt,
  1349. Arguments& args) -> std::string {
  1350. if (CheckListParametersEx(ctx, cnt, "REMOVE_AT"_s, args.size(), 2,
  1351. false)) {
  1352. auto list = GetList(args.front());
  1353. std::vector<cmList::index_type> indexes;
  1354. if (!GetNumericArguments(ctx, cnt, args.advance(1), indexes,
  1355. cmList::ExpandElements::Yes)) {
  1356. return std::string{};
  1357. }
  1358. try {
  1359. return list.remove_items(indexes.begin(), indexes.end())
  1360. .to_string();
  1361. } catch (std::out_of_range& e) {
  1362. reportError(ctx, cnt->GetOriginalExpression(), e.what());
  1363. return std::string{};
  1364. }
  1365. }
  1366. return std::string{};
  1367. } },
  1368. { "FILTER"_s,
  1369. [](cmGeneratorExpressionContext* ctx,
  1370. GeneratorExpressionContent const* cnt,
  1371. Arguments& args) -> std::string {
  1372. if (CheckListParameters(ctx, cnt, "FILTER"_s, args, 3)) {
  1373. auto const& op = args[1];
  1374. if (op != "INCLUDE"_s && op != "EXCLUDE"_s) {
  1375. reportError(
  1376. ctx, cnt->GetOriginalExpression(),
  1377. cmStrCat("sub-command FILTER does not recognize operator \"",
  1378. op, "\". It must be either INCLUDE or EXCLUDE."));
  1379. return std::string{};
  1380. }
  1381. try {
  1382. return GetList(args.front())
  1383. .filter(args[2],
  1384. op == "INCLUDE"_s ? cmList::FilterMode::INCLUDE
  1385. : cmList::FilterMode::EXCLUDE)
  1386. .to_string();
  1387. } catch (std::invalid_argument&) {
  1388. reportError(
  1389. ctx, cnt->GetOriginalExpression(),
  1390. cmStrCat("sub-command FILTER, failed to compile regex \"",
  1391. args[2], "\"."));
  1392. return std::string{};
  1393. }
  1394. }
  1395. return std::string{};
  1396. } },
  1397. { "TRANSFORM"_s,
  1398. [](cmGeneratorExpressionContext* ctx,
  1399. GeneratorExpressionContent const* cnt,
  1400. Arguments& args) -> std::string {
  1401. if (CheckListParametersEx(ctx, cnt, "TRANSFORM"_s, args.size(), 2,
  1402. false)) {
  1403. auto list = GetList(args.front());
  1404. if (!list.empty()) {
  1405. struct ActionDescriptor
  1406. {
  1407. ActionDescriptor(std::string name)
  1408. : Name(std::move(name))
  1409. {
  1410. }
  1411. ActionDescriptor(std::string name,
  1412. cmList::TransformAction action, int arity)
  1413. : Name(std::move(name))
  1414. , Action(action)
  1415. , Arity(arity)
  1416. {
  1417. }
  1418. operator std::string const&() const { return this->Name; }
  1419. std::string Name;
  1420. cmList::TransformAction Action;
  1421. int Arity = 0;
  1422. };
  1423. static std::set<
  1424. ActionDescriptor,
  1425. std::function<bool(std::string const&, std::string const&)>>
  1426. descriptors{
  1427. { { "APPEND", cmList::TransformAction::APPEND, 1 },
  1428. { "PREPEND", cmList::TransformAction::PREPEND, 1 },
  1429. { "TOUPPER", cmList::TransformAction::TOUPPER, 0 },
  1430. { "TOLOWER", cmList::TransformAction::TOLOWER, 0 },
  1431. { "STRIP", cmList::TransformAction::STRIP, 0 },
  1432. { "REPLACE", cmList::TransformAction::REPLACE, 2 } },
  1433. [](std::string const& x, std::string const& y) {
  1434. return x < y;
  1435. }
  1436. };
  1437. auto descriptor = descriptors.find(args.advance(1).front());
  1438. if (descriptor == descriptors.end()) {
  1439. reportError(ctx, cnt->GetOriginalExpression(),
  1440. cmStrCat(" sub-command TRANSFORM, ",
  1441. args.front(), " invalid action."));
  1442. return std::string{};
  1443. }
  1444. // Action arguments
  1445. args.advance(1);
  1446. if (args.size() < descriptor->Arity) {
  1447. reportError(ctx, cnt->GetOriginalExpression(),
  1448. cmStrCat("sub-command TRANSFORM, action ",
  1449. descriptor->Name, " expects ",
  1450. descriptor->Arity, " argument(s)."));
  1451. return std::string{};
  1452. }
  1453. std::vector<std::string> arguments;
  1454. if (descriptor->Arity > 0) {
  1455. arguments = std::vector<std::string>(
  1456. args.begin(), args.begin() + descriptor->Arity);
  1457. args.advance(descriptor->Arity);
  1458. }
  1459. std::string const REGEX{ "REGEX" };
  1460. std::string const AT{ "AT" };
  1461. std::string const FOR{ "FOR" };
  1462. std::unique_ptr<cmList::TransformSelector> selector;
  1463. try {
  1464. // handle optional arguments
  1465. while (!args.empty()) {
  1466. if ((args.front() == REGEX || args.front() == AT ||
  1467. args.front() == FOR) &&
  1468. selector) {
  1469. reportError(ctx, cnt->GetOriginalExpression(),
  1470. cmStrCat("sub-command TRANSFORM, selector "
  1471. "already specified (",
  1472. selector->GetTag(), ")."));
  1473. return std::string{};
  1474. }
  1475. // REGEX selector
  1476. if (args.front() == REGEX) {
  1477. if (args.advance(1).empty()) {
  1478. reportError(
  1479. ctx, cnt->GetOriginalExpression(),
  1480. "sub-command TRANSFORM, selector REGEX expects "
  1481. "'regular expression' argument.");
  1482. return std::string{};
  1483. }
  1484. selector = cmList::TransformSelector::New<
  1485. cmList::TransformSelector::REGEX>(args.front());
  1486. args.advance(1);
  1487. continue;
  1488. }
  1489. // AT selector
  1490. if (args.front() == AT) {
  1491. args.advance(1);
  1492. // get all specified indexes
  1493. std::vector<cmList::index_type> indexes;
  1494. while (!args.empty()) {
  1495. cmList indexList{ args.front() };
  1496. for (auto const& index : indexList) {
  1497. cmList::index_type value;
  1498. if (!GetNumericArgument(index, value)) {
  1499. // this is not a number, stop processing
  1500. reportError(
  1501. ctx, cnt->GetOriginalExpression(),
  1502. cmStrCat("sub-command TRANSFORM, selector AT: '",
  1503. index, "': unexpected argument."));
  1504. return std::string{};
  1505. }
  1506. indexes.push_back(value);
  1507. }
  1508. args.advance(1);
  1509. }
  1510. if (indexes.empty()) {
  1511. reportError(ctx, cnt->GetOriginalExpression(),
  1512. "sub-command TRANSFORM, selector AT "
  1513. "expects at least one "
  1514. "numeric value.");
  1515. return std::string{};
  1516. }
  1517. selector = cmList::TransformSelector::New<
  1518. cmList::TransformSelector::AT>(std::move(indexes));
  1519. continue;
  1520. }
  1521. // FOR selector
  1522. if (args.front() == FOR) {
  1523. if (args.advance(1).size() < 2) {
  1524. reportError(ctx, cnt->GetOriginalExpression(),
  1525. "sub-command TRANSFORM, selector FOR "
  1526. "expects, at least,"
  1527. " two arguments.");
  1528. return std::string{};
  1529. }
  1530. cmList::index_type start = 0;
  1531. cmList::index_type stop = 0;
  1532. cmList::index_type step = 1;
  1533. bool valid = false;
  1534. if (GetNumericArgument(args.front(), start) &&
  1535. GetNumericArgument(args.advance(1).front(), stop)) {
  1536. valid = true;
  1537. }
  1538. if (!valid) {
  1539. reportError(
  1540. ctx, cnt->GetOriginalExpression(),
  1541. "sub-command TRANSFORM, selector FOR expects, "
  1542. "at least, two numeric values.");
  1543. return std::string{};
  1544. }
  1545. // try to read a third numeric value for step
  1546. if (!args.advance(1).empty()) {
  1547. if (!GetNumericArgument(args.front(), step)) {
  1548. // this is not a number
  1549. step = -1;
  1550. }
  1551. args.advance(1);
  1552. }
  1553. if (step <= 0) {
  1554. reportError(
  1555. ctx, cnt->GetOriginalExpression(),
  1556. "sub-command TRANSFORM, selector FOR expects "
  1557. "positive numeric value for <step>.");
  1558. return std::string{};
  1559. }
  1560. selector = cmList::TransformSelector::New<
  1561. cmList::TransformSelector::FOR>({ start, stop, step });
  1562. continue;
  1563. }
  1564. reportError(ctx, cnt->GetOriginalExpression(),
  1565. cmStrCat("sub-command TRANSFORM, '",
  1566. cmJoin(args, ", "),
  1567. "': unexpected argument(s)."));
  1568. return std::string{};
  1569. }
  1570. if (!selector) {
  1571. selector = cmList::TransformSelector::New();
  1572. }
  1573. selector->Makefile = ctx->LG->GetMakefile();
  1574. return list
  1575. .transform(descriptor->Action, arguments,
  1576. std::move(selector))
  1577. .to_string();
  1578. } catch (cmList::transform_error& e) {
  1579. reportError(ctx, cnt->GetOriginalExpression(), e.what());
  1580. return std::string{};
  1581. }
  1582. }
  1583. }
  1584. return std::string{};
  1585. } },
  1586. { "REVERSE"_s,
  1587. [](cmGeneratorExpressionContext* ctx,
  1588. GeneratorExpressionContent const* cnt,
  1589. Arguments& args) -> std::string {
  1590. if (CheckListParameters(ctx, cnt, "REVERSE"_s, args)) {
  1591. return GetList(args.front()).reverse().to_string();
  1592. }
  1593. return std::string{};
  1594. } },
  1595. { "SORT"_s,
  1596. [](cmGeneratorExpressionContext* ctx,
  1597. GeneratorExpressionContent const* cnt,
  1598. Arguments& args) -> std::string {
  1599. if (CheckListParametersEx(ctx, cnt, "SORT"_s, args.size(), 1,
  1600. false)) {
  1601. auto list = GetList(args.front());
  1602. args.advance(1);
  1603. auto const COMPARE = "COMPARE:"_s;
  1604. auto const CASE = "CASE:"_s;
  1605. auto const ORDER = "ORDER:"_s;
  1606. using SortConfig = cmList::SortConfiguration;
  1607. SortConfig sortConfig;
  1608. for (auto const& arg : args) {
  1609. if (cmHasPrefix(arg, COMPARE)) {
  1610. if (sortConfig.Compare !=
  1611. SortConfig::CompareMethod::DEFAULT) {
  1612. reportError(ctx, cnt->GetOriginalExpression(),
  1613. "sub-command SORT, COMPARE option has been "
  1614. "specified multiple times.");
  1615. return std::string{};
  1616. }
  1617. auto option =
  1618. cm::string_view{ arg.c_str() + COMPARE.length() };
  1619. if (option == "STRING"_s) {
  1620. sortConfig.Compare = SortConfig::CompareMethod::STRING;
  1621. continue;
  1622. }
  1623. if (option == "FILE_BASENAME"_s) {
  1624. sortConfig.Compare =
  1625. SortConfig::CompareMethod::FILE_BASENAME;
  1626. continue;
  1627. }
  1628. if (option == "NATURAL"_s) {
  1629. sortConfig.Compare = SortConfig::CompareMethod::NATURAL;
  1630. continue;
  1631. }
  1632. reportError(
  1633. ctx, cnt->GetOriginalExpression(),
  1634. cmStrCat(
  1635. "sub-command SORT, an invalid COMPARE option has been "
  1636. "specified: \"",
  1637. option, "\"."));
  1638. return std::string{};
  1639. }
  1640. if (cmHasPrefix(arg, CASE)) {
  1641. if (sortConfig.Case !=
  1642. SortConfig::CaseSensitivity::DEFAULT) {
  1643. reportError(ctx, cnt->GetOriginalExpression(),
  1644. "sub-command SORT, CASE option has been "
  1645. "specified multiple times.");
  1646. return std::string{};
  1647. }
  1648. auto option = cm::string_view{ arg.c_str() + CASE.length() };
  1649. if (option == "SENSITIVE"_s) {
  1650. sortConfig.Case = SortConfig::CaseSensitivity::SENSITIVE;
  1651. continue;
  1652. }
  1653. if (option == "INSENSITIVE"_s) {
  1654. sortConfig.Case = SortConfig::CaseSensitivity::INSENSITIVE;
  1655. continue;
  1656. }
  1657. reportError(
  1658. ctx, cnt->GetOriginalExpression(),
  1659. cmStrCat(
  1660. "sub-command SORT, an invalid CASE option has been "
  1661. "specified: \"",
  1662. option, "\"."));
  1663. return std::string{};
  1664. }
  1665. if (cmHasPrefix(arg, ORDER)) {
  1666. if (sortConfig.Order != SortConfig::OrderMode::DEFAULT) {
  1667. reportError(ctx, cnt->GetOriginalExpression(),
  1668. "sub-command SORT, ORDER option has been "
  1669. "specified multiple times.");
  1670. return std::string{};
  1671. }
  1672. auto option =
  1673. cm::string_view{ arg.c_str() + ORDER.length() };
  1674. if (option == "ASCENDING"_s) {
  1675. sortConfig.Order = SortConfig::OrderMode::ASCENDING;
  1676. continue;
  1677. }
  1678. if (option == "DESCENDING"_s) {
  1679. sortConfig.Order = SortConfig::OrderMode::DESCENDING;
  1680. continue;
  1681. }
  1682. reportError(
  1683. ctx, cnt->GetOriginalExpression(),
  1684. cmStrCat(
  1685. "sub-command SORT, an invalid ORDER option has been "
  1686. "specified: \"",
  1687. option, "\"."));
  1688. return std::string{};
  1689. }
  1690. reportError(ctx, cnt->GetOriginalExpression(),
  1691. cmStrCat("sub-command SORT, option \"", arg,
  1692. "\" is invalid."));
  1693. return std::string{};
  1694. }
  1695. return list.sort(sortConfig).to_string();
  1696. }
  1697. return std::string{};
  1698. } }
  1699. };
  1700. if (cm::contains(listCommands, parameters.front())) {
  1701. auto args = Arguments{ parameters }.advance(1);
  1702. return listCommands[parameters.front()](context, content, args);
  1703. }
  1704. reportError(context, content->GetOriginalExpression(),
  1705. cmStrCat(parameters.front(), ": invalid option."));
  1706. return std::string{};
  1707. }
  1708. } listNode;
  1709. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  1710. {
  1711. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  1712. bool AcceptsArbitraryContentParameter() const override { return true; }
  1713. std::string Evaluate(
  1714. std::vector<std::string> const& parameters,
  1715. cmGeneratorExpressionContext* /*context*/,
  1716. GeneratorExpressionContent const* /*content*/,
  1717. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1718. {
  1719. return cmSystemTools::MakeCidentifier(parameters.front());
  1720. }
  1721. } makeCIdentifierNode;
  1722. template <char C>
  1723. struct CharacterNode : public cmGeneratorExpressionNode
  1724. {
  1725. CharacterNode() {} // NOLINT(modernize-use-equals-default)
  1726. int NumExpectedParameters() const override { return 0; }
  1727. std::string Evaluate(
  1728. std::vector<std::string> const& /*parameters*/,
  1729. cmGeneratorExpressionContext* /*context*/,
  1730. GeneratorExpressionContent const* /*content*/,
  1731. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1732. {
  1733. return { C };
  1734. }
  1735. };
  1736. static CharacterNode<'>'> const angle_rNode;
  1737. static CharacterNode<','> const commaNode;
  1738. static CharacterNode<';'> const semicolonNode;
  1739. static CharacterNode<'"'> const quoteNode;
  1740. struct CompilerIdNode : public cmGeneratorExpressionNode
  1741. {
  1742. CompilerIdNode(char const* compilerLang)
  1743. : CompilerLanguage(compilerLang)
  1744. {
  1745. }
  1746. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1747. std::string Evaluate(
  1748. std::vector<std::string> const& parameters,
  1749. cmGeneratorExpressionContext* context,
  1750. GeneratorExpressionContent const* content,
  1751. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1752. {
  1753. if (!context->HeadTarget) {
  1754. std::ostringstream e;
  1755. e << "$<" << this->CompilerLanguage
  1756. << "_COMPILER_ID> may only be used with binary targets. It may "
  1757. "not be used with add_custom_command or add_custom_target.";
  1758. reportError(context, content->GetOriginalExpression(), e.str());
  1759. return {};
  1760. }
  1761. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1762. this->CompilerLanguage);
  1763. }
  1764. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  1765. cmGeneratorExpressionContext* context,
  1766. GeneratorExpressionContent const* content,
  1767. cmGeneratorExpressionDAGChecker* /*unused*/,
  1768. std::string const& lang) const
  1769. {
  1770. std::string const& compilerId =
  1771. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1772. "_COMPILER_ID");
  1773. if (parameters.empty()) {
  1774. return compilerId;
  1775. }
  1776. if (compilerId.empty()) {
  1777. return parameters.front().empty() ? "1" : "0";
  1778. }
  1779. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  1780. for (auto const& param : parameters) {
  1781. if (!compilerIdValidator.find(param)) {
  1782. reportError(context, content->GetOriginalExpression(),
  1783. "Expression syntax not recognized.");
  1784. return std::string();
  1785. }
  1786. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  1787. return "1";
  1788. }
  1789. }
  1790. return "0";
  1791. }
  1792. char const* const CompilerLanguage;
  1793. };
  1794. static CompilerIdNode const cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  1795. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  1796. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  1797. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  1798. struct CompilerVersionNode : public cmGeneratorExpressionNode
  1799. {
  1800. CompilerVersionNode(char const* compilerLang)
  1801. : CompilerLanguage(compilerLang)
  1802. {
  1803. }
  1804. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  1805. std::string Evaluate(
  1806. std::vector<std::string> const& parameters,
  1807. cmGeneratorExpressionContext* context,
  1808. GeneratorExpressionContent const* content,
  1809. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1810. {
  1811. if (!context->HeadTarget) {
  1812. std::ostringstream e;
  1813. e << "$<" << this->CompilerLanguage
  1814. << "_COMPILER_VERSION> may only be used with binary targets. It "
  1815. "may not be used with add_custom_command or add_custom_target.";
  1816. reportError(context, content->GetOriginalExpression(), e.str());
  1817. return {};
  1818. }
  1819. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1820. this->CompilerLanguage);
  1821. }
  1822. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  1823. cmGeneratorExpressionContext* context,
  1824. GeneratorExpressionContent const* content,
  1825. cmGeneratorExpressionDAGChecker* /*unused*/,
  1826. std::string const& lang) const
  1827. {
  1828. std::string const& compilerVersion =
  1829. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1830. "_COMPILER_VERSION");
  1831. if (parameters.empty()) {
  1832. return compilerVersion;
  1833. }
  1834. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  1835. if (!compilerIdValidator.find(parameters.front())) {
  1836. reportError(context, content->GetOriginalExpression(),
  1837. "Expression syntax not recognized.");
  1838. return {};
  1839. }
  1840. if (compilerVersion.empty()) {
  1841. return parameters.front().empty() ? "1" : "0";
  1842. }
  1843. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  1844. parameters.front(), compilerVersion)
  1845. ? "1"
  1846. : "0";
  1847. }
  1848. char const* const CompilerLanguage;
  1849. };
  1850. static CompilerVersionNode const cCompilerVersionNode("C"),
  1851. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  1852. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  1853. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  1854. hipCompilerVersionNode("HIP");
  1855. struct CompilerFrontendVariantNode : public cmGeneratorExpressionNode
  1856. {
  1857. CompilerFrontendVariantNode(char const* compilerLang)
  1858. : CompilerLanguage(compilerLang)
  1859. {
  1860. }
  1861. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1862. std::string Evaluate(
  1863. std::vector<std::string> const& parameters,
  1864. cmGeneratorExpressionContext* context,
  1865. GeneratorExpressionContent const* content,
  1866. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1867. {
  1868. if (!context->HeadTarget) {
  1869. std::ostringstream e;
  1870. e << "$<" << this->CompilerLanguage
  1871. << "_COMPILER_FRONTEND_VARIANT> may only be used with binary targets. "
  1872. " It may not be used with add_custom_command or add_custom_target.";
  1873. reportError(context, content->GetOriginalExpression(), e.str());
  1874. return {};
  1875. }
  1876. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1877. this->CompilerLanguage);
  1878. }
  1879. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  1880. cmGeneratorExpressionContext* context,
  1881. GeneratorExpressionContent const* content,
  1882. cmGeneratorExpressionDAGChecker* /*unused*/,
  1883. std::string const& lang) const
  1884. {
  1885. std::string const& compilerFrontendVariant =
  1886. context->LG->GetMakefile()->GetSafeDefinition(
  1887. "CMAKE_" + lang + "_COMPILER_FRONTEND_VARIANT");
  1888. if (parameters.empty()) {
  1889. return compilerFrontendVariant;
  1890. }
  1891. if (compilerFrontendVariant.empty()) {
  1892. return parameters.front().empty() ? "1" : "0";
  1893. }
  1894. static cmsys::RegularExpression compilerFrontendVariantValidator(
  1895. "^[A-Za-z0-9_]*$");
  1896. for (auto const& param : parameters) {
  1897. if (!compilerFrontendVariantValidator.find(param)) {
  1898. reportError(context, content->GetOriginalExpression(),
  1899. "Expression syntax not recognized.");
  1900. return {};
  1901. }
  1902. if (strcmp(param.c_str(), compilerFrontendVariant.c_str()) == 0) {
  1903. return "1";
  1904. }
  1905. }
  1906. return "0";
  1907. }
  1908. char const* const CompilerLanguage;
  1909. };
  1910. static CompilerFrontendVariantNode const cCompilerFrontendVariantNode("C"),
  1911. cxxCompilerFrontendVariantNode("CXX"),
  1912. cudaCompilerFrontendVariantNode("CUDA"),
  1913. objcCompilerFrontendVariantNode("OBJC"),
  1914. objcxxCompilerFrontendVariantNode("OBJCXX"),
  1915. fortranCompilerFrontendVariantNode("Fortran"),
  1916. hipCompilerFrontendVariantNode("HIP"),
  1917. ispcCompilerFrontendVariantNode("ISPC");
  1918. struct PlatformIdNode : public cmGeneratorExpressionNode
  1919. {
  1920. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  1921. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1922. std::string Evaluate(
  1923. std::vector<std::string> const& parameters,
  1924. cmGeneratorExpressionContext* context,
  1925. GeneratorExpressionContent const* /*content*/,
  1926. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1927. {
  1928. std::string const& platformId =
  1929. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  1930. if (parameters.empty()) {
  1931. return platformId;
  1932. }
  1933. if (platformId.empty()) {
  1934. return parameters.front().empty() ? "1" : "0";
  1935. }
  1936. for (auto const& param : parameters) {
  1937. if (param == platformId) {
  1938. return "1";
  1939. }
  1940. }
  1941. return "0";
  1942. }
  1943. };
  1944. static struct PlatformIdNode platformIdNode;
  1945. template <cmSystemTools::CompareOp Op>
  1946. struct VersionNode : public cmGeneratorExpressionNode
  1947. {
  1948. VersionNode() {} // NOLINT(modernize-use-equals-default)
  1949. int NumExpectedParameters() const override { return 2; }
  1950. std::string Evaluate(
  1951. std::vector<std::string> const& parameters,
  1952. cmGeneratorExpressionContext* /*context*/,
  1953. GeneratorExpressionContent const* /*content*/,
  1954. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1955. {
  1956. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  1957. ? "1"
  1958. : "0";
  1959. }
  1960. };
  1961. static VersionNode<cmSystemTools::OP_GREATER> const versionGreaterNode;
  1962. static VersionNode<cmSystemTools::OP_GREATER_EQUAL> const versionGreaterEqNode;
  1963. static VersionNode<cmSystemTools::OP_LESS> const versionLessNode;
  1964. static VersionNode<cmSystemTools::OP_LESS_EQUAL> const versionLessEqNode;
  1965. static VersionNode<cmSystemTools::OP_EQUAL> const versionEqualNode;
  1966. static const struct CompileOnlyNode : public cmGeneratorExpressionNode
  1967. {
  1968. CompileOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1969. std::string Evaluate(
  1970. std::vector<std::string> const& parameters,
  1971. cmGeneratorExpressionContext* context,
  1972. GeneratorExpressionContent const* content,
  1973. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1974. {
  1975. if (!dagChecker) {
  1976. reportError(context, content->GetOriginalExpression(),
  1977. "$<COMPILE_ONLY:...> may only be used for linking");
  1978. return std::string();
  1979. }
  1980. if (dagChecker->GetTransitivePropertiesOnly()) {
  1981. return parameters.front();
  1982. }
  1983. return std::string{};
  1984. }
  1985. } compileOnlyNode;
  1986. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  1987. {
  1988. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1989. std::string Evaluate(
  1990. std::vector<std::string> const& parameters,
  1991. cmGeneratorExpressionContext* context,
  1992. GeneratorExpressionContent const* content,
  1993. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1994. {
  1995. if (!dagChecker) {
  1996. reportError(context, content->GetOriginalExpression(),
  1997. "$<LINK_ONLY:...> may only be used for linking");
  1998. return std::string();
  1999. }
  2000. if (!dagChecker->GetTransitivePropertiesOnlyCMP0131()) {
  2001. return parameters.front();
  2002. }
  2003. return std::string();
  2004. }
  2005. } linkOnlyNode;
  2006. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  2007. {
  2008. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  2009. int NumExpectedParameters() const override { return 0; }
  2010. std::string Evaluate(
  2011. std::vector<std::string> const& /*parameters*/,
  2012. cmGeneratorExpressionContext* context,
  2013. GeneratorExpressionContent const* /*content*/,
  2014. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2015. {
  2016. context->HadContextSensitiveCondition = true;
  2017. return context->Config;
  2018. }
  2019. } configurationNode;
  2020. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  2021. {
  2022. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  2023. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2024. std::string Evaluate(
  2025. std::vector<std::string> const& parameters,
  2026. cmGeneratorExpressionContext* context,
  2027. GeneratorExpressionContent const* content,
  2028. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2029. {
  2030. if (parameters.empty()) {
  2031. return configurationNode.Evaluate(parameters, context, content, nullptr);
  2032. }
  2033. context->HadContextSensitiveCondition = true;
  2034. // First, validate our arguments.
  2035. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  2036. bool firstParam = true;
  2037. for (auto const& param : parameters) {
  2038. if (!configValidator.find(param)) {
  2039. if (firstParam) {
  2040. reportError(context, content->GetOriginalExpression(),
  2041. "Expression syntax not recognized.");
  2042. return std::string();
  2043. }
  2044. // for backwards compat invalid config names are only errors as
  2045. // the first parameter
  2046. std::ostringstream e;
  2047. /* clang-format off */
  2048. e << "Warning evaluating generator expression:\n"
  2049. << " " << content->GetOriginalExpression() << "\n"
  2050. << "The config name of \"" << param << "\" is invalid";
  2051. /* clang-format on */
  2052. context->LG->GetCMakeInstance()->IssueMessage(
  2053. MessageType::WARNING, e.str(), context->Backtrace);
  2054. }
  2055. firstParam = false;
  2056. }
  2057. // Determine the context(s) in which the expression should be evaluated. If
  2058. // CMPxxxx is NEW, the context is exactly one of the imported target's
  2059. // selected configuration, if applicable, or the consuming target's
  2060. // configuration, otherwise.
  2061. //
  2062. // If CMPxxxx is OLD, we evaluate first in the context of the consuming
  2063. // target, then, if the consumed target is imported, we evaluate based on
  2064. // the mapped configurations (this logic is... problematic; see comment
  2065. // below), then finally based on the selected configuration of the imported
  2066. // target.
  2067. bool const targetIsImported =
  2068. (context->CurrentTarget && context->CurrentTarget->IsImported());
  2069. bool const oldPolicy = [&] {
  2070. if (!targetIsImported) {
  2071. // For non-imported targets, there is no behavior difference between
  2072. // the OLD and NEW policy.
  2073. return false;
  2074. }
  2075. cmTarget const* const t = context->CurrentTarget->Target;
  2076. if (t->GetOrigin() == cmTarget::Origin::Cps) {
  2077. // Generator expressions appearing on targets imported from CPS should
  2078. // always be evaluated according to the selected configuration of the
  2079. // imported target, i.e. the NEW policy.
  2080. return false;
  2081. }
  2082. switch (context->HeadTarget->GetPolicyStatusCMP0199()) {
  2083. case cmPolicies::WARN:
  2084. if (context->LG->GetMakefile()->PolicyOptionalWarningEnabled(
  2085. "CMAKE_POLICY_WARNING_CMP0199")) {
  2086. std::string const err =
  2087. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0199),
  2088. "\nEvaluation of $<CONFIG> for imported target \"",
  2089. context->CurrentTarget->GetName(), "\", used by \"",
  2090. context->HeadTarget->GetName(),
  2091. "\", may match multiple configurations.\n");
  2092. context->LG->GetCMakeInstance()->IssueMessage(
  2093. MessageType ::AUTHOR_WARNING, err, context->Backtrace);
  2094. }
  2095. CM_FALLTHROUGH;
  2096. case cmPolicies::OLD:
  2097. return true;
  2098. case cmPolicies::NEW:
  2099. return false;
  2100. }
  2101. // Should be unreachable
  2102. assert(false);
  2103. return false;
  2104. }();
  2105. if (!targetIsImported || oldPolicy) {
  2106. // Does the consuming target's configuration match any of the arguments?
  2107. for (auto const& param : parameters) {
  2108. if (context->Config.empty()) {
  2109. if (param.empty()) {
  2110. return "1";
  2111. }
  2112. } else if (cmsysString_strcasecmp(param.c_str(),
  2113. context->Config.c_str()) == 0) {
  2114. return "1";
  2115. }
  2116. }
  2117. }
  2118. if (targetIsImported) {
  2119. cmValue loc = nullptr;
  2120. cmValue imp = nullptr;
  2121. std::string suffix;
  2122. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  2123. imp, suffix)) {
  2124. if (oldPolicy) {
  2125. // If the target has a MAP_IMPORTED_CONFIG_<CONFIG> property for the
  2126. // consumer's <CONFIG>, match *any* config in that list, regardless
  2127. // of whether it's valid or of what GetMappedConfig actually picked.
  2128. // This will result in $<CONFIG> producing '1' for multiple configs,
  2129. // and is almost certainly wrong, but it's what CMake did for a very
  2130. // long time, and... Hyrum's Law.
  2131. cmList mappedConfigs;
  2132. std::string mapProp = cmStrCat(
  2133. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  2134. if (cmValue mapValue =
  2135. context->CurrentTarget->GetProperty(mapProp)) {
  2136. mappedConfigs.assign(cmSystemTools::UpperCase(*mapValue));
  2137. for (auto const& param : parameters) {
  2138. if (cm::contains(mappedConfigs,
  2139. cmSystemTools::UpperCase(param))) {
  2140. return "1";
  2141. }
  2142. }
  2143. return "0";
  2144. }
  2145. }
  2146. // This imported target has an appropriate location for this (possibly
  2147. // mapped) config.
  2148. if (!suffix.empty()) {
  2149. // Use the (possibly mapped) configuration of the imported location
  2150. // that was selected.
  2151. for (auto const& param : parameters) {
  2152. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  2153. return "1";
  2154. }
  2155. }
  2156. }
  2157. }
  2158. }
  2159. return "0";
  2160. }
  2161. } configurationTestNode;
  2162. static const struct JoinNode : public cmGeneratorExpressionNode
  2163. {
  2164. JoinNode() {} // NOLINT(modernize-use-equals-default)
  2165. int NumExpectedParameters() const override { return 2; }
  2166. bool AcceptsArbitraryContentParameter() const override { return true; }
  2167. std::string Evaluate(
  2168. std::vector<std::string> const& parameters,
  2169. cmGeneratorExpressionContext* /*context*/,
  2170. GeneratorExpressionContent const* /*content*/,
  2171. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2172. {
  2173. return cmList{ parameters.front() }.join(parameters[1]);
  2174. }
  2175. } joinNode;
  2176. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  2177. {
  2178. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2179. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2180. std::string Evaluate(
  2181. std::vector<std::string> const& parameters,
  2182. cmGeneratorExpressionContext* context,
  2183. GeneratorExpressionContent const* content,
  2184. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2185. {
  2186. if (context->Language.empty() &&
  2187. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  2188. reportError(
  2189. context, content->GetOriginalExpression(),
  2190. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  2191. "directories, compile definitions, compile options, and to evaluate "
  2192. "components of the file(GENERATE) command.");
  2193. return std::string();
  2194. }
  2195. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2196. std::string genName = gg->GetName();
  2197. if (genName.find("Makefiles") == std::string::npos &&
  2198. genName.find("Ninja") == std::string::npos &&
  2199. genName.find("Visual Studio") == std::string::npos &&
  2200. genName.find("Xcode") == std::string::npos &&
  2201. genName.find("Watcom WMake") == std::string::npos &&
  2202. genName.find("Green Hills MULTI") == std::string::npos) {
  2203. reportError(context, content->GetOriginalExpression(),
  2204. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  2205. return std::string();
  2206. }
  2207. if (parameters.empty()) {
  2208. return context->Language;
  2209. }
  2210. for (auto const& param : parameters) {
  2211. if (context->Language == param) {
  2212. return "1";
  2213. }
  2214. }
  2215. return "0";
  2216. }
  2217. } languageNode;
  2218. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  2219. {
  2220. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2221. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2222. std::string Evaluate(
  2223. std::vector<std::string> const& parameters,
  2224. cmGeneratorExpressionContext* context,
  2225. GeneratorExpressionContent const* content,
  2226. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2227. {
  2228. if (!context->HeadTarget ||
  2229. (context->Language.empty() &&
  2230. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  2231. // reportError(context, content->GetOriginalExpression(), "");
  2232. reportError(
  2233. context, content->GetOriginalExpression(),
  2234. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary "
  2235. "targets "
  2236. "to specify include directories, compile definitions, and compile "
  2237. "options. It may not be used with the add_custom_command, "
  2238. "add_custom_target, or file(GENERATE) commands.");
  2239. return std::string();
  2240. }
  2241. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2242. std::string genName = gg->GetName();
  2243. if (genName.find("Makefiles") == std::string::npos &&
  2244. genName.find("Ninja") == std::string::npos &&
  2245. genName.find("Visual Studio") == std::string::npos &&
  2246. genName.find("Xcode") == std::string::npos &&
  2247. genName.find("Watcom WMake") == std::string::npos &&
  2248. genName.find("Green Hills MULTI") == std::string::npos) {
  2249. reportError(
  2250. context, content->GetOriginalExpression(),
  2251. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  2252. return std::string();
  2253. }
  2254. std::string const& lang = context->Language;
  2255. if (lang == parameters.front()) {
  2256. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2257. parameters.cend());
  2258. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  2259. idParameter, context, content, dagChecker, lang);
  2260. }
  2261. return "0";
  2262. }
  2263. } languageAndIdNode;
  2264. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  2265. {
  2266. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2267. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2268. std::string Evaluate(
  2269. std::vector<std::string> const& parameters,
  2270. cmGeneratorExpressionContext* context,
  2271. GeneratorExpressionContent const* content,
  2272. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2273. {
  2274. if (!context->HeadTarget || !dagChecker ||
  2275. !(dagChecker->EvaluatingLinkExpression() ||
  2276. dagChecker->EvaluatingLinkLibraries() ||
  2277. dagChecker->EvaluatingLinkerLauncher())) {
  2278. reportError(context, content->GetOriginalExpression(),
  2279. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  2280. "to specify link libraries, link directories, link options "
  2281. "and link depends.");
  2282. return std::string();
  2283. }
  2284. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  2285. reportError(
  2286. context, content->GetOriginalExpression(),
  2287. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  2288. return std::string();
  2289. }
  2290. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2291. std::string genName = gg->GetName();
  2292. if (genName.find("Makefiles") == std::string::npos &&
  2293. genName.find("Ninja") == std::string::npos &&
  2294. genName.find("Visual Studio") == std::string::npos &&
  2295. genName.find("Xcode") == std::string::npos &&
  2296. genName.find("Watcom WMake") == std::string::npos &&
  2297. genName.find("Green Hills MULTI") == std::string::npos) {
  2298. reportError(context, content->GetOriginalExpression(),
  2299. "$<LINK_LANGUAGE:...> not supported for this generator.");
  2300. return std::string();
  2301. }
  2302. if (dagChecker->EvaluatingLinkLibraries()) {
  2303. context->HadHeadSensitiveCondition = true;
  2304. context->HadLinkLanguageSensitiveCondition = true;
  2305. }
  2306. if (parameters.empty()) {
  2307. return context->Language;
  2308. }
  2309. for (auto const& param : parameters) {
  2310. if (context->Language == param) {
  2311. return "1";
  2312. }
  2313. }
  2314. return "0";
  2315. }
  2316. } linkLanguageNode;
  2317. namespace {
  2318. struct LinkerId
  2319. {
  2320. static std::string Evaluate(std::vector<std::string> const& parameters,
  2321. cmGeneratorExpressionContext* context,
  2322. GeneratorExpressionContent const* content,
  2323. std::string const& lang)
  2324. {
  2325. std::string const& linkerId =
  2326. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  2327. "_COMPILER_ID");
  2328. if (parameters.empty()) {
  2329. return linkerId;
  2330. }
  2331. if (linkerId.empty()) {
  2332. return parameters.front().empty() ? "1" : "0";
  2333. }
  2334. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  2335. for (auto const& param : parameters) {
  2336. if (!linkerIdValidator.find(param)) {
  2337. reportError(context, content->GetOriginalExpression(),
  2338. "Expression syntax not recognized.");
  2339. return std::string();
  2340. }
  2341. if (param == linkerId) {
  2342. return "1";
  2343. }
  2344. }
  2345. return "0";
  2346. }
  2347. };
  2348. }
  2349. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  2350. {
  2351. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2352. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2353. std::string Evaluate(
  2354. std::vector<std::string> const& parameters,
  2355. cmGeneratorExpressionContext* context,
  2356. GeneratorExpressionContent const* content,
  2357. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2358. {
  2359. if (!context->HeadTarget || !dagChecker ||
  2360. !(dagChecker->EvaluatingLinkExpression() ||
  2361. dagChecker->EvaluatingLinkLibraries() ||
  2362. dagChecker->EvaluatingLinkerLauncher())) {
  2363. reportError(
  2364. context, content->GetOriginalExpression(),
  2365. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  2366. "to specify link libraries, link directories, link options, and "
  2367. "link "
  2368. "depends.");
  2369. return std::string();
  2370. }
  2371. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2372. std::string genName = gg->GetName();
  2373. if (genName.find("Makefiles") == std::string::npos &&
  2374. genName.find("Ninja") == std::string::npos &&
  2375. genName.find("Visual Studio") == std::string::npos &&
  2376. genName.find("Xcode") == std::string::npos &&
  2377. genName.find("Watcom WMake") == std::string::npos &&
  2378. genName.find("Green Hills MULTI") == std::string::npos) {
  2379. reportError(
  2380. context, content->GetOriginalExpression(),
  2381. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  2382. return std::string();
  2383. }
  2384. if (dagChecker->EvaluatingLinkLibraries()) {
  2385. context->HadHeadSensitiveCondition = true;
  2386. context->HadLinkLanguageSensitiveCondition = true;
  2387. }
  2388. std::string const& lang = context->Language;
  2389. if (lang == parameters.front()) {
  2390. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2391. parameters.cend());
  2392. return LinkerId::Evaluate(idParameter, context, content, lang);
  2393. }
  2394. return "0";
  2395. }
  2396. } linkLanguageAndIdNode;
  2397. struct CompilerLinkerIdNode : public cmGeneratorExpressionNode
  2398. {
  2399. CompilerLinkerIdNode(char const* lang)
  2400. : Language(lang)
  2401. {
  2402. }
  2403. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2404. std::string Evaluate(
  2405. std::vector<std::string> const& parameters,
  2406. cmGeneratorExpressionContext* context,
  2407. GeneratorExpressionContent const* content,
  2408. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2409. {
  2410. if (!context->HeadTarget) {
  2411. reportError(
  2412. context, content->GetOriginalExpression(),
  2413. cmStrCat(
  2414. "$<", this->Language,
  2415. "_COMPILER_LINKER_ID> may only be used with binary targets. It may "
  2416. "not be used with add_custom_command or add_custom_target."));
  2417. return {};
  2418. }
  2419. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2420. this->Language);
  2421. }
  2422. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2423. cmGeneratorExpressionContext* context,
  2424. GeneratorExpressionContent const* content,
  2425. cmGeneratorExpressionDAGChecker* /*unused*/,
  2426. std::string const& lang) const
  2427. {
  2428. std::string const& compilerLinkerId =
  2429. context->LG->GetMakefile()->GetSafeDefinition(
  2430. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_ID"));
  2431. if (parameters.empty()) {
  2432. return compilerLinkerId;
  2433. }
  2434. if (compilerLinkerId.empty()) {
  2435. return parameters.front().empty() ? "1" : "0";
  2436. }
  2437. static cmsys::RegularExpression compilerLinkerIdValidator(
  2438. "^[A-Za-z0-9_]*$");
  2439. for (auto const& param : parameters) {
  2440. if (!compilerLinkerIdValidator.find(param)) {
  2441. reportError(context, content->GetOriginalExpression(),
  2442. "Expression syntax not recognized.");
  2443. return std::string();
  2444. }
  2445. if (param == compilerLinkerId) {
  2446. return "1";
  2447. }
  2448. }
  2449. return "0";
  2450. }
  2451. char const* const Language;
  2452. };
  2453. static CompilerLinkerIdNode const cCompilerLinkerIdNode("C"),
  2454. cxxCompilerLinkerIdNode("CXX"), cudaCompilerLinkerIdNode("CUDA"),
  2455. objcCompilerLinkerIdNode("OBJC"), objcxxCompilerLinkerIdNode("OBJCXX"),
  2456. fortranCompilerLinkerIdNode("Fortran"), hipCompilerLinkerIdNode("HIP");
  2457. struct CompilerLinkerFrontendVariantNode : public cmGeneratorExpressionNode
  2458. {
  2459. CompilerLinkerFrontendVariantNode(char const* lang)
  2460. : Language(lang)
  2461. {
  2462. }
  2463. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2464. std::string Evaluate(
  2465. std::vector<std::string> const& parameters,
  2466. cmGeneratorExpressionContext* context,
  2467. GeneratorExpressionContent const* content,
  2468. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2469. {
  2470. if (!context->HeadTarget) {
  2471. reportError(
  2472. context, content->GetOriginalExpression(),
  2473. cmStrCat(
  2474. "$<", this->Language,
  2475. "_COMPILER_LINKER_FRONTEND_VARIANT> may only be used with binary "
  2476. "targets. It may not be used with add_custom_command or "
  2477. "add_custom_target."));
  2478. return {};
  2479. }
  2480. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2481. this->Language);
  2482. }
  2483. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2484. cmGeneratorExpressionContext* context,
  2485. GeneratorExpressionContent const* content,
  2486. cmGeneratorExpressionDAGChecker* /*unused*/,
  2487. std::string const& lang) const
  2488. {
  2489. std::string const& compilerLinkerFrontendVariant =
  2490. context->LG->GetMakefile()->GetSafeDefinition(
  2491. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_FRONTEND_VARIANT"));
  2492. if (parameters.empty()) {
  2493. return compilerLinkerFrontendVariant;
  2494. }
  2495. if (compilerLinkerFrontendVariant.empty()) {
  2496. return parameters.front().empty() ? "1" : "0";
  2497. }
  2498. static cmsys::RegularExpression compilerLinkerFrontendVariantValidator(
  2499. "^[A-Za-z0-9_]*$");
  2500. for (auto const& param : parameters) {
  2501. if (!compilerLinkerFrontendVariantValidator.find(param)) {
  2502. reportError(context, content->GetOriginalExpression(),
  2503. "Expression syntax not recognized.");
  2504. return {};
  2505. }
  2506. if (param == compilerLinkerFrontendVariant) {
  2507. return "1";
  2508. }
  2509. }
  2510. return "0";
  2511. }
  2512. char const* const Language;
  2513. };
  2514. static CompilerLinkerFrontendVariantNode const
  2515. cCompilerLinkerFrontendVariantNode("C"),
  2516. cxxCompilerLinkerFrontendVariantNode("CXX"),
  2517. cudaCompilerLinkerFrontendVariantNode("CUDA"),
  2518. objcCompilerLinkerFrontendVariantNode("OBJC"),
  2519. objcxxCompilerLinkerFrontendVariantNode("OBJCXX"),
  2520. fortranCompilerLinkerFrontendVariantNode("Fortran"),
  2521. hipCompilerLinkerFrontendVariantNode("HIP");
  2522. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  2523. {
  2524. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  2525. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2526. std::string Evaluate(
  2527. std::vector<std::string> const& parameters,
  2528. cmGeneratorExpressionContext* context,
  2529. GeneratorExpressionContent const* content,
  2530. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2531. {
  2532. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2533. if (!context->HeadTarget || !dagChecker ||
  2534. !dagChecker->EvaluatingLinkLibraries(nullptr,
  2535. ForGenex::LINK_LIBRARY)) {
  2536. reportError(context, content->GetOriginalExpression(),
  2537. "$<LINK_LIBRARY:...> may only be used with binary targets "
  2538. "to specify link libraries through 'LINK_LIBRARIES', "
  2539. "'INTERFACE_LINK_LIBRARIES', and "
  2540. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2541. return std::string();
  2542. }
  2543. cmList list{ parameters.begin(), parameters.end() };
  2544. if (list.empty()) {
  2545. reportError(
  2546. context, content->GetOriginalExpression(),
  2547. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  2548. return std::string();
  2549. }
  2550. if (list.size() == 1) {
  2551. // no libraries specified, ignore this genex
  2552. return std::string();
  2553. }
  2554. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2555. auto const& feature = list.front();
  2556. if (!featureNameValidator.find(feature)) {
  2557. reportError(context, content->GetOriginalExpression(),
  2558. cmStrCat("The feature name '", feature,
  2559. "' contains invalid characters."));
  2560. return std::string();
  2561. }
  2562. auto const LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  2563. auto const LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  2564. // filter out $<LINK_LIBRARY:..> tags with same feature
  2565. // and raise an error for any different feature
  2566. cm::erase_if(list, [&](std::string const& item) -> bool {
  2567. return item == LL_BEGIN || item == LL_END;
  2568. });
  2569. auto it =
  2570. std::find_if(list.cbegin() + 1, list.cend(),
  2571. [&feature](std::string const& item) -> bool {
  2572. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  2573. item.substr(14, item.find('>', 14) - 14) != feature;
  2574. });
  2575. if (it != list.cend()) {
  2576. reportError(
  2577. context, content->GetOriginalExpression(),
  2578. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  2579. return std::string();
  2580. }
  2581. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  2582. it = std::find_if(list.cbegin() + 1, list.cend(),
  2583. [](std::string const& item) -> bool {
  2584. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  2585. });
  2586. if (it != list.cend()) {
  2587. reportError(context, content->GetOriginalExpression(),
  2588. "$<LINK_GROUP:...> cannot be nested inside a "
  2589. "$<LINK_LIBRARY:...> expression.");
  2590. return std::string();
  2591. }
  2592. list.front() = LL_BEGIN;
  2593. list.push_back(LL_END);
  2594. return list.to_string();
  2595. }
  2596. } linkLibraryNode;
  2597. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  2598. {
  2599. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  2600. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2601. std::string Evaluate(
  2602. std::vector<std::string> const& parameters,
  2603. cmGeneratorExpressionContext* context,
  2604. GeneratorExpressionContent const* content,
  2605. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2606. {
  2607. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2608. if (!context->HeadTarget || !dagChecker ||
  2609. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  2610. reportError(
  2611. context, content->GetOriginalExpression(),
  2612. "$<LINK_GROUP:...> may only be used with binary targets "
  2613. "to specify group of link libraries through 'LINK_LIBRARIES', "
  2614. "'INTERFACE_LINK_LIBRARIES', and "
  2615. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2616. return std::string();
  2617. }
  2618. cmList list{ parameters.begin(), parameters.end() };
  2619. if (list.empty()) {
  2620. reportError(
  2621. context, content->GetOriginalExpression(),
  2622. "$<LINK_GROUP:...> expects a feature name as first argument.");
  2623. return std::string();
  2624. }
  2625. // $<LINK_GROUP:..> cannot be nested
  2626. if (std::find_if(list.cbegin(), list.cend(),
  2627. [](std::string const& item) -> bool {
  2628. return cmHasPrefix(item, "<LINK_GROUP"_s);
  2629. }) != list.cend()) {
  2630. reportError(context, content->GetOriginalExpression(),
  2631. "$<LINK_GROUP:...> cannot be nested.");
  2632. return std::string();
  2633. }
  2634. if (list.size() == 1) {
  2635. // no libraries specified, ignore this genex
  2636. return std::string();
  2637. }
  2638. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2639. auto const& feature = list.front();
  2640. if (!featureNameValidator.find(feature)) {
  2641. reportError(context, content->GetOriginalExpression(),
  2642. cmStrCat("The feature name '", feature,
  2643. "' contains invalid characters."));
  2644. return std::string();
  2645. }
  2646. auto const LG_BEGIN = cmStrCat(
  2647. "<LINK_GROUP:", feature, ':',
  2648. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  2649. "|"_s),
  2650. '>');
  2651. auto const LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  2652. list.front() = LG_BEGIN;
  2653. list.push_back(LG_END);
  2654. return list.to_string();
  2655. }
  2656. } linkGroupNode;
  2657. static const struct HostLinkNode : public cmGeneratorExpressionNode
  2658. {
  2659. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  2660. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2661. std::string Evaluate(
  2662. std::vector<std::string> const& parameters,
  2663. cmGeneratorExpressionContext* context,
  2664. GeneratorExpressionContent const* content,
  2665. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2666. {
  2667. if (!context->HeadTarget || !dagChecker ||
  2668. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2669. reportError(context, content->GetOriginalExpression(),
  2670. "$<HOST_LINK:...> may only be used with binary targets "
  2671. "to specify link options.");
  2672. return std::string();
  2673. }
  2674. return context->HeadTarget->IsDeviceLink() ? std::string()
  2675. : cmList::to_string(parameters);
  2676. }
  2677. } hostLinkNode;
  2678. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  2679. {
  2680. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  2681. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2682. std::string Evaluate(
  2683. std::vector<std::string> const& parameters,
  2684. cmGeneratorExpressionContext* context,
  2685. GeneratorExpressionContent const* content,
  2686. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2687. {
  2688. if (!context->HeadTarget || !dagChecker ||
  2689. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2690. reportError(context, content->GetOriginalExpression(),
  2691. "$<DEVICE_LINK:...> may only be used with binary targets "
  2692. "to specify link options.");
  2693. return std::string();
  2694. }
  2695. if (context->HeadTarget->IsDeviceLink()) {
  2696. cmList list{ parameters.begin(), parameters.end() };
  2697. auto const DL_BEGIN = "<DEVICE_LINK>"_s;
  2698. auto const DL_END = "</DEVICE_LINK>"_s;
  2699. cm::erase_if(list, [&](std::string const& item) {
  2700. return item == DL_BEGIN || item == DL_END;
  2701. });
  2702. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  2703. list.push_back(static_cast<std::string>(DL_END));
  2704. return list.to_string();
  2705. }
  2706. return std::string();
  2707. }
  2708. } deviceLinkNode;
  2709. static std::string getLinkedTargetsContent(
  2710. cmGeneratorTarget const* target, std::string const& prop,
  2711. cmGeneratorExpressionContext* context,
  2712. cmGeneratorExpressionDAGChecker* dagChecker, cmGeneratorTarget::UseTo usage)
  2713. {
  2714. std::string result;
  2715. if (cmLinkImplementationLibraries const* impl =
  2716. target->GetLinkImplementationLibraries(
  2717. context->Config, cmGeneratorTarget::UseTo::Compile)) {
  2718. for (cmLinkImplItem const& lib : impl->Libraries) {
  2719. if (lib.Target) {
  2720. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  2721. // caller's property and hand-evaluate it as if it were compiled.
  2722. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  2723. cmGeneratorExpressionContext libContext(
  2724. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  2725. target, context->EvaluateForBuildsystem, lib.Backtrace,
  2726. context->Language);
  2727. std::string libResult = lib.Target->EvaluateInterfaceProperty(
  2728. prop, &libContext, dagChecker, usage);
  2729. if (!libResult.empty()) {
  2730. if (result.empty()) {
  2731. result = std::move(libResult);
  2732. } else {
  2733. result.reserve(result.size() + 1 + libResult.size());
  2734. result += ";";
  2735. result += libResult;
  2736. }
  2737. }
  2738. }
  2739. }
  2740. }
  2741. return result;
  2742. }
  2743. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  2744. {
  2745. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  2746. // This node handles errors on parameter count itself.
  2747. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2748. static char const* GetErrorText(std::string const& targetName,
  2749. std::string const& propertyName)
  2750. {
  2751. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2752. if (targetName.empty() && propertyName.empty()) {
  2753. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2754. "target name and property name.";
  2755. }
  2756. if (targetName.empty()) {
  2757. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2758. "target name.";
  2759. }
  2760. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  2761. if (!propertyNameValidator.find(propertyName)) {
  2762. return "Target name and property name not supported.";
  2763. }
  2764. return "Target name not supported.";
  2765. }
  2766. return nullptr;
  2767. }
  2768. std::string Evaluate(
  2769. std::vector<std::string> const& parameters,
  2770. cmGeneratorExpressionContext* context,
  2771. GeneratorExpressionContent const* content,
  2772. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  2773. {
  2774. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2775. cmGeneratorTarget const* target = nullptr;
  2776. std::string targetName;
  2777. std::string propertyName;
  2778. if (parameters.size() == 2) {
  2779. targetName = parameters[0];
  2780. propertyName = parameters[1];
  2781. if (char const* e = GetErrorText(targetName, propertyName)) {
  2782. reportError(context, content->GetOriginalExpression(), e);
  2783. return std::string();
  2784. }
  2785. if (propertyName == "ALIASED_TARGET"_s) {
  2786. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2787. if (cmGeneratorTarget* tgt =
  2788. context->LG->FindGeneratorTargetToUse(targetName)) {
  2789. return tgt->GetName();
  2790. }
  2791. }
  2792. return std::string();
  2793. }
  2794. if (propertyName == "ALIAS_GLOBAL"_s) {
  2795. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2796. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  2797. ? "TRUE"
  2798. : "FALSE";
  2799. }
  2800. return std::string();
  2801. }
  2802. cmLocalGenerator const* lg = context->CurrentTarget
  2803. ? context->CurrentTarget->GetLocalGenerator()
  2804. : context->LG;
  2805. target = lg->FindGeneratorTargetToUse(targetName);
  2806. if (!target) {
  2807. std::ostringstream e;
  2808. e << "Target \"" << targetName << "\" not found.";
  2809. reportError(context, content->GetOriginalExpression(), e.str());
  2810. return std::string();
  2811. }
  2812. context->AllTargets.insert(target);
  2813. } else if (parameters.size() == 1) {
  2814. target = context->HeadTarget;
  2815. propertyName = parameters[0];
  2816. // Keep track of the properties seen while processing.
  2817. // The evaluation of the LINK_LIBRARIES generator expressions
  2818. // will check this to ensure that properties have one consistent
  2819. // value for all evaluations.
  2820. context->SeenTargetProperties.insert(propertyName);
  2821. context->HadHeadSensitiveCondition = true;
  2822. if (!target) {
  2823. reportError(
  2824. context, content->GetOriginalExpression(),
  2825. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  2826. "It may not be used with add_custom_command or add_custom_target. "
  2827. " "
  2828. " "
  2829. "Specify the target to read a property from using the "
  2830. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  2831. return std::string();
  2832. }
  2833. } else {
  2834. reportError(
  2835. context, content->GetOriginalExpression(),
  2836. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  2837. return std::string();
  2838. }
  2839. if (propertyName == "SOURCES") {
  2840. context->SourceSensitiveTargets.insert(target);
  2841. }
  2842. if (propertyName.empty()) {
  2843. reportError(
  2844. context, content->GetOriginalExpression(),
  2845. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  2846. "name.");
  2847. return std::string();
  2848. }
  2849. if (!propertyNameValidator.find(propertyName)) {
  2850. ::reportError(context, content->GetOriginalExpression(),
  2851. "Property name not supported.");
  2852. return std::string();
  2853. }
  2854. assert(target);
  2855. if (propertyName == "LINKER_LANGUAGE") {
  2856. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  2857. (dagCheckerParent->EvaluatingLinkLibraries() ||
  2858. dagCheckerParent->EvaluatingSources())) {
  2859. reportError(
  2860. context, content->GetOriginalExpression(),
  2861. "LINKER_LANGUAGE target property can not be used while evaluating "
  2862. "link libraries for a static library");
  2863. return std::string();
  2864. }
  2865. return target->GetLinkerLanguage(context->Config);
  2866. }
  2867. bool const evaluatingLinkLibraries =
  2868. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries();
  2869. std::string interfacePropertyName;
  2870. bool isInterfaceProperty = false;
  2871. cmGeneratorTarget::UseTo usage = cmGeneratorTarget::UseTo::Compile;
  2872. if (cm::optional<cmGeneratorTarget::TransitiveProperty> transitiveProp =
  2873. target->IsTransitiveProperty(propertyName, context->LG,
  2874. context->Config, dagCheckerParent)) {
  2875. interfacePropertyName = std::string(transitiveProp->InterfaceName);
  2876. isInterfaceProperty = transitiveProp->InterfaceName == propertyName;
  2877. usage = transitiveProp->Usage;
  2878. }
  2879. if (dagCheckerParent) {
  2880. // This $<TARGET_PROPERTY:...> node has been reached while evaluating
  2881. // another target property value. Check that the outermost evaluation
  2882. // expects such nested evaluations.
  2883. if (dagCheckerParent->EvaluatingGenexExpression() ||
  2884. dagCheckerParent->EvaluatingPICExpression() ||
  2885. dagCheckerParent->EvaluatingLinkerLauncher()) {
  2886. // No check required.
  2887. } else if (evaluatingLinkLibraries) {
  2888. if (!interfacePropertyName.empty()) {
  2889. reportError(
  2890. context, content->GetOriginalExpression(),
  2891. "$<TARGET_PROPERTY:...> expression in link libraries "
  2892. "evaluation depends on target property which is transitive "
  2893. "over the link libraries, creating a recursion.");
  2894. return std::string();
  2895. }
  2896. } else {
  2897. assert(dagCheckerParent->EvaluatingTransitiveProperty());
  2898. }
  2899. }
  2900. if (isInterfaceProperty) {
  2901. return cmGeneratorExpression::StripEmptyListElements(
  2902. target->EvaluateInterfaceProperty(propertyName, context,
  2903. dagCheckerParent, usage));
  2904. }
  2905. cmGeneratorExpressionDAGChecker dagChecker{
  2906. target,
  2907. propertyName,
  2908. content,
  2909. dagCheckerParent,
  2910. context->LG,
  2911. context->Config,
  2912. context->Backtrace,
  2913. };
  2914. switch (dagChecker.Check()) {
  2915. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  2916. dagChecker.ReportError(context, content->GetOriginalExpression());
  2917. return std::string();
  2918. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  2919. // No error. We just skip cyclic references.
  2920. return std::string();
  2921. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  2922. // We handle transitive properties above. For non-transitive
  2923. // properties we accept repeats anyway.
  2924. case cmGeneratorExpressionDAGChecker::DAG:
  2925. break;
  2926. }
  2927. std::string result;
  2928. bool haveProp = false;
  2929. if (cmValue p = target->GetProperty(propertyName)) {
  2930. result = *p;
  2931. haveProp = true;
  2932. } else if (evaluatingLinkLibraries) {
  2933. return std::string();
  2934. }
  2935. // Properties named by COMPATIBLE_INTERFACE_ properties combine over
  2936. // the transitive link closure as a single order-independent value.
  2937. // Imported targets do not themselves have a defined value for these
  2938. // properties, but they can contribute to the value of a non-imported
  2939. // dependent.
  2940. //
  2941. // For COMPATIBLE_INTERFACE_{BOOL,STRING}:
  2942. // * If set on this target, use the value directly. It is checked
  2943. // elsewhere for consistency over the transitive link closure.
  2944. // * If not set on this target, compute the value from the closure.
  2945. //
  2946. // For COMPATIBLE_INTERFACE_NUMBER_{MAX,MIN} we always compute the value
  2947. // from this target and the transitive link closure to get the max or min.
  2948. if (!haveProp && !target->IsImported()) {
  2949. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  2950. context->Config)) {
  2951. context->HadContextSensitiveCondition = true;
  2952. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  2953. context->Config)
  2954. ? "1"
  2955. : "0";
  2956. }
  2957. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  2958. context->Config)) {
  2959. context->HadContextSensitiveCondition = true;
  2960. char const* propContent =
  2961. target->GetLinkInterfaceDependentStringProperty(propertyName,
  2962. context->Config);
  2963. return propContent ? propContent : "";
  2964. }
  2965. }
  2966. if (!evaluatingLinkLibraries && !target->IsImported()) {
  2967. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  2968. context->Config)) {
  2969. context->HadContextSensitiveCondition = true;
  2970. char const* propContent =
  2971. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  2972. context->Config);
  2973. return propContent ? propContent : "";
  2974. }
  2975. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  2976. context->Config)) {
  2977. context->HadContextSensitiveCondition = true;
  2978. char const* propContent =
  2979. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  2980. context->Config);
  2981. return propContent ? propContent : "";
  2982. }
  2983. }
  2984. // Some properties, such as usage requirements, combine over the
  2985. // transitive link closure as an ordered list.
  2986. if (!interfacePropertyName.empty()) {
  2987. result = cmGeneratorExpression::StripEmptyListElements(
  2988. this->EvaluateDependentExpression(result, context->LG, context, target,
  2989. &dagChecker, target));
  2990. std::string linkedTargetsContent = getLinkedTargetsContent(
  2991. target, interfacePropertyName, context, &dagChecker, usage);
  2992. if (!linkedTargetsContent.empty()) {
  2993. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  2994. }
  2995. }
  2996. return result;
  2997. }
  2998. } targetPropertyNode;
  2999. static const struct TargetNameNode : public cmGeneratorExpressionNode
  3000. {
  3001. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  3002. bool GeneratesContent() const override { return true; }
  3003. bool AcceptsArbitraryContentParameter() const override { return true; }
  3004. bool RequiresLiteralInput() const override { return true; }
  3005. std::string Evaluate(
  3006. std::vector<std::string> const& parameters,
  3007. cmGeneratorExpressionContext* /*context*/,
  3008. GeneratorExpressionContent const* /*content*/,
  3009. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3010. {
  3011. return parameters.front();
  3012. }
  3013. int NumExpectedParameters() const override { return 1; }
  3014. } targetNameNode;
  3015. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  3016. {
  3017. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  3018. std::string Evaluate(
  3019. std::vector<std::string> const& parameters,
  3020. cmGeneratorExpressionContext* context,
  3021. GeneratorExpressionContent const* content,
  3022. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3023. {
  3024. std::string const& tgtName = parameters.front();
  3025. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  3026. if (!gt) {
  3027. std::ostringstream e;
  3028. e << "Objects of target \"" << tgtName
  3029. << "\" referenced but no such target exists.";
  3030. reportError(context, content->GetOriginalExpression(), e.str());
  3031. return std::string();
  3032. }
  3033. cmStateEnums::TargetType type = gt->GetType();
  3034. if (type != cmStateEnums::EXECUTABLE &&
  3035. type != cmStateEnums::STATIC_LIBRARY &&
  3036. type != cmStateEnums::SHARED_LIBRARY &&
  3037. type != cmStateEnums::MODULE_LIBRARY &&
  3038. type != cmStateEnums::OBJECT_LIBRARY) {
  3039. std::ostringstream e;
  3040. e << "Objects of target \"" << tgtName
  3041. << "\" referenced but is not one of the allowed target types "
  3042. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  3043. reportError(context, content->GetOriginalExpression(), e.str());
  3044. return std::string();
  3045. }
  3046. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  3047. {
  3048. std::string reason;
  3049. if (!context->EvaluateForBuildsystem &&
  3050. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  3051. std::ostringstream e;
  3052. e << "The evaluation of the TARGET_OBJECTS generator expression "
  3053. "is only suitable for consumption by CMake (limited"
  3054. << reason
  3055. << "). "
  3056. "It is not suitable for writing out elsewhere.";
  3057. reportError(context, content->GetOriginalExpression(), e.str());
  3058. return std::string();
  3059. }
  3060. }
  3061. cmList objects;
  3062. if (gt->IsImported()) {
  3063. cmValue loc = nullptr;
  3064. cmValue imp = nullptr;
  3065. std::string suffix;
  3066. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  3067. objects.assign(*loc);
  3068. }
  3069. context->HadContextSensitiveCondition = true;
  3070. } else {
  3071. gt->GetTargetObjectNames(context->Config, objects);
  3072. std::string obj_dir;
  3073. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  3074. // Use object file directory with buildsystem placeholder.
  3075. obj_dir = gt->ObjectDirectory;
  3076. context->HadContextSensitiveCondition =
  3077. gt->HasContextDependentSources();
  3078. } else {
  3079. // Use object file directory with per-config location.
  3080. obj_dir = gt->GetObjectDirectory(context->Config);
  3081. context->HadContextSensitiveCondition = true;
  3082. }
  3083. for (auto& o : objects) {
  3084. o = cmStrCat(obj_dir, o);
  3085. }
  3086. }
  3087. // Create the cmSourceFile instances in the referencing directory.
  3088. cmMakefile* mf = context->LG->GetMakefile();
  3089. for (std::string const& o : objects) {
  3090. mf->AddTargetObject(tgtName, o);
  3091. }
  3092. return objects.to_string();
  3093. }
  3094. } targetObjectsNode;
  3095. struct TargetRuntimeDllsBaseNode : public cmGeneratorExpressionNode
  3096. {
  3097. std::vector<std::string> CollectDlls(
  3098. std::vector<std::string> const& parameters,
  3099. cmGeneratorExpressionContext* context,
  3100. GeneratorExpressionContent const* content) const
  3101. {
  3102. std::string const& tgtName = parameters.front();
  3103. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  3104. if (!gt) {
  3105. std::ostringstream e;
  3106. e << "Objects of target \"" << tgtName
  3107. << "\" referenced but no such target exists.";
  3108. reportError(context, content->GetOriginalExpression(), e.str());
  3109. return std::vector<std::string>();
  3110. }
  3111. cmStateEnums::TargetType type = gt->GetType();
  3112. if (type != cmStateEnums::EXECUTABLE &&
  3113. type != cmStateEnums::SHARED_LIBRARY &&
  3114. type != cmStateEnums::MODULE_LIBRARY) {
  3115. std::ostringstream e;
  3116. e << "Objects of target \"" << tgtName
  3117. << "\" referenced but is not one of the allowed target types "
  3118. << "(EXECUTABLE, SHARED, MODULE).";
  3119. reportError(context, content->GetOriginalExpression(), e.str());
  3120. return std::vector<std::string>();
  3121. }
  3122. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  3123. std::vector<std::string> dllPaths;
  3124. auto const& dlls = cli->GetRuntimeDLLs();
  3125. for (auto const& dll : dlls) {
  3126. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  3127. dllPaths.emplace_back(*loc);
  3128. }
  3129. }
  3130. return dllPaths;
  3131. }
  3132. return std::vector<std::string>();
  3133. }
  3134. };
  3135. static const struct TargetRuntimeDllsNode : public TargetRuntimeDllsBaseNode
  3136. {
  3137. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  3138. std::string Evaluate(
  3139. std::vector<std::string> const& parameters,
  3140. cmGeneratorExpressionContext* context,
  3141. GeneratorExpressionContent const* content,
  3142. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3143. {
  3144. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3145. return cmList::to_string(dlls);
  3146. }
  3147. } targetRuntimeDllsNode;
  3148. static const struct TargetRuntimeDllDirsNode : public TargetRuntimeDllsBaseNode
  3149. {
  3150. TargetRuntimeDllDirsNode() {} // NOLINT(modernize-use-equals-default)
  3151. std::string Evaluate(
  3152. std::vector<std::string> const& parameters,
  3153. cmGeneratorExpressionContext* context,
  3154. GeneratorExpressionContent const* content,
  3155. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3156. {
  3157. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3158. std::vector<std::string> dllDirs;
  3159. for (std::string const& dll : dlls) {
  3160. std::string directory = cmSystemTools::GetFilenamePath(dll);
  3161. if (std::find(dllDirs.begin(), dllDirs.end(), directory) ==
  3162. dllDirs.end()) {
  3163. dllDirs.push_back(directory);
  3164. }
  3165. }
  3166. return cmList::to_string(dllDirs);
  3167. }
  3168. } targetRuntimeDllDirsNode;
  3169. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  3170. {
  3171. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  3172. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  3173. std::string Evaluate(
  3174. std::vector<std::string> const& parameters,
  3175. cmGeneratorExpressionContext* context,
  3176. GeneratorExpressionContent const* content,
  3177. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3178. {
  3179. cmGeneratorTarget const* target = context->HeadTarget;
  3180. if (!target) {
  3181. reportError(
  3182. context, content->GetOriginalExpression(),
  3183. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  3184. "not be used with add_custom_command or add_custom_target.");
  3185. return std::string();
  3186. }
  3187. context->HadHeadSensitiveCondition = true;
  3188. using LangMap = std::map<std::string, cmList>;
  3189. static LangMap availableFeatures;
  3190. LangMap testedFeatures;
  3191. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  3192. for (std::string const& p : parameters) {
  3193. std::string error;
  3194. std::string lang;
  3195. if (!standardResolver.CompileFeatureKnown(
  3196. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  3197. reportError(context, content->GetOriginalExpression(), error);
  3198. return std::string();
  3199. }
  3200. testedFeatures[lang].push_back(p);
  3201. if (availableFeatures.find(lang) == availableFeatures.end()) {
  3202. cmValue featuresKnown =
  3203. standardResolver.CompileFeaturesAvailable(lang, &error);
  3204. if (!featuresKnown) {
  3205. reportError(context, content->GetOriginalExpression(), error);
  3206. return std::string();
  3207. }
  3208. availableFeatures[lang].assign(featuresKnown);
  3209. }
  3210. }
  3211. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  3212. for (auto const& lit : testedFeatures) {
  3213. std::vector<std::string> const& langAvailable =
  3214. availableFeatures[lit.first];
  3215. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  3216. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  3217. for (std::string const& it : lit.second) {
  3218. if (!cm::contains(langAvailable, it)) {
  3219. return "0";
  3220. }
  3221. if (standardDefault && standardDefault->empty()) {
  3222. // This compiler has no notion of language standard levels.
  3223. // All features known for the language are always available.
  3224. continue;
  3225. }
  3226. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  3227. context->Config, it)) {
  3228. if (evalLL) {
  3229. cmValue l =
  3230. target->GetLanguageStandard(lit.first, context->Config);
  3231. if (!l) {
  3232. l = standardDefault;
  3233. }
  3234. assert(l);
  3235. context->MaxLanguageStandard[target][lit.first] = *l;
  3236. } else {
  3237. return "0";
  3238. }
  3239. }
  3240. }
  3241. }
  3242. return "1";
  3243. }
  3244. } compileFeaturesNode;
  3245. static char const* targetPolicyWhitelist[] = {
  3246. nullptr
  3247. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  3248. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  3249. #undef TARGET_POLICY_STRING
  3250. };
  3251. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  3252. char const* policy)
  3253. {
  3254. #define RETURN_POLICY(POLICY) \
  3255. if (strcmp(policy, #POLICY) == 0) { \
  3256. return tgt->GetPolicyStatus##POLICY(); \
  3257. }
  3258. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  3259. #undef RETURN_POLICY
  3260. assert(false && "Unreachable code. Not a valid policy");
  3261. return cmPolicies::WARN;
  3262. }
  3263. static cmPolicies::PolicyID policyForString(char const* policy_id)
  3264. {
  3265. #define RETURN_POLICY_ID(POLICY_ID) \
  3266. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  3267. return cmPolicies::POLICY_ID; \
  3268. }
  3269. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  3270. #undef RETURN_POLICY_ID
  3271. assert(false && "Unreachable code. Not a valid policy");
  3272. return cmPolicies::CMPCOUNT;
  3273. }
  3274. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  3275. {
  3276. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  3277. int NumExpectedParameters() const override { return 1; }
  3278. std::string Evaluate(
  3279. std::vector<std::string> const& parameters,
  3280. cmGeneratorExpressionContext* context,
  3281. GeneratorExpressionContent const* content,
  3282. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3283. {
  3284. if (!context->HeadTarget) {
  3285. reportError(
  3286. context, content->GetOriginalExpression(),
  3287. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  3288. "may not be used with add_custom_command or add_custom_target.");
  3289. return std::string();
  3290. }
  3291. context->HadContextSensitiveCondition = true;
  3292. context->HadHeadSensitiveCondition = true;
  3293. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  3294. char const* policy = targetPolicyWhitelist[i];
  3295. if (parameters.front() == policy) {
  3296. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  3297. switch (statusForTarget(context->HeadTarget, policy)) {
  3298. case cmPolicies::WARN:
  3299. lg->IssueMessage(
  3300. MessageType::AUTHOR_WARNING,
  3301. cmPolicies::GetPolicyWarning(policyForString(policy)));
  3302. CM_FALLTHROUGH;
  3303. case cmPolicies::OLD:
  3304. return "0";
  3305. case cmPolicies::NEW:
  3306. return "1";
  3307. }
  3308. }
  3309. }
  3310. reportError(
  3311. context, content->GetOriginalExpression(),
  3312. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  3313. "policies. Currently it may be used with the following policies:\n"
  3314. #define STRINGIFY_HELPER(X) #X
  3315. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  3316. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  3317. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  3318. #undef TARGET_POLICY_LIST_ITEM
  3319. );
  3320. return std::string();
  3321. }
  3322. } targetPolicyNode;
  3323. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  3324. {
  3325. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  3326. bool GeneratesContent() const override { return true; }
  3327. int NumExpectedParameters() const override { return 0; }
  3328. std::string Evaluate(
  3329. std::vector<std::string> const& /*parameters*/,
  3330. cmGeneratorExpressionContext* context,
  3331. GeneratorExpressionContent const* content,
  3332. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3333. {
  3334. reportError(context, content->GetOriginalExpression(),
  3335. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  3336. "should never be evaluated.");
  3337. return std::string();
  3338. }
  3339. } installPrefixNode;
  3340. class ArtifactDirTag;
  3341. class ArtifactLinkerTag;
  3342. class ArtifactLinkerLibraryTag;
  3343. class ArtifactLinkerImportTag;
  3344. class ArtifactNameTag;
  3345. class ArtifactImportTag;
  3346. class ArtifactPathTag;
  3347. class ArtifactPdbTag;
  3348. class ArtifactSonameTag;
  3349. class ArtifactSonameImportTag;
  3350. class ArtifactBundleDirTag;
  3351. class ArtifactBundleDirNameTag;
  3352. class ArtifactBundleContentDirTag;
  3353. template <typename ArtifactT, typename ComponentT>
  3354. struct TargetFilesystemArtifactDependency
  3355. {
  3356. static void AddDependency(cmGeneratorTarget* target,
  3357. cmGeneratorExpressionContext* context)
  3358. {
  3359. context->DependTargets.insert(target);
  3360. context->AllTargets.insert(target);
  3361. }
  3362. };
  3363. struct TargetFilesystemArtifactDependencyCMP0112
  3364. {
  3365. static void AddDependency(cmGeneratorTarget* target,
  3366. cmGeneratorExpressionContext* context)
  3367. {
  3368. context->AllTargets.insert(target);
  3369. cmLocalGenerator const* lg = context->LG;
  3370. switch (target->GetPolicyStatusCMP0112()) {
  3371. case cmPolicies::WARN:
  3372. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  3373. "CMAKE_POLICY_WARNING_CMP0112")) {
  3374. std::string err =
  3375. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  3376. "\nDependency being added to target:\n \"",
  3377. target->GetName(), "\"\n");
  3378. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  3379. err, context->Backtrace);
  3380. }
  3381. CM_FALLTHROUGH;
  3382. case cmPolicies::OLD:
  3383. context->DependTargets.insert(target);
  3384. break;
  3385. case cmPolicies::NEW:
  3386. break;
  3387. }
  3388. }
  3389. };
  3390. template <typename ArtifactT>
  3391. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  3392. : TargetFilesystemArtifactDependencyCMP0112
  3393. {
  3394. };
  3395. template <typename ArtifactT>
  3396. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  3397. : TargetFilesystemArtifactDependencyCMP0112
  3398. {
  3399. };
  3400. template <>
  3401. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  3402. ArtifactPathTag>
  3403. : TargetFilesystemArtifactDependencyCMP0112
  3404. {
  3405. };
  3406. template <>
  3407. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  3408. ArtifactPathTag>
  3409. : TargetFilesystemArtifactDependencyCMP0112
  3410. {
  3411. };
  3412. template <>
  3413. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  3414. ArtifactPathTag>
  3415. : TargetFilesystemArtifactDependencyCMP0112
  3416. {
  3417. };
  3418. template <typename ArtifactT>
  3419. struct TargetFilesystemArtifactResultCreator
  3420. {
  3421. static std::string Create(cmGeneratorTarget* target,
  3422. cmGeneratorExpressionContext* context,
  3423. GeneratorExpressionContent const* content);
  3424. };
  3425. template <>
  3426. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  3427. {
  3428. static std::string Create(cmGeneratorTarget* target,
  3429. cmGeneratorExpressionContext* context,
  3430. GeneratorExpressionContent const* content)
  3431. {
  3432. // The target soname file (.so.1).
  3433. if (target->IsDLLPlatform()) {
  3434. ::reportError(context, content->GetOriginalExpression(),
  3435. "TARGET_SONAME_FILE is not allowed "
  3436. "for DLL target platforms.");
  3437. return std::string();
  3438. }
  3439. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3440. ::reportError(context, content->GetOriginalExpression(),
  3441. "TARGET_SONAME_FILE is allowed only for "
  3442. "SHARED libraries.");
  3443. return std::string();
  3444. }
  3445. if (target->IsArchivedAIXSharedLibrary()) {
  3446. ::reportError(context, content->GetOriginalExpression(),
  3447. "TARGET_SONAME_FILE is not allowed for "
  3448. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3449. return std::string();
  3450. }
  3451. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  3452. target->GetSOName(context->Config));
  3453. return result;
  3454. }
  3455. };
  3456. template <>
  3457. struct TargetFilesystemArtifactResultCreator<ArtifactSonameImportTag>
  3458. {
  3459. static std::string Create(cmGeneratorTarget* target,
  3460. cmGeneratorExpressionContext* context,
  3461. GeneratorExpressionContent const* content)
  3462. {
  3463. // The target soname file (.so.1).
  3464. if (target->IsDLLPlatform()) {
  3465. ::reportError(context, content->GetOriginalExpression(),
  3466. "TARGET_SONAME_IMPORT_FILE is not allowed "
  3467. "for DLL target platforms.");
  3468. return std::string();
  3469. }
  3470. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3471. ::reportError(context, content->GetOriginalExpression(),
  3472. "TARGET_SONAME_IMPORT_FILE is allowed only for "
  3473. "SHARED libraries.");
  3474. return std::string();
  3475. }
  3476. if (target->IsArchivedAIXSharedLibrary()) {
  3477. ::reportError(context, content->GetOriginalExpression(),
  3478. "TARGET_SONAME_IMPORT_FILE is not allowed for "
  3479. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3480. return std::string();
  3481. }
  3482. if (target->HasImportLibrary(context->Config)) {
  3483. return cmStrCat(target->GetDirectory(
  3484. context->Config, cmStateEnums::ImportLibraryArtifact),
  3485. '/',
  3486. target->GetSOName(context->Config,
  3487. cmStateEnums::ImportLibraryArtifact));
  3488. }
  3489. return std::string{};
  3490. }
  3491. };
  3492. template <>
  3493. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  3494. {
  3495. static std::string Create(cmGeneratorTarget* target,
  3496. cmGeneratorExpressionContext* context,
  3497. GeneratorExpressionContent const* content)
  3498. {
  3499. if (target->IsImported()) {
  3500. ::reportError(context, content->GetOriginalExpression(),
  3501. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  3502. return std::string();
  3503. }
  3504. std::string language = target->GetLinkerLanguage(context->Config);
  3505. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3506. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3507. ::reportError(context, content->GetOriginalExpression(),
  3508. "TARGET_PDB_FILE is not supported by the target linker.");
  3509. return std::string();
  3510. }
  3511. cmStateEnums::TargetType targetType = target->GetType();
  3512. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3513. targetType != cmStateEnums::MODULE_LIBRARY &&
  3514. targetType != cmStateEnums::EXECUTABLE) {
  3515. ::reportError(context, content->GetOriginalExpression(),
  3516. "TARGET_PDB_FILE is allowed only for "
  3517. "targets with linker created artifacts.");
  3518. return std::string();
  3519. }
  3520. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  3521. '/', target->GetPDBName(context->Config));
  3522. return result;
  3523. }
  3524. };
  3525. template <>
  3526. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  3527. {
  3528. static std::string Create(cmGeneratorTarget* target,
  3529. cmGeneratorExpressionContext* context,
  3530. GeneratorExpressionContent const* content)
  3531. {
  3532. // The file used to link to the target (.so, .lib, .a) or import file
  3533. // (.lib, .tbd).
  3534. if (!target->IsLinkable()) {
  3535. ::reportError(context, content->GetOriginalExpression(),
  3536. "TARGET_LINKER_FILE is allowed only for libraries and "
  3537. "executables with ENABLE_EXPORTS.");
  3538. return std::string();
  3539. }
  3540. cmStateEnums::ArtifactType artifact =
  3541. target->HasImportLibrary(context->Config)
  3542. ? cmStateEnums::ImportLibraryArtifact
  3543. : cmStateEnums::RuntimeBinaryArtifact;
  3544. return target->GetFullPath(context->Config, artifact);
  3545. }
  3546. };
  3547. template <>
  3548. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerLibraryTag>
  3549. {
  3550. static std::string Create(cmGeneratorTarget* target,
  3551. cmGeneratorExpressionContext* context,
  3552. GeneratorExpressionContent const* content)
  3553. {
  3554. // The file used to link to the target (.dylib, .so, .a).
  3555. if (!target->IsLinkable() ||
  3556. target->GetType() == cmStateEnums::EXECUTABLE) {
  3557. ::reportError(context, content->GetOriginalExpression(),
  3558. "TARGET_LINKER_LIBRARY_FILE is allowed only for libraries "
  3559. "with ENABLE_EXPORTS.");
  3560. return std::string();
  3561. }
  3562. if (!target->IsDLLPlatform() ||
  3563. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3564. return target->GetFullPath(context->Config,
  3565. cmStateEnums::RuntimeBinaryArtifact);
  3566. }
  3567. return std::string{};
  3568. }
  3569. };
  3570. template <>
  3571. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerImportTag>
  3572. {
  3573. static std::string Create(cmGeneratorTarget* target,
  3574. cmGeneratorExpressionContext* context,
  3575. GeneratorExpressionContent const* content)
  3576. {
  3577. // The file used to link to the target (.lib, .tbd).
  3578. if (!target->IsLinkable()) {
  3579. ::reportError(
  3580. context, content->GetOriginalExpression(),
  3581. "TARGET_LINKER_IMPORT_FILE is allowed only for libraries and "
  3582. "executables with ENABLE_EXPORTS.");
  3583. return std::string();
  3584. }
  3585. if (target->HasImportLibrary(context->Config)) {
  3586. return target->GetFullPath(context->Config,
  3587. cmStateEnums::ImportLibraryArtifact);
  3588. }
  3589. return std::string{};
  3590. }
  3591. };
  3592. template <>
  3593. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  3594. {
  3595. static std::string Create(cmGeneratorTarget* target,
  3596. cmGeneratorExpressionContext* context,
  3597. GeneratorExpressionContent const* content)
  3598. {
  3599. if (target->IsImported()) {
  3600. ::reportError(context, content->GetOriginalExpression(),
  3601. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  3602. return std::string();
  3603. }
  3604. if (!target->IsBundleOnApple()) {
  3605. ::reportError(context, content->GetOriginalExpression(),
  3606. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  3607. return std::string();
  3608. }
  3609. std::string outpath = target->GetDirectory(context->Config) + '/';
  3610. return target->BuildBundleDirectory(outpath, context->Config,
  3611. cmGeneratorTarget::BundleDirLevel);
  3612. }
  3613. };
  3614. template <>
  3615. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  3616. {
  3617. static std::string Create(cmGeneratorTarget* target,
  3618. cmGeneratorExpressionContext* context,
  3619. GeneratorExpressionContent const* content)
  3620. {
  3621. if (target->IsImported()) {
  3622. ::reportError(
  3623. context, content->GetOriginalExpression(),
  3624. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  3625. return std::string();
  3626. }
  3627. if (!target->IsBundleOnApple()) {
  3628. ::reportError(
  3629. context, content->GetOriginalExpression(),
  3630. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  3631. return std::string();
  3632. }
  3633. auto level = cmGeneratorTarget::BundleDirLevel;
  3634. auto config = context->Config;
  3635. if (target->IsAppBundleOnApple()) {
  3636. return target->GetAppBundleDirectory(config, level);
  3637. }
  3638. if (target->IsFrameworkOnApple()) {
  3639. return target->GetFrameworkDirectory(config, level);
  3640. }
  3641. if (target->IsCFBundleOnApple()) {
  3642. return target->GetCFBundleDirectory(config, level);
  3643. }
  3644. return std::string();
  3645. }
  3646. };
  3647. template <>
  3648. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  3649. {
  3650. static std::string Create(cmGeneratorTarget* target,
  3651. cmGeneratorExpressionContext* context,
  3652. GeneratorExpressionContent const* content)
  3653. {
  3654. if (target->IsImported()) {
  3655. ::reportError(
  3656. context, content->GetOriginalExpression(),
  3657. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  3658. return std::string();
  3659. }
  3660. if (!target->IsBundleOnApple()) {
  3661. ::reportError(
  3662. context, content->GetOriginalExpression(),
  3663. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  3664. return std::string();
  3665. }
  3666. std::string outpath = target->GetDirectory(context->Config) + '/';
  3667. return target->BuildBundleDirectory(outpath, context->Config,
  3668. cmGeneratorTarget::ContentLevel);
  3669. }
  3670. };
  3671. template <>
  3672. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  3673. {
  3674. static std::string Create(cmGeneratorTarget* target,
  3675. cmGeneratorExpressionContext* context,
  3676. GeneratorExpressionContent const* /*unused*/)
  3677. {
  3678. return target->GetFullPath(context->Config,
  3679. cmStateEnums::RuntimeBinaryArtifact, true);
  3680. }
  3681. };
  3682. template <>
  3683. struct TargetFilesystemArtifactResultCreator<ArtifactImportTag>
  3684. {
  3685. static std::string Create(cmGeneratorTarget* target,
  3686. cmGeneratorExpressionContext* context,
  3687. GeneratorExpressionContent const* /*unused*/)
  3688. {
  3689. if (target->HasImportLibrary(context->Config)) {
  3690. return target->GetFullPath(context->Config,
  3691. cmStateEnums::ImportLibraryArtifact, true);
  3692. }
  3693. return std::string{};
  3694. }
  3695. };
  3696. template <typename ArtifactT>
  3697. struct TargetFilesystemArtifactResultGetter
  3698. {
  3699. static std::string Get(std::string const& result);
  3700. };
  3701. template <>
  3702. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  3703. {
  3704. static std::string Get(std::string const& result)
  3705. {
  3706. return cmSystemTools::GetFilenameName(result);
  3707. }
  3708. };
  3709. template <>
  3710. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  3711. {
  3712. static std::string Get(std::string const& result)
  3713. {
  3714. return cmSystemTools::GetFilenamePath(result);
  3715. }
  3716. };
  3717. template <>
  3718. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  3719. {
  3720. static std::string Get(std::string const& result) { return result; }
  3721. };
  3722. struct TargetArtifactBase : public cmGeneratorExpressionNode
  3723. {
  3724. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  3725. protected:
  3726. cmGeneratorTarget* GetTarget(
  3727. std::vector<std::string> const& parameters,
  3728. cmGeneratorExpressionContext* context,
  3729. GeneratorExpressionContent const* content,
  3730. cmGeneratorExpressionDAGChecker* dagChecker) const
  3731. {
  3732. // Lookup the referenced target.
  3733. std::string const& name = parameters.front();
  3734. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  3735. ::reportError(context, content->GetOriginalExpression(),
  3736. "Expression syntax not recognized.");
  3737. return nullptr;
  3738. }
  3739. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  3740. if (!target) {
  3741. ::reportError(context, content->GetOriginalExpression(),
  3742. "No target \"" + name + "\"");
  3743. return nullptr;
  3744. }
  3745. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  3746. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  3747. ::reportError(context, content->GetOriginalExpression(),
  3748. "Target \"" + name +
  3749. "\" is not an executable or library.");
  3750. return nullptr;
  3751. }
  3752. if (dagChecker &&
  3753. (dagChecker->EvaluatingLinkLibraries(target) ||
  3754. (dagChecker->EvaluatingSources() &&
  3755. target == dagChecker->TopTarget()))) {
  3756. ::reportError(context, content->GetOriginalExpression(),
  3757. "Expressions which require the linker language may not "
  3758. "be used while evaluating link libraries");
  3759. return nullptr;
  3760. }
  3761. return target;
  3762. }
  3763. };
  3764. template <typename ArtifactT, typename ComponentT>
  3765. struct TargetFilesystemArtifact : public TargetArtifactBase
  3766. {
  3767. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  3768. int NumExpectedParameters() const override { return 1; }
  3769. std::string Evaluate(
  3770. std::vector<std::string> const& parameters,
  3771. cmGeneratorExpressionContext* context,
  3772. GeneratorExpressionContent const* content,
  3773. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3774. {
  3775. cmGeneratorTarget* target =
  3776. this->GetTarget(parameters, context, content, dagChecker);
  3777. if (!target) {
  3778. return std::string();
  3779. }
  3780. // Not a dependent target if we are querying for ArtifactDirTag,
  3781. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  3782. // and ArtifactBundleContentDirTag
  3783. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  3784. target, context);
  3785. std::string result =
  3786. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  3787. content);
  3788. if (context->HadError) {
  3789. return std::string();
  3790. }
  3791. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  3792. }
  3793. };
  3794. template <typename ArtifactT>
  3795. struct TargetFilesystemArtifactNodeGroup
  3796. {
  3797. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  3798. {
  3799. }
  3800. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  3801. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  3802. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  3803. };
  3804. static TargetFilesystemArtifactNodeGroup<ArtifactNameTag> const
  3805. targetNodeGroup;
  3806. static TargetFilesystemArtifactNodeGroup<ArtifactImportTag> const
  3807. targetImportNodeGroup;
  3808. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag> const
  3809. targetLinkerNodeGroup;
  3810. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerLibraryTag> const
  3811. targetLinkerLibraryNodeGroup;
  3812. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerImportTag> const
  3813. targetLinkerImportNodeGroup;
  3814. static TargetFilesystemArtifactNodeGroup<ArtifactSonameTag> const
  3815. targetSoNameNodeGroup;
  3816. static TargetFilesystemArtifactNodeGroup<ArtifactSonameImportTag> const
  3817. targetSoNameImportNodeGroup;
  3818. static TargetFilesystemArtifactNodeGroup<ArtifactPdbTag> const
  3819. targetPdbNodeGroup;
  3820. static TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag> const
  3821. targetBundleDirNode;
  3822. static TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  3823. ArtifactNameTag> const targetBundleDirNameNode;
  3824. static TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  3825. ArtifactPathTag> const
  3826. targetBundleContentDirNode;
  3827. //
  3828. // To retrieve base name for various artifacts
  3829. //
  3830. template <typename ArtifactT>
  3831. struct TargetOutputNameArtifactResultGetter
  3832. {
  3833. static std::string Get(cmGeneratorTarget* target,
  3834. cmGeneratorExpressionContext* context,
  3835. GeneratorExpressionContent const* content);
  3836. };
  3837. template <>
  3838. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  3839. {
  3840. static std::string Get(cmGeneratorTarget* target,
  3841. cmGeneratorExpressionContext* context,
  3842. GeneratorExpressionContent const* /*unused*/)
  3843. {
  3844. return target->GetOutputName(context->Config,
  3845. cmStateEnums::RuntimeBinaryArtifact) +
  3846. target->GetFilePostfix(context->Config);
  3847. }
  3848. };
  3849. template <>
  3850. struct TargetOutputNameArtifactResultGetter<ArtifactImportTag>
  3851. {
  3852. static std::string Get(cmGeneratorTarget* target,
  3853. cmGeneratorExpressionContext* context,
  3854. GeneratorExpressionContent const* /*unused*/)
  3855. {
  3856. if (target->HasImportLibrary(context->Config)) {
  3857. return target->GetOutputName(context->Config,
  3858. cmStateEnums::ImportLibraryArtifact) +
  3859. target->GetFilePostfix(context->Config);
  3860. }
  3861. return std::string{};
  3862. }
  3863. };
  3864. template <>
  3865. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  3866. {
  3867. static std::string Get(cmGeneratorTarget* target,
  3868. cmGeneratorExpressionContext* context,
  3869. GeneratorExpressionContent const* content)
  3870. {
  3871. // The library file used to link to the target (.so, .lib, .a) or import
  3872. // file (.lin, .tbd).
  3873. if (!target->IsLinkable()) {
  3874. ::reportError(context, content->GetOriginalExpression(),
  3875. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  3876. "libraries and executables with ENABLE_EXPORTS.");
  3877. return std::string();
  3878. }
  3879. cmStateEnums::ArtifactType artifact =
  3880. target->HasImportLibrary(context->Config)
  3881. ? cmStateEnums::ImportLibraryArtifact
  3882. : cmStateEnums::RuntimeBinaryArtifact;
  3883. return target->GetOutputName(context->Config, artifact) +
  3884. target->GetFilePostfix(context->Config);
  3885. }
  3886. };
  3887. template <>
  3888. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerLibraryTag>
  3889. {
  3890. static std::string Get(cmGeneratorTarget* target,
  3891. cmGeneratorExpressionContext* context,
  3892. GeneratorExpressionContent const* content)
  3893. {
  3894. // The library file used to link to the target (.so, .lib, .a).
  3895. if (!target->IsLinkable() ||
  3896. target->GetType() == cmStateEnums::EXECUTABLE) {
  3897. ::reportError(context, content->GetOriginalExpression(),
  3898. "TARGET_LINKER_LIBRARY_FILE_BASE_NAME is allowed only for "
  3899. "libraries with ENABLE_EXPORTS.");
  3900. return std::string();
  3901. }
  3902. if (!target->IsDLLPlatform() ||
  3903. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3904. return target->GetOutputName(context->Config,
  3905. cmStateEnums::ImportLibraryArtifact) +
  3906. target->GetFilePostfix(context->Config);
  3907. }
  3908. return std::string{};
  3909. }
  3910. };
  3911. template <>
  3912. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerImportTag>
  3913. {
  3914. static std::string Get(cmGeneratorTarget* target,
  3915. cmGeneratorExpressionContext* context,
  3916. GeneratorExpressionContent const* content)
  3917. {
  3918. // The import file used to link to the target (.lib, .tbd).
  3919. if (!target->IsLinkable()) {
  3920. ::reportError(context, content->GetOriginalExpression(),
  3921. "TARGET_LINKER_IMPORT_FILE_BASE_NAME is allowed only for "
  3922. "libraries and executables with ENABLE_EXPORTS.");
  3923. return std::string();
  3924. }
  3925. if (target->HasImportLibrary(context->Config)) {
  3926. return target->GetOutputName(context->Config,
  3927. cmStateEnums::ImportLibraryArtifact) +
  3928. target->GetFilePostfix(context->Config);
  3929. }
  3930. return std::string{};
  3931. }
  3932. };
  3933. template <>
  3934. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  3935. {
  3936. static std::string Get(cmGeneratorTarget* target,
  3937. cmGeneratorExpressionContext* context,
  3938. GeneratorExpressionContent const* content)
  3939. {
  3940. if (target->IsImported()) {
  3941. ::reportError(
  3942. context, content->GetOriginalExpression(),
  3943. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  3944. return std::string();
  3945. }
  3946. std::string language = target->GetLinkerLanguage(context->Config);
  3947. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3948. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3949. ::reportError(
  3950. context, content->GetOriginalExpression(),
  3951. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  3952. return std::string();
  3953. }
  3954. cmStateEnums::TargetType targetType = target->GetType();
  3955. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3956. targetType != cmStateEnums::MODULE_LIBRARY &&
  3957. targetType != cmStateEnums::EXECUTABLE) {
  3958. ::reportError(context, content->GetOriginalExpression(),
  3959. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  3960. "targets with linker created artifacts.");
  3961. return std::string();
  3962. }
  3963. return target->GetPDBOutputName(context->Config);
  3964. }
  3965. };
  3966. template <typename ArtifactT>
  3967. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  3968. {
  3969. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  3970. int NumExpectedParameters() const override { return 1; }
  3971. std::string Evaluate(
  3972. std::vector<std::string> const& parameters,
  3973. cmGeneratorExpressionContext* context,
  3974. GeneratorExpressionContent const* content,
  3975. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3976. {
  3977. cmGeneratorTarget* target =
  3978. this->GetTarget(parameters, context, content, dagChecker);
  3979. if (!target) {
  3980. return std::string();
  3981. }
  3982. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  3983. target, context, content);
  3984. if (context->HadError) {
  3985. return std::string();
  3986. }
  3987. return result;
  3988. }
  3989. };
  3990. static TargetFileBaseNameArtifact<ArtifactNameTag> const
  3991. targetFileBaseNameNode;
  3992. static TargetFileBaseNameArtifact<ArtifactImportTag> const
  3993. targetImportFileBaseNameNode;
  3994. static TargetFileBaseNameArtifact<ArtifactLinkerTag> const
  3995. targetLinkerFileBaseNameNode;
  3996. static TargetFileBaseNameArtifact<ArtifactLinkerLibraryTag> const
  3997. targetLinkerLibraryFileBaseNameNode;
  3998. static TargetFileBaseNameArtifact<ArtifactLinkerImportTag> const
  3999. targetLinkerImportFileBaseNameNode;
  4000. static TargetFileBaseNameArtifact<ArtifactPdbTag> const
  4001. targetPdbFileBaseNameNode;
  4002. class ArtifactFilePrefixTag;
  4003. class ArtifactImportFilePrefixTag;
  4004. class ArtifactLinkerFilePrefixTag;
  4005. class ArtifactLinkerLibraryFilePrefixTag;
  4006. class ArtifactLinkerImportFilePrefixTag;
  4007. class ArtifactFileSuffixTag;
  4008. class ArtifactImportFileSuffixTag;
  4009. class ArtifactLinkerFileSuffixTag;
  4010. class ArtifactLinkerLibraryFileSuffixTag;
  4011. class ArtifactLinkerImportFileSuffixTag;
  4012. template <typename ArtifactT>
  4013. struct TargetFileArtifactResultGetter
  4014. {
  4015. static std::string Get(cmGeneratorTarget* target,
  4016. cmGeneratorExpressionContext* context,
  4017. GeneratorExpressionContent const* content);
  4018. };
  4019. template <>
  4020. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  4021. {
  4022. static std::string Get(cmGeneratorTarget* target,
  4023. cmGeneratorExpressionContext* context,
  4024. GeneratorExpressionContent const*)
  4025. {
  4026. return target->GetFilePrefix(context->Config);
  4027. }
  4028. };
  4029. template <>
  4030. struct TargetFileArtifactResultGetter<ArtifactImportFilePrefixTag>
  4031. {
  4032. static std::string Get(cmGeneratorTarget* target,
  4033. cmGeneratorExpressionContext* context,
  4034. GeneratorExpressionContent const*)
  4035. {
  4036. if (target->HasImportLibrary(context->Config)) {
  4037. return target->GetFilePrefix(context->Config,
  4038. cmStateEnums::ImportLibraryArtifact);
  4039. }
  4040. return std::string{};
  4041. }
  4042. };
  4043. template <>
  4044. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  4045. {
  4046. static std::string Get(cmGeneratorTarget* target,
  4047. cmGeneratorExpressionContext* context,
  4048. GeneratorExpressionContent const* content)
  4049. {
  4050. if (!target->IsLinkable()) {
  4051. ::reportError(
  4052. context, content->GetOriginalExpression(),
  4053. "TARGET_LINKER_FILE_PREFIX is allowed only for libraries and "
  4054. "executables with ENABLE_EXPORTS.");
  4055. return std::string();
  4056. }
  4057. cmStateEnums::ArtifactType artifact =
  4058. target->HasImportLibrary(context->Config)
  4059. ? cmStateEnums::ImportLibraryArtifact
  4060. : cmStateEnums::RuntimeBinaryArtifact;
  4061. return target->GetFilePrefix(context->Config, artifact);
  4062. }
  4063. };
  4064. template <>
  4065. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFilePrefixTag>
  4066. {
  4067. static std::string Get(cmGeneratorTarget* target,
  4068. cmGeneratorExpressionContext* context,
  4069. GeneratorExpressionContent const* content)
  4070. {
  4071. if (!target->IsLinkable() ||
  4072. target->GetType() == cmStateEnums::EXECUTABLE) {
  4073. ::reportError(
  4074. context, content->GetOriginalExpression(),
  4075. "TARGET_LINKER_LIBRARY_FILE_PREFIX is allowed only for libraries "
  4076. "with ENABLE_EXPORTS.");
  4077. return std::string();
  4078. }
  4079. if (!target->IsDLLPlatform() ||
  4080. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4081. return target->GetFilePrefix(context->Config,
  4082. cmStateEnums::RuntimeBinaryArtifact);
  4083. }
  4084. return std::string{};
  4085. }
  4086. };
  4087. template <>
  4088. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFilePrefixTag>
  4089. {
  4090. static std::string Get(cmGeneratorTarget* target,
  4091. cmGeneratorExpressionContext* context,
  4092. GeneratorExpressionContent const* content)
  4093. {
  4094. if (!target->IsLinkable()) {
  4095. ::reportError(
  4096. context, content->GetOriginalExpression(),
  4097. "TARGET_LINKER_IMPORT_FILE_PREFIX is allowed only for libraries and "
  4098. "executables with ENABLE_EXPORTS.");
  4099. return std::string();
  4100. }
  4101. if (target->HasImportLibrary(context->Config)) {
  4102. return target->GetFilePrefix(context->Config,
  4103. cmStateEnums::ImportLibraryArtifact);
  4104. }
  4105. return std::string{};
  4106. }
  4107. };
  4108. template <>
  4109. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  4110. {
  4111. static std::string Get(cmGeneratorTarget* target,
  4112. cmGeneratorExpressionContext* context,
  4113. GeneratorExpressionContent const*)
  4114. {
  4115. return target->GetFileSuffix(context->Config);
  4116. }
  4117. };
  4118. template <>
  4119. struct TargetFileArtifactResultGetter<ArtifactImportFileSuffixTag>
  4120. {
  4121. static std::string Get(cmGeneratorTarget* target,
  4122. cmGeneratorExpressionContext* context,
  4123. GeneratorExpressionContent const*)
  4124. {
  4125. if (target->HasImportLibrary(context->Config)) {
  4126. return target->GetFileSuffix(context->Config,
  4127. cmStateEnums::ImportLibraryArtifact);
  4128. }
  4129. return std::string{};
  4130. }
  4131. };
  4132. template <>
  4133. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  4134. {
  4135. static std::string Get(cmGeneratorTarget* target,
  4136. cmGeneratorExpressionContext* context,
  4137. GeneratorExpressionContent const* content)
  4138. {
  4139. if (!target->IsLinkable()) {
  4140. ::reportError(
  4141. context, content->GetOriginalExpression(),
  4142. "TARGET_LINKER_FILE_SUFFIX is allowed only for libraries and "
  4143. "executables with ENABLE_EXPORTS.");
  4144. return std::string();
  4145. }
  4146. cmStateEnums::ArtifactType artifact =
  4147. target->HasImportLibrary(context->Config)
  4148. ? cmStateEnums::ImportLibraryArtifact
  4149. : cmStateEnums::RuntimeBinaryArtifact;
  4150. return target->GetFileSuffix(context->Config, artifact);
  4151. }
  4152. };
  4153. template <>
  4154. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFileSuffixTag>
  4155. {
  4156. static std::string Get(cmGeneratorTarget* target,
  4157. cmGeneratorExpressionContext* context,
  4158. GeneratorExpressionContent const* content)
  4159. {
  4160. if (!target->IsLinkable() ||
  4161. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4162. ::reportError(context, content->GetOriginalExpression(),
  4163. "TARGET_LINKER_LIBRARY_FILE_SUFFIX is allowed only for "
  4164. "libraries with ENABLE_EXPORTS.");
  4165. return std::string();
  4166. }
  4167. if (!target->IsDLLPlatform() ||
  4168. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4169. return target->GetFileSuffix(context->Config,
  4170. cmStateEnums::RuntimeBinaryArtifact);
  4171. }
  4172. return std::string{};
  4173. }
  4174. };
  4175. template <>
  4176. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFileSuffixTag>
  4177. {
  4178. static std::string Get(cmGeneratorTarget* target,
  4179. cmGeneratorExpressionContext* context,
  4180. GeneratorExpressionContent const* content)
  4181. {
  4182. if (!target->IsLinkable()) {
  4183. ::reportError(
  4184. context, content->GetOriginalExpression(),
  4185. "TARGET_LINKER_IMPORT_FILE_SUFFIX is allowed only for libraries and "
  4186. "executables with ENABLE_EXPORTS.");
  4187. return std::string();
  4188. }
  4189. if (target->HasImportLibrary(context->Config)) {
  4190. return target->GetFileSuffix(context->Config,
  4191. cmStateEnums::ImportLibraryArtifact);
  4192. }
  4193. return std::string{};
  4194. }
  4195. };
  4196. template <typename ArtifactT>
  4197. struct TargetFileArtifact : public TargetArtifactBase
  4198. {
  4199. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  4200. int NumExpectedParameters() const override { return 1; }
  4201. std::string Evaluate(
  4202. std::vector<std::string> const& parameters,
  4203. cmGeneratorExpressionContext* context,
  4204. GeneratorExpressionContent const* content,
  4205. cmGeneratorExpressionDAGChecker* dagChecker) const override
  4206. {
  4207. cmGeneratorTarget* target =
  4208. this->GetTarget(parameters, context, content, dagChecker);
  4209. if (!target) {
  4210. return std::string();
  4211. }
  4212. std::string result =
  4213. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  4214. if (context->HadError) {
  4215. return std::string();
  4216. }
  4217. return result;
  4218. }
  4219. };
  4220. static TargetFileArtifact<ArtifactFilePrefixTag> const targetFilePrefixNode;
  4221. static TargetFileArtifact<ArtifactImportFilePrefixTag> const
  4222. targetImportFilePrefixNode;
  4223. static TargetFileArtifact<ArtifactLinkerFilePrefixTag> const
  4224. targetLinkerFilePrefixNode;
  4225. static TargetFileArtifact<ArtifactLinkerLibraryFilePrefixTag> const
  4226. targetLinkerLibraryFilePrefixNode;
  4227. static TargetFileArtifact<ArtifactLinkerImportFilePrefixTag> const
  4228. targetLinkerImportFilePrefixNode;
  4229. static TargetFileArtifact<ArtifactFileSuffixTag> const targetFileSuffixNode;
  4230. static TargetFileArtifact<ArtifactImportFileSuffixTag> const
  4231. targetImportFileSuffixNode;
  4232. static TargetFileArtifact<ArtifactLinkerFileSuffixTag> const
  4233. targetLinkerFileSuffixNode;
  4234. static TargetFileArtifact<ArtifactLinkerLibraryFileSuffixTag> const
  4235. targetLinkerLibraryFileSuffixNode;
  4236. static TargetFileArtifact<ArtifactLinkerImportFileSuffixTag> const
  4237. targetLinkerImportFileSuffixNode;
  4238. static const struct ShellPathNode : public cmGeneratorExpressionNode
  4239. {
  4240. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  4241. std::string Evaluate(
  4242. std::vector<std::string> const& parameters,
  4243. cmGeneratorExpressionContext* context,
  4244. GeneratorExpressionContent const* content,
  4245. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  4246. {
  4247. cmList list_in{ parameters.front() };
  4248. if (list_in.empty()) {
  4249. reportError(context, content->GetOriginalExpression(),
  4250. "\"\" is not an absolute path.");
  4251. return std::string();
  4252. }
  4253. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  4254. cmOutputConverter converter(snapshot);
  4255. char const* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  4256. std::vector<std::string> list_out;
  4257. list_out.reserve(list_in.size());
  4258. for (auto const& in : list_in) {
  4259. if (!cmSystemTools::FileIsFullPath(in)) {
  4260. reportError(context, content->GetOriginalExpression(),
  4261. "\"" + in + "\" is not an absolute path.");
  4262. return std::string();
  4263. }
  4264. list_out.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  4265. }
  4266. return cmJoin(list_out, separator);
  4267. }
  4268. } shellPathNode;
  4269. cmGeneratorExpressionNode const* cmGeneratorExpressionNode::GetNode(
  4270. std::string const& identifier)
  4271. {
  4272. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  4273. { "0", &zeroNode },
  4274. { "1", &oneNode },
  4275. { "AND", &andNode },
  4276. { "OR", &orNode },
  4277. { "NOT", &notNode },
  4278. { "C_COMPILER_ID", &cCompilerIdNode },
  4279. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  4280. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  4281. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  4282. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  4283. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  4284. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  4285. { "VERSION_GREATER", &versionGreaterNode },
  4286. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  4287. { "VERSION_LESS", &versionLessNode },
  4288. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  4289. { "VERSION_EQUAL", &versionEqualNode },
  4290. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  4291. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  4292. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  4293. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  4294. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  4295. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  4296. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  4297. { "C_COMPILER_FRONTEND_VARIANT", &cCompilerFrontendVariantNode },
  4298. { "CXX_COMPILER_FRONTEND_VARIANT", &cxxCompilerFrontendVariantNode },
  4299. { "CUDA_COMPILER_FRONTEND_VARIANT", &cudaCompilerFrontendVariantNode },
  4300. { "OBJC_COMPILER_FRONTEND_VARIANT", &objcCompilerFrontendVariantNode },
  4301. { "OBJCXX_COMPILER_FRONTEND_VARIANT", &objcxxCompilerFrontendVariantNode },
  4302. { "Fortran_COMPILER_FRONTEND_VARIANT",
  4303. &fortranCompilerFrontendVariantNode },
  4304. { "HIP_COMPILER_FRONTEND_VARIANT", &hipCompilerFrontendVariantNode },
  4305. { "PLATFORM_ID", &platformIdNode },
  4306. { "COMPILE_FEATURES", &compileFeaturesNode },
  4307. { "CONFIGURATION", &configurationNode },
  4308. { "CONFIG", &configurationTestNode },
  4309. { "TARGET_FILE", &targetNodeGroup.File },
  4310. { "TARGET_IMPORT_FILE", &targetImportNodeGroup.File },
  4311. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  4312. { "TARGET_LINKER_LIBRARY_FILE", &targetLinkerLibraryNodeGroup.File },
  4313. { "TARGET_LINKER_IMPORT_FILE", &targetLinkerImportNodeGroup.File },
  4314. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  4315. { "TARGET_SONAME_IMPORT_FILE", &targetSoNameImportNodeGroup.File },
  4316. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  4317. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  4318. { "TARGET_IMPORT_FILE_BASE_NAME", &targetImportFileBaseNameNode },
  4319. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  4320. { "TARGET_LINKER_LIBRARY_FILE_BASE_NAME",
  4321. &targetLinkerLibraryFileBaseNameNode },
  4322. { "TARGET_LINKER_IMPORT_FILE_BASE_NAME",
  4323. &targetLinkerImportFileBaseNameNode },
  4324. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  4325. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  4326. { "TARGET_IMPORT_FILE_PREFIX", &targetImportFilePrefixNode },
  4327. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  4328. { "TARGET_LINKER_LIBRARY_FILE_PREFIX",
  4329. &targetLinkerLibraryFilePrefixNode },
  4330. { "TARGET_LINKER_IMPORT_FILE_PREFIX", &targetLinkerImportFilePrefixNode },
  4331. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  4332. { "TARGET_IMPORT_FILE_SUFFIX", &targetImportFileSuffixNode },
  4333. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  4334. { "TARGET_LINKER_LIBRARY_FILE_SUFFIX",
  4335. &targetLinkerLibraryFileSuffixNode },
  4336. { "TARGET_LINKER_IMPORT_FILE_SUFFIX", &targetLinkerImportFileSuffixNode },
  4337. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  4338. { "TARGET_IMPORT_FILE_NAME", &targetImportNodeGroup.FileName },
  4339. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  4340. { "TARGET_LINKER_LIBRARY_FILE_NAME",
  4341. &targetLinkerLibraryNodeGroup.FileName },
  4342. { "TARGET_LINKER_IMPORT_FILE_NAME",
  4343. &targetLinkerImportNodeGroup.FileName },
  4344. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  4345. { "TARGET_SONAME_IMPORT_FILE_NAME",
  4346. &targetSoNameImportNodeGroup.FileName },
  4347. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  4348. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  4349. { "TARGET_IMPORT_FILE_DIR", &targetImportNodeGroup.FileDir },
  4350. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  4351. { "TARGET_LINKER_LIBRARY_FILE_DIR",
  4352. &targetLinkerLibraryNodeGroup.FileDir },
  4353. { "TARGET_LINKER_IMPORT_FILE_DIR", &targetLinkerImportNodeGroup.FileDir },
  4354. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  4355. { "TARGET_SONAME_IMPORT_FILE_DIR", &targetSoNameImportNodeGroup.FileDir },
  4356. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  4357. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  4358. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  4359. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  4360. { "STREQUAL", &strEqualNode },
  4361. { "EQUAL", &equalNode },
  4362. { "IN_LIST", &inListNode },
  4363. { "FILTER", &filterNode },
  4364. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  4365. { "LIST", &listNode },
  4366. { "LOWER_CASE", &lowerCaseNode },
  4367. { "UPPER_CASE", &upperCaseNode },
  4368. { "PATH", &pathNode },
  4369. { "PATH_EQUAL", &pathEqualNode },
  4370. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  4371. { "BOOL", &boolNode },
  4372. { "IF", &ifNode },
  4373. { "ANGLE-R", &angle_rNode },
  4374. { "COMMA", &commaNode },
  4375. { "SEMICOLON", &semicolonNode },
  4376. { "QUOTE", &quoteNode },
  4377. { "TARGET_PROPERTY", &targetPropertyNode },
  4378. { "TARGET_NAME", &targetNameNode },
  4379. { "TARGET_OBJECTS", &targetObjectsNode },
  4380. { "TARGET_POLICY", &targetPolicyNode },
  4381. { "TARGET_EXISTS", &targetExistsNode },
  4382. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  4383. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  4384. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  4385. { "TARGET_RUNTIME_DLL_DIRS", &targetRuntimeDllDirsNode },
  4386. { "GENEX_EVAL", &genexEvalNode },
  4387. { "BUILD_INTERFACE", &buildInterfaceNode },
  4388. { "INSTALL_INTERFACE", &installInterfaceNode },
  4389. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  4390. { "INSTALL_PREFIX", &installPrefixNode },
  4391. { "JOIN", &joinNode },
  4392. { "COMPILE_ONLY", &compileOnlyNode },
  4393. { "LINK_ONLY", &linkOnlyNode },
  4394. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  4395. { "COMPILE_LANGUAGE", &languageNode },
  4396. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  4397. { "LINK_LANGUAGE", &linkLanguageNode },
  4398. { "C_COMPILER_LINKER_ID", &cCompilerLinkerIdNode },
  4399. { "CXX_COMPILER_LINKER_ID", &cxxCompilerLinkerIdNode },
  4400. { "OBJC_COMPILER_LINKER_ID", &objcCompilerLinkerIdNode },
  4401. { "OBJCXX_COMPILER_LINKER_ID", &objcxxCompilerLinkerIdNode },
  4402. { "CUDA_COMPILER_LINKER_ID", &cudaCompilerLinkerIdNode },
  4403. { "Fortran_COMPILER_LINKER_ID", &fortranCompilerLinkerIdNode },
  4404. { "HIP_COMPILER_LINKER_ID", &hipCompilerLinkerIdNode },
  4405. { "C_COMPILER_LINKER_FRONTEND_VARIANT",
  4406. &cCompilerLinkerFrontendVariantNode },
  4407. { "CXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4408. &cxxCompilerLinkerFrontendVariantNode },
  4409. { "CUDA_COMPILER_LINKER_FRONTEND_VARIANT",
  4410. &cudaCompilerLinkerFrontendVariantNode },
  4411. { "OBJC_COMPILER_LINKER_FRONTEND_VARIANT",
  4412. &objcCompilerLinkerFrontendVariantNode },
  4413. { "OBJCXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4414. &objcxxCompilerLinkerFrontendVariantNode },
  4415. { "Fortran_COMPILER_LINKER_FRONTEND_VARIANT",
  4416. &fortranCompilerLinkerFrontendVariantNode },
  4417. { "HIP_COMPILER_LINKER_FRONTEND_VARIANT",
  4418. &hipCompilerLinkerFrontendVariantNode },
  4419. { "LINK_LIBRARY", &linkLibraryNode },
  4420. { "LINK_GROUP", &linkGroupNode },
  4421. { "HOST_LINK", &hostLinkNode },
  4422. { "DEVICE_LINK", &deviceLinkNode },
  4423. { "SHELL_PATH", &shellPathNode }
  4424. };
  4425. {
  4426. auto itr = nodeMap.find(identifier);
  4427. if (itr != nodeMap.end()) {
  4428. return itr->second;
  4429. }
  4430. }
  4431. return nullptr;
  4432. }
  4433. void reportError(cmGeneratorExpressionContext* context,
  4434. std::string const& expr, std::string const& result)
  4435. {
  4436. context->HadError = true;
  4437. if (context->Quiet) {
  4438. return;
  4439. }
  4440. std::ostringstream e;
  4441. /* clang-format off */
  4442. e << "Error evaluating generator expression:\n"
  4443. << " " << expr << "\n"
  4444. << result;
  4445. /* clang-format on */
  4446. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  4447. e.str(), context->Backtrace);
  4448. }