cmGeneratorExpressionNode.cxx 89 KB

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