cmGeneratorExpressionNode.cxx 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835
  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 const& 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 const& 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 const& 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. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  2034. context->HadContextSensitiveCondition = true;
  2035. bool firstParam = true;
  2036. for (auto const& param : parameters) {
  2037. if (!configValidator.find(param)) {
  2038. if (firstParam) {
  2039. reportError(context, content->GetOriginalExpression(),
  2040. "Expression syntax not recognized.");
  2041. return std::string();
  2042. }
  2043. // for backwards compat invalid config names are only errors as
  2044. // the first parameter
  2045. std::ostringstream e;
  2046. /* clang-format off */
  2047. e << "Warning evaluating generator expression:\n"
  2048. << " " << content->GetOriginalExpression() << "\n"
  2049. << "The config name of \"" << param << "\" is invalid";
  2050. /* clang-format on */
  2051. context->LG->GetCMakeInstance()->IssueMessage(
  2052. MessageType::WARNING, e.str(), context->Backtrace);
  2053. }
  2054. firstParam = false;
  2055. if (context->Config.empty()) {
  2056. if (param.empty()) {
  2057. return "1";
  2058. }
  2059. } else if (cmsysString_strcasecmp(param.c_str(),
  2060. context->Config.c_str()) == 0) {
  2061. return "1";
  2062. }
  2063. }
  2064. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  2065. cmValue loc = nullptr;
  2066. cmValue imp = nullptr;
  2067. std::string suffix;
  2068. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  2069. imp, suffix)) {
  2070. // This imported target has an appropriate location
  2071. // for this (possibly mapped) config.
  2072. // Check if there is a proper config mapping for the tested config.
  2073. cmList mappedConfigs;
  2074. std::string mapProp = cmStrCat(
  2075. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  2076. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  2077. mappedConfigs.assign(cmSystemTools::UpperCase(*mapValue));
  2078. for (auto const& param : parameters) {
  2079. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  2080. return "1";
  2081. }
  2082. }
  2083. } else if (!suffix.empty()) {
  2084. // There is no explicit mapping for the tested config, so use
  2085. // the configuration of the imported location that was selected.
  2086. for (auto const& param : parameters) {
  2087. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  2088. return "1";
  2089. }
  2090. }
  2091. }
  2092. }
  2093. }
  2094. return "0";
  2095. }
  2096. } configurationTestNode;
  2097. static const struct JoinNode : public cmGeneratorExpressionNode
  2098. {
  2099. JoinNode() {} // NOLINT(modernize-use-equals-default)
  2100. int NumExpectedParameters() const override { return 2; }
  2101. bool AcceptsArbitraryContentParameter() const override { return true; }
  2102. std::string Evaluate(
  2103. std::vector<std::string> const& parameters,
  2104. cmGeneratorExpressionContext* /*context*/,
  2105. GeneratorExpressionContent const* /*content*/,
  2106. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2107. {
  2108. return cmList{ parameters.front() }.join(parameters[1]);
  2109. }
  2110. } joinNode;
  2111. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  2112. {
  2113. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2114. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2115. std::string Evaluate(
  2116. std::vector<std::string> const& parameters,
  2117. cmGeneratorExpressionContext* context,
  2118. GeneratorExpressionContent const* content,
  2119. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2120. {
  2121. if (context->Language.empty() &&
  2122. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  2123. reportError(
  2124. context, content->GetOriginalExpression(),
  2125. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  2126. "directories, compile definitions, compile options, and to evaluate "
  2127. "components of the file(GENERATE) command.");
  2128. return std::string();
  2129. }
  2130. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2131. std::string genName = gg->GetName();
  2132. if (genName.find("Makefiles") == std::string::npos &&
  2133. genName.find("Ninja") == std::string::npos &&
  2134. genName.find("Visual Studio") == std::string::npos &&
  2135. genName.find("Xcode") == std::string::npos &&
  2136. genName.find("Watcom WMake") == std::string::npos &&
  2137. genName.find("Green Hills MULTI") == std::string::npos) {
  2138. reportError(context, content->GetOriginalExpression(),
  2139. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  2140. return std::string();
  2141. }
  2142. if (parameters.empty()) {
  2143. return context->Language;
  2144. }
  2145. for (auto const& param : parameters) {
  2146. if (context->Language == param) {
  2147. return "1";
  2148. }
  2149. }
  2150. return "0";
  2151. }
  2152. } languageNode;
  2153. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  2154. {
  2155. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2156. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2157. std::string Evaluate(
  2158. std::vector<std::string> const& parameters,
  2159. cmGeneratorExpressionContext* context,
  2160. GeneratorExpressionContent const* content,
  2161. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2162. {
  2163. if (!context->HeadTarget ||
  2164. (context->Language.empty() &&
  2165. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  2166. // reportError(context, content->GetOriginalExpression(), "");
  2167. reportError(
  2168. context, content->GetOriginalExpression(),
  2169. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary "
  2170. "targets "
  2171. "to specify include directories, compile definitions, and compile "
  2172. "options. It may not be used with the add_custom_command, "
  2173. "add_custom_target, or file(GENERATE) commands.");
  2174. return std::string();
  2175. }
  2176. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2177. std::string genName = gg->GetName();
  2178. if (genName.find("Makefiles") == std::string::npos &&
  2179. genName.find("Ninja") == std::string::npos &&
  2180. genName.find("Visual Studio") == std::string::npos &&
  2181. genName.find("Xcode") == std::string::npos &&
  2182. genName.find("Watcom WMake") == std::string::npos &&
  2183. genName.find("Green Hills MULTI") == std::string::npos) {
  2184. reportError(
  2185. context, content->GetOriginalExpression(),
  2186. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  2187. return std::string();
  2188. }
  2189. std::string const& lang = context->Language;
  2190. if (lang == parameters.front()) {
  2191. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2192. parameters.cend());
  2193. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  2194. idParameter, context, content, dagChecker, lang);
  2195. }
  2196. return "0";
  2197. }
  2198. } languageAndIdNode;
  2199. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  2200. {
  2201. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  2202. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2203. std::string Evaluate(
  2204. std::vector<std::string> const& parameters,
  2205. cmGeneratorExpressionContext* context,
  2206. GeneratorExpressionContent const* content,
  2207. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2208. {
  2209. if (!context->HeadTarget || !dagChecker ||
  2210. !(dagChecker->EvaluatingLinkExpression() ||
  2211. dagChecker->EvaluatingLinkLibraries() ||
  2212. dagChecker->EvaluatingLinkerLauncher())) {
  2213. reportError(context, content->GetOriginalExpression(),
  2214. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  2215. "to specify link libraries, link directories, link options "
  2216. "and link depends.");
  2217. return std::string();
  2218. }
  2219. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  2220. reportError(
  2221. context, content->GetOriginalExpression(),
  2222. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  2223. return std::string();
  2224. }
  2225. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2226. std::string genName = gg->GetName();
  2227. if (genName.find("Makefiles") == std::string::npos &&
  2228. genName.find("Ninja") == std::string::npos &&
  2229. genName.find("Visual Studio") == std::string::npos &&
  2230. genName.find("Xcode") == std::string::npos &&
  2231. genName.find("Watcom WMake") == std::string::npos &&
  2232. genName.find("Green Hills MULTI") == std::string::npos) {
  2233. reportError(context, content->GetOriginalExpression(),
  2234. "$<LINK_LANGUAGE:...> not supported for this generator.");
  2235. return std::string();
  2236. }
  2237. if (dagChecker->EvaluatingLinkLibraries()) {
  2238. context->HadHeadSensitiveCondition = true;
  2239. context->HadLinkLanguageSensitiveCondition = true;
  2240. }
  2241. if (parameters.empty()) {
  2242. return context->Language;
  2243. }
  2244. for (auto const& param : parameters) {
  2245. if (context->Language == param) {
  2246. return "1";
  2247. }
  2248. }
  2249. return "0";
  2250. }
  2251. } linkLanguageNode;
  2252. namespace {
  2253. struct LinkerId
  2254. {
  2255. static std::string Evaluate(std::vector<std::string> const& parameters,
  2256. cmGeneratorExpressionContext* context,
  2257. GeneratorExpressionContent const* content,
  2258. std::string const& lang)
  2259. {
  2260. std::string const& linkerId =
  2261. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  2262. "_COMPILER_ID");
  2263. if (parameters.empty()) {
  2264. return linkerId;
  2265. }
  2266. if (linkerId.empty()) {
  2267. return parameters.front().empty() ? "1" : "0";
  2268. }
  2269. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  2270. for (auto const& param : parameters) {
  2271. if (!linkerIdValidator.find(param)) {
  2272. reportError(context, content->GetOriginalExpression(),
  2273. "Expression syntax not recognized.");
  2274. return std::string();
  2275. }
  2276. if (param == linkerId) {
  2277. return "1";
  2278. }
  2279. }
  2280. return "0";
  2281. }
  2282. };
  2283. }
  2284. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  2285. {
  2286. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  2287. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  2288. std::string Evaluate(
  2289. std::vector<std::string> const& parameters,
  2290. cmGeneratorExpressionContext* context,
  2291. GeneratorExpressionContent const* content,
  2292. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2293. {
  2294. if (!context->HeadTarget || !dagChecker ||
  2295. !(dagChecker->EvaluatingLinkExpression() ||
  2296. dagChecker->EvaluatingLinkLibraries() ||
  2297. dagChecker->EvaluatingLinkerLauncher())) {
  2298. reportError(
  2299. context, content->GetOriginalExpression(),
  2300. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  2301. "to specify link libraries, link directories, link options, and "
  2302. "link "
  2303. "depends.");
  2304. return std::string();
  2305. }
  2306. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2307. std::string genName = gg->GetName();
  2308. if (genName.find("Makefiles") == std::string::npos &&
  2309. genName.find("Ninja") == std::string::npos &&
  2310. genName.find("Visual Studio") == std::string::npos &&
  2311. genName.find("Xcode") == std::string::npos &&
  2312. genName.find("Watcom WMake") == std::string::npos &&
  2313. genName.find("Green Hills MULTI") == std::string::npos) {
  2314. reportError(
  2315. context, content->GetOriginalExpression(),
  2316. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  2317. return std::string();
  2318. }
  2319. if (dagChecker->EvaluatingLinkLibraries()) {
  2320. context->HadHeadSensitiveCondition = true;
  2321. context->HadLinkLanguageSensitiveCondition = true;
  2322. }
  2323. std::string const& lang = context->Language;
  2324. if (lang == parameters.front()) {
  2325. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  2326. parameters.cend());
  2327. return LinkerId::Evaluate(idParameter, context, content, lang);
  2328. }
  2329. return "0";
  2330. }
  2331. } linkLanguageAndIdNode;
  2332. struct CompilerLinkerIdNode : public cmGeneratorExpressionNode
  2333. {
  2334. CompilerLinkerIdNode(char const* lang)
  2335. : Language(lang)
  2336. {
  2337. }
  2338. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2339. std::string Evaluate(
  2340. std::vector<std::string> const& parameters,
  2341. cmGeneratorExpressionContext* context,
  2342. GeneratorExpressionContent const* content,
  2343. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2344. {
  2345. if (!context->HeadTarget) {
  2346. reportError(
  2347. context, content->GetOriginalExpression(),
  2348. cmStrCat(
  2349. "$<", this->Language,
  2350. "_COMPILER_LINKER_ID> may only be used with binary targets. It may "
  2351. "not be used with add_custom_command or add_custom_target."));
  2352. return {};
  2353. }
  2354. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2355. this->Language);
  2356. }
  2357. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2358. cmGeneratorExpressionContext* context,
  2359. GeneratorExpressionContent const* content,
  2360. cmGeneratorExpressionDAGChecker* /*unused*/,
  2361. std::string const& lang) const
  2362. {
  2363. std::string const& compilerLinkerId =
  2364. context->LG->GetMakefile()->GetSafeDefinition(
  2365. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_ID"));
  2366. if (parameters.empty()) {
  2367. return compilerLinkerId;
  2368. }
  2369. if (compilerLinkerId.empty()) {
  2370. return parameters.front().empty() ? "1" : "0";
  2371. }
  2372. static cmsys::RegularExpression compilerLinkerIdValidator(
  2373. "^[A-Za-z0-9_]*$");
  2374. for (auto const& param : parameters) {
  2375. if (!compilerLinkerIdValidator.find(param)) {
  2376. reportError(context, content->GetOriginalExpression(),
  2377. "Expression syntax not recognized.");
  2378. return std::string();
  2379. }
  2380. if (param == compilerLinkerId) {
  2381. return "1";
  2382. }
  2383. }
  2384. return "0";
  2385. }
  2386. char const* const Language;
  2387. };
  2388. static CompilerLinkerIdNode const cCompilerLinkerIdNode("C"),
  2389. cxxCompilerLinkerIdNode("CXX"), cudaCompilerLinkerIdNode("CUDA"),
  2390. objcCompilerLinkerIdNode("OBJC"), objcxxCompilerLinkerIdNode("OBJCXX"),
  2391. fortranCompilerLinkerIdNode("Fortran"), hipCompilerLinkerIdNode("HIP");
  2392. struct CompilerLinkerFrontendVariantNode : public cmGeneratorExpressionNode
  2393. {
  2394. CompilerLinkerFrontendVariantNode(char const* lang)
  2395. : Language(lang)
  2396. {
  2397. }
  2398. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2399. std::string Evaluate(
  2400. std::vector<std::string> const& parameters,
  2401. cmGeneratorExpressionContext* context,
  2402. GeneratorExpressionContent const* content,
  2403. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2404. {
  2405. if (!context->HeadTarget) {
  2406. reportError(
  2407. context, content->GetOriginalExpression(),
  2408. cmStrCat(
  2409. "$<", this->Language,
  2410. "_COMPILER_LINKER_FRONTEND_VARIANT> may only be used with binary "
  2411. "targets. It may not be used with add_custom_command or "
  2412. "add_custom_target."));
  2413. return {};
  2414. }
  2415. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  2416. this->Language);
  2417. }
  2418. std::string EvaluateWithLanguage(std::vector<std::string> const& parameters,
  2419. cmGeneratorExpressionContext* context,
  2420. GeneratorExpressionContent const* content,
  2421. cmGeneratorExpressionDAGChecker* /*unused*/,
  2422. std::string const& lang) const
  2423. {
  2424. std::string const& compilerLinkerFrontendVariant =
  2425. context->LG->GetMakefile()->GetSafeDefinition(
  2426. cmStrCat("CMAKE_", lang, "_COMPILER_LINKER_FRONTEND_VARIANT"));
  2427. if (parameters.empty()) {
  2428. return compilerLinkerFrontendVariant;
  2429. }
  2430. if (compilerLinkerFrontendVariant.empty()) {
  2431. return parameters.front().empty() ? "1" : "0";
  2432. }
  2433. static cmsys::RegularExpression compilerLinkerFrontendVariantValidator(
  2434. "^[A-Za-z0-9_]*$");
  2435. for (auto const& param : parameters) {
  2436. if (!compilerLinkerFrontendVariantValidator.find(param)) {
  2437. reportError(context, content->GetOriginalExpression(),
  2438. "Expression syntax not recognized.");
  2439. return {};
  2440. }
  2441. if (param == compilerLinkerFrontendVariant) {
  2442. return "1";
  2443. }
  2444. }
  2445. return "0";
  2446. }
  2447. char const* const Language;
  2448. };
  2449. static CompilerLinkerFrontendVariantNode const
  2450. cCompilerLinkerFrontendVariantNode("C"),
  2451. cxxCompilerLinkerFrontendVariantNode("CXX"),
  2452. cudaCompilerLinkerFrontendVariantNode("CUDA"),
  2453. objcCompilerLinkerFrontendVariantNode("OBJC"),
  2454. objcxxCompilerLinkerFrontendVariantNode("OBJCXX"),
  2455. fortranCompilerLinkerFrontendVariantNode("Fortran"),
  2456. hipCompilerLinkerFrontendVariantNode("HIP");
  2457. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  2458. {
  2459. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  2460. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2461. std::string Evaluate(
  2462. std::vector<std::string> const& parameters,
  2463. cmGeneratorExpressionContext* context,
  2464. GeneratorExpressionContent const* content,
  2465. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2466. {
  2467. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2468. if (!context->HeadTarget || !dagChecker ||
  2469. !dagChecker->EvaluatingLinkLibraries(nullptr,
  2470. ForGenex::LINK_LIBRARY)) {
  2471. reportError(context, content->GetOriginalExpression(),
  2472. "$<LINK_LIBRARY:...> may only be used with binary targets "
  2473. "to specify link libraries through 'LINK_LIBRARIES', "
  2474. "'INTERFACE_LINK_LIBRARIES', and "
  2475. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2476. return std::string();
  2477. }
  2478. cmList list{ parameters.begin(), parameters.end() };
  2479. if (list.empty()) {
  2480. reportError(
  2481. context, content->GetOriginalExpression(),
  2482. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  2483. return std::string();
  2484. }
  2485. if (list.size() == 1) {
  2486. // no libraries specified, ignore this genex
  2487. return std::string();
  2488. }
  2489. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2490. auto const& feature = list.front();
  2491. if (!featureNameValidator.find(feature)) {
  2492. reportError(context, content->GetOriginalExpression(),
  2493. cmStrCat("The feature name '", feature,
  2494. "' contains invalid characters."));
  2495. return std::string();
  2496. }
  2497. auto const LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  2498. auto const LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  2499. // filter out $<LINK_LIBRARY:..> tags with same feature
  2500. // and raise an error for any different feature
  2501. cm::erase_if(list, [&](std::string const& item) -> bool {
  2502. return item == LL_BEGIN || item == LL_END;
  2503. });
  2504. auto it =
  2505. std::find_if(list.cbegin() + 1, list.cend(),
  2506. [&feature](std::string const& item) -> bool {
  2507. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  2508. item.substr(14, item.find('>', 14) - 14) != feature;
  2509. });
  2510. if (it != list.cend()) {
  2511. reportError(
  2512. context, content->GetOriginalExpression(),
  2513. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  2514. return std::string();
  2515. }
  2516. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  2517. it = std::find_if(list.cbegin() + 1, list.cend(),
  2518. [](std::string const& item) -> bool {
  2519. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  2520. });
  2521. if (it != list.cend()) {
  2522. reportError(context, content->GetOriginalExpression(),
  2523. "$<LINK_GROUP:...> cannot be nested inside a "
  2524. "$<LINK_LIBRARY:...> expression.");
  2525. return std::string();
  2526. }
  2527. list.front() = LL_BEGIN;
  2528. list.push_back(LL_END);
  2529. return list.to_string();
  2530. }
  2531. } linkLibraryNode;
  2532. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  2533. {
  2534. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  2535. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2536. std::string Evaluate(
  2537. std::vector<std::string> const& parameters,
  2538. cmGeneratorExpressionContext* context,
  2539. GeneratorExpressionContent const* content,
  2540. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2541. {
  2542. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  2543. if (!context->HeadTarget || !dagChecker ||
  2544. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  2545. reportError(
  2546. context, content->GetOriginalExpression(),
  2547. "$<LINK_GROUP:...> may only be used with binary targets "
  2548. "to specify group of link libraries through 'LINK_LIBRARIES', "
  2549. "'INTERFACE_LINK_LIBRARIES', and "
  2550. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  2551. return std::string();
  2552. }
  2553. cmList list{ parameters.begin(), parameters.end() };
  2554. if (list.empty()) {
  2555. reportError(
  2556. context, content->GetOriginalExpression(),
  2557. "$<LINK_GROUP:...> expects a feature name as first argument.");
  2558. return std::string();
  2559. }
  2560. // $<LINK_GROUP:..> cannot be nested
  2561. if (std::find_if(list.cbegin(), list.cend(),
  2562. [](std::string const& item) -> bool {
  2563. return cmHasPrefix(item, "<LINK_GROUP"_s);
  2564. }) != list.cend()) {
  2565. reportError(context, content->GetOriginalExpression(),
  2566. "$<LINK_GROUP:...> cannot be nested.");
  2567. return std::string();
  2568. }
  2569. if (list.size() == 1) {
  2570. // no libraries specified, ignore this genex
  2571. return std::string();
  2572. }
  2573. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  2574. auto const& feature = list.front();
  2575. if (!featureNameValidator.find(feature)) {
  2576. reportError(context, content->GetOriginalExpression(),
  2577. cmStrCat("The feature name '", feature,
  2578. "' contains invalid characters."));
  2579. return std::string();
  2580. }
  2581. auto const LG_BEGIN = cmStrCat(
  2582. "<LINK_GROUP:", feature, ':',
  2583. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  2584. "|"_s),
  2585. '>');
  2586. auto const LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  2587. list.front() = LG_BEGIN;
  2588. list.push_back(LG_END);
  2589. return list.to_string();
  2590. }
  2591. } linkGroupNode;
  2592. static const struct HostLinkNode : public cmGeneratorExpressionNode
  2593. {
  2594. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  2595. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2596. std::string Evaluate(
  2597. std::vector<std::string> const& parameters,
  2598. cmGeneratorExpressionContext* context,
  2599. GeneratorExpressionContent const* content,
  2600. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2601. {
  2602. if (!context->HeadTarget || !dagChecker ||
  2603. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2604. reportError(context, content->GetOriginalExpression(),
  2605. "$<HOST_LINK:...> may only be used with binary targets "
  2606. "to specify link options.");
  2607. return std::string();
  2608. }
  2609. return context->HeadTarget->IsDeviceLink() ? std::string()
  2610. : cmList::to_string(parameters);
  2611. }
  2612. } hostLinkNode;
  2613. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  2614. {
  2615. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  2616. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  2617. std::string Evaluate(
  2618. std::vector<std::string> const& parameters,
  2619. cmGeneratorExpressionContext* context,
  2620. GeneratorExpressionContent const* content,
  2621. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2622. {
  2623. if (!context->HeadTarget || !dagChecker ||
  2624. !dagChecker->EvaluatingLinkOptionsExpression()) {
  2625. reportError(context, content->GetOriginalExpression(),
  2626. "$<DEVICE_LINK:...> may only be used with binary targets "
  2627. "to specify link options.");
  2628. return std::string();
  2629. }
  2630. if (context->HeadTarget->IsDeviceLink()) {
  2631. cmList list{ parameters.begin(), parameters.end() };
  2632. auto const DL_BEGIN = "<DEVICE_LINK>"_s;
  2633. auto const DL_END = "</DEVICE_LINK>"_s;
  2634. cm::erase_if(list, [&](std::string const& item) {
  2635. return item == DL_BEGIN || item == DL_END;
  2636. });
  2637. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  2638. list.push_back(static_cast<std::string>(DL_END));
  2639. return list.to_string();
  2640. }
  2641. return std::string();
  2642. }
  2643. } deviceLinkNode;
  2644. static std::string getLinkedTargetsContent(
  2645. cmGeneratorTarget const* target, std::string const& prop,
  2646. cmGeneratorExpressionContext* context,
  2647. cmGeneratorExpressionDAGChecker* dagChecker, cmGeneratorTarget::UseTo usage)
  2648. {
  2649. std::string result;
  2650. if (cmLinkImplementationLibraries const* impl =
  2651. target->GetLinkImplementationLibraries(
  2652. context->Config, cmGeneratorTarget::UseTo::Compile)) {
  2653. for (cmLinkImplItem const& lib : impl->Libraries) {
  2654. if (lib.Target) {
  2655. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  2656. // caller's property and hand-evaluate it as if it were compiled.
  2657. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  2658. cmGeneratorExpressionContext libContext(
  2659. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  2660. target, context->EvaluateForBuildsystem, lib.Backtrace,
  2661. context->Language);
  2662. std::string libResult = lib.Target->EvaluateInterfaceProperty(
  2663. prop, &libContext, dagChecker, usage);
  2664. if (!libResult.empty()) {
  2665. if (result.empty()) {
  2666. result = std::move(libResult);
  2667. } else {
  2668. result.reserve(result.size() + 1 + libResult.size());
  2669. result += ";";
  2670. result += libResult;
  2671. }
  2672. }
  2673. }
  2674. }
  2675. }
  2676. return result;
  2677. }
  2678. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  2679. {
  2680. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  2681. // This node handles errors on parameter count itself.
  2682. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2683. static char const* GetErrorText(std::string const& targetName,
  2684. std::string const& propertyName)
  2685. {
  2686. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2687. if (targetName.empty() && propertyName.empty()) {
  2688. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2689. "target name and property name.";
  2690. }
  2691. if (targetName.empty()) {
  2692. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  2693. "target name.";
  2694. }
  2695. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  2696. if (!propertyNameValidator.find(propertyName)) {
  2697. return "Target name and property name not supported.";
  2698. }
  2699. return "Target name not supported.";
  2700. }
  2701. return nullptr;
  2702. }
  2703. std::string Evaluate(
  2704. std::vector<std::string> const& parameters,
  2705. cmGeneratorExpressionContext* context,
  2706. GeneratorExpressionContent const* content,
  2707. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  2708. {
  2709. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  2710. cmGeneratorTarget const* target = nullptr;
  2711. std::string targetName;
  2712. std::string propertyName;
  2713. if (parameters.size() == 2) {
  2714. targetName = parameters[0];
  2715. propertyName = parameters[1];
  2716. if (char const* e = GetErrorText(targetName, propertyName)) {
  2717. reportError(context, content->GetOriginalExpression(), e);
  2718. return std::string();
  2719. }
  2720. if (propertyName == "ALIASED_TARGET"_s) {
  2721. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2722. if (cmGeneratorTarget* tgt =
  2723. context->LG->FindGeneratorTargetToUse(targetName)) {
  2724. return tgt->GetName();
  2725. }
  2726. }
  2727. return std::string();
  2728. }
  2729. if (propertyName == "ALIAS_GLOBAL"_s) {
  2730. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  2731. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  2732. ? "TRUE"
  2733. : "FALSE";
  2734. }
  2735. return std::string();
  2736. }
  2737. cmLocalGenerator const* lg = context->CurrentTarget
  2738. ? context->CurrentTarget->GetLocalGenerator()
  2739. : context->LG;
  2740. target = lg->FindGeneratorTargetToUse(targetName);
  2741. if (!target) {
  2742. std::ostringstream e;
  2743. e << "Target \"" << targetName << "\" not found.";
  2744. reportError(context, content->GetOriginalExpression(), e.str());
  2745. return std::string();
  2746. }
  2747. context->AllTargets.insert(target);
  2748. } else if (parameters.size() == 1) {
  2749. target = context->HeadTarget;
  2750. propertyName = parameters[0];
  2751. // Keep track of the properties seen while processing.
  2752. // The evaluation of the LINK_LIBRARIES generator expressions
  2753. // will check this to ensure that properties have one consistent
  2754. // value for all evaluations.
  2755. context->SeenTargetProperties.insert(propertyName);
  2756. context->HadHeadSensitiveCondition = true;
  2757. if (!target) {
  2758. reportError(
  2759. context, content->GetOriginalExpression(),
  2760. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  2761. "It may not be used with add_custom_command or add_custom_target. "
  2762. " "
  2763. " "
  2764. "Specify the target to read a property from using the "
  2765. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  2766. return std::string();
  2767. }
  2768. } else {
  2769. reportError(
  2770. context, content->GetOriginalExpression(),
  2771. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  2772. return std::string();
  2773. }
  2774. if (propertyName == "SOURCES") {
  2775. context->SourceSensitiveTargets.insert(target);
  2776. }
  2777. if (propertyName.empty()) {
  2778. reportError(
  2779. context, content->GetOriginalExpression(),
  2780. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  2781. "name.");
  2782. return std::string();
  2783. }
  2784. if (!propertyNameValidator.find(propertyName)) {
  2785. ::reportError(context, content->GetOriginalExpression(),
  2786. "Property name not supported.");
  2787. return std::string();
  2788. }
  2789. assert(target);
  2790. if (propertyName == "LINKER_LANGUAGE") {
  2791. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  2792. (dagCheckerParent->EvaluatingLinkLibraries() ||
  2793. dagCheckerParent->EvaluatingSources())) {
  2794. reportError(
  2795. context, content->GetOriginalExpression(),
  2796. "LINKER_LANGUAGE target property can not be used while evaluating "
  2797. "link libraries for a static library");
  2798. return std::string();
  2799. }
  2800. return target->GetLinkerLanguage(context->Config);
  2801. }
  2802. bool const evaluatingLinkLibraries =
  2803. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries();
  2804. std::string interfacePropertyName;
  2805. bool isInterfaceProperty = false;
  2806. cmGeneratorTarget::UseTo usage = cmGeneratorTarget::UseTo::Compile;
  2807. if (cm::optional<cmGeneratorTarget::TransitiveProperty> transitiveProp =
  2808. target->IsTransitiveProperty(propertyName, context->LG,
  2809. context->Config, dagCheckerParent)) {
  2810. interfacePropertyName = std::string(transitiveProp->InterfaceName);
  2811. isInterfaceProperty = transitiveProp->InterfaceName == propertyName;
  2812. usage = transitiveProp->Usage;
  2813. }
  2814. if (dagCheckerParent) {
  2815. // This $<TARGET_PROPERTY:...> node has been reached while evaluating
  2816. // another target property value. Check that the outermost evaluation
  2817. // expects such nested evaluations.
  2818. if (dagCheckerParent->EvaluatingGenexExpression() ||
  2819. dagCheckerParent->EvaluatingPICExpression() ||
  2820. dagCheckerParent->EvaluatingLinkerLauncher()) {
  2821. // No check required.
  2822. } else if (evaluatingLinkLibraries) {
  2823. if (!interfacePropertyName.empty()) {
  2824. reportError(
  2825. context, content->GetOriginalExpression(),
  2826. "$<TARGET_PROPERTY:...> expression in link libraries "
  2827. "evaluation depends on target property which is transitive "
  2828. "over the link libraries, creating a recursion.");
  2829. return std::string();
  2830. }
  2831. } else {
  2832. assert(dagCheckerParent->EvaluatingTransitiveProperty());
  2833. }
  2834. }
  2835. if (isInterfaceProperty) {
  2836. return cmGeneratorExpression::StripEmptyListElements(
  2837. target->EvaluateInterfaceProperty(propertyName, context,
  2838. dagCheckerParent, usage));
  2839. }
  2840. cmGeneratorExpressionDAGChecker dagChecker{
  2841. target,
  2842. propertyName,
  2843. content,
  2844. dagCheckerParent,
  2845. context->LG,
  2846. context->Config,
  2847. context->Backtrace,
  2848. };
  2849. switch (dagChecker.Check()) {
  2850. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  2851. dagChecker.ReportError(context, content->GetOriginalExpression());
  2852. return std::string();
  2853. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  2854. // No error. We just skip cyclic references.
  2855. return std::string();
  2856. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  2857. // We handle transitive properties above. For non-transitive
  2858. // properties we accept repeats anyway.
  2859. case cmGeneratorExpressionDAGChecker::DAG:
  2860. break;
  2861. }
  2862. std::string result;
  2863. bool haveProp = false;
  2864. if (cmValue p = target->GetProperty(propertyName)) {
  2865. result = *p;
  2866. haveProp = true;
  2867. } else if (evaluatingLinkLibraries) {
  2868. return std::string();
  2869. }
  2870. // Properties named by COMPATIBLE_INTERFACE_ properties combine over
  2871. // the transitive link closure as a single order-independent value.
  2872. // Imported targets do not themselves have a defined value for these
  2873. // properties, but they can contribute to the value of a non-imported
  2874. // dependent.
  2875. //
  2876. // For COMPATIBLE_INTERFACE_{BOOL,STRING}:
  2877. // * If set on this target, use the value directly. It is checked
  2878. // elsewhere for consistency over the transitive link closure.
  2879. // * If not set on this target, compute the value from the closure.
  2880. //
  2881. // For COMPATIBLE_INTERFACE_NUMBER_{MAX,MIN} we always compute the value
  2882. // from this target and the transitive link closure to get the max or min.
  2883. if (!haveProp && !target->IsImported()) {
  2884. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  2885. context->Config)) {
  2886. context->HadContextSensitiveCondition = true;
  2887. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  2888. context->Config)
  2889. ? "1"
  2890. : "0";
  2891. }
  2892. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  2893. context->Config)) {
  2894. context->HadContextSensitiveCondition = true;
  2895. char const* propContent =
  2896. target->GetLinkInterfaceDependentStringProperty(propertyName,
  2897. context->Config);
  2898. return propContent ? propContent : "";
  2899. }
  2900. }
  2901. if (!evaluatingLinkLibraries && !target->IsImported()) {
  2902. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  2903. context->Config)) {
  2904. context->HadContextSensitiveCondition = true;
  2905. char const* propContent =
  2906. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  2907. context->Config);
  2908. return propContent ? propContent : "";
  2909. }
  2910. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  2911. context->Config)) {
  2912. context->HadContextSensitiveCondition = true;
  2913. char const* propContent =
  2914. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  2915. context->Config);
  2916. return propContent ? propContent : "";
  2917. }
  2918. }
  2919. // Some properties, such as usage requirements, combine over the
  2920. // transitive link closure as an ordered list.
  2921. if (!interfacePropertyName.empty()) {
  2922. result = cmGeneratorExpression::StripEmptyListElements(
  2923. this->EvaluateDependentExpression(result, context->LG, context, target,
  2924. &dagChecker, target));
  2925. std::string linkedTargetsContent = getLinkedTargetsContent(
  2926. target, interfacePropertyName, context, &dagChecker, usage);
  2927. if (!linkedTargetsContent.empty()) {
  2928. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  2929. }
  2930. }
  2931. return result;
  2932. }
  2933. } targetPropertyNode;
  2934. static const struct TargetNameNode : public cmGeneratorExpressionNode
  2935. {
  2936. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  2937. bool GeneratesContent() const override { return true; }
  2938. bool AcceptsArbitraryContentParameter() const override { return true; }
  2939. bool RequiresLiteralInput() const override { return true; }
  2940. std::string Evaluate(
  2941. std::vector<std::string> const& parameters,
  2942. cmGeneratorExpressionContext* /*context*/,
  2943. GeneratorExpressionContent const* /*content*/,
  2944. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2945. {
  2946. return parameters.front();
  2947. }
  2948. int NumExpectedParameters() const override { return 1; }
  2949. } targetNameNode;
  2950. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  2951. {
  2952. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  2953. std::string Evaluate(
  2954. std::vector<std::string> const& parameters,
  2955. cmGeneratorExpressionContext* context,
  2956. GeneratorExpressionContent const* content,
  2957. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2958. {
  2959. std::string const& tgtName = parameters.front();
  2960. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2961. if (!gt) {
  2962. std::ostringstream e;
  2963. e << "Objects of target \"" << tgtName
  2964. << "\" referenced but no such target exists.";
  2965. reportError(context, content->GetOriginalExpression(), e.str());
  2966. return std::string();
  2967. }
  2968. cmStateEnums::TargetType type = gt->GetType();
  2969. if (type != cmStateEnums::EXECUTABLE &&
  2970. type != cmStateEnums::STATIC_LIBRARY &&
  2971. type != cmStateEnums::SHARED_LIBRARY &&
  2972. type != cmStateEnums::MODULE_LIBRARY &&
  2973. type != cmStateEnums::OBJECT_LIBRARY) {
  2974. std::ostringstream e;
  2975. e << "Objects of target \"" << tgtName
  2976. << "\" referenced but is not one of the allowed target types "
  2977. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2978. reportError(context, content->GetOriginalExpression(), e.str());
  2979. return std::string();
  2980. }
  2981. cmGlobalGenerator const* gg = context->LG->GetGlobalGenerator();
  2982. {
  2983. std::string reason;
  2984. if (!context->EvaluateForBuildsystem &&
  2985. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2986. std::ostringstream e;
  2987. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2988. "is only suitable for consumption by CMake (limited"
  2989. << reason
  2990. << "). "
  2991. "It is not suitable for writing out elsewhere.";
  2992. reportError(context, content->GetOriginalExpression(), e.str());
  2993. return std::string();
  2994. }
  2995. }
  2996. cmList objects;
  2997. if (gt->IsImported()) {
  2998. cmValue loc = nullptr;
  2999. cmValue imp = nullptr;
  3000. std::string suffix;
  3001. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  3002. objects.assign(*loc);
  3003. }
  3004. context->HadContextSensitiveCondition = true;
  3005. } else {
  3006. gt->GetTargetObjectNames(context->Config, objects);
  3007. std::string obj_dir;
  3008. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  3009. // Use object file directory with buildsystem placeholder.
  3010. obj_dir = gt->ObjectDirectory;
  3011. context->HadContextSensitiveCondition =
  3012. gt->HasContextDependentSources();
  3013. } else {
  3014. // Use object file directory with per-config location.
  3015. obj_dir = gt->GetObjectDirectory(context->Config);
  3016. context->HadContextSensitiveCondition = true;
  3017. }
  3018. for (auto& o : objects) {
  3019. o = cmStrCat(obj_dir, o);
  3020. }
  3021. }
  3022. // Create the cmSourceFile instances in the referencing directory.
  3023. cmMakefile* mf = context->LG->GetMakefile();
  3024. for (std::string const& o : objects) {
  3025. mf->AddTargetObject(tgtName, o);
  3026. }
  3027. return objects.to_string();
  3028. }
  3029. } targetObjectsNode;
  3030. struct TargetRuntimeDllsBaseNode : public cmGeneratorExpressionNode
  3031. {
  3032. std::vector<std::string> CollectDlls(
  3033. std::vector<std::string> const& parameters,
  3034. cmGeneratorExpressionContext* context,
  3035. GeneratorExpressionContent const* content) const
  3036. {
  3037. std::string const& tgtName = parameters.front();
  3038. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  3039. if (!gt) {
  3040. std::ostringstream e;
  3041. e << "Objects of target \"" << tgtName
  3042. << "\" referenced but no such target exists.";
  3043. reportError(context, content->GetOriginalExpression(), e.str());
  3044. return std::vector<std::string>();
  3045. }
  3046. cmStateEnums::TargetType type = gt->GetType();
  3047. if (type != cmStateEnums::EXECUTABLE &&
  3048. type != cmStateEnums::SHARED_LIBRARY &&
  3049. type != cmStateEnums::MODULE_LIBRARY) {
  3050. std::ostringstream e;
  3051. e << "Objects of target \"" << tgtName
  3052. << "\" referenced but is not one of the allowed target types "
  3053. << "(EXECUTABLE, SHARED, MODULE).";
  3054. reportError(context, content->GetOriginalExpression(), e.str());
  3055. return std::vector<std::string>();
  3056. }
  3057. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  3058. std::vector<std::string> dllPaths;
  3059. auto const& dlls = cli->GetRuntimeDLLs();
  3060. for (auto const& dll : dlls) {
  3061. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  3062. dllPaths.emplace_back(*loc);
  3063. }
  3064. }
  3065. return dllPaths;
  3066. }
  3067. return std::vector<std::string>();
  3068. }
  3069. };
  3070. static const struct TargetRuntimeDllsNode : public TargetRuntimeDllsBaseNode
  3071. {
  3072. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  3073. std::string Evaluate(
  3074. std::vector<std::string> const& parameters,
  3075. cmGeneratorExpressionContext* context,
  3076. GeneratorExpressionContent const* content,
  3077. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3078. {
  3079. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3080. return cmList::to_string(dlls);
  3081. }
  3082. } targetRuntimeDllsNode;
  3083. static const struct TargetRuntimeDllDirsNode : public TargetRuntimeDllsBaseNode
  3084. {
  3085. TargetRuntimeDllDirsNode() {} // NOLINT(modernize-use-equals-default)
  3086. std::string Evaluate(
  3087. std::vector<std::string> const& parameters,
  3088. cmGeneratorExpressionContext* context,
  3089. GeneratorExpressionContent const* content,
  3090. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3091. {
  3092. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  3093. std::vector<std::string> dllDirs;
  3094. for (std::string const& dll : dlls) {
  3095. std::string directory = cmSystemTools::GetFilenamePath(dll);
  3096. if (std::find(dllDirs.begin(), dllDirs.end(), directory) ==
  3097. dllDirs.end()) {
  3098. dllDirs.push_back(directory);
  3099. }
  3100. }
  3101. return cmList::to_string(dllDirs);
  3102. }
  3103. } targetRuntimeDllDirsNode;
  3104. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  3105. {
  3106. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  3107. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  3108. std::string Evaluate(
  3109. std::vector<std::string> const& parameters,
  3110. cmGeneratorExpressionContext* context,
  3111. GeneratorExpressionContent const* content,
  3112. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3113. {
  3114. cmGeneratorTarget const* target = context->HeadTarget;
  3115. if (!target) {
  3116. reportError(
  3117. context, content->GetOriginalExpression(),
  3118. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  3119. "not be used with add_custom_command or add_custom_target.");
  3120. return std::string();
  3121. }
  3122. context->HadHeadSensitiveCondition = true;
  3123. using LangMap = std::map<std::string, cmList>;
  3124. static LangMap availableFeatures;
  3125. LangMap testedFeatures;
  3126. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  3127. for (std::string const& p : parameters) {
  3128. std::string error;
  3129. std::string lang;
  3130. if (!standardResolver.CompileFeatureKnown(
  3131. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  3132. reportError(context, content->GetOriginalExpression(), error);
  3133. return std::string();
  3134. }
  3135. testedFeatures[lang].push_back(p);
  3136. if (availableFeatures.find(lang) == availableFeatures.end()) {
  3137. cmValue featuresKnown =
  3138. standardResolver.CompileFeaturesAvailable(lang, &error);
  3139. if (!featuresKnown) {
  3140. reportError(context, content->GetOriginalExpression(), error);
  3141. return std::string();
  3142. }
  3143. availableFeatures[lang].assign(featuresKnown);
  3144. }
  3145. }
  3146. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  3147. for (auto const& lit : testedFeatures) {
  3148. std::vector<std::string> const& langAvailable =
  3149. availableFeatures[lit.first];
  3150. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  3151. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  3152. for (std::string const& it : lit.second) {
  3153. if (!cm::contains(langAvailable, it)) {
  3154. return "0";
  3155. }
  3156. if (standardDefault && standardDefault->empty()) {
  3157. // This compiler has no notion of language standard levels.
  3158. // All features known for the language are always available.
  3159. continue;
  3160. }
  3161. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  3162. context->Config, it)) {
  3163. if (evalLL) {
  3164. cmValue l =
  3165. target->GetLanguageStandard(lit.first, context->Config);
  3166. if (!l) {
  3167. l = standardDefault;
  3168. }
  3169. assert(l);
  3170. context->MaxLanguageStandard[target][lit.first] = *l;
  3171. } else {
  3172. return "0";
  3173. }
  3174. }
  3175. }
  3176. }
  3177. return "1";
  3178. }
  3179. } compileFeaturesNode;
  3180. static char const* targetPolicyWhitelist[] = {
  3181. nullptr
  3182. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  3183. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  3184. #undef TARGET_POLICY_STRING
  3185. };
  3186. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  3187. char const* policy)
  3188. {
  3189. #define RETURN_POLICY(POLICY) \
  3190. if (strcmp(policy, #POLICY) == 0) { \
  3191. return tgt->GetPolicyStatus##POLICY(); \
  3192. }
  3193. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  3194. #undef RETURN_POLICY
  3195. assert(false && "Unreachable code. Not a valid policy");
  3196. return cmPolicies::WARN;
  3197. }
  3198. static cmPolicies::PolicyID policyForString(char const* policy_id)
  3199. {
  3200. #define RETURN_POLICY_ID(POLICY_ID) \
  3201. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  3202. return cmPolicies::POLICY_ID; \
  3203. }
  3204. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  3205. #undef RETURN_POLICY_ID
  3206. assert(false && "Unreachable code. Not a valid policy");
  3207. return cmPolicies::CMPCOUNT;
  3208. }
  3209. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  3210. {
  3211. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  3212. int NumExpectedParameters() const override { return 1; }
  3213. std::string Evaluate(
  3214. std::vector<std::string> const& parameters,
  3215. cmGeneratorExpressionContext* context,
  3216. GeneratorExpressionContent const* content,
  3217. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3218. {
  3219. if (!context->HeadTarget) {
  3220. reportError(
  3221. context, content->GetOriginalExpression(),
  3222. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  3223. "may not be used with add_custom_command or add_custom_target.");
  3224. return std::string();
  3225. }
  3226. context->HadContextSensitiveCondition = true;
  3227. context->HadHeadSensitiveCondition = true;
  3228. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  3229. char const* policy = targetPolicyWhitelist[i];
  3230. if (parameters.front() == policy) {
  3231. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  3232. switch (statusForTarget(context->HeadTarget, policy)) {
  3233. case cmPolicies::WARN:
  3234. lg->IssueMessage(
  3235. MessageType::AUTHOR_WARNING,
  3236. cmPolicies::GetPolicyWarning(policyForString(policy)));
  3237. CM_FALLTHROUGH;
  3238. case cmPolicies::OLD:
  3239. return "0";
  3240. case cmPolicies::NEW:
  3241. return "1";
  3242. }
  3243. }
  3244. }
  3245. reportError(
  3246. context, content->GetOriginalExpression(),
  3247. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  3248. "policies. Currently it may be used with the following policies:\n"
  3249. #define STRINGIFY_HELPER(X) #X
  3250. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  3251. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  3252. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  3253. #undef TARGET_POLICY_LIST_ITEM
  3254. );
  3255. return std::string();
  3256. }
  3257. } targetPolicyNode;
  3258. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  3259. {
  3260. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  3261. bool GeneratesContent() const override { return true; }
  3262. int NumExpectedParameters() const override { return 0; }
  3263. std::string Evaluate(
  3264. std::vector<std::string> const& /*parameters*/,
  3265. cmGeneratorExpressionContext* context,
  3266. GeneratorExpressionContent const* content,
  3267. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  3268. {
  3269. reportError(context, content->GetOriginalExpression(),
  3270. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  3271. "should never be evaluated.");
  3272. return std::string();
  3273. }
  3274. } installPrefixNode;
  3275. class ArtifactDirTag;
  3276. class ArtifactLinkerTag;
  3277. class ArtifactLinkerLibraryTag;
  3278. class ArtifactLinkerImportTag;
  3279. class ArtifactNameTag;
  3280. class ArtifactImportTag;
  3281. class ArtifactPathTag;
  3282. class ArtifactPdbTag;
  3283. class ArtifactSonameTag;
  3284. class ArtifactSonameImportTag;
  3285. class ArtifactBundleDirTag;
  3286. class ArtifactBundleDirNameTag;
  3287. class ArtifactBundleContentDirTag;
  3288. template <typename ArtifactT, typename ComponentT>
  3289. struct TargetFilesystemArtifactDependency
  3290. {
  3291. static void AddDependency(cmGeneratorTarget* target,
  3292. cmGeneratorExpressionContext* context)
  3293. {
  3294. context->DependTargets.insert(target);
  3295. context->AllTargets.insert(target);
  3296. }
  3297. };
  3298. struct TargetFilesystemArtifactDependencyCMP0112
  3299. {
  3300. static void AddDependency(cmGeneratorTarget* target,
  3301. cmGeneratorExpressionContext* context)
  3302. {
  3303. context->AllTargets.insert(target);
  3304. cmLocalGenerator const* lg = context->LG;
  3305. switch (target->GetPolicyStatusCMP0112()) {
  3306. case cmPolicies::WARN:
  3307. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  3308. "CMAKE_POLICY_WARNING_CMP0112")) {
  3309. std::string err =
  3310. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  3311. "\nDependency being added to target:\n \"",
  3312. target->GetName(), "\"\n");
  3313. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  3314. err, context->Backtrace);
  3315. }
  3316. CM_FALLTHROUGH;
  3317. case cmPolicies::OLD:
  3318. context->DependTargets.insert(target);
  3319. break;
  3320. case cmPolicies::NEW:
  3321. break;
  3322. }
  3323. }
  3324. };
  3325. template <typename ArtifactT>
  3326. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  3327. : TargetFilesystemArtifactDependencyCMP0112
  3328. {
  3329. };
  3330. template <typename ArtifactT>
  3331. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  3332. : TargetFilesystemArtifactDependencyCMP0112
  3333. {
  3334. };
  3335. template <>
  3336. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  3337. ArtifactPathTag>
  3338. : TargetFilesystemArtifactDependencyCMP0112
  3339. {
  3340. };
  3341. template <>
  3342. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  3343. ArtifactPathTag>
  3344. : TargetFilesystemArtifactDependencyCMP0112
  3345. {
  3346. };
  3347. template <>
  3348. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  3349. ArtifactPathTag>
  3350. : TargetFilesystemArtifactDependencyCMP0112
  3351. {
  3352. };
  3353. template <typename ArtifactT>
  3354. struct TargetFilesystemArtifactResultCreator
  3355. {
  3356. static std::string Create(cmGeneratorTarget* target,
  3357. cmGeneratorExpressionContext* context,
  3358. GeneratorExpressionContent const* content);
  3359. };
  3360. template <>
  3361. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  3362. {
  3363. static std::string Create(cmGeneratorTarget* target,
  3364. cmGeneratorExpressionContext* context,
  3365. GeneratorExpressionContent const* content)
  3366. {
  3367. // The target soname file (.so.1).
  3368. if (target->IsDLLPlatform()) {
  3369. ::reportError(context, content->GetOriginalExpression(),
  3370. "TARGET_SONAME_FILE is not allowed "
  3371. "for DLL target platforms.");
  3372. return std::string();
  3373. }
  3374. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3375. ::reportError(context, content->GetOriginalExpression(),
  3376. "TARGET_SONAME_FILE is allowed only for "
  3377. "SHARED libraries.");
  3378. return std::string();
  3379. }
  3380. if (target->IsArchivedAIXSharedLibrary()) {
  3381. ::reportError(context, content->GetOriginalExpression(),
  3382. "TARGET_SONAME_FILE is not allowed for "
  3383. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3384. return std::string();
  3385. }
  3386. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  3387. target->GetSOName(context->Config));
  3388. return result;
  3389. }
  3390. };
  3391. template <>
  3392. struct TargetFilesystemArtifactResultCreator<ArtifactSonameImportTag>
  3393. {
  3394. static std::string Create(cmGeneratorTarget* target,
  3395. cmGeneratorExpressionContext* context,
  3396. GeneratorExpressionContent const* content)
  3397. {
  3398. // The target soname file (.so.1).
  3399. if (target->IsDLLPlatform()) {
  3400. ::reportError(context, content->GetOriginalExpression(),
  3401. "TARGET_SONAME_IMPORT_FILE is not allowed "
  3402. "for DLL target platforms.");
  3403. return std::string();
  3404. }
  3405. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  3406. ::reportError(context, content->GetOriginalExpression(),
  3407. "TARGET_SONAME_IMPORT_FILE is allowed only for "
  3408. "SHARED libraries.");
  3409. return std::string();
  3410. }
  3411. if (target->IsArchivedAIXSharedLibrary()) {
  3412. ::reportError(context, content->GetOriginalExpression(),
  3413. "TARGET_SONAME_IMPORT_FILE is not allowed for "
  3414. "AIX_SHARED_LIBRARY_ARCHIVE libraries.");
  3415. return std::string();
  3416. }
  3417. if (target->HasImportLibrary(context->Config)) {
  3418. return cmStrCat(target->GetDirectory(
  3419. context->Config, cmStateEnums::ImportLibraryArtifact),
  3420. '/',
  3421. target->GetSOName(context->Config,
  3422. cmStateEnums::ImportLibraryArtifact));
  3423. }
  3424. return std::string{};
  3425. }
  3426. };
  3427. template <>
  3428. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  3429. {
  3430. static std::string Create(cmGeneratorTarget* target,
  3431. cmGeneratorExpressionContext* context,
  3432. GeneratorExpressionContent const* content)
  3433. {
  3434. if (target->IsImported()) {
  3435. ::reportError(context, content->GetOriginalExpression(),
  3436. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  3437. return std::string();
  3438. }
  3439. std::string language = target->GetLinkerLanguage(context->Config);
  3440. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3441. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3442. ::reportError(context, content->GetOriginalExpression(),
  3443. "TARGET_PDB_FILE is not supported by the target linker.");
  3444. return std::string();
  3445. }
  3446. cmStateEnums::TargetType targetType = target->GetType();
  3447. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3448. targetType != cmStateEnums::MODULE_LIBRARY &&
  3449. targetType != cmStateEnums::EXECUTABLE) {
  3450. ::reportError(context, content->GetOriginalExpression(),
  3451. "TARGET_PDB_FILE is allowed only for "
  3452. "targets with linker created artifacts.");
  3453. return std::string();
  3454. }
  3455. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  3456. '/', target->GetPDBName(context->Config));
  3457. return result;
  3458. }
  3459. };
  3460. template <>
  3461. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  3462. {
  3463. static std::string Create(cmGeneratorTarget* target,
  3464. cmGeneratorExpressionContext* context,
  3465. GeneratorExpressionContent const* content)
  3466. {
  3467. // The file used to link to the target (.so, .lib, .a) or import file
  3468. // (.lib, .tbd).
  3469. if (!target->IsLinkable()) {
  3470. ::reportError(context, content->GetOriginalExpression(),
  3471. "TARGET_LINKER_FILE is allowed only for libraries and "
  3472. "executables with ENABLE_EXPORTS.");
  3473. return std::string();
  3474. }
  3475. cmStateEnums::ArtifactType artifact =
  3476. target->HasImportLibrary(context->Config)
  3477. ? cmStateEnums::ImportLibraryArtifact
  3478. : cmStateEnums::RuntimeBinaryArtifact;
  3479. return target->GetFullPath(context->Config, artifact);
  3480. }
  3481. };
  3482. template <>
  3483. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerLibraryTag>
  3484. {
  3485. static std::string Create(cmGeneratorTarget* target,
  3486. cmGeneratorExpressionContext* context,
  3487. GeneratorExpressionContent const* content)
  3488. {
  3489. // The file used to link to the target (.dylib, .so, .a).
  3490. if (!target->IsLinkable() ||
  3491. target->GetType() == cmStateEnums::EXECUTABLE) {
  3492. ::reportError(context, content->GetOriginalExpression(),
  3493. "TARGET_LINKER_LIBRARY_FILE is allowed only for libraries "
  3494. "with ENABLE_EXPORTS.");
  3495. return std::string();
  3496. }
  3497. if (!target->IsDLLPlatform() ||
  3498. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3499. return target->GetFullPath(context->Config,
  3500. cmStateEnums::RuntimeBinaryArtifact);
  3501. }
  3502. return std::string{};
  3503. }
  3504. };
  3505. template <>
  3506. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerImportTag>
  3507. {
  3508. static std::string Create(cmGeneratorTarget* target,
  3509. cmGeneratorExpressionContext* context,
  3510. GeneratorExpressionContent const* content)
  3511. {
  3512. // The file used to link to the target (.lib, .tbd).
  3513. if (!target->IsLinkable()) {
  3514. ::reportError(
  3515. context, content->GetOriginalExpression(),
  3516. "TARGET_LINKER_IMPORT_FILE is allowed only for libraries and "
  3517. "executables with ENABLE_EXPORTS.");
  3518. return std::string();
  3519. }
  3520. if (target->HasImportLibrary(context->Config)) {
  3521. return target->GetFullPath(context->Config,
  3522. cmStateEnums::ImportLibraryArtifact);
  3523. }
  3524. return std::string{};
  3525. }
  3526. };
  3527. template <>
  3528. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  3529. {
  3530. static std::string Create(cmGeneratorTarget* target,
  3531. cmGeneratorExpressionContext* context,
  3532. GeneratorExpressionContent const* content)
  3533. {
  3534. if (target->IsImported()) {
  3535. ::reportError(context, content->GetOriginalExpression(),
  3536. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  3537. return std::string();
  3538. }
  3539. if (!target->IsBundleOnApple()) {
  3540. ::reportError(context, content->GetOriginalExpression(),
  3541. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  3542. return std::string();
  3543. }
  3544. std::string outpath = target->GetDirectory(context->Config) + '/';
  3545. return target->BuildBundleDirectory(outpath, context->Config,
  3546. cmGeneratorTarget::BundleDirLevel);
  3547. }
  3548. };
  3549. template <>
  3550. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  3551. {
  3552. static std::string Create(cmGeneratorTarget* target,
  3553. cmGeneratorExpressionContext* context,
  3554. GeneratorExpressionContent const* content)
  3555. {
  3556. if (target->IsImported()) {
  3557. ::reportError(
  3558. context, content->GetOriginalExpression(),
  3559. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  3560. return std::string();
  3561. }
  3562. if (!target->IsBundleOnApple()) {
  3563. ::reportError(
  3564. context, content->GetOriginalExpression(),
  3565. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  3566. return std::string();
  3567. }
  3568. auto level = cmGeneratorTarget::BundleDirLevel;
  3569. auto config = context->Config;
  3570. if (target->IsAppBundleOnApple()) {
  3571. return target->GetAppBundleDirectory(config, level);
  3572. }
  3573. if (target->IsFrameworkOnApple()) {
  3574. return target->GetFrameworkDirectory(config, level);
  3575. }
  3576. if (target->IsCFBundleOnApple()) {
  3577. return target->GetCFBundleDirectory(config, level);
  3578. }
  3579. return std::string();
  3580. }
  3581. };
  3582. template <>
  3583. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  3584. {
  3585. static std::string Create(cmGeneratorTarget* target,
  3586. cmGeneratorExpressionContext* context,
  3587. GeneratorExpressionContent const* content)
  3588. {
  3589. if (target->IsImported()) {
  3590. ::reportError(
  3591. context, content->GetOriginalExpression(),
  3592. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  3593. return std::string();
  3594. }
  3595. if (!target->IsBundleOnApple()) {
  3596. ::reportError(
  3597. context, content->GetOriginalExpression(),
  3598. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  3599. return std::string();
  3600. }
  3601. std::string outpath = target->GetDirectory(context->Config) + '/';
  3602. return target->BuildBundleDirectory(outpath, context->Config,
  3603. cmGeneratorTarget::ContentLevel);
  3604. }
  3605. };
  3606. template <>
  3607. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  3608. {
  3609. static std::string Create(cmGeneratorTarget* target,
  3610. cmGeneratorExpressionContext* context,
  3611. GeneratorExpressionContent const* /*unused*/)
  3612. {
  3613. return target->GetFullPath(context->Config,
  3614. cmStateEnums::RuntimeBinaryArtifact, true);
  3615. }
  3616. };
  3617. template <>
  3618. struct TargetFilesystemArtifactResultCreator<ArtifactImportTag>
  3619. {
  3620. static std::string Create(cmGeneratorTarget* target,
  3621. cmGeneratorExpressionContext* context,
  3622. GeneratorExpressionContent const* /*unused*/)
  3623. {
  3624. if (target->HasImportLibrary(context->Config)) {
  3625. return target->GetFullPath(context->Config,
  3626. cmStateEnums::ImportLibraryArtifact, true);
  3627. }
  3628. return std::string{};
  3629. }
  3630. };
  3631. template <typename ArtifactT>
  3632. struct TargetFilesystemArtifactResultGetter
  3633. {
  3634. static std::string Get(std::string const& result);
  3635. };
  3636. template <>
  3637. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  3638. {
  3639. static std::string Get(std::string const& result)
  3640. {
  3641. return cmSystemTools::GetFilenameName(result);
  3642. }
  3643. };
  3644. template <>
  3645. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  3646. {
  3647. static std::string Get(std::string const& result)
  3648. {
  3649. return cmSystemTools::GetFilenamePath(result);
  3650. }
  3651. };
  3652. template <>
  3653. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  3654. {
  3655. static std::string Get(std::string const& result) { return result; }
  3656. };
  3657. struct TargetArtifactBase : public cmGeneratorExpressionNode
  3658. {
  3659. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  3660. protected:
  3661. cmGeneratorTarget* GetTarget(
  3662. std::vector<std::string> const& parameters,
  3663. cmGeneratorExpressionContext* context,
  3664. GeneratorExpressionContent const* content,
  3665. cmGeneratorExpressionDAGChecker* dagChecker) const
  3666. {
  3667. // Lookup the referenced target.
  3668. std::string const& name = parameters.front();
  3669. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  3670. ::reportError(context, content->GetOriginalExpression(),
  3671. "Expression syntax not recognized.");
  3672. return nullptr;
  3673. }
  3674. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  3675. if (!target) {
  3676. ::reportError(context, content->GetOriginalExpression(),
  3677. "No target \"" + name + "\"");
  3678. return nullptr;
  3679. }
  3680. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  3681. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  3682. ::reportError(context, content->GetOriginalExpression(),
  3683. "Target \"" + name +
  3684. "\" is not an executable or library.");
  3685. return nullptr;
  3686. }
  3687. if (dagChecker &&
  3688. (dagChecker->EvaluatingLinkLibraries(target) ||
  3689. (dagChecker->EvaluatingSources() &&
  3690. target == dagChecker->TopTarget()))) {
  3691. ::reportError(context, content->GetOriginalExpression(),
  3692. "Expressions which require the linker language may not "
  3693. "be used while evaluating link libraries");
  3694. return nullptr;
  3695. }
  3696. return target;
  3697. }
  3698. };
  3699. template <typename ArtifactT, typename ComponentT>
  3700. struct TargetFilesystemArtifact : public TargetArtifactBase
  3701. {
  3702. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  3703. int NumExpectedParameters() const override { return 1; }
  3704. std::string Evaluate(
  3705. std::vector<std::string> const& parameters,
  3706. cmGeneratorExpressionContext* context,
  3707. GeneratorExpressionContent const* content,
  3708. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3709. {
  3710. cmGeneratorTarget* target =
  3711. this->GetTarget(parameters, context, content, dagChecker);
  3712. if (!target) {
  3713. return std::string();
  3714. }
  3715. // Not a dependent target if we are querying for ArtifactDirTag,
  3716. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  3717. // and ArtifactBundleContentDirTag
  3718. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  3719. target, context);
  3720. std::string result =
  3721. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  3722. content);
  3723. if (context->HadError) {
  3724. return std::string();
  3725. }
  3726. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  3727. }
  3728. };
  3729. template <typename ArtifactT>
  3730. struct TargetFilesystemArtifactNodeGroup
  3731. {
  3732. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  3733. {
  3734. }
  3735. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  3736. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  3737. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  3738. };
  3739. static TargetFilesystemArtifactNodeGroup<ArtifactNameTag> const
  3740. targetNodeGroup;
  3741. static TargetFilesystemArtifactNodeGroup<ArtifactImportTag> const
  3742. targetImportNodeGroup;
  3743. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag> const
  3744. targetLinkerNodeGroup;
  3745. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerLibraryTag> const
  3746. targetLinkerLibraryNodeGroup;
  3747. static TargetFilesystemArtifactNodeGroup<ArtifactLinkerImportTag> const
  3748. targetLinkerImportNodeGroup;
  3749. static TargetFilesystemArtifactNodeGroup<ArtifactSonameTag> const
  3750. targetSoNameNodeGroup;
  3751. static TargetFilesystemArtifactNodeGroup<ArtifactSonameImportTag> const
  3752. targetSoNameImportNodeGroup;
  3753. static TargetFilesystemArtifactNodeGroup<ArtifactPdbTag> const
  3754. targetPdbNodeGroup;
  3755. static TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag> const
  3756. targetBundleDirNode;
  3757. static TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  3758. ArtifactNameTag> const targetBundleDirNameNode;
  3759. static TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  3760. ArtifactPathTag> const
  3761. targetBundleContentDirNode;
  3762. //
  3763. // To retrieve base name for various artifacts
  3764. //
  3765. template <typename ArtifactT>
  3766. struct TargetOutputNameArtifactResultGetter
  3767. {
  3768. static std::string Get(cmGeneratorTarget* target,
  3769. cmGeneratorExpressionContext* context,
  3770. GeneratorExpressionContent const* content);
  3771. };
  3772. template <>
  3773. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  3774. {
  3775. static std::string Get(cmGeneratorTarget* target,
  3776. cmGeneratorExpressionContext* context,
  3777. GeneratorExpressionContent const* /*unused*/)
  3778. {
  3779. return target->GetOutputName(context->Config,
  3780. cmStateEnums::RuntimeBinaryArtifact) +
  3781. target->GetFilePostfix(context->Config);
  3782. }
  3783. };
  3784. template <>
  3785. struct TargetOutputNameArtifactResultGetter<ArtifactImportTag>
  3786. {
  3787. static std::string Get(cmGeneratorTarget* target,
  3788. cmGeneratorExpressionContext* context,
  3789. GeneratorExpressionContent const* /*unused*/)
  3790. {
  3791. if (target->HasImportLibrary(context->Config)) {
  3792. return target->GetOutputName(context->Config,
  3793. cmStateEnums::ImportLibraryArtifact) +
  3794. target->GetFilePostfix(context->Config);
  3795. }
  3796. return std::string{};
  3797. }
  3798. };
  3799. template <>
  3800. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  3801. {
  3802. static std::string Get(cmGeneratorTarget* target,
  3803. cmGeneratorExpressionContext* context,
  3804. GeneratorExpressionContent const* content)
  3805. {
  3806. // The library file used to link to the target (.so, .lib, .a) or import
  3807. // file (.lin, .tbd).
  3808. if (!target->IsLinkable()) {
  3809. ::reportError(context, content->GetOriginalExpression(),
  3810. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  3811. "libraries and executables with ENABLE_EXPORTS.");
  3812. return std::string();
  3813. }
  3814. cmStateEnums::ArtifactType artifact =
  3815. target->HasImportLibrary(context->Config)
  3816. ? cmStateEnums::ImportLibraryArtifact
  3817. : cmStateEnums::RuntimeBinaryArtifact;
  3818. return target->GetOutputName(context->Config, artifact) +
  3819. target->GetFilePostfix(context->Config);
  3820. }
  3821. };
  3822. template <>
  3823. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerLibraryTag>
  3824. {
  3825. static std::string Get(cmGeneratorTarget* target,
  3826. cmGeneratorExpressionContext* context,
  3827. GeneratorExpressionContent const* content)
  3828. {
  3829. // The library file used to link to the target (.so, .lib, .a).
  3830. if (!target->IsLinkable() ||
  3831. target->GetType() == cmStateEnums::EXECUTABLE) {
  3832. ::reportError(context, content->GetOriginalExpression(),
  3833. "TARGET_LINKER_LIBRARY_FILE_BASE_NAME is allowed only for "
  3834. "libraries with ENABLE_EXPORTS.");
  3835. return std::string();
  3836. }
  3837. if (!target->IsDLLPlatform() ||
  3838. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3839. return target->GetOutputName(context->Config,
  3840. cmStateEnums::ImportLibraryArtifact) +
  3841. target->GetFilePostfix(context->Config);
  3842. }
  3843. return std::string{};
  3844. }
  3845. };
  3846. template <>
  3847. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerImportTag>
  3848. {
  3849. static std::string Get(cmGeneratorTarget* target,
  3850. cmGeneratorExpressionContext* context,
  3851. GeneratorExpressionContent const* content)
  3852. {
  3853. // The import file used to link to the target (.lib, .tbd).
  3854. if (!target->IsLinkable()) {
  3855. ::reportError(context, content->GetOriginalExpression(),
  3856. "TARGET_LINKER_IMPORT_FILE_BASE_NAME is allowed only for "
  3857. "libraries and executables with ENABLE_EXPORTS.");
  3858. return std::string();
  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<ArtifactPdbTag>
  3870. {
  3871. static std::string Get(cmGeneratorTarget* target,
  3872. cmGeneratorExpressionContext* context,
  3873. GeneratorExpressionContent const* content)
  3874. {
  3875. if (target->IsImported()) {
  3876. ::reportError(
  3877. context, content->GetOriginalExpression(),
  3878. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  3879. return std::string();
  3880. }
  3881. std::string language = target->GetLinkerLanguage(context->Config);
  3882. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  3883. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  3884. ::reportError(
  3885. context, content->GetOriginalExpression(),
  3886. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  3887. return std::string();
  3888. }
  3889. cmStateEnums::TargetType targetType = target->GetType();
  3890. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  3891. targetType != cmStateEnums::MODULE_LIBRARY &&
  3892. targetType != cmStateEnums::EXECUTABLE) {
  3893. ::reportError(context, content->GetOriginalExpression(),
  3894. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  3895. "targets with linker created artifacts.");
  3896. return std::string();
  3897. }
  3898. return target->GetPDBOutputName(context->Config);
  3899. }
  3900. };
  3901. template <typename ArtifactT>
  3902. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  3903. {
  3904. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  3905. int NumExpectedParameters() const override { return 1; }
  3906. std::string Evaluate(
  3907. std::vector<std::string> const& parameters,
  3908. cmGeneratorExpressionContext* context,
  3909. GeneratorExpressionContent const* content,
  3910. cmGeneratorExpressionDAGChecker* dagChecker) const override
  3911. {
  3912. cmGeneratorTarget* target =
  3913. this->GetTarget(parameters, context, content, dagChecker);
  3914. if (!target) {
  3915. return std::string();
  3916. }
  3917. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  3918. target, context, content);
  3919. if (context->HadError) {
  3920. return std::string();
  3921. }
  3922. return result;
  3923. }
  3924. };
  3925. static TargetFileBaseNameArtifact<ArtifactNameTag> const
  3926. targetFileBaseNameNode;
  3927. static TargetFileBaseNameArtifact<ArtifactImportTag> const
  3928. targetImportFileBaseNameNode;
  3929. static TargetFileBaseNameArtifact<ArtifactLinkerTag> const
  3930. targetLinkerFileBaseNameNode;
  3931. static TargetFileBaseNameArtifact<ArtifactLinkerLibraryTag> const
  3932. targetLinkerLibraryFileBaseNameNode;
  3933. static TargetFileBaseNameArtifact<ArtifactLinkerImportTag> const
  3934. targetLinkerImportFileBaseNameNode;
  3935. static TargetFileBaseNameArtifact<ArtifactPdbTag> const
  3936. targetPdbFileBaseNameNode;
  3937. class ArtifactFilePrefixTag;
  3938. class ArtifactImportFilePrefixTag;
  3939. class ArtifactLinkerFilePrefixTag;
  3940. class ArtifactLinkerLibraryFilePrefixTag;
  3941. class ArtifactLinkerImportFilePrefixTag;
  3942. class ArtifactFileSuffixTag;
  3943. class ArtifactImportFileSuffixTag;
  3944. class ArtifactLinkerFileSuffixTag;
  3945. class ArtifactLinkerLibraryFileSuffixTag;
  3946. class ArtifactLinkerImportFileSuffixTag;
  3947. template <typename ArtifactT>
  3948. struct TargetFileArtifactResultGetter
  3949. {
  3950. static std::string Get(cmGeneratorTarget* target,
  3951. cmGeneratorExpressionContext* context,
  3952. GeneratorExpressionContent const* content);
  3953. };
  3954. template <>
  3955. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  3956. {
  3957. static std::string Get(cmGeneratorTarget* target,
  3958. cmGeneratorExpressionContext* context,
  3959. GeneratorExpressionContent const*)
  3960. {
  3961. return target->GetFilePrefix(context->Config);
  3962. }
  3963. };
  3964. template <>
  3965. struct TargetFileArtifactResultGetter<ArtifactImportFilePrefixTag>
  3966. {
  3967. static std::string Get(cmGeneratorTarget* target,
  3968. cmGeneratorExpressionContext* context,
  3969. GeneratorExpressionContent const*)
  3970. {
  3971. if (target->HasImportLibrary(context->Config)) {
  3972. return target->GetFilePrefix(context->Config,
  3973. cmStateEnums::ImportLibraryArtifact);
  3974. }
  3975. return std::string{};
  3976. }
  3977. };
  3978. template <>
  3979. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  3980. {
  3981. static std::string Get(cmGeneratorTarget* target,
  3982. cmGeneratorExpressionContext* context,
  3983. GeneratorExpressionContent const* content)
  3984. {
  3985. if (!target->IsLinkable()) {
  3986. ::reportError(
  3987. context, content->GetOriginalExpression(),
  3988. "TARGET_LINKER_FILE_PREFIX is allowed only for libraries and "
  3989. "executables with ENABLE_EXPORTS.");
  3990. return std::string();
  3991. }
  3992. cmStateEnums::ArtifactType artifact =
  3993. target->HasImportLibrary(context->Config)
  3994. ? cmStateEnums::ImportLibraryArtifact
  3995. : cmStateEnums::RuntimeBinaryArtifact;
  3996. return target->GetFilePrefix(context->Config, artifact);
  3997. }
  3998. };
  3999. template <>
  4000. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFilePrefixTag>
  4001. {
  4002. static std::string Get(cmGeneratorTarget* target,
  4003. cmGeneratorExpressionContext* context,
  4004. GeneratorExpressionContent const* content)
  4005. {
  4006. if (!target->IsLinkable() ||
  4007. target->GetType() == cmStateEnums::EXECUTABLE) {
  4008. ::reportError(
  4009. context, content->GetOriginalExpression(),
  4010. "TARGET_LINKER_LIBRARY_FILE_PREFIX is allowed only for libraries "
  4011. "with ENABLE_EXPORTS.");
  4012. return std::string();
  4013. }
  4014. if (!target->IsDLLPlatform() ||
  4015. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4016. return target->GetFilePrefix(context->Config,
  4017. cmStateEnums::RuntimeBinaryArtifact);
  4018. }
  4019. return std::string{};
  4020. }
  4021. };
  4022. template <>
  4023. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFilePrefixTag>
  4024. {
  4025. static std::string Get(cmGeneratorTarget* target,
  4026. cmGeneratorExpressionContext* context,
  4027. GeneratorExpressionContent const* content)
  4028. {
  4029. if (!target->IsLinkable()) {
  4030. ::reportError(
  4031. context, content->GetOriginalExpression(),
  4032. "TARGET_LINKER_IMPORT_FILE_PREFIX is allowed only for libraries and "
  4033. "executables with ENABLE_EXPORTS.");
  4034. return std::string();
  4035. }
  4036. if (target->HasImportLibrary(context->Config)) {
  4037. return target->GetFilePrefix(context->Config,
  4038. cmStateEnums::ImportLibraryArtifact);
  4039. }
  4040. return std::string{};
  4041. }
  4042. };
  4043. template <>
  4044. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  4045. {
  4046. static std::string Get(cmGeneratorTarget* target,
  4047. cmGeneratorExpressionContext* context,
  4048. GeneratorExpressionContent const*)
  4049. {
  4050. return target->GetFileSuffix(context->Config);
  4051. }
  4052. };
  4053. template <>
  4054. struct TargetFileArtifactResultGetter<ArtifactImportFileSuffixTag>
  4055. {
  4056. static std::string Get(cmGeneratorTarget* target,
  4057. cmGeneratorExpressionContext* context,
  4058. GeneratorExpressionContent const*)
  4059. {
  4060. if (target->HasImportLibrary(context->Config)) {
  4061. return target->GetFileSuffix(context->Config,
  4062. cmStateEnums::ImportLibraryArtifact);
  4063. }
  4064. return std::string{};
  4065. }
  4066. };
  4067. template <>
  4068. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  4069. {
  4070. static std::string Get(cmGeneratorTarget* target,
  4071. cmGeneratorExpressionContext* context,
  4072. GeneratorExpressionContent const* content)
  4073. {
  4074. if (!target->IsLinkable()) {
  4075. ::reportError(
  4076. context, content->GetOriginalExpression(),
  4077. "TARGET_LINKER_FILE_SUFFIX is allowed only for libraries and "
  4078. "executables with ENABLE_EXPORTS.");
  4079. return std::string();
  4080. }
  4081. cmStateEnums::ArtifactType artifact =
  4082. target->HasImportLibrary(context->Config)
  4083. ? cmStateEnums::ImportLibraryArtifact
  4084. : cmStateEnums::RuntimeBinaryArtifact;
  4085. return target->GetFileSuffix(context->Config, artifact);
  4086. }
  4087. };
  4088. template <>
  4089. struct TargetFileArtifactResultGetter<ArtifactLinkerLibraryFileSuffixTag>
  4090. {
  4091. static std::string Get(cmGeneratorTarget* target,
  4092. cmGeneratorExpressionContext* context,
  4093. GeneratorExpressionContent const* content)
  4094. {
  4095. if (!target->IsLinkable() ||
  4096. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4097. ::reportError(context, content->GetOriginalExpression(),
  4098. "TARGET_LINKER_LIBRARY_FILE_SUFFIX is allowed only for "
  4099. "libraries with ENABLE_EXPORTS.");
  4100. return std::string();
  4101. }
  4102. if (!target->IsDLLPlatform() ||
  4103. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4104. return target->GetFileSuffix(context->Config,
  4105. cmStateEnums::RuntimeBinaryArtifact);
  4106. }
  4107. return std::string{};
  4108. }
  4109. };
  4110. template <>
  4111. struct TargetFileArtifactResultGetter<ArtifactLinkerImportFileSuffixTag>
  4112. {
  4113. static std::string Get(cmGeneratorTarget* target,
  4114. cmGeneratorExpressionContext* context,
  4115. GeneratorExpressionContent const* content)
  4116. {
  4117. if (!target->IsLinkable()) {
  4118. ::reportError(
  4119. context, content->GetOriginalExpression(),
  4120. "TARGET_LINKER_IMPORT_FILE_SUFFIX is allowed only for libraries and "
  4121. "executables with ENABLE_EXPORTS.");
  4122. return std::string();
  4123. }
  4124. if (target->HasImportLibrary(context->Config)) {
  4125. return target->GetFileSuffix(context->Config,
  4126. cmStateEnums::ImportLibraryArtifact);
  4127. }
  4128. return std::string{};
  4129. }
  4130. };
  4131. template <typename ArtifactT>
  4132. struct TargetFileArtifact : public TargetArtifactBase
  4133. {
  4134. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  4135. int NumExpectedParameters() const override { return 1; }
  4136. std::string Evaluate(
  4137. std::vector<std::string> const& parameters,
  4138. cmGeneratorExpressionContext* context,
  4139. GeneratorExpressionContent const* content,
  4140. cmGeneratorExpressionDAGChecker* dagChecker) const override
  4141. {
  4142. cmGeneratorTarget* target =
  4143. this->GetTarget(parameters, context, content, dagChecker);
  4144. if (!target) {
  4145. return std::string();
  4146. }
  4147. std::string result =
  4148. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  4149. if (context->HadError) {
  4150. return std::string();
  4151. }
  4152. return result;
  4153. }
  4154. };
  4155. static TargetFileArtifact<ArtifactFilePrefixTag> const targetFilePrefixNode;
  4156. static TargetFileArtifact<ArtifactImportFilePrefixTag> const
  4157. targetImportFilePrefixNode;
  4158. static TargetFileArtifact<ArtifactLinkerFilePrefixTag> const
  4159. targetLinkerFilePrefixNode;
  4160. static TargetFileArtifact<ArtifactLinkerLibraryFilePrefixTag> const
  4161. targetLinkerLibraryFilePrefixNode;
  4162. static TargetFileArtifact<ArtifactLinkerImportFilePrefixTag> const
  4163. targetLinkerImportFilePrefixNode;
  4164. static TargetFileArtifact<ArtifactFileSuffixTag> const targetFileSuffixNode;
  4165. static TargetFileArtifact<ArtifactImportFileSuffixTag> const
  4166. targetImportFileSuffixNode;
  4167. static TargetFileArtifact<ArtifactLinkerFileSuffixTag> const
  4168. targetLinkerFileSuffixNode;
  4169. static TargetFileArtifact<ArtifactLinkerLibraryFileSuffixTag> const
  4170. targetLinkerLibraryFileSuffixNode;
  4171. static TargetFileArtifact<ArtifactLinkerImportFileSuffixTag> const
  4172. targetLinkerImportFileSuffixNode;
  4173. static const struct ShellPathNode : public cmGeneratorExpressionNode
  4174. {
  4175. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  4176. std::string Evaluate(
  4177. std::vector<std::string> const& parameters,
  4178. cmGeneratorExpressionContext* context,
  4179. GeneratorExpressionContent const* content,
  4180. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  4181. {
  4182. cmList list_in{ parameters.front() };
  4183. if (list_in.empty()) {
  4184. reportError(context, content->GetOriginalExpression(),
  4185. "\"\" is not an absolute path.");
  4186. return std::string();
  4187. }
  4188. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  4189. cmOutputConverter converter(snapshot);
  4190. char const* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  4191. std::vector<std::string> list_out;
  4192. list_out.reserve(list_in.size());
  4193. for (auto const& in : list_in) {
  4194. if (!cmSystemTools::FileIsFullPath(in)) {
  4195. reportError(context, content->GetOriginalExpression(),
  4196. "\"" + in + "\" is not an absolute path.");
  4197. return std::string();
  4198. }
  4199. list_out.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  4200. }
  4201. return cmJoin(list_out, separator);
  4202. }
  4203. } shellPathNode;
  4204. cmGeneratorExpressionNode const* cmGeneratorExpressionNode::GetNode(
  4205. std::string const& identifier)
  4206. {
  4207. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  4208. { "0", &zeroNode },
  4209. { "1", &oneNode },
  4210. { "AND", &andNode },
  4211. { "OR", &orNode },
  4212. { "NOT", &notNode },
  4213. { "C_COMPILER_ID", &cCompilerIdNode },
  4214. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  4215. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  4216. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  4217. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  4218. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  4219. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  4220. { "VERSION_GREATER", &versionGreaterNode },
  4221. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  4222. { "VERSION_LESS", &versionLessNode },
  4223. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  4224. { "VERSION_EQUAL", &versionEqualNode },
  4225. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  4226. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  4227. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  4228. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  4229. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  4230. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  4231. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  4232. { "C_COMPILER_FRONTEND_VARIANT", &cCompilerFrontendVariantNode },
  4233. { "CXX_COMPILER_FRONTEND_VARIANT", &cxxCompilerFrontendVariantNode },
  4234. { "CUDA_COMPILER_FRONTEND_VARIANT", &cudaCompilerFrontendVariantNode },
  4235. { "OBJC_COMPILER_FRONTEND_VARIANT", &objcCompilerFrontendVariantNode },
  4236. { "OBJCXX_COMPILER_FRONTEND_VARIANT", &objcxxCompilerFrontendVariantNode },
  4237. { "Fortran_COMPILER_FRONTEND_VARIANT",
  4238. &fortranCompilerFrontendVariantNode },
  4239. { "HIP_COMPILER_FRONTEND_VARIANT", &hipCompilerFrontendVariantNode },
  4240. { "PLATFORM_ID", &platformIdNode },
  4241. { "COMPILE_FEATURES", &compileFeaturesNode },
  4242. { "CONFIGURATION", &configurationNode },
  4243. { "CONFIG", &configurationTestNode },
  4244. { "TARGET_FILE", &targetNodeGroup.File },
  4245. { "TARGET_IMPORT_FILE", &targetImportNodeGroup.File },
  4246. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  4247. { "TARGET_LINKER_LIBRARY_FILE", &targetLinkerLibraryNodeGroup.File },
  4248. { "TARGET_LINKER_IMPORT_FILE", &targetLinkerImportNodeGroup.File },
  4249. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  4250. { "TARGET_SONAME_IMPORT_FILE", &targetSoNameImportNodeGroup.File },
  4251. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  4252. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  4253. { "TARGET_IMPORT_FILE_BASE_NAME", &targetImportFileBaseNameNode },
  4254. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  4255. { "TARGET_LINKER_LIBRARY_FILE_BASE_NAME",
  4256. &targetLinkerLibraryFileBaseNameNode },
  4257. { "TARGET_LINKER_IMPORT_FILE_BASE_NAME",
  4258. &targetLinkerImportFileBaseNameNode },
  4259. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  4260. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  4261. { "TARGET_IMPORT_FILE_PREFIX", &targetImportFilePrefixNode },
  4262. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  4263. { "TARGET_LINKER_LIBRARY_FILE_PREFIX",
  4264. &targetLinkerLibraryFilePrefixNode },
  4265. { "TARGET_LINKER_IMPORT_FILE_PREFIX", &targetLinkerImportFilePrefixNode },
  4266. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  4267. { "TARGET_IMPORT_FILE_SUFFIX", &targetImportFileSuffixNode },
  4268. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  4269. { "TARGET_LINKER_LIBRARY_FILE_SUFFIX",
  4270. &targetLinkerLibraryFileSuffixNode },
  4271. { "TARGET_LINKER_IMPORT_FILE_SUFFIX", &targetLinkerImportFileSuffixNode },
  4272. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  4273. { "TARGET_IMPORT_FILE_NAME", &targetImportNodeGroup.FileName },
  4274. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  4275. { "TARGET_LINKER_LIBRARY_FILE_NAME",
  4276. &targetLinkerLibraryNodeGroup.FileName },
  4277. { "TARGET_LINKER_IMPORT_FILE_NAME",
  4278. &targetLinkerImportNodeGroup.FileName },
  4279. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  4280. { "TARGET_SONAME_IMPORT_FILE_NAME",
  4281. &targetSoNameImportNodeGroup.FileName },
  4282. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  4283. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  4284. { "TARGET_IMPORT_FILE_DIR", &targetImportNodeGroup.FileDir },
  4285. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  4286. { "TARGET_LINKER_LIBRARY_FILE_DIR",
  4287. &targetLinkerLibraryNodeGroup.FileDir },
  4288. { "TARGET_LINKER_IMPORT_FILE_DIR", &targetLinkerImportNodeGroup.FileDir },
  4289. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  4290. { "TARGET_SONAME_IMPORT_FILE_DIR", &targetSoNameImportNodeGroup.FileDir },
  4291. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  4292. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  4293. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  4294. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  4295. { "STREQUAL", &strEqualNode },
  4296. { "EQUAL", &equalNode },
  4297. { "IN_LIST", &inListNode },
  4298. { "FILTER", &filterNode },
  4299. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  4300. { "LIST", &listNode },
  4301. { "LOWER_CASE", &lowerCaseNode },
  4302. { "UPPER_CASE", &upperCaseNode },
  4303. { "PATH", &pathNode },
  4304. { "PATH_EQUAL", &pathEqualNode },
  4305. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  4306. { "BOOL", &boolNode },
  4307. { "IF", &ifNode },
  4308. { "ANGLE-R", &angle_rNode },
  4309. { "COMMA", &commaNode },
  4310. { "SEMICOLON", &semicolonNode },
  4311. { "QUOTE", &quoteNode },
  4312. { "TARGET_PROPERTY", &targetPropertyNode },
  4313. { "TARGET_NAME", &targetNameNode },
  4314. { "TARGET_OBJECTS", &targetObjectsNode },
  4315. { "TARGET_POLICY", &targetPolicyNode },
  4316. { "TARGET_EXISTS", &targetExistsNode },
  4317. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  4318. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  4319. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  4320. { "TARGET_RUNTIME_DLL_DIRS", &targetRuntimeDllDirsNode },
  4321. { "GENEX_EVAL", &genexEvalNode },
  4322. { "BUILD_INTERFACE", &buildInterfaceNode },
  4323. { "INSTALL_INTERFACE", &installInterfaceNode },
  4324. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  4325. { "INSTALL_PREFIX", &installPrefixNode },
  4326. { "JOIN", &joinNode },
  4327. { "COMPILE_ONLY", &compileOnlyNode },
  4328. { "LINK_ONLY", &linkOnlyNode },
  4329. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  4330. { "COMPILE_LANGUAGE", &languageNode },
  4331. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  4332. { "LINK_LANGUAGE", &linkLanguageNode },
  4333. { "C_COMPILER_LINKER_ID", &cCompilerLinkerIdNode },
  4334. { "CXX_COMPILER_LINKER_ID", &cxxCompilerLinkerIdNode },
  4335. { "OBJC_COMPILER_LINKER_ID", &objcCompilerLinkerIdNode },
  4336. { "OBJCXX_COMPILER_LINKER_ID", &objcxxCompilerLinkerIdNode },
  4337. { "CUDA_COMPILER_LINKER_ID", &cudaCompilerLinkerIdNode },
  4338. { "Fortran_COMPILER_LINKER_ID", &fortranCompilerLinkerIdNode },
  4339. { "HIP_COMPILER_LINKER_ID", &hipCompilerLinkerIdNode },
  4340. { "C_COMPILER_LINKER_FRONTEND_VARIANT",
  4341. &cCompilerLinkerFrontendVariantNode },
  4342. { "CXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4343. &cxxCompilerLinkerFrontendVariantNode },
  4344. { "CUDA_COMPILER_LINKER_FRONTEND_VARIANT",
  4345. &cudaCompilerLinkerFrontendVariantNode },
  4346. { "OBJC_COMPILER_LINKER_FRONTEND_VARIANT",
  4347. &objcCompilerLinkerFrontendVariantNode },
  4348. { "OBJCXX_COMPILER_LINKER_FRONTEND_VARIANT",
  4349. &objcxxCompilerLinkerFrontendVariantNode },
  4350. { "Fortran_COMPILER_LINKER_FRONTEND_VARIANT",
  4351. &fortranCompilerLinkerFrontendVariantNode },
  4352. { "HIP_COMPILER_LINKER_FRONTEND_VARIANT",
  4353. &hipCompilerLinkerFrontendVariantNode },
  4354. { "LINK_LIBRARY", &linkLibraryNode },
  4355. { "LINK_GROUP", &linkGroupNode },
  4356. { "HOST_LINK", &hostLinkNode },
  4357. { "DEVICE_LINK", &deviceLinkNode },
  4358. { "SHELL_PATH", &shellPathNode }
  4359. };
  4360. {
  4361. auto itr = nodeMap.find(identifier);
  4362. if (itr != nodeMap.end()) {
  4363. return itr->second;
  4364. }
  4365. }
  4366. return nullptr;
  4367. }
  4368. void reportError(cmGeneratorExpressionContext* context,
  4369. std::string const& expr, std::string const& result)
  4370. {
  4371. context->HadError = true;
  4372. if (context->Quiet) {
  4373. return;
  4374. }
  4375. std::ostringstream e;
  4376. /* clang-format off */
  4377. e << "Error evaluating generator expression:\n"
  4378. << " " << expr << "\n"
  4379. << result;
  4380. /* clang-format on */
  4381. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  4382. e.str(), context->Backtrace);
  4383. }