cmGeneratorExpressionNode.cxx 88 KB

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