1
0

cmGeneratorExpressionNode.cxx 164 KB

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