cmGeneratorExpressionNode.cxx 178 KB

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