cmGeneratorExpressionNode.cxx 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324
  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 MakeCIdentifierNode : public cmGeneratorExpressionNode
  936. {
  937. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  938. bool AcceptsArbitraryContentParameter() const override { return true; }
  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 cmSystemTools::MakeCidentifier(parameters.front());
  946. }
  947. } makeCIdentifierNode;
  948. template <char C>
  949. struct CharacterNode : public cmGeneratorExpressionNode
  950. {
  951. CharacterNode() {} // NOLINT(modernize-use-equals-default)
  952. int NumExpectedParameters() const override { return 0; }
  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 { C };
  960. }
  961. };
  962. static const CharacterNode<'>'> angle_rNode;
  963. static const CharacterNode<','> commaNode;
  964. static const CharacterNode<';'> semicolonNode;
  965. struct CompilerIdNode : public cmGeneratorExpressionNode
  966. {
  967. CompilerIdNode(const char* compilerLang)
  968. : CompilerLanguage(compilerLang)
  969. {
  970. }
  971. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  972. std::string Evaluate(
  973. const std::vector<std::string>& parameters,
  974. cmGeneratorExpressionContext* context,
  975. const GeneratorExpressionContent* content,
  976. cmGeneratorExpressionDAGChecker* dagChecker) const override
  977. {
  978. if (!context->HeadTarget) {
  979. std::ostringstream e;
  980. e << "$<" << this->CompilerLanguage
  981. << "_COMPILER_ID> may only be used with binary targets. It may "
  982. "not be used with add_custom_command or add_custom_target.";
  983. reportError(context, content->GetOriginalExpression(), e.str());
  984. return {};
  985. }
  986. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  987. this->CompilerLanguage);
  988. }
  989. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  990. cmGeneratorExpressionContext* context,
  991. const GeneratorExpressionContent* content,
  992. cmGeneratorExpressionDAGChecker* /*unused*/,
  993. const std::string& lang) const
  994. {
  995. std::string const& compilerId =
  996. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  997. "_COMPILER_ID");
  998. if (parameters.empty()) {
  999. return compilerId;
  1000. }
  1001. if (compilerId.empty()) {
  1002. return parameters.front().empty() ? "1" : "0";
  1003. }
  1004. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  1005. for (auto const& param : parameters) {
  1006. if (!compilerIdValidator.find(param)) {
  1007. reportError(context, content->GetOriginalExpression(),
  1008. "Expression syntax not recognized.");
  1009. return std::string();
  1010. }
  1011. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  1012. return "1";
  1013. }
  1014. if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
  1015. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  1016. case cmPolicies::WARN: {
  1017. context->LG->GetCMakeInstance()->IssueMessage(
  1018. MessageType::AUTHOR_WARNING,
  1019. cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
  1020. context->Backtrace);
  1021. CM_FALLTHROUGH;
  1022. }
  1023. case cmPolicies::OLD:
  1024. return "1";
  1025. case cmPolicies::NEW:
  1026. case cmPolicies::REQUIRED_ALWAYS:
  1027. case cmPolicies::REQUIRED_IF_USED:
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. return "0";
  1033. }
  1034. const char* const CompilerLanguage;
  1035. };
  1036. static const CompilerIdNode cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  1037. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  1038. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  1039. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  1040. struct CompilerVersionNode : public cmGeneratorExpressionNode
  1041. {
  1042. CompilerVersionNode(const char* compilerLang)
  1043. : CompilerLanguage(compilerLang)
  1044. {
  1045. }
  1046. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  1047. std::string Evaluate(
  1048. const std::vector<std::string>& parameters,
  1049. cmGeneratorExpressionContext* context,
  1050. const GeneratorExpressionContent* content,
  1051. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1052. {
  1053. if (!context->HeadTarget) {
  1054. std::ostringstream e;
  1055. e << "$<" << this->CompilerLanguage
  1056. << "_COMPILER_VERSION> may only be used with binary targets. It "
  1057. "may not be used with add_custom_command or add_custom_target.";
  1058. reportError(context, content->GetOriginalExpression(), e.str());
  1059. return {};
  1060. }
  1061. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  1062. this->CompilerLanguage);
  1063. }
  1064. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  1065. cmGeneratorExpressionContext* context,
  1066. const GeneratorExpressionContent* content,
  1067. cmGeneratorExpressionDAGChecker* /*unused*/,
  1068. const std::string& lang) const
  1069. {
  1070. std::string const& compilerVersion =
  1071. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1072. "_COMPILER_VERSION");
  1073. if (parameters.empty()) {
  1074. return compilerVersion;
  1075. }
  1076. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  1077. if (!compilerIdValidator.find(parameters.front())) {
  1078. reportError(context, content->GetOriginalExpression(),
  1079. "Expression syntax not recognized.");
  1080. return {};
  1081. }
  1082. if (compilerVersion.empty()) {
  1083. return parameters.front().empty() ? "1" : "0";
  1084. }
  1085. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  1086. parameters.front(), compilerVersion)
  1087. ? "1"
  1088. : "0";
  1089. }
  1090. const char* const CompilerLanguage;
  1091. };
  1092. static const CompilerVersionNode cCompilerVersionNode("C"),
  1093. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  1094. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  1095. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  1096. hipCompilerVersionNode("HIP");
  1097. struct PlatformIdNode : public cmGeneratorExpressionNode
  1098. {
  1099. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  1100. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1101. std::string Evaluate(
  1102. const std::vector<std::string>& parameters,
  1103. cmGeneratorExpressionContext* context,
  1104. const GeneratorExpressionContent* /*content*/,
  1105. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1106. {
  1107. std::string const& platformId =
  1108. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  1109. if (parameters.empty()) {
  1110. return platformId;
  1111. }
  1112. if (platformId.empty()) {
  1113. return parameters.front().empty() ? "1" : "0";
  1114. }
  1115. for (auto const& param : parameters) {
  1116. if (param == platformId) {
  1117. return "1";
  1118. }
  1119. }
  1120. return "0";
  1121. }
  1122. };
  1123. static struct PlatformIdNode platformIdNode;
  1124. template <cmSystemTools::CompareOp Op>
  1125. struct VersionNode : public cmGeneratorExpressionNode
  1126. {
  1127. VersionNode() {} // NOLINT(modernize-use-equals-default)
  1128. int NumExpectedParameters() const override { return 2; }
  1129. std::string Evaluate(
  1130. const std::vector<std::string>& parameters,
  1131. cmGeneratorExpressionContext* /*context*/,
  1132. const GeneratorExpressionContent* /*content*/,
  1133. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1134. {
  1135. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  1136. ? "1"
  1137. : "0";
  1138. }
  1139. };
  1140. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  1141. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  1142. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  1143. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  1144. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  1145. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  1146. {
  1147. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  1148. std::string Evaluate(
  1149. const std::vector<std::string>& parameters,
  1150. cmGeneratorExpressionContext* context,
  1151. const GeneratorExpressionContent* content,
  1152. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1153. {
  1154. if (!dagChecker) {
  1155. reportError(context, content->GetOriginalExpression(),
  1156. "$<LINK_ONLY:...> may only be used for linking");
  1157. return std::string();
  1158. }
  1159. if (!dagChecker->GetTransitivePropertiesOnly()) {
  1160. return parameters.front();
  1161. }
  1162. return std::string();
  1163. }
  1164. } linkOnlyNode;
  1165. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  1166. {
  1167. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  1168. int NumExpectedParameters() const override { return 0; }
  1169. std::string Evaluate(
  1170. const std::vector<std::string>& /*parameters*/,
  1171. cmGeneratorExpressionContext* context,
  1172. const GeneratorExpressionContent* /*content*/,
  1173. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1174. {
  1175. context->HadContextSensitiveCondition = true;
  1176. return context->Config;
  1177. }
  1178. } configurationNode;
  1179. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  1180. {
  1181. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  1182. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  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. if (parameters.empty()) {
  1190. return configurationNode.Evaluate(parameters, context, content, nullptr);
  1191. }
  1192. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  1193. if (!configValidator.find(parameters.front())) {
  1194. reportError(context, content->GetOriginalExpression(),
  1195. "Expression syntax not recognized.");
  1196. return std::string();
  1197. }
  1198. context->HadContextSensitiveCondition = true;
  1199. for (auto const& param : parameters) {
  1200. if (context->Config.empty()) {
  1201. if (param.empty()) {
  1202. return "1";
  1203. }
  1204. } else if (cmsysString_strcasecmp(param.c_str(),
  1205. context->Config.c_str()) == 0) {
  1206. return "1";
  1207. }
  1208. }
  1209. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  1210. cmValue loc = nullptr;
  1211. cmValue imp = nullptr;
  1212. std::string suffix;
  1213. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  1214. imp, suffix)) {
  1215. // This imported target has an appropriate location
  1216. // for this (possibly mapped) config.
  1217. // Check if there is a proper config mapping for the tested config.
  1218. std::vector<std::string> mappedConfigs;
  1219. std::string mapProp = cmStrCat(
  1220. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  1221. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  1222. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  1223. for (auto const& param : parameters) {
  1224. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  1225. return "1";
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. return "0";
  1232. }
  1233. } configurationTestNode;
  1234. static const struct JoinNode : public cmGeneratorExpressionNode
  1235. {
  1236. JoinNode() {} // NOLINT(modernize-use-equals-default)
  1237. int NumExpectedParameters() const override { return 2; }
  1238. bool AcceptsArbitraryContentParameter() const override { return true; }
  1239. std::string Evaluate(
  1240. const std::vector<std::string>& parameters,
  1241. cmGeneratorExpressionContext* /*context*/,
  1242. const GeneratorExpressionContent* /*content*/,
  1243. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1244. {
  1245. std::vector<std::string> list = cmExpandedList(parameters.front());
  1246. return cmJoin(list, parameters[1]);
  1247. }
  1248. } joinNode;
  1249. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  1250. {
  1251. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1252. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  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. if (context->Language.empty() &&
  1260. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  1261. reportError(
  1262. context, content->GetOriginalExpression(),
  1263. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  1264. "directories, compile definitions, compile options, and to evaluate "
  1265. "components of the file(GENERATE) command.");
  1266. return std::string();
  1267. }
  1268. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1269. std::string genName = gg->GetName();
  1270. if (genName.find("Makefiles") == std::string::npos &&
  1271. genName.find("Ninja") == std::string::npos &&
  1272. genName.find("Visual Studio") == std::string::npos &&
  1273. genName.find("Xcode") == std::string::npos &&
  1274. genName.find("Watcom WMake") == std::string::npos) {
  1275. reportError(context, content->GetOriginalExpression(),
  1276. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  1277. return std::string();
  1278. }
  1279. if (parameters.empty()) {
  1280. return context->Language;
  1281. }
  1282. for (auto const& param : parameters) {
  1283. if (context->Language == param) {
  1284. return "1";
  1285. }
  1286. }
  1287. return "0";
  1288. }
  1289. } languageNode;
  1290. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  1291. {
  1292. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1293. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1294. std::string Evaluate(
  1295. const std::vector<std::string>& parameters,
  1296. cmGeneratorExpressionContext* context,
  1297. const GeneratorExpressionContent* content,
  1298. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1299. {
  1300. if (!context->HeadTarget ||
  1301. (context->Language.empty() &&
  1302. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  1303. // reportError(context, content->GetOriginalExpression(), "");
  1304. reportError(
  1305. context, content->GetOriginalExpression(),
  1306. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  1307. "to specify include directories, compile definitions, and compile "
  1308. "options. It may not be used with the add_custom_command, "
  1309. "add_custom_target, or file(GENERATE) commands.");
  1310. return std::string();
  1311. }
  1312. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1313. std::string genName = gg->GetName();
  1314. if (genName.find("Makefiles") == std::string::npos &&
  1315. genName.find("Ninja") == std::string::npos &&
  1316. genName.find("Visual Studio") == std::string::npos &&
  1317. genName.find("Xcode") == std::string::npos &&
  1318. genName.find("Watcom WMake") == std::string::npos) {
  1319. reportError(
  1320. context, content->GetOriginalExpression(),
  1321. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  1322. return std::string();
  1323. }
  1324. const std::string& lang = context->Language;
  1325. if (lang == parameters.front()) {
  1326. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1327. parameters.cend());
  1328. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  1329. idParameter, context, content, dagChecker, lang);
  1330. }
  1331. return "0";
  1332. }
  1333. } languageAndIdNode;
  1334. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  1335. {
  1336. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1337. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1338. std::string Evaluate(
  1339. const std::vector<std::string>& parameters,
  1340. cmGeneratorExpressionContext* context,
  1341. const GeneratorExpressionContent* content,
  1342. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1343. {
  1344. if (!context->HeadTarget || !dagChecker ||
  1345. !(dagChecker->EvaluatingLinkExpression() ||
  1346. dagChecker->EvaluatingLinkLibraries())) {
  1347. reportError(context, content->GetOriginalExpression(),
  1348. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  1349. "to specify link libraries, link directories, link options "
  1350. "and link depends.");
  1351. return std::string();
  1352. }
  1353. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  1354. reportError(
  1355. context, content->GetOriginalExpression(),
  1356. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  1357. return std::string();
  1358. }
  1359. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1360. std::string genName = gg->GetName();
  1361. if (genName.find("Makefiles") == std::string::npos &&
  1362. genName.find("Ninja") == std::string::npos &&
  1363. genName.find("Visual Studio") == std::string::npos &&
  1364. genName.find("Xcode") == std::string::npos &&
  1365. genName.find("Watcom WMake") == std::string::npos) {
  1366. reportError(context, content->GetOriginalExpression(),
  1367. "$<LINK_LANGUAGE:...> not supported for this generator.");
  1368. return std::string();
  1369. }
  1370. if (dagChecker->EvaluatingLinkLibraries()) {
  1371. context->HadHeadSensitiveCondition = true;
  1372. context->HadLinkLanguageSensitiveCondition = true;
  1373. }
  1374. if (parameters.empty()) {
  1375. return context->Language;
  1376. }
  1377. for (auto const& param : parameters) {
  1378. if (context->Language == param) {
  1379. return "1";
  1380. }
  1381. }
  1382. return "0";
  1383. }
  1384. } linkLanguageNode;
  1385. namespace {
  1386. struct LinkerId
  1387. {
  1388. static std::string Evaluate(const std::vector<std::string>& parameters,
  1389. cmGeneratorExpressionContext* context,
  1390. const GeneratorExpressionContent* content,
  1391. const std::string& lang)
  1392. {
  1393. std::string const& linkerId =
  1394. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1395. "_COMPILER_ID");
  1396. if (parameters.empty()) {
  1397. return linkerId;
  1398. }
  1399. if (linkerId.empty()) {
  1400. return parameters.front().empty() ? "1" : "0";
  1401. }
  1402. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  1403. for (auto const& param : parameters) {
  1404. if (!linkerIdValidator.find(param)) {
  1405. reportError(context, content->GetOriginalExpression(),
  1406. "Expression syntax not recognized.");
  1407. return std::string();
  1408. }
  1409. if (param == linkerId) {
  1410. return "1";
  1411. }
  1412. }
  1413. return "0";
  1414. }
  1415. };
  1416. }
  1417. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  1418. {
  1419. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1420. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1421. std::string Evaluate(
  1422. const std::vector<std::string>& parameters,
  1423. cmGeneratorExpressionContext* context,
  1424. const GeneratorExpressionContent* content,
  1425. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1426. {
  1427. if (!context->HeadTarget || !dagChecker ||
  1428. !(dagChecker->EvaluatingLinkExpression() ||
  1429. dagChecker->EvaluatingLinkLibraries())) {
  1430. reportError(
  1431. context, content->GetOriginalExpression(),
  1432. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1433. "to specify link libraries, link directories, link options, and link "
  1434. "depends.");
  1435. return std::string();
  1436. }
  1437. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1438. std::string genName = gg->GetName();
  1439. if (genName.find("Makefiles") == std::string::npos &&
  1440. genName.find("Ninja") == std::string::npos &&
  1441. genName.find("Visual Studio") == std::string::npos &&
  1442. genName.find("Xcode") == std::string::npos &&
  1443. genName.find("Watcom WMake") == std::string::npos) {
  1444. reportError(
  1445. context, content->GetOriginalExpression(),
  1446. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1447. return std::string();
  1448. }
  1449. if (dagChecker->EvaluatingLinkLibraries()) {
  1450. context->HadHeadSensitiveCondition = true;
  1451. context->HadLinkLanguageSensitiveCondition = true;
  1452. }
  1453. const std::string& lang = context->Language;
  1454. if (lang == parameters.front()) {
  1455. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1456. parameters.cend());
  1457. return LinkerId::Evaluate(idParameter, context, content, lang);
  1458. }
  1459. return "0";
  1460. }
  1461. } linkLanguageAndIdNode;
  1462. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  1463. {
  1464. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  1465. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1466. std::string Evaluate(
  1467. const std::vector<std::string>& parameters,
  1468. cmGeneratorExpressionContext* context,
  1469. const GeneratorExpressionContent* content,
  1470. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1471. {
  1472. if (!context->HeadTarget || !dagChecker ||
  1473. !dagChecker->EvaluatingLinkLibraries()) {
  1474. reportError(context, content->GetOriginalExpression(),
  1475. "$<LINK_LIBRARY:...> may only be used with binary targets "
  1476. "to specify link libraries.");
  1477. return std::string();
  1478. }
  1479. std::vector<std::string> list;
  1480. cmExpandLists(parameters.begin(), parameters.end(), list);
  1481. if (list.empty()) {
  1482. reportError(
  1483. context, content->GetOriginalExpression(),
  1484. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  1485. return std::string();
  1486. }
  1487. if (list.size() == 1) {
  1488. // no libraries specified, ignore this genex
  1489. return std::string();
  1490. }
  1491. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1492. auto const& feature = list.front();
  1493. if (!featureNameValidator.find(feature)) {
  1494. reportError(context, content->GetOriginalExpression(),
  1495. cmStrCat("The feature name '", feature,
  1496. "' contains invalid characters."));
  1497. return std::string();
  1498. }
  1499. const auto LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  1500. const auto LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  1501. // filter out $<LINK_LIBRARY:..> tags with same feature
  1502. // and raise an error for any different feature
  1503. cm::erase_if(list, [&](const std::string& item) -> bool {
  1504. return item == LL_BEGIN || item == LL_END;
  1505. });
  1506. auto it =
  1507. std::find_if(list.cbegin() + 1, list.cend(),
  1508. [&feature](const std::string& item) -> bool {
  1509. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  1510. item.substr(14, item.find('>', 14) - 14) != feature;
  1511. });
  1512. if (it != list.cend()) {
  1513. reportError(
  1514. context, content->GetOriginalExpression(),
  1515. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  1516. return std::string();
  1517. }
  1518. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  1519. it = std::find_if(list.cbegin() + 1, list.cend(),
  1520. [](const std::string& item) -> bool {
  1521. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  1522. });
  1523. if (it != list.cend()) {
  1524. reportError(context, content->GetOriginalExpression(),
  1525. "$<LINK_GROUP:...> cannot be nested inside a "
  1526. "$<LINK_LIBRARY:...> expression.");
  1527. return std::string();
  1528. }
  1529. list.front() = LL_BEGIN;
  1530. list.push_back(LL_END);
  1531. return cmJoin(list, ";"_s);
  1532. }
  1533. } linkLibraryNode;
  1534. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  1535. {
  1536. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  1537. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1538. std::string Evaluate(
  1539. const std::vector<std::string>& parameters,
  1540. cmGeneratorExpressionContext* context,
  1541. const GeneratorExpressionContent* content,
  1542. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1543. {
  1544. if (!context->HeadTarget || !dagChecker ||
  1545. !dagChecker->EvaluatingLinkLibraries()) {
  1546. reportError(context, content->GetOriginalExpression(),
  1547. "$<LINK_GROUP:...> may only be used with binary targets "
  1548. "to specify group of link libraries.");
  1549. return std::string();
  1550. }
  1551. std::vector<std::string> list;
  1552. cmExpandLists(parameters.begin(), parameters.end(), list);
  1553. if (list.empty()) {
  1554. reportError(
  1555. context, content->GetOriginalExpression(),
  1556. "$<LINK_GROUP:...> expects a feature name as first argument.");
  1557. return std::string();
  1558. }
  1559. // $<LINK_GROUP:..> cannot be nested
  1560. if (std::find_if(list.cbegin(), list.cend(),
  1561. [](const std::string& item) -> bool {
  1562. return cmHasPrefix(item, "<LINK_GROUP"_s);
  1563. }) != list.cend()) {
  1564. reportError(context, content->GetOriginalExpression(),
  1565. "$<LINK_GROUP:...> cannot be nested.");
  1566. return std::string();
  1567. }
  1568. if (list.size() == 1) {
  1569. // no libraries specified, ignore this genex
  1570. return std::string();
  1571. }
  1572. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1573. auto const& feature = list.front();
  1574. if (!featureNameValidator.find(feature)) {
  1575. reportError(context, content->GetOriginalExpression(),
  1576. cmStrCat("The feature name '", feature,
  1577. "' contains invalid characters."));
  1578. return std::string();
  1579. }
  1580. const auto LG_BEGIN = cmStrCat(
  1581. "<LINK_GROUP:", feature, ':',
  1582. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  1583. "|"_s),
  1584. '>');
  1585. const auto LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  1586. list.front() = LG_BEGIN;
  1587. list.push_back(LG_END);
  1588. return cmJoin(list, ";"_s);
  1589. }
  1590. } linkGroupNode;
  1591. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1592. {
  1593. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1594. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1595. std::string Evaluate(
  1596. const std::vector<std::string>& parameters,
  1597. cmGeneratorExpressionContext* context,
  1598. const GeneratorExpressionContent* content,
  1599. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1600. {
  1601. if (!context->HeadTarget || !dagChecker ||
  1602. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1603. reportError(context, content->GetOriginalExpression(),
  1604. "$<HOST_LINK:...> may only be used with binary targets "
  1605. "to specify link options.");
  1606. return std::string();
  1607. }
  1608. return context->HeadTarget->IsDeviceLink() ? std::string()
  1609. : cmJoin(parameters, ";");
  1610. }
  1611. } hostLinkNode;
  1612. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1613. {
  1614. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1615. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1616. std::string Evaluate(
  1617. const std::vector<std::string>& parameters,
  1618. cmGeneratorExpressionContext* context,
  1619. const GeneratorExpressionContent* content,
  1620. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1621. {
  1622. if (!context->HeadTarget || !dagChecker ||
  1623. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1624. reportError(context, content->GetOriginalExpression(),
  1625. "$<DEVICE_LINK:...> may only be used with binary targets "
  1626. "to specify link options.");
  1627. return std::string();
  1628. }
  1629. if (context->HeadTarget->IsDeviceLink()) {
  1630. std::vector<std::string> list;
  1631. cmExpandLists(parameters.begin(), parameters.end(), list);
  1632. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1633. const auto DL_END = "</DEVICE_LINK>"_s;
  1634. cm::erase_if(list, [&](const std::string& item) {
  1635. return item == DL_BEGIN || item == DL_END;
  1636. });
  1637. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1638. list.push_back(static_cast<std::string>(DL_END));
  1639. return cmJoin(list, ";");
  1640. }
  1641. return std::string();
  1642. }
  1643. } deviceLinkNode;
  1644. static std::string getLinkedTargetsContent(
  1645. cmGeneratorTarget const* target, std::string const& prop,
  1646. cmGeneratorExpressionContext* context,
  1647. cmGeneratorExpressionDAGChecker* dagChecker)
  1648. {
  1649. std::string result;
  1650. if (cmLinkImplementationLibraries const* impl =
  1651. target->GetLinkImplementationLibraries(
  1652. context->Config, cmGeneratorTarget::LinkInterfaceFor::Usage)) {
  1653. for (cmLinkImplItem const& lib : impl->Libraries) {
  1654. if (lib.Target) {
  1655. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1656. // caller's property and hand-evaluate it as if it were compiled.
  1657. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1658. cmGeneratorExpressionContext libContext(
  1659. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1660. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1661. context->Language);
  1662. std::string libResult =
  1663. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1664. if (!libResult.empty()) {
  1665. if (result.empty()) {
  1666. result = std::move(libResult);
  1667. } else {
  1668. result.reserve(result.size() + 1 + libResult.size());
  1669. result += ";";
  1670. result += libResult;
  1671. }
  1672. }
  1673. }
  1674. }
  1675. }
  1676. return result;
  1677. }
  1678. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1679. {
  1680. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1681. // This node handles errors on parameter count itself.
  1682. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1683. static const char* GetErrorText(std::string const& targetName,
  1684. std::string const& propertyName)
  1685. {
  1686. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1687. if (targetName.empty() && propertyName.empty()) {
  1688. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1689. "target name and property name.";
  1690. }
  1691. if (targetName.empty()) {
  1692. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1693. "target name.";
  1694. }
  1695. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1696. if (!propertyNameValidator.find(propertyName)) {
  1697. return "Target name and property name not supported.";
  1698. }
  1699. return "Target name not supported.";
  1700. }
  1701. return nullptr;
  1702. }
  1703. std::string Evaluate(
  1704. const std::vector<std::string>& parameters,
  1705. cmGeneratorExpressionContext* context,
  1706. const GeneratorExpressionContent* content,
  1707. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1708. {
  1709. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1710. cmGeneratorTarget const* target = nullptr;
  1711. std::string targetName;
  1712. std::string propertyName;
  1713. if (parameters.size() == 2) {
  1714. targetName = parameters[0];
  1715. propertyName = parameters[1];
  1716. if (const char* e = GetErrorText(targetName, propertyName)) {
  1717. reportError(context, content->GetOriginalExpression(), e);
  1718. return std::string();
  1719. }
  1720. if (propertyName == "ALIASED_TARGET"_s) {
  1721. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1722. if (cmGeneratorTarget* tgt =
  1723. context->LG->FindGeneratorTargetToUse(targetName)) {
  1724. return tgt->GetName();
  1725. }
  1726. }
  1727. return std::string();
  1728. }
  1729. if (propertyName == "ALIAS_GLOBAL"_s) {
  1730. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1731. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1732. ? "TRUE"
  1733. : "FALSE";
  1734. }
  1735. return std::string();
  1736. }
  1737. target = context->LG->FindGeneratorTargetToUse(targetName);
  1738. if (!target) {
  1739. std::ostringstream e;
  1740. e << "Target \"" << targetName << "\" not found.";
  1741. reportError(context, content->GetOriginalExpression(), e.str());
  1742. return std::string();
  1743. }
  1744. context->AllTargets.insert(target);
  1745. } else if (parameters.size() == 1) {
  1746. target = context->HeadTarget;
  1747. propertyName = parameters[0];
  1748. // Keep track of the properties seen while processing.
  1749. // The evaluation of the LINK_LIBRARIES generator expressions
  1750. // will check this to ensure that properties have one consistent
  1751. // value for all evaluations.
  1752. context->SeenTargetProperties.insert(propertyName);
  1753. context->HadHeadSensitiveCondition = true;
  1754. if (!target) {
  1755. reportError(
  1756. context, content->GetOriginalExpression(),
  1757. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1758. "It may not be used with add_custom_command or add_custom_target. "
  1759. " "
  1760. "Specify the target to read a property from using the "
  1761. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1762. return std::string();
  1763. }
  1764. } else {
  1765. reportError(
  1766. context, content->GetOriginalExpression(),
  1767. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1768. return std::string();
  1769. }
  1770. if (propertyName == "SOURCES") {
  1771. context->SourceSensitiveTargets.insert(target);
  1772. }
  1773. if (propertyName.empty()) {
  1774. reportError(
  1775. context, content->GetOriginalExpression(),
  1776. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1777. "name.");
  1778. return std::string();
  1779. }
  1780. if (!propertyNameValidator.find(propertyName)) {
  1781. ::reportError(context, content->GetOriginalExpression(),
  1782. "Property name not supported.");
  1783. return std::string();
  1784. }
  1785. assert(target);
  1786. if (propertyName == "LINKER_LANGUAGE") {
  1787. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1788. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1789. dagCheckerParent->EvaluatingSources())) {
  1790. reportError(
  1791. context, content->GetOriginalExpression(),
  1792. "LINKER_LANGUAGE target property can not be used while evaluating "
  1793. "link libraries for a static library");
  1794. return std::string();
  1795. }
  1796. return target->GetLinkerLanguage(context->Config);
  1797. }
  1798. std::string interfacePropertyName;
  1799. bool isInterfaceProperty = false;
  1800. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1801. if (propertyName == #prop) { \
  1802. interfacePropertyName = "INTERFACE_" #prop; \
  1803. } else if (propertyName == "INTERFACE_" #prop) { \
  1804. interfacePropertyName = "INTERFACE_" #prop; \
  1805. isInterfaceProperty = true; \
  1806. } else
  1807. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1808. // Note that the above macro terminates with an else
  1809. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1810. cmPolicies::PolicyStatus polSt =
  1811. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1812. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1813. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1814. }
  1815. }
  1816. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1817. bool evaluatingLinkLibraries = false;
  1818. if (dagCheckerParent) {
  1819. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1820. dagCheckerParent->EvaluatingPICExpression()) {
  1821. // No check required.
  1822. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1823. evaluatingLinkLibraries = true;
  1824. if (!interfacePropertyName.empty()) {
  1825. reportError(
  1826. context, content->GetOriginalExpression(),
  1827. "$<TARGET_PROPERTY:...> expression in link libraries "
  1828. "evaluation depends on target property which is transitive "
  1829. "over the link libraries, creating a recursion.");
  1830. return std::string();
  1831. }
  1832. } else {
  1833. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1834. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1835. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1836. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1837. }
  1838. }
  1839. if (isInterfaceProperty) {
  1840. return cmGeneratorExpression::StripEmptyListElements(
  1841. target->EvaluateInterfaceProperty(propertyName, context,
  1842. dagCheckerParent));
  1843. }
  1844. cmGeneratorExpressionDAGChecker dagChecker(
  1845. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1846. switch (dagChecker.Check()) {
  1847. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1848. dagChecker.ReportError(context, content->GetOriginalExpression());
  1849. return std::string();
  1850. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1851. // No error. We just skip cyclic references.
  1852. return std::string();
  1853. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1854. // We handle transitive properties above. For non-transitive
  1855. // properties we accept repeats anyway.
  1856. case cmGeneratorExpressionDAGChecker::DAG:
  1857. break;
  1858. }
  1859. std::string result;
  1860. bool haveProp = false;
  1861. if (cmValue p = target->GetProperty(propertyName)) {
  1862. result = *p;
  1863. haveProp = true;
  1864. } else if (evaluatingLinkLibraries) {
  1865. return std::string();
  1866. }
  1867. if (!haveProp && !target->IsImported() &&
  1868. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1869. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1870. context->Config)) {
  1871. context->HadContextSensitiveCondition = true;
  1872. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1873. context->Config)
  1874. ? "1"
  1875. : "0";
  1876. }
  1877. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1878. context->Config)) {
  1879. context->HadContextSensitiveCondition = true;
  1880. const char* propContent =
  1881. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1882. context->Config);
  1883. return propContent ? propContent : "";
  1884. }
  1885. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1886. context->Config)) {
  1887. context->HadContextSensitiveCondition = true;
  1888. const char* propContent =
  1889. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1890. context->Config);
  1891. return propContent ? propContent : "";
  1892. }
  1893. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1894. context->Config)) {
  1895. context->HadContextSensitiveCondition = true;
  1896. const char* propContent =
  1897. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1898. context->Config);
  1899. return propContent ? propContent : "";
  1900. }
  1901. }
  1902. if (!target->IsImported() && dagCheckerParent &&
  1903. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1904. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1905. context->Config)) {
  1906. context->HadContextSensitiveCondition = true;
  1907. const char* propContent =
  1908. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1909. context->Config);
  1910. return propContent ? propContent : "";
  1911. }
  1912. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1913. context->Config)) {
  1914. context->HadContextSensitiveCondition = true;
  1915. const char* propContent =
  1916. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1917. context->Config);
  1918. return propContent ? propContent : "";
  1919. }
  1920. }
  1921. if (!interfacePropertyName.empty()) {
  1922. result = cmGeneratorExpression::StripEmptyListElements(
  1923. this->EvaluateDependentExpression(result, context->LG, context, target,
  1924. &dagChecker, target));
  1925. std::string linkedTargetsContent = getLinkedTargetsContent(
  1926. target, interfacePropertyName, context, &dagChecker);
  1927. if (!linkedTargetsContent.empty()) {
  1928. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1929. }
  1930. }
  1931. return result;
  1932. }
  1933. } targetPropertyNode;
  1934. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1935. {
  1936. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1937. bool GeneratesContent() const override { return true; }
  1938. bool AcceptsArbitraryContentParameter() const override { return true; }
  1939. bool RequiresLiteralInput() const override { return true; }
  1940. std::string Evaluate(
  1941. const std::vector<std::string>& parameters,
  1942. cmGeneratorExpressionContext* /*context*/,
  1943. const GeneratorExpressionContent* /*content*/,
  1944. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1945. {
  1946. return parameters.front();
  1947. }
  1948. int NumExpectedParameters() const override { return 1; }
  1949. } targetNameNode;
  1950. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1951. {
  1952. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1953. std::string Evaluate(
  1954. const std::vector<std::string>& parameters,
  1955. cmGeneratorExpressionContext* context,
  1956. const GeneratorExpressionContent* content,
  1957. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1958. {
  1959. std::string const& tgtName = parameters.front();
  1960. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1961. if (!gt) {
  1962. std::ostringstream e;
  1963. e << "Objects of target \"" << tgtName
  1964. << "\" referenced but no such target exists.";
  1965. reportError(context, content->GetOriginalExpression(), e.str());
  1966. return std::string();
  1967. }
  1968. cmStateEnums::TargetType type = gt->GetType();
  1969. if (type != cmStateEnums::EXECUTABLE &&
  1970. type != cmStateEnums::STATIC_LIBRARY &&
  1971. type != cmStateEnums::SHARED_LIBRARY &&
  1972. type != cmStateEnums::MODULE_LIBRARY &&
  1973. type != cmStateEnums::OBJECT_LIBRARY) {
  1974. std::ostringstream e;
  1975. e << "Objects of target \"" << tgtName
  1976. << "\" referenced but is not one of the allowed target types "
  1977. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  1978. reportError(context, content->GetOriginalExpression(), e.str());
  1979. return std::string();
  1980. }
  1981. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1982. {
  1983. std::string reason;
  1984. if (!context->EvaluateForBuildsystem &&
  1985. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  1986. std::ostringstream e;
  1987. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1988. "is only suitable for consumption by CMake (limited"
  1989. << reason
  1990. << "). "
  1991. "It is not suitable for writing out elsewhere.";
  1992. reportError(context, content->GetOriginalExpression(), e.str());
  1993. return std::string();
  1994. }
  1995. }
  1996. std::vector<std::string> objects;
  1997. if (gt->IsImported()) {
  1998. cmValue loc = nullptr;
  1999. cmValue imp = nullptr;
  2000. std::string suffix;
  2001. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  2002. cmExpandList(*loc, objects);
  2003. }
  2004. context->HadContextSensitiveCondition = true;
  2005. } else {
  2006. gt->GetTargetObjectNames(context->Config, objects);
  2007. std::string obj_dir;
  2008. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  2009. // Use object file directory with buildsystem placeholder.
  2010. obj_dir = gt->ObjectDirectory;
  2011. context->HadContextSensitiveCondition =
  2012. gt->HasContextDependentSources();
  2013. } else {
  2014. // Use object file directory with per-config location.
  2015. obj_dir = gt->GetObjectDirectory(context->Config);
  2016. context->HadContextSensitiveCondition = true;
  2017. }
  2018. for (std::string& o : objects) {
  2019. o = cmStrCat(obj_dir, o);
  2020. }
  2021. }
  2022. // Create the cmSourceFile instances in the referencing directory.
  2023. cmMakefile* mf = context->LG->GetMakefile();
  2024. for (std::string const& o : objects) {
  2025. mf->AddTargetObject(tgtName, o);
  2026. }
  2027. return cmJoin(objects, ";");
  2028. }
  2029. } targetObjectsNode;
  2030. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  2031. {
  2032. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  2033. std::string Evaluate(
  2034. const std::vector<std::string>& parameters,
  2035. cmGeneratorExpressionContext* context,
  2036. const GeneratorExpressionContent* content,
  2037. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2038. {
  2039. std::string const& tgtName = parameters.front();
  2040. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2041. if (!gt) {
  2042. std::ostringstream e;
  2043. e << "Objects of target \"" << tgtName
  2044. << "\" referenced but no such target exists.";
  2045. reportError(context, content->GetOriginalExpression(), e.str());
  2046. return std::string();
  2047. }
  2048. cmStateEnums::TargetType type = gt->GetType();
  2049. if (type != cmStateEnums::EXECUTABLE &&
  2050. type != cmStateEnums::SHARED_LIBRARY &&
  2051. type != cmStateEnums::MODULE_LIBRARY) {
  2052. std::ostringstream e;
  2053. e << "Objects of target \"" << tgtName
  2054. << "\" referenced but is not one of the allowed target types "
  2055. << "(EXECUTABLE, SHARED, MODULE).";
  2056. reportError(context, content->GetOriginalExpression(), e.str());
  2057. return std::string();
  2058. }
  2059. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  2060. std::vector<std::string> dllPaths;
  2061. auto const& dlls = cli->GetRuntimeDLLs();
  2062. for (auto const& dll : dlls) {
  2063. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  2064. dllPaths.emplace_back(*loc);
  2065. }
  2066. }
  2067. return cmJoin(dllPaths, ";");
  2068. }
  2069. return "";
  2070. }
  2071. } targetRuntimeDllsNode;
  2072. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  2073. {
  2074. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  2075. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2076. std::string Evaluate(
  2077. const std::vector<std::string>& parameters,
  2078. cmGeneratorExpressionContext* context,
  2079. const GeneratorExpressionContent* content,
  2080. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2081. {
  2082. cmGeneratorTarget const* target = context->HeadTarget;
  2083. if (!target) {
  2084. reportError(
  2085. context, content->GetOriginalExpression(),
  2086. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  2087. "not be used with add_custom_command or add_custom_target.");
  2088. return std::string();
  2089. }
  2090. context->HadHeadSensitiveCondition = true;
  2091. using LangMap = std::map<std::string, std::vector<std::string>>;
  2092. static LangMap availableFeatures;
  2093. LangMap testedFeatures;
  2094. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  2095. for (std::string const& p : parameters) {
  2096. std::string error;
  2097. std::string lang;
  2098. if (!standardResolver.CompileFeatureKnown(
  2099. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  2100. reportError(context, content->GetOriginalExpression(), error);
  2101. return std::string();
  2102. }
  2103. testedFeatures[lang].push_back(p);
  2104. if (availableFeatures.find(lang) == availableFeatures.end()) {
  2105. cmValue featuresKnown =
  2106. standardResolver.CompileFeaturesAvailable(lang, &error);
  2107. if (!featuresKnown) {
  2108. reportError(context, content->GetOriginalExpression(), error);
  2109. return std::string();
  2110. }
  2111. cmExpandList(featuresKnown, availableFeatures[lang]);
  2112. }
  2113. }
  2114. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  2115. for (auto const& lit : testedFeatures) {
  2116. std::vector<std::string> const& langAvailable =
  2117. availableFeatures[lit.first];
  2118. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  2119. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  2120. for (std::string const& it : lit.second) {
  2121. if (!cm::contains(langAvailable, it)) {
  2122. return "0";
  2123. }
  2124. if (standardDefault && standardDefault->empty()) {
  2125. // This compiler has no notion of language standard levels.
  2126. // All features known for the language are always available.
  2127. continue;
  2128. }
  2129. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  2130. context->Config, it)) {
  2131. if (evalLL) {
  2132. cmValue l =
  2133. target->GetLanguageStandard(lit.first, context->Config);
  2134. if (!l) {
  2135. l = standardDefault;
  2136. }
  2137. assert(l);
  2138. context->MaxLanguageStandard[target][lit.first] = *l;
  2139. } else {
  2140. return "0";
  2141. }
  2142. }
  2143. }
  2144. }
  2145. return "1";
  2146. }
  2147. } compileFeaturesNode;
  2148. static const char* targetPolicyWhitelist[] = {
  2149. nullptr
  2150. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  2151. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  2152. #undef TARGET_POLICY_STRING
  2153. };
  2154. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  2155. const char* policy)
  2156. {
  2157. #define RETURN_POLICY(POLICY) \
  2158. if (strcmp(policy, #POLICY) == 0) { \
  2159. return tgt->GetPolicyStatus##POLICY(); \
  2160. }
  2161. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  2162. #undef RETURN_POLICY
  2163. assert(false && "Unreachable code. Not a valid policy");
  2164. return cmPolicies::WARN;
  2165. }
  2166. static cmPolicies::PolicyID policyForString(const char* policy_id)
  2167. {
  2168. #define RETURN_POLICY_ID(POLICY_ID) \
  2169. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  2170. return cmPolicies::POLICY_ID; \
  2171. }
  2172. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  2173. #undef RETURN_POLICY_ID
  2174. assert(false && "Unreachable code. Not a valid policy");
  2175. return cmPolicies::CMP0002;
  2176. }
  2177. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  2178. {
  2179. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  2180. int NumExpectedParameters() const override { return 1; }
  2181. std::string Evaluate(
  2182. const std::vector<std::string>& parameters,
  2183. cmGeneratorExpressionContext* context,
  2184. const GeneratorExpressionContent* content,
  2185. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2186. {
  2187. if (!context->HeadTarget) {
  2188. reportError(
  2189. context, content->GetOriginalExpression(),
  2190. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  2191. "may not be used with add_custom_command or add_custom_target.");
  2192. return std::string();
  2193. }
  2194. context->HadContextSensitiveCondition = true;
  2195. context->HadHeadSensitiveCondition = true;
  2196. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  2197. const char* policy = targetPolicyWhitelist[i];
  2198. if (parameters.front() == policy) {
  2199. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  2200. switch (statusForTarget(context->HeadTarget, policy)) {
  2201. case cmPolicies::WARN:
  2202. lg->IssueMessage(
  2203. MessageType::AUTHOR_WARNING,
  2204. cmPolicies::GetPolicyWarning(policyForString(policy)));
  2205. CM_FALLTHROUGH;
  2206. case cmPolicies::REQUIRED_IF_USED:
  2207. case cmPolicies::REQUIRED_ALWAYS:
  2208. case cmPolicies::OLD:
  2209. return "0";
  2210. case cmPolicies::NEW:
  2211. return "1";
  2212. }
  2213. }
  2214. }
  2215. reportError(
  2216. context, content->GetOriginalExpression(),
  2217. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  2218. "policies. Currently it may be used with the following policies:\n"
  2219. #define STRINGIFY_HELPER(X) #X
  2220. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  2221. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  2222. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  2223. #undef TARGET_POLICY_LIST_ITEM
  2224. );
  2225. return std::string();
  2226. }
  2227. } targetPolicyNode;
  2228. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  2229. {
  2230. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  2231. bool GeneratesContent() const override { return true; }
  2232. int NumExpectedParameters() const override { return 0; }
  2233. std::string Evaluate(
  2234. const std::vector<std::string>& /*parameters*/,
  2235. cmGeneratorExpressionContext* context,
  2236. const GeneratorExpressionContent* content,
  2237. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2238. {
  2239. reportError(context, content->GetOriginalExpression(),
  2240. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  2241. "should never be evaluated.");
  2242. return std::string();
  2243. }
  2244. } installPrefixNode;
  2245. class ArtifactDirTag;
  2246. class ArtifactLinkerTag;
  2247. class ArtifactNameTag;
  2248. class ArtifactPathTag;
  2249. class ArtifactPdbTag;
  2250. class ArtifactSonameTag;
  2251. class ArtifactBundleDirTag;
  2252. class ArtifactBundleDirNameTag;
  2253. class ArtifactBundleContentDirTag;
  2254. template <typename ArtifactT, typename ComponentT>
  2255. struct TargetFilesystemArtifactDependency
  2256. {
  2257. static void AddDependency(cmGeneratorTarget* target,
  2258. cmGeneratorExpressionContext* context)
  2259. {
  2260. context->DependTargets.insert(target);
  2261. context->AllTargets.insert(target);
  2262. }
  2263. };
  2264. struct TargetFilesystemArtifactDependencyCMP0112
  2265. {
  2266. static void AddDependency(cmGeneratorTarget* target,
  2267. cmGeneratorExpressionContext* context)
  2268. {
  2269. context->AllTargets.insert(target);
  2270. cmLocalGenerator* lg = context->LG;
  2271. switch (target->GetPolicyStatusCMP0112()) {
  2272. case cmPolicies::WARN:
  2273. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  2274. "CMAKE_POLICY_WARNING_CMP0112")) {
  2275. std::string err =
  2276. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  2277. "\nDependency being added to target:\n \"",
  2278. target->GetName(), "\"\n");
  2279. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  2280. err, context->Backtrace);
  2281. }
  2282. CM_FALLTHROUGH;
  2283. case cmPolicies::OLD:
  2284. context->DependTargets.insert(target);
  2285. break;
  2286. case cmPolicies::REQUIRED_IF_USED:
  2287. case cmPolicies::REQUIRED_ALWAYS:
  2288. case cmPolicies::NEW:
  2289. break;
  2290. }
  2291. }
  2292. };
  2293. template <typename ArtifactT>
  2294. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  2295. : TargetFilesystemArtifactDependencyCMP0112
  2296. {
  2297. };
  2298. template <typename ArtifactT>
  2299. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  2300. : TargetFilesystemArtifactDependencyCMP0112
  2301. {
  2302. };
  2303. template <>
  2304. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  2305. ArtifactPathTag>
  2306. : TargetFilesystemArtifactDependencyCMP0112
  2307. {
  2308. };
  2309. template <>
  2310. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  2311. ArtifactPathTag>
  2312. : TargetFilesystemArtifactDependencyCMP0112
  2313. {
  2314. };
  2315. template <>
  2316. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  2317. ArtifactPathTag>
  2318. : TargetFilesystemArtifactDependencyCMP0112
  2319. {
  2320. };
  2321. template <typename ArtifactT>
  2322. struct TargetFilesystemArtifactResultCreator
  2323. {
  2324. static std::string Create(cmGeneratorTarget* target,
  2325. cmGeneratorExpressionContext* context,
  2326. const GeneratorExpressionContent* content);
  2327. };
  2328. template <>
  2329. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  2330. {
  2331. static std::string Create(cmGeneratorTarget* target,
  2332. cmGeneratorExpressionContext* context,
  2333. const GeneratorExpressionContent* content)
  2334. {
  2335. // The target soname file (.so.1).
  2336. if (target->IsDLLPlatform()) {
  2337. ::reportError(context, content->GetOriginalExpression(),
  2338. "TARGET_SONAME_FILE is not allowed "
  2339. "for DLL target platforms.");
  2340. return std::string();
  2341. }
  2342. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2343. ::reportError(context, content->GetOriginalExpression(),
  2344. "TARGET_SONAME_FILE is allowed only for "
  2345. "SHARED libraries.");
  2346. return std::string();
  2347. }
  2348. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  2349. target->GetSOName(context->Config));
  2350. return result;
  2351. }
  2352. };
  2353. template <>
  2354. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  2355. {
  2356. static std::string Create(cmGeneratorTarget* target,
  2357. cmGeneratorExpressionContext* context,
  2358. const GeneratorExpressionContent* content)
  2359. {
  2360. if (target->IsImported()) {
  2361. ::reportError(context, content->GetOriginalExpression(),
  2362. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  2363. return std::string();
  2364. }
  2365. std::string language = target->GetLinkerLanguage(context->Config);
  2366. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2367. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2368. ::reportError(context, content->GetOriginalExpression(),
  2369. "TARGET_PDB_FILE is not supported by the target linker.");
  2370. return std::string();
  2371. }
  2372. cmStateEnums::TargetType targetType = target->GetType();
  2373. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2374. targetType != cmStateEnums::MODULE_LIBRARY &&
  2375. targetType != cmStateEnums::EXECUTABLE) {
  2376. ::reportError(context, content->GetOriginalExpression(),
  2377. "TARGET_PDB_FILE is allowed only for "
  2378. "targets with linker created artifacts.");
  2379. return std::string();
  2380. }
  2381. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  2382. '/', target->GetPDBName(context->Config));
  2383. return result;
  2384. }
  2385. };
  2386. template <>
  2387. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  2388. {
  2389. static std::string Create(cmGeneratorTarget* target,
  2390. cmGeneratorExpressionContext* context,
  2391. const GeneratorExpressionContent* content)
  2392. {
  2393. // The file used to link to the target (.so, .lib, .a).
  2394. if (!target->IsLinkable()) {
  2395. ::reportError(context, content->GetOriginalExpression(),
  2396. "TARGET_LINKER_FILE is allowed only for libraries and "
  2397. "executables with ENABLE_EXPORTS.");
  2398. return std::string();
  2399. }
  2400. cmStateEnums::ArtifactType artifact =
  2401. target->HasImportLibrary(context->Config)
  2402. ? cmStateEnums::ImportLibraryArtifact
  2403. : cmStateEnums::RuntimeBinaryArtifact;
  2404. return target->GetFullPath(context->Config, artifact);
  2405. }
  2406. };
  2407. template <>
  2408. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  2409. {
  2410. static std::string Create(cmGeneratorTarget* target,
  2411. cmGeneratorExpressionContext* context,
  2412. const GeneratorExpressionContent* content)
  2413. {
  2414. if (target->IsImported()) {
  2415. ::reportError(context, content->GetOriginalExpression(),
  2416. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  2417. return std::string();
  2418. }
  2419. if (!target->IsBundleOnApple()) {
  2420. ::reportError(context, content->GetOriginalExpression(),
  2421. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  2422. return std::string();
  2423. }
  2424. std::string outpath = target->GetDirectory(context->Config) + '/';
  2425. return target->BuildBundleDirectory(outpath, context->Config,
  2426. cmGeneratorTarget::BundleDirLevel);
  2427. }
  2428. };
  2429. template <>
  2430. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  2431. {
  2432. static std::string Create(cmGeneratorTarget* target,
  2433. cmGeneratorExpressionContext* context,
  2434. const GeneratorExpressionContent* content)
  2435. {
  2436. if (target->IsImported()) {
  2437. ::reportError(
  2438. context, content->GetOriginalExpression(),
  2439. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  2440. return std::string();
  2441. }
  2442. if (!target->IsBundleOnApple()) {
  2443. ::reportError(
  2444. context, content->GetOriginalExpression(),
  2445. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  2446. return std::string();
  2447. }
  2448. return target->GetAppBundleDirectory(context->Config,
  2449. cmGeneratorTarget::BundleDirLevel);
  2450. }
  2451. };
  2452. template <>
  2453. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  2454. {
  2455. static std::string Create(cmGeneratorTarget* target,
  2456. cmGeneratorExpressionContext* context,
  2457. const GeneratorExpressionContent* content)
  2458. {
  2459. if (target->IsImported()) {
  2460. ::reportError(
  2461. context, content->GetOriginalExpression(),
  2462. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  2463. return std::string();
  2464. }
  2465. if (!target->IsBundleOnApple()) {
  2466. ::reportError(
  2467. context, content->GetOriginalExpression(),
  2468. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  2469. return std::string();
  2470. }
  2471. std::string outpath = target->GetDirectory(context->Config) + '/';
  2472. return target->BuildBundleDirectory(outpath, context->Config,
  2473. cmGeneratorTarget::ContentLevel);
  2474. }
  2475. };
  2476. template <>
  2477. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  2478. {
  2479. static std::string Create(cmGeneratorTarget* target,
  2480. cmGeneratorExpressionContext* context,
  2481. const GeneratorExpressionContent* /*unused*/)
  2482. {
  2483. return target->GetFullPath(context->Config,
  2484. cmStateEnums::RuntimeBinaryArtifact, true);
  2485. }
  2486. };
  2487. template <typename ArtifactT>
  2488. struct TargetFilesystemArtifactResultGetter
  2489. {
  2490. static std::string Get(const std::string& result);
  2491. };
  2492. template <>
  2493. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  2494. {
  2495. static std::string Get(const std::string& result)
  2496. {
  2497. return cmSystemTools::GetFilenameName(result);
  2498. }
  2499. };
  2500. template <>
  2501. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  2502. {
  2503. static std::string Get(const std::string& result)
  2504. {
  2505. return cmSystemTools::GetFilenamePath(result);
  2506. }
  2507. };
  2508. template <>
  2509. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  2510. {
  2511. static std::string Get(const std::string& result) { return result; }
  2512. };
  2513. struct TargetArtifactBase : public cmGeneratorExpressionNode
  2514. {
  2515. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  2516. protected:
  2517. cmGeneratorTarget* GetTarget(
  2518. const std::vector<std::string>& parameters,
  2519. cmGeneratorExpressionContext* context,
  2520. const GeneratorExpressionContent* content,
  2521. cmGeneratorExpressionDAGChecker* dagChecker) const
  2522. {
  2523. // Lookup the referenced target.
  2524. std::string const& name = parameters.front();
  2525. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  2526. ::reportError(context, content->GetOriginalExpression(),
  2527. "Expression syntax not recognized.");
  2528. return nullptr;
  2529. }
  2530. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  2531. if (!target) {
  2532. ::reportError(context, content->GetOriginalExpression(),
  2533. "No target \"" + name + "\"");
  2534. return nullptr;
  2535. }
  2536. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  2537. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  2538. ::reportError(context, content->GetOriginalExpression(),
  2539. "Target \"" + name +
  2540. "\" is not an executable or library.");
  2541. return nullptr;
  2542. }
  2543. if (dagChecker &&
  2544. (dagChecker->EvaluatingLinkLibraries(target) ||
  2545. (dagChecker->EvaluatingSources() &&
  2546. target == dagChecker->TopTarget()))) {
  2547. ::reportError(context, content->GetOriginalExpression(),
  2548. "Expressions which require the linker language may not "
  2549. "be used while evaluating link libraries");
  2550. return nullptr;
  2551. }
  2552. return target;
  2553. }
  2554. };
  2555. template <typename ArtifactT, typename ComponentT>
  2556. struct TargetFilesystemArtifact : public TargetArtifactBase
  2557. {
  2558. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  2559. int NumExpectedParameters() const override { return 1; }
  2560. std::string Evaluate(
  2561. const std::vector<std::string>& parameters,
  2562. cmGeneratorExpressionContext* context,
  2563. const GeneratorExpressionContent* content,
  2564. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2565. {
  2566. cmGeneratorTarget* target =
  2567. this->GetTarget(parameters, context, content, dagChecker);
  2568. if (!target) {
  2569. return std::string();
  2570. }
  2571. // Not a dependent target if we are querying for ArtifactDirTag,
  2572. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  2573. // and ArtifactBundleContentDirTag
  2574. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  2575. target, context);
  2576. std::string result =
  2577. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  2578. content);
  2579. if (context->HadError) {
  2580. return std::string();
  2581. }
  2582. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2583. }
  2584. };
  2585. template <typename ArtifactT>
  2586. struct TargetFilesystemArtifactNodeGroup
  2587. {
  2588. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2589. {
  2590. }
  2591. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2592. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2593. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2594. };
  2595. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2596. targetNodeGroup;
  2597. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2598. targetLinkerNodeGroup;
  2599. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2600. targetSoNameNodeGroup;
  2601. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2602. targetPdbNodeGroup;
  2603. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2604. targetBundleDirNode;
  2605. static const TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  2606. ArtifactNameTag>
  2607. targetBundleDirNameNode;
  2608. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2609. ArtifactPathTag>
  2610. targetBundleContentDirNode;
  2611. //
  2612. // To retrieve base name for various artifacts
  2613. //
  2614. template <typename ArtifactT>
  2615. struct TargetOutputNameArtifactResultGetter
  2616. {
  2617. static std::string Get(cmGeneratorTarget* target,
  2618. cmGeneratorExpressionContext* context,
  2619. const GeneratorExpressionContent* content);
  2620. };
  2621. template <>
  2622. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2623. {
  2624. static std::string Get(cmGeneratorTarget* target,
  2625. cmGeneratorExpressionContext* context,
  2626. const GeneratorExpressionContent* /*unused*/)
  2627. {
  2628. return target->GetOutputName(context->Config,
  2629. cmStateEnums::RuntimeBinaryArtifact) +
  2630. target->GetFilePostfix(context->Config);
  2631. }
  2632. };
  2633. template <>
  2634. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2635. {
  2636. static std::string Get(cmGeneratorTarget* target,
  2637. cmGeneratorExpressionContext* context,
  2638. const GeneratorExpressionContent* content)
  2639. {
  2640. // The file used to link to the target (.so, .lib, .a).
  2641. if (!target->IsLinkable()) {
  2642. ::reportError(context, content->GetOriginalExpression(),
  2643. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2644. "libraries and executables with ENABLE_EXPORTS.");
  2645. return std::string();
  2646. }
  2647. cmStateEnums::ArtifactType artifact =
  2648. target->HasImportLibrary(context->Config)
  2649. ? cmStateEnums::ImportLibraryArtifact
  2650. : cmStateEnums::RuntimeBinaryArtifact;
  2651. return target->GetOutputName(context->Config, artifact) +
  2652. target->GetFilePostfix(context->Config);
  2653. }
  2654. };
  2655. template <>
  2656. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2657. {
  2658. static std::string Get(cmGeneratorTarget* target,
  2659. cmGeneratorExpressionContext* context,
  2660. const GeneratorExpressionContent* content)
  2661. {
  2662. if (target->IsImported()) {
  2663. ::reportError(
  2664. context, content->GetOriginalExpression(),
  2665. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2666. return std::string();
  2667. }
  2668. std::string language = target->GetLinkerLanguage(context->Config);
  2669. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2670. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2671. ::reportError(
  2672. context, content->GetOriginalExpression(),
  2673. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2674. return std::string();
  2675. }
  2676. cmStateEnums::TargetType targetType = target->GetType();
  2677. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2678. targetType != cmStateEnums::MODULE_LIBRARY &&
  2679. targetType != cmStateEnums::EXECUTABLE) {
  2680. ::reportError(context, content->GetOriginalExpression(),
  2681. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2682. "targets with linker created artifacts.");
  2683. return std::string();
  2684. }
  2685. return target->GetPDBOutputName(context->Config) +
  2686. target->GetFilePostfix(context->Config);
  2687. }
  2688. };
  2689. template <typename ArtifactT>
  2690. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2691. {
  2692. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2693. int NumExpectedParameters() const override { return 1; }
  2694. std::string Evaluate(
  2695. const std::vector<std::string>& parameters,
  2696. cmGeneratorExpressionContext* context,
  2697. const GeneratorExpressionContent* content,
  2698. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2699. {
  2700. cmGeneratorTarget* target =
  2701. this->GetTarget(parameters, context, content, dagChecker);
  2702. if (!target) {
  2703. return std::string();
  2704. }
  2705. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2706. target, context, content);
  2707. if (context->HadError) {
  2708. return std::string();
  2709. }
  2710. return result;
  2711. }
  2712. };
  2713. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2714. targetFileBaseNameNode;
  2715. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2716. targetLinkerFileBaseNameNode;
  2717. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2718. targetPdbFileBaseNameNode;
  2719. class ArtifactFilePrefixTag;
  2720. class ArtifactLinkerFilePrefixTag;
  2721. class ArtifactFileSuffixTag;
  2722. class ArtifactLinkerFileSuffixTag;
  2723. template <typename ArtifactT>
  2724. struct TargetFileArtifactResultGetter
  2725. {
  2726. static std::string Get(cmGeneratorTarget* target,
  2727. cmGeneratorExpressionContext* context,
  2728. const GeneratorExpressionContent* content);
  2729. };
  2730. template <>
  2731. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2732. {
  2733. static std::string Get(cmGeneratorTarget* target,
  2734. cmGeneratorExpressionContext* context,
  2735. const GeneratorExpressionContent*)
  2736. {
  2737. return target->GetFilePrefix(context->Config);
  2738. }
  2739. };
  2740. template <>
  2741. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2742. {
  2743. static std::string Get(cmGeneratorTarget* target,
  2744. cmGeneratorExpressionContext* context,
  2745. const GeneratorExpressionContent* content)
  2746. {
  2747. if (!target->IsLinkable()) {
  2748. ::reportError(context, content->GetOriginalExpression(),
  2749. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2750. "executables with ENABLE_EXPORTS.");
  2751. return std::string();
  2752. }
  2753. cmStateEnums::ArtifactType artifact =
  2754. target->HasImportLibrary(context->Config)
  2755. ? cmStateEnums::ImportLibraryArtifact
  2756. : cmStateEnums::RuntimeBinaryArtifact;
  2757. return target->GetFilePrefix(context->Config, artifact);
  2758. }
  2759. };
  2760. template <>
  2761. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2762. {
  2763. static std::string Get(cmGeneratorTarget* target,
  2764. cmGeneratorExpressionContext* context,
  2765. const GeneratorExpressionContent*)
  2766. {
  2767. return target->GetFileSuffix(context->Config);
  2768. }
  2769. };
  2770. template <>
  2771. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2772. {
  2773. static std::string Get(cmGeneratorTarget* target,
  2774. cmGeneratorExpressionContext* context,
  2775. const GeneratorExpressionContent* content)
  2776. {
  2777. if (!target->IsLinkable()) {
  2778. ::reportError(context, content->GetOriginalExpression(),
  2779. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2780. "executables with ENABLE_EXPORTS.");
  2781. return std::string();
  2782. }
  2783. cmStateEnums::ArtifactType artifact =
  2784. target->HasImportLibrary(context->Config)
  2785. ? cmStateEnums::ImportLibraryArtifact
  2786. : cmStateEnums::RuntimeBinaryArtifact;
  2787. return target->GetFileSuffix(context->Config, artifact);
  2788. }
  2789. };
  2790. template <typename ArtifactT>
  2791. struct TargetFileArtifact : public TargetArtifactBase
  2792. {
  2793. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2794. int NumExpectedParameters() const override { return 1; }
  2795. std::string Evaluate(
  2796. const std::vector<std::string>& parameters,
  2797. cmGeneratorExpressionContext* context,
  2798. const GeneratorExpressionContent* content,
  2799. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2800. {
  2801. cmGeneratorTarget* target =
  2802. this->GetTarget(parameters, context, content, dagChecker);
  2803. if (!target) {
  2804. return std::string();
  2805. }
  2806. std::string result =
  2807. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2808. if (context->HadError) {
  2809. return std::string();
  2810. }
  2811. return result;
  2812. }
  2813. };
  2814. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2815. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2816. targetLinkerFilePrefixNode;
  2817. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2818. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2819. targetLinkerFileSuffixNode;
  2820. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2821. {
  2822. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2823. std::string Evaluate(
  2824. const std::vector<std::string>& parameters,
  2825. cmGeneratorExpressionContext* context,
  2826. const GeneratorExpressionContent* content,
  2827. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2828. {
  2829. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2830. if (listIn.empty()) {
  2831. reportError(context, content->GetOriginalExpression(),
  2832. "\"\" is not an absolute path.");
  2833. return std::string();
  2834. }
  2835. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2836. cmOutputConverter converter(snapshot);
  2837. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2838. std::vector<std::string> listOut;
  2839. listOut.reserve(listIn.size());
  2840. for (auto const& in : listIn) {
  2841. if (!cmSystemTools::FileIsFullPath(in)) {
  2842. reportError(context, content->GetOriginalExpression(),
  2843. "\"" + in + "\" is not an absolute path.");
  2844. return std::string();
  2845. }
  2846. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2847. }
  2848. return cmJoin(listOut, separator);
  2849. }
  2850. } shellPathNode;
  2851. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2852. const std::string& identifier)
  2853. {
  2854. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2855. { "0", &zeroNode },
  2856. { "1", &oneNode },
  2857. { "AND", &andNode },
  2858. { "OR", &orNode },
  2859. { "NOT", &notNode },
  2860. { "C_COMPILER_ID", &cCompilerIdNode },
  2861. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2862. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2863. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2864. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2865. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2866. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2867. { "VERSION_GREATER", &versionGreaterNode },
  2868. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2869. { "VERSION_LESS", &versionLessNode },
  2870. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2871. { "VERSION_EQUAL", &versionEqualNode },
  2872. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2873. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2874. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2875. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2876. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2877. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2878. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2879. { "PLATFORM_ID", &platformIdNode },
  2880. { "COMPILE_FEATURES", &compileFeaturesNode },
  2881. { "CONFIGURATION", &configurationNode },
  2882. { "CONFIG", &configurationTestNode },
  2883. { "TARGET_FILE", &targetNodeGroup.File },
  2884. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2885. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2886. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2887. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2888. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2889. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2890. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2891. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2892. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2893. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2894. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2895. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2896. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2897. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2898. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2899. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2900. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2901. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2902. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2903. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  2904. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2905. { "STREQUAL", &strEqualNode },
  2906. { "EQUAL", &equalNode },
  2907. { "IN_LIST", &inListNode },
  2908. { "FILTER", &filterNode },
  2909. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2910. { "LOWER_CASE", &lowerCaseNode },
  2911. { "UPPER_CASE", &upperCaseNode },
  2912. { "PATH", &pathNode },
  2913. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2914. { "BOOL", &boolNode },
  2915. { "IF", &ifNode },
  2916. { "ANGLE-R", &angle_rNode },
  2917. { "COMMA", &commaNode },
  2918. { "SEMICOLON", &semicolonNode },
  2919. { "TARGET_PROPERTY", &targetPropertyNode },
  2920. { "TARGET_NAME", &targetNameNode },
  2921. { "TARGET_OBJECTS", &targetObjectsNode },
  2922. { "TARGET_POLICY", &targetPolicyNode },
  2923. { "TARGET_EXISTS", &targetExistsNode },
  2924. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2925. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2926. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2927. { "GENEX_EVAL", &genexEvalNode },
  2928. { "BUILD_INTERFACE", &buildInterfaceNode },
  2929. { "INSTALL_INTERFACE", &installInterfaceNode },
  2930. { "INSTALL_PREFIX", &installPrefixNode },
  2931. { "JOIN", &joinNode },
  2932. { "LINK_ONLY", &linkOnlyNode },
  2933. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2934. { "COMPILE_LANGUAGE", &languageNode },
  2935. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2936. { "LINK_LANGUAGE", &linkLanguageNode },
  2937. { "LINK_LIBRARY", &linkLibraryNode },
  2938. { "LINK_GROUP", &linkGroupNode },
  2939. { "HOST_LINK", &hostLinkNode },
  2940. { "DEVICE_LINK", &deviceLinkNode },
  2941. { "SHELL_PATH", &shellPathNode }
  2942. };
  2943. {
  2944. auto itr = nodeMap.find(identifier);
  2945. if (itr != nodeMap.end()) {
  2946. return itr->second;
  2947. }
  2948. }
  2949. return nullptr;
  2950. }
  2951. void reportError(cmGeneratorExpressionContext* context,
  2952. const std::string& expr, const std::string& result)
  2953. {
  2954. context->HadError = true;
  2955. if (context->Quiet) {
  2956. return;
  2957. }
  2958. std::ostringstream e;
  2959. /* clang-format off */
  2960. e << "Error evaluating generator expression:\n"
  2961. << " " << expr << "\n"
  2962. << result;
  2963. /* clang-format on */
  2964. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2965. e.str(), context->Backtrace);
  2966. }