cmGeneratorExpressionNode.cxx 175 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837
  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. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  2034. if (!configValidator.find(parameters.front())) {
  2035. }
  2036. context->HadContextSensitiveCondition = true;
  2037. bool firstParam = true;
  2038. for (auto const& param : parameters) {
  2039. if (!configValidator.find(param)) {
  2040. if (firstParam) {
  2041. reportError(context, content->GetOriginalExpression(),
  2042. "Expression syntax not recognized.");
  2043. return std::string();
  2044. }
  2045. // for backwards compat invalid config names are only errors as
  2046. // the first parameter
  2047. std::ostringstream e;
  2048. /* clang-format off */
  2049. e << "Warning evaluating generator expression:\n"
  2050. << " " << content->GetOriginalExpression() << "\n"
  2051. << "The config name of \"" << param << "\" is invalid";
  2052. /* clang-format on */
  2053. context->LG->GetCMakeInstance()->IssueMessage(
  2054. MessageType::WARNING, e.str(), context->Backtrace);
  2055. }
  2056. firstParam = false;
  2057. if (context->Config.empty()) {
  2058. if (param.empty()) {
  2059. return "1";
  2060. }
  2061. } else if (cmsysString_strcasecmp(param.c_str(),
  2062. context->Config.c_str()) == 0) {
  2063. return "1";
  2064. }
  2065. }
  2066. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  2067. cmValue loc = nullptr;
  2068. cmValue imp = nullptr;
  2069. std::string suffix;
  2070. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  2071. imp, suffix)) {
  2072. // This imported target has an appropriate location
  2073. // for this (possibly mapped) config.
  2074. // Check if there is a proper config mapping for the tested config.
  2075. cmList mappedConfigs;
  2076. std::string mapProp = cmStrCat(
  2077. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  2078. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  2079. mappedConfigs.assign(cmSystemTools::UpperCase(*mapValue));
  2080. for (auto const& param : parameters) {
  2081. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  2082. return "1";
  2083. }
  2084. }
  2085. } else if (!suffix.empty()) {
  2086. // There is no explicit mapping for the tested config, so use
  2087. // the configuration of the imported location that was selected.
  2088. for (auto const& param : parameters) {
  2089. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  2090. return "1";
  2091. }
  2092. }
  2093. }
  2094. }
  2095. }
  2096. return "0";
  2097. }
  2098. } configurationTestNode;
  2099. static const struct JoinNode : public cmGeneratorExpressionNode
  2100. {
  2101. JoinNode() {} // NOLINT(modernize-use-equals-default)
  2102. int NumExpectedParameters() const override { return 2; }
  2103. bool AcceptsArbitraryContentParameter() const override { return true; }
  2104. std::string Evaluate(
  2105. std::vector<std::string> const& parameters,
  2106. cmGeneratorExpressionContext* /*context*/,
  2107. GeneratorExpressionContent const* /*content*/,
  2108. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2109. {
  2110. return cmList{ parameters.front() }.join(parameters[1]);
  2111. }
  2112. } joinNode;
  2113. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  2114. {
  2115. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2116. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2117. std::string Evaluate(
  2118. std::vector<std::string> const& parameters,
  2119. cmGeneratorExpressionContext* context,
  2120. GeneratorExpressionContent const* content,
  2121. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2122. {
  2123. if (context->Language.empty() &&
  2124. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  2125. reportError(
  2126. context, content->GetOriginalExpression(),
  2127. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  2128. "directories, compile definitions, compile options, and to evaluate "
  2129. "components of the file(GENERATE) command.");
  2130. return std::string();
  2131. }
  2132. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2133. std::string genName = gg->GetName();
  2134. if (genName.find("Makefiles") == std::string::npos &&
  2135. genName.find("Ninja") == std::string::npos &&
  2136. genName.find("Visual Studio") == std::string::npos &&
  2137. genName.find("Xcode") == std::string::npos &&
  2138. genName.find("Watcom WMake") == std::string::npos &&
  2139. genName.find("Green Hills MULTI") == std::string::npos) {
  2140. reportError(context, content->GetOriginalExpression(),
  2141. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  2142. return std::string();
  2143. }
  2144. if (parameters.empty()) {
  2145. return context->Language;
  2146. }
  2147. for (auto const& param : parameters) {
  2148. if (context->Language == param) {
  2149. return "1";
  2150. }
  2151. }
  2152. return "0";
  2153. }
  2154. } languageNode;
  2155. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  2156. {
  2157. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2158. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2159. std::string Evaluate(
  2160. std::vector<std::string> const& parameters,
  2161. cmGeneratorExpressionContext* context,
  2162. GeneratorExpressionContent const* content,
  2163. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2164. {
  2165. if (!context->HeadTarget ||
  2166. (context->Language.empty() &&
  2167. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  2168. // reportError(context, content->GetOriginalExpression(), "");
  2169. reportError(
  2170. context, content->GetOriginalExpression(),
  2171. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary "
  2172. "targets "
  2173. "to specify include directories, compile definitions, and compile "
  2174. "options. It may not be used with the add_custom_command, "
  2175. "add_custom_target, or file(GENERATE) commands.");
  2176. return std::string();
  2177. }
  2178. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2179. std::string genName = gg->GetName();
  2180. if (genName.find("Makefiles") == std::string::npos &&
  2181. genName.find("Ninja") == std::string::npos &&
  2182. genName.find("Visual Studio") == std::string::npos &&
  2183. genName.find("Xcode") == std::string::npos &&
  2184. genName.find("Watcom WMake") == std::string::npos &&
  2185. genName.find("Green Hills MULTI") == std::string::npos) {
  2186. reportError(
  2187. context, content->GetOriginalExpression(),
  2188. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  2189. return std::string();
  2190. }
  2191. std::string const& lang = context->Language;
  2192. if (lang == parameters.front()) {
  2193. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2194. parameters.cend());
  2195. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  2196. idParameter, context, content, dagChecker, lang);
  2197. }
  2198. return "0";
  2199. }
  2200. } languageAndIdNode;
  2201. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  2202. {
  2203. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2204. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2205. std::string Evaluate(
  2206. std::vector<std::string> const& parameters,
  2207. cmGeneratorExpressionContext* context,
  2208. GeneratorExpressionContent const* content,
  2209. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2210. {
  2211. if (!context->HeadTarget || !dagChecker ||
  2212. !(dagChecker->EvaluatingLinkExpression() ||
  2213. dagChecker->EvaluatingLinkLibraries() ||
  2214. dagChecker->EvaluatingLinkerLauncher())) {
  2215. reportError(context, content->GetOriginalExpression(),
  2216. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  2217. "to specify link libraries, link directories, link options "
  2218. "and link depends.");
  2219. return std::string();
  2220. }
  2221. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  2222. reportError(
  2223. context, content->GetOriginalExpression(),
  2224. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  2225. return std::string();
  2226. }
  2227. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2228. std::string genName = gg->GetName();
  2229. if (genName.find("Makefiles") == std::string::npos &&
  2230. genName.find("Ninja") == std::string::npos &&
  2231. genName.find("Visual Studio") == std::string::npos &&
  2232. genName.find("Xcode") == std::string::npos &&
  2233. genName.find("Watcom WMake") == std::string::npos &&
  2234. genName.find("Green Hills MULTI") == std::string::npos) {
  2235. reportError(context, content->GetOriginalExpression(),
  2236. "$<LINK_LANGUAGE:...> not supported for this generator.");
  2237. return std::string();
  2238. }
  2239. if (dagChecker->EvaluatingLinkLibraries()) {
  2240. context->HadHeadSensitiveCondition = true;
  2241. context->HadLinkLanguageSensitiveCondition = true;
  2242. }
  2243. if (parameters.empty()) {
  2244. return context->Language;
  2245. }
  2246. for (auto const& param : parameters) {
  2247. if (context->Language == param) {
  2248. return "1";
  2249. }
  2250. }
  2251. return "0";
  2252. }
  2253. } linkLanguageNode;
  2254. namespace {
  2255. struct LinkerId
  2256. {
  2257. static std::string Evaluate(std::vector<std::string> const& parameters,
  2258. cmGeneratorExpressionContext* context,
  2259. GeneratorExpressionContent const* content,
  2260. std::string const& lang)
  2261. {
  2262. std::string const& linkerId =
  2263. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  2264. "_COMPILER_ID");
  2265. if (parameters.empty()) {
  2266. return linkerId;
  2267. }
  2268. if (linkerId.empty()) {
  2269. return parameters.front().empty() ? "1" : "0";
  2270. }
  2271. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  2272. for (auto const& param : parameters) {
  2273. if (!linkerIdValidator.find(param)) {
  2274. reportError(context, content->GetOriginalExpression(),
  2275. "Expression syntax not recognized.");
  2276. return std::string();
  2277. }
  2278. if (param == linkerId) {
  2279. return "1";
  2280. }
  2281. }
  2282. return "0";
  2283. }
  2284. };
  2285. }
  2286. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  2287. {
  2288. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2289. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2290. std::string Evaluate(
  2291. std::vector<std::string> const& parameters,
  2292. cmGeneratorExpressionContext* context,
  2293. GeneratorExpressionContent const* content,
  2294. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2295. {
  2296. if (!context->HeadTarget || !dagChecker ||
  2297. !(dagChecker->EvaluatingLinkExpression() ||
  2298. dagChecker->EvaluatingLinkLibraries() ||
  2299. dagChecker->EvaluatingLinkerLauncher())) {
  2300. reportError(
  2301. context, content->GetOriginalExpression(),
  2302. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  2303. "to specify link libraries, link directories, link options, and "
  2304. "link "
  2305. "depends.");
  2306. return std::string();
  2307. }
  2308. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2309. std::string genName = gg->GetName();
  2310. if (genName.find("Makefiles") == std::string::npos &&
  2311. genName.find("Ninja") == std::string::npos &&
  2312. genName.find("Visual Studio") == std::string::npos &&
  2313. genName.find("Xcode") == std::string::npos &&
  2314. genName.find("Watcom WMake") == std::string::npos &&
  2315. genName.find("Green Hills MULTI") == std::string::npos) {
  2316. reportError(
  2317. context, content->GetOriginalExpression(),
  2318. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  2319. return std::string();
  2320. }
  2321. if (dagChecker->EvaluatingLinkLibraries()) {
  2322. context->HadHeadSensitiveCondition = true;
  2323. context->HadLinkLanguageSensitiveCondition = true;
  2324. }
  2325. std::string const& lang = context->Language;
  2326. if (lang == parameters.front()) {
  2327. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2328. parameters.cend());
  2329. return LinkerId::Evaluate(idParameter, context, content, lang);
  2330. }
  2331. return "0";
  2332. }
  2333. } linkLanguageAndIdNode;
  2334. struct CompilerLinkerIdNode : public cmGeneratorExpressionNode
  2335. {
  2336. CompilerLinkerIdNode(char const* lang)
  2337. : Language(lang)
  2338. {
  2339. }
  2340. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2341. std::string Evaluate(
  2342. std::vector<std::string> const& parameters,
  2343. cmGeneratorExpressionContext* context,
  2344. GeneratorExpressionContent const* content,
  2345. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2346. {
  2347. if (!context->HeadTarget) {
  2348. reportError(
  2349. context, content->GetOriginalExpression(),
  2350. cmStrCat(
  2351. "$<", this->Language,
  2352. "_COMPILER_LINKER_ID> may only be used with binary targets. It may "
  2353. "not be used with add_custom_command or add_custom_target."));
  2354. return {};
  2355. }
  2356. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2357. this->Language);
  2358. }
  2359. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2360. cmGeneratorExpressionContext* context,
  2361. GeneratorExpressionContent const* content,
  2362. cmGeneratorExpressionDAGChecker* /*unused*/,
  2363. std::string const& lang) const
  2364. {
  2365. std::string const& compilerLinkerId =
  2366. context->LG->GetMakefile()->GetSafeDefinition(
  2367. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_ID"));
  2368. if (parameters.empty()) {
  2369. return compilerLinkerId;
  2370. }
  2371. if (compilerLinkerId.empty()) {
  2372. return parameters.front().empty() ? "1" : "0";
  2373. }
  2374. static cmsys::RegularExpression compilerLinkerIdValidator(
  2375. "^[A-Za-z0-9_]*$");
  2376. for (auto const& param : parameters) {
  2377. if (!compilerLinkerIdValidator.find(param)) {
  2378. reportError(context, content->GetOriginalExpression(),
  2379. "Expression syntax not recognized.");
  2380. return std::string();
  2381. }
  2382. if (param == compilerLinkerId) {
  2383. return "1";
  2384. }
  2385. }
  2386. return "0";
  2387. }
  2388. char const* const Language;
  2389. };
  2390. static CompilerLinkerIdNode const cCompilerLinkerIdNode("C"),
  2391. cxxCompilerLinkerIdNode("CXX"), cudaCompilerLinkerIdNode("CUDA"),
  2392. objcCompilerLinkerIdNode("OBJC"), objcxxCompilerLinkerIdNode("OBJCXX"),
  2393. fortranCompilerLinkerIdNode("Fortran"), hipCompilerLinkerIdNode("HIP");
  2394. struct CompilerLinkerFrontendVariantNode : public cmGeneratorExpressionNode
  2395. {
  2396. CompilerLinkerFrontendVariantNode(char const* lang)
  2397. : Language(lang)
  2398. {
  2399. }
  2400. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2401. std::string Evaluate(
  2402. std::vector<std::string> const& parameters,
  2403. cmGeneratorExpressionContext* context,
  2404. GeneratorExpressionContent const* content,
  2405. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2406. {
  2407. if (!context->HeadTarget) {
  2408. reportError(
  2409. context, content->GetOriginalExpression(),
  2410. cmStrCat(
  2411. "$<", this->Language,
  2412. "_COMPILER_LINKER_FRONTEND_VARIANT> may only be used with binary "
  2413. "targets. It may not be used with add_custom_command or "
  2414. "add_custom_target."));
  2415. return {};
  2416. }
  2417. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2418. this->Language);
  2419. }
  2420. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2421. cmGeneratorExpressionContext* context,
  2422. GeneratorExpressionContent const* content,
  2423. cmGeneratorExpressionDAGChecker* /*unused*/,
  2424. std::string const& lang) const
  2425. {
  2426. std::string const& compilerLinkerFrontendVariant =
  2427. context->LG->GetMakefile()->GetSafeDefinition(
  2428. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_FRONTEND_VARIANT"));
  2429. if (parameters.empty()) {
  2430. return compilerLinkerFrontendVariant;
  2431. }
  2432. if (compilerLinkerFrontendVariant.empty()) {
  2433. return parameters.front().empty() ? "1" : "0";
  2434. }
  2435. static cmsys::RegularExpression compilerLinkerFrontendVariantValidator(
  2436. "^[A-Za-z0-9_]*$");
  2437. for (auto const& param : parameters) {
  2438. if (!compilerLinkerFrontendVariantValidator.find(param)) {
  2439. reportError(context, content->GetOriginalExpression(),
  2440. "Expression syntax not recognized.");
  2441. return {};
  2442. }
  2443. if (param == compilerLinkerFrontendVariant) {
  2444. return "1";
  2445. }
  2446. }
  2447. return "0";
  2448. }
  2449. char const* const Language;
  2450. };
  2451. static CompilerLinkerFrontendVariantNode const
  2452. cCompilerLinkerFrontendVariantNode("C"),
  2453. cxxCompilerLinkerFrontendVariantNode("CXX"),
  2454. cudaCompilerLinkerFrontendVariantNode("CUDA"),
  2455. objcCompilerLinkerFrontendVariantNode("OBJC"),
  2456. objcxxCompilerLinkerFrontendVariantNode("OBJCXX"),
  2457. fortranCompilerLinkerFrontendVariantNode("Fortran"),
  2458. hipCompilerLinkerFrontendVariantNode("HIP");
  2459. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  2460. {
  2461. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  2462. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2463. std::string Evaluate(
  2464. std::vector<std::string> const& parameters,
  2465. cmGeneratorExpressionContext* context,
  2466. GeneratorExpressionContent const* content,
  2467. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2468. {
  2469. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2470. if (!context->HeadTarget || !dagChecker ||
  2471. !dagChecker->EvaluatingLinkLibraries(nullptr,
  2472. ForGenex::LINK_LIBRARY)) {
  2473. reportError(context, content->GetOriginalExpression(),
  2474. "$<LINK_LIBRARY:...> may only be used with binary targets "
  2475. "to specify link libraries through 'LINK_LIBRARIES', "
  2476. "'INTERFACE_LINK_LIBRARIES', and "
  2477. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2478. return std::string();
  2479. }
  2480. cmList list{ parameters.begin(), parameters.end() };
  2481. if (list.empty()) {
  2482. reportError(
  2483. context, content->GetOriginalExpression(),
  2484. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  2485. return std::string();
  2486. }
  2487. if (list.size() == 1) {
  2488. // no libraries specified, ignore this genex
  2489. return std::string();
  2490. }
  2491. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2492. auto const& feature = list.front();
  2493. if (!featureNameValidator.find(feature)) {
  2494. reportError(context, content->GetOriginalExpression(),
  2495. cmStrCat("The feature name '", feature,
  2496. "' contains invalid characters."));
  2497. return std::string();
  2498. }
  2499. auto const LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  2500. auto const LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  2501. // filter out $<LINK_LIBRARY:..> tags with same feature
  2502. // and raise an error for any different feature
  2503. cm::erase_if(list, [&](std::string const& item) -> bool {
  2504. return item == LL_BEGIN || item == LL_END;
  2505. });
  2506. auto it =
  2507. std::find_if(list.cbegin() + 1, list.cend(),
  2508. [&feature](std::string const& item) -> bool {
  2509. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  2510. item.substr(14, item.find('>', 14) - 14) != feature;
  2511. });
  2512. if (it != list.cend()) {
  2513. reportError(
  2514. context, content->GetOriginalExpression(),
  2515. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  2516. return std::string();
  2517. }
  2518. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  2519. it = std::find_if(list.cbegin() + 1, list.cend(),
  2520. [](std::string const& item) -> bool {
  2521. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  2522. });
  2523. if (it != list.cend()) {
  2524. reportError(context, content->GetOriginalExpression(),
  2525. "$<LINK_GROUP:...> cannot be nested inside a "
  2526. "$<LINK_LIBRARY:...> expression.");
  2527. return std::string();
  2528. }
  2529. list.front() = LL_BEGIN;
  2530. list.push_back(LL_END);
  2531. return list.to_string();
  2532. }
  2533. } linkLibraryNode;
  2534. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  2535. {
  2536. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  2537. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2538. std::string Evaluate(
  2539. std::vector<std::string> const& parameters,
  2540. cmGeneratorExpressionContext* context,
  2541. GeneratorExpressionContent const* content,
  2542. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2543. {
  2544. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2545. if (!context->HeadTarget || !dagChecker ||
  2546. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  2547. reportError(
  2548. context, content->GetOriginalExpression(),
  2549. "$<LINK_GROUP:...> may only be used with binary targets "
  2550. "to specify group of link libraries through 'LINK_LIBRARIES', "
  2551. "'INTERFACE_LINK_LIBRARIES', and "
  2552. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2553. return std::string();
  2554. }
  2555. cmList list{ parameters.begin(), parameters.end() };
  2556. if (list.empty()) {
  2557. reportError(
  2558. context, content->GetOriginalExpression(),
  2559. "$<LINK_GROUP:...> expects a feature name as first argument.");
  2560. return std::string();
  2561. }
  2562. // $<LINK_GROUP:..> cannot be nested
  2563. if (std::find_if(list.cbegin(), list.cend(),
  2564. [](std::string const& item) -> bool {
  2565. return cmHasPrefix(item, "<LINK_GROUP"_s);
  2566. }) != list.cend()) {
  2567. reportError(context, content->GetOriginalExpression(),
  2568. "$<LINK_GROUP:...> cannot be nested.");
  2569. return std::string();
  2570. }
  2571. if (list.size() == 1) {
  2572. // no libraries specified, ignore this genex
  2573. return std::string();
  2574. }
  2575. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2576. auto const& feature = list.front();
  2577. if (!featureNameValidator.find(feature)) {
  2578. reportError(context, content->GetOriginalExpression(),
  2579. cmStrCat("The feature name '", feature,
  2580. "' contains invalid characters."));
  2581. return std::string();
  2582. }
  2583. auto const LG_BEGIN = cmStrCat(
  2584. "<LINK_GROUP:", feature, ':',
  2585. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  2586. "|"_s),
  2587. '>');
  2588. auto const LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  2589. list.front() = LG_BEGIN;
  2590. list.push_back(LG_END);
  2591. return list.to_string();
  2592. }
  2593. } linkGroupNode;
  2594. static const struct HostLinkNode : public cmGeneratorExpressionNode
  2595. {
  2596. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  2597. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2598. std::string Evaluate(
  2599. std::vector<std::string> const& parameters,
  2600. cmGeneratorExpressionContext* context,
  2601. GeneratorExpressionContent const* content,
  2602. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2603. {
  2604. if (!context->HeadTarget || !dagChecker ||
  2605. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2606. reportError(context, content->GetOriginalExpression(),
  2607. "$<HOST_LINK:...> may only be used with binary targets "
  2608. "to specify link options.");
  2609. return std::string();
  2610. }
  2611. return context->HeadTarget->IsDeviceLink() ? std::string()
  2612. : cmList::to_string(parameters);
  2613. }
  2614. } hostLinkNode;
  2615. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  2616. {
  2617. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  2618. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2619. std::string Evaluate(
  2620. std::vector<std::string> const& parameters,
  2621. cmGeneratorExpressionContext* context,
  2622. GeneratorExpressionContent const* content,
  2623. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2624. {
  2625. if (!context->HeadTarget || !dagChecker ||
  2626. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2627. reportError(context, content->GetOriginalExpression(),
  2628. "$<DEVICE_LINK:...> may only be used with binary targets "
  2629. "to specify link options.");
  2630. return std::string();
  2631. }
  2632. if (context->HeadTarget->IsDeviceLink()) {
  2633. cmList list{ parameters.begin(), parameters.end() };
  2634. auto const DL_BEGIN = "<DEVICE_LINK>"_s;
  2635. auto const DL_END = "</DEVICE_LINK>"_s;
  2636. cm::erase_if(list, [&](std::string const& item) {
  2637. return item == DL_BEGIN || item == DL_END;
  2638. });
  2639. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  2640. list.push_back(static_cast<std::string>(DL_END));
  2641. return list.to_string();
  2642. }
  2643. return std::string();
  2644. }
  2645. } deviceLinkNode;
  2646. static std::string getLinkedTargetsContent(
  2647. cmGeneratorTarget const* target, std::string const& prop,
  2648. cmGeneratorExpressionContext* context,
  2649. cmGeneratorExpressionDAGChecker* dagChecker, cmGeneratorTarget::UseTo usage)
  2650. {
  2651. std::string result;
  2652. if (cmLinkImplementationLibraries const* impl =
  2653. target->GetLinkImplementationLibraries(
  2654. context->Config, cmGeneratorTarget::UseTo::Compile)) {
  2655. for (cmLinkImplItem const& lib : impl->Libraries) {
  2656. if (lib.Target) {
  2657. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  2658. // caller's property and hand-evaluate it as if it were compiled.
  2659. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  2660. cmGeneratorExpressionContext libContext(
  2661. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  2662. target, context->EvaluateForBuildsystem, lib.Backtrace,
  2663. context->Language);
  2664. std::string libResult = lib.Target->EvaluateInterfaceProperty(
  2665. prop, &libContext, dagChecker, usage);
  2666. if (!libResult.empty()) {
  2667. if (result.empty()) {
  2668. result = std::move(libResult);
  2669. } else {
  2670. result.reserve(result.size() + 1 + libResult.size());
  2671. result += ";";
  2672. result += libResult;
  2673. }
  2674. }
  2675. }
  2676. }
  2677. }
  2678. return result;
  2679. }
  2680. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  2681. {
  2682. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  2683. // This node handles errors on parameter count itself.
  2684. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2685. static char const* GetErrorText(std::string const& targetName,
  2686. std::string const& propertyName)
  2687. {
  2688. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2689. if (targetName.empty() && propertyName.empty()) {
  2690. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2691. "target name and property name.";
  2692. }
  2693. if (targetName.empty()) {
  2694. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2695. "target name.";
  2696. }
  2697. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  2698. if (!propertyNameValidator.find(propertyName)) {
  2699. return "Target name and property name not supported.";
  2700. }
  2701. return "Target name not supported.";
  2702. }
  2703. return nullptr;
  2704. }
  2705. std::string Evaluate(
  2706. std::vector<std::string> const& parameters,
  2707. cmGeneratorExpressionContext* context,
  2708. GeneratorExpressionContent const* content,
  2709. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  2710. {
  2711. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2712. cmGeneratorTarget const* target = nullptr;
  2713. std::string targetName;
  2714. std::string propertyName;
  2715. if (parameters.size() == 2) {
  2716. targetName = parameters[0];
  2717. propertyName = parameters[1];
  2718. if (char const* e = GetErrorText(targetName, propertyName)) {
  2719. reportError(context, content->GetOriginalExpression(), e);
  2720. return std::string();
  2721. }
  2722. if (propertyName == "ALIASED_TARGET"_s) {
  2723. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2724. if (cmGeneratorTarget* tgt =
  2725. context->LG->FindGeneratorTargetToUse(targetName)) {
  2726. return tgt->GetName();
  2727. }
  2728. }
  2729. return std::string();
  2730. }
  2731. if (propertyName == "ALIAS_GLOBAL"_s) {
  2732. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2733. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  2734. ? "TRUE"
  2735. : "FALSE";
  2736. }
  2737. return std::string();
  2738. }
  2739. cmLocalGenerator const* lg = context->CurrentTarget
  2740. ? context->CurrentTarget->GetLocalGenerator()
  2741. : context->LG;
  2742. target = lg->FindGeneratorTargetToUse(targetName);
  2743. if (!target) {
  2744. std::ostringstream e;
  2745. e << "Target \"" << targetName << "\" not found.";
  2746. reportError(context, content->GetOriginalExpression(), e.str());
  2747. return std::string();
  2748. }
  2749. context->AllTargets.insert(target);
  2750. } else if (parameters.size() == 1) {
  2751. target = context->HeadTarget;
  2752. propertyName = parameters[0];
  2753. // Keep track of the properties seen while processing.
  2754. // The evaluation of the LINK_LIBRARIES generator expressions
  2755. // will check this to ensure that properties have one consistent
  2756. // value for all evaluations.
  2757. context->SeenTargetProperties.insert(propertyName);
  2758. context->HadHeadSensitiveCondition = true;
  2759. if (!target) {
  2760. reportError(
  2761. context, content->GetOriginalExpression(),
  2762. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  2763. "It may not be used with add_custom_command or add_custom_target. "
  2764. " "
  2765. " "
  2766. "Specify the target to read a property from using the "
  2767. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  2768. return std::string();
  2769. }
  2770. } else {
  2771. reportError(
  2772. context, content->GetOriginalExpression(),
  2773. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  2774. return std::string();
  2775. }
  2776. if (propertyName == "SOURCES") {
  2777. context->SourceSensitiveTargets.insert(target);
  2778. }
  2779. if (propertyName.empty()) {
  2780. reportError(
  2781. context, content->GetOriginalExpression(),
  2782. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  2783. "name.");
  2784. return std::string();
  2785. }
  2786. if (!propertyNameValidator.find(propertyName)) {
  2787. ::reportError(context, content->GetOriginalExpression(),
  2788. "Property name not supported.");
  2789. return std::string();
  2790. }
  2791. assert(target);
  2792. if (propertyName == "LINKER_LANGUAGE") {
  2793. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  2794. (dagCheckerParent->EvaluatingLinkLibraries() ||
  2795. dagCheckerParent->EvaluatingSources())) {
  2796. reportError(
  2797. context, content->GetOriginalExpression(),
  2798. "LINKER_LANGUAGE target property can not be used while evaluating "
  2799. "link libraries for a static library");
  2800. return std::string();
  2801. }
  2802. return target->GetLinkerLanguage(context->Config);
  2803. }
  2804. bool const evaluatingLinkLibraries =
  2805. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries();
  2806. std::string interfacePropertyName;
  2807. bool isInterfaceProperty = false;
  2808. cmGeneratorTarget::UseTo usage = cmGeneratorTarget::UseTo::Compile;
  2809. if (cm::optional<cmGeneratorTarget::TransitiveProperty> transitiveProp =
  2810. target->IsTransitiveProperty(propertyName, context->LG,
  2811. context->Config, dagCheckerParent)) {
  2812. interfacePropertyName = std::string(transitiveProp->InterfaceName);
  2813. isInterfaceProperty = transitiveProp->InterfaceName == propertyName;
  2814. usage = transitiveProp->Usage;
  2815. }
  2816. if (dagCheckerParent) {
  2817. // This $<TARGET_PROPERTY:...> node has been reached while evaluating
  2818. // another target property value. Check that the outermost evaluation
  2819. // expects such nested evaluations.
  2820. if (dagCheckerParent->EvaluatingGenexExpression() ||
  2821. dagCheckerParent->EvaluatingPICExpression() ||
  2822. dagCheckerParent->EvaluatingLinkerLauncher()) {
  2823. // No check required.
  2824. } else if (evaluatingLinkLibraries) {
  2825. if (!interfacePropertyName.empty()) {
  2826. reportError(
  2827. context, content->GetOriginalExpression(),
  2828. "$<TARGET_PROPERTY:...> expression in link libraries "
  2829. "evaluation depends on target property which is transitive "
  2830. "over the link libraries, creating a recursion.");
  2831. return std::string();
  2832. }
  2833. } else {
  2834. assert(dagCheckerParent->EvaluatingTransitiveProperty());
  2835. }
  2836. }
  2837. if (isInterfaceProperty) {
  2838. return cmGeneratorExpression::StripEmptyListElements(
  2839. target->EvaluateInterfaceProperty(propertyName, context,
  2840. dagCheckerParent, usage));
  2841. }
  2842. cmGeneratorExpressionDAGChecker dagChecker{
  2843. target,
  2844. propertyName,
  2845. content,
  2846. dagCheckerParent,
  2847. context->LG,
  2848. context->Config,
  2849. context->Backtrace,
  2850. };
  2851. switch (dagChecker.Check()) {
  2852. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  2853. dagChecker.ReportError(context, content->GetOriginalExpression());
  2854. return std::string();
  2855. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  2856. // No error. We just skip cyclic references.
  2857. return std::string();
  2858. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  2859. // We handle transitive properties above. For non-transitive
  2860. // properties we accept repeats anyway.
  2861. case cmGeneratorExpressionDAGChecker::DAG:
  2862. break;
  2863. }
  2864. std::string result;
  2865. bool haveProp = false;
  2866. if (cmValue p = target->GetProperty(propertyName)) {
  2867. result = *p;
  2868. haveProp = true;
  2869. } else if (evaluatingLinkLibraries) {
  2870. return std::string();
  2871. }
  2872. // Properties named by COMPATIBLE_INTERFACE_ properties combine over
  2873. // the transitive link closure as a single order-independent value.
  2874. // Imported targets do not themselves have a defined value for these
  2875. // properties, but they can contribute to the value of a non-imported
  2876. // dependent.
  2877. //
  2878. // For COMPATIBLE_INTERFACE_{BOOL,STRING}:
  2879. // * If set on this target, use the value directly. It is checked
  2880. // elsewhere for consistency over the transitive link closure.
  2881. // * If not set on this target, compute the value from the closure.
  2882. //
  2883. // For COMPATIBLE_INTERFACE_NUMBER_{MAX,MIN} we always compute the value
  2884. // from this target and the transitive link closure to get the max or min.
  2885. if (!haveProp && !target->IsImported()) {
  2886. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  2887. context->Config)) {
  2888. context->HadContextSensitiveCondition = true;
  2889. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  2890. context->Config)
  2891. ? "1"
  2892. : "0";
  2893. }
  2894. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  2895. context->Config)) {
  2896. context->HadContextSensitiveCondition = true;
  2897. char const* propContent =
  2898. target->GetLinkInterfaceDependentStringProperty(propertyName,
  2899. context->Config);
  2900. return propContent ? propContent : "";
  2901. }
  2902. }
  2903. if (!evaluatingLinkLibraries && !target->IsImported()) {
  2904. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  2905. context->Config)) {
  2906. context->HadContextSensitiveCondition = true;
  2907. char const* propContent =
  2908. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  2909. context->Config);
  2910. return propContent ? propContent : "";
  2911. }
  2912. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  2913. context->Config)) {
  2914. context->HadContextSensitiveCondition = true;
  2915. char const* propContent =
  2916. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  2917. context->Config);
  2918. return propContent ? propContent : "";
  2919. }
  2920. }
  2921. // Some properties, such as usage requirements, combine over the
  2922. // transitive link closure as an ordered list.
  2923. if (!interfacePropertyName.empty()) {
  2924. result = cmGeneratorExpression::StripEmptyListElements(
  2925. this->EvaluateDependentExpression(result, context->LG, context, target,
  2926. &dagChecker, target));
  2927. std::string linkedTargetsContent = getLinkedTargetsContent(
  2928. target, interfacePropertyName, context, &dagChecker, usage);
  2929. if (!linkedTargetsContent.empty()) {
  2930. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  2931. }
  2932. }
  2933. return result;
  2934. }
  2935. } targetPropertyNode;
  2936. static const struct TargetNameNode : public cmGeneratorExpressionNode
  2937. {
  2938. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  2939. bool GeneratesContent() const override { return true; }
  2940. bool AcceptsArbitraryContentParameter() const override { return true; }
  2941. bool RequiresLiteralInput() const override { return true; }
  2942. std::string Evaluate(
  2943. std::vector<std::string> const& parameters,
  2944. cmGeneratorExpressionContext* /*context*/,
  2945. GeneratorExpressionContent const* /*content*/,
  2946. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2947. {
  2948. return parameters.front();
  2949. }
  2950. int NumExpectedParameters() const override { return 1; }
  2951. } targetNameNode;
  2952. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  2953. {
  2954. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  2955. std::string Evaluate(
  2956. std::vector<std::string> const& parameters,
  2957. cmGeneratorExpressionContext* context,
  2958. GeneratorExpressionContent const* content,
  2959. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2960. {
  2961. std::string const& tgtName = parameters.front();
  2962. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2963. if (!gt) {
  2964. std::ostringstream e;
  2965. e << "Objects of target \"" << tgtName
  2966. << "\" referenced but no such target exists.";
  2967. reportError(context, content->GetOriginalExpression(), e.str());
  2968. return std::string();
  2969. }
  2970. cmStateEnums::TargetType type = gt->GetType();
  2971. if (type != cmStateEnums::EXECUTABLE &&
  2972. type != cmStateEnums::STATIC_LIBRARY &&
  2973. type != cmStateEnums::SHARED_LIBRARY &&
  2974. type != cmStateEnums::MODULE_LIBRARY &&
  2975. type != cmStateEnums::OBJECT_LIBRARY) {
  2976. std::ostringstream e;
  2977. e << "Objects of target \"" << tgtName
  2978. << "\" referenced but is not one of the allowed target types "
  2979. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2980. reportError(context, content->GetOriginalExpression(), e.str());
  2981. return std::string();
  2982. }
  2983. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2984. {
  2985. std::string reason;
  2986. if (!context->EvaluateForBuildsystem &&
  2987. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2988. std::ostringstream e;
  2989. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2990. "is only suitable for consumption by CMake (limited"
  2991. << reason
  2992. << "). "
  2993. "It is not suitable for writing out elsewhere.";
  2994. reportError(context, content->GetOriginalExpression(), e.str());
  2995. return std::string();
  2996. }
  2997. }
  2998. cmList objects;
  2999. if (gt->IsImported()) {
  3000. cmValue loc = nullptr;
  3001. cmValue imp = nullptr;
  3002. std::string suffix;
  3003. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  3004. objects.assign(*loc);
  3005. }
  3006. context->HadContextSensitiveCondition = true;
  3007. } else {
  3008. gt->GetTargetObjectNames(context->Config, objects);
  3009. std::string obj_dir;
  3010. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  3011. // Use object file directory with buildsystem placeholder.
  3012. obj_dir = gt->ObjectDirectory;
  3013. context->HadContextSensitiveCondition =
  3014. gt->HasContextDependentSources();
  3015. } else {
  3016. // Use object file directory with per-config location.
  3017. obj_dir = gt->GetObjectDirectory(context->Config);
  3018. context->HadContextSensitiveCondition = true;
  3019. }
  3020. for (auto& o : objects) {
  3021. o = cmStrCat(obj_dir, o);
  3022. }
  3023. }
  3024. // Create the cmSourceFile instances in the referencing directory.
  3025. cmMakefile* mf = context->LG->GetMakefile();
  3026. for (std::string const& o : objects) {
  3027. mf->AddTargetObject(tgtName, o);
  3028. }
  3029. return objects.to_string();
  3030. }
  3031. } targetObjectsNode;
  3032. struct TargetRuntimeDllsBaseNode : public cmGeneratorExpressionNode
  3033. {
  3034. std::vector<std::string> CollectDlls(
  3035. std::vector<std::string> const& parameters,
  3036. cmGeneratorExpressionContext* context,
  3037. GeneratorExpressionContent const* content) const
  3038. {
  3039. std::string const& tgtName = parameters.front();
  3040. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  3041. if (!gt) {
  3042. std::ostringstream e;
  3043. e << "Objects of target \"" << tgtName
  3044. << "\" referenced but no such target exists.";
  3045. reportError(context, content->GetOriginalExpression(), e.str());
  3046. return std::vector<std::string>();
  3047. }
  3048. cmStateEnums::TargetType type = gt->GetType();
  3049. if (type != cmStateEnums::EXECUTABLE &&
  3050. type != cmStateEnums::SHARED_LIBRARY &&
  3051. type != cmStateEnums::MODULE_LIBRARY) {
  3052. std::ostringstream e;
  3053. e << "Objects of target \"" << tgtName
  3054. << "\" referenced but is not one of the allowed target types "
  3055. << "(EXECUTABLE, SHARED, MODULE).";
  3056. reportError(context, content->GetOriginalExpression(), e.str());
  3057. return std::vector<std::string>();
  3058. }
  3059. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  3060. std::vector<std::string> dllPaths;
  3061. auto const& dlls = cli->GetRuntimeDLLs();
  3062. for (auto const& dll : dlls) {
  3063. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  3064. dllPaths.emplace_back(*loc);
  3065. }
  3066. }
  3067. return dllPaths;
  3068. }
  3069. return std::vector<std::string>();
  3070. }
  3071. };
  3072. static const struct TargetRuntimeDllsNode : public TargetRuntimeDllsBaseNode
  3073. {
  3074. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  3075. std::string Evaluate(
  3076. std::vector<std::string> const& parameters,
  3077. cmGeneratorExpressionContext* context,
  3078. GeneratorExpressionContent const* content,
  3079. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3080. {
  3081. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3082. return cmList::to_string(dlls);
  3083. }
  3084. } targetRuntimeDllsNode;
  3085. static const struct TargetRuntimeDllDirsNode : public TargetRuntimeDllsBaseNode
  3086. {
  3087. TargetRuntimeDllDirsNode() {} // NOLINT(modernize-use-equals-default)
  3088. std::string Evaluate(
  3089. std::vector<std::string> const& parameters,
  3090. cmGeneratorExpressionContext* context,
  3091. GeneratorExpressionContent const* content,
  3092. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3093. {
  3094. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3095. std::vector<std::string> dllDirs;
  3096. for (std::string const& dll : dlls) {
  3097. std::string directory = cmSystemTools::GetFilenamePath(dll);
  3098. if (std::find(dllDirs.begin(), dllDirs.end(), directory) ==
  3099. dllDirs.end()) {
  3100. dllDirs.push_back(directory);
  3101. }
  3102. }
  3103. return cmList::to_string(dllDirs);
  3104. }
  3105. } targetRuntimeDllDirsNode;
  3106. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  3107. {
  3108. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  3109. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  3110. std::string Evaluate(
  3111. std::vector<std::string> const& parameters,
  3112. cmGeneratorExpressionContext* context,
  3113. GeneratorExpressionContent const* content,
  3114. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3115. {
  3116. cmGeneratorTarget const* target = context->HeadTarget;
  3117. if (!target) {
  3118. reportError(
  3119. context, content->GetOriginalExpression(),
  3120. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  3121. "not be used with add_custom_command or add_custom_target.");
  3122. return std::string();
  3123. }
  3124. context->HadHeadSensitiveCondition = true;
  3125. using LangMap = std::map<std::string, cmList>;
  3126. static LangMap availableFeatures;
  3127. LangMap testedFeatures;
  3128. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  3129. for (std::string const& p : parameters) {
  3130. std::string error;
  3131. std::string lang;
  3132. if (!standardResolver.CompileFeatureKnown(
  3133. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  3134. reportError(context, content->GetOriginalExpression(), error);
  3135. return std::string();
  3136. }
  3137. testedFeatures[lang].push_back(p);
  3138. if (availableFeatures.find(lang) == availableFeatures.end()) {
  3139. cmValue featuresKnown =
  3140. standardResolver.CompileFeaturesAvailable(lang, &error);
  3141. if (!featuresKnown) {
  3142. reportError(context, content->GetOriginalExpression(), error);
  3143. return std::string();
  3144. }
  3145. availableFeatures[lang].assign(featuresKnown);
  3146. }
  3147. }
  3148. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  3149. for (auto const& lit : testedFeatures) {
  3150. std::vector<std::string> const& langAvailable =
  3151. availableFeatures[lit.first];
  3152. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  3153. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  3154. for (std::string const& it : lit.second) {
  3155. if (!cm::contains(langAvailable, it)) {
  3156. return "0";
  3157. }
  3158. if (standardDefault && standardDefault->empty()) {
  3159. // This compiler has no notion of language standard levels.
  3160. // All features known for the language are always available.
  3161. continue;
  3162. }
  3163. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  3164. context->Config, it)) {
  3165. if (evalLL) {
  3166. cmValue l =
  3167. target->GetLanguageStandard(lit.first, context->Config);
  3168. if (!l) {
  3169. l = standardDefault;
  3170. }
  3171. assert(l);
  3172. context->MaxLanguageStandard[target][lit.first] = *l;
  3173. } else {
  3174. return "0";
  3175. }
  3176. }
  3177. }
  3178. }
  3179. return "1";
  3180. }
  3181. } compileFeaturesNode;
  3182. static char const* targetPolicyWhitelist[] = {
  3183. nullptr
  3184. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  3185. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  3186. #undef TARGET_POLICY_STRING
  3187. };
  3188. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  3189. char const* policy)
  3190. {
  3191. #define RETURN_POLICY(POLICY) \
  3192. if (strcmp(policy, #POLICY) == 0) { \
  3193. return tgt->GetPolicyStatus##POLICY(); \
  3194. }
  3195. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  3196. #undef RETURN_POLICY
  3197. assert(false && "Unreachable code. Not a valid policy");
  3198. return cmPolicies::WARN;
  3199. }
  3200. static cmPolicies::PolicyID policyForString(char const* policy_id)
  3201. {
  3202. #define RETURN_POLICY_ID(POLICY_ID) \
  3203. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  3204. return cmPolicies::POLICY_ID; \
  3205. }
  3206. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  3207. #undef RETURN_POLICY_ID
  3208. assert(false && "Unreachable code. Not a valid policy");
  3209. return cmPolicies::CMPCOUNT;
  3210. }
  3211. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  3212. {
  3213. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  3214. int NumExpectedParameters() const override { return 1; }
  3215. std::string Evaluate(
  3216. std::vector<std::string> const& parameters,
  3217. cmGeneratorExpressionContext* context,
  3218. GeneratorExpressionContent const* content,
  3219. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3220. {
  3221. if (!context->HeadTarget) {
  3222. reportError(
  3223. context, content->GetOriginalExpression(),
  3224. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  3225. "may not be used with add_custom_command or add_custom_target.");
  3226. return std::string();
  3227. }
  3228. context->HadContextSensitiveCondition = true;
  3229. context->HadHeadSensitiveCondition = true;
  3230. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  3231. char const* policy = targetPolicyWhitelist[i];
  3232. if (parameters.front() == policy) {
  3233. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  3234. switch (statusForTarget(context->HeadTarget, policy)) {
  3235. case cmPolicies::WARN:
  3236. lg->IssueMessage(
  3237. MessageType::AUTHOR_WARNING,
  3238. cmPolicies::GetPolicyWarning(policyForString(policy)));
  3239. CM_FALLTHROUGH;
  3240. case cmPolicies::OLD:
  3241. return "0";
  3242. case cmPolicies::NEW:
  3243. return "1";
  3244. }
  3245. }
  3246. }
  3247. reportError(
  3248. context, content->GetOriginalExpression(),
  3249. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  3250. "policies. Currently it may be used with the following policies:\n"
  3251. #define STRINGIFY_HELPER(X) #X
  3252. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  3253. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  3254. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  3255. #undef TARGET_POLICY_LIST_ITEM
  3256. );
  3257. return std::string();
  3258. }
  3259. } targetPolicyNode;
  3260. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  3261. {
  3262. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  3263. bool GeneratesContent() const override { return true; }
  3264. int NumExpectedParameters() const override { return 0; }
  3265. std::string Evaluate(
  3266. std::vector<std::string> const& /*parameters*/,
  3267. cmGeneratorExpressionContext* context,
  3268. GeneratorExpressionContent const* content,
  3269. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3270. {
  3271. reportError(context, content->GetOriginalExpression(),
  3272. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  3273. "should never be evaluated.");
  3274. return std::string();
  3275. }
  3276. } installPrefixNode;
  3277. class ArtifactDirTag;
  3278. class ArtifactLinkerTag;
  3279. class ArtifactLinkerLibraryTag;
  3280. class ArtifactLinkerImportTag;
  3281. class ArtifactNameTag;
  3282. class ArtifactImportTag;
  3283. class ArtifactPathTag;
  3284. class ArtifactPdbTag;
  3285. class ArtifactSonameTag;
  3286. class ArtifactSonameImportTag;
  3287. class ArtifactBundleDirTag;
  3288. class ArtifactBundleDirNameTag;
  3289. class ArtifactBundleContentDirTag;
  3290. template <typename ArtifactT, typename ComponentT>
  3291. struct TargetFilesystemArtifactDependency
  3292. {
  3293. static void AddDependency(cmGeneratorTarget* target,
  3294. cmGeneratorExpressionContext* context)
  3295. {
  3296. context->DependTargets.insert(target);
  3297. context->AllTargets.insert(target);
  3298. }
  3299. };
  3300. struct TargetFilesystemArtifactDependencyCMP0112
  3301. {
  3302. static void AddDependency(cmGeneratorTarget* target,
  3303. cmGeneratorExpressionContext* context)
  3304. {
  3305. context->AllTargets.insert(target);
  3306. cmLocalGenerator const* lg = context->LG;
  3307. switch (target->GetPolicyStatusCMP0112()) {
  3308. case cmPolicies::WARN:
  3309. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  3310. "CMAKE_POLICY_WARNING_CMP0112")) {
  3311. std::string err =
  3312. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  3313. "\nDependency being added to target:\n \"",
  3314. target->GetName(), "\"\n");
  3315. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  3316. err, context->Backtrace);
  3317. }
  3318. CM_FALLTHROUGH;
  3319. case cmPolicies::OLD:
  3320. context->DependTargets.insert(target);
  3321. break;
  3322. case cmPolicies::NEW:
  3323. break;
  3324. }
  3325. }
  3326. };
  3327. template <typename ArtifactT>
  3328. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  3329. : TargetFilesystemArtifactDependencyCMP0112
  3330. {
  3331. };
  3332. template <typename ArtifactT>
  3333. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  3334. : TargetFilesystemArtifactDependencyCMP0112
  3335. {
  3336. };
  3337. template <>
  3338. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  3339. ArtifactPathTag>
  3340. : TargetFilesystemArtifactDependencyCMP0112
  3341. {
  3342. };
  3343. template <>
  3344. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  3345. ArtifactPathTag>
  3346. : TargetFilesystemArtifactDependencyCMP0112
  3347. {
  3348. };
  3349. template <>
  3350. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  3351. ArtifactPathTag>
  3352. : TargetFilesystemArtifactDependencyCMP0112
  3353. {
  3354. };
  3355. template <typename ArtifactT>
  3356. struct TargetFilesystemArtifactResultCreator
  3357. {
  3358. static std::string Create(cmGeneratorTarget* target,
  3359. cmGeneratorExpressionContext* context,
  3360. GeneratorExpressionContent const* content);
  3361. };
  3362. template <>
  3363. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  3364. {
  3365. static std::string Create(cmGeneratorTarget* target,
  3366. cmGeneratorExpressionContext* context,
  3367. GeneratorExpressionContent const* content)
  3368. {
  3369. // The target soname file (.so.1).
  3370. if (target->IsDLLPlatform()) {
  3371. ::reportError(context, content->GetOriginalExpression(),
  3372. "TARGET_SONAME_FILE is not allowed "
  3373. "for DLL target platforms.");
  3374. return std::string();
  3375. }
  3376. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3377. ::reportError(context, content->GetOriginalExpression(),
  3378. "TARGET_SONAME_FILE is allowed only for "
  3379. "SHARED libraries.");
  3380. return std::string();
  3381. }
  3382. if (target->IsArchivedAIXSharedLibrary()) {
  3383. ::reportError(context, content->GetOriginalExpression(),
  3384. "TARGET_SONAME_FILE is not allowed for "
  3385. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3386. return std::string();
  3387. }
  3388. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  3389. target->GetSOName(context->Config));
  3390. return result;
  3391. }
  3392. };
  3393. template <>
  3394. struct TargetFilesystemArtifactResultCreator<ArtifactSonameImportTag>
  3395. {
  3396. static std::string Create(cmGeneratorTarget* target,
  3397. cmGeneratorExpressionContext* context,
  3398. GeneratorExpressionContent const* content)
  3399. {
  3400. // The target soname file (.so.1).
  3401. if (target->IsDLLPlatform()) {
  3402. ::reportError(context, content->GetOriginalExpression(),
  3403. "TARGET_SONAME_IMPORT_FILE is not allowed "
  3404. "for DLL target platforms.");
  3405. return std::string();
  3406. }
  3407. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3408. ::reportError(context, content->GetOriginalExpression(),
  3409. "TARGET_SONAME_IMPORT_FILE is allowed only for "
  3410. "SHARED libraries.");
  3411. return std::string();
  3412. }
  3413. if (target->IsArchivedAIXSharedLibrary()) {
  3414. ::reportError(context, content->GetOriginalExpression(),
  3415. "TARGET_SONAME_IMPORT_FILE is not allowed for "
  3416. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3417. return std::string();
  3418. }
  3419. if (target->HasImportLibrary(context->Config)) {
  3420. return cmStrCat(target->GetDirectory(
  3421. context->Config, cmStateEnums::ImportLibraryArtifact),
  3422. '/',
  3423. target->GetSOName(context->Config,
  3424. cmStateEnums::ImportLibraryArtifact));
  3425. }
  3426. return std::string{};
  3427. }
  3428. };
  3429. template <>
  3430. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  3431. {
  3432. static std::string Create(cmGeneratorTarget* target,
  3433. cmGeneratorExpressionContext* context,
  3434. GeneratorExpressionContent const* content)
  3435. {
  3436. if (target->IsImported()) {
  3437. ::reportError(context, content->GetOriginalExpression(),
  3438. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  3439. return std::string();
  3440. }
  3441. std::string language = target->GetLinkerLanguage(context->Config);
  3442. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3443. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3444. ::reportError(context, content->GetOriginalExpression(),
  3445. "TARGET_PDB_FILE is not supported by the target linker.");
  3446. return std::string();
  3447. }
  3448. cmStateEnums::TargetType targetType = target->GetType();
  3449. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3450. targetType != cmStateEnums::MODULE_LIBRARY &&
  3451. targetType != cmStateEnums::EXECUTABLE) {
  3452. ::reportError(context, content->GetOriginalExpression(),
  3453. "TARGET_PDB_FILE is allowed only for "
  3454. "targets with linker created artifacts.");
  3455. return std::string();
  3456. }
  3457. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  3458. '/', target->GetPDBName(context->Config));
  3459. return result;
  3460. }
  3461. };
  3462. template <>
  3463. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  3464. {
  3465. static std::string Create(cmGeneratorTarget* target,
  3466. cmGeneratorExpressionContext* context,
  3467. GeneratorExpressionContent const* content)
  3468. {
  3469. // The file used to link to the target (.so, .lib, .a) or import file
  3470. // (.lib, .tbd).
  3471. if (!target->IsLinkable()) {
  3472. ::reportError(context, content->GetOriginalExpression(),
  3473. "TARGET_LINKER_FILE is allowed only for libraries and "
  3474. "executables with ENABLE_EXPORTS.");
  3475. return std::string();
  3476. }
  3477. cmStateEnums::ArtifactType artifact =
  3478. target->HasImportLibrary(context->Config)
  3479. ? cmStateEnums::ImportLibraryArtifact
  3480. : cmStateEnums::RuntimeBinaryArtifact;
  3481. return target->GetFullPath(context->Config, artifact);
  3482. }
  3483. };
  3484. template <>
  3485. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerLibraryTag>
  3486. {
  3487. static std::string Create(cmGeneratorTarget* target,
  3488. cmGeneratorExpressionContext* context,
  3489. GeneratorExpressionContent const* content)
  3490. {
  3491. // The file used to link to the target (.dylib, .so, .a).
  3492. if (!target->IsLinkable() ||
  3493. target->GetType() == cmStateEnums::EXECUTABLE) {
  3494. ::reportError(context, content->GetOriginalExpression(),
  3495. "TARGET_LINKER_LIBRARY_FILE is allowed only for libraries "
  3496. "with ENABLE_EXPORTS.");
  3497. return std::string();
  3498. }
  3499. if (!target->IsDLLPlatform() ||
  3500. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3501. return target->GetFullPath(context->Config,
  3502. cmStateEnums::RuntimeBinaryArtifact);
  3503. }
  3504. return std::string{};
  3505. }
  3506. };
  3507. template <>
  3508. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerImportTag>
  3509. {
  3510. static std::string Create(cmGeneratorTarget* target,
  3511. cmGeneratorExpressionContext* context,
  3512. GeneratorExpressionContent const* content)
  3513. {
  3514. // The file used to link to the target (.lib, .tbd).
  3515. if (!target->IsLinkable()) {
  3516. ::reportError(
  3517. context, content->GetOriginalExpression(),
  3518. "TARGET_LINKER_IMPORT_FILE is allowed only for libraries and "
  3519. "executables with ENABLE_EXPORTS.");
  3520. return std::string();
  3521. }
  3522. if (target->HasImportLibrary(context->Config)) {
  3523. return target->GetFullPath(context->Config,
  3524. cmStateEnums::ImportLibraryArtifact);
  3525. }
  3526. return std::string{};
  3527. }
  3528. };
  3529. template <>
  3530. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  3531. {
  3532. static std::string Create(cmGeneratorTarget* target,
  3533. cmGeneratorExpressionContext* context,
  3534. GeneratorExpressionContent const* content)
  3535. {
  3536. if (target->IsImported()) {
  3537. ::reportError(context, content->GetOriginalExpression(),
  3538. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  3539. return std::string();
  3540. }
  3541. if (!target->IsBundleOnApple()) {
  3542. ::reportError(context, content->GetOriginalExpression(),
  3543. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  3544. return std::string();
  3545. }
  3546. std::string outpath = target->GetDirectory(context->Config) + '/';
  3547. return target->BuildBundleDirectory(outpath, context->Config,
  3548. cmGeneratorTarget::BundleDirLevel);
  3549. }
  3550. };
  3551. template <>
  3552. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  3553. {
  3554. static std::string Create(cmGeneratorTarget* target,
  3555. cmGeneratorExpressionContext* context,
  3556. GeneratorExpressionContent const* content)
  3557. {
  3558. if (target->IsImported()) {
  3559. ::reportError(
  3560. context, content->GetOriginalExpression(),
  3561. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  3562. return std::string();
  3563. }
  3564. if (!target->IsBundleOnApple()) {
  3565. ::reportError(
  3566. context, content->GetOriginalExpression(),
  3567. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  3568. return std::string();
  3569. }
  3570. auto level = cmGeneratorTarget::BundleDirLevel;
  3571. auto config = context->Config;
  3572. if (target->IsAppBundleOnApple()) {
  3573. return target->GetAppBundleDirectory(config, level);
  3574. }
  3575. if (target->IsFrameworkOnApple()) {
  3576. return target->GetFrameworkDirectory(config, level);
  3577. }
  3578. if (target->IsCFBundleOnApple()) {
  3579. return target->GetCFBundleDirectory(config, level);
  3580. }
  3581. return std::string();
  3582. }
  3583. };
  3584. template <>
  3585. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  3586. {
  3587. static std::string Create(cmGeneratorTarget* target,
  3588. cmGeneratorExpressionContext* context,
  3589. GeneratorExpressionContent const* content)
  3590. {
  3591. if (target->IsImported()) {
  3592. ::reportError(
  3593. context, content->GetOriginalExpression(),
  3594. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  3595. return std::string();
  3596. }
  3597. if (!target->IsBundleOnApple()) {
  3598. ::reportError(
  3599. context, content->GetOriginalExpression(),
  3600. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  3601. return std::string();
  3602. }
  3603. std::string outpath = target->GetDirectory(context->Config) + '/';
  3604. return target->BuildBundleDirectory(outpath, context->Config,
  3605. cmGeneratorTarget::ContentLevel);
  3606. }
  3607. };
  3608. template <>
  3609. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  3610. {
  3611. static std::string Create(cmGeneratorTarget* target,
  3612. cmGeneratorExpressionContext* context,
  3613. GeneratorExpressionContent const* /*unused*/)
  3614. {
  3615. return target->GetFullPath(context->Config,
  3616. cmStateEnums::RuntimeBinaryArtifact, true);
  3617. }
  3618. };
  3619. template <>
  3620. struct TargetFilesystemArtifactResultCreator<ArtifactImportTag>
  3621. {
  3622. static std::string Create(cmGeneratorTarget* target,
  3623. cmGeneratorExpressionContext* context,
  3624. GeneratorExpressionContent const* /*unused*/)
  3625. {
  3626. if (target->HasImportLibrary(context->Config)) {
  3627. return target->GetFullPath(context->Config,
  3628. cmStateEnums::ImportLibraryArtifact, true);
  3629. }
  3630. return std::string{};
  3631. }
  3632. };
  3633. template <typename ArtifactT>
  3634. struct TargetFilesystemArtifactResultGetter
  3635. {
  3636. static std::string Get(std::string const& result);
  3637. };
  3638. template <>
  3639. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  3640. {
  3641. static std::string Get(std::string const& result)
  3642. {
  3643. return cmSystemTools::GetFilenameName(result);
  3644. }
  3645. };
  3646. template <>
  3647. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  3648. {
  3649. static std::string Get(std::string const& result)
  3650. {
  3651. return cmSystemTools::GetFilenamePath(result);
  3652. }
  3653. };
  3654. template <>
  3655. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  3656. {
  3657. static std::string Get(std::string const& result) { return result; }
  3658. };
  3659. struct TargetArtifactBase : public cmGeneratorExpressionNode
  3660. {
  3661. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  3662. protected:
  3663. cmGeneratorTarget* GetTarget(
  3664. std::vector<std::string> const& parameters,
  3665. cmGeneratorExpressionContext* context,
  3666. GeneratorExpressionContent const* content,
  3667. cmGeneratorExpressionDAGChecker* dagChecker) const
  3668. {
  3669. // Lookup the referenced target.
  3670. std::string const& name = parameters.front();
  3671. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  3672. ::reportError(context, content->GetOriginalExpression(),
  3673. "Expression syntax not recognized.");
  3674. return nullptr;
  3675. }
  3676. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  3677. if (!target) {
  3678. ::reportError(context, content->GetOriginalExpression(),
  3679. "No target \"" + name + "\"");
  3680. return nullptr;
  3681. }
  3682. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  3683. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  3684. ::reportError(context, content->GetOriginalExpression(),
  3685. "Target \"" + name +
  3686. "\" is not an executable or library.");
  3687. return nullptr;
  3688. }
  3689. if (dagChecker &&
  3690. (dagChecker->EvaluatingLinkLibraries(target) ||
  3691. (dagChecker->EvaluatingSources() &&
  3692. target == dagChecker->TopTarget()))) {
  3693. ::reportError(context, content->GetOriginalExpression(),
  3694. "Expressions which require the linker language may not "
  3695. "be used while evaluating link libraries");
  3696. return nullptr;
  3697. }
  3698. return target;
  3699. }
  3700. };
  3701. template <typename ArtifactT, typename ComponentT>
  3702. struct TargetFilesystemArtifact : public TargetArtifactBase
  3703. {
  3704. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  3705. int NumExpectedParameters() const override { return 1; }
  3706. std::string Evaluate(
  3707. std::vector<std::string> const& parameters,
  3708. cmGeneratorExpressionContext* context,
  3709. GeneratorExpressionContent const* content,
  3710. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3711. {
  3712. cmGeneratorTarget* target =
  3713. this->GetTarget(parameters, context, content, dagChecker);
  3714. if (!target) {
  3715. return std::string();
  3716. }
  3717. // Not a dependent target if we are querying for ArtifactDirTag,
  3718. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  3719. // and ArtifactBundleContentDirTag
  3720. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  3721. target, context);
  3722. std::string result =
  3723. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  3724. content);
  3725. if (context->HadError) {
  3726. return std::string();
  3727. }
  3728. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  3729. }
  3730. };
  3731. template <typename ArtifactT>
  3732. struct TargetFilesystemArtifactNodeGroup
  3733. {
  3734. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  3735. {
  3736. }
  3737. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  3738. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  3739. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  3740. };
  3741. static TargetFilesystemArtifactNodeGroup<ArtifactNameTag> const
  3742. targetNodeGroup;
  3743. static TargetFilesystemArtifactNodeGroup<ArtifactImportTag> const
  3744. targetImportNodeGroup;
  3745. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag> const
  3746. targetLinkerNodeGroup;
  3747. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerLibraryTag> const
  3748. targetLinkerLibraryNodeGroup;
  3749. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerImportTag> const
  3750. targetLinkerImportNodeGroup;
  3751. static TargetFilesystemArtifactNodeGroup<ArtifactSonameTag> const
  3752. targetSoNameNodeGroup;
  3753. static TargetFilesystemArtifactNodeGroup<ArtifactSonameImportTag> const
  3754. targetSoNameImportNodeGroup;
  3755. static TargetFilesystemArtifactNodeGroup<ArtifactPdbTag> const
  3756. targetPdbNodeGroup;
  3757. static TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag> const
  3758. targetBundleDirNode;
  3759. static TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  3760. ArtifactNameTag> const targetBundleDirNameNode;
  3761. static TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  3762. ArtifactPathTag> const
  3763. targetBundleContentDirNode;
  3764. //
  3765. // To retrieve base name for various artifacts
  3766. //
  3767. template <typename ArtifactT>
  3768. struct TargetOutputNameArtifactResultGetter
  3769. {
  3770. static std::string Get(cmGeneratorTarget* target,
  3771. cmGeneratorExpressionContext* context,
  3772. GeneratorExpressionContent const* content);
  3773. };
  3774. template <>
  3775. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  3776. {
  3777. static std::string Get(cmGeneratorTarget* target,
  3778. cmGeneratorExpressionContext* context,
  3779. GeneratorExpressionContent const* /*unused*/)
  3780. {
  3781. return target->GetOutputName(context->Config,
  3782. cmStateEnums::RuntimeBinaryArtifact) +
  3783. target->GetFilePostfix(context->Config);
  3784. }
  3785. };
  3786. template <>
  3787. struct TargetOutputNameArtifactResultGetter<ArtifactImportTag>
  3788. {
  3789. static std::string Get(cmGeneratorTarget* target,
  3790. cmGeneratorExpressionContext* context,
  3791. GeneratorExpressionContent const* /*unused*/)
  3792. {
  3793. if (target->HasImportLibrary(context->Config)) {
  3794. return target->GetOutputName(context->Config,
  3795. cmStateEnums::ImportLibraryArtifact) +
  3796. target->GetFilePostfix(context->Config);
  3797. }
  3798. return std::string{};
  3799. }
  3800. };
  3801. template <>
  3802. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  3803. {
  3804. static std::string Get(cmGeneratorTarget* target,
  3805. cmGeneratorExpressionContext* context,
  3806. GeneratorExpressionContent const* content)
  3807. {
  3808. // The library file used to link to the target (.so, .lib, .a) or import
  3809. // file (.lin, .tbd).
  3810. if (!target->IsLinkable()) {
  3811. ::reportError(context, content->GetOriginalExpression(),
  3812. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  3813. "libraries and executables with ENABLE_EXPORTS.");
  3814. return std::string();
  3815. }
  3816. cmStateEnums::ArtifactType artifact =
  3817. target->HasImportLibrary(context->Config)
  3818. ? cmStateEnums::ImportLibraryArtifact
  3819. : cmStateEnums::RuntimeBinaryArtifact;
  3820. return target->GetOutputName(context->Config, artifact) +
  3821. target->GetFilePostfix(context->Config);
  3822. }
  3823. };
  3824. template <>
  3825. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerLibraryTag>
  3826. {
  3827. static std::string Get(cmGeneratorTarget* target,
  3828. cmGeneratorExpressionContext* context,
  3829. GeneratorExpressionContent const* content)
  3830. {
  3831. // The library file used to link to the target (.so, .lib, .a).
  3832. if (!target->IsLinkable() ||
  3833. target->GetType() == cmStateEnums::EXECUTABLE) {
  3834. ::reportError(context, content->GetOriginalExpression(),
  3835. "TARGET_LINKER_LIBRARY_FILE_BASE_NAME is allowed only for "
  3836. "libraries with ENABLE_EXPORTS.");
  3837. return std::string();
  3838. }
  3839. if (!target->IsDLLPlatform() ||
  3840. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3841. return target->GetOutputName(context->Config,
  3842. cmStateEnums::ImportLibraryArtifact) +
  3843. target->GetFilePostfix(context->Config);
  3844. }
  3845. return std::string{};
  3846. }
  3847. };
  3848. template <>
  3849. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerImportTag>
  3850. {
  3851. static std::string Get(cmGeneratorTarget* target,
  3852. cmGeneratorExpressionContext* context,
  3853. GeneratorExpressionContent const* content)
  3854. {
  3855. // The import file used to link to the target (.lib, .tbd).
  3856. if (!target->IsLinkable()) {
  3857. ::reportError(context, content->GetOriginalExpression(),
  3858. "TARGET_LINKER_IMPORT_FILE_BASE_NAME is allowed only for "
  3859. "libraries and executables with ENABLE_EXPORTS.");
  3860. return std::string();
  3861. }
  3862. if (target->HasImportLibrary(context->Config)) {
  3863. return target->GetOutputName(context->Config,
  3864. cmStateEnums::ImportLibraryArtifact) +
  3865. target->GetFilePostfix(context->Config);
  3866. }
  3867. return std::string{};
  3868. }
  3869. };
  3870. template <>
  3871. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  3872. {
  3873. static std::string Get(cmGeneratorTarget* target,
  3874. cmGeneratorExpressionContext* context,
  3875. GeneratorExpressionContent const* content)
  3876. {
  3877. if (target->IsImported()) {
  3878. ::reportError(
  3879. context, content->GetOriginalExpression(),
  3880. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  3881. return std::string();
  3882. }
  3883. std::string language = target->GetLinkerLanguage(context->Config);
  3884. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3885. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3886. ::reportError(
  3887. context, content->GetOriginalExpression(),
  3888. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  3889. return std::string();
  3890. }
  3891. cmStateEnums::TargetType targetType = target->GetType();
  3892. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3893. targetType != cmStateEnums::MODULE_LIBRARY &&
  3894. targetType != cmStateEnums::EXECUTABLE) {
  3895. ::reportError(context, content->GetOriginalExpression(),
  3896. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  3897. "targets with linker created artifacts.");
  3898. return std::string();
  3899. }
  3900. return target->GetPDBOutputName(context->Config);
  3901. }
  3902. };
  3903. template <typename ArtifactT>
  3904. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  3905. {
  3906. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  3907. int NumExpectedParameters() const override { return 1; }
  3908. std::string Evaluate(
  3909. std::vector<std::string> const& parameters,
  3910. cmGeneratorExpressionContext* context,
  3911. GeneratorExpressionContent const* content,
  3912. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3913. {
  3914. cmGeneratorTarget* target =
  3915. this->GetTarget(parameters, context, content, dagChecker);
  3916. if (!target) {
  3917. return std::string();
  3918. }
  3919. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  3920. target, context, content);
  3921. if (context->HadError) {
  3922. return std::string();
  3923. }
  3924. return result;
  3925. }
  3926. };
  3927. static TargetFileBaseNameArtifact<ArtifactNameTag> const
  3928. targetFileBaseNameNode;
  3929. static TargetFileBaseNameArtifact<ArtifactImportTag> const
  3930. targetImportFileBaseNameNode;
  3931. static TargetFileBaseNameArtifact<ArtifactLinkerTag> const
  3932. targetLinkerFileBaseNameNode;
  3933. static TargetFileBaseNameArtifact<ArtifactLinkerLibraryTag> const
  3934. targetLinkerLibraryFileBaseNameNode;
  3935. static TargetFileBaseNameArtifact<ArtifactLinkerImportTag> const
  3936. targetLinkerImportFileBaseNameNode;
  3937. static TargetFileBaseNameArtifact<ArtifactPdbTag> const
  3938. targetPdbFileBaseNameNode;
  3939. class ArtifactFilePrefixTag;
  3940. class ArtifactImportFilePrefixTag;
  3941. class ArtifactLinkerFilePrefixTag;
  3942. class ArtifactLinkerLibraryFilePrefixTag;
  3943. class ArtifactLinkerImportFilePrefixTag;
  3944. class ArtifactFileSuffixTag;
  3945. class ArtifactImportFileSuffixTag;
  3946. class ArtifactLinkerFileSuffixTag;
  3947. class ArtifactLinkerLibraryFileSuffixTag;
  3948. class ArtifactLinkerImportFileSuffixTag;
  3949. template <typename ArtifactT>
  3950. struct TargetFileArtifactResultGetter
  3951. {
  3952. static std::string Get(cmGeneratorTarget* target,
  3953. cmGeneratorExpressionContext* context,
  3954. GeneratorExpressionContent const* content);
  3955. };
  3956. template <>
  3957. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  3958. {
  3959. static std::string Get(cmGeneratorTarget* target,
  3960. cmGeneratorExpressionContext* context,
  3961. GeneratorExpressionContent const*)
  3962. {
  3963. return target->GetFilePrefix(context->Config);
  3964. }
  3965. };
  3966. template <>
  3967. struct TargetFileArtifactResultGetter<ArtifactImportFilePrefixTag>
  3968. {
  3969. static std::string Get(cmGeneratorTarget* target,
  3970. cmGeneratorExpressionContext* context,
  3971. GeneratorExpressionContent const*)
  3972. {
  3973. if (target->HasImportLibrary(context->Config)) {
  3974. return target->GetFilePrefix(context->Config,
  3975. cmStateEnums::ImportLibraryArtifact);
  3976. }
  3977. return std::string{};
  3978. }
  3979. };
  3980. template <>
  3981. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  3982. {
  3983. static std::string Get(cmGeneratorTarget* target,
  3984. cmGeneratorExpressionContext* context,
  3985. GeneratorExpressionContent const* content)
  3986. {
  3987. if (!target->IsLinkable()) {
  3988. ::reportError(
  3989. context, content->GetOriginalExpression(),
  3990. "TARGET_LINKER_FILE_PREFIX is allowed only for libraries and "
  3991. "executables with ENABLE_EXPORTS.");
  3992. return std::string();
  3993. }
  3994. cmStateEnums::ArtifactType artifact =
  3995. target->HasImportLibrary(context->Config)
  3996. ? cmStateEnums::ImportLibraryArtifact
  3997. : cmStateEnums::RuntimeBinaryArtifact;
  3998. return target->GetFilePrefix(context->Config, artifact);
  3999. }
  4000. };
  4001. template <>
  4002. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFilePrefixTag>
  4003. {
  4004. static std::string Get(cmGeneratorTarget* target,
  4005. cmGeneratorExpressionContext* context,
  4006. GeneratorExpressionContent const* content)
  4007. {
  4008. if (!target->IsLinkable() ||
  4009. target->GetType() == cmStateEnums::EXECUTABLE) {
  4010. ::reportError(
  4011. context, content->GetOriginalExpression(),
  4012. "TARGET_LINKER_LIBRARY_FILE_PREFIX is allowed only for libraries "
  4013. "with ENABLE_EXPORTS.");
  4014. return std::string();
  4015. }
  4016. if (!target->IsDLLPlatform() ||
  4017. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4018. return target->GetFilePrefix(context->Config,
  4019. cmStateEnums::RuntimeBinaryArtifact);
  4020. }
  4021. return std::string{};
  4022. }
  4023. };
  4024. template <>
  4025. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFilePrefixTag>
  4026. {
  4027. static std::string Get(cmGeneratorTarget* target,
  4028. cmGeneratorExpressionContext* context,
  4029. GeneratorExpressionContent const* content)
  4030. {
  4031. if (!target->IsLinkable()) {
  4032. ::reportError(
  4033. context, content->GetOriginalExpression(),
  4034. "TARGET_LINKER_IMPORT_FILE_PREFIX is allowed only for libraries and "
  4035. "executables with ENABLE_EXPORTS.");
  4036. return std::string();
  4037. }
  4038. if (target->HasImportLibrary(context->Config)) {
  4039. return target->GetFilePrefix(context->Config,
  4040. cmStateEnums::ImportLibraryArtifact);
  4041. }
  4042. return std::string{};
  4043. }
  4044. };
  4045. template <>
  4046. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  4047. {
  4048. static std::string Get(cmGeneratorTarget* target,
  4049. cmGeneratorExpressionContext* context,
  4050. GeneratorExpressionContent const*)
  4051. {
  4052. return target->GetFileSuffix(context->Config);
  4053. }
  4054. };
  4055. template <>
  4056. struct TargetFileArtifactResultGetter<ArtifactImportFileSuffixTag>
  4057. {
  4058. static std::string Get(cmGeneratorTarget* target,
  4059. cmGeneratorExpressionContext* context,
  4060. GeneratorExpressionContent const*)
  4061. {
  4062. if (target->HasImportLibrary(context->Config)) {
  4063. return target->GetFileSuffix(context->Config,
  4064. cmStateEnums::ImportLibraryArtifact);
  4065. }
  4066. return std::string{};
  4067. }
  4068. };
  4069. template <>
  4070. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  4071. {
  4072. static std::string Get(cmGeneratorTarget* target,
  4073. cmGeneratorExpressionContext* context,
  4074. GeneratorExpressionContent const* content)
  4075. {
  4076. if (!target->IsLinkable()) {
  4077. ::reportError(
  4078. context, content->GetOriginalExpression(),
  4079. "TARGET_LINKER_FILE_SUFFIX is allowed only for libraries and "
  4080. "executables with ENABLE_EXPORTS.");
  4081. return std::string();
  4082. }
  4083. cmStateEnums::ArtifactType artifact =
  4084. target->HasImportLibrary(context->Config)
  4085. ? cmStateEnums::ImportLibraryArtifact
  4086. : cmStateEnums::RuntimeBinaryArtifact;
  4087. return target->GetFileSuffix(context->Config, artifact);
  4088. }
  4089. };
  4090. template <>
  4091. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFileSuffixTag>
  4092. {
  4093. static std::string Get(cmGeneratorTarget* target,
  4094. cmGeneratorExpressionContext* context,
  4095. GeneratorExpressionContent const* content)
  4096. {
  4097. if (!target->IsLinkable() ||
  4098. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4099. ::reportError(context, content->GetOriginalExpression(),
  4100. "TARGET_LINKER_LIBRARY_FILE_SUFFIX is allowed only for "
  4101. "libraries with ENABLE_EXPORTS.");
  4102. return std::string();
  4103. }
  4104. if (!target->IsDLLPlatform() ||
  4105. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4106. return target->GetFileSuffix(context->Config,
  4107. cmStateEnums::RuntimeBinaryArtifact);
  4108. }
  4109. return std::string{};
  4110. }
  4111. };
  4112. template <>
  4113. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFileSuffixTag>
  4114. {
  4115. static std::string Get(cmGeneratorTarget* target,
  4116. cmGeneratorExpressionContext* context,
  4117. GeneratorExpressionContent const* content)
  4118. {
  4119. if (!target->IsLinkable()) {
  4120. ::reportError(
  4121. context, content->GetOriginalExpression(),
  4122. "TARGET_LINKER_IMPORT_FILE_SUFFIX is allowed only for libraries and "
  4123. "executables with ENABLE_EXPORTS.");
  4124. return std::string();
  4125. }
  4126. if (target->HasImportLibrary(context->Config)) {
  4127. return target->GetFileSuffix(context->Config,
  4128. cmStateEnums::ImportLibraryArtifact);
  4129. }
  4130. return std::string{};
  4131. }
  4132. };
  4133. template <typename ArtifactT>
  4134. struct TargetFileArtifact : public TargetArtifactBase
  4135. {
  4136. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  4137. int NumExpectedParameters() const override { return 1; }
  4138. std::string Evaluate(
  4139. std::vector<std::string> const& parameters,
  4140. cmGeneratorExpressionContext* context,
  4141. GeneratorExpressionContent const* content,
  4142. cmGeneratorExpressionDAGChecker* dagChecker) const override
  4143. {
  4144. cmGeneratorTarget* target =
  4145. this->GetTarget(parameters, context, content, dagChecker);
  4146. if (!target) {
  4147. return std::string();
  4148. }
  4149. std::string result =
  4150. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  4151. if (context->HadError) {
  4152. return std::string();
  4153. }
  4154. return result;
  4155. }
  4156. };
  4157. static TargetFileArtifact<ArtifactFilePrefixTag> const targetFilePrefixNode;
  4158. static TargetFileArtifact<ArtifactImportFilePrefixTag> const
  4159. targetImportFilePrefixNode;
  4160. static TargetFileArtifact<ArtifactLinkerFilePrefixTag> const
  4161. targetLinkerFilePrefixNode;
  4162. static TargetFileArtifact<ArtifactLinkerLibraryFilePrefixTag> const
  4163. targetLinkerLibraryFilePrefixNode;
  4164. static TargetFileArtifact<ArtifactLinkerImportFilePrefixTag> const
  4165. targetLinkerImportFilePrefixNode;
  4166. static TargetFileArtifact<ArtifactFileSuffixTag> const targetFileSuffixNode;
  4167. static TargetFileArtifact<ArtifactImportFileSuffixTag> const
  4168. targetImportFileSuffixNode;
  4169. static TargetFileArtifact<ArtifactLinkerFileSuffixTag> const
  4170. targetLinkerFileSuffixNode;
  4171. static TargetFileArtifact<ArtifactLinkerLibraryFileSuffixTag> const
  4172. targetLinkerLibraryFileSuffixNode;
  4173. static TargetFileArtifact<ArtifactLinkerImportFileSuffixTag> const
  4174. targetLinkerImportFileSuffixNode;
  4175. static const struct ShellPathNode : public cmGeneratorExpressionNode
  4176. {
  4177. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  4178. std::string Evaluate(
  4179. std::vector<std::string> const& parameters,
  4180. cmGeneratorExpressionContext* context,
  4181. GeneratorExpressionContent const* content,
  4182. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  4183. {
  4184. cmList list_in{ parameters.front() };
  4185. if (list_in.empty()) {
  4186. reportError(context, content->GetOriginalExpression(),
  4187. "\"\" is not an absolute path.");
  4188. return std::string();
  4189. }
  4190. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  4191. cmOutputConverter converter(snapshot);
  4192. char const* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  4193. std::vector<std::string> list_out;
  4194. list_out.reserve(list_in.size());
  4195. for (auto const& in : list_in) {
  4196. if (!cmSystemTools::FileIsFullPath(in)) {
  4197. reportError(context, content->GetOriginalExpression(),
  4198. "\"" + in + "\" is not an absolute path.");
  4199. return std::string();
  4200. }
  4201. list_out.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  4202. }
  4203. return cmJoin(list_out, separator);
  4204. }
  4205. } shellPathNode;
  4206. cmGeneratorExpressionNode const* cmGeneratorExpressionNode::GetNode(
  4207. std::string const& identifier)
  4208. {
  4209. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  4210. { "0", &zeroNode },
  4211. { "1", &oneNode },
  4212. { "AND", &andNode },
  4213. { "OR", &orNode },
  4214. { "NOT", &notNode },
  4215. { "C_COMPILER_ID", &cCompilerIdNode },
  4216. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  4217. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  4218. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  4219. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  4220. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  4221. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  4222. { "VERSION_GREATER", &versionGreaterNode },
  4223. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  4224. { "VERSION_LESS", &versionLessNode },
  4225. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  4226. { "VERSION_EQUAL", &versionEqualNode },
  4227. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  4228. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  4229. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  4230. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  4231. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  4232. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  4233. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  4234. { "C_COMPILER_FRONTEND_VARIANT", &cCompilerFrontendVariantNode },
  4235. { "CXX_COMPILER_FRONTEND_VARIANT", &cxxCompilerFrontendVariantNode },
  4236. { "CUDA_COMPILER_FRONTEND_VARIANT", &cudaCompilerFrontendVariantNode },
  4237. { "OBJC_COMPILER_FRONTEND_VARIANT", &objcCompilerFrontendVariantNode },
  4238. { "OBJCXX_COMPILER_FRONTEND_VARIANT", &objcxxCompilerFrontendVariantNode },
  4239. { "Fortran_COMPILER_FRONTEND_VARIANT",
  4240. &fortranCompilerFrontendVariantNode },
  4241. { "HIP_COMPILER_FRONTEND_VARIANT", &hipCompilerFrontendVariantNode },
  4242. { "PLATFORM_ID", &platformIdNode },
  4243. { "COMPILE_FEATURES", &compileFeaturesNode },
  4244. { "CONFIGURATION", &configurationNode },
  4245. { "CONFIG", &configurationTestNode },
  4246. { "TARGET_FILE", &targetNodeGroup.File },
  4247. { "TARGET_IMPORT_FILE", &targetImportNodeGroup.File },
  4248. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  4249. { "TARGET_LINKER_LIBRARY_FILE", &targetLinkerLibraryNodeGroup.File },
  4250. { "TARGET_LINKER_IMPORT_FILE", &targetLinkerImportNodeGroup.File },
  4251. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  4252. { "TARGET_SONAME_IMPORT_FILE", &targetSoNameImportNodeGroup.File },
  4253. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  4254. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  4255. { "TARGET_IMPORT_FILE_BASE_NAME", &targetImportFileBaseNameNode },
  4256. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  4257. { "TARGET_LINKER_LIBRARY_FILE_BASE_NAME",
  4258. &targetLinkerLibraryFileBaseNameNode },
  4259. { "TARGET_LINKER_IMPORT_FILE_BASE_NAME",
  4260. &targetLinkerImportFileBaseNameNode },
  4261. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  4262. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  4263. { "TARGET_IMPORT_FILE_PREFIX", &targetImportFilePrefixNode },
  4264. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  4265. { "TARGET_LINKER_LIBRARY_FILE_PREFIX",
  4266. &targetLinkerLibraryFilePrefixNode },
  4267. { "TARGET_LINKER_IMPORT_FILE_PREFIX", &targetLinkerImportFilePrefixNode },
  4268. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  4269. { "TARGET_IMPORT_FILE_SUFFIX", &targetImportFileSuffixNode },
  4270. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  4271. { "TARGET_LINKER_LIBRARY_FILE_SUFFIX",
  4272. &targetLinkerLibraryFileSuffixNode },
  4273. { "TARGET_LINKER_IMPORT_FILE_SUFFIX", &targetLinkerImportFileSuffixNode },
  4274. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  4275. { "TARGET_IMPORT_FILE_NAME", &targetImportNodeGroup.FileName },
  4276. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  4277. { "TARGET_LINKER_LIBRARY_FILE_NAME",
  4278. &targetLinkerLibraryNodeGroup.FileName },
  4279. { "TARGET_LINKER_IMPORT_FILE_NAME",
  4280. &targetLinkerImportNodeGroup.FileName },
  4281. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  4282. { "TARGET_SONAME_IMPORT_FILE_NAME",
  4283. &targetSoNameImportNodeGroup.FileName },
  4284. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  4285. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  4286. { "TARGET_IMPORT_FILE_DIR", &targetImportNodeGroup.FileDir },
  4287. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  4288. { "TARGET_LINKER_LIBRARY_FILE_DIR",
  4289. &targetLinkerLibraryNodeGroup.FileDir },
  4290. { "TARGET_LINKER_IMPORT_FILE_DIR", &targetLinkerImportNodeGroup.FileDir },
  4291. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  4292. { "TARGET_SONAME_IMPORT_FILE_DIR", &targetSoNameImportNodeGroup.FileDir },
  4293. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  4294. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  4295. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  4296. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  4297. { "STREQUAL", &strEqualNode },
  4298. { "EQUAL", &equalNode },
  4299. { "IN_LIST", &inListNode },
  4300. { "FILTER", &filterNode },
  4301. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  4302. { "LIST", &listNode },
  4303. { "LOWER_CASE", &lowerCaseNode },
  4304. { "UPPER_CASE", &upperCaseNode },
  4305. { "PATH", &pathNode },
  4306. { "PATH_EQUAL", &pathEqualNode },
  4307. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  4308. { "BOOL", &boolNode },
  4309. { "IF", &ifNode },
  4310. { "ANGLE-R", &angle_rNode },
  4311. { "COMMA", &commaNode },
  4312. { "SEMICOLON", &semicolonNode },
  4313. { "QUOTE", &quoteNode },
  4314. { "TARGET_PROPERTY", &targetPropertyNode },
  4315. { "TARGET_NAME", &targetNameNode },
  4316. { "TARGET_OBJECTS", &targetObjectsNode },
  4317. { "TARGET_POLICY", &targetPolicyNode },
  4318. { "TARGET_EXISTS", &targetExistsNode },
  4319. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  4320. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  4321. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  4322. { "TARGET_RUNTIME_DLL_DIRS", &targetRuntimeDllDirsNode },
  4323. { "GENEX_EVAL", &genexEvalNode },
  4324. { "BUILD_INTERFACE", &buildInterfaceNode },
  4325. { "INSTALL_INTERFACE", &installInterfaceNode },
  4326. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  4327. { "INSTALL_PREFIX", &installPrefixNode },
  4328. { "JOIN", &joinNode },
  4329. { "COMPILE_ONLY", &compileOnlyNode },
  4330. { "LINK_ONLY", &linkOnlyNode },
  4331. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  4332. { "COMPILE_LANGUAGE", &languageNode },
  4333. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  4334. { "LINK_LANGUAGE", &linkLanguageNode },
  4335. { "C_COMPILER_LINKER_ID", &cCompilerLinkerIdNode },
  4336. { "CXX_COMPILER_LINKER_ID", &cxxCompilerLinkerIdNode },
  4337. { "OBJC_COMPILER_LINKER_ID", &objcCompilerLinkerIdNode },
  4338. { "OBJCXX_COMPILER_LINKER_ID", &objcxxCompilerLinkerIdNode },
  4339. { "CUDA_COMPILER_LINKER_ID", &cudaCompilerLinkerIdNode },
  4340. { "Fortran_COMPILER_LINKER_ID", &fortranCompilerLinkerIdNode },
  4341. { "HIP_COMPILER_LINKER_ID", &hipCompilerLinkerIdNode },
  4342. { "C_COMPILER_LINKER_FRONTEND_VARIANT",
  4343. &cCompilerLinkerFrontendVariantNode },
  4344. { "CXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4345. &cxxCompilerLinkerFrontendVariantNode },
  4346. { "CUDA_COMPILER_LINKER_FRONTEND_VARIANT",
  4347. &cudaCompilerLinkerFrontendVariantNode },
  4348. { "OBJC_COMPILER_LINKER_FRONTEND_VARIANT",
  4349. &objcCompilerLinkerFrontendVariantNode },
  4350. { "OBJCXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4351. &objcxxCompilerLinkerFrontendVariantNode },
  4352. { "Fortran_COMPILER_LINKER_FRONTEND_VARIANT",
  4353. &fortranCompilerLinkerFrontendVariantNode },
  4354. { "HIP_COMPILER_LINKER_FRONTEND_VARIANT",
  4355. &hipCompilerLinkerFrontendVariantNode },
  4356. { "LINK_LIBRARY", &linkLibraryNode },
  4357. { "LINK_GROUP", &linkGroupNode },
  4358. { "HOST_LINK", &hostLinkNode },
  4359. { "DEVICE_LINK", &deviceLinkNode },
  4360. { "SHELL_PATH", &shellPathNode }
  4361. };
  4362. {
  4363. auto itr = nodeMap.find(identifier);
  4364. if (itr != nodeMap.end()) {
  4365. return itr->second;
  4366. }
  4367. }
  4368. return nullptr;
  4369. }
  4370. void reportError(cmGeneratorExpressionContext* context,
  4371. std::string const& expr, std::string const& result)
  4372. {
  4373. context->HadError = true;
  4374. if (context->Quiet) {
  4375. return;
  4376. }
  4377. std::ostringstream e;
  4378. /* clang-format off */
  4379. e << "Error evaluating generator expression:\n"
  4380. << " " << expr << "\n"
  4381. << result;
  4382. /* clang-format on */
  4383. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  4384. e.str(), context->Backtrace);
  4385. }