cmGeneratorExpressionNode.cxx 166 KB

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