cmGeneratorExpressionNode.cxx 169 KB

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