cmGeneratorExpressionNode.cxx 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  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. if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1339. std::ostringstream e;
  1340. e << "Objects of target \"" << tgtName
  1341. << "\" referenced but is not an OBJECT library.";
  1342. reportError(context, content->GetOriginalExpression(), e.str());
  1343. return std::string();
  1344. }
  1345. if (!context->EvaluateForBuildsystem) {
  1346. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1347. std::string reason;
  1348. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1349. std::ostringstream e;
  1350. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1351. "is only suitable for consumption by CMake (limited"
  1352. << reason
  1353. << "). "
  1354. "It is not suitable for writing out elsewhere.";
  1355. reportError(context, content->GetOriginalExpression(), e.str());
  1356. return std::string();
  1357. }
  1358. }
  1359. std::vector<std::string> objects;
  1360. if (gt->IsImported()) {
  1361. const char* loc = nullptr;
  1362. const char* imp = nullptr;
  1363. std::string suffix;
  1364. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1365. cmSystemTools::ExpandListArgument(loc, objects);
  1366. }
  1367. context->HadContextSensitiveCondition = true;
  1368. } else {
  1369. gt->GetTargetObjectNames(context->Config, objects);
  1370. std::string obj_dir;
  1371. if (context->EvaluateForBuildsystem) {
  1372. // Use object file directory with buildsystem placeholder.
  1373. obj_dir = gt->ObjectDirectory;
  1374. // Here we assume that the set of object files produced
  1375. // by an object library does not vary with configuration
  1376. // and do not set HadContextSensitiveCondition to true.
  1377. } else {
  1378. // Use object file directory with per-config location.
  1379. obj_dir = gt->GetObjectDirectory(context->Config);
  1380. context->HadContextSensitiveCondition = true;
  1381. }
  1382. for (std::string& o : objects) {
  1383. o = obj_dir + o;
  1384. }
  1385. }
  1386. // Create the cmSourceFile instances in the referencing directory.
  1387. cmMakefile* mf = context->LG->GetMakefile();
  1388. for (std::string& o : objects) {
  1389. mf->AddTargetObject(tgtName, o);
  1390. }
  1391. return cmJoin(objects, ";");
  1392. }
  1393. } targetObjectsNode;
  1394. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1395. {
  1396. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1397. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1398. std::string Evaluate(
  1399. const std::vector<std::string>& parameters,
  1400. cmGeneratorExpressionContext* context,
  1401. const GeneratorExpressionContent* content,
  1402. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1403. {
  1404. cmGeneratorTarget const* target = context->HeadTarget;
  1405. if (!target) {
  1406. reportError(
  1407. context, content->GetOriginalExpression(),
  1408. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1409. "not be used with add_custom_command or add_custom_target.");
  1410. return std::string();
  1411. }
  1412. context->HadHeadSensitiveCondition = true;
  1413. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1414. static LangMap availableFeatures;
  1415. LangMap testedFeatures;
  1416. for (std::string const& p : parameters) {
  1417. std::string error;
  1418. std::string lang;
  1419. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1420. context->HeadTarget->Target, p, lang, &error)) {
  1421. reportError(context, content->GetOriginalExpression(), error);
  1422. return std::string();
  1423. }
  1424. testedFeatures[lang].push_back(p);
  1425. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1426. const char* featuresKnown =
  1427. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1428. if (!featuresKnown) {
  1429. reportError(context, content->GetOriginalExpression(), error);
  1430. return std::string();
  1431. }
  1432. cmSystemTools::ExpandListArgument(featuresKnown,
  1433. availableFeatures[lang]);
  1434. }
  1435. }
  1436. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1437. for (auto const& lit : testedFeatures) {
  1438. std::vector<std::string> const& langAvailable =
  1439. availableFeatures[lit.first];
  1440. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1441. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1442. for (std::string const& it : lit.second) {
  1443. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1444. langAvailable.end()) {
  1445. return "0";
  1446. }
  1447. if (standardDefault && !*standardDefault) {
  1448. // This compiler has no notion of language standard levels.
  1449. // All features known for the language are always available.
  1450. continue;
  1451. }
  1452. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1453. target->Target, lit.first, it)) {
  1454. if (evalLL) {
  1455. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1456. if (!l) {
  1457. l = standardDefault;
  1458. }
  1459. assert(l);
  1460. context->MaxLanguageStandard[target][lit.first] = l;
  1461. } else {
  1462. return "0";
  1463. }
  1464. }
  1465. }
  1466. }
  1467. return "1";
  1468. }
  1469. } compileFeaturesNode;
  1470. static const char* targetPolicyWhitelist[] = {
  1471. nullptr
  1472. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1473. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1474. #undef TARGET_POLICY_STRING
  1475. };
  1476. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1477. const char* policy)
  1478. {
  1479. #define RETURN_POLICY(POLICY) \
  1480. if (strcmp(policy, #POLICY) == 0) { \
  1481. return tgt->GetPolicyStatus##POLICY(); \
  1482. }
  1483. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1484. #undef RETURN_POLICY
  1485. assert(false && "Unreachable code. Not a valid policy");
  1486. return cmPolicies::WARN;
  1487. }
  1488. cmPolicies::PolicyID policyForString(const char* policy_id)
  1489. {
  1490. #define RETURN_POLICY_ID(POLICY_ID) \
  1491. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1492. return cmPolicies::POLICY_ID; \
  1493. }
  1494. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1495. #undef RETURN_POLICY_ID
  1496. assert(false && "Unreachable code. Not a valid policy");
  1497. return cmPolicies::CMP0002;
  1498. }
  1499. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1500. {
  1501. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1502. int NumExpectedParameters() const override { return 1; }
  1503. std::string Evaluate(
  1504. const std::vector<std::string>& parameters,
  1505. cmGeneratorExpressionContext* context,
  1506. const GeneratorExpressionContent* content,
  1507. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1508. {
  1509. if (!context->HeadTarget) {
  1510. reportError(
  1511. context, content->GetOriginalExpression(),
  1512. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1513. "may not be used with add_custom_command or add_custom_target.");
  1514. return std::string();
  1515. }
  1516. context->HadContextSensitiveCondition = true;
  1517. context->HadHeadSensitiveCondition = true;
  1518. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1519. const char* policy = targetPolicyWhitelist[i];
  1520. if (parameters.front() == policy) {
  1521. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1522. switch (statusForTarget(context->HeadTarget, policy)) {
  1523. case cmPolicies::WARN:
  1524. lg->IssueMessage(
  1525. MessageType::AUTHOR_WARNING,
  1526. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1527. CM_FALLTHROUGH;
  1528. case cmPolicies::REQUIRED_IF_USED:
  1529. case cmPolicies::REQUIRED_ALWAYS:
  1530. case cmPolicies::OLD:
  1531. return "0";
  1532. case cmPolicies::NEW:
  1533. return "1";
  1534. }
  1535. }
  1536. }
  1537. reportError(
  1538. context, content->GetOriginalExpression(),
  1539. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1540. "policies. Currently it may be used with the following policies:\n"
  1541. #define STRINGIFY_HELPER(X) #X
  1542. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1543. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1544. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1545. #undef TARGET_POLICY_LIST_ITEM
  1546. );
  1547. return std::string();
  1548. }
  1549. } targetPolicyNode;
  1550. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1551. {
  1552. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1553. bool GeneratesContent() const override { return true; }
  1554. int NumExpectedParameters() const override { return 0; }
  1555. std::string Evaluate(
  1556. const std::vector<std::string>& /*parameters*/,
  1557. cmGeneratorExpressionContext* context,
  1558. const GeneratorExpressionContent* content,
  1559. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1560. {
  1561. reportError(context, content->GetOriginalExpression(),
  1562. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1563. "should never be evaluated.");
  1564. return std::string();
  1565. }
  1566. } installPrefixNode;
  1567. class ArtifactDirTag;
  1568. class ArtifactLinkerTag;
  1569. class ArtifactNameTag;
  1570. class ArtifactPathTag;
  1571. class ArtifactPdbTag;
  1572. class ArtifactSonameTag;
  1573. class ArtifactBundleDirTag;
  1574. class ArtifactBundleContentDirTag;
  1575. template <typename ArtifactT>
  1576. struct TargetFilesystemArtifactResultCreator
  1577. {
  1578. static std::string Create(cmGeneratorTarget* target,
  1579. cmGeneratorExpressionContext* context,
  1580. const GeneratorExpressionContent* content);
  1581. };
  1582. template <>
  1583. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1584. {
  1585. static std::string Create(cmGeneratorTarget* target,
  1586. cmGeneratorExpressionContext* context,
  1587. const GeneratorExpressionContent* content)
  1588. {
  1589. // The target soname file (.so.1).
  1590. if (target->IsDLLPlatform()) {
  1591. ::reportError(context, content->GetOriginalExpression(),
  1592. "TARGET_SONAME_FILE is not allowed "
  1593. "for DLL target platforms.");
  1594. return std::string();
  1595. }
  1596. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1597. ::reportError(context, content->GetOriginalExpression(),
  1598. "TARGET_SONAME_FILE is allowed only for "
  1599. "SHARED libraries.");
  1600. return std::string();
  1601. }
  1602. std::string result = target->GetDirectory(context->Config);
  1603. result += "/";
  1604. result += target->GetSOName(context->Config);
  1605. return result;
  1606. }
  1607. };
  1608. template <>
  1609. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1610. {
  1611. static std::string Create(cmGeneratorTarget* target,
  1612. cmGeneratorExpressionContext* context,
  1613. const GeneratorExpressionContent* content)
  1614. {
  1615. if (target->IsImported()) {
  1616. ::reportError(context, content->GetOriginalExpression(),
  1617. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1618. return std::string();
  1619. }
  1620. std::string language = target->GetLinkerLanguage(context->Config);
  1621. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1622. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1623. ::reportError(context, content->GetOriginalExpression(),
  1624. "TARGET_PDB_FILE is not supported by the target linker.");
  1625. return std::string();
  1626. }
  1627. cmStateEnums::TargetType targetType = target->GetType();
  1628. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1629. targetType != cmStateEnums::MODULE_LIBRARY &&
  1630. targetType != cmStateEnums::EXECUTABLE) {
  1631. ::reportError(context, content->GetOriginalExpression(),
  1632. "TARGET_PDB_FILE is allowed only for "
  1633. "targets with linker created artifacts.");
  1634. return std::string();
  1635. }
  1636. std::string result = target->GetPDBDirectory(context->Config);
  1637. result += "/";
  1638. result += target->GetPDBName(context->Config);
  1639. return result;
  1640. }
  1641. };
  1642. template <>
  1643. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1644. {
  1645. static std::string Create(cmGeneratorTarget* target,
  1646. cmGeneratorExpressionContext* context,
  1647. const GeneratorExpressionContent* content)
  1648. {
  1649. // The file used to link to the target (.so, .lib, .a).
  1650. if (!target->IsLinkable()) {
  1651. ::reportError(context, content->GetOriginalExpression(),
  1652. "TARGET_LINKER_FILE is allowed only for libraries and "
  1653. "executables with ENABLE_EXPORTS.");
  1654. return std::string();
  1655. }
  1656. cmStateEnums::ArtifactType artifact =
  1657. target->HasImportLibrary(context->Config)
  1658. ? cmStateEnums::ImportLibraryArtifact
  1659. : cmStateEnums::RuntimeBinaryArtifact;
  1660. return target->GetFullPath(context->Config, artifact);
  1661. }
  1662. };
  1663. template <>
  1664. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1665. {
  1666. static std::string Create(cmGeneratorTarget* target,
  1667. cmGeneratorExpressionContext* context,
  1668. const GeneratorExpressionContent* content)
  1669. {
  1670. if (target->IsImported()) {
  1671. ::reportError(context, content->GetOriginalExpression(),
  1672. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1673. return std::string();
  1674. }
  1675. if (!target->IsBundleOnApple()) {
  1676. ::reportError(context, content->GetOriginalExpression(),
  1677. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1678. return std::string();
  1679. }
  1680. std::string outpath = target->GetDirectory(context->Config) + '/';
  1681. return target->BuildBundleDirectory(outpath, context->Config,
  1682. cmGeneratorTarget::BundleDirLevel);
  1683. }
  1684. };
  1685. template <>
  1686. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1687. {
  1688. static std::string Create(cmGeneratorTarget* target,
  1689. cmGeneratorExpressionContext* context,
  1690. const GeneratorExpressionContent* content)
  1691. {
  1692. if (target->IsImported()) {
  1693. ::reportError(
  1694. context, content->GetOriginalExpression(),
  1695. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1696. return std::string();
  1697. }
  1698. if (!target->IsBundleOnApple()) {
  1699. ::reportError(
  1700. context, content->GetOriginalExpression(),
  1701. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1702. return std::string();
  1703. }
  1704. std::string outpath = target->GetDirectory(context->Config) + '/';
  1705. return target->BuildBundleDirectory(outpath, context->Config,
  1706. cmGeneratorTarget::ContentLevel);
  1707. }
  1708. };
  1709. template <>
  1710. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1711. {
  1712. static std::string Create(cmGeneratorTarget* target,
  1713. cmGeneratorExpressionContext* context,
  1714. const GeneratorExpressionContent* /*unused*/)
  1715. {
  1716. return target->GetFullPath(context->Config,
  1717. cmStateEnums::RuntimeBinaryArtifact, true);
  1718. }
  1719. };
  1720. template <typename ArtifactT>
  1721. struct TargetFilesystemArtifactResultGetter
  1722. {
  1723. static std::string Get(const std::string& result);
  1724. };
  1725. template <>
  1726. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1727. {
  1728. static std::string Get(const std::string& result)
  1729. {
  1730. return cmSystemTools::GetFilenameName(result);
  1731. }
  1732. };
  1733. template <>
  1734. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1735. {
  1736. static std::string Get(const std::string& result)
  1737. {
  1738. return cmSystemTools::GetFilenamePath(result);
  1739. }
  1740. };
  1741. template <>
  1742. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1743. {
  1744. static std::string Get(const std::string& result) { return result; }
  1745. };
  1746. template <typename ArtifactT, typename ComponentT>
  1747. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1748. {
  1749. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1750. int NumExpectedParameters() const override { return 1; }
  1751. std::string Evaluate(
  1752. const std::vector<std::string>& parameters,
  1753. cmGeneratorExpressionContext* context,
  1754. const GeneratorExpressionContent* content,
  1755. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1756. {
  1757. // Lookup the referenced target.
  1758. std::string name = parameters.front();
  1759. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1760. ::reportError(context, content->GetOriginalExpression(),
  1761. "Expression syntax not recognized.");
  1762. return std::string();
  1763. }
  1764. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1765. if (!target) {
  1766. ::reportError(context, content->GetOriginalExpression(),
  1767. "No target \"" + name + "\"");
  1768. return std::string();
  1769. }
  1770. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1771. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1772. ::reportError(context, content->GetOriginalExpression(),
  1773. "Target \"" + name +
  1774. "\" is not an executable or library.");
  1775. return std::string();
  1776. }
  1777. if (dagChecker &&
  1778. (dagChecker->EvaluatingLinkLibraries(target) ||
  1779. (dagChecker->EvaluatingSources() &&
  1780. target == dagChecker->TopTarget()))) {
  1781. ::reportError(context, content->GetOriginalExpression(),
  1782. "Expressions which require the linker language may not "
  1783. "be used while evaluating link libraries");
  1784. return std::string();
  1785. }
  1786. context->DependTargets.insert(target);
  1787. context->AllTargets.insert(target);
  1788. std::string result =
  1789. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1790. content);
  1791. if (context->HadError) {
  1792. return std::string();
  1793. }
  1794. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1795. }
  1796. };
  1797. template <typename ArtifactT>
  1798. struct TargetFilesystemArtifactNodeGroup
  1799. {
  1800. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  1801. {
  1802. }
  1803. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1804. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1805. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1806. };
  1807. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1808. targetNodeGroup;
  1809. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1810. targetLinkerNodeGroup;
  1811. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1812. targetSoNameNodeGroup;
  1813. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1814. targetPdbNodeGroup;
  1815. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1816. targetBundleDirNode;
  1817. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1818. ArtifactPathTag>
  1819. targetBundleContentDirNode;
  1820. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1821. {
  1822. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  1823. std::string Evaluate(
  1824. const std::vector<std::string>& parameters,
  1825. cmGeneratorExpressionContext* context,
  1826. const GeneratorExpressionContent* content,
  1827. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1828. {
  1829. std::vector<std::string> listIn;
  1830. cmSystemTools::ExpandListArgument(parameters.front(), listIn);
  1831. if (listIn.empty()) {
  1832. reportError(context, content->GetOriginalExpression(),
  1833. "\"\" is not an absolute path.");
  1834. return std::string();
  1835. }
  1836. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  1837. cmOutputConverter converter(snapshot);
  1838. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  1839. std::vector<std::string> listOut;
  1840. listOut.reserve(listIn.size());
  1841. for (auto const& in : listIn) {
  1842. if (!cmSystemTools::FileIsFullPath(in)) {
  1843. reportError(context, content->GetOriginalExpression(),
  1844. "\"" + in + "\" is not an absolute path.");
  1845. return std::string();
  1846. }
  1847. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  1848. }
  1849. return cmJoin(listOut, separator);
  1850. }
  1851. } shellPathNode;
  1852. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1853. const std::string& identifier)
  1854. {
  1855. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  1856. { "0", &zeroNode },
  1857. { "1", &oneNode },
  1858. { "AND", &andNode },
  1859. { "OR", &orNode },
  1860. { "NOT", &notNode },
  1861. { "C_COMPILER_ID", &cCompilerIdNode },
  1862. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  1863. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  1864. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  1865. { "VERSION_GREATER", &versionGreaterNode },
  1866. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  1867. { "VERSION_LESS", &versionLessNode },
  1868. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  1869. { "VERSION_EQUAL", &versionEqualNode },
  1870. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  1871. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  1872. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  1873. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  1874. { "PLATFORM_ID", &platformIdNode },
  1875. { "COMPILE_FEATURES", &compileFeaturesNode },
  1876. { "CONFIGURATION", &configurationNode },
  1877. { "CONFIG", &configurationTestNode },
  1878. { "TARGET_FILE", &targetNodeGroup.File },
  1879. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  1880. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  1881. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  1882. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  1883. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  1884. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  1885. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  1886. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  1887. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  1888. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  1889. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  1890. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  1891. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  1892. { "STREQUAL", &strEqualNode },
  1893. { "EQUAL", &equalNode },
  1894. { "IN_LIST", &inListNode },
  1895. { "LOWER_CASE", &lowerCaseNode },
  1896. { "UPPER_CASE", &upperCaseNode },
  1897. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  1898. { "BOOL", &boolNode },
  1899. { "IF", &ifNode },
  1900. { "ANGLE-R", &angle_rNode },
  1901. { "COMMA", &commaNode },
  1902. { "SEMICOLON", &semicolonNode },
  1903. { "TARGET_PROPERTY", &targetPropertyNode },
  1904. { "TARGET_NAME", &targetNameNode },
  1905. { "TARGET_OBJECTS", &targetObjectsNode },
  1906. { "TARGET_POLICY", &targetPolicyNode },
  1907. { "TARGET_EXISTS", &targetExistsNode },
  1908. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  1909. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  1910. { "GENEX_EVAL", &genexEvalNode },
  1911. { "BUILD_INTERFACE", &buildInterfaceNode },
  1912. { "INSTALL_INTERFACE", &installInterfaceNode },
  1913. { "INSTALL_PREFIX", &installPrefixNode },
  1914. { "JOIN", &joinNode },
  1915. { "LINK_ONLY", &linkOnlyNode },
  1916. { "COMPILE_LANGUAGE", &languageNode },
  1917. { "SHELL_PATH", &shellPathNode }
  1918. };
  1919. {
  1920. auto itr = nodeMap.find(identifier);
  1921. if (itr != nodeMap.end()) {
  1922. return itr->second;
  1923. }
  1924. }
  1925. return nullptr;
  1926. }
  1927. void reportError(cmGeneratorExpressionContext* context,
  1928. const std::string& expr, const std::string& result)
  1929. {
  1930. context->HadError = true;
  1931. if (context->Quiet) {
  1932. return;
  1933. }
  1934. std::ostringstream e;
  1935. /* clang-format off */
  1936. e << "Error evaluating generator expression:\n"
  1937. << " " << expr << "\n"
  1938. << result;
  1939. /* clang-format on */
  1940. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  1941. e.str(), context->Backtrace);
  1942. }