cmGeneratorExpressionNode.cxx 178 KB

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