cmGeneratorExpressionNode.cxx 180 KB

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