cmGeneratorExpressionNode.cxx 93 KB

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