cmGeneratorExpressionNode.cxx 166 KB

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