cmGeneratorExpressionNode.cxx 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437
  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. }
  1210. context->HadContextSensitiveCondition = true;
  1211. bool firstParam = true;
  1212. for (auto const& param : parameters) {
  1213. if (!configValidator.find(param)) {
  1214. if (firstParam) {
  1215. reportError(context, content->GetOriginalExpression(),
  1216. "Expression syntax not recognized.");
  1217. return std::string();
  1218. }
  1219. // for backwards compat invalid config names are only errors as
  1220. // the first parameter
  1221. std::ostringstream e;
  1222. /* clang-format off */
  1223. e << "Warning evaluating generator expression:\n"
  1224. << " " << content->GetOriginalExpression() << "\n"
  1225. << "The config name of \"" << param << "\" is invalid";
  1226. /* clang-format on */
  1227. context->LG->GetCMakeInstance()->IssueMessage(
  1228. MessageType::WARNING, e.str(), context->Backtrace);
  1229. }
  1230. firstParam = false;
  1231. if (context->Config.empty()) {
  1232. if (param.empty()) {
  1233. return "1";
  1234. }
  1235. } else if (cmsysString_strcasecmp(param.c_str(),
  1236. context->Config.c_str()) == 0) {
  1237. return "1";
  1238. }
  1239. }
  1240. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  1241. cmValue loc = nullptr;
  1242. cmValue imp = nullptr;
  1243. std::string suffix;
  1244. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  1245. imp, suffix)) {
  1246. // This imported target has an appropriate location
  1247. // for this (possibly mapped) config.
  1248. // Check if there is a proper config mapping for the tested config.
  1249. std::vector<std::string> mappedConfigs;
  1250. std::string mapProp = cmStrCat(
  1251. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  1252. if (cmValue mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  1253. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  1254. for (auto const& param : parameters) {
  1255. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  1256. return "1";
  1257. }
  1258. }
  1259. } else if (!suffix.empty()) {
  1260. // There is no explicit mapping for the tested config, so use
  1261. // the configuration of the imported location that was selected.
  1262. for (auto const& param : parameters) {
  1263. if (cmStrCat('_', cmSystemTools::UpperCase(param)) == suffix) {
  1264. return "1";
  1265. }
  1266. }
  1267. }
  1268. }
  1269. }
  1270. return "0";
  1271. }
  1272. } configurationTestNode;
  1273. static const struct JoinNode : public cmGeneratorExpressionNode
  1274. {
  1275. JoinNode() {} // NOLINT(modernize-use-equals-default)
  1276. int NumExpectedParameters() const override { return 2; }
  1277. bool AcceptsArbitraryContentParameter() const override { return true; }
  1278. std::string Evaluate(
  1279. const std::vector<std::string>& parameters,
  1280. cmGeneratorExpressionContext* /*context*/,
  1281. const GeneratorExpressionContent* /*content*/,
  1282. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1283. {
  1284. std::vector<std::string> list = cmExpandedList(parameters.front());
  1285. return cmJoin(list, parameters[1]);
  1286. }
  1287. } joinNode;
  1288. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  1289. {
  1290. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1291. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1292. std::string Evaluate(
  1293. const std::vector<std::string>& parameters,
  1294. cmGeneratorExpressionContext* context,
  1295. const GeneratorExpressionContent* content,
  1296. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1297. {
  1298. if (context->Language.empty() &&
  1299. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  1300. reportError(
  1301. context, content->GetOriginalExpression(),
  1302. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  1303. "directories, compile definitions, compile options, and to evaluate "
  1304. "components of the file(GENERATE) command.");
  1305. return std::string();
  1306. }
  1307. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1308. std::string genName = gg->GetName();
  1309. if (genName.find("Makefiles") == std::string::npos &&
  1310. genName.find("Ninja") == std::string::npos &&
  1311. genName.find("Visual Studio") == std::string::npos &&
  1312. genName.find("Xcode") == std::string::npos &&
  1313. genName.find("Watcom WMake") == std::string::npos &&
  1314. genName.find("Green Hills MULTI") == std::string::npos) {
  1315. reportError(context, content->GetOriginalExpression(),
  1316. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  1317. return std::string();
  1318. }
  1319. if (parameters.empty()) {
  1320. return context->Language;
  1321. }
  1322. for (auto const& param : parameters) {
  1323. if (context->Language == param) {
  1324. return "1";
  1325. }
  1326. }
  1327. return "0";
  1328. }
  1329. } languageNode;
  1330. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  1331. {
  1332. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1333. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1334. std::string Evaluate(
  1335. const std::vector<std::string>& parameters,
  1336. cmGeneratorExpressionContext* context,
  1337. const GeneratorExpressionContent* content,
  1338. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1339. {
  1340. if (!context->HeadTarget ||
  1341. (context->Language.empty() &&
  1342. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  1343. // reportError(context, content->GetOriginalExpression(), "");
  1344. reportError(
  1345. context, content->GetOriginalExpression(),
  1346. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  1347. "to specify include directories, compile definitions, and compile "
  1348. "options. It may not be used with the add_custom_command, "
  1349. "add_custom_target, or file(GENERATE) commands.");
  1350. return std::string();
  1351. }
  1352. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1353. std::string genName = gg->GetName();
  1354. if (genName.find("Makefiles") == std::string::npos &&
  1355. genName.find("Ninja") == std::string::npos &&
  1356. genName.find("Visual Studio") == std::string::npos &&
  1357. genName.find("Xcode") == std::string::npos &&
  1358. genName.find("Watcom WMake") == std::string::npos &&
  1359. genName.find("Green Hills MULTI") == std::string::npos) {
  1360. reportError(
  1361. context, content->GetOriginalExpression(),
  1362. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  1363. return std::string();
  1364. }
  1365. const std::string& lang = context->Language;
  1366. if (lang == parameters.front()) {
  1367. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1368. parameters.cend());
  1369. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  1370. idParameter, context, content, dagChecker, lang);
  1371. }
  1372. return "0";
  1373. }
  1374. } languageAndIdNode;
  1375. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  1376. {
  1377. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1378. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1379. std::string Evaluate(
  1380. const std::vector<std::string>& parameters,
  1381. cmGeneratorExpressionContext* context,
  1382. const GeneratorExpressionContent* content,
  1383. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1384. {
  1385. if (!context->HeadTarget || !dagChecker ||
  1386. !(dagChecker->EvaluatingLinkExpression() ||
  1387. dagChecker->EvaluatingLinkLibraries() ||
  1388. dagChecker->EvaluatingLinkerLauncher())) {
  1389. reportError(context, content->GetOriginalExpression(),
  1390. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  1391. "to specify link libraries, link directories, link options "
  1392. "and link depends.");
  1393. return std::string();
  1394. }
  1395. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  1396. reportError(
  1397. context, content->GetOriginalExpression(),
  1398. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  1399. return std::string();
  1400. }
  1401. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1402. std::string genName = gg->GetName();
  1403. if (genName.find("Makefiles") == std::string::npos &&
  1404. genName.find("Ninja") == std::string::npos &&
  1405. genName.find("Visual Studio") == std::string::npos &&
  1406. genName.find("Xcode") == std::string::npos &&
  1407. genName.find("Watcom WMake") == std::string::npos &&
  1408. genName.find("Green Hills MULTI") == std::string::npos) {
  1409. reportError(context, content->GetOriginalExpression(),
  1410. "$<LINK_LANGUAGE:...> not supported for this generator.");
  1411. return std::string();
  1412. }
  1413. if (dagChecker->EvaluatingLinkLibraries()) {
  1414. context->HadHeadSensitiveCondition = true;
  1415. context->HadLinkLanguageSensitiveCondition = true;
  1416. }
  1417. if (parameters.empty()) {
  1418. return context->Language;
  1419. }
  1420. for (auto const& param : parameters) {
  1421. if (context->Language == param) {
  1422. return "1";
  1423. }
  1424. }
  1425. return "0";
  1426. }
  1427. } linkLanguageNode;
  1428. namespace {
  1429. struct LinkerId
  1430. {
  1431. static std::string Evaluate(const std::vector<std::string>& parameters,
  1432. cmGeneratorExpressionContext* context,
  1433. const GeneratorExpressionContent* content,
  1434. const std::string& lang)
  1435. {
  1436. std::string const& linkerId =
  1437. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  1438. "_COMPILER_ID");
  1439. if (parameters.empty()) {
  1440. return linkerId;
  1441. }
  1442. if (linkerId.empty()) {
  1443. return parameters.front().empty() ? "1" : "0";
  1444. }
  1445. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  1446. for (auto const& param : parameters) {
  1447. if (!linkerIdValidator.find(param)) {
  1448. reportError(context, content->GetOriginalExpression(),
  1449. "Expression syntax not recognized.");
  1450. return std::string();
  1451. }
  1452. if (param == linkerId) {
  1453. return "1";
  1454. }
  1455. }
  1456. return "0";
  1457. }
  1458. };
  1459. }
  1460. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  1461. {
  1462. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  1463. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  1464. std::string Evaluate(
  1465. const std::vector<std::string>& parameters,
  1466. cmGeneratorExpressionContext* context,
  1467. const GeneratorExpressionContent* content,
  1468. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1469. {
  1470. if (!context->HeadTarget || !dagChecker ||
  1471. !(dagChecker->EvaluatingLinkExpression() ||
  1472. dagChecker->EvaluatingLinkLibraries() ||
  1473. dagChecker->EvaluatingLinkerLauncher())) {
  1474. reportError(
  1475. context, content->GetOriginalExpression(),
  1476. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1477. "to specify link libraries, link directories, link options, and link "
  1478. "depends.");
  1479. return std::string();
  1480. }
  1481. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1482. std::string genName = gg->GetName();
  1483. if (genName.find("Makefiles") == std::string::npos &&
  1484. genName.find("Ninja") == std::string::npos &&
  1485. genName.find("Visual Studio") == std::string::npos &&
  1486. genName.find("Xcode") == std::string::npos &&
  1487. genName.find("Watcom WMake") == std::string::npos &&
  1488. genName.find("Green Hills MULTI") == std::string::npos) {
  1489. reportError(
  1490. context, content->GetOriginalExpression(),
  1491. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1492. return std::string();
  1493. }
  1494. if (dagChecker->EvaluatingLinkLibraries()) {
  1495. context->HadHeadSensitiveCondition = true;
  1496. context->HadLinkLanguageSensitiveCondition = true;
  1497. }
  1498. const std::string& lang = context->Language;
  1499. if (lang == parameters.front()) {
  1500. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1501. parameters.cend());
  1502. return LinkerId::Evaluate(idParameter, context, content, lang);
  1503. }
  1504. return "0";
  1505. }
  1506. } linkLanguageAndIdNode;
  1507. static const struct LinkLibraryNode : public cmGeneratorExpressionNode
  1508. {
  1509. LinkLibraryNode() {} // NOLINT(modernize-use-equals-default)
  1510. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1511. std::string Evaluate(
  1512. const std::vector<std::string>& parameters,
  1513. cmGeneratorExpressionContext* context,
  1514. const GeneratorExpressionContent* content,
  1515. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1516. {
  1517. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  1518. if (!context->HeadTarget || !dagChecker ||
  1519. !dagChecker->EvaluatingLinkLibraries(nullptr,
  1520. ForGenex::LINK_LIBRARY)) {
  1521. reportError(context, content->GetOriginalExpression(),
  1522. "$<LINK_LIBRARY:...> may only be used with binary targets "
  1523. "to specify link libraries through 'LINK_LIBRARIES', "
  1524. "'INTERFACE_LINK_LIBRARIES', and "
  1525. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  1526. return std::string();
  1527. }
  1528. std::vector<std::string> list;
  1529. cmExpandLists(parameters.begin(), parameters.end(), list);
  1530. if (list.empty()) {
  1531. reportError(
  1532. context, content->GetOriginalExpression(),
  1533. "$<LINK_LIBRARY:...> expects a feature name as first argument.");
  1534. return std::string();
  1535. }
  1536. if (list.size() == 1) {
  1537. // no libraries specified, ignore this genex
  1538. return std::string();
  1539. }
  1540. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1541. auto const& feature = list.front();
  1542. if (!featureNameValidator.find(feature)) {
  1543. reportError(context, content->GetOriginalExpression(),
  1544. cmStrCat("The feature name '", feature,
  1545. "' contains invalid characters."));
  1546. return std::string();
  1547. }
  1548. const auto LL_BEGIN = cmStrCat("<LINK_LIBRARY:", feature, '>');
  1549. const auto LL_END = cmStrCat("</LINK_LIBRARY:", feature, '>');
  1550. // filter out $<LINK_LIBRARY:..> tags with same feature
  1551. // and raise an error for any different feature
  1552. cm::erase_if(list, [&](const std::string& item) -> bool {
  1553. return item == LL_BEGIN || item == LL_END;
  1554. });
  1555. auto it =
  1556. std::find_if(list.cbegin() + 1, list.cend(),
  1557. [&feature](const std::string& item) -> bool {
  1558. return cmHasPrefix(item, "<LINK_LIBRARY:"_s) &&
  1559. item.substr(14, item.find('>', 14) - 14) != feature;
  1560. });
  1561. if (it != list.cend()) {
  1562. reportError(
  1563. context, content->GetOriginalExpression(),
  1564. "$<LINK_LIBRARY:...> with different features cannot be nested.");
  1565. return std::string();
  1566. }
  1567. // $<LINK_GROUP:...> must not appear as part of $<LINK_LIBRARY:...>
  1568. it = std::find_if(list.cbegin() + 1, list.cend(),
  1569. [](const std::string& item) -> bool {
  1570. return cmHasPrefix(item, "<LINK_GROUP:"_s);
  1571. });
  1572. if (it != list.cend()) {
  1573. reportError(context, content->GetOriginalExpression(),
  1574. "$<LINK_GROUP:...> cannot be nested inside a "
  1575. "$<LINK_LIBRARY:...> expression.");
  1576. return std::string();
  1577. }
  1578. list.front() = LL_BEGIN;
  1579. list.push_back(LL_END);
  1580. return cmJoin(list, ";"_s);
  1581. }
  1582. } linkLibraryNode;
  1583. static const struct LinkGroupNode : public cmGeneratorExpressionNode
  1584. {
  1585. LinkGroupNode() {} // NOLINT(modernize-use-equals-default)
  1586. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1587. std::string Evaluate(
  1588. const std::vector<std::string>& parameters,
  1589. cmGeneratorExpressionContext* context,
  1590. const GeneratorExpressionContent* content,
  1591. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1592. {
  1593. using ForGenex = cmGeneratorExpressionDAGChecker::ForGenex;
  1594. if (!context->HeadTarget || !dagChecker ||
  1595. !dagChecker->EvaluatingLinkLibraries(nullptr, ForGenex::LINK_GROUP)) {
  1596. reportError(
  1597. context, content->GetOriginalExpression(),
  1598. "$<LINK_GROUP:...> may only be used with binary targets "
  1599. "to specify group of link libraries through 'LINK_LIBRARIES', "
  1600. "'INTERFACE_LINK_LIBRARIES', and "
  1601. "'INTERFACE_LINK_LIBRARIES_DIRECT' properties.");
  1602. return std::string();
  1603. }
  1604. std::vector<std::string> list;
  1605. cmExpandLists(parameters.begin(), parameters.end(), list);
  1606. if (list.empty()) {
  1607. reportError(
  1608. context, content->GetOriginalExpression(),
  1609. "$<LINK_GROUP:...> expects a feature name as first argument.");
  1610. return std::string();
  1611. }
  1612. // $<LINK_GROUP:..> cannot be nested
  1613. if (std::find_if(list.cbegin(), list.cend(),
  1614. [](const std::string& item) -> bool {
  1615. return cmHasPrefix(item, "<LINK_GROUP"_s);
  1616. }) != list.cend()) {
  1617. reportError(context, content->GetOriginalExpression(),
  1618. "$<LINK_GROUP:...> cannot be nested.");
  1619. return std::string();
  1620. }
  1621. if (list.size() == 1) {
  1622. // no libraries specified, ignore this genex
  1623. return std::string();
  1624. }
  1625. static cmsys::RegularExpression featureNameValidator("^[A-Za-z0-9_]+$");
  1626. auto const& feature = list.front();
  1627. if (!featureNameValidator.find(feature)) {
  1628. reportError(context, content->GetOriginalExpression(),
  1629. cmStrCat("The feature name '", feature,
  1630. "' contains invalid characters."));
  1631. return std::string();
  1632. }
  1633. const auto LG_BEGIN = cmStrCat(
  1634. "<LINK_GROUP:", feature, ':',
  1635. cmJoin(cmRange<decltype(list.cbegin())>(list.cbegin() + 1, list.cend()),
  1636. "|"_s),
  1637. '>');
  1638. const auto LG_END = cmStrCat("</LINK_GROUP:", feature, '>');
  1639. list.front() = LG_BEGIN;
  1640. list.push_back(LG_END);
  1641. return cmJoin(list, ";"_s);
  1642. }
  1643. } linkGroupNode;
  1644. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1645. {
  1646. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1647. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1648. std::string Evaluate(
  1649. const std::vector<std::string>& parameters,
  1650. cmGeneratorExpressionContext* context,
  1651. const GeneratorExpressionContent* content,
  1652. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1653. {
  1654. if (!context->HeadTarget || !dagChecker ||
  1655. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1656. reportError(context, content->GetOriginalExpression(),
  1657. "$<HOST_LINK:...> may only be used with binary targets "
  1658. "to specify link options.");
  1659. return std::string();
  1660. }
  1661. return context->HeadTarget->IsDeviceLink() ? std::string()
  1662. : cmJoin(parameters, ";");
  1663. }
  1664. } hostLinkNode;
  1665. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1666. {
  1667. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1668. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1669. std::string Evaluate(
  1670. const std::vector<std::string>& parameters,
  1671. cmGeneratorExpressionContext* context,
  1672. const GeneratorExpressionContent* content,
  1673. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1674. {
  1675. if (!context->HeadTarget || !dagChecker ||
  1676. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1677. reportError(context, content->GetOriginalExpression(),
  1678. "$<DEVICE_LINK:...> may only be used with binary targets "
  1679. "to specify link options.");
  1680. return std::string();
  1681. }
  1682. if (context->HeadTarget->IsDeviceLink()) {
  1683. std::vector<std::string> list;
  1684. cmExpandLists(parameters.begin(), parameters.end(), list);
  1685. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1686. const auto DL_END = "</DEVICE_LINK>"_s;
  1687. cm::erase_if(list, [&](const std::string& item) {
  1688. return item == DL_BEGIN || item == DL_END;
  1689. });
  1690. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1691. list.push_back(static_cast<std::string>(DL_END));
  1692. return cmJoin(list, ";");
  1693. }
  1694. return std::string();
  1695. }
  1696. } deviceLinkNode;
  1697. static std::string getLinkedTargetsContent(
  1698. cmGeneratorTarget const* target, std::string const& prop,
  1699. cmGeneratorExpressionContext* context,
  1700. cmGeneratorExpressionDAGChecker* dagChecker)
  1701. {
  1702. std::string result;
  1703. if (cmLinkImplementationLibraries const* impl =
  1704. target->GetLinkImplementationLibraries(
  1705. context->Config, cmGeneratorTarget::LinkInterfaceFor::Usage)) {
  1706. for (cmLinkImplItem const& lib : impl->Libraries) {
  1707. if (lib.Target) {
  1708. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1709. // caller's property and hand-evaluate it as if it were compiled.
  1710. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1711. cmGeneratorExpressionContext libContext(
  1712. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1713. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1714. context->Language);
  1715. std::string libResult =
  1716. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1717. if (!libResult.empty()) {
  1718. if (result.empty()) {
  1719. result = std::move(libResult);
  1720. } else {
  1721. result.reserve(result.size() + 1 + libResult.size());
  1722. result += ";";
  1723. result += libResult;
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. return result;
  1730. }
  1731. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1732. {
  1733. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1734. // This node handles errors on parameter count itself.
  1735. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1736. static const char* GetErrorText(std::string const& targetName,
  1737. std::string const& propertyName)
  1738. {
  1739. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1740. if (targetName.empty() && propertyName.empty()) {
  1741. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1742. "target name and property name.";
  1743. }
  1744. if (targetName.empty()) {
  1745. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1746. "target name.";
  1747. }
  1748. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1749. if (!propertyNameValidator.find(propertyName)) {
  1750. return "Target name and property name not supported.";
  1751. }
  1752. return "Target name not supported.";
  1753. }
  1754. return nullptr;
  1755. }
  1756. std::string Evaluate(
  1757. const std::vector<std::string>& parameters,
  1758. cmGeneratorExpressionContext* context,
  1759. const GeneratorExpressionContent* content,
  1760. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1761. {
  1762. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1763. cmGeneratorTarget const* target = nullptr;
  1764. std::string targetName;
  1765. std::string propertyName;
  1766. if (parameters.size() == 2) {
  1767. targetName = parameters[0];
  1768. propertyName = parameters[1];
  1769. if (const char* e = GetErrorText(targetName, propertyName)) {
  1770. reportError(context, content->GetOriginalExpression(), e);
  1771. return std::string();
  1772. }
  1773. if (propertyName == "ALIASED_TARGET"_s) {
  1774. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1775. if (cmGeneratorTarget* tgt =
  1776. context->LG->FindGeneratorTargetToUse(targetName)) {
  1777. return tgt->GetName();
  1778. }
  1779. }
  1780. return std::string();
  1781. }
  1782. if (propertyName == "ALIAS_GLOBAL"_s) {
  1783. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1784. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1785. ? "TRUE"
  1786. : "FALSE";
  1787. }
  1788. return std::string();
  1789. }
  1790. cmLocalGenerator const* lg = context->CurrentTarget
  1791. ? context->CurrentTarget->GetLocalGenerator()
  1792. : context->LG;
  1793. target = lg->FindGeneratorTargetToUse(targetName);
  1794. if (!target) {
  1795. std::ostringstream e;
  1796. e << "Target \"" << targetName << "\" not found.";
  1797. reportError(context, content->GetOriginalExpression(), e.str());
  1798. return std::string();
  1799. }
  1800. context->AllTargets.insert(target);
  1801. } else if (parameters.size() == 1) {
  1802. target = context->HeadTarget;
  1803. propertyName = parameters[0];
  1804. // Keep track of the properties seen while processing.
  1805. // The evaluation of the LINK_LIBRARIES generator expressions
  1806. // will check this to ensure that properties have one consistent
  1807. // value for all evaluations.
  1808. context->SeenTargetProperties.insert(propertyName);
  1809. context->HadHeadSensitiveCondition = true;
  1810. if (!target) {
  1811. reportError(
  1812. context, content->GetOriginalExpression(),
  1813. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1814. "It may not be used with add_custom_command or add_custom_target. "
  1815. " "
  1816. "Specify the target to read a property from using the "
  1817. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1818. return std::string();
  1819. }
  1820. } else {
  1821. reportError(
  1822. context, content->GetOriginalExpression(),
  1823. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1824. return std::string();
  1825. }
  1826. if (propertyName == "SOURCES") {
  1827. context->SourceSensitiveTargets.insert(target);
  1828. }
  1829. if (propertyName.empty()) {
  1830. reportError(
  1831. context, content->GetOriginalExpression(),
  1832. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1833. "name.");
  1834. return std::string();
  1835. }
  1836. if (!propertyNameValidator.find(propertyName)) {
  1837. ::reportError(context, content->GetOriginalExpression(),
  1838. "Property name not supported.");
  1839. return std::string();
  1840. }
  1841. assert(target);
  1842. if (propertyName == "LINKER_LANGUAGE") {
  1843. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1844. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1845. dagCheckerParent->EvaluatingSources())) {
  1846. reportError(
  1847. context, content->GetOriginalExpression(),
  1848. "LINKER_LANGUAGE target property can not be used while evaluating "
  1849. "link libraries for a static library");
  1850. return std::string();
  1851. }
  1852. return target->GetLinkerLanguage(context->Config);
  1853. }
  1854. std::string interfacePropertyName;
  1855. bool isInterfaceProperty = false;
  1856. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1857. if (propertyName == #prop) { \
  1858. interfacePropertyName = "INTERFACE_" #prop; \
  1859. } else if (propertyName == "INTERFACE_" #prop) { \
  1860. interfacePropertyName = "INTERFACE_" #prop; \
  1861. isInterfaceProperty = true; \
  1862. } else
  1863. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1864. // Note that the above macro terminates with an else
  1865. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1866. cmPolicies::PolicyStatus polSt =
  1867. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1868. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1869. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1870. }
  1871. }
  1872. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1873. bool evaluatingLinkLibraries = false;
  1874. if (dagCheckerParent) {
  1875. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1876. dagCheckerParent->EvaluatingPICExpression() ||
  1877. dagCheckerParent->EvaluatingLinkerLauncher()) {
  1878. // No check required.
  1879. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1880. evaluatingLinkLibraries = true;
  1881. if (!interfacePropertyName.empty()) {
  1882. reportError(
  1883. context, content->GetOriginalExpression(),
  1884. "$<TARGET_PROPERTY:...> expression in link libraries "
  1885. "evaluation depends on target property which is transitive "
  1886. "over the link libraries, creating a recursion.");
  1887. return std::string();
  1888. }
  1889. } else {
  1890. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1891. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1892. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1893. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1894. }
  1895. }
  1896. if (isInterfaceProperty) {
  1897. return cmGeneratorExpression::StripEmptyListElements(
  1898. target->EvaluateInterfaceProperty(propertyName, context,
  1899. dagCheckerParent));
  1900. }
  1901. cmGeneratorExpressionDAGChecker dagChecker(
  1902. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1903. switch (dagChecker.Check()) {
  1904. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1905. dagChecker.ReportError(context, content->GetOriginalExpression());
  1906. return std::string();
  1907. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1908. // No error. We just skip cyclic references.
  1909. return std::string();
  1910. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1911. // We handle transitive properties above. For non-transitive
  1912. // properties we accept repeats anyway.
  1913. case cmGeneratorExpressionDAGChecker::DAG:
  1914. break;
  1915. }
  1916. std::string result;
  1917. bool haveProp = false;
  1918. if (cmValue p = target->GetProperty(propertyName)) {
  1919. result = *p;
  1920. haveProp = true;
  1921. } else if (evaluatingLinkLibraries) {
  1922. return std::string();
  1923. }
  1924. if (!haveProp && !target->IsImported() &&
  1925. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1926. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1927. context->Config)) {
  1928. context->HadContextSensitiveCondition = true;
  1929. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1930. context->Config)
  1931. ? "1"
  1932. : "0";
  1933. }
  1934. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1935. context->Config)) {
  1936. context->HadContextSensitiveCondition = true;
  1937. const char* propContent =
  1938. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1939. context->Config);
  1940. return propContent ? propContent : "";
  1941. }
  1942. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1943. context->Config)) {
  1944. context->HadContextSensitiveCondition = true;
  1945. const char* propContent =
  1946. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1947. context->Config);
  1948. return propContent ? propContent : "";
  1949. }
  1950. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1951. context->Config)) {
  1952. context->HadContextSensitiveCondition = true;
  1953. const char* propContent =
  1954. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1955. context->Config);
  1956. return propContent ? propContent : "";
  1957. }
  1958. }
  1959. if (!target->IsImported() && dagCheckerParent &&
  1960. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1961. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1962. context->Config)) {
  1963. context->HadContextSensitiveCondition = true;
  1964. const char* propContent =
  1965. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1966. context->Config);
  1967. return propContent ? propContent : "";
  1968. }
  1969. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1970. context->Config)) {
  1971. context->HadContextSensitiveCondition = true;
  1972. const char* propContent =
  1973. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1974. context->Config);
  1975. return propContent ? propContent : "";
  1976. }
  1977. }
  1978. if (!interfacePropertyName.empty()) {
  1979. result = cmGeneratorExpression::StripEmptyListElements(
  1980. this->EvaluateDependentExpression(result, context->LG, context, target,
  1981. &dagChecker, target));
  1982. std::string linkedTargetsContent = getLinkedTargetsContent(
  1983. target, interfacePropertyName, context, &dagChecker);
  1984. if (!linkedTargetsContent.empty()) {
  1985. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1986. }
  1987. }
  1988. return result;
  1989. }
  1990. } targetPropertyNode;
  1991. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1992. {
  1993. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1994. bool GeneratesContent() const override { return true; }
  1995. bool AcceptsArbitraryContentParameter() const override { return true; }
  1996. bool RequiresLiteralInput() const override { return true; }
  1997. std::string Evaluate(
  1998. const std::vector<std::string>& parameters,
  1999. cmGeneratorExpressionContext* /*context*/,
  2000. const GeneratorExpressionContent* /*content*/,
  2001. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2002. {
  2003. return parameters.front();
  2004. }
  2005. int NumExpectedParameters() const override { return 1; }
  2006. } targetNameNode;
  2007. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  2008. {
  2009. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  2010. std::string Evaluate(
  2011. const std::vector<std::string>& parameters,
  2012. cmGeneratorExpressionContext* context,
  2013. const GeneratorExpressionContent* content,
  2014. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2015. {
  2016. std::string const& tgtName = parameters.front();
  2017. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2018. if (!gt) {
  2019. std::ostringstream e;
  2020. e << "Objects of target \"" << tgtName
  2021. << "\" referenced but no such target exists.";
  2022. reportError(context, content->GetOriginalExpression(), e.str());
  2023. return std::string();
  2024. }
  2025. cmStateEnums::TargetType type = gt->GetType();
  2026. if (type != cmStateEnums::EXECUTABLE &&
  2027. type != cmStateEnums::STATIC_LIBRARY &&
  2028. type != cmStateEnums::SHARED_LIBRARY &&
  2029. type != cmStateEnums::MODULE_LIBRARY &&
  2030. type != cmStateEnums::OBJECT_LIBRARY) {
  2031. std::ostringstream e;
  2032. e << "Objects of target \"" << tgtName
  2033. << "\" referenced but is not one of the allowed target types "
  2034. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  2035. reportError(context, content->GetOriginalExpression(), e.str());
  2036. return std::string();
  2037. }
  2038. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  2039. {
  2040. std::string reason;
  2041. if (!context->EvaluateForBuildsystem &&
  2042. !gt->Target->HasKnownObjectFileLocation(&reason)) {
  2043. std::ostringstream e;
  2044. e << "The evaluation of the TARGET_OBJECTS generator expression "
  2045. "is only suitable for consumption by CMake (limited"
  2046. << reason
  2047. << "). "
  2048. "It is not suitable for writing out elsewhere.";
  2049. reportError(context, content->GetOriginalExpression(), e.str());
  2050. return std::string();
  2051. }
  2052. }
  2053. std::vector<std::string> objects;
  2054. if (gt->IsImported()) {
  2055. cmValue loc = nullptr;
  2056. cmValue imp = nullptr;
  2057. std::string suffix;
  2058. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  2059. cmExpandList(*loc, objects);
  2060. }
  2061. context->HadContextSensitiveCondition = true;
  2062. } else {
  2063. gt->GetTargetObjectNames(context->Config, objects);
  2064. std::string obj_dir;
  2065. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  2066. // Use object file directory with buildsystem placeholder.
  2067. obj_dir = gt->ObjectDirectory;
  2068. context->HadContextSensitiveCondition =
  2069. gt->HasContextDependentSources();
  2070. } else {
  2071. // Use object file directory with per-config location.
  2072. obj_dir = gt->GetObjectDirectory(context->Config);
  2073. context->HadContextSensitiveCondition = true;
  2074. }
  2075. for (std::string& o : objects) {
  2076. o = cmStrCat(obj_dir, o);
  2077. }
  2078. }
  2079. // Create the cmSourceFile instances in the referencing directory.
  2080. cmMakefile* mf = context->LG->GetMakefile();
  2081. for (std::string const& o : objects) {
  2082. mf->AddTargetObject(tgtName, o);
  2083. }
  2084. return cmJoin(objects, ";");
  2085. }
  2086. } targetObjectsNode;
  2087. struct TargetRuntimeDllsBaseNode : public cmGeneratorExpressionNode
  2088. {
  2089. std::vector<std::string> CollectDlls(
  2090. const std::vector<std::string>& parameters,
  2091. cmGeneratorExpressionContext* context,
  2092. const GeneratorExpressionContent* content) const
  2093. {
  2094. std::string const& tgtName = parameters.front();
  2095. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  2096. if (!gt) {
  2097. std::ostringstream e;
  2098. e << "Objects of target \"" << tgtName
  2099. << "\" referenced but no such target exists.";
  2100. reportError(context, content->GetOriginalExpression(), e.str());
  2101. return std::vector<std::string>();
  2102. }
  2103. cmStateEnums::TargetType type = gt->GetType();
  2104. if (type != cmStateEnums::EXECUTABLE &&
  2105. type != cmStateEnums::SHARED_LIBRARY &&
  2106. type != cmStateEnums::MODULE_LIBRARY) {
  2107. std::ostringstream e;
  2108. e << "Objects of target \"" << tgtName
  2109. << "\" referenced but is not one of the allowed target types "
  2110. << "(EXECUTABLE, SHARED, MODULE).";
  2111. reportError(context, content->GetOriginalExpression(), e.str());
  2112. return std::vector<std::string>();
  2113. }
  2114. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  2115. std::vector<std::string> dllPaths;
  2116. auto const& dlls = cli->GetRuntimeDLLs();
  2117. for (auto const& dll : dlls) {
  2118. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  2119. dllPaths.emplace_back(*loc);
  2120. }
  2121. }
  2122. return dllPaths;
  2123. }
  2124. return std::vector<std::string>();
  2125. }
  2126. };
  2127. static const struct TargetRuntimeDllsNode : public TargetRuntimeDllsBaseNode
  2128. {
  2129. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  2130. std::string Evaluate(
  2131. const std::vector<std::string>& parameters,
  2132. cmGeneratorExpressionContext* context,
  2133. const GeneratorExpressionContent* content,
  2134. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2135. {
  2136. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  2137. return cmJoin(dlls, ";");
  2138. }
  2139. } targetRuntimeDllsNode;
  2140. static const struct TargetRuntimeDllDirsNode : public TargetRuntimeDllsBaseNode
  2141. {
  2142. TargetRuntimeDllDirsNode() {} // NOLINT(modernize-use-equals-default)
  2143. std::string Evaluate(
  2144. const std::vector<std::string>& parameters,
  2145. cmGeneratorExpressionContext* context,
  2146. const GeneratorExpressionContent* content,
  2147. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2148. {
  2149. std::vector<std::string> dlls = CollectDlls(parameters, context, content);
  2150. std::vector<std::string> dllDirs;
  2151. for (const std::string& dll : dlls) {
  2152. std::string directory = cmSystemTools::GetFilenamePath(dll);
  2153. if (std::find(dllDirs.begin(), dllDirs.end(), directory) ==
  2154. dllDirs.end()) {
  2155. dllDirs.push_back(directory);
  2156. }
  2157. }
  2158. return cmJoin(dllDirs, ";");
  2159. }
  2160. } targetRuntimeDllDirsNode;
  2161. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  2162. {
  2163. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  2164. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  2165. std::string Evaluate(
  2166. const std::vector<std::string>& parameters,
  2167. cmGeneratorExpressionContext* context,
  2168. const GeneratorExpressionContent* content,
  2169. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2170. {
  2171. cmGeneratorTarget const* target = context->HeadTarget;
  2172. if (!target) {
  2173. reportError(
  2174. context, content->GetOriginalExpression(),
  2175. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  2176. "not be used with add_custom_command or add_custom_target.");
  2177. return std::string();
  2178. }
  2179. context->HadHeadSensitiveCondition = true;
  2180. using LangMap = std::map<std::string, std::vector<std::string>>;
  2181. static LangMap availableFeatures;
  2182. LangMap testedFeatures;
  2183. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  2184. for (std::string const& p : parameters) {
  2185. std::string error;
  2186. std::string lang;
  2187. if (!standardResolver.CompileFeatureKnown(
  2188. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  2189. reportError(context, content->GetOriginalExpression(), error);
  2190. return std::string();
  2191. }
  2192. testedFeatures[lang].push_back(p);
  2193. if (availableFeatures.find(lang) == availableFeatures.end()) {
  2194. cmValue featuresKnown =
  2195. standardResolver.CompileFeaturesAvailable(lang, &error);
  2196. if (!featuresKnown) {
  2197. reportError(context, content->GetOriginalExpression(), error);
  2198. return std::string();
  2199. }
  2200. cmExpandList(featuresKnown, availableFeatures[lang]);
  2201. }
  2202. }
  2203. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  2204. for (auto const& lit : testedFeatures) {
  2205. std::vector<std::string> const& langAvailable =
  2206. availableFeatures[lit.first];
  2207. cmValue standardDefault = context->LG->GetMakefile()->GetDefinition(
  2208. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  2209. for (std::string const& it : lit.second) {
  2210. if (!cm::contains(langAvailable, it)) {
  2211. return "0";
  2212. }
  2213. if (standardDefault && standardDefault->empty()) {
  2214. // This compiler has no notion of language standard levels.
  2215. // All features known for the language are always available.
  2216. continue;
  2217. }
  2218. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  2219. context->Config, it)) {
  2220. if (evalLL) {
  2221. cmValue l =
  2222. target->GetLanguageStandard(lit.first, context->Config);
  2223. if (!l) {
  2224. l = standardDefault;
  2225. }
  2226. assert(l);
  2227. context->MaxLanguageStandard[target][lit.first] = *l;
  2228. } else {
  2229. return "0";
  2230. }
  2231. }
  2232. }
  2233. }
  2234. return "1";
  2235. }
  2236. } compileFeaturesNode;
  2237. static const char* targetPolicyWhitelist[] = {
  2238. nullptr
  2239. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  2240. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  2241. #undef TARGET_POLICY_STRING
  2242. };
  2243. static cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  2244. const char* policy)
  2245. {
  2246. #define RETURN_POLICY(POLICY) \
  2247. if (strcmp(policy, #POLICY) == 0) { \
  2248. return tgt->GetPolicyStatus##POLICY(); \
  2249. }
  2250. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  2251. #undef RETURN_POLICY
  2252. assert(false && "Unreachable code. Not a valid policy");
  2253. return cmPolicies::WARN;
  2254. }
  2255. static cmPolicies::PolicyID policyForString(const char* policy_id)
  2256. {
  2257. #define RETURN_POLICY_ID(POLICY_ID) \
  2258. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  2259. return cmPolicies::POLICY_ID; \
  2260. }
  2261. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  2262. #undef RETURN_POLICY_ID
  2263. assert(false && "Unreachable code. Not a valid policy");
  2264. return cmPolicies::CMP0002;
  2265. }
  2266. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  2267. {
  2268. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  2269. int NumExpectedParameters() const override { return 1; }
  2270. std::string Evaluate(
  2271. const std::vector<std::string>& parameters,
  2272. cmGeneratorExpressionContext* context,
  2273. const GeneratorExpressionContent* content,
  2274. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2275. {
  2276. if (!context->HeadTarget) {
  2277. reportError(
  2278. context, content->GetOriginalExpression(),
  2279. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  2280. "may not be used with add_custom_command or add_custom_target.");
  2281. return std::string();
  2282. }
  2283. context->HadContextSensitiveCondition = true;
  2284. context->HadHeadSensitiveCondition = true;
  2285. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  2286. const char* policy = targetPolicyWhitelist[i];
  2287. if (parameters.front() == policy) {
  2288. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  2289. switch (statusForTarget(context->HeadTarget, policy)) {
  2290. case cmPolicies::WARN:
  2291. lg->IssueMessage(
  2292. MessageType::AUTHOR_WARNING,
  2293. cmPolicies::GetPolicyWarning(policyForString(policy)));
  2294. CM_FALLTHROUGH;
  2295. case cmPolicies::REQUIRED_IF_USED:
  2296. case cmPolicies::REQUIRED_ALWAYS:
  2297. case cmPolicies::OLD:
  2298. return "0";
  2299. case cmPolicies::NEW:
  2300. return "1";
  2301. }
  2302. }
  2303. }
  2304. reportError(
  2305. context, content->GetOriginalExpression(),
  2306. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  2307. "policies. Currently it may be used with the following policies:\n"
  2308. #define STRINGIFY_HELPER(X) #X
  2309. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  2310. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  2311. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  2312. #undef TARGET_POLICY_LIST_ITEM
  2313. );
  2314. return std::string();
  2315. }
  2316. } targetPolicyNode;
  2317. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  2318. {
  2319. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  2320. bool GeneratesContent() const override { return true; }
  2321. int NumExpectedParameters() const override { return 0; }
  2322. std::string Evaluate(
  2323. const std::vector<std::string>& /*parameters*/,
  2324. cmGeneratorExpressionContext* context,
  2325. const GeneratorExpressionContent* content,
  2326. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2327. {
  2328. reportError(context, content->GetOriginalExpression(),
  2329. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  2330. "should never be evaluated.");
  2331. return std::string();
  2332. }
  2333. } installPrefixNode;
  2334. class ArtifactDirTag;
  2335. class ArtifactLinkerTag;
  2336. class ArtifactNameTag;
  2337. class ArtifactPathTag;
  2338. class ArtifactPdbTag;
  2339. class ArtifactSonameTag;
  2340. class ArtifactBundleDirTag;
  2341. class ArtifactBundleDirNameTag;
  2342. class ArtifactBundleContentDirTag;
  2343. template <typename ArtifactT, typename ComponentT>
  2344. struct TargetFilesystemArtifactDependency
  2345. {
  2346. static void AddDependency(cmGeneratorTarget* target,
  2347. cmGeneratorExpressionContext* context)
  2348. {
  2349. context->DependTargets.insert(target);
  2350. context->AllTargets.insert(target);
  2351. }
  2352. };
  2353. struct TargetFilesystemArtifactDependencyCMP0112
  2354. {
  2355. static void AddDependency(cmGeneratorTarget* target,
  2356. cmGeneratorExpressionContext* context)
  2357. {
  2358. context->AllTargets.insert(target);
  2359. cmLocalGenerator* lg = context->LG;
  2360. switch (target->GetPolicyStatusCMP0112()) {
  2361. case cmPolicies::WARN:
  2362. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  2363. "CMAKE_POLICY_WARNING_CMP0112")) {
  2364. std::string err =
  2365. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  2366. "\nDependency being added to target:\n \"",
  2367. target->GetName(), "\"\n");
  2368. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  2369. err, context->Backtrace);
  2370. }
  2371. CM_FALLTHROUGH;
  2372. case cmPolicies::OLD:
  2373. context->DependTargets.insert(target);
  2374. break;
  2375. case cmPolicies::REQUIRED_IF_USED:
  2376. case cmPolicies::REQUIRED_ALWAYS:
  2377. case cmPolicies::NEW:
  2378. break;
  2379. }
  2380. }
  2381. };
  2382. template <typename ArtifactT>
  2383. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  2384. : TargetFilesystemArtifactDependencyCMP0112
  2385. {
  2386. };
  2387. template <typename ArtifactT>
  2388. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  2389. : TargetFilesystemArtifactDependencyCMP0112
  2390. {
  2391. };
  2392. template <>
  2393. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  2394. ArtifactPathTag>
  2395. : TargetFilesystemArtifactDependencyCMP0112
  2396. {
  2397. };
  2398. template <>
  2399. struct TargetFilesystemArtifactDependency<ArtifactBundleDirNameTag,
  2400. ArtifactPathTag>
  2401. : TargetFilesystemArtifactDependencyCMP0112
  2402. {
  2403. };
  2404. template <>
  2405. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  2406. ArtifactPathTag>
  2407. : TargetFilesystemArtifactDependencyCMP0112
  2408. {
  2409. };
  2410. template <typename ArtifactT>
  2411. struct TargetFilesystemArtifactResultCreator
  2412. {
  2413. static std::string Create(cmGeneratorTarget* target,
  2414. cmGeneratorExpressionContext* context,
  2415. const GeneratorExpressionContent* content);
  2416. };
  2417. template <>
  2418. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  2419. {
  2420. static std::string Create(cmGeneratorTarget* target,
  2421. cmGeneratorExpressionContext* context,
  2422. const GeneratorExpressionContent* content)
  2423. {
  2424. // The target soname file (.so.1).
  2425. if (target->IsDLLPlatform()) {
  2426. ::reportError(context, content->GetOriginalExpression(),
  2427. "TARGET_SONAME_FILE is not allowed "
  2428. "for DLL target platforms.");
  2429. return std::string();
  2430. }
  2431. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  2432. ::reportError(context, content->GetOriginalExpression(),
  2433. "TARGET_SONAME_FILE is allowed only for "
  2434. "SHARED libraries.");
  2435. return std::string();
  2436. }
  2437. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  2438. target->GetSOName(context->Config));
  2439. return result;
  2440. }
  2441. };
  2442. template <>
  2443. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  2444. {
  2445. static std::string Create(cmGeneratorTarget* target,
  2446. cmGeneratorExpressionContext* context,
  2447. const GeneratorExpressionContent* content)
  2448. {
  2449. if (target->IsImported()) {
  2450. ::reportError(context, content->GetOriginalExpression(),
  2451. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  2452. return std::string();
  2453. }
  2454. std::string language = target->GetLinkerLanguage(context->Config);
  2455. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2456. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2457. ::reportError(context, content->GetOriginalExpression(),
  2458. "TARGET_PDB_FILE is not supported by the target linker.");
  2459. return std::string();
  2460. }
  2461. cmStateEnums::TargetType targetType = target->GetType();
  2462. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2463. targetType != cmStateEnums::MODULE_LIBRARY &&
  2464. targetType != cmStateEnums::EXECUTABLE) {
  2465. ::reportError(context, content->GetOriginalExpression(),
  2466. "TARGET_PDB_FILE is allowed only for "
  2467. "targets with linker created artifacts.");
  2468. return std::string();
  2469. }
  2470. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  2471. '/', target->GetPDBName(context->Config));
  2472. return result;
  2473. }
  2474. };
  2475. template <>
  2476. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  2477. {
  2478. static std::string Create(cmGeneratorTarget* target,
  2479. cmGeneratorExpressionContext* context,
  2480. const GeneratorExpressionContent* content)
  2481. {
  2482. // The file used to link to the target (.so, .lib, .a).
  2483. if (!target->IsLinkable()) {
  2484. ::reportError(context, content->GetOriginalExpression(),
  2485. "TARGET_LINKER_FILE is allowed only for libraries and "
  2486. "executables with ENABLE_EXPORTS.");
  2487. return std::string();
  2488. }
  2489. cmStateEnums::ArtifactType artifact =
  2490. target->HasImportLibrary(context->Config)
  2491. ? cmStateEnums::ImportLibraryArtifact
  2492. : cmStateEnums::RuntimeBinaryArtifact;
  2493. return target->GetFullPath(context->Config, artifact);
  2494. }
  2495. };
  2496. template <>
  2497. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  2498. {
  2499. static std::string Create(cmGeneratorTarget* target,
  2500. cmGeneratorExpressionContext* context,
  2501. const GeneratorExpressionContent* content)
  2502. {
  2503. if (target->IsImported()) {
  2504. ::reportError(context, content->GetOriginalExpression(),
  2505. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  2506. return std::string();
  2507. }
  2508. if (!target->IsBundleOnApple()) {
  2509. ::reportError(context, content->GetOriginalExpression(),
  2510. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  2511. return std::string();
  2512. }
  2513. std::string outpath = target->GetDirectory(context->Config) + '/';
  2514. return target->BuildBundleDirectory(outpath, context->Config,
  2515. cmGeneratorTarget::BundleDirLevel);
  2516. }
  2517. };
  2518. template <>
  2519. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirNameTag>
  2520. {
  2521. static std::string Create(cmGeneratorTarget* target,
  2522. cmGeneratorExpressionContext* context,
  2523. const GeneratorExpressionContent* content)
  2524. {
  2525. if (target->IsImported()) {
  2526. ::reportError(
  2527. context, content->GetOriginalExpression(),
  2528. "TARGET_BUNDLE_DIR_NAME not allowed for IMPORTED targets.");
  2529. return std::string();
  2530. }
  2531. if (!target->IsBundleOnApple()) {
  2532. ::reportError(
  2533. context, content->GetOriginalExpression(),
  2534. "TARGET_BUNDLE_DIR_NAME is allowed only for Bundle targets.");
  2535. return std::string();
  2536. }
  2537. auto level = cmGeneratorTarget::BundleDirLevel;
  2538. auto config = context->Config;
  2539. if (target->IsAppBundleOnApple()) {
  2540. return target->GetAppBundleDirectory(config, level);
  2541. }
  2542. if (target->IsFrameworkOnApple()) {
  2543. return target->GetFrameworkDirectory(config, level);
  2544. }
  2545. if (target->IsCFBundleOnApple()) {
  2546. return target->GetCFBundleDirectory(config, level);
  2547. }
  2548. return std::string();
  2549. }
  2550. };
  2551. template <>
  2552. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  2553. {
  2554. static std::string Create(cmGeneratorTarget* target,
  2555. cmGeneratorExpressionContext* context,
  2556. const GeneratorExpressionContent* content)
  2557. {
  2558. if (target->IsImported()) {
  2559. ::reportError(
  2560. context, content->GetOriginalExpression(),
  2561. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  2562. return std::string();
  2563. }
  2564. if (!target->IsBundleOnApple()) {
  2565. ::reportError(
  2566. context, content->GetOriginalExpression(),
  2567. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  2568. return std::string();
  2569. }
  2570. std::string outpath = target->GetDirectory(context->Config) + '/';
  2571. return target->BuildBundleDirectory(outpath, context->Config,
  2572. cmGeneratorTarget::ContentLevel);
  2573. }
  2574. };
  2575. template <>
  2576. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  2577. {
  2578. static std::string Create(cmGeneratorTarget* target,
  2579. cmGeneratorExpressionContext* context,
  2580. const GeneratorExpressionContent* /*unused*/)
  2581. {
  2582. return target->GetFullPath(context->Config,
  2583. cmStateEnums::RuntimeBinaryArtifact, true);
  2584. }
  2585. };
  2586. template <typename ArtifactT>
  2587. struct TargetFilesystemArtifactResultGetter
  2588. {
  2589. static std::string Get(const std::string& result);
  2590. };
  2591. template <>
  2592. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  2593. {
  2594. static std::string Get(const std::string& result)
  2595. {
  2596. return cmSystemTools::GetFilenameName(result);
  2597. }
  2598. };
  2599. template <>
  2600. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  2601. {
  2602. static std::string Get(const std::string& result)
  2603. {
  2604. return cmSystemTools::GetFilenamePath(result);
  2605. }
  2606. };
  2607. template <>
  2608. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  2609. {
  2610. static std::string Get(const std::string& result) { return result; }
  2611. };
  2612. struct TargetArtifactBase : public cmGeneratorExpressionNode
  2613. {
  2614. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  2615. protected:
  2616. cmGeneratorTarget* GetTarget(
  2617. const std::vector<std::string>& parameters,
  2618. cmGeneratorExpressionContext* context,
  2619. const GeneratorExpressionContent* content,
  2620. cmGeneratorExpressionDAGChecker* dagChecker) const
  2621. {
  2622. // Lookup the referenced target.
  2623. std::string const& name = parameters.front();
  2624. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  2625. ::reportError(context, content->GetOriginalExpression(),
  2626. "Expression syntax not recognized.");
  2627. return nullptr;
  2628. }
  2629. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  2630. if (!target) {
  2631. ::reportError(context, content->GetOriginalExpression(),
  2632. "No target \"" + name + "\"");
  2633. return nullptr;
  2634. }
  2635. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  2636. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  2637. ::reportError(context, content->GetOriginalExpression(),
  2638. "Target \"" + name +
  2639. "\" is not an executable or library.");
  2640. return nullptr;
  2641. }
  2642. if (dagChecker &&
  2643. (dagChecker->EvaluatingLinkLibraries(target) ||
  2644. (dagChecker->EvaluatingSources() &&
  2645. target == dagChecker->TopTarget()))) {
  2646. ::reportError(context, content->GetOriginalExpression(),
  2647. "Expressions which require the linker language may not "
  2648. "be used while evaluating link libraries");
  2649. return nullptr;
  2650. }
  2651. return target;
  2652. }
  2653. };
  2654. template <typename ArtifactT, typename ComponentT>
  2655. struct TargetFilesystemArtifact : public TargetArtifactBase
  2656. {
  2657. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  2658. int NumExpectedParameters() const override { return 1; }
  2659. std::string Evaluate(
  2660. const std::vector<std::string>& parameters,
  2661. cmGeneratorExpressionContext* context,
  2662. const GeneratorExpressionContent* content,
  2663. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2664. {
  2665. cmGeneratorTarget* target =
  2666. this->GetTarget(parameters, context, content, dagChecker);
  2667. if (!target) {
  2668. return std::string();
  2669. }
  2670. // Not a dependent target if we are querying for ArtifactDirTag,
  2671. // ArtifactNameTag, ArtifactBundleDirTag, ArtifactBundleDirNameTag,
  2672. // and ArtifactBundleContentDirTag
  2673. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  2674. target, context);
  2675. std::string result =
  2676. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  2677. content);
  2678. if (context->HadError) {
  2679. return std::string();
  2680. }
  2681. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  2682. }
  2683. };
  2684. template <typename ArtifactT>
  2685. struct TargetFilesystemArtifactNodeGroup
  2686. {
  2687. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2688. {
  2689. }
  2690. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2691. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2692. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2693. };
  2694. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2695. targetNodeGroup;
  2696. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2697. targetLinkerNodeGroup;
  2698. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2699. targetSoNameNodeGroup;
  2700. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2701. targetPdbNodeGroup;
  2702. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2703. targetBundleDirNode;
  2704. static const TargetFilesystemArtifact<ArtifactBundleDirNameTag,
  2705. ArtifactNameTag>
  2706. targetBundleDirNameNode;
  2707. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2708. ArtifactPathTag>
  2709. targetBundleContentDirNode;
  2710. //
  2711. // To retrieve base name for various artifacts
  2712. //
  2713. template <typename ArtifactT>
  2714. struct TargetOutputNameArtifactResultGetter
  2715. {
  2716. static std::string Get(cmGeneratorTarget* target,
  2717. cmGeneratorExpressionContext* context,
  2718. const GeneratorExpressionContent* content);
  2719. };
  2720. template <>
  2721. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2722. {
  2723. static std::string Get(cmGeneratorTarget* target,
  2724. cmGeneratorExpressionContext* context,
  2725. const GeneratorExpressionContent* /*unused*/)
  2726. {
  2727. return target->GetOutputName(context->Config,
  2728. cmStateEnums::RuntimeBinaryArtifact) +
  2729. target->GetFilePostfix(context->Config);
  2730. }
  2731. };
  2732. template <>
  2733. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2734. {
  2735. static std::string Get(cmGeneratorTarget* target,
  2736. cmGeneratorExpressionContext* context,
  2737. const GeneratorExpressionContent* content)
  2738. {
  2739. // The file used to link to the target (.so, .lib, .a).
  2740. if (!target->IsLinkable()) {
  2741. ::reportError(context, content->GetOriginalExpression(),
  2742. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2743. "libraries and executables with ENABLE_EXPORTS.");
  2744. return std::string();
  2745. }
  2746. cmStateEnums::ArtifactType artifact =
  2747. target->HasImportLibrary(context->Config)
  2748. ? cmStateEnums::ImportLibraryArtifact
  2749. : cmStateEnums::RuntimeBinaryArtifact;
  2750. return target->GetOutputName(context->Config, artifact) +
  2751. target->GetFilePostfix(context->Config);
  2752. }
  2753. };
  2754. template <>
  2755. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2756. {
  2757. static std::string Get(cmGeneratorTarget* target,
  2758. cmGeneratorExpressionContext* context,
  2759. const GeneratorExpressionContent* content)
  2760. {
  2761. if (target->IsImported()) {
  2762. ::reportError(
  2763. context, content->GetOriginalExpression(),
  2764. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2765. return std::string();
  2766. }
  2767. std::string language = target->GetLinkerLanguage(context->Config);
  2768. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2769. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2770. ::reportError(
  2771. context, content->GetOriginalExpression(),
  2772. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2773. return std::string();
  2774. }
  2775. cmStateEnums::TargetType targetType = target->GetType();
  2776. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2777. targetType != cmStateEnums::MODULE_LIBRARY &&
  2778. targetType != cmStateEnums::EXECUTABLE) {
  2779. ::reportError(context, content->GetOriginalExpression(),
  2780. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2781. "targets with linker created artifacts.");
  2782. return std::string();
  2783. }
  2784. return target->GetPDBOutputName(context->Config) +
  2785. target->GetFilePostfix(context->Config);
  2786. }
  2787. };
  2788. template <typename ArtifactT>
  2789. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2790. {
  2791. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2792. int NumExpectedParameters() const override { return 1; }
  2793. std::string Evaluate(
  2794. const std::vector<std::string>& parameters,
  2795. cmGeneratorExpressionContext* context,
  2796. const GeneratorExpressionContent* content,
  2797. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2798. {
  2799. cmGeneratorTarget* target =
  2800. this->GetTarget(parameters, context, content, dagChecker);
  2801. if (!target) {
  2802. return std::string();
  2803. }
  2804. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2805. target, context, content);
  2806. if (context->HadError) {
  2807. return std::string();
  2808. }
  2809. return result;
  2810. }
  2811. };
  2812. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2813. targetFileBaseNameNode;
  2814. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2815. targetLinkerFileBaseNameNode;
  2816. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2817. targetPdbFileBaseNameNode;
  2818. class ArtifactFilePrefixTag;
  2819. class ArtifactLinkerFilePrefixTag;
  2820. class ArtifactFileSuffixTag;
  2821. class ArtifactLinkerFileSuffixTag;
  2822. template <typename ArtifactT>
  2823. struct TargetFileArtifactResultGetter
  2824. {
  2825. static std::string Get(cmGeneratorTarget* target,
  2826. cmGeneratorExpressionContext* context,
  2827. const GeneratorExpressionContent* content);
  2828. };
  2829. template <>
  2830. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2831. {
  2832. static std::string Get(cmGeneratorTarget* target,
  2833. cmGeneratorExpressionContext* context,
  2834. const GeneratorExpressionContent*)
  2835. {
  2836. return target->GetFilePrefix(context->Config);
  2837. }
  2838. };
  2839. template <>
  2840. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2841. {
  2842. static std::string Get(cmGeneratorTarget* target,
  2843. cmGeneratorExpressionContext* context,
  2844. const GeneratorExpressionContent* content)
  2845. {
  2846. if (!target->IsLinkable()) {
  2847. ::reportError(context, content->GetOriginalExpression(),
  2848. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2849. "executables with ENABLE_EXPORTS.");
  2850. return std::string();
  2851. }
  2852. cmStateEnums::ArtifactType artifact =
  2853. target->HasImportLibrary(context->Config)
  2854. ? cmStateEnums::ImportLibraryArtifact
  2855. : cmStateEnums::RuntimeBinaryArtifact;
  2856. return target->GetFilePrefix(context->Config, artifact);
  2857. }
  2858. };
  2859. template <>
  2860. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2861. {
  2862. static std::string Get(cmGeneratorTarget* target,
  2863. cmGeneratorExpressionContext* context,
  2864. const GeneratorExpressionContent*)
  2865. {
  2866. return target->GetFileSuffix(context->Config);
  2867. }
  2868. };
  2869. template <>
  2870. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2871. {
  2872. static std::string Get(cmGeneratorTarget* target,
  2873. cmGeneratorExpressionContext* context,
  2874. const GeneratorExpressionContent* content)
  2875. {
  2876. if (!target->IsLinkable()) {
  2877. ::reportError(context, content->GetOriginalExpression(),
  2878. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2879. "executables with ENABLE_EXPORTS.");
  2880. return std::string();
  2881. }
  2882. cmStateEnums::ArtifactType artifact =
  2883. target->HasImportLibrary(context->Config)
  2884. ? cmStateEnums::ImportLibraryArtifact
  2885. : cmStateEnums::RuntimeBinaryArtifact;
  2886. return target->GetFileSuffix(context->Config, artifact);
  2887. }
  2888. };
  2889. template <typename ArtifactT>
  2890. struct TargetFileArtifact : public TargetArtifactBase
  2891. {
  2892. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2893. int NumExpectedParameters() const override { return 1; }
  2894. std::string Evaluate(
  2895. const std::vector<std::string>& parameters,
  2896. cmGeneratorExpressionContext* context,
  2897. const GeneratorExpressionContent* content,
  2898. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2899. {
  2900. cmGeneratorTarget* target =
  2901. this->GetTarget(parameters, context, content, dagChecker);
  2902. if (!target) {
  2903. return std::string();
  2904. }
  2905. std::string result =
  2906. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2907. if (context->HadError) {
  2908. return std::string();
  2909. }
  2910. return result;
  2911. }
  2912. };
  2913. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2914. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2915. targetLinkerFilePrefixNode;
  2916. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2917. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2918. targetLinkerFileSuffixNode;
  2919. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2920. {
  2921. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2922. std::string Evaluate(
  2923. const std::vector<std::string>& parameters,
  2924. cmGeneratorExpressionContext* context,
  2925. const GeneratorExpressionContent* content,
  2926. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2927. {
  2928. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2929. if (listIn.empty()) {
  2930. reportError(context, content->GetOriginalExpression(),
  2931. "\"\" is not an absolute path.");
  2932. return std::string();
  2933. }
  2934. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2935. cmOutputConverter converter(snapshot);
  2936. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2937. std::vector<std::string> listOut;
  2938. listOut.reserve(listIn.size());
  2939. for (auto const& in : listIn) {
  2940. if (!cmSystemTools::FileIsFullPath(in)) {
  2941. reportError(context, content->GetOriginalExpression(),
  2942. "\"" + in + "\" is not an absolute path.");
  2943. return std::string();
  2944. }
  2945. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2946. }
  2947. return cmJoin(listOut, separator);
  2948. }
  2949. } shellPathNode;
  2950. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2951. const std::string& identifier)
  2952. {
  2953. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2954. { "0", &zeroNode },
  2955. { "1", &oneNode },
  2956. { "AND", &andNode },
  2957. { "OR", &orNode },
  2958. { "NOT", &notNode },
  2959. { "C_COMPILER_ID", &cCompilerIdNode },
  2960. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2961. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2962. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2963. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2964. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2965. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2966. { "VERSION_GREATER", &versionGreaterNode },
  2967. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2968. { "VERSION_LESS", &versionLessNode },
  2969. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2970. { "VERSION_EQUAL", &versionEqualNode },
  2971. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2972. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2973. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2974. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2975. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2976. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2977. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2978. { "PLATFORM_ID", &platformIdNode },
  2979. { "COMPILE_FEATURES", &compileFeaturesNode },
  2980. { "CONFIGURATION", &configurationNode },
  2981. { "CONFIG", &configurationTestNode },
  2982. { "TARGET_FILE", &targetNodeGroup.File },
  2983. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2984. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2985. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2986. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2987. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2988. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2989. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2990. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2991. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2992. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2993. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2994. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2995. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2996. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2997. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2998. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2999. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  3000. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  3001. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  3002. { "TARGET_BUNDLE_DIR_NAME", &targetBundleDirNameNode },
  3003. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  3004. { "STREQUAL", &strEqualNode },
  3005. { "EQUAL", &equalNode },
  3006. { "IN_LIST", &inListNode },
  3007. { "FILTER", &filterNode },
  3008. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  3009. { "LOWER_CASE", &lowerCaseNode },
  3010. { "UPPER_CASE", &upperCaseNode },
  3011. { "PATH", &pathNode },
  3012. { "PATH_EQUAL", &pathEqualNode },
  3013. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  3014. { "BOOL", &boolNode },
  3015. { "IF", &ifNode },
  3016. { "ANGLE-R", &angle_rNode },
  3017. { "COMMA", &commaNode },
  3018. { "SEMICOLON", &semicolonNode },
  3019. { "TARGET_PROPERTY", &targetPropertyNode },
  3020. { "TARGET_NAME", &targetNameNode },
  3021. { "TARGET_OBJECTS", &targetObjectsNode },
  3022. { "TARGET_POLICY", &targetPolicyNode },
  3023. { "TARGET_EXISTS", &targetExistsNode },
  3024. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  3025. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  3026. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  3027. { "TARGET_RUNTIME_DLL_DIRS", &targetRuntimeDllDirsNode },
  3028. { "GENEX_EVAL", &genexEvalNode },
  3029. { "BUILD_INTERFACE", &buildInterfaceNode },
  3030. { "INSTALL_INTERFACE", &installInterfaceNode },
  3031. { "BUILD_LOCAL_INTERFACE", &buildLocalInterfaceNode },
  3032. { "INSTALL_PREFIX", &installPrefixNode },
  3033. { "JOIN", &joinNode },
  3034. { "LINK_ONLY", &linkOnlyNode },
  3035. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  3036. { "COMPILE_LANGUAGE", &languageNode },
  3037. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  3038. { "LINK_LANGUAGE", &linkLanguageNode },
  3039. { "LINK_LIBRARY", &linkLibraryNode },
  3040. { "LINK_GROUP", &linkGroupNode },
  3041. { "HOST_LINK", &hostLinkNode },
  3042. { "DEVICE_LINK", &deviceLinkNode },
  3043. { "SHELL_PATH", &shellPathNode }
  3044. };
  3045. {
  3046. auto itr = nodeMap.find(identifier);
  3047. if (itr != nodeMap.end()) {
  3048. return itr->second;
  3049. }
  3050. }
  3051. return nullptr;
  3052. }
  3053. void reportError(cmGeneratorExpressionContext* context,
  3054. const std::string& expr, const std::string& result)
  3055. {
  3056. context->HadError = true;
  3057. if (context->Quiet) {
  3058. return;
  3059. }
  3060. std::ostringstream e;
  3061. /* clang-format off */
  3062. e << "Error evaluating generator expression:\n"
  3063. << " " << expr << "\n"
  3064. << result;
  3065. /* clang-format on */
  3066. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  3067. e.str(), context->Backtrace);
  3068. }