cmGeneratorExpressionNode.cxx 165 KB

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