cmGeneratorExpressionNode.cxx 165 KB

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