cmGeneratorExpressionNode.cxx 169 KB

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