cmGeneratorExpressionNode.cxx 118 KB

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