cmGeneratorExpressionNode.cxx 78 KB

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