cmGeneratorExpressionNode.cxx 166 KB

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