cmGeneratorExpressionNode.cxx 166 KB

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