cmGeneratorExpressionNode.cxx 118 KB

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