cmGeneratorExpressionNode.cxx 137 KB

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