cmGeneratorExpressionNode.cxx 119 KB

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