cmGeneratorExpressionNode.cxx 166 KB

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