cmGeneratorExpressionNode.cxx 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365
  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. cmLocalGenerator const* lg = context->CurrentTarget
  1760. ? context->CurrentTarget->GetLocalGenerator()
  1761. : context->LG;
  1762. target = lg->FindGeneratorTargetToUse(targetName);
  1763. if (!target) {
  1764. std::ostringstream e;
  1765. e << "Target \"" << targetName << "\" not found.";
  1766. reportError(context, content->GetOriginalExpression(), e.str());
  1767. return std::string();
  1768. }
  1769. context->AllTargets.insert(target);
  1770. } else if (parameters.size() == 1) {
  1771. target = context->HeadTarget;
  1772. propertyName = parameters[0];
  1773. // Keep track of the properties seen while processing.
  1774. // The evaluation of the LINK_LIBRARIES generator expressions
  1775. // will check this to ensure that properties have one consistent
  1776. // value for all evaluations.
  1777. context->SeenTargetProperties.insert(propertyName);
  1778. context->HadHeadSensitiveCondition = true;
  1779. if (!target) {
  1780. reportError(
  1781. context, content->GetOriginalExpression(),
  1782. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1783. "It may not be used with add_custom_command or add_custom_target. "
  1784. " "
  1785. "Specify the target to read a property from using the "
  1786. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1787. return std::string();
  1788. }
  1789. } else {
  1790. reportError(
  1791. context, content->GetOriginalExpression(),
  1792. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1793. return std::string();
  1794. }
  1795. if (propertyName == "SOURCES") {
  1796. context->SourceSensitiveTargets.insert(target);
  1797. }
  1798. if (propertyName.empty()) {
  1799. reportError(
  1800. context, content->GetOriginalExpression(),
  1801. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1802. "name.");
  1803. return std::string();
  1804. }
  1805. if (!propertyNameValidator.find(propertyName)) {
  1806. ::reportError(context, content->GetOriginalExpression(),
  1807. "Property name not supported.");
  1808. return std::string();
  1809. }
  1810. assert(target);
  1811. if (propertyName == "LINKER_LANGUAGE") {
  1812. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1813. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1814. dagCheckerParent->EvaluatingSources())) {
  1815. reportError(
  1816. context, content->GetOriginalExpression(),
  1817. "LINKER_LANGUAGE target property can not be used while evaluating "
  1818. "link libraries for a static library");
  1819. return std::string();
  1820. }
  1821. return target->GetLinkerLanguage(context->Config);
  1822. }
  1823. std::string interfacePropertyName;
  1824. bool isInterfaceProperty = false;
  1825. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1826. if (propertyName == #prop) { \
  1827. interfacePropertyName = "INTERFACE_" #prop; \
  1828. } else if (propertyName == "INTERFACE_" #prop) { \
  1829. interfacePropertyName = "INTERFACE_" #prop; \
  1830. isInterfaceProperty = true; \
  1831. } else
  1832. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1833. // Note that the above macro terminates with an else
  1834. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1835. cmPolicies::PolicyStatus polSt =
  1836. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1837. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1838. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1839. }
  1840. }
  1841. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1842. bool evaluatingLinkLibraries = false;
  1843. if (dagCheckerParent) {
  1844. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1845. dagCheckerParent->EvaluatingPICExpression()) {
  1846. // No check required.
  1847. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1848. evaluatingLinkLibraries = true;
  1849. if (!interfacePropertyName.empty()) {
  1850. reportError(
  1851. context, content->GetOriginalExpression(),
  1852. "$<TARGET_PROPERTY:...> expression in link libraries "
  1853. "evaluation depends on target property which is transitive "
  1854. "over the link libraries, creating a recursion.");
  1855. return std::string();
  1856. }
  1857. } else {
  1858. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1859. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1860. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1861. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1862. }
  1863. }
  1864. if (isInterfaceProperty) {
  1865. return cmGeneratorExpression::StripEmptyListElements(
  1866. target->EvaluateInterfaceProperty(propertyName, context,
  1867. dagCheckerParent));
  1868. }
  1869. cmGeneratorExpressionDAGChecker dagChecker(
  1870. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1871. switch (dagChecker.Check()) {
  1872. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1873. dagChecker.ReportError(context, content->GetOriginalExpression());
  1874. return std::string();
  1875. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1876. // No error. We just skip cyclic references.
  1877. return std::string();
  1878. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1879. // We handle transitive properties above. For non-transitive
  1880. // properties we accept repeats anyway.
  1881. case cmGeneratorExpressionDAGChecker::DAG:
  1882. break;
  1883. }
  1884. std::string result;
  1885. bool haveProp = false;
  1886. if (cmValue p = target->GetProperty(propertyName)) {
  1887. result = *p;
  1888. haveProp = true;
  1889. } else if (evaluatingLinkLibraries) {
  1890. return std::string();
  1891. }
  1892. if (!haveProp && !target->IsImported() &&
  1893. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1894. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1895. context->Config)) {
  1896. context->HadContextSensitiveCondition = true;
  1897. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1898. context->Config)
  1899. ? "1"
  1900. : "0";
  1901. }
  1902. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1903. context->Config)) {
  1904. context->HadContextSensitiveCondition = true;
  1905. const char* propContent =
  1906. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1907. context->Config);
  1908. return propContent ? propContent : "";
  1909. }
  1910. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1911. context->Config)) {
  1912. context->HadContextSensitiveCondition = true;
  1913. const char* propContent =
  1914. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1915. context->Config);
  1916. return propContent ? propContent : "";
  1917. }
  1918. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1919. context->Config)) {
  1920. context->HadContextSensitiveCondition = true;
  1921. const char* propContent =
  1922. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1923. context->Config);
  1924. return propContent ? propContent : "";
  1925. }
  1926. }
  1927. if (!target->IsImported() && dagCheckerParent &&
  1928. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1929. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1930. context->Config)) {
  1931. context->HadContextSensitiveCondition = true;
  1932. const char* propContent =
  1933. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1934. context->Config);
  1935. return propContent ? propContent : "";
  1936. }
  1937. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1938. context->Config)) {
  1939. context->HadContextSensitiveCondition = true;
  1940. const char* propContent =
  1941. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1942. context->Config);
  1943. return propContent ? propContent : "";
  1944. }
  1945. }
  1946. if (!interfacePropertyName.empty()) {
  1947. result = cmGeneratorExpression::StripEmptyListElements(
  1948. this->EvaluateDependentExpression(result, context->LG, context, target,
  1949. &dagChecker, target));
  1950. std::string linkedTargetsContent = getLinkedTargetsContent(
  1951. target, interfacePropertyName, context, &dagChecker);
  1952. if (!linkedTargetsContent.empty()) {
  1953. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1954. }
  1955. }
  1956. return result;
  1957. }
  1958. } targetPropertyNode;
  1959. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1960. {
  1961. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1962. bool GeneratesContent() const override { return true; }
  1963. bool AcceptsArbitraryContentParameter() const override { return true; }
  1964. bool RequiresLiteralInput() const override { return true; }
  1965. std::string Evaluate(
  1966. const std::vector<std::string>& parameters,
  1967. cmGeneratorExpressionContext* /*context*/,
  1968. const GeneratorExpressionContent* /*content*/,
  1969. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1970. {
  1971. return parameters.front();
  1972. }
  1973. int NumExpectedParameters() const override { return 1; }
  1974. } targetNameNode;
  1975. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1976. {
  1977. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1978. std::string Evaluate(
  1979. const std::vector<std::string>& parameters,
  1980. cmGeneratorExpressionContext* context,
  1981. const GeneratorExpressionContent* content,
  1982. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1983. {
  1984. std::string const& tgtName = parameters.front();
  1985. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1986. if (!gt) {
  1987. std::ostringstream e;
  1988. e << "Objects of target \"" << tgtName
  1989. << "\" referenced but no such target exists.";
  1990. reportError(context, content->GetOriginalExpression(), e.str());
  1991. return std::string();
  1992. }
  1993. cmStateEnums::TargetType type = gt->GetType();
  1994. if (type != cmStateEnums::EXECUTABLE &&
  1995. type != cmStateEnums::STATIC_LIBRARY &&
  1996. type != cmStateEnums::SHARED_LIBRARY &&
  1997. type != cmStateEnums::MODULE_LIBRARY &&
  1998. type != cmStateEnums::OBJECT_LIBRARY) {
  1999. std::ostringstream e;
  2000. e << "Objects of target \"" << tgtName
  2001. << "\" referenced but is not one of the allowed target types "
  2002. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2003. reportError(context, content->GetOriginalExpression(), e.str());
  2004. return std::string();
  2005. }
  2006. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2007. {
  2008. std::string reason;
  2009. if (!context->EvaluateForBuildsystem &&
  2010. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2011. std::ostringstream e;
  2012. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2013. "is only suitable for consumption by CMake (limited"
  2014. << reason
  2015. << "). "
  2016. "It is not suitable for writing out elsewhere.";
  2017. reportError(context, content->GetOriginalExpression(), e.str());
  2018. return std::string();
  2019. }
  2020. }
  2021. std::vector<std::string> objects;
  2022. if (gt->IsImported()) {
  2023. cmValue loc = nullptr;
  2024. cmValue imp = nullptr;
  2025. std::string suffix;
  2026. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  2027. cmExpandList(*loc, objects);
  2028. }
  2029. context->HadContextSensitiveCondition = true;
  2030. } else {
  2031. gt->GetTargetObjectNames(context->Config, objects);
  2032. std::string obj_dir;
  2033. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  2034. // Use object file directory with buildsystem placeholder.
  2035. obj_dir = gt->ObjectDirectory;
  2036. context->HadContextSensitiveCondition =
  2037. gt->HasContextDependentSources();
  2038. } else {
  2039. // Use object file directory with per-config location.
  2040. obj_dir = gt->GetObjectDirectory(context->Config);
  2041. context->HadContextSensitiveCondition = true;
  2042. }
  2043. for (std::string& o : objects) {
  2044. o = cmStrCat(obj_dir, o);
  2045. }
  2046. }
  2047. // Create the cmSourceFile instances in the referencing directory.
  2048. cmMakefile* mf = context->LG->GetMakefile();
  2049. for (std::string const& o : objects) {
  2050. mf->AddTargetObject(tgtName, o);
  2051. }
  2052. return cmJoin(objects, ";");
  2053. }
  2054. } targetObjectsNode;
  2055. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  2056. {
  2057. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  2058. std::string Evaluate(
  2059. const std::vector<std::string>& parameters,
  2060. cmGeneratorExpressionContext* context,
  2061. const GeneratorExpressionContent* content,
  2062. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2063. {
  2064. std::string const& tgtName = parameters.front();
  2065. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2066. if (!gt) {
  2067. std::ostringstream e;
  2068. e << "Objects of target \"" << tgtName
  2069. << "\" referenced but no such target exists.";
  2070. reportError(context, content->GetOriginalExpression(), e.str());
  2071. return std::string();
  2072. }
  2073. cmStateEnums::TargetType type = gt->GetType();
  2074. if (type != cmStateEnums::EXECUTABLE &&
  2075. type != cmStateEnums::SHARED_LIBRARY &&
  2076. type != cmStateEnums::MODULE_LIBRARY) {
  2077. std::ostringstream e;
  2078. e << "Objects of target \"" << tgtName
  2079. << "\" referenced but is not one of the allowed target types "
  2080. << "(EXECUTABLE, SHARED, MODULE).";
  2081. reportError(context, content->GetOriginalExpression(), e.str());
  2082. return std::string();
  2083. }
  2084. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  2085. std::vector<std::string> dllPaths;
  2086. auto const& dlls = cli->GetRuntimeDLLs();
  2087. for (auto const& dll : dlls) {
  2088. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  2089. dllPaths.emplace_back(*loc);
  2090. }
  2091. }
  2092. return cmJoin(dllPaths, ";");
  2093. }
  2094. return "";
  2095. }
  2096. } targetRuntimeDllsNode;
  2097. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  2098. {
  2099. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  2100. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2101. std::string Evaluate(
  2102. const std::vector<std::string>& parameters,
  2103. cmGeneratorExpressionContext* context,
  2104. const GeneratorExpressionContent* content,
  2105. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2106. {
  2107. cmGeneratorTarget const* target = context->HeadTarget;
  2108. if (!target) {
  2109. reportError(
  2110. context, content->GetOriginalExpression(),
  2111. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  2112. "not be used with add_custom_command or add_custom_target.");
  2113. return std::string();
  2114. }
  2115. context->HadHeadSensitiveCondition = true;
  2116. using LangMap = std::map<std::string, std::vector<std::string>>;
  2117. static LangMap availableFeatures;
  2118. LangMap testedFeatures;
  2119. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  2120. for (std::string const& p : parameters) {
  2121. std::string error;
  2122. std::string lang;
  2123. if (!standardResolver.CompileFeatureKnown(
  2124. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  2125. reportError(context, content->GetOriginalExpression(), error);
  2126. return std::string();
  2127. }
  2128. testedFeatures[lang].push_back(p);
  2129. if (availableFeatures.find(lang) == availableFeatures.end()) {
  2130. cmValue featuresKnown =
  2131. standardResolver.CompileFeaturesAvailable(lang, &error);
  2132. if (!featuresKnown) {
  2133. reportError(context, content->GetOriginalExpression(), error);
  2134. return std::string();
  2135. }
  2136. cmExpandList(featuresKnown, availableFeatures[lang]);
  2137. }
  2138. }
  2139. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  2140. for (auto const& lit : testedFeatures) {
  2141. std::vector<std::string> const& langAvailable =
  2142. availableFeatures[lit.first];
  2143. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  2144. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  2145. for (std::string const& it : lit.second) {
  2146. if (!cm::contains(langAvailable, it)) {
  2147. return "0";
  2148. }
  2149. if (standardDefault && standardDefault->empty()) {
  2150. // This compiler has no notion of language standard levels.
  2151. // All features known for the language are always available.
  2152. continue;
  2153. }
  2154. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  2155. context->Config, it)) {
  2156. if (evalLL) {
  2157. cmValue l =
  2158. target->GetLanguageStandard(lit.first, context->Config);
  2159. if (!l) {
  2160. l = standardDefault;
  2161. }
  2162. assert(l);
  2163. context->MaxLanguageStandard[target][lit.first] = *l;
  2164. } else {
  2165. return "0";
  2166. }
  2167. }
  2168. }
  2169. }
  2170. return "1";
  2171. }
  2172. } compileFeaturesNode;
  2173. static const char* targetPolicyWhitelist[] = {
  2174. nullptr
  2175. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  2176. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  2177. #undef TARGET_POLICY_STRING
  2178. };
  2179. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  2180. const char* policy)
  2181. {
  2182. #define RETURN_POLICY(POLICY) \
  2183. if (strcmp(policy, #POLICY) == 0) { \
  2184. return tgt->GetPolicyStatus##POLICY(); \
  2185. }
  2186. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  2187. #undef RETURN_POLICY
  2188. assert(false && "Unreachable code. Not a valid policy");
  2189. return cmPolicies::WARN;
  2190. }
  2191. static cmPolicies::PolicyID policyForString(const char* policy_id)
  2192. {
  2193. #define RETURN_POLICY_ID(POLICY_ID) \
  2194. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  2195. return cmPolicies::POLICY_ID; \
  2196. }
  2197. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  2198. #undef RETURN_POLICY_ID
  2199. assert(false && "Unreachable code. Not a valid policy");
  2200. return cmPolicies::CMP0002;
  2201. }
  2202. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  2203. {
  2204. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  2205. int NumExpectedParameters() const override { return 1; }
  2206. std::string Evaluate(
  2207. const std::vector<std::string>& parameters,
  2208. cmGeneratorExpressionContext* context,
  2209. const GeneratorExpressionContent* content,
  2210. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2211. {
  2212. if (!context->HeadTarget) {
  2213. reportError(
  2214. context, content->GetOriginalExpression(),
  2215. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  2216. "may not be used with add_custom_command or add_custom_target.");
  2217. return std::string();
  2218. }
  2219. context->HadContextSensitiveCondition = true;
  2220. context->HadHeadSensitiveCondition = true;
  2221. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  2222. const char* policy = targetPolicyWhitelist[i];
  2223. if (parameters.front() == policy) {
  2224. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  2225. switch (statusForTarget(context->HeadTarget, policy)) {
  2226. case cmPolicies::WARN:
  2227. lg->IssueMessage(
  2228. MessageType::AUTHOR_WARNING,
  2229. cmPolicies::GetPolicyWarning(policyForString(policy)));
  2230. CM_FALLTHROUGH;
  2231. case cmPolicies::REQUIRED_IF_USED:
  2232. case cmPolicies::REQUIRED_ALWAYS:
  2233. case cmPolicies::OLD:
  2234. return "0";
  2235. case cmPolicies::NEW:
  2236. return "1";
  2237. }
  2238. }
  2239. }
  2240. reportError(
  2241. context, content->GetOriginalExpression(),
  2242. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  2243. "policies. Currently it may be used with the following policies:\n"
  2244. #define STRINGIFY_HELPER(X) #X
  2245. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  2246. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  2247. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  2248. #undef TARGET_POLICY_LIST_ITEM
  2249. );
  2250. return std::string();
  2251. }
  2252. } targetPolicyNode;
  2253. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  2254. {
  2255. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  2256. bool GeneratesContent() const override { return true; }
  2257. int NumExpectedParameters() const override { return 0; }
  2258. std::string Evaluate(
  2259. const std::vector<std::string>& /*parameters*/,
  2260. cmGeneratorExpressionContext* context,
  2261. const GeneratorExpressionContent* content,
  2262. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2263. {
  2264. reportError(context, content->GetOriginalExpression(),
  2265. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  2266. "should never be evaluated.");
  2267. return std::string();
  2268. }
  2269. } installPrefixNode;
  2270. class ArtifactDirTag;
  2271. class ArtifactLinkerTag;
  2272. class ArtifactNameTag;
  2273. class ArtifactPathTag;
  2274. class ArtifactPdbTag;
  2275. class ArtifactSonameTag;
  2276. class ArtifactBundleDirTag;
  2277. class ArtifactBundleDirNameTag;
  2278. class ArtifactBundleContentDirTag;
  2279. template <typename ArtifactT, typename ComponentT>
  2280. struct TargetFilesystemArtifactDependency
  2281. {
  2282. static void AddDependency(cmGeneratorTarget* target,
  2283. cmGeneratorExpressionContext* context)
  2284. {
  2285. context->DependTargets.insert(target);
  2286. context->AllTargets.insert(target);
  2287. }
  2288. };
  2289. struct TargetFilesystemArtifactDependencyCMP0112
  2290. {
  2291. static void AddDependency(cmGeneratorTarget* target,
  2292. cmGeneratorExpressionContext* context)
  2293. {
  2294. context->AllTargets.insert(target);
  2295. cmLocalGenerator* lg = context->LG;
  2296. switch (target->GetPolicyStatusCMP0112()) {
  2297. case cmPolicies::WARN:
  2298. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  2299. "CMAKE_POLICY_WARNING_CMP0112")) {
  2300. std::string err =
  2301. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  2302. "\nDependency being added to target:\n \"",
  2303. target->GetName(), "\"\n");
  2304. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  2305. err, context->Backtrace);
  2306. }
  2307. CM_FALLTHROUGH;
  2308. case cmPolicies::OLD:
  2309. context->DependTargets.insert(target);
  2310. break;
  2311. case cmPolicies::REQUIRED_IF_USED:
  2312. case cmPolicies::REQUIRED_ALWAYS:
  2313. case cmPolicies::NEW:
  2314. break;
  2315. }
  2316. }
  2317. };
  2318. template <typename ArtifactT>
  2319. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  2320. : TargetFilesystemArtifactDependencyCMP0112
  2321. {
  2322. };
  2323. template <typename ArtifactT>
  2324. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  2325. : TargetFilesystemArtifactDependencyCMP0112
  2326. {
  2327. };
  2328. template <>
  2329. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  2330. ArtifactPathTag>
  2331. : TargetFilesystemArtifactDependencyCMP0112
  2332. {
  2333. };
  2334. template <>
  2335. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  2336. ArtifactPathTag>
  2337. : TargetFilesystemArtifactDependencyCMP0112
  2338. {
  2339. };
  2340. template <>
  2341. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  2342. ArtifactPathTag>
  2343. : TargetFilesystemArtifactDependencyCMP0112
  2344. {
  2345. };
  2346. template <typename ArtifactT>
  2347. struct TargetFilesystemArtifactResultCreator
  2348. {
  2349. static std::string Create(cmGeneratorTarget* target,
  2350. cmGeneratorExpressionContext* context,
  2351. const GeneratorExpressionContent* content);
  2352. };
  2353. template <>
  2354. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  2355. {
  2356. static std::string Create(cmGeneratorTarget* target,
  2357. cmGeneratorExpressionContext* context,
  2358. const GeneratorExpressionContent* content)
  2359. {
  2360. // The target soname file (.so.1).
  2361. if (target->IsDLLPlatform()) {
  2362. ::reportError(context, content->GetOriginalExpression(),
  2363. "TARGET_SONAME_FILE is not allowed "
  2364. "for DLL target platforms.");
  2365. return std::string();
  2366. }
  2367. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2368. ::reportError(context, content->GetOriginalExpression(),
  2369. "TARGET_SONAME_FILE is allowed only for "
  2370. "SHARED libraries.");
  2371. return std::string();
  2372. }
  2373. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  2374. target->GetSOName(context->Config));
  2375. return result;
  2376. }
  2377. };
  2378. template <>
  2379. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  2380. {
  2381. static std::string Create(cmGeneratorTarget* target,
  2382. cmGeneratorExpressionContext* context,
  2383. const GeneratorExpressionContent* content)
  2384. {
  2385. if (target->IsImported()) {
  2386. ::reportError(context, content->GetOriginalExpression(),
  2387. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  2388. return std::string();
  2389. }
  2390. std::string language = target->GetLinkerLanguage(context->Config);
  2391. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2392. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2393. ::reportError(context, content->GetOriginalExpression(),
  2394. "TARGET_PDB_FILE is not supported by the target linker.");
  2395. return std::string();
  2396. }
  2397. cmStateEnums::TargetType targetType = target->GetType();
  2398. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2399. targetType != cmStateEnums::MODULE_LIBRARY &&
  2400. targetType != cmStateEnums::EXECUTABLE) {
  2401. ::reportError(context, content->GetOriginalExpression(),
  2402. "TARGET_PDB_FILE is allowed only for "
  2403. "targets with linker created artifacts.");
  2404. return std::string();
  2405. }
  2406. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  2407. '/', target->GetPDBName(context->Config));
  2408. return result;
  2409. }
  2410. };
  2411. template <>
  2412. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  2413. {
  2414. static std::string Create(cmGeneratorTarget* target,
  2415. cmGeneratorExpressionContext* context,
  2416. const GeneratorExpressionContent* content)
  2417. {
  2418. // The file used to link to the target (.so, .lib, .a).
  2419. if (!target->IsLinkable()) {
  2420. ::reportError(context, content->GetOriginalExpression(),
  2421. "TARGET_LINKER_FILE is allowed only for libraries and "
  2422. "executables with ENABLE_EXPORTS.");
  2423. return std::string();
  2424. }
  2425. cmStateEnums::ArtifactType artifact =
  2426. target->HasImportLibrary(context->Config)
  2427. ? cmStateEnums::ImportLibraryArtifact
  2428. : cmStateEnums::RuntimeBinaryArtifact;
  2429. return target->GetFullPath(context->Config, artifact);
  2430. }
  2431. };
  2432. template <>
  2433. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  2434. {
  2435. static std::string Create(cmGeneratorTarget* target,
  2436. cmGeneratorExpressionContext* context,
  2437. const GeneratorExpressionContent* content)
  2438. {
  2439. if (target->IsImported()) {
  2440. ::reportError(context, content->GetOriginalExpression(),
  2441. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  2442. return std::string();
  2443. }
  2444. if (!target->IsBundleOnApple()) {
  2445. ::reportError(context, content->GetOriginalExpression(),
  2446. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  2447. return std::string();
  2448. }
  2449. std::string outpath = target->GetDirectory(context->Config) + '/';
  2450. return target->BuildBundleDirectory(outpath, context->Config,
  2451. cmGeneratorTarget::BundleDirLevel);
  2452. }
  2453. };
  2454. template <>
  2455. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  2456. {
  2457. static std::string Create(cmGeneratorTarget* target,
  2458. cmGeneratorExpressionContext* context,
  2459. const GeneratorExpressionContent* content)
  2460. {
  2461. if (target->IsImported()) {
  2462. ::reportError(
  2463. context, content->GetOriginalExpression(),
  2464. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  2465. return std::string();
  2466. }
  2467. if (!target->IsBundleOnApple()) {
  2468. ::reportError(
  2469. context, content->GetOriginalExpression(),
  2470. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  2471. return std::string();
  2472. }
  2473. auto level = cmGeneratorTarget::BundleDirLevel;
  2474. auto config = context->Config;
  2475. if (target->IsAppBundleOnApple()) {
  2476. return target->GetAppBundleDirectory(config, level);
  2477. }
  2478. if (target->IsFrameworkOnApple()) {
  2479. return target->GetFrameworkDirectory(config, level);
  2480. }
  2481. if (target->IsCFBundleOnApple()) {
  2482. return target->GetCFBundleDirectory(config, level);
  2483. }
  2484. return std::string();
  2485. }
  2486. };
  2487. template <>
  2488. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  2489. {
  2490. static std::string Create(cmGeneratorTarget* target,
  2491. cmGeneratorExpressionContext* context,
  2492. const GeneratorExpressionContent* content)
  2493. {
  2494. if (target->IsImported()) {
  2495. ::reportError(
  2496. context, content->GetOriginalExpression(),
  2497. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  2498. return std::string();
  2499. }
  2500. if (!target->IsBundleOnApple()) {
  2501. ::reportError(
  2502. context, content->GetOriginalExpression(),
  2503. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  2504. return std::string();
  2505. }
  2506. std::string outpath = target->GetDirectory(context->Config) + '/';
  2507. return target->BuildBundleDirectory(outpath, context->Config,
  2508. cmGeneratorTarget::ContentLevel);
  2509. }
  2510. };
  2511. template <>
  2512. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  2513. {
  2514. static std::string Create(cmGeneratorTarget* target,
  2515. cmGeneratorExpressionContext* context,
  2516. const GeneratorExpressionContent* /*unused*/)
  2517. {
  2518. return target->GetFullPath(context->Config,
  2519. cmStateEnums::RuntimeBinaryArtifact, true);
  2520. }
  2521. };
  2522. template <typename ArtifactT>
  2523. struct TargetFilesystemArtifactResultGetter
  2524. {
  2525. static std::string Get(const std::string& result);
  2526. };
  2527. template <>
  2528. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  2529. {
  2530. static std::string Get(const std::string& result)
  2531. {
  2532. return cmSystemTools::GetFilenameName(result);
  2533. }
  2534. };
  2535. template <>
  2536. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  2537. {
  2538. static std::string Get(const std::string& result)
  2539. {
  2540. return cmSystemTools::GetFilenamePath(result);
  2541. }
  2542. };
  2543. template <>
  2544. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  2545. {
  2546. static std::string Get(const std::string& result) { return result; }
  2547. };
  2548. struct TargetArtifactBase : public cmGeneratorExpressionNode
  2549. {
  2550. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  2551. protected:
  2552. cmGeneratorTarget* GetTarget(
  2553. const std::vector<std::string>& parameters,
  2554. cmGeneratorExpressionContext* context,
  2555. const GeneratorExpressionContent* content,
  2556. cmGeneratorExpressionDAGChecker* dagChecker) const
  2557. {
  2558. // Lookup the referenced target.
  2559. std::string const& name = parameters.front();
  2560. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  2561. ::reportError(context, content->GetOriginalExpression(),
  2562. "Expression syntax not recognized.");
  2563. return nullptr;
  2564. }
  2565. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  2566. if (!target) {
  2567. ::reportError(context, content->GetOriginalExpression(),
  2568. "No target \"" + name + "\"");
  2569. return nullptr;
  2570. }
  2571. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  2572. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  2573. ::reportError(context, content->GetOriginalExpression(),
  2574. "Target \"" + name +
  2575. "\" is not an executable or library.");
  2576. return nullptr;
  2577. }
  2578. if (dagChecker &&
  2579. (dagChecker->EvaluatingLinkLibraries(target) ||
  2580. (dagChecker->EvaluatingSources() &&
  2581. target == dagChecker->TopTarget()))) {
  2582. ::reportError(context, content->GetOriginalExpression(),
  2583. "Expressions which require the linker language may not "
  2584. "be used while evaluating link libraries");
  2585. return nullptr;
  2586. }
  2587. return target;
  2588. }
  2589. };
  2590. template <typename ArtifactT, typename ComponentT>
  2591. struct TargetFilesystemArtifact : public TargetArtifactBase
  2592. {
  2593. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  2594. int NumExpectedParameters() const override { return 1; }
  2595. std::string Evaluate(
  2596. const std::vector<std::string>& parameters,
  2597. cmGeneratorExpressionContext* context,
  2598. const GeneratorExpressionContent* content,
  2599. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2600. {
  2601. cmGeneratorTarget* target =
  2602. this->GetTarget(parameters, context, content, dagChecker);
  2603. if (!target) {
  2604. return std::string();
  2605. }
  2606. // Not a dependent target if we are querying for ArtifactDirTag,
  2607. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  2608. // and ArtifactBundleContentDirTag
  2609. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  2610. target, context);
  2611. std::string result =
  2612. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  2613. content);
  2614. if (context->HadError) {
  2615. return std::string();
  2616. }
  2617. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2618. }
  2619. };
  2620. template <typename ArtifactT>
  2621. struct TargetFilesystemArtifactNodeGroup
  2622. {
  2623. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2624. {
  2625. }
  2626. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2627. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2628. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2629. };
  2630. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2631. targetNodeGroup;
  2632. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2633. targetLinkerNodeGroup;
  2634. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2635. targetSoNameNodeGroup;
  2636. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2637. targetPdbNodeGroup;
  2638. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2639. targetBundleDirNode;
  2640. static const TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  2641. ArtifactNameTag>
  2642. targetBundleDirNameNode;
  2643. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2644. ArtifactPathTag>
  2645. targetBundleContentDirNode;
  2646. //
  2647. // To retrieve base name for various artifacts
  2648. //
  2649. template <typename ArtifactT>
  2650. struct TargetOutputNameArtifactResultGetter
  2651. {
  2652. static std::string Get(cmGeneratorTarget* target,
  2653. cmGeneratorExpressionContext* context,
  2654. const GeneratorExpressionContent* content);
  2655. };
  2656. template <>
  2657. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2658. {
  2659. static std::string Get(cmGeneratorTarget* target,
  2660. cmGeneratorExpressionContext* context,
  2661. const GeneratorExpressionContent* /*unused*/)
  2662. {
  2663. return target->GetOutputName(context->Config,
  2664. cmStateEnums::RuntimeBinaryArtifact) +
  2665. target->GetFilePostfix(context->Config);
  2666. }
  2667. };
  2668. template <>
  2669. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2670. {
  2671. static std::string Get(cmGeneratorTarget* target,
  2672. cmGeneratorExpressionContext* context,
  2673. const GeneratorExpressionContent* content)
  2674. {
  2675. // The file used to link to the target (.so, .lib, .a).
  2676. if (!target->IsLinkable()) {
  2677. ::reportError(context, content->GetOriginalExpression(),
  2678. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2679. "libraries and executables with ENABLE_EXPORTS.");
  2680. return std::string();
  2681. }
  2682. cmStateEnums::ArtifactType artifact =
  2683. target->HasImportLibrary(context->Config)
  2684. ? cmStateEnums::ImportLibraryArtifact
  2685. : cmStateEnums::RuntimeBinaryArtifact;
  2686. return target->GetOutputName(context->Config, artifact) +
  2687. target->GetFilePostfix(context->Config);
  2688. }
  2689. };
  2690. template <>
  2691. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2692. {
  2693. static std::string Get(cmGeneratorTarget* target,
  2694. cmGeneratorExpressionContext* context,
  2695. const GeneratorExpressionContent* content)
  2696. {
  2697. if (target->IsImported()) {
  2698. ::reportError(
  2699. context, content->GetOriginalExpression(),
  2700. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2701. return std::string();
  2702. }
  2703. std::string language = target->GetLinkerLanguage(context->Config);
  2704. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2705. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2706. ::reportError(
  2707. context, content->GetOriginalExpression(),
  2708. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2709. return std::string();
  2710. }
  2711. cmStateEnums::TargetType targetType = target->GetType();
  2712. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2713. targetType != cmStateEnums::MODULE_LIBRARY &&
  2714. targetType != cmStateEnums::EXECUTABLE) {
  2715. ::reportError(context, content->GetOriginalExpression(),
  2716. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2717. "targets with linker created artifacts.");
  2718. return std::string();
  2719. }
  2720. return target->GetPDBOutputName(context->Config) +
  2721. target->GetFilePostfix(context->Config);
  2722. }
  2723. };
  2724. template <typename ArtifactT>
  2725. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2726. {
  2727. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2728. int NumExpectedParameters() const override { return 1; }
  2729. std::string Evaluate(
  2730. const std::vector<std::string>& parameters,
  2731. cmGeneratorExpressionContext* context,
  2732. const GeneratorExpressionContent* content,
  2733. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2734. {
  2735. cmGeneratorTarget* target =
  2736. this->GetTarget(parameters, context, content, dagChecker);
  2737. if (!target) {
  2738. return std::string();
  2739. }
  2740. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2741. target, context, content);
  2742. if (context->HadError) {
  2743. return std::string();
  2744. }
  2745. return result;
  2746. }
  2747. };
  2748. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2749. targetFileBaseNameNode;
  2750. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2751. targetLinkerFileBaseNameNode;
  2752. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2753. targetPdbFileBaseNameNode;
  2754. class ArtifactFilePrefixTag;
  2755. class ArtifactLinkerFilePrefixTag;
  2756. class ArtifactFileSuffixTag;
  2757. class ArtifactLinkerFileSuffixTag;
  2758. template <typename ArtifactT>
  2759. struct TargetFileArtifactResultGetter
  2760. {
  2761. static std::string Get(cmGeneratorTarget* target,
  2762. cmGeneratorExpressionContext* context,
  2763. const GeneratorExpressionContent* content);
  2764. };
  2765. template <>
  2766. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2767. {
  2768. static std::string Get(cmGeneratorTarget* target,
  2769. cmGeneratorExpressionContext* context,
  2770. const GeneratorExpressionContent*)
  2771. {
  2772. return target->GetFilePrefix(context->Config);
  2773. }
  2774. };
  2775. template <>
  2776. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2777. {
  2778. static std::string Get(cmGeneratorTarget* target,
  2779. cmGeneratorExpressionContext* context,
  2780. const GeneratorExpressionContent* content)
  2781. {
  2782. if (!target->IsLinkable()) {
  2783. ::reportError(context, content->GetOriginalExpression(),
  2784. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2785. "executables with ENABLE_EXPORTS.");
  2786. return std::string();
  2787. }
  2788. cmStateEnums::ArtifactType artifact =
  2789. target->HasImportLibrary(context->Config)
  2790. ? cmStateEnums::ImportLibraryArtifact
  2791. : cmStateEnums::RuntimeBinaryArtifact;
  2792. return target->GetFilePrefix(context->Config, artifact);
  2793. }
  2794. };
  2795. template <>
  2796. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2797. {
  2798. static std::string Get(cmGeneratorTarget* target,
  2799. cmGeneratorExpressionContext* context,
  2800. const GeneratorExpressionContent*)
  2801. {
  2802. return target->GetFileSuffix(context->Config);
  2803. }
  2804. };
  2805. template <>
  2806. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2807. {
  2808. static std::string Get(cmGeneratorTarget* target,
  2809. cmGeneratorExpressionContext* context,
  2810. const GeneratorExpressionContent* content)
  2811. {
  2812. if (!target->IsLinkable()) {
  2813. ::reportError(context, content->GetOriginalExpression(),
  2814. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2815. "executables with ENABLE_EXPORTS.");
  2816. return std::string();
  2817. }
  2818. cmStateEnums::ArtifactType artifact =
  2819. target->HasImportLibrary(context->Config)
  2820. ? cmStateEnums::ImportLibraryArtifact
  2821. : cmStateEnums::RuntimeBinaryArtifact;
  2822. return target->GetFileSuffix(context->Config, artifact);
  2823. }
  2824. };
  2825. template <typename ArtifactT>
  2826. struct TargetFileArtifact : public TargetArtifactBase
  2827. {
  2828. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2829. int NumExpectedParameters() const override { return 1; }
  2830. std::string Evaluate(
  2831. const std::vector<std::string>& parameters,
  2832. cmGeneratorExpressionContext* context,
  2833. const GeneratorExpressionContent* content,
  2834. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2835. {
  2836. cmGeneratorTarget* target =
  2837. this->GetTarget(parameters, context, content, dagChecker);
  2838. if (!target) {
  2839. return std::string();
  2840. }
  2841. std::string result =
  2842. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2843. if (context->HadError) {
  2844. return std::string();
  2845. }
  2846. return result;
  2847. }
  2848. };
  2849. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2850. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2851. targetLinkerFilePrefixNode;
  2852. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2853. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2854. targetLinkerFileSuffixNode;
  2855. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2856. {
  2857. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2858. std::string Evaluate(
  2859. const std::vector<std::string>& parameters,
  2860. cmGeneratorExpressionContext* context,
  2861. const GeneratorExpressionContent* content,
  2862. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2863. {
  2864. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2865. if (listIn.empty()) {
  2866. reportError(context, content->GetOriginalExpression(),
  2867. "\"\" is not an absolute path.");
  2868. return std::string();
  2869. }
  2870. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2871. cmOutputConverter converter(snapshot);
  2872. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2873. std::vector<std::string> listOut;
  2874. listOut.reserve(listIn.size());
  2875. for (auto const& in : listIn) {
  2876. if (!cmSystemTools::FileIsFullPath(in)) {
  2877. reportError(context, content->GetOriginalExpression(),
  2878. "\"" + in + "\" is not an absolute path.");
  2879. return std::string();
  2880. }
  2881. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2882. }
  2883. return cmJoin(listOut, separator);
  2884. }
  2885. } shellPathNode;
  2886. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2887. const std::string& identifier)
  2888. {
  2889. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2890. { "0", &zeroNode },
  2891. { "1", &oneNode },
  2892. { "AND", &andNode },
  2893. { "OR", &orNode },
  2894. { "NOT", &notNode },
  2895. { "C_COMPILER_ID", &cCompilerIdNode },
  2896. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2897. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2898. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2899. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2900. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2901. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2902. { "VERSION_GREATER", &versionGreaterNode },
  2903. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2904. { "VERSION_LESS", &versionLessNode },
  2905. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2906. { "VERSION_EQUAL", &versionEqualNode },
  2907. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2908. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2909. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2910. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2911. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2912. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2913. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2914. { "PLATFORM_ID", &platformIdNode },
  2915. { "COMPILE_FEATURES", &compileFeaturesNode },
  2916. { "CONFIGURATION", &configurationNode },
  2917. { "CONFIG", &configurationTestNode },
  2918. { "TARGET_FILE", &targetNodeGroup.File },
  2919. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2920. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2921. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2922. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2923. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2924. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2925. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2926. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2927. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2928. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2929. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2930. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2931. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2932. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2933. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2934. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2935. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2936. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2937. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2938. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  2939. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2940. { "STREQUAL", &strEqualNode },
  2941. { "EQUAL", &equalNode },
  2942. { "IN_LIST", &inListNode },
  2943. { "FILTER", &filterNode },
  2944. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2945. { "LOWER_CASE", &lowerCaseNode },
  2946. { "UPPER_CASE", &upperCaseNode },
  2947. { "PATH", &pathNode },
  2948. { "PATH_EQUAL", &pathEqualNode },
  2949. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2950. { "BOOL", &boolNode },
  2951. { "IF", &ifNode },
  2952. { "ANGLE-R", &angle_rNode },
  2953. { "COMMA", &commaNode },
  2954. { "SEMICOLON", &semicolonNode },
  2955. { "TARGET_PROPERTY", &targetPropertyNode },
  2956. { "TARGET_NAME", &targetNameNode },
  2957. { "TARGET_OBJECTS", &targetObjectsNode },
  2958. { "TARGET_POLICY", &targetPolicyNode },
  2959. { "TARGET_EXISTS", &targetExistsNode },
  2960. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2961. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2962. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2963. { "GENEX_EVAL", &genexEvalNode },
  2964. { "BUILD_INTERFACE", &buildInterfaceNode },
  2965. { "INSTALL_INTERFACE", &installInterfaceNode },
  2966. { "INSTALL_PREFIX", &installPrefixNode },
  2967. { "JOIN", &joinNode },
  2968. { "LINK_ONLY", &linkOnlyNode },
  2969. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2970. { "COMPILE_LANGUAGE", &languageNode },
  2971. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2972. { "LINK_LANGUAGE", &linkLanguageNode },
  2973. { "LINK_LIBRARY", &linkLibraryNode },
  2974. { "LINK_GROUP", &linkGroupNode },
  2975. { "HOST_LINK", &hostLinkNode },
  2976. { "DEVICE_LINK", &deviceLinkNode },
  2977. { "SHELL_PATH", &shellPathNode }
  2978. };
  2979. {
  2980. auto itr = nodeMap.find(identifier);
  2981. if (itr != nodeMap.end()) {
  2982. return itr->second;
  2983. }
  2984. }
  2985. return nullptr;
  2986. }
  2987. void reportError(cmGeneratorExpressionContext* context,
  2988. const std::string& expr, const std::string& result)
  2989. {
  2990. context->HadError = true;
  2991. if (context->Quiet) {
  2992. return;
  2993. }
  2994. std::ostringstream e;
  2995. /* clang-format off */
  2996. e << "Error evaluating generator expression:\n"
  2997. << " " << expr << "\n"
  2998. << result;
  2999. /* clang-format on */
  3000. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  3001. e.str(), context->Backtrace);
  3002. }