cmGeneratorExpressionNode.cxx 98 KB

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