cmGeneratorExpressionNode.cxx 119 KB

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