cmGeneratorExpressionNode.cxx 136 KB

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