cmGeneratorExpressionNode.cxx 165 KB

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