cmGeneratorExpressionNode.cxx 164 KB

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