cmGeneratorExpressionNode.cxx 93 KB

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