cmGeneratorExpressionNode.cxx 178 KB

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