cmGeneratorExpressionNode.cxx 88 KB

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