cmGeneratorExpressionNode.cxx 138 KB

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