cmGeneratorExpressionNode.cxx 91 KB

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