cmGeneratorExpressionNode.cxx 169 KB

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