cmGeneratorExpressionNode.cxx 166 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGeneratorExpressionNode.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cerrno>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <functional>
  10. #include <map>
  11. #include <memory>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdexcept>
  15. #include <unordered_map>
  16. #include <utility>
  17. #include <cm/iterator>
  18. #include <cm/optional>
  19. #include <cm/string_view>
  20. #include <cmext/algorithm>
  21. #include <cmext/string_view>
  22. #include "cmsys/RegularExpression.hxx"
  23. #include "cmsys/String.h"
  24. #include "cmCMakePath.h"
  25. #include "cmComputeLinkInformation.h"
  26. #include "cmGeneratorExpression.h"
  27. #include "cmGeneratorExpressionContext.h"
  28. #include "cmGeneratorExpressionDAGChecker.h"
  29. #include "cmGeneratorExpressionEvaluator.h"
  30. #include "cmGeneratorTarget.h"
  31. #include "cmGlobalGenerator.h"
  32. #include "cmLinkItem.h"
  33. #include "cmList.h"
  34. #include "cmLocalGenerator.h"
  35. #include "cmMakefile.h"
  36. #include "cmMessageType.h"
  37. #include "cmOutputConverter.h"
  38. #include "cmPolicies.h"
  39. #include "cmRange.h"
  40. #include "cmStandardLevelResolver.h"
  41. #include "cmState.h"
  42. #include "cmStateSnapshot.h"
  43. #include "cmStateTypes.h"
  44. #include "cmStringAlgorithms.h"
  45. #include "cmSystemTools.h"
  46. #include "cmTarget.h"
  47. #include "cmValue.h"
  48. #include "cmake.h"
  49. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  50. std::string const& prop, cmLocalGenerator* lg,
  51. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  52. cmGeneratorExpressionDAGChecker* dagChecker,
  53. cmGeneratorTarget const* currentTarget)
  54. {
  55. cmGeneratorExpression ge(*lg->GetCMakeInstance(), context->Backtrace);
  56. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  57. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  58. cge->SetQuiet(context->Quiet);
  59. std::string result =
  60. cge->Evaluate(lg, context->Config, headTarget, dagChecker, currentTarget,
  61. context->Language);
  62. if (cge->GetHadContextSensitiveCondition()) {
  63. context->HadContextSensitiveCondition = true;
  64. }
  65. if (cge->GetHadHeadSensitiveCondition()) {
  66. context->HadHeadSensitiveCondition = true;
  67. }
  68. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  69. context->HadLinkLanguageSensitiveCondition = true;
  70. }
  71. return result;
  72. }
  73. static const struct ZeroNode : public cmGeneratorExpressionNode
  74. {
  75. ZeroNode() {} // NOLINT(modernize-use-equals-default)
  76. bool GeneratesContent() const override { return false; }
  77. bool AcceptsArbitraryContentParameter() const override { return true; }
  78. std::string Evaluate(
  79. const std::vector<std::string>& /*parameters*/,
  80. cmGeneratorExpressionContext* /*context*/,
  81. const GeneratorExpressionContent* /*content*/,
  82. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  83. {
  84. return std::string();
  85. }
  86. } zeroNode;
  87. static const struct OneNode : public cmGeneratorExpressionNode
  88. {
  89. OneNode() {} // NOLINT(modernize-use-equals-default)
  90. bool AcceptsArbitraryContentParameter() const override { return true; }
  91. std::string Evaluate(
  92. const std::vector<std::string>& parameters,
  93. cmGeneratorExpressionContext* /*context*/,
  94. const GeneratorExpressionContent* /*content*/,
  95. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  96. {
  97. return parameters.front();
  98. }
  99. } oneNode;
  100. static const struct OneNode buildInterfaceNode;
  101. static const struct ZeroNode installInterfaceNode;
  102. static const struct OneNode buildLocalInterfaceNode;
  103. struct BooleanOpNode : public cmGeneratorExpressionNode
  104. {
  105. BooleanOpNode(const char* op_, const char* successVal_,
  106. const char* failureVal_)
  107. : op(op_)
  108. , successVal(successVal_)
  109. , failureVal(failureVal_)
  110. {
  111. }
  112. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  113. bool ShouldEvaluateNextParameter(const std::vector<std::string>& parameters,
  114. std::string& def_value) const override
  115. {
  116. if (!parameters.empty() && parameters.back() == failureVal) {
  117. def_value = failureVal;
  118. return false;
  119. }
  120. return true;
  121. }
  122. std::string Evaluate(const std::vector<std::string>& parameters,
  123. cmGeneratorExpressionContext* context,
  124. const GeneratorExpressionContent* content,
  125. cmGeneratorExpressionDAGChecker*) const override
  126. {
  127. for (std::string const& param : parameters) {
  128. if (param == this->failureVal) {
  129. return this->failureVal;
  130. }
  131. if (param != this->successVal) {
  132. std::ostringstream e;
  133. e << "Parameters to $<" << this->op;
  134. e << "> must resolve to either '0' or '1'.";
  135. reportError(context, content->GetOriginalExpression(), e.str());
  136. return std::string();
  137. }
  138. }
  139. return this->successVal;
  140. }
  141. const char *const op, *const successVal, *const failureVal;
  142. };
  143. static const BooleanOpNode andNode("AND", "1", "0"), orNode("OR", "0", "1");
  144. static const struct NotNode : public cmGeneratorExpressionNode
  145. {
  146. NotNode() {} // NOLINT(modernize-use-equals-default)
  147. std::string Evaluate(
  148. const std::vector<std::string>& parameters,
  149. cmGeneratorExpressionContext* context,
  150. const GeneratorExpressionContent* content,
  151. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  152. {
  153. if (parameters.front() != "0" && parameters.front() != "1") {
  154. reportError(
  155. context, content->GetOriginalExpression(),
  156. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  157. return std::string();
  158. }
  159. return parameters.front() == "0" ? "1" : "0";
  160. }
  161. } notNode;
  162. static const struct BoolNode : public cmGeneratorExpressionNode
  163. {
  164. BoolNode() {} // NOLINT(modernize-use-equals-default)
  165. int NumExpectedParameters() const override { return 1; }
  166. std::string Evaluate(
  167. const std::vector<std::string>& parameters,
  168. cmGeneratorExpressionContext* /*context*/,
  169. const GeneratorExpressionContent* /*content*/,
  170. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  171. {
  172. return !cmIsOff(parameters.front()) ? "1" : "0";
  173. }
  174. } boolNode;
  175. static const struct IfNode : public cmGeneratorExpressionNode
  176. {
  177. IfNode() {} // NOLINT(modernize-use-equals-default)
  178. int NumExpectedParameters() const override { return 3; }
  179. bool ShouldEvaluateNextParameter(const std::vector<std::string>& parameters,
  180. std::string&) const override
  181. {
  182. return (parameters.empty() ||
  183. parameters[0] != cmStrCat(parameters.size() - 1, ""));
  184. }
  185. std::string Evaluate(const std::vector<std::string>& parameters,
  186. cmGeneratorExpressionContext* context,
  187. const GeneratorExpressionContent* content,
  188. cmGeneratorExpressionDAGChecker*) const override
  189. {
  190. if (parameters[0] != "1" && parameters[0] != "0") {
  191. reportError(context, content->GetOriginalExpression(),
  192. "First parameter to $<IF> must resolve to exactly one '0' "
  193. "or '1' value.");
  194. return std::string();
  195. }
  196. return parameters[0] == "1" ? parameters[1] : parameters[2];
  197. }
  198. } ifNode;
  199. static const struct StrEqualNode : public cmGeneratorExpressionNode
  200. {
  201. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  202. int NumExpectedParameters() const override { return 2; }
  203. std::string Evaluate(
  204. const std::vector<std::string>& parameters,
  205. cmGeneratorExpressionContext* /*context*/,
  206. const GeneratorExpressionContent* /*content*/,
  207. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  208. {
  209. return parameters.front() == parameters[1] ? "1" : "0";
  210. }
  211. } strEqualNode;
  212. static const struct EqualNode : public cmGeneratorExpressionNode
  213. {
  214. EqualNode() {} // NOLINT(modernize-use-equals-default)
  215. int NumExpectedParameters() const override { return 2; }
  216. std::string Evaluate(
  217. const std::vector<std::string>& parameters,
  218. cmGeneratorExpressionContext* context,
  219. const GeneratorExpressionContent* content,
  220. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  221. {
  222. long numbers[2];
  223. for (int i = 0; i < 2; ++i) {
  224. if (!ParameterToLong(parameters[i].c_str(), &numbers[i])) {
  225. reportError(context, content->GetOriginalExpression(),
  226. "$<EQUAL> parameter " + parameters[i] +
  227. " is not a valid integer.");
  228. return {};
  229. }
  230. }
  231. return numbers[0] == numbers[1] ? "1" : "0";
  232. }
  233. static bool ParameterToLong(const char* param, long* outResult)
  234. {
  235. const char isNegative = param[0] == '-';
  236. int base = 0;
  237. if (cmHasLiteralPrefix(param, "0b") || cmHasLiteralPrefix(param, "0B")) {
  238. base = 2;
  239. param += 2;
  240. } else if (cmHasLiteralPrefix(param, "-0b") ||
  241. cmHasLiteralPrefix(param, "-0B") ||
  242. cmHasLiteralPrefix(param, "+0b") ||
  243. cmHasLiteralPrefix(param, "+0B")) {
  244. base = 2;
  245. param += 3;
  246. }
  247. char* pEnd;
  248. long result = strtol(param, &pEnd, base);
  249. if (pEnd == param || *pEnd != '\0' || errno == ERANGE) {
  250. return false;
  251. }
  252. if (isNegative && result > 0) {
  253. result *= -1;
  254. }
  255. *outResult = result;
  256. return true;
  257. }
  258. } equalNode;
  259. static const struct InListNode : public cmGeneratorExpressionNode
  260. {
  261. InListNode() {} // NOLINT(modernize-use-equals-default)
  262. int NumExpectedParameters() const override { return 2; }
  263. std::string Evaluate(
  264. const std::vector<std::string>& parameters,
  265. cmGeneratorExpressionContext* context,
  266. const GeneratorExpressionContent* /*content*/,
  267. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  268. {
  269. cmList values;
  270. cmList checkValues;
  271. bool check = false;
  272. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  273. case cmPolicies::WARN:
  274. if (parameters.front().empty()) {
  275. check = true;
  276. checkValues.assign(parameters[1], cmList::EmptyElements::Yes);
  277. }
  278. CM_FALLTHROUGH;
  279. case cmPolicies::OLD:
  280. values.assign(parameters[1]);
  281. if (check && values != checkValues) {
  282. std::ostringstream e;
  283. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  284. << "\nSearch Item:\n \"" << parameters.front()
  285. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  286. context->LG->GetCMakeInstance()->IssueMessage(
  287. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  288. return "0";
  289. }
  290. if (values.empty()) {
  291. return "0";
  292. }
  293. break;
  294. case cmPolicies::REQUIRED_IF_USED:
  295. case cmPolicies::REQUIRED_ALWAYS:
  296. case cmPolicies::NEW:
  297. values.assign(parameters[1], cmList::EmptyElements::Yes);
  298. break;
  299. }
  300. return values.find(parameters.front()) != cmList::npos ? "1" : "0";
  301. }
  302. } inListNode;
  303. static const struct FilterNode : public cmGeneratorExpressionNode
  304. {
  305. FilterNode() {} // NOLINT(modernize-use-equals-default)
  306. int NumExpectedParameters() const override { return 3; }
  307. std::string Evaluate(
  308. const std::vector<std::string>& parameters,
  309. cmGeneratorExpressionContext* context,
  310. const GeneratorExpressionContent* content,
  311. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  312. {
  313. if (parameters.size() != 3) {
  314. reportError(context, content->GetOriginalExpression(),
  315. "$<FILTER:...> expression requires three parameters");
  316. return {};
  317. }
  318. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  319. reportError(
  320. context, content->GetOriginalExpression(),
  321. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  322. return {};
  323. }
  324. try {
  325. return cmList{ parameters.front(), cmList::EmptyElements::Yes }
  326. .filter(parameters[2],
  327. parameters[1] == "EXCLUDE" ? cmList::FilterMode::EXCLUDE
  328. : cmList::FilterMode::INCLUDE)
  329. .to_string();
  330. } catch (std::invalid_argument&) {
  331. reportError(context, content->GetOriginalExpression(),
  332. "$<FILTER:...> failed to compile regex");
  333. return {};
  334. }
  335. }
  336. } filterNode;
  337. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  338. {
  339. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  340. int NumExpectedParameters() const override { return 1; }
  341. std::string Evaluate(
  342. const std::vector<std::string>& parameters,
  343. cmGeneratorExpressionContext* context,
  344. const GeneratorExpressionContent* content,
  345. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  346. {
  347. if (parameters.size() != 1) {
  348. reportError(
  349. context, content->GetOriginalExpression(),
  350. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  351. }
  352. return cmList{ parameters.front(), cmList::EmptyElements::Yes }
  353. .remove_duplicates()
  354. .to_string();
  355. }
  356. } removeDuplicatesNode;
  357. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  358. {
  359. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  360. int NumExpectedParameters() const override { return 1; }
  361. std::string Evaluate(
  362. const std::vector<std::string>& parameters,
  363. cmGeneratorExpressionContext* context,
  364. const GeneratorExpressionContent* content,
  365. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  366. {
  367. if (parameters.size() != 1) {
  368. reportError(context, content->GetOriginalExpression(),
  369. "$<TARGET_EXISTS:...> expression requires one parameter");
  370. return std::string();
  371. }
  372. std::string const& targetName = parameters.front();
  373. if (targetName.empty() ||
  374. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  375. reportError(context, content->GetOriginalExpression(),
  376. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  377. "valid target name.");
  378. return std::string();
  379. }
  380. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  381. }
  382. } targetExistsNode;
  383. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  384. {
  385. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  386. int NumExpectedParameters() const override { return 1; }
  387. std::string Evaluate(
  388. const std::vector<std::string>& parameters,
  389. cmGeneratorExpressionContext* context,
  390. const GeneratorExpressionContent* content,
  391. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  392. {
  393. if (parameters.size() != 1) {
  394. reportError(context, content->GetOriginalExpression(),
  395. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  396. "parameter");
  397. return std::string();
  398. }
  399. std::string const& targetName = parameters.front();
  400. if (targetName.empty() ||
  401. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  402. reportError(context, content->GetOriginalExpression(),
  403. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  404. "non-empty valid target name.");
  405. return std::string();
  406. }
  407. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  408. ? targetName
  409. : std::string();
  410. }
  411. } targetNameIfExistsNode;
  412. struct GenexEvaluator : public cmGeneratorExpressionNode
  413. {
  414. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  415. protected:
  416. std::string EvaluateExpression(
  417. const std::string& genexOperator, const std::string& expression,
  418. cmGeneratorExpressionContext* context,
  419. const GeneratorExpressionContent* content,
  420. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  421. {
  422. if (context->HeadTarget) {
  423. cmGeneratorExpressionDAGChecker dagChecker(
  424. context->Backtrace, context->HeadTarget,
  425. genexOperator + ":" + expression, content, dagCheckerParent);
  426. switch (dagChecker.Check()) {
  427. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  428. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  429. dagChecker.ReportError(context, content->GetOriginalExpression());
  430. return std::string();
  431. }
  432. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  433. case cmGeneratorExpressionDAGChecker::DAG:
  434. break;
  435. }
  436. return this->EvaluateDependentExpression(
  437. expression, context->LG, context, context->HeadTarget, &dagChecker,
  438. context->CurrentTarget);
  439. }
  440. return this->EvaluateDependentExpression(
  441. expression, context->LG, context, context->HeadTarget, dagCheckerParent,
  442. context->CurrentTarget);
  443. }
  444. };
  445. static const struct TargetGenexEvalNode : public GenexEvaluator
  446. {
  447. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  448. int NumExpectedParameters() const override { return 2; }
  449. bool AcceptsArbitraryContentParameter() const override { return true; }
  450. std::string Evaluate(
  451. const std::vector<std::string>& parameters,
  452. cmGeneratorExpressionContext* context,
  453. const GeneratorExpressionContent* content,
  454. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  455. {
  456. const std::string& targetName = parameters.front();
  457. if (targetName.empty() ||
  458. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  459. reportError(context, content->GetOriginalExpression(),
  460. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  461. "non-empty valid target name.");
  462. return std::string();
  463. }
  464. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  465. if (!target) {
  466. std::ostringstream e;
  467. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  468. << "\" not found.";
  469. reportError(context, content->GetOriginalExpression(), e.str());
  470. return std::string();
  471. }
  472. const std::string& expression = parameters[1];
  473. if (expression.empty()) {
  474. return expression;
  475. }
  476. 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. static const CharacterNode<'"'> quoteNode;
  1710. struct CompilerIdNode : public cmGeneratorExpressionNode
  1711. {
  1712. CompilerIdNode(const char* compilerLang)
  1713. : CompilerLanguage(compilerLang)
  1714. {
  1715. }
  1716. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1717. std::string Evaluate(
  1718. const std::vector<std::string>& parameters,
  1719. cmGeneratorExpressionContext* context,
  1720. const GeneratorExpressionContent* content,
  1721. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1722. {
  1723. if (!context->HeadTarget) {
  1724. std::ostringstream e;
  1725. e << "$<" << this->CompilerLanguage
  1726. << "_COMPILER_ID> may only be used with binary targets. It may "
  1727. "not be used with add_custom_command or add_custom_target.";
  1728. reportError(context, content->GetOriginalExpression(), e.str());
  1729. return {};
  1730. }
  1731. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1732. this->CompilerLanguage);
  1733. }
  1734. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  1735. cmGeneratorExpressionContext* context,
  1736. const GeneratorExpressionContent* content,
  1737. cmGeneratorExpressionDAGChecker* /*unused*/,
  1738. const std::string& lang) const
  1739. {
  1740. std::string const& compilerId =
  1741. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1742. "_COMPILER_ID");
  1743. if (parameters.empty()) {
  1744. return compilerId;
  1745. }
  1746. if (compilerId.empty()) {
  1747. return parameters.front().empty() ? "1" : "0";
  1748. }
  1749. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  1750. for (auto const& param : parameters) {
  1751. if (!compilerIdValidator.find(param)) {
  1752. reportError(context, content->GetOriginalExpression(),
  1753. "Expression syntax not recognized.");
  1754. return std::string();
  1755. }
  1756. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  1757. return "1";
  1758. }
  1759. if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
  1760. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  1761. case cmPolicies::WARN: {
  1762. context->LG->GetCMakeInstance()->IssueMessage(
  1763. MessageType::AUTHOR_WARNING,
  1764. cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
  1765. context->Backtrace);
  1766. CM_FALLTHROUGH;
  1767. }
  1768. case cmPolicies::OLD:
  1769. return "1";
  1770. case cmPolicies::NEW:
  1771. case cmPolicies::REQUIRED_ALWAYS:
  1772. case cmPolicies::REQUIRED_IF_USED:
  1773. break;
  1774. }
  1775. }
  1776. }
  1777. return "0";
  1778. }
  1779. const char* const CompilerLanguage;
  1780. };
  1781. static const CompilerIdNode cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  1782. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  1783. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  1784. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  1785. struct CompilerVersionNode : public cmGeneratorExpressionNode
  1786. {
  1787. CompilerVersionNode(const char* compilerLang)
  1788. : CompilerLanguage(compilerLang)
  1789. {
  1790. }
  1791. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  1792. std::string Evaluate(
  1793. const std::vector<std::string>& parameters,
  1794. cmGeneratorExpressionContext* context,
  1795. const GeneratorExpressionContent* content,
  1796. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1797. {
  1798. if (!context->HeadTarget) {
  1799. std::ostringstream e;
  1800. e << "$<" << this->CompilerLanguage
  1801. << "_COMPILER_VERSION> may only be used with binary targets. It "
  1802. "may not be used with add_custom_command or add_custom_target.";
  1803. reportError(context, content->GetOriginalExpression(), e.str());
  1804. return {};
  1805. }
  1806. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1807. this->CompilerLanguage);
  1808. }
  1809. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  1810. cmGeneratorExpressionContext* context,
  1811. const GeneratorExpressionContent* content,
  1812. cmGeneratorExpressionDAGChecker* /*unused*/,
  1813. const std::string& lang) const
  1814. {
  1815. std::string const& compilerVersion =
  1816. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1817. "_COMPILER_VERSION");
  1818. if (parameters.empty()) {
  1819. return compilerVersion;
  1820. }
  1821. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  1822. if (!compilerIdValidator.find(parameters.front())) {
  1823. reportError(context, content->GetOriginalExpression(),
  1824. "Expression syntax not recognized.");
  1825. return {};
  1826. }
  1827. if (compilerVersion.empty()) {
  1828. return parameters.front().empty() ? "1" : "0";
  1829. }
  1830. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  1831. parameters.front(), compilerVersion)
  1832. ? "1"
  1833. : "0";
  1834. }
  1835. const char* const CompilerLanguage;
  1836. };
  1837. static const CompilerVersionNode cCompilerVersionNode("C"),
  1838. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  1839. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  1840. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  1841. hipCompilerVersionNode("HIP");
  1842. struct PlatformIdNode : public cmGeneratorExpressionNode
  1843. {
  1844. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  1845. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1846. std::string Evaluate(
  1847. const std::vector<std::string>& parameters,
  1848. cmGeneratorExpressionContext* context,
  1849. const GeneratorExpressionContent* /*content*/,
  1850. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1851. {
  1852. std::string const& platformId =
  1853. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  1854. if (parameters.empty()) {
  1855. return platformId;
  1856. }
  1857. if (platformId.empty()) {
  1858. return parameters.front().empty() ? "1" : "0";
  1859. }
  1860. for (auto const& param : parameters) {
  1861. if (param == platformId) {
  1862. return "1";
  1863. }
  1864. }
  1865. return "0";
  1866. }
  1867. };
  1868. static struct PlatformIdNode platformIdNode;
  1869. template <cmSystemTools::CompareOp Op>
  1870. struct VersionNode : public cmGeneratorExpressionNode
  1871. {
  1872. VersionNode() {} // NOLINT(modernize-use-equals-default)
  1873. int NumExpectedParameters() const override { return 2; }
  1874. std::string Evaluate(
  1875. const std::vector<std::string>& parameters,
  1876. cmGeneratorExpressionContext* /*context*/,
  1877. const GeneratorExpressionContent* /*content*/,
  1878. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1879. {
  1880. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  1881. ? "1"
  1882. : "0";
  1883. }
  1884. };
  1885. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  1886. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  1887. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  1888. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  1889. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  1890. static const struct CompileOnlyNode : public cmGeneratorExpressionNode
  1891. {
  1892. CompileOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1893. std::string Evaluate(
  1894. const std::vector<std::string>& parameters,
  1895. cmGeneratorExpressionContext* context,
  1896. const GeneratorExpressionContent* content,
  1897. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1898. {
  1899. if (!dagChecker) {
  1900. reportError(context, content->GetOriginalExpression(),
  1901. "$<COMPILE_ONLY:...> may only be used for linking");
  1902. return std::string();
  1903. }
  1904. if (dagChecker->GetTransitivePropertiesOnly()) {
  1905. return parameters.front();
  1906. }
  1907. return std::string{};
  1908. }
  1909. } compileOnlyNode;
  1910. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  1911. {
  1912. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1913. std::string Evaluate(
  1914. const std::vector<std::string>& parameters,
  1915. cmGeneratorExpressionContext* context,
  1916. const GeneratorExpressionContent* content,
  1917. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1918. {
  1919. if (!dagChecker) {
  1920. reportError(context, content->GetOriginalExpression(),
  1921. "$<LINK_ONLY:...> may only be used for linking");
  1922. return std::string();
  1923. }
  1924. if (!dagChecker->GetTransitivePropertiesOnlyCMP0131()) {
  1925. return parameters.front();
  1926. }
  1927. return std::string();
  1928. }
  1929. } linkOnlyNode;
  1930. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  1931. {
  1932. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  1933. int NumExpectedParameters() const override { return 0; }
  1934. std::string Evaluate(
  1935. const std::vector<std::string>& /*parameters*/,
  1936. cmGeneratorExpressionContext* context,
  1937. const GeneratorExpressionContent* /*content*/,
  1938. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1939. {
  1940. context->HadContextSensitiveCondition = true;
  1941. return context->Config;
  1942. }
  1943. } configurationNode;
  1944. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  1945. {
  1946. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  1947. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1948. std::string Evaluate(
  1949. const std::vector<std::string>& parameters,
  1950. cmGeneratorExpressionContext* context,
  1951. const GeneratorExpressionContent* content,
  1952. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1953. {
  1954. if (parameters.empty()) {
  1955. return configurationNode.Evaluate(parameters, context, content, nullptr);
  1956. }
  1957. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  1958. if (!configValidator.find(parameters.front())) {
  1959. }
  1960. context->HadContextSensitiveCondition = true;
  1961. bool firstParam = true;
  1962. for (auto const& param : parameters) {
  1963. if (!configValidator.find(param)) {
  1964. if (firstParam) {
  1965. reportError(context, content->GetOriginalExpression(),
  1966. "Expression syntax not recognized.");
  1967. return std::string();
  1968. }
  1969. // for backwards compat invalid config names are only errors as
  1970. // the first parameter
  1971. std::ostringstream e;
  1972. /* clang-format off */
  1973. e << "Warning evaluating generator expression:\n"
  1974. << " " << content->GetOriginalExpression() << "\n"
  1975. << "The config name of \"" << param << "\" is invalid";
  1976. /* clang-format on */
  1977. context->LG->GetCMakeInstance()->IssueMessage(
  1978. MessageType::WARNING, e.str(), context->Backtrace);
  1979. }
  1980. firstParam = false;
  1981. if (context->Config.empty()) {
  1982. if (param.empty()) {
  1983. return "1";
  1984. }
  1985. } else if (cmsysString_strcasecmp(param.c_str(),
  1986. context->Config.c_str()) == 0) {
  1987. return "1";
  1988. }
  1989. }
  1990. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  1991. cmValue loc = nullptr;
  1992. cmValue imp = nullptr;
  1993. std::string suffix;
  1994. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  1995. imp, suffix)) {
  1996. // This imported target has an appropriate location
  1997. // for this (possibly mapped) config.
  1998. // Check if there is a proper config mapping for the tested config.
  1999. cmList mappedConfigs;
  2000. std::string mapProp = cmStrCat(
  2001. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  2002. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  2003. mappedConfigs.assign(cmSystemTools::UpperCase(*mapValue));
  2004. for (auto const& param : parameters) {
  2005. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  2006. return "1";
  2007. }
  2008. }
  2009. } else if (!suffix.empty()) {
  2010. // There is no explicit mapping for the tested config, so use
  2011. // the configuration of the imported location that was selected.
  2012. for (auto const& param : parameters) {
  2013. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  2014. return "1";
  2015. }
  2016. }
  2017. }
  2018. }
  2019. }
  2020. return "0";
  2021. }
  2022. } configurationTestNode;
  2023. static const struct JoinNode : public cmGeneratorExpressionNode
  2024. {
  2025. JoinNode() {} // NOLINT(modernize-use-equals-default)
  2026. int NumExpectedParameters() const override { return 2; }
  2027. bool AcceptsArbitraryContentParameter() const override { return true; }
  2028. std::string Evaluate(
  2029. const std::vector<std::string>& parameters,
  2030. cmGeneratorExpressionContext* /*context*/,
  2031. const GeneratorExpressionContent* /*content*/,
  2032. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2033. {
  2034. return cmList{ parameters.front() }.join(parameters[1]);
  2035. }
  2036. } joinNode;
  2037. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  2038. {
  2039. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2040. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2041. std::string Evaluate(
  2042. const std::vector<std::string>& parameters,
  2043. cmGeneratorExpressionContext* context,
  2044. const GeneratorExpressionContent* content,
  2045. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2046. {
  2047. if (context->Language.empty() &&
  2048. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  2049. reportError(
  2050. context, content->GetOriginalExpression(),
  2051. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  2052. "directories, compile definitions, compile options, and to evaluate "
  2053. "components of the file(GENERATE) command.");
  2054. return std::string();
  2055. }
  2056. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2057. std::string genName = gg->GetName();
  2058. if (genName.find("Makefiles") == std::string::npos &&
  2059. genName.find("Ninja") == std::string::npos &&
  2060. genName.find("Visual Studio") == std::string::npos &&
  2061. genName.find("Xcode") == std::string::npos &&
  2062. genName.find("Watcom WMake") == std::string::npos &&
  2063. genName.find("Green Hills MULTI") == std::string::npos) {
  2064. reportError(context, content->GetOriginalExpression(),
  2065. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  2066. return std::string();
  2067. }
  2068. if (parameters.empty()) {
  2069. return context->Language;
  2070. }
  2071. for (auto const& param : parameters) {
  2072. if (context->Language == param) {
  2073. return "1";
  2074. }
  2075. }
  2076. return "0";
  2077. }
  2078. } languageNode;
  2079. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  2080. {
  2081. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2082. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2083. std::string Evaluate(
  2084. const std::vector<std::string>& parameters,
  2085. cmGeneratorExpressionContext* context,
  2086. const GeneratorExpressionContent* content,
  2087. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2088. {
  2089. if (!context->HeadTarget ||
  2090. (context->Language.empty() &&
  2091. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  2092. // reportError(context, content->GetOriginalExpression(), "");
  2093. reportError(
  2094. context, content->GetOriginalExpression(),
  2095. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary "
  2096. "targets "
  2097. "to specify include directories, compile definitions, and compile "
  2098. "options. It may not be used with the add_custom_command, "
  2099. "add_custom_target, or file(GENERATE) commands.");
  2100. return std::string();
  2101. }
  2102. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2103. std::string genName = gg->GetName();
  2104. if (genName.find("Makefiles") == std::string::npos &&
  2105. genName.find("Ninja") == std::string::npos &&
  2106. genName.find("Visual Studio") == std::string::npos &&
  2107. genName.find("Xcode") == std::string::npos &&
  2108. genName.find("Watcom WMake") == std::string::npos &&
  2109. genName.find("Green Hills MULTI") == std::string::npos) {
  2110. reportError(
  2111. context, content->GetOriginalExpression(),
  2112. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  2113. return std::string();
  2114. }
  2115. const std::string& lang = context->Language;
  2116. if (lang == parameters.front()) {
  2117. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2118. parameters.cend());
  2119. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  2120. idParameter, context, content, dagChecker, lang);
  2121. }
  2122. return "0";
  2123. }
  2124. } languageAndIdNode;
  2125. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  2126. {
  2127. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2128. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2129. std::string Evaluate(
  2130. const std::vector<std::string>& parameters,
  2131. cmGeneratorExpressionContext* context,
  2132. const GeneratorExpressionContent* content,
  2133. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2134. {
  2135. if (!context->HeadTarget || !dagChecker ||
  2136. !(dagChecker->EvaluatingLinkExpression() ||
  2137. dagChecker->EvaluatingLinkLibraries() ||
  2138. dagChecker->EvaluatingLinkerLauncher())) {
  2139. reportError(context, content->GetOriginalExpression(),
  2140. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  2141. "to specify link libraries, link directories, link options "
  2142. "and link depends.");
  2143. return std::string();
  2144. }
  2145. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  2146. reportError(
  2147. context, content->GetOriginalExpression(),
  2148. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  2149. return std::string();
  2150. }
  2151. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2152. std::string genName = gg->GetName();
  2153. if (genName.find("Makefiles") == std::string::npos &&
  2154. genName.find("Ninja") == std::string::npos &&
  2155. genName.find("Visual Studio") == std::string::npos &&
  2156. genName.find("Xcode") == std::string::npos &&
  2157. genName.find("Watcom WMake") == std::string::npos &&
  2158. genName.find("Green Hills MULTI") == std::string::npos) {
  2159. reportError(context, content->GetOriginalExpression(),
  2160. "$<LINK_LANGUAGE:...> not supported for this generator.");
  2161. return std::string();
  2162. }
  2163. if (dagChecker->EvaluatingLinkLibraries()) {
  2164. context->HadHeadSensitiveCondition = true;
  2165. context->HadLinkLanguageSensitiveCondition = true;
  2166. }
  2167. if (parameters.empty()) {
  2168. return context->Language;
  2169. }
  2170. for (auto const& param : parameters) {
  2171. if (context->Language == param) {
  2172. return "1";
  2173. }
  2174. }
  2175. return "0";
  2176. }
  2177. } linkLanguageNode;
  2178. namespace {
  2179. struct LinkerId
  2180. {
  2181. static std::string Evaluate(const std::vector<std::string>& parameters,
  2182. cmGeneratorExpressionContext* context,
  2183. const GeneratorExpressionContent* content,
  2184. const std::string& lang)
  2185. {
  2186. std::string const& linkerId =
  2187. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  2188. "_COMPILER_ID");
  2189. if (parameters.empty()) {
  2190. return linkerId;
  2191. }
  2192. if (linkerId.empty()) {
  2193. return parameters.front().empty() ? "1" : "0";
  2194. }
  2195. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  2196. for (auto const& param : parameters) {
  2197. if (!linkerIdValidator.find(param)) {
  2198. reportError(context, content->GetOriginalExpression(),
  2199. "Expression syntax not recognized.");
  2200. return std::string();
  2201. }
  2202. if (param == linkerId) {
  2203. return "1";
  2204. }
  2205. }
  2206. return "0";
  2207. }
  2208. };
  2209. }
  2210. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  2211. {
  2212. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2213. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2214. std::string Evaluate(
  2215. const std::vector<std::string>& parameters,
  2216. cmGeneratorExpressionContext* context,
  2217. const GeneratorExpressionContent* content,
  2218. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2219. {
  2220. if (!context->HeadTarget || !dagChecker ||
  2221. !(dagChecker->EvaluatingLinkExpression() ||
  2222. dagChecker->EvaluatingLinkLibraries() ||
  2223. dagChecker->EvaluatingLinkerLauncher())) {
  2224. reportError(
  2225. context, content->GetOriginalExpression(),
  2226. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  2227. "to specify link libraries, link directories, link options, and "
  2228. "link "
  2229. "depends.");
  2230. return std::string();
  2231. }
  2232. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2233. std::string genName = gg->GetName();
  2234. if (genName.find("Makefiles") == std::string::npos &&
  2235. genName.find("Ninja") == std::string::npos &&
  2236. genName.find("Visual Studio") == std::string::npos &&
  2237. genName.find("Xcode") == std::string::npos &&
  2238. genName.find("Watcom WMake") == std::string::npos &&
  2239. genName.find("Green Hills MULTI") == std::string::npos) {
  2240. reportError(
  2241. context, content->GetOriginalExpression(),
  2242. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  2243. return std::string();
  2244. }
  2245. if (dagChecker->EvaluatingLinkLibraries()) {
  2246. context->HadHeadSensitiveCondition = true;
  2247. context->HadLinkLanguageSensitiveCondition = true;
  2248. }
  2249. const std::string& lang = context->Language;
  2250. if (lang == parameters.front()) {
  2251. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2252. parameters.cend());
  2253. return LinkerId::Evaluate(idParameter, context, content, lang);
  2254. }
  2255. return "0";
  2256. }
  2257. } linkLanguageAndIdNode;
  2258. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  2259. {
  2260. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  2261. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2262. std::string Evaluate(
  2263. const std::vector<std::string>& parameters,
  2264. cmGeneratorExpressionContext* context,
  2265. const GeneratorExpressionContent* content,
  2266. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2267. {
  2268. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2269. if (!context->HeadTarget || !dagChecker ||
  2270. !dagChecker->EvaluatingLinkLibraries(nullptr,
  2271. ForGenex::LINK_LIBRARY)) {
  2272. reportError(context, content->GetOriginalExpression(),
  2273. "$<LINK_LIBRARY:...> may only be used with binary targets "
  2274. "to specify link libraries through 'LINK_LIBRARIES', "
  2275. "'INTERFACE_LINK_LIBRARIES', and "
  2276. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2277. return std::string();
  2278. }
  2279. cmList list{ parameters.begin(), parameters.end() };
  2280. if (list.empty()) {
  2281. reportError(
  2282. context, content->GetOriginalExpression(),
  2283. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  2284. return std::string();
  2285. }
  2286. if (list.size() == 1) {
  2287. // no libraries specified, ignore this genex
  2288. return std::string();
  2289. }
  2290. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2291. auto const& feature = list.front();
  2292. if (!featureNameValidator.find(feature)) {
  2293. reportError(context, content->GetOriginalExpression(),
  2294. cmStrCat("The feature name '", feature,
  2295. "' contains invalid characters."));
  2296. return std::string();
  2297. }
  2298. const auto LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  2299. const auto LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  2300. // filter out $<LINK_LIBRARY:..> tags with same feature
  2301. // and raise an error for any different feature
  2302. cm::erase_if(list, [&](const std::string& item) -> bool {
  2303. return item == LL_BEGIN || item == LL_END;
  2304. });
  2305. auto it =
  2306. std::find_if(list.cbegin() + 1, list.cend(),
  2307. [&feature](const std::string& item) -> bool {
  2308. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  2309. item.substr(14, item.find('>', 14) - 14) != feature;
  2310. });
  2311. if (it != list.cend()) {
  2312. reportError(
  2313. context, content->GetOriginalExpression(),
  2314. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  2315. return std::string();
  2316. }
  2317. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  2318. it = std::find_if(list.cbegin() + 1, list.cend(),
  2319. [](const std::string& item) -> bool {
  2320. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  2321. });
  2322. if (it != list.cend()) {
  2323. reportError(context, content->GetOriginalExpression(),
  2324. "$<LINK_GROUP:...> cannot be nested inside a "
  2325. "$<LINK_LIBRARY:...> expression.");
  2326. return std::string();
  2327. }
  2328. list.front() = LL_BEGIN;
  2329. list.push_back(LL_END);
  2330. return list.to_string();
  2331. }
  2332. } linkLibraryNode;
  2333. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  2334. {
  2335. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  2336. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2337. std::string Evaluate(
  2338. const std::vector<std::string>& parameters,
  2339. cmGeneratorExpressionContext* context,
  2340. const GeneratorExpressionContent* content,
  2341. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2342. {
  2343. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2344. if (!context->HeadTarget || !dagChecker ||
  2345. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  2346. reportError(
  2347. context, content->GetOriginalExpression(),
  2348. "$<LINK_GROUP:...> may only be used with binary targets "
  2349. "to specify group of link libraries through 'LINK_LIBRARIES', "
  2350. "'INTERFACE_LINK_LIBRARIES', and "
  2351. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2352. return std::string();
  2353. }
  2354. cmList list{ parameters.begin(), parameters.end() };
  2355. if (list.empty()) {
  2356. reportError(
  2357. context, content->GetOriginalExpression(),
  2358. "$<LINK_GROUP:...> expects a feature name as first argument.");
  2359. return std::string();
  2360. }
  2361. // $<LINK_GROUP:..> cannot be nested
  2362. if (std::find_if(list.cbegin(), list.cend(),
  2363. [](const std::string& item) -> bool {
  2364. return cmHasPrefix(item, "<LINK_GROUP"_s);
  2365. }) != list.cend()) {
  2366. reportError(context, content->GetOriginalExpression(),
  2367. "$<LINK_GROUP:...> cannot be nested.");
  2368. return std::string();
  2369. }
  2370. if (list.size() == 1) {
  2371. // no libraries specified, ignore this genex
  2372. return std::string();
  2373. }
  2374. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2375. auto const& feature = list.front();
  2376. if (!featureNameValidator.find(feature)) {
  2377. reportError(context, content->GetOriginalExpression(),
  2378. cmStrCat("The feature name '", feature,
  2379. "' contains invalid characters."));
  2380. return std::string();
  2381. }
  2382. const auto LG_BEGIN = cmStrCat(
  2383. "<LINK_GROUP:", feature, ':',
  2384. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  2385. "|"_s),
  2386. '>');
  2387. const auto LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  2388. list.front() = LG_BEGIN;
  2389. list.push_back(LG_END);
  2390. return list.to_string();
  2391. }
  2392. } linkGroupNode;
  2393. static const struct HostLinkNode : public cmGeneratorExpressionNode
  2394. {
  2395. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  2396. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2397. std::string Evaluate(
  2398. const std::vector<std::string>& parameters,
  2399. cmGeneratorExpressionContext* context,
  2400. const GeneratorExpressionContent* content,
  2401. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2402. {
  2403. if (!context->HeadTarget || !dagChecker ||
  2404. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2405. reportError(context, content->GetOriginalExpression(),
  2406. "$<HOST_LINK:...> may only be used with binary targets "
  2407. "to specify link options.");
  2408. return std::string();
  2409. }
  2410. return context->HeadTarget->IsDeviceLink() ? std::string()
  2411. : cmList::to_string(parameters);
  2412. }
  2413. } hostLinkNode;
  2414. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  2415. {
  2416. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  2417. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2418. std::string Evaluate(
  2419. const std::vector<std::string>& parameters,
  2420. cmGeneratorExpressionContext* context,
  2421. const GeneratorExpressionContent* content,
  2422. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2423. {
  2424. if (!context->HeadTarget || !dagChecker ||
  2425. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2426. reportError(context, content->GetOriginalExpression(),
  2427. "$<DEVICE_LINK:...> may only be used with binary targets "
  2428. "to specify link options.");
  2429. return std::string();
  2430. }
  2431. if (context->HeadTarget->IsDeviceLink()) {
  2432. cmList list{ parameters.begin(), parameters.end() };
  2433. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  2434. const auto DL_END = "</DEVICE_LINK>"_s;
  2435. cm::erase_if(list, [&](const std::string& item) {
  2436. return item == DL_BEGIN || item == DL_END;
  2437. });
  2438. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  2439. list.push_back(static_cast<std::string>(DL_END));
  2440. return list.to_string();
  2441. }
  2442. return std::string();
  2443. }
  2444. } deviceLinkNode;
  2445. static std::string getLinkedTargetsContent(
  2446. cmGeneratorTarget const* target, std::string const& prop,
  2447. cmGeneratorExpressionContext* context,
  2448. cmGeneratorExpressionDAGChecker* dagChecker)
  2449. {
  2450. std::string result;
  2451. if (cmLinkImplementationLibraries const* impl =
  2452. target->GetLinkImplementationLibraries(
  2453. context->Config, cmGeneratorTarget::LinkInterfaceFor::Usage)) {
  2454. for (cmLinkImplItem const& lib : impl->Libraries) {
  2455. if (lib.Target) {
  2456. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  2457. // caller's property and hand-evaluate it as if it were compiled.
  2458. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  2459. cmGeneratorExpressionContext libContext(
  2460. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  2461. target, context->EvaluateForBuildsystem, lib.Backtrace,
  2462. context->Language);
  2463. std::string libResult =
  2464. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  2465. if (!libResult.empty()) {
  2466. if (result.empty()) {
  2467. result = std::move(libResult);
  2468. } else {
  2469. result.reserve(result.size() + 1 + libResult.size());
  2470. result += ";";
  2471. result += libResult;
  2472. }
  2473. }
  2474. }
  2475. }
  2476. }
  2477. return result;
  2478. }
  2479. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  2480. {
  2481. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  2482. // This node handles errors on parameter count itself.
  2483. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2484. static const char* GetErrorText(std::string const& targetName,
  2485. std::string const& propertyName)
  2486. {
  2487. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2488. if (targetName.empty() && propertyName.empty()) {
  2489. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2490. "target name and property name.";
  2491. }
  2492. if (targetName.empty()) {
  2493. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2494. "target name.";
  2495. }
  2496. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  2497. if (!propertyNameValidator.find(propertyName)) {
  2498. return "Target name and property name not supported.";
  2499. }
  2500. return "Target name not supported.";
  2501. }
  2502. return nullptr;
  2503. }
  2504. std::string Evaluate(
  2505. const std::vector<std::string>& parameters,
  2506. cmGeneratorExpressionContext* context,
  2507. const GeneratorExpressionContent* content,
  2508. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  2509. {
  2510. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2511. cmGeneratorTarget const* target = nullptr;
  2512. std::string targetName;
  2513. std::string propertyName;
  2514. if (parameters.size() == 2) {
  2515. targetName = parameters[0];
  2516. propertyName = parameters[1];
  2517. if (const char* e = GetErrorText(targetName, propertyName)) {
  2518. reportError(context, content->GetOriginalExpression(), e);
  2519. return std::string();
  2520. }
  2521. if (propertyName == "ALIASED_TARGET"_s) {
  2522. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2523. if (cmGeneratorTarget* tgt =
  2524. context->LG->FindGeneratorTargetToUse(targetName)) {
  2525. return tgt->GetName();
  2526. }
  2527. }
  2528. return std::string();
  2529. }
  2530. if (propertyName == "ALIAS_GLOBAL"_s) {
  2531. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2532. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  2533. ? "TRUE"
  2534. : "FALSE";
  2535. }
  2536. return std::string();
  2537. }
  2538. cmLocalGenerator const* lg = context->CurrentTarget
  2539. ? context->CurrentTarget->GetLocalGenerator()
  2540. : context->LG;
  2541. target = lg->FindGeneratorTargetToUse(targetName);
  2542. if (!target) {
  2543. std::ostringstream e;
  2544. e << "Target \"" << targetName << "\" not found.";
  2545. reportError(context, content->GetOriginalExpression(), e.str());
  2546. return std::string();
  2547. }
  2548. context->AllTargets.insert(target);
  2549. } else if (parameters.size() == 1) {
  2550. target = context->HeadTarget;
  2551. propertyName = parameters[0];
  2552. // Keep track of the properties seen while processing.
  2553. // The evaluation of the LINK_LIBRARIES generator expressions
  2554. // will check this to ensure that properties have one consistent
  2555. // value for all evaluations.
  2556. context->SeenTargetProperties.insert(propertyName);
  2557. context->HadHeadSensitiveCondition = true;
  2558. if (!target) {
  2559. reportError(
  2560. context, content->GetOriginalExpression(),
  2561. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  2562. "It may not be used with add_custom_command or add_custom_target. "
  2563. " "
  2564. " "
  2565. "Specify the target to read a property from using the "
  2566. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  2567. return std::string();
  2568. }
  2569. } else {
  2570. reportError(
  2571. context, content->GetOriginalExpression(),
  2572. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  2573. return std::string();
  2574. }
  2575. if (propertyName == "SOURCES") {
  2576. context->SourceSensitiveTargets.insert(target);
  2577. }
  2578. if (propertyName.empty()) {
  2579. reportError(
  2580. context, content->GetOriginalExpression(),
  2581. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  2582. "name.");
  2583. return std::string();
  2584. }
  2585. if (!propertyNameValidator.find(propertyName)) {
  2586. ::reportError(context, content->GetOriginalExpression(),
  2587. "Property name not supported.");
  2588. return std::string();
  2589. }
  2590. assert(target);
  2591. if (propertyName == "LINKER_LANGUAGE") {
  2592. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  2593. (dagCheckerParent->EvaluatingLinkLibraries() ||
  2594. dagCheckerParent->EvaluatingSources())) {
  2595. reportError(
  2596. context, content->GetOriginalExpression(),
  2597. "LINKER_LANGUAGE target property can not be used while evaluating "
  2598. "link libraries for a static library");
  2599. return std::string();
  2600. }
  2601. return target->GetLinkerLanguage(context->Config);
  2602. }
  2603. std::string interfacePropertyName;
  2604. bool isInterfaceProperty = false;
  2605. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  2606. if (propertyName == #prop) { \
  2607. interfacePropertyName = "INTERFACE_" #prop; \
  2608. } else if (propertyName == "INTERFACE_" #prop) { \
  2609. interfacePropertyName = "INTERFACE_" #prop; \
  2610. isInterfaceProperty = true; \
  2611. } else
  2612. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  2613. // Note that the above macro terminates with an else
  2614. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  2615. cmPolicies::PolicyStatus polSt =
  2616. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  2617. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  2618. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  2619. }
  2620. }
  2621. #undef POPULATE_INTERFACE_PROPERTY_NAME
  2622. bool evaluatingLinkLibraries = false;
  2623. if (dagCheckerParent) {
  2624. if (dagCheckerParent->EvaluatingGenexExpression() ||
  2625. dagCheckerParent->EvaluatingPICExpression() ||
  2626. dagCheckerParent->EvaluatingLinkerLauncher()) {
  2627. // No check required.
  2628. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  2629. evaluatingLinkLibraries = true;
  2630. if (!interfacePropertyName.empty()) {
  2631. reportError(
  2632. context, content->GetOriginalExpression(),
  2633. "$<TARGET_PROPERTY:...> expression in link libraries "
  2634. "evaluation depends on target property which is transitive "
  2635. "over the link libraries, creating a recursion.");
  2636. return std::string();
  2637. }
  2638. } else {
  2639. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  2640. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  2641. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  2642. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  2643. }
  2644. }
  2645. if (isInterfaceProperty) {
  2646. return cmGeneratorExpression::StripEmptyListElements(
  2647. target->EvaluateInterfaceProperty(propertyName, context,
  2648. dagCheckerParent));
  2649. }
  2650. cmGeneratorExpressionDAGChecker dagChecker(
  2651. context->Backtrace, target, propertyName, content, dagCheckerParent);
  2652. switch (dagChecker.Check()) {
  2653. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  2654. dagChecker.ReportError(context, content->GetOriginalExpression());
  2655. return std::string();
  2656. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  2657. // No error. We just skip cyclic references.
  2658. return std::string();
  2659. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  2660. // We handle transitive properties above. For non-transitive
  2661. // properties we accept repeats anyway.
  2662. case cmGeneratorExpressionDAGChecker::DAG:
  2663. break;
  2664. }
  2665. std::string result;
  2666. bool haveProp = false;
  2667. if (cmValue p = target->GetProperty(propertyName)) {
  2668. result = *p;
  2669. haveProp = true;
  2670. } else if (evaluatingLinkLibraries) {
  2671. return std::string();
  2672. }
  2673. if (!haveProp && !target->IsImported() &&
  2674. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2675. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  2676. context->Config)) {
  2677. context->HadContextSensitiveCondition = true;
  2678. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  2679. context->Config)
  2680. ? "1"
  2681. : "0";
  2682. }
  2683. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  2684. context->Config)) {
  2685. context->HadContextSensitiveCondition = true;
  2686. const char* propContent =
  2687. target->GetLinkInterfaceDependentStringProperty(propertyName,
  2688. context->Config);
  2689. return propContent ? propContent : "";
  2690. }
  2691. // FIXME: This duplicates the COMPATIBLE_INTERFACE_NUMBER_{MAX,MIN}
  2692. // evaluation below because it is not reached when evaluating outside of
  2693. // usage requirements, such as in add_custom_target, because there is no
  2694. // dagCheckerParent.
  2695. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  2696. context->Config)) {
  2697. context->HadContextSensitiveCondition = true;
  2698. const char* propContent =
  2699. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  2700. context->Config);
  2701. return propContent ? propContent : "";
  2702. }
  2703. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  2704. context->Config)) {
  2705. context->HadContextSensitiveCondition = true;
  2706. const char* propContent =
  2707. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  2708. context->Config);
  2709. return propContent ? propContent : "";
  2710. }
  2711. }
  2712. if (!target->IsImported() && dagCheckerParent &&
  2713. !dagCheckerParent->EvaluatingLinkLibraries()) {
  2714. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  2715. context->Config)) {
  2716. context->HadContextSensitiveCondition = true;
  2717. const char* propContent =
  2718. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  2719. context->Config);
  2720. return propContent ? propContent : "";
  2721. }
  2722. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  2723. context->Config)) {
  2724. context->HadContextSensitiveCondition = true;
  2725. const char* propContent =
  2726. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  2727. context->Config);
  2728. return propContent ? propContent : "";
  2729. }
  2730. }
  2731. if (!interfacePropertyName.empty()) {
  2732. result = cmGeneratorExpression::StripEmptyListElements(
  2733. this->EvaluateDependentExpression(result, context->LG, context, target,
  2734. &dagChecker, target));
  2735. std::string linkedTargetsContent = getLinkedTargetsContent(
  2736. target, interfacePropertyName, context, &dagChecker);
  2737. if (!linkedTargetsContent.empty()) {
  2738. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  2739. }
  2740. }
  2741. return result;
  2742. }
  2743. } targetPropertyNode;
  2744. static const struct TargetNameNode : public cmGeneratorExpressionNode
  2745. {
  2746. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  2747. bool GeneratesContent() const override { return true; }
  2748. bool AcceptsArbitraryContentParameter() const override { return true; }
  2749. bool RequiresLiteralInput() const override { return true; }
  2750. std::string Evaluate(
  2751. const std::vector<std::string>& parameters,
  2752. cmGeneratorExpressionContext* /*context*/,
  2753. const GeneratorExpressionContent* /*content*/,
  2754. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2755. {
  2756. return parameters.front();
  2757. }
  2758. int NumExpectedParameters() const override { return 1; }
  2759. } targetNameNode;
  2760. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  2761. {
  2762. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  2763. std::string Evaluate(
  2764. const std::vector<std::string>& parameters,
  2765. cmGeneratorExpressionContext* context,
  2766. const GeneratorExpressionContent* content,
  2767. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2768. {
  2769. std::string const& tgtName = parameters.front();
  2770. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2771. if (!gt) {
  2772. std::ostringstream e;
  2773. e << "Objects of target \"" << tgtName
  2774. << "\" referenced but no such target exists.";
  2775. reportError(context, content->GetOriginalExpression(), e.str());
  2776. return std::string();
  2777. }
  2778. cmStateEnums::TargetType type = gt->GetType();
  2779. if (type != cmStateEnums::EXECUTABLE &&
  2780. type != cmStateEnums::STATIC_LIBRARY &&
  2781. type != cmStateEnums::SHARED_LIBRARY &&
  2782. type != cmStateEnums::MODULE_LIBRARY &&
  2783. type != cmStateEnums::OBJECT_LIBRARY) {
  2784. std::ostringstream e;
  2785. e << "Objects of target \"" << tgtName
  2786. << "\" referenced but is not one of the allowed target types "
  2787. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2788. reportError(context, content->GetOriginalExpression(), e.str());
  2789. return std::string();
  2790. }
  2791. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2792. {
  2793. std::string reason;
  2794. if (!context->EvaluateForBuildsystem &&
  2795. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2796. std::ostringstream e;
  2797. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2798. "is only suitable for consumption by CMake (limited"
  2799. << reason
  2800. << "). "
  2801. "It is not suitable for writing out elsewhere.";
  2802. reportError(context, content->GetOriginalExpression(), e.str());
  2803. return std::string();
  2804. }
  2805. }
  2806. cmList objects;
  2807. if (gt->IsImported()) {
  2808. cmValue loc = nullptr;
  2809. cmValue imp = nullptr;
  2810. std::string suffix;
  2811. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  2812. objects.assign(*loc);
  2813. }
  2814. context->HadContextSensitiveCondition = true;
  2815. } else {
  2816. gt->GetTargetObjectNames(context->Config, objects);
  2817. std::string obj_dir;
  2818. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  2819. // Use object file directory with buildsystem placeholder.
  2820. obj_dir = gt->ObjectDirectory;
  2821. context->HadContextSensitiveCondition =
  2822. gt->HasContextDependentSources();
  2823. } else {
  2824. // Use object file directory with per-config location.
  2825. obj_dir = gt->GetObjectDirectory(context->Config);
  2826. context->HadContextSensitiveCondition = true;
  2827. }
  2828. for (auto& o : objects) {
  2829. o = cmStrCat(obj_dir, o);
  2830. }
  2831. }
  2832. // Create the cmSourceFile instances in the referencing directory.
  2833. cmMakefile* mf = context->LG->GetMakefile();
  2834. for (std::string const& o : objects) {
  2835. mf->AddTargetObject(tgtName, o);
  2836. }
  2837. return objects.to_string();
  2838. }
  2839. } targetObjectsNode;
  2840. struct TargetRuntimeDllsBaseNode : public cmGeneratorExpressionNode
  2841. {
  2842. std::vector<std::string> CollectDlls(
  2843. const std::vector<std::string>& parameters,
  2844. cmGeneratorExpressionContext* context,
  2845. const GeneratorExpressionContent* content) const
  2846. {
  2847. std::string const& tgtName = parameters.front();
  2848. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2849. if (!gt) {
  2850. std::ostringstream e;
  2851. e << "Objects of target \"" << tgtName
  2852. << "\" referenced but no such target exists.";
  2853. reportError(context, content->GetOriginalExpression(), e.str());
  2854. return std::vector<std::string>();
  2855. }
  2856. cmStateEnums::TargetType type = gt->GetType();
  2857. if (type != cmStateEnums::EXECUTABLE &&
  2858. type != cmStateEnums::SHARED_LIBRARY &&
  2859. type != cmStateEnums::MODULE_LIBRARY) {
  2860. std::ostringstream e;
  2861. e << "Objects of target \"" << tgtName
  2862. << "\" referenced but is not one of the allowed target types "
  2863. << "(EXECUTABLE, SHARED, MODULE).";
  2864. reportError(context, content->GetOriginalExpression(), e.str());
  2865. return std::vector<std::string>();
  2866. }
  2867. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  2868. std::vector<std::string> dllPaths;
  2869. auto const& dlls = cli->GetRuntimeDLLs();
  2870. for (auto const& dll : dlls) {
  2871. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  2872. dllPaths.emplace_back(*loc);
  2873. }
  2874. }
  2875. return dllPaths;
  2876. }
  2877. return std::vector<std::string>();
  2878. }
  2879. };
  2880. static const struct TargetRuntimeDllsNode : public TargetRuntimeDllsBaseNode
  2881. {
  2882. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  2883. std::string Evaluate(
  2884. const std::vector<std::string>& parameters,
  2885. cmGeneratorExpressionContext* context,
  2886. const GeneratorExpressionContent* content,
  2887. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2888. {
  2889. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  2890. return cmList::to_string(dlls);
  2891. }
  2892. } targetRuntimeDllsNode;
  2893. static const struct TargetRuntimeDllDirsNode : public TargetRuntimeDllsBaseNode
  2894. {
  2895. TargetRuntimeDllDirsNode() {} // NOLINT(modernize-use-equals-default)
  2896. std::string Evaluate(
  2897. const std::vector<std::string>& parameters,
  2898. cmGeneratorExpressionContext* context,
  2899. const GeneratorExpressionContent* content,
  2900. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2901. {
  2902. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  2903. std::vector<std::string> dllDirs;
  2904. for (const std::string& dll : dlls) {
  2905. std::string directory = cmSystemTools::GetFilenamePath(dll);
  2906. if (std::find(dllDirs.begin(), dllDirs.end(), directory) ==
  2907. dllDirs.end()) {
  2908. dllDirs.push_back(directory);
  2909. }
  2910. }
  2911. return cmList::to_string(dllDirs);
  2912. }
  2913. } targetRuntimeDllDirsNode;
  2914. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  2915. {
  2916. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  2917. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2918. std::string Evaluate(
  2919. const std::vector<std::string>& parameters,
  2920. cmGeneratorExpressionContext* context,
  2921. const GeneratorExpressionContent* content,
  2922. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2923. {
  2924. cmGeneratorTarget const* target = context->HeadTarget;
  2925. if (!target) {
  2926. reportError(
  2927. context, content->GetOriginalExpression(),
  2928. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  2929. "not be used with add_custom_command or add_custom_target.");
  2930. return std::string();
  2931. }
  2932. context->HadHeadSensitiveCondition = true;
  2933. using LangMap = std::map<std::string, cmList>;
  2934. static LangMap availableFeatures;
  2935. LangMap testedFeatures;
  2936. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  2937. for (std::string const& p : parameters) {
  2938. std::string error;
  2939. std::string lang;
  2940. if (!standardResolver.CompileFeatureKnown(
  2941. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  2942. reportError(context, content->GetOriginalExpression(), error);
  2943. return std::string();
  2944. }
  2945. testedFeatures[lang].push_back(p);
  2946. if (availableFeatures.find(lang) == availableFeatures.end()) {
  2947. cmValue featuresKnown =
  2948. standardResolver.CompileFeaturesAvailable(lang, &error);
  2949. if (!featuresKnown) {
  2950. reportError(context, content->GetOriginalExpression(), error);
  2951. return std::string();
  2952. }
  2953. availableFeatures[lang].assign(featuresKnown);
  2954. }
  2955. }
  2956. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  2957. for (auto const& lit : testedFeatures) {
  2958. std::vector<std::string> const& langAvailable =
  2959. availableFeatures[lit.first];
  2960. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  2961. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  2962. for (std::string const& it : lit.second) {
  2963. if (!cm::contains(langAvailable, it)) {
  2964. return "0";
  2965. }
  2966. if (standardDefault && standardDefault->empty()) {
  2967. // This compiler has no notion of language standard levels.
  2968. // All features known for the language are always available.
  2969. continue;
  2970. }
  2971. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  2972. context->Config, it)) {
  2973. if (evalLL) {
  2974. cmValue l =
  2975. target->GetLanguageStandard(lit.first, context->Config);
  2976. if (!l) {
  2977. l = standardDefault;
  2978. }
  2979. assert(l);
  2980. context->MaxLanguageStandard[target][lit.first] = *l;
  2981. } else {
  2982. return "0";
  2983. }
  2984. }
  2985. }
  2986. }
  2987. return "1";
  2988. }
  2989. } compileFeaturesNode;
  2990. static const char* targetPolicyWhitelist[] = {
  2991. nullptr
  2992. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  2993. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  2994. #undef TARGET_POLICY_STRING
  2995. };
  2996. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  2997. const char* policy)
  2998. {
  2999. #define RETURN_POLICY(POLICY) \
  3000. if (strcmp(policy, #POLICY) == 0) { \
  3001. return tgt->GetPolicyStatus##POLICY(); \
  3002. }
  3003. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  3004. #undef RETURN_POLICY
  3005. assert(false && "Unreachable code. Not a valid policy");
  3006. return cmPolicies::WARN;
  3007. }
  3008. static cmPolicies::PolicyID policyForString(const char* policy_id)
  3009. {
  3010. #define RETURN_POLICY_ID(POLICY_ID) \
  3011. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  3012. return cmPolicies::POLICY_ID; \
  3013. }
  3014. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  3015. #undef RETURN_POLICY_ID
  3016. assert(false && "Unreachable code. Not a valid policy");
  3017. return cmPolicies::CMP0002;
  3018. }
  3019. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  3020. {
  3021. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  3022. int NumExpectedParameters() const override { return 1; }
  3023. std::string Evaluate(
  3024. const std::vector<std::string>& parameters,
  3025. cmGeneratorExpressionContext* context,
  3026. const GeneratorExpressionContent* content,
  3027. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3028. {
  3029. if (!context->HeadTarget) {
  3030. reportError(
  3031. context, content->GetOriginalExpression(),
  3032. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  3033. "may not be used with add_custom_command or add_custom_target.");
  3034. return std::string();
  3035. }
  3036. context->HadContextSensitiveCondition = true;
  3037. context->HadHeadSensitiveCondition = true;
  3038. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  3039. const char* policy = targetPolicyWhitelist[i];
  3040. if (parameters.front() == policy) {
  3041. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  3042. switch (statusForTarget(context->HeadTarget, policy)) {
  3043. case cmPolicies::WARN:
  3044. lg->IssueMessage(
  3045. MessageType::AUTHOR_WARNING,
  3046. cmPolicies::GetPolicyWarning(policyForString(policy)));
  3047. CM_FALLTHROUGH;
  3048. case cmPolicies::REQUIRED_IF_USED:
  3049. case cmPolicies::REQUIRED_ALWAYS:
  3050. case cmPolicies::OLD:
  3051. return "0";
  3052. case cmPolicies::NEW:
  3053. return "1";
  3054. }
  3055. }
  3056. }
  3057. reportError(
  3058. context, content->GetOriginalExpression(),
  3059. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  3060. "policies. Currently it may be used with the following policies:\n"
  3061. #define STRINGIFY_HELPER(X) #X
  3062. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  3063. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  3064. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  3065. #undef TARGET_POLICY_LIST_ITEM
  3066. );
  3067. return std::string();
  3068. }
  3069. } targetPolicyNode;
  3070. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  3071. {
  3072. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  3073. bool GeneratesContent() const override { return true; }
  3074. int NumExpectedParameters() const override { return 0; }
  3075. std::string Evaluate(
  3076. const std::vector<std::string>& /*parameters*/,
  3077. cmGeneratorExpressionContext* context,
  3078. const GeneratorExpressionContent* content,
  3079. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3080. {
  3081. reportError(context, content->GetOriginalExpression(),
  3082. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  3083. "should never be evaluated.");
  3084. return std::string();
  3085. }
  3086. } installPrefixNode;
  3087. class ArtifactDirTag;
  3088. class ArtifactLinkerTag;
  3089. class ArtifactLinkerLibraryTag;
  3090. class ArtifactLinkerImportTag;
  3091. class ArtifactNameTag;
  3092. class ArtifactImportTag;
  3093. class ArtifactPathTag;
  3094. class ArtifactPdbTag;
  3095. class ArtifactSonameTag;
  3096. class ArtifactSonameImportTag;
  3097. class ArtifactBundleDirTag;
  3098. class ArtifactBundleDirNameTag;
  3099. class ArtifactBundleContentDirTag;
  3100. template <typename ArtifactT, typename ComponentT>
  3101. struct TargetFilesystemArtifactDependency
  3102. {
  3103. static void AddDependency(cmGeneratorTarget* target,
  3104. cmGeneratorExpressionContext* context)
  3105. {
  3106. context->DependTargets.insert(target);
  3107. context->AllTargets.insert(target);
  3108. }
  3109. };
  3110. struct TargetFilesystemArtifactDependencyCMP0112
  3111. {
  3112. static void AddDependency(cmGeneratorTarget* target,
  3113. cmGeneratorExpressionContext* context)
  3114. {
  3115. context->AllTargets.insert(target);
  3116. cmLocalGenerator* lg = context->LG;
  3117. switch (target->GetPolicyStatusCMP0112()) {
  3118. case cmPolicies::WARN:
  3119. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  3120. "CMAKE_POLICY_WARNING_CMP0112")) {
  3121. std::string err =
  3122. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  3123. "\nDependency being added to target:\n \"",
  3124. target->GetName(), "\"\n");
  3125. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  3126. err, context->Backtrace);
  3127. }
  3128. CM_FALLTHROUGH;
  3129. case cmPolicies::OLD:
  3130. context->DependTargets.insert(target);
  3131. break;
  3132. case cmPolicies::REQUIRED_IF_USED:
  3133. case cmPolicies::REQUIRED_ALWAYS:
  3134. case cmPolicies::NEW:
  3135. break;
  3136. }
  3137. }
  3138. };
  3139. template <typename ArtifactT>
  3140. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  3141. : TargetFilesystemArtifactDependencyCMP0112
  3142. {
  3143. };
  3144. template <typename ArtifactT>
  3145. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  3146. : TargetFilesystemArtifactDependencyCMP0112
  3147. {
  3148. };
  3149. template <>
  3150. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  3151. ArtifactPathTag>
  3152. : TargetFilesystemArtifactDependencyCMP0112
  3153. {
  3154. };
  3155. template <>
  3156. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  3157. ArtifactPathTag>
  3158. : TargetFilesystemArtifactDependencyCMP0112
  3159. {
  3160. };
  3161. template <>
  3162. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  3163. ArtifactPathTag>
  3164. : TargetFilesystemArtifactDependencyCMP0112
  3165. {
  3166. };
  3167. template <typename ArtifactT>
  3168. struct TargetFilesystemArtifactResultCreator
  3169. {
  3170. static std::string Create(cmGeneratorTarget* target,
  3171. cmGeneratorExpressionContext* context,
  3172. const GeneratorExpressionContent* content);
  3173. };
  3174. template <>
  3175. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  3176. {
  3177. static std::string Create(cmGeneratorTarget* target,
  3178. cmGeneratorExpressionContext* context,
  3179. const GeneratorExpressionContent* content)
  3180. {
  3181. // The target soname file (.so.1).
  3182. if (target->IsDLLPlatform()) {
  3183. ::reportError(context, content->GetOriginalExpression(),
  3184. "TARGET_SONAME_FILE is not allowed "
  3185. "for DLL target platforms.");
  3186. return std::string();
  3187. }
  3188. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3189. ::reportError(context, content->GetOriginalExpression(),
  3190. "TARGET_SONAME_FILE is allowed only for "
  3191. "SHARED libraries.");
  3192. return std::string();
  3193. }
  3194. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  3195. target->GetSOName(context->Config));
  3196. return result;
  3197. }
  3198. };
  3199. template <>
  3200. struct TargetFilesystemArtifactResultCreator<ArtifactSonameImportTag>
  3201. {
  3202. static std::string Create(cmGeneratorTarget* target,
  3203. cmGeneratorExpressionContext* context,
  3204. const GeneratorExpressionContent* content)
  3205. {
  3206. // The target soname file (.so.1).
  3207. if (target->IsDLLPlatform()) {
  3208. ::reportError(context, content->GetOriginalExpression(),
  3209. "TARGET_SONAME_IMPORT_FILE is not allowed "
  3210. "for DLL target platforms.");
  3211. return std::string();
  3212. }
  3213. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3214. ::reportError(context, content->GetOriginalExpression(),
  3215. "TARGET_SONAME_IMPORT_FILE is allowed only for "
  3216. "SHARED libraries.");
  3217. return std::string();
  3218. }
  3219. if (target->HasImportLibrary(context->Config)) {
  3220. return cmStrCat(target->GetDirectory(
  3221. context->Config, cmStateEnums::ImportLibraryArtifact),
  3222. '/',
  3223. target->GetSOName(context->Config,
  3224. cmStateEnums::ImportLibraryArtifact));
  3225. }
  3226. return std::string{};
  3227. }
  3228. };
  3229. template <>
  3230. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  3231. {
  3232. static std::string Create(cmGeneratorTarget* target,
  3233. cmGeneratorExpressionContext* context,
  3234. const GeneratorExpressionContent* content)
  3235. {
  3236. if (target->IsImported()) {
  3237. ::reportError(context, content->GetOriginalExpression(),
  3238. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  3239. return std::string();
  3240. }
  3241. std::string language = target->GetLinkerLanguage(context->Config);
  3242. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3243. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3244. ::reportError(context, content->GetOriginalExpression(),
  3245. "TARGET_PDB_FILE is not supported by the target linker.");
  3246. return std::string();
  3247. }
  3248. cmStateEnums::TargetType targetType = target->GetType();
  3249. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3250. targetType != cmStateEnums::MODULE_LIBRARY &&
  3251. targetType != cmStateEnums::EXECUTABLE) {
  3252. ::reportError(context, content->GetOriginalExpression(),
  3253. "TARGET_PDB_FILE is allowed only for "
  3254. "targets with linker created artifacts.");
  3255. return std::string();
  3256. }
  3257. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  3258. '/', target->GetPDBName(context->Config));
  3259. return result;
  3260. }
  3261. };
  3262. template <>
  3263. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  3264. {
  3265. static std::string Create(cmGeneratorTarget* target,
  3266. cmGeneratorExpressionContext* context,
  3267. const GeneratorExpressionContent* content)
  3268. {
  3269. // The file used to link to the target (.so, .lib, .a) or import file
  3270. // (.lib, .tbd).
  3271. if (!target->IsLinkable()) {
  3272. ::reportError(context, content->GetOriginalExpression(),
  3273. "TARGET_LINKER_FILE is allowed only for libraries and "
  3274. "executables with ENABLE_EXPORTS.");
  3275. return std::string();
  3276. }
  3277. cmStateEnums::ArtifactType artifact =
  3278. target->HasImportLibrary(context->Config)
  3279. ? cmStateEnums::ImportLibraryArtifact
  3280. : cmStateEnums::RuntimeBinaryArtifact;
  3281. return target->GetFullPath(context->Config, artifact);
  3282. }
  3283. };
  3284. template <>
  3285. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerLibraryTag>
  3286. {
  3287. static std::string Create(cmGeneratorTarget* target,
  3288. cmGeneratorExpressionContext* context,
  3289. const GeneratorExpressionContent* content)
  3290. {
  3291. // The file used to link to the target (.dylib, .so, .a).
  3292. if (!target->IsLinkable() ||
  3293. target->GetType() == cmStateEnums::EXECUTABLE) {
  3294. ::reportError(context, content->GetOriginalExpression(),
  3295. "TARGET_LINKER_LIBRARY_FILE is allowed only for libraries "
  3296. "with ENABLE_EXPORTS.");
  3297. return std::string();
  3298. }
  3299. if (!target->IsDLLPlatform() ||
  3300. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3301. return target->GetFullPath(context->Config,
  3302. cmStateEnums::RuntimeBinaryArtifact);
  3303. }
  3304. return std::string{};
  3305. }
  3306. };
  3307. template <>
  3308. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerImportTag>
  3309. {
  3310. static std::string Create(cmGeneratorTarget* target,
  3311. cmGeneratorExpressionContext* context,
  3312. const GeneratorExpressionContent* content)
  3313. {
  3314. // The file used to link to the target (.lib, .tbd).
  3315. if (!target->IsLinkable()) {
  3316. ::reportError(
  3317. context, content->GetOriginalExpression(),
  3318. "TARGET_LINKER_IMPORT_FILE is allowed only for libraries and "
  3319. "executables with ENABLE_EXPORTS.");
  3320. return std::string();
  3321. }
  3322. if (target->HasImportLibrary(context->Config)) {
  3323. return target->GetFullPath(context->Config,
  3324. cmStateEnums::ImportLibraryArtifact);
  3325. }
  3326. return std::string{};
  3327. }
  3328. };
  3329. template <>
  3330. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  3331. {
  3332. static std::string Create(cmGeneratorTarget* target,
  3333. cmGeneratorExpressionContext* context,
  3334. const GeneratorExpressionContent* content)
  3335. {
  3336. if (target->IsImported()) {
  3337. ::reportError(context, content->GetOriginalExpression(),
  3338. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  3339. return std::string();
  3340. }
  3341. if (!target->IsBundleOnApple()) {
  3342. ::reportError(context, content->GetOriginalExpression(),
  3343. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  3344. return std::string();
  3345. }
  3346. std::string outpath = target->GetDirectory(context->Config) + '/';
  3347. return target->BuildBundleDirectory(outpath, context->Config,
  3348. cmGeneratorTarget::BundleDirLevel);
  3349. }
  3350. };
  3351. template <>
  3352. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  3353. {
  3354. static std::string Create(cmGeneratorTarget* target,
  3355. cmGeneratorExpressionContext* context,
  3356. const GeneratorExpressionContent* content)
  3357. {
  3358. if (target->IsImported()) {
  3359. ::reportError(
  3360. context, content->GetOriginalExpression(),
  3361. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  3362. return std::string();
  3363. }
  3364. if (!target->IsBundleOnApple()) {
  3365. ::reportError(
  3366. context, content->GetOriginalExpression(),
  3367. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  3368. return std::string();
  3369. }
  3370. auto level = cmGeneratorTarget::BundleDirLevel;
  3371. auto config = context->Config;
  3372. if (target->IsAppBundleOnApple()) {
  3373. return target->GetAppBundleDirectory(config, level);
  3374. }
  3375. if (target->IsFrameworkOnApple()) {
  3376. return target->GetFrameworkDirectory(config, level);
  3377. }
  3378. if (target->IsCFBundleOnApple()) {
  3379. return target->GetCFBundleDirectory(config, level);
  3380. }
  3381. return std::string();
  3382. }
  3383. };
  3384. template <>
  3385. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  3386. {
  3387. static std::string Create(cmGeneratorTarget* target,
  3388. cmGeneratorExpressionContext* context,
  3389. const GeneratorExpressionContent* content)
  3390. {
  3391. if (target->IsImported()) {
  3392. ::reportError(
  3393. context, content->GetOriginalExpression(),
  3394. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  3395. return std::string();
  3396. }
  3397. if (!target->IsBundleOnApple()) {
  3398. ::reportError(
  3399. context, content->GetOriginalExpression(),
  3400. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  3401. return std::string();
  3402. }
  3403. std::string outpath = target->GetDirectory(context->Config) + '/';
  3404. return target->BuildBundleDirectory(outpath, context->Config,
  3405. cmGeneratorTarget::ContentLevel);
  3406. }
  3407. };
  3408. template <>
  3409. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  3410. {
  3411. static std::string Create(cmGeneratorTarget* target,
  3412. cmGeneratorExpressionContext* context,
  3413. const GeneratorExpressionContent* /*unused*/)
  3414. {
  3415. return target->GetFullPath(context->Config,
  3416. cmStateEnums::RuntimeBinaryArtifact, true);
  3417. }
  3418. };
  3419. template <>
  3420. struct TargetFilesystemArtifactResultCreator<ArtifactImportTag>
  3421. {
  3422. static std::string Create(cmGeneratorTarget* target,
  3423. cmGeneratorExpressionContext* context,
  3424. const GeneratorExpressionContent* /*unused*/)
  3425. {
  3426. if (target->HasImportLibrary(context->Config)) {
  3427. return target->GetFullPath(context->Config,
  3428. cmStateEnums::ImportLibraryArtifact, true);
  3429. }
  3430. return std::string{};
  3431. }
  3432. };
  3433. template <typename ArtifactT>
  3434. struct TargetFilesystemArtifactResultGetter
  3435. {
  3436. static std::string Get(const std::string& result);
  3437. };
  3438. template <>
  3439. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  3440. {
  3441. static std::string Get(const std::string& result)
  3442. {
  3443. return cmSystemTools::GetFilenameName(result);
  3444. }
  3445. };
  3446. template <>
  3447. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  3448. {
  3449. static std::string Get(const std::string& result)
  3450. {
  3451. return cmSystemTools::GetFilenamePath(result);
  3452. }
  3453. };
  3454. template <>
  3455. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  3456. {
  3457. static std::string Get(const std::string& result) { return result; }
  3458. };
  3459. struct TargetArtifactBase : public cmGeneratorExpressionNode
  3460. {
  3461. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  3462. protected:
  3463. cmGeneratorTarget* GetTarget(
  3464. const std::vector<std::string>& parameters,
  3465. cmGeneratorExpressionContext* context,
  3466. const GeneratorExpressionContent* content,
  3467. cmGeneratorExpressionDAGChecker* dagChecker) const
  3468. {
  3469. // Lookup the referenced target.
  3470. std::string const& name = parameters.front();
  3471. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  3472. ::reportError(context, content->GetOriginalExpression(),
  3473. "Expression syntax not recognized.");
  3474. return nullptr;
  3475. }
  3476. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  3477. if (!target) {
  3478. ::reportError(context, content->GetOriginalExpression(),
  3479. "No target \"" + name + "\"");
  3480. return nullptr;
  3481. }
  3482. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  3483. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  3484. ::reportError(context, content->GetOriginalExpression(),
  3485. "Target \"" + name +
  3486. "\" is not an executable or library.");
  3487. return nullptr;
  3488. }
  3489. if (dagChecker &&
  3490. (dagChecker->EvaluatingLinkLibraries(target) ||
  3491. (dagChecker->EvaluatingSources() &&
  3492. target == dagChecker->TopTarget()))) {
  3493. ::reportError(context, content->GetOriginalExpression(),
  3494. "Expressions which require the linker language may not "
  3495. "be used while evaluating link libraries");
  3496. return nullptr;
  3497. }
  3498. return target;
  3499. }
  3500. };
  3501. template <typename ArtifactT, typename ComponentT>
  3502. struct TargetFilesystemArtifact : public TargetArtifactBase
  3503. {
  3504. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  3505. int NumExpectedParameters() const override { return 1; }
  3506. std::string Evaluate(
  3507. const std::vector<std::string>& parameters,
  3508. cmGeneratorExpressionContext* context,
  3509. const GeneratorExpressionContent* content,
  3510. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3511. {
  3512. cmGeneratorTarget* target =
  3513. this->GetTarget(parameters, context, content, dagChecker);
  3514. if (!target) {
  3515. return std::string();
  3516. }
  3517. // Not a dependent target if we are querying for ArtifactDirTag,
  3518. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  3519. // and ArtifactBundleContentDirTag
  3520. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  3521. target, context);
  3522. std::string result =
  3523. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  3524. content);
  3525. if (context->HadError) {
  3526. return std::string();
  3527. }
  3528. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  3529. }
  3530. };
  3531. template <typename ArtifactT>
  3532. struct TargetFilesystemArtifactNodeGroup
  3533. {
  3534. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  3535. {
  3536. }
  3537. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  3538. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  3539. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  3540. };
  3541. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  3542. targetNodeGroup;
  3543. static const TargetFilesystemArtifactNodeGroup<ArtifactImportTag>
  3544. targetImportNodeGroup;
  3545. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  3546. targetLinkerNodeGroup;
  3547. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerLibraryTag>
  3548. targetLinkerLibraryNodeGroup;
  3549. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerImportTag>
  3550. targetLinkerImportNodeGroup;
  3551. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  3552. targetSoNameNodeGroup;
  3553. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameImportTag>
  3554. targetSoNameImportNodeGroup;
  3555. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  3556. targetPdbNodeGroup;
  3557. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  3558. targetBundleDirNode;
  3559. static const TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  3560. ArtifactNameTag>
  3561. targetBundleDirNameNode;
  3562. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  3563. ArtifactPathTag>
  3564. targetBundleContentDirNode;
  3565. //
  3566. // To retrieve base name for various artifacts
  3567. //
  3568. template <typename ArtifactT>
  3569. struct TargetOutputNameArtifactResultGetter
  3570. {
  3571. static std::string Get(cmGeneratorTarget* target,
  3572. cmGeneratorExpressionContext* context,
  3573. const GeneratorExpressionContent* content);
  3574. };
  3575. template <>
  3576. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  3577. {
  3578. static std::string Get(cmGeneratorTarget* target,
  3579. cmGeneratorExpressionContext* context,
  3580. const GeneratorExpressionContent* /*unused*/)
  3581. {
  3582. return target->GetOutputName(context->Config,
  3583. cmStateEnums::RuntimeBinaryArtifact) +
  3584. target->GetFilePostfix(context->Config);
  3585. }
  3586. };
  3587. template <>
  3588. struct TargetOutputNameArtifactResultGetter<ArtifactImportTag>
  3589. {
  3590. static std::string Get(cmGeneratorTarget* target,
  3591. cmGeneratorExpressionContext* context,
  3592. const GeneratorExpressionContent* /*unused*/)
  3593. {
  3594. if (target->HasImportLibrary(context->Config)) {
  3595. return target->GetOutputName(context->Config,
  3596. cmStateEnums::ImportLibraryArtifact) +
  3597. target->GetFilePostfix(context->Config);
  3598. }
  3599. return std::string{};
  3600. }
  3601. };
  3602. template <>
  3603. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  3604. {
  3605. static std::string Get(cmGeneratorTarget* target,
  3606. cmGeneratorExpressionContext* context,
  3607. const GeneratorExpressionContent* content)
  3608. {
  3609. // The library file used to link to the target (.so, .lib, .a) or import
  3610. // file (.lin, .tbd).
  3611. if (!target->IsLinkable()) {
  3612. ::reportError(context, content->GetOriginalExpression(),
  3613. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  3614. "libraries and executables with ENABLE_EXPORTS.");
  3615. return std::string();
  3616. }
  3617. cmStateEnums::ArtifactType artifact =
  3618. target->HasImportLibrary(context->Config)
  3619. ? cmStateEnums::ImportLibraryArtifact
  3620. : cmStateEnums::RuntimeBinaryArtifact;
  3621. return target->GetOutputName(context->Config, artifact) +
  3622. target->GetFilePostfix(context->Config);
  3623. }
  3624. };
  3625. template <>
  3626. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerLibraryTag>
  3627. {
  3628. static std::string Get(cmGeneratorTarget* target,
  3629. cmGeneratorExpressionContext* context,
  3630. const GeneratorExpressionContent* content)
  3631. {
  3632. // The library file used to link to the target (.so, .lib, .a).
  3633. if (!target->IsLinkable() ||
  3634. target->GetType() == cmStateEnums::EXECUTABLE) {
  3635. ::reportError(context, content->GetOriginalExpression(),
  3636. "TARGET_LINKER_LIBRARY_FILE_BASE_NAME is allowed only for "
  3637. "libraries with ENABLE_EXPORTS.");
  3638. return std::string();
  3639. }
  3640. if (!target->IsDLLPlatform() ||
  3641. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3642. return target->GetOutputName(context->Config,
  3643. cmStateEnums::ImportLibraryArtifact) +
  3644. target->GetFilePostfix(context->Config);
  3645. }
  3646. return std::string{};
  3647. }
  3648. };
  3649. template <>
  3650. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerImportTag>
  3651. {
  3652. static std::string Get(cmGeneratorTarget* target,
  3653. cmGeneratorExpressionContext* context,
  3654. const GeneratorExpressionContent* content)
  3655. {
  3656. // The import file used to link to the target (.lib, .tbd).
  3657. if (!target->IsLinkable()) {
  3658. ::reportError(context, content->GetOriginalExpression(),
  3659. "TARGET_LINKER_IMPORT_FILE_BASE_NAME is allowed only for "
  3660. "libraries and executables with ENABLE_EXPORTS.");
  3661. return std::string();
  3662. }
  3663. if (target->HasImportLibrary(context->Config)) {
  3664. return target->GetOutputName(context->Config,
  3665. cmStateEnums::ImportLibraryArtifact) +
  3666. target->GetFilePostfix(context->Config);
  3667. }
  3668. return std::string{};
  3669. }
  3670. };
  3671. template <>
  3672. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  3673. {
  3674. static std::string Get(cmGeneratorTarget* target,
  3675. cmGeneratorExpressionContext* context,
  3676. const GeneratorExpressionContent* content)
  3677. {
  3678. if (target->IsImported()) {
  3679. ::reportError(
  3680. context, content->GetOriginalExpression(),
  3681. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  3682. return std::string();
  3683. }
  3684. std::string language = target->GetLinkerLanguage(context->Config);
  3685. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3686. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3687. ::reportError(
  3688. context, content->GetOriginalExpression(),
  3689. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  3690. return std::string();
  3691. }
  3692. cmStateEnums::TargetType targetType = target->GetType();
  3693. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3694. targetType != cmStateEnums::MODULE_LIBRARY &&
  3695. targetType != cmStateEnums::EXECUTABLE) {
  3696. ::reportError(context, content->GetOriginalExpression(),
  3697. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  3698. "targets with linker created artifacts.");
  3699. return std::string();
  3700. }
  3701. return target->GetPDBOutputName(context->Config) +
  3702. target->GetFilePostfix(context->Config);
  3703. }
  3704. };
  3705. template <typename ArtifactT>
  3706. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  3707. {
  3708. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  3709. int NumExpectedParameters() const override { return 1; }
  3710. std::string Evaluate(
  3711. const std::vector<std::string>& parameters,
  3712. cmGeneratorExpressionContext* context,
  3713. const GeneratorExpressionContent* content,
  3714. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3715. {
  3716. cmGeneratorTarget* target =
  3717. this->GetTarget(parameters, context, content, dagChecker);
  3718. if (!target) {
  3719. return std::string();
  3720. }
  3721. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  3722. target, context, content);
  3723. if (context->HadError) {
  3724. return std::string();
  3725. }
  3726. return result;
  3727. }
  3728. };
  3729. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  3730. targetFileBaseNameNode;
  3731. static const TargetFileBaseNameArtifact<ArtifactImportTag>
  3732. targetImportFileBaseNameNode;
  3733. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  3734. targetLinkerFileBaseNameNode;
  3735. static const TargetFileBaseNameArtifact<ArtifactLinkerLibraryTag>
  3736. targetLinkerLibraryFileBaseNameNode;
  3737. static const TargetFileBaseNameArtifact<ArtifactLinkerImportTag>
  3738. targetLinkerImportFileBaseNameNode;
  3739. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  3740. targetPdbFileBaseNameNode;
  3741. class ArtifactFilePrefixTag;
  3742. class ArtifactImportFilePrefixTag;
  3743. class ArtifactLinkerFilePrefixTag;
  3744. class ArtifactLinkerLibraryFilePrefixTag;
  3745. class ArtifactLinkerImportFilePrefixTag;
  3746. class ArtifactFileSuffixTag;
  3747. class ArtifactImportFileSuffixTag;
  3748. class ArtifactLinkerFileSuffixTag;
  3749. class ArtifactLinkerLibraryFileSuffixTag;
  3750. class ArtifactLinkerImportFileSuffixTag;
  3751. template <typename ArtifactT>
  3752. struct TargetFileArtifactResultGetter
  3753. {
  3754. static std::string Get(cmGeneratorTarget* target,
  3755. cmGeneratorExpressionContext* context,
  3756. const GeneratorExpressionContent* content);
  3757. };
  3758. template <>
  3759. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  3760. {
  3761. static std::string Get(cmGeneratorTarget* target,
  3762. cmGeneratorExpressionContext* context,
  3763. const GeneratorExpressionContent*)
  3764. {
  3765. return target->GetFilePrefix(context->Config);
  3766. }
  3767. };
  3768. template <>
  3769. struct TargetFileArtifactResultGetter<ArtifactImportFilePrefixTag>
  3770. {
  3771. static std::string Get(cmGeneratorTarget* target,
  3772. cmGeneratorExpressionContext* context,
  3773. const GeneratorExpressionContent*)
  3774. {
  3775. if (target->HasImportLibrary(context->Config)) {
  3776. return target->GetFilePrefix(context->Config,
  3777. cmStateEnums::ImportLibraryArtifact);
  3778. }
  3779. return std::string{};
  3780. }
  3781. };
  3782. template <>
  3783. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  3784. {
  3785. static std::string Get(cmGeneratorTarget* target,
  3786. cmGeneratorExpressionContext* context,
  3787. const GeneratorExpressionContent* content)
  3788. {
  3789. if (!target->IsLinkable()) {
  3790. ::reportError(
  3791. context, content->GetOriginalExpression(),
  3792. "TARGET_LINKER_FILE_PREFIX is allowed only for libraries and "
  3793. "executables with ENABLE_EXPORTS.");
  3794. return std::string();
  3795. }
  3796. cmStateEnums::ArtifactType artifact =
  3797. target->HasImportLibrary(context->Config)
  3798. ? cmStateEnums::ImportLibraryArtifact
  3799. : cmStateEnums::RuntimeBinaryArtifact;
  3800. return target->GetFilePrefix(context->Config, artifact);
  3801. }
  3802. };
  3803. template <>
  3804. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFilePrefixTag>
  3805. {
  3806. static std::string Get(cmGeneratorTarget* target,
  3807. cmGeneratorExpressionContext* context,
  3808. const GeneratorExpressionContent* content)
  3809. {
  3810. if (!target->IsLinkable() ||
  3811. target->GetType() == cmStateEnums::EXECUTABLE) {
  3812. ::reportError(
  3813. context, content->GetOriginalExpression(),
  3814. "TARGET_LINKER_LIBRARY_FILE_PREFIX is allowed only for libraries "
  3815. "with ENABLE_EXPORTS.");
  3816. return std::string();
  3817. }
  3818. if (!target->IsDLLPlatform() ||
  3819. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3820. return target->GetFilePrefix(context->Config,
  3821. cmStateEnums::RuntimeBinaryArtifact);
  3822. }
  3823. return std::string{};
  3824. }
  3825. };
  3826. template <>
  3827. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFilePrefixTag>
  3828. {
  3829. static std::string Get(cmGeneratorTarget* target,
  3830. cmGeneratorExpressionContext* context,
  3831. const GeneratorExpressionContent* content)
  3832. {
  3833. if (!target->IsLinkable()) {
  3834. ::reportError(
  3835. context, content->GetOriginalExpression(),
  3836. "TARGET_LINKER_IMPORT_FILE_PREFIX is allowed only for libraries and "
  3837. "executables with ENABLE_EXPORTS.");
  3838. return std::string();
  3839. }
  3840. if (target->HasImportLibrary(context->Config)) {
  3841. return target->GetFilePrefix(context->Config,
  3842. cmStateEnums::ImportLibraryArtifact);
  3843. }
  3844. return std::string{};
  3845. }
  3846. };
  3847. template <>
  3848. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  3849. {
  3850. static std::string Get(cmGeneratorTarget* target,
  3851. cmGeneratorExpressionContext* context,
  3852. const GeneratorExpressionContent*)
  3853. {
  3854. return target->GetFileSuffix(context->Config);
  3855. }
  3856. };
  3857. template <>
  3858. struct TargetFileArtifactResultGetter<ArtifactImportFileSuffixTag>
  3859. {
  3860. static std::string Get(cmGeneratorTarget* target,
  3861. cmGeneratorExpressionContext* context,
  3862. const GeneratorExpressionContent*)
  3863. {
  3864. if (target->HasImportLibrary(context->Config)) {
  3865. return target->GetFileSuffix(context->Config,
  3866. cmStateEnums::ImportLibraryArtifact);
  3867. }
  3868. return std::string{};
  3869. }
  3870. };
  3871. template <>
  3872. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  3873. {
  3874. static std::string Get(cmGeneratorTarget* target,
  3875. cmGeneratorExpressionContext* context,
  3876. const GeneratorExpressionContent* content)
  3877. {
  3878. if (!target->IsLinkable()) {
  3879. ::reportError(
  3880. context, content->GetOriginalExpression(),
  3881. "TARGET_LINKER_FILE_SUFFIX is allowed only for libraries and "
  3882. "executables with ENABLE_EXPORTS.");
  3883. return std::string();
  3884. }
  3885. cmStateEnums::ArtifactType artifact =
  3886. target->HasImportLibrary(context->Config)
  3887. ? cmStateEnums::ImportLibraryArtifact
  3888. : cmStateEnums::RuntimeBinaryArtifact;
  3889. return target->GetFileSuffix(context->Config, artifact);
  3890. }
  3891. };
  3892. template <>
  3893. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFileSuffixTag>
  3894. {
  3895. static std::string Get(cmGeneratorTarget* target,
  3896. cmGeneratorExpressionContext* context,
  3897. const GeneratorExpressionContent* content)
  3898. {
  3899. if (!target->IsLinkable() ||
  3900. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3901. ::reportError(context, content->GetOriginalExpression(),
  3902. "TARGET_LINKER_LIBRARY_FILE_SUFFIX is allowed only for "
  3903. "libraries with ENABLE_EXPORTS.");
  3904. return std::string();
  3905. }
  3906. if (!target->IsDLLPlatform() ||
  3907. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3908. return target->GetFileSuffix(context->Config,
  3909. cmStateEnums::RuntimeBinaryArtifact);
  3910. }
  3911. return std::string{};
  3912. }
  3913. };
  3914. template <>
  3915. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFileSuffixTag>
  3916. {
  3917. static std::string Get(cmGeneratorTarget* target,
  3918. cmGeneratorExpressionContext* context,
  3919. const GeneratorExpressionContent* content)
  3920. {
  3921. if (!target->IsLinkable()) {
  3922. ::reportError(
  3923. context, content->GetOriginalExpression(),
  3924. "TARGET_LINKER_IMPORT_FILE_SUFFIX is allowed only for libraries and "
  3925. "executables with ENABLE_EXPORTS.");
  3926. return std::string();
  3927. }
  3928. if (target->HasImportLibrary(context->Config)) {
  3929. return target->GetFileSuffix(context->Config,
  3930. cmStateEnums::ImportLibraryArtifact);
  3931. }
  3932. return std::string{};
  3933. }
  3934. };
  3935. template <typename ArtifactT>
  3936. struct TargetFileArtifact : public TargetArtifactBase
  3937. {
  3938. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  3939. int NumExpectedParameters() const override { return 1; }
  3940. std::string Evaluate(
  3941. const std::vector<std::string>& parameters,
  3942. cmGeneratorExpressionContext* context,
  3943. const GeneratorExpressionContent* content,
  3944. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3945. {
  3946. cmGeneratorTarget* target =
  3947. this->GetTarget(parameters, context, content, dagChecker);
  3948. if (!target) {
  3949. return std::string();
  3950. }
  3951. std::string result =
  3952. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  3953. if (context->HadError) {
  3954. return std::string();
  3955. }
  3956. return result;
  3957. }
  3958. };
  3959. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  3960. static const TargetFileArtifact<ArtifactImportFilePrefixTag>
  3961. targetImportFilePrefixNode;
  3962. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  3963. targetLinkerFilePrefixNode;
  3964. static const TargetFileArtifact<ArtifactLinkerLibraryFilePrefixTag>
  3965. targetLinkerLibraryFilePrefixNode;
  3966. static const TargetFileArtifact<ArtifactLinkerImportFilePrefixTag>
  3967. targetLinkerImportFilePrefixNode;
  3968. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  3969. static const TargetFileArtifact<ArtifactImportFileSuffixTag>
  3970. targetImportFileSuffixNode;
  3971. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  3972. targetLinkerFileSuffixNode;
  3973. static const TargetFileArtifact<ArtifactLinkerLibraryFileSuffixTag>
  3974. targetLinkerLibraryFileSuffixNode;
  3975. static const TargetFileArtifact<ArtifactLinkerImportFileSuffixTag>
  3976. targetLinkerImportFileSuffixNode;
  3977. static const struct ShellPathNode : public cmGeneratorExpressionNode
  3978. {
  3979. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  3980. std::string Evaluate(
  3981. const std::vector<std::string>& parameters,
  3982. cmGeneratorExpressionContext* context,
  3983. const GeneratorExpressionContent* content,
  3984. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3985. {
  3986. cmList listIn{ parameters.front() };
  3987. if (listIn.empty()) {
  3988. reportError(context, content->GetOriginalExpression(),
  3989. "\"\" is not an absolute path.");
  3990. return std::string();
  3991. }
  3992. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  3993. cmOutputConverter converter(snapshot);
  3994. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  3995. std::vector<std::string> listOut;
  3996. listOut.reserve(listIn.size());
  3997. for (auto const& in : listIn) {
  3998. if (!cmSystemTools::FileIsFullPath(in)) {
  3999. reportError(context, content->GetOriginalExpression(),
  4000. "\"" + in + "\" is not an absolute path.");
  4001. return std::string();
  4002. }
  4003. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  4004. }
  4005. return cmJoin(listOut, separator);
  4006. }
  4007. } shellPathNode;
  4008. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  4009. const std::string& identifier)
  4010. {
  4011. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  4012. { "0", &zeroNode },
  4013. { "1", &oneNode },
  4014. { "AND", &andNode },
  4015. { "OR", &orNode },
  4016. { "NOT", &notNode },
  4017. { "C_COMPILER_ID", &cCompilerIdNode },
  4018. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  4019. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  4020. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  4021. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  4022. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  4023. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  4024. { "VERSION_GREATER", &versionGreaterNode },
  4025. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  4026. { "VERSION_LESS", &versionLessNode },
  4027. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  4028. { "VERSION_EQUAL", &versionEqualNode },
  4029. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  4030. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  4031. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  4032. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  4033. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  4034. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  4035. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  4036. { "PLATFORM_ID", &platformIdNode },
  4037. { "COMPILE_FEATURES", &compileFeaturesNode },
  4038. { "CONFIGURATION", &configurationNode },
  4039. { "CONFIG", &configurationTestNode },
  4040. { "TARGET_FILE", &targetNodeGroup.File },
  4041. { "TARGET_IMPORT_FILE", &targetImportNodeGroup.File },
  4042. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  4043. { "TARGET_LINKER_LIBRARY_FILE", &targetLinkerLibraryNodeGroup.File },
  4044. { "TARGET_LINKER_IMPORT_FILE", &targetLinkerImportNodeGroup.File },
  4045. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  4046. { "TARGET_SONAME_IMPORT_FILE", &targetSoNameImportNodeGroup.File },
  4047. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  4048. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  4049. { "TARGET_IMPORT_FILE_BASE_NAME", &targetImportFileBaseNameNode },
  4050. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  4051. { "TARGET_LINKER_LIBRARY_FILE_BASE_NAME",
  4052. &targetLinkerLibraryFileBaseNameNode },
  4053. { "TARGET_LINKER_IMPORT_FILE_BASE_NAME",
  4054. &targetLinkerImportFileBaseNameNode },
  4055. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  4056. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  4057. { "TARGET_IMPORT_FILE_PREFIX", &targetImportFilePrefixNode },
  4058. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  4059. { "TARGET_LINKER_LIBRARY_FILE_PREFIX",
  4060. &targetLinkerLibraryFilePrefixNode },
  4061. { "TARGET_LINKER_IMPORT_FILE_PREFIX", &targetLinkerImportFilePrefixNode },
  4062. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  4063. { "TARGET_IMPORT_FILE_SUFFIX", &targetImportFileSuffixNode },
  4064. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  4065. { "TARGET_LINKER_LIBRARY_FILE_SUFFIX",
  4066. &targetLinkerLibraryFileSuffixNode },
  4067. { "TARGET_LINKER_IMPORT_FILE_SUFFIX", &targetLinkerImportFileSuffixNode },
  4068. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  4069. { "TARGET_IMPORT_FILE_NAME", &targetImportNodeGroup.FileName },
  4070. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  4071. { "TARGET_LINKER_LIBRARY_FILE_NAME",
  4072. &targetLinkerLibraryNodeGroup.FileName },
  4073. { "TARGET_LINKER_IMPORT_FILE_NAME",
  4074. &targetLinkerImportNodeGroup.FileName },
  4075. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  4076. { "TARGET_SONAME_IMPORT_FILE_NAME",
  4077. &targetSoNameImportNodeGroup.FileName },
  4078. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  4079. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  4080. { "TARGET_IMPORT_FILE_DIR", &targetImportNodeGroup.FileDir },
  4081. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  4082. { "TARGET_LINKER_LIBRARY_FILE_DIR",
  4083. &targetLinkerLibraryNodeGroup.FileDir },
  4084. { "TARGET_LINKER_IMPORT_FILE_DIR", &targetLinkerImportNodeGroup.FileDir },
  4085. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  4086. { "TARGET_SONAME_IMPORT_FILE_DIR", &targetSoNameImportNodeGroup.FileDir },
  4087. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  4088. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  4089. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  4090. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  4091. { "STREQUAL", &strEqualNode },
  4092. { "EQUAL", &equalNode },
  4093. { "IN_LIST", &inListNode },
  4094. { "FILTER", &filterNode },
  4095. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  4096. { "LIST", &listNode },
  4097. { "LOWER_CASE", &lowerCaseNode },
  4098. { "UPPER_CASE", &upperCaseNode },
  4099. { "PATH", &pathNode },
  4100. { "PATH_EQUAL", &pathEqualNode },
  4101. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  4102. { "BOOL", &boolNode },
  4103. { "IF", &ifNode },
  4104. { "ANGLE-R", &angle_rNode },
  4105. { "COMMA", &commaNode },
  4106. { "SEMICOLON", &semicolonNode },
  4107. { "QUOTE", &quoteNode },
  4108. { "TARGET_PROPERTY", &targetPropertyNode },
  4109. { "TARGET_NAME", &targetNameNode },
  4110. { "TARGET_OBJECTS", &targetObjectsNode },
  4111. { "TARGET_POLICY", &targetPolicyNode },
  4112. { "TARGET_EXISTS", &targetExistsNode },
  4113. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  4114. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  4115. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  4116. { "TARGET_RUNTIME_DLL_DIRS", &targetRuntimeDllDirsNode },
  4117. { "GENEX_EVAL", &genexEvalNode },
  4118. { "BUILD_INTERFACE", &buildInterfaceNode },
  4119. { "INSTALL_INTERFACE", &installInterfaceNode },
  4120. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  4121. { "INSTALL_PREFIX", &installPrefixNode },
  4122. { "JOIN", &joinNode },
  4123. { "COMPILE_ONLY", &compileOnlyNode },
  4124. { "LINK_ONLY", &linkOnlyNode },
  4125. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  4126. { "COMPILE_LANGUAGE", &languageNode },
  4127. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  4128. { "LINK_LANGUAGE", &linkLanguageNode },
  4129. { "LINK_LIBRARY", &linkLibraryNode },
  4130. { "LINK_GROUP", &linkGroupNode },
  4131. { "HOST_LINK", &hostLinkNode },
  4132. { "DEVICE_LINK", &deviceLinkNode },
  4133. { "SHELL_PATH", &shellPathNode }
  4134. };
  4135. {
  4136. auto itr = nodeMap.find(identifier);
  4137. if (itr != nodeMap.end()) {
  4138. return itr->second;
  4139. }
  4140. }
  4141. return nullptr;
  4142. }
  4143. void reportError(cmGeneratorExpressionContext* context,
  4144. const std::string& expr, const std::string& result)
  4145. {
  4146. context->HadError = true;
  4147. if (context->Quiet) {
  4148. return;
  4149. }
  4150. std::ostringstream e;
  4151. /* clang-format off */
  4152. e << "Error evaluating generator expression:\n"
  4153. << " " << expr << "\n"
  4154. << result;
  4155. /* clang-format on */
  4156. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  4157. e.str(), context->Backtrace);
  4158. }