cmGeneratorExpressionNode.cxx 165 KB

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