cmGeneratorExpressionNode.cxx 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  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 <map>
  10. #include <memory>
  11. #include <set>
  12. #include <sstream>
  13. #include <utility>
  14. #include <cm/iterator>
  15. #include <cm/optional>
  16. #include <cm/string_view>
  17. #include <cm/vector>
  18. #include <cmext/algorithm>
  19. #include <cmext/string_view>
  20. #include "cmsys/RegularExpression.hxx"
  21. #include "cmsys/String.h"
  22. #include "cmAlgorithms.h"
  23. #include "cmComputeLinkInformation.h"
  24. #include "cmGeneratorExpression.h"
  25. #include "cmGeneratorExpressionContext.h"
  26. #include "cmGeneratorExpressionDAGChecker.h"
  27. #include "cmGeneratorExpressionEvaluator.h"
  28. #include "cmGeneratorTarget.h"
  29. #include "cmGlobalGenerator.h"
  30. #include "cmLinkItem.h"
  31. #include "cmLocalGenerator.h"
  32. #include "cmMakefile.h"
  33. #include "cmMessageType.h"
  34. #include "cmOutputConverter.h"
  35. #include "cmPolicies.h"
  36. #include "cmProperty.h"
  37. #include "cmRange.h"
  38. #include "cmStandardLevelResolver.h"
  39. #include "cmState.h"
  40. #include "cmStateSnapshot.h"
  41. #include "cmStateTypes.h"
  42. #include "cmStringAlgorithms.h"
  43. #include "cmSystemTools.h"
  44. #include "cmTarget.h"
  45. #include "cmake.h"
  46. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  47. std::string const& prop, cmLocalGenerator* lg,
  48. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  49. cmGeneratorExpressionDAGChecker* dagChecker,
  50. cmGeneratorTarget const* currentTarget)
  51. {
  52. cmGeneratorExpression ge(context->Backtrace);
  53. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  54. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  55. cge->SetQuiet(context->Quiet);
  56. std::string result =
  57. cge->Evaluate(lg, context->Config, headTarget, dagChecker, currentTarget,
  58. context->Language);
  59. if (cge->GetHadContextSensitiveCondition()) {
  60. context->HadContextSensitiveCondition = true;
  61. }
  62. if (cge->GetHadHeadSensitiveCondition()) {
  63. context->HadHeadSensitiveCondition = true;
  64. }
  65. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  66. context->HadLinkLanguageSensitiveCondition = true;
  67. }
  68. return result;
  69. }
  70. static const struct ZeroNode : public cmGeneratorExpressionNode
  71. {
  72. ZeroNode() {} // NOLINT(modernize-use-equals-default)
  73. bool GeneratesContent() const override { return false; }
  74. bool AcceptsArbitraryContentParameter() const override { return true; }
  75. std::string Evaluate(
  76. const std::vector<std::string>& /*parameters*/,
  77. cmGeneratorExpressionContext* /*context*/,
  78. const GeneratorExpressionContent* /*content*/,
  79. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  80. {
  81. return std::string();
  82. }
  83. } zeroNode;
  84. static const struct OneNode : public cmGeneratorExpressionNode
  85. {
  86. OneNode() {} // NOLINT(modernize-use-equals-default)
  87. bool AcceptsArbitraryContentParameter() const override { return true; }
  88. std::string Evaluate(
  89. const std::vector<std::string>& parameters,
  90. cmGeneratorExpressionContext* /*context*/,
  91. const GeneratorExpressionContent* /*content*/,
  92. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  93. {
  94. return parameters.front();
  95. }
  96. } oneNode;
  97. static const struct OneNode buildInterfaceNode;
  98. static const struct ZeroNode installInterfaceNode;
  99. struct BooleanOpNode : public cmGeneratorExpressionNode
  100. {
  101. BooleanOpNode(const char* op_, const char* successVal_,
  102. const char* failureVal_)
  103. : op(op_)
  104. , successVal(successVal_)
  105. , failureVal(failureVal_)
  106. {
  107. }
  108. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  109. std::string Evaluate(const std::vector<std::string>& parameters,
  110. cmGeneratorExpressionContext* context,
  111. const GeneratorExpressionContent* content,
  112. cmGeneratorExpressionDAGChecker*) const override
  113. {
  114. for (std::string const& param : parameters) {
  115. if (param == this->failureVal) {
  116. return this->failureVal;
  117. }
  118. if (param != this->successVal) {
  119. std::ostringstream e;
  120. e << "Parameters to $<" << this->op;
  121. e << "> must resolve to either '0' or '1'.";
  122. reportError(context, content->GetOriginalExpression(), e.str());
  123. return std::string();
  124. }
  125. }
  126. return this->successVal;
  127. }
  128. const char *const op, *const successVal, *const failureVal;
  129. };
  130. static const BooleanOpNode andNode("AND", "1", "0"), orNode("OR", "0", "1");
  131. static const struct NotNode : public cmGeneratorExpressionNode
  132. {
  133. NotNode() {} // NOLINT(modernize-use-equals-default)
  134. std::string Evaluate(
  135. const std::vector<std::string>& parameters,
  136. cmGeneratorExpressionContext* context,
  137. const GeneratorExpressionContent* content,
  138. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  139. {
  140. if (parameters.front() != "0" && parameters.front() != "1") {
  141. reportError(
  142. context, content->GetOriginalExpression(),
  143. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  144. return std::string();
  145. }
  146. return parameters.front() == "0" ? "1" : "0";
  147. }
  148. } notNode;
  149. static const struct BoolNode : public cmGeneratorExpressionNode
  150. {
  151. BoolNode() {} // NOLINT(modernize-use-equals-default)
  152. int NumExpectedParameters() const override { return 1; }
  153. std::string Evaluate(
  154. const std::vector<std::string>& parameters,
  155. cmGeneratorExpressionContext* /*context*/,
  156. const GeneratorExpressionContent* /*content*/,
  157. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  158. {
  159. return !cmIsOff(parameters.front()) ? "1" : "0";
  160. }
  161. } boolNode;
  162. static const struct IfNode : public cmGeneratorExpressionNode
  163. {
  164. IfNode() {} // NOLINT(modernize-use-equals-default)
  165. int NumExpectedParameters() const override { return 3; }
  166. std::string Evaluate(const std::vector<std::string>& parameters,
  167. cmGeneratorExpressionContext* context,
  168. const GeneratorExpressionContent* content,
  169. cmGeneratorExpressionDAGChecker*) const override
  170. {
  171. if (parameters[0] != "1" && parameters[0] != "0") {
  172. reportError(context, content->GetOriginalExpression(),
  173. "First parameter to $<IF> must resolve to exactly one '0' "
  174. "or '1' value.");
  175. return std::string();
  176. }
  177. return parameters[0] == "1" ? parameters[1] : parameters[2];
  178. }
  179. } ifNode;
  180. static const struct StrEqualNode : public cmGeneratorExpressionNode
  181. {
  182. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  183. int NumExpectedParameters() const override { return 2; }
  184. std::string Evaluate(
  185. const std::vector<std::string>& parameters,
  186. cmGeneratorExpressionContext* /*context*/,
  187. const GeneratorExpressionContent* /*content*/,
  188. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  189. {
  190. return parameters.front() == parameters[1] ? "1" : "0";
  191. }
  192. } strEqualNode;
  193. static const struct EqualNode : public cmGeneratorExpressionNode
  194. {
  195. EqualNode() {} // NOLINT(modernize-use-equals-default)
  196. int NumExpectedParameters() const override { return 2; }
  197. std::string Evaluate(
  198. const std::vector<std::string>& parameters,
  199. cmGeneratorExpressionContext* context,
  200. const GeneratorExpressionContent* content,
  201. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  202. {
  203. long numbers[2];
  204. for (int i = 0; i < 2; ++i) {
  205. if (!ParameterToLong(parameters[i].c_str(), &numbers[i])) {
  206. reportError(context, content->GetOriginalExpression(),
  207. "$<EQUAL> parameter " + parameters[i] +
  208. " is not a valid integer.");
  209. return {};
  210. }
  211. }
  212. return numbers[0] == numbers[1] ? "1" : "0";
  213. }
  214. static bool ParameterToLong(const char* param, long* outResult)
  215. {
  216. const char isNegative = param[0] == '-';
  217. int base = 0;
  218. if (cmHasLiteralPrefix(param, "0b") || cmHasLiteralPrefix(param, "0B")) {
  219. base = 2;
  220. param += 2;
  221. } else if (cmHasLiteralPrefix(param, "-0b") ||
  222. cmHasLiteralPrefix(param, "-0B") ||
  223. cmHasLiteralPrefix(param, "+0b") ||
  224. cmHasLiteralPrefix(param, "+0B")) {
  225. base = 2;
  226. param += 3;
  227. }
  228. char* pEnd;
  229. long result = strtol(param, &pEnd, base);
  230. if (pEnd == param || *pEnd != '\0' || errno == ERANGE) {
  231. return false;
  232. }
  233. if (isNegative && result > 0) {
  234. result *= -1;
  235. }
  236. *outResult = result;
  237. return true;
  238. }
  239. } equalNode;
  240. static const struct InListNode : public cmGeneratorExpressionNode
  241. {
  242. InListNode() {} // NOLINT(modernize-use-equals-default)
  243. int NumExpectedParameters() const override { return 2; }
  244. std::string Evaluate(
  245. const std::vector<std::string>& parameters,
  246. cmGeneratorExpressionContext* context,
  247. const GeneratorExpressionContent* /*content*/,
  248. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  249. {
  250. std::vector<std::string> values;
  251. std::vector<std::string> checkValues;
  252. bool check = false;
  253. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  254. case cmPolicies::WARN:
  255. if (parameters.front().empty()) {
  256. check = true;
  257. cmExpandList(parameters[1], checkValues, true);
  258. }
  259. CM_FALLTHROUGH;
  260. case cmPolicies::OLD:
  261. cmExpandList(parameters[1], values);
  262. if (check && values != checkValues) {
  263. std::ostringstream e;
  264. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  265. << "\nSearch Item:\n \"" << parameters.front()
  266. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  267. context->LG->GetCMakeInstance()->IssueMessage(
  268. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  269. return "0";
  270. }
  271. if (values.empty()) {
  272. return "0";
  273. }
  274. break;
  275. case cmPolicies::REQUIRED_IF_USED:
  276. case cmPolicies::REQUIRED_ALWAYS:
  277. case cmPolicies::NEW:
  278. cmExpandList(parameters[1], values, true);
  279. break;
  280. }
  281. return cm::contains(values, parameters.front()) ? "1" : "0";
  282. }
  283. } inListNode;
  284. static const struct FilterNode : public cmGeneratorExpressionNode
  285. {
  286. FilterNode() {} // NOLINT(modernize-use-equals-default)
  287. int NumExpectedParameters() const override { return 3; }
  288. std::string Evaluate(
  289. const std::vector<std::string>& parameters,
  290. cmGeneratorExpressionContext* context,
  291. const GeneratorExpressionContent* content,
  292. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  293. {
  294. if (parameters.size() != 3) {
  295. reportError(context, content->GetOriginalExpression(),
  296. "$<FILTER:...> expression requires three parameters");
  297. return {};
  298. }
  299. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  300. reportError(
  301. context, content->GetOriginalExpression(),
  302. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  303. return {};
  304. }
  305. const bool exclude = parameters[1] == "EXCLUDE";
  306. cmsys::RegularExpression re;
  307. if (!re.compile(parameters[2])) {
  308. reportError(context, content->GetOriginalExpression(),
  309. "$<FILTER:...> failed to compile regex");
  310. return {};
  311. }
  312. std::vector<std::string> values;
  313. std::vector<std::string> result;
  314. cmExpandList(parameters.front(), values, true);
  315. std::copy_if(values.cbegin(), values.cend(), std::back_inserter(result),
  316. [&re, exclude](std::string const& input) {
  317. return exclude ^ re.find(input);
  318. });
  319. return cmJoin(cmMakeRange(result.cbegin(), result.cend()), ";");
  320. }
  321. } filterNode;
  322. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  323. {
  324. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  325. int NumExpectedParameters() const override { return 1; }
  326. std::string Evaluate(
  327. const std::vector<std::string>& parameters,
  328. cmGeneratorExpressionContext* context,
  329. const GeneratorExpressionContent* content,
  330. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  331. {
  332. if (parameters.size() != 1) {
  333. reportError(
  334. context, content->GetOriginalExpression(),
  335. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  336. }
  337. std::vector<std::string> values = cmExpandedList(parameters.front(), true);
  338. auto valuesEnd = cmRemoveDuplicates(values);
  339. auto valuesBegin = values.cbegin();
  340. return cmJoin(cmMakeRange(valuesBegin, valuesEnd), ";");
  341. }
  342. } removeDuplicatesNode;
  343. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  344. {
  345. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  346. int NumExpectedParameters() const override { return 1; }
  347. std::string Evaluate(
  348. const std::vector<std::string>& parameters,
  349. cmGeneratorExpressionContext* context,
  350. const GeneratorExpressionContent* content,
  351. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  352. {
  353. if (parameters.size() != 1) {
  354. reportError(context, content->GetOriginalExpression(),
  355. "$<TARGET_EXISTS:...> expression requires one parameter");
  356. return std::string();
  357. }
  358. std::string targetName = parameters.front();
  359. if (targetName.empty() ||
  360. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  361. reportError(context, content->GetOriginalExpression(),
  362. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  363. "valid target name.");
  364. return std::string();
  365. }
  366. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  367. }
  368. } targetExistsNode;
  369. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  370. {
  371. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  372. int NumExpectedParameters() const override { return 1; }
  373. std::string Evaluate(
  374. const std::vector<std::string>& parameters,
  375. cmGeneratorExpressionContext* context,
  376. const GeneratorExpressionContent* content,
  377. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  378. {
  379. if (parameters.size() != 1) {
  380. reportError(context, content->GetOriginalExpression(),
  381. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  382. "parameter");
  383. return std::string();
  384. }
  385. std::string targetName = parameters.front();
  386. if (targetName.empty() ||
  387. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  388. reportError(context, content->GetOriginalExpression(),
  389. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  390. "non-empty valid target name.");
  391. return std::string();
  392. }
  393. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  394. ? targetName
  395. : std::string();
  396. }
  397. } targetNameIfExistsNode;
  398. struct GenexEvaluator : public cmGeneratorExpressionNode
  399. {
  400. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  401. protected:
  402. std::string EvaluateExpression(
  403. const std::string& genexOperator, const std::string& expression,
  404. cmGeneratorExpressionContext* context,
  405. const GeneratorExpressionContent* content,
  406. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  407. {
  408. if (context->HeadTarget) {
  409. cmGeneratorExpressionDAGChecker dagChecker(
  410. context->Backtrace, context->HeadTarget,
  411. genexOperator + ":" + expression, content, dagCheckerParent);
  412. switch (dagChecker.Check()) {
  413. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  414. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  415. dagChecker.ReportError(context, content->GetOriginalExpression());
  416. return std::string();
  417. }
  418. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  419. case cmGeneratorExpressionDAGChecker::DAG:
  420. break;
  421. }
  422. return this->EvaluateDependentExpression(
  423. expression, context->LG, context, context->HeadTarget, &dagChecker,
  424. context->CurrentTarget);
  425. }
  426. return this->EvaluateDependentExpression(
  427. expression, context->LG, context, context->HeadTarget, dagCheckerParent,
  428. context->CurrentTarget);
  429. }
  430. };
  431. static const struct TargetGenexEvalNode : public GenexEvaluator
  432. {
  433. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  434. int NumExpectedParameters() const override { return 2; }
  435. bool AcceptsArbitraryContentParameter() const override { return true; }
  436. std::string Evaluate(
  437. const std::vector<std::string>& parameters,
  438. cmGeneratorExpressionContext* context,
  439. const GeneratorExpressionContent* content,
  440. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  441. {
  442. const std::string& targetName = parameters.front();
  443. if (targetName.empty() ||
  444. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  445. reportError(context, content->GetOriginalExpression(),
  446. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  447. "non-empty valid target name.");
  448. return std::string();
  449. }
  450. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  451. if (!target) {
  452. std::ostringstream e;
  453. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  454. << "\" not found.";
  455. reportError(context, content->GetOriginalExpression(), e.str());
  456. return std::string();
  457. }
  458. const std::string& expression = parameters[1];
  459. if (expression.empty()) {
  460. return expression;
  461. }
  462. cmGeneratorExpressionContext targetContext(
  463. context->LG, context->Config, context->Quiet, target, target,
  464. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  465. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  466. &targetContext, content, dagCheckerParent);
  467. }
  468. } targetGenexEvalNode;
  469. static const struct GenexEvalNode : public GenexEvaluator
  470. {
  471. GenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  472. int NumExpectedParameters() const override { return 1; }
  473. bool AcceptsArbitraryContentParameter() const override { return true; }
  474. std::string Evaluate(
  475. const std::vector<std::string>& parameters,
  476. cmGeneratorExpressionContext* context,
  477. const GeneratorExpressionContent* content,
  478. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  479. {
  480. const std::string& expression = parameters[0];
  481. if (expression.empty()) {
  482. return expression;
  483. }
  484. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  485. dagCheckerParent);
  486. }
  487. } genexEvalNode;
  488. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  489. {
  490. LowerCaseNode() {} // NOLINT(modernize-use-equals-default)
  491. bool AcceptsArbitraryContentParameter() const override { return true; }
  492. std::string Evaluate(
  493. const std::vector<std::string>& parameters,
  494. cmGeneratorExpressionContext* /*context*/,
  495. const GeneratorExpressionContent* /*content*/,
  496. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  497. {
  498. return cmSystemTools::LowerCase(parameters.front());
  499. }
  500. } lowerCaseNode;
  501. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  502. {
  503. UpperCaseNode() {} // NOLINT(modernize-use-equals-default)
  504. bool AcceptsArbitraryContentParameter() const override { return true; }
  505. std::string Evaluate(
  506. const std::vector<std::string>& parameters,
  507. cmGeneratorExpressionContext* /*context*/,
  508. const GeneratorExpressionContent* /*content*/,
  509. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  510. {
  511. return cmSystemTools::UpperCase(parameters.front());
  512. }
  513. } upperCaseNode;
  514. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  515. {
  516. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  517. bool AcceptsArbitraryContentParameter() const override { return true; }
  518. std::string Evaluate(
  519. const std::vector<std::string>& parameters,
  520. cmGeneratorExpressionContext* /*context*/,
  521. const GeneratorExpressionContent* /*content*/,
  522. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  523. {
  524. return cmSystemTools::MakeCidentifier(parameters.front());
  525. }
  526. } makeCIdentifierNode;
  527. template <char C>
  528. struct CharacterNode : public cmGeneratorExpressionNode
  529. {
  530. CharacterNode() {} // NOLINT(modernize-use-equals-default)
  531. int NumExpectedParameters() const override { return 0; }
  532. std::string Evaluate(
  533. const std::vector<std::string>& /*parameters*/,
  534. cmGeneratorExpressionContext* /*context*/,
  535. const GeneratorExpressionContent* /*content*/,
  536. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  537. {
  538. return { C };
  539. }
  540. };
  541. static const CharacterNode<'>'> angle_rNode;
  542. static const CharacterNode<','> commaNode;
  543. static const CharacterNode<';'> semicolonNode;
  544. struct CompilerIdNode : public cmGeneratorExpressionNode
  545. {
  546. CompilerIdNode(const char* compilerLang)
  547. : CompilerLanguage(compilerLang)
  548. {
  549. }
  550. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  551. std::string Evaluate(
  552. const std::vector<std::string>& parameters,
  553. cmGeneratorExpressionContext* context,
  554. const GeneratorExpressionContent* content,
  555. cmGeneratorExpressionDAGChecker* dagChecker) const override
  556. {
  557. if (!context->HeadTarget) {
  558. std::ostringstream e;
  559. e << "$<" << this->CompilerLanguage
  560. << "_COMPILER_ID> may only be used with binary targets. It may "
  561. "not be used with add_custom_command or add_custom_target.";
  562. reportError(context, content->GetOriginalExpression(), e.str());
  563. return {};
  564. }
  565. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  566. this->CompilerLanguage);
  567. }
  568. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  569. cmGeneratorExpressionContext* context,
  570. const GeneratorExpressionContent* content,
  571. cmGeneratorExpressionDAGChecker* /*unused*/,
  572. const std::string& lang) const
  573. {
  574. std::string const& compilerId =
  575. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  576. "_COMPILER_ID");
  577. if (parameters.empty()) {
  578. return compilerId;
  579. }
  580. if (compilerId.empty()) {
  581. return parameters.front().empty() ? "1" : "0";
  582. }
  583. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  584. for (auto const& param : parameters) {
  585. if (!compilerIdValidator.find(param)) {
  586. reportError(context, content->GetOriginalExpression(),
  587. "Expression syntax not recognized.");
  588. return std::string();
  589. }
  590. if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
  591. return "1";
  592. }
  593. if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
  594. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  595. case cmPolicies::WARN: {
  596. context->LG->GetCMakeInstance()->IssueMessage(
  597. MessageType::AUTHOR_WARNING,
  598. cmPolicies::GetPolicyWarning(cmPolicies::CMP0044),
  599. context->Backtrace);
  600. CM_FALLTHROUGH;
  601. }
  602. case cmPolicies::OLD:
  603. return "1";
  604. case cmPolicies::NEW:
  605. case cmPolicies::REQUIRED_ALWAYS:
  606. case cmPolicies::REQUIRED_IF_USED:
  607. break;
  608. }
  609. }
  610. }
  611. return "0";
  612. }
  613. const char* const CompilerLanguage;
  614. };
  615. static const CompilerIdNode cCompilerIdNode("C"), cxxCompilerIdNode("CXX"),
  616. cudaCompilerIdNode("CUDA"), objcCompilerIdNode("OBJC"),
  617. objcxxCompilerIdNode("OBJCXX"), fortranCompilerIdNode("Fortran"),
  618. hipCompilerIdNode("HIP"), ispcCompilerIdNode("ISPC");
  619. struct CompilerVersionNode : public cmGeneratorExpressionNode
  620. {
  621. CompilerVersionNode(const char* compilerLang)
  622. : CompilerLanguage(compilerLang)
  623. {
  624. }
  625. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  626. std::string Evaluate(
  627. const std::vector<std::string>& parameters,
  628. cmGeneratorExpressionContext* context,
  629. const GeneratorExpressionContent* content,
  630. cmGeneratorExpressionDAGChecker* dagChecker) const override
  631. {
  632. if (!context->HeadTarget) {
  633. std::ostringstream e;
  634. e << "$<" << this->CompilerLanguage
  635. << "_COMPILER_VERSION> may only be used with binary targets. It "
  636. "may not be used with add_custom_command or add_custom_target.";
  637. reportError(context, content->GetOriginalExpression(), e.str());
  638. return {};
  639. }
  640. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  641. this->CompilerLanguage);
  642. }
  643. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  644. cmGeneratorExpressionContext* context,
  645. const GeneratorExpressionContent* content,
  646. cmGeneratorExpressionDAGChecker* /*unused*/,
  647. const std::string& lang) const
  648. {
  649. std::string const& compilerVersion =
  650. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  651. "_COMPILER_VERSION");
  652. if (parameters.empty()) {
  653. return compilerVersion;
  654. }
  655. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  656. if (!compilerIdValidator.find(parameters.front())) {
  657. reportError(context, content->GetOriginalExpression(),
  658. "Expression syntax not recognized.");
  659. return {};
  660. }
  661. if (compilerVersion.empty()) {
  662. return parameters.front().empty() ? "1" : "0";
  663. }
  664. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  665. parameters.front(), compilerVersion)
  666. ? "1"
  667. : "0";
  668. }
  669. const char* const CompilerLanguage;
  670. };
  671. static const CompilerVersionNode cCompilerVersionNode("C"),
  672. cxxCompilerVersionNode("CXX"), cudaCompilerVersionNode("CUDA"),
  673. objcCompilerVersionNode("OBJC"), objcxxCompilerVersionNode("OBJCXX"),
  674. fortranCompilerVersionNode("Fortran"), ispcCompilerVersionNode("ISPC"),
  675. hipCompilerVersionNode("HIP");
  676. struct PlatformIdNode : public cmGeneratorExpressionNode
  677. {
  678. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  679. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  680. std::string Evaluate(
  681. const std::vector<std::string>& parameters,
  682. cmGeneratorExpressionContext* context,
  683. const GeneratorExpressionContent* /*content*/,
  684. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  685. {
  686. std::string const& platformId =
  687. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  688. if (parameters.empty()) {
  689. return platformId;
  690. }
  691. if (platformId.empty()) {
  692. return parameters.front().empty() ? "1" : "0";
  693. }
  694. for (auto const& param : parameters) {
  695. if (param == platformId) {
  696. return "1";
  697. }
  698. }
  699. return "0";
  700. }
  701. } platformIdNode;
  702. template <cmSystemTools::CompareOp Op>
  703. struct VersionNode : public cmGeneratorExpressionNode
  704. {
  705. VersionNode() {} // NOLINT(modernize-use-equals-default)
  706. int NumExpectedParameters() const override { return 2; }
  707. std::string Evaluate(
  708. const std::vector<std::string>& parameters,
  709. cmGeneratorExpressionContext* /*context*/,
  710. const GeneratorExpressionContent* /*content*/,
  711. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  712. {
  713. return cmSystemTools::VersionCompare(Op, parameters.front(), parameters[1])
  714. ? "1"
  715. : "0";
  716. }
  717. };
  718. static const VersionNode<cmSystemTools::OP_GREATER> versionGreaterNode;
  719. static const VersionNode<cmSystemTools::OP_GREATER_EQUAL> versionGreaterEqNode;
  720. static const VersionNode<cmSystemTools::OP_LESS> versionLessNode;
  721. static const VersionNode<cmSystemTools::OP_LESS_EQUAL> versionLessEqNode;
  722. static const VersionNode<cmSystemTools::OP_EQUAL> versionEqualNode;
  723. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  724. {
  725. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  726. std::string Evaluate(
  727. const std::vector<std::string>& parameters,
  728. cmGeneratorExpressionContext* context,
  729. const GeneratorExpressionContent* content,
  730. cmGeneratorExpressionDAGChecker* dagChecker) const override
  731. {
  732. if (!dagChecker) {
  733. reportError(context, content->GetOriginalExpression(),
  734. "$<LINK_ONLY:...> may only be used for linking");
  735. return std::string();
  736. }
  737. if (!dagChecker->GetTransitivePropertiesOnly()) {
  738. return parameters.front();
  739. }
  740. return std::string();
  741. }
  742. } linkOnlyNode;
  743. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  744. {
  745. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  746. int NumExpectedParameters() const override { return 0; }
  747. std::string Evaluate(
  748. const std::vector<std::string>& /*parameters*/,
  749. cmGeneratorExpressionContext* context,
  750. const GeneratorExpressionContent* /*content*/,
  751. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  752. {
  753. context->HadContextSensitiveCondition = true;
  754. return context->Config;
  755. }
  756. } configurationNode;
  757. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  758. {
  759. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  760. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  761. std::string Evaluate(
  762. const std::vector<std::string>& parameters,
  763. cmGeneratorExpressionContext* context,
  764. const GeneratorExpressionContent* content,
  765. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  766. {
  767. if (parameters.empty()) {
  768. return configurationNode.Evaluate(parameters, context, content, nullptr);
  769. }
  770. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  771. if (!configValidator.find(parameters.front())) {
  772. reportError(context, content->GetOriginalExpression(),
  773. "Expression syntax not recognized.");
  774. return std::string();
  775. }
  776. context->HadContextSensitiveCondition = true;
  777. for (auto const& param : parameters) {
  778. if (context->Config.empty()) {
  779. if (param.empty()) {
  780. return "1";
  781. }
  782. } else if (cmsysString_strcasecmp(param.c_str(),
  783. context->Config.c_str()) == 0) {
  784. return "1";
  785. }
  786. }
  787. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  788. cmProp loc = nullptr;
  789. cmProp imp = nullptr;
  790. std::string suffix;
  791. if (context->CurrentTarget->Target->GetMappedConfig(context->Config, loc,
  792. imp, suffix)) {
  793. // This imported target has an appropriate location
  794. // for this (possibly mapped) config.
  795. // Check if there is a proper config mapping for the tested config.
  796. std::vector<std::string> mappedConfigs;
  797. std::string mapProp = cmStrCat(
  798. "MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(context->Config));
  799. if (cmProp mapValue = context->CurrentTarget->GetProperty(mapProp)) {
  800. cmExpandList(cmSystemTools::UpperCase(*mapValue), mappedConfigs);
  801. for (auto const& param : parameters) {
  802. if (cm::contains(mappedConfigs, cmSystemTools::UpperCase(param))) {
  803. return "1";
  804. }
  805. }
  806. }
  807. }
  808. }
  809. return "0";
  810. }
  811. } configurationTestNode;
  812. static const struct JoinNode : public cmGeneratorExpressionNode
  813. {
  814. JoinNode() {} // NOLINT(modernize-use-equals-default)
  815. int NumExpectedParameters() const override { return 2; }
  816. bool AcceptsArbitraryContentParameter() const override { return true; }
  817. std::string Evaluate(
  818. const std::vector<std::string>& parameters,
  819. cmGeneratorExpressionContext* /*context*/,
  820. const GeneratorExpressionContent* /*content*/,
  821. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  822. {
  823. std::vector<std::string> list = cmExpandedList(parameters.front());
  824. return cmJoin(list, parameters[1]);
  825. }
  826. } joinNode;
  827. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  828. {
  829. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  830. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  831. std::string Evaluate(
  832. const std::vector<std::string>& parameters,
  833. cmGeneratorExpressionContext* context,
  834. const GeneratorExpressionContent* content,
  835. cmGeneratorExpressionDAGChecker* dagChecker) const override
  836. {
  837. if (context->Language.empty() &&
  838. (!dagChecker || !dagChecker->EvaluatingCompileExpression())) {
  839. reportError(
  840. context, content->GetOriginalExpression(),
  841. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  842. "directories, compile definitions, compile options, and to evaluate "
  843. "components of the file(GENERATE) command.");
  844. return std::string();
  845. }
  846. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  847. std::string genName = gg->GetName();
  848. if (genName.find("Makefiles") == std::string::npos &&
  849. genName.find("Ninja") == std::string::npos &&
  850. genName.find("Visual Studio") == std::string::npos &&
  851. genName.find("Xcode") == std::string::npos &&
  852. genName.find("Watcom WMake") == std::string::npos) {
  853. reportError(context, content->GetOriginalExpression(),
  854. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  855. return std::string();
  856. }
  857. if (parameters.empty()) {
  858. return context->Language;
  859. }
  860. for (auto const& param : parameters) {
  861. if (context->Language == param) {
  862. return "1";
  863. }
  864. }
  865. return "0";
  866. }
  867. } languageNode;
  868. static const struct CompileLanguageAndIdNode : public cmGeneratorExpressionNode
  869. {
  870. CompileLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  871. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  872. std::string Evaluate(
  873. const std::vector<std::string>& parameters,
  874. cmGeneratorExpressionContext* context,
  875. const GeneratorExpressionContent* content,
  876. cmGeneratorExpressionDAGChecker* dagChecker) const override
  877. {
  878. if (!context->HeadTarget ||
  879. (context->Language.empty() &&
  880. (!dagChecker || !dagChecker->EvaluatingCompileExpression()))) {
  881. // reportError(context, content->GetOriginalExpression(), "");
  882. reportError(
  883. context, content->GetOriginalExpression(),
  884. "$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets "
  885. "to specify include directories, compile definitions, and compile "
  886. "options. It may not be used with the add_custom_command, "
  887. "add_custom_target, or file(GENERATE) commands.");
  888. return std::string();
  889. }
  890. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  891. std::string genName = gg->GetName();
  892. if (genName.find("Makefiles") == std::string::npos &&
  893. genName.find("Ninja") == std::string::npos &&
  894. genName.find("Visual Studio") == std::string::npos &&
  895. genName.find("Xcode") == std::string::npos &&
  896. genName.find("Watcom WMake") == std::string::npos) {
  897. reportError(
  898. context, content->GetOriginalExpression(),
  899. "$<COMPILE_LANG_AND_ID:lang,id> not supported for this generator.");
  900. return std::string();
  901. }
  902. const std::string& lang = context->Language;
  903. if (lang == parameters.front()) {
  904. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  905. parameters.cend());
  906. return CompilerIdNode{ lang.c_str() }.EvaluateWithLanguage(
  907. idParameter, context, content, dagChecker, lang);
  908. }
  909. return "0";
  910. }
  911. } languageAndIdNode;
  912. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  913. {
  914. LinkLanguageNode() {} // NOLINT(modernize-use-equals-default)
  915. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  916. std::string Evaluate(
  917. const std::vector<std::string>& parameters,
  918. cmGeneratorExpressionContext* context,
  919. const GeneratorExpressionContent* content,
  920. cmGeneratorExpressionDAGChecker* dagChecker) const override
  921. {
  922. if (!context->HeadTarget || !dagChecker ||
  923. !(dagChecker->EvaluatingLinkExpression() ||
  924. dagChecker->EvaluatingLinkLibraries())) {
  925. reportError(context, content->GetOriginalExpression(),
  926. "$<LINK_LANGUAGE:...> may only be used with binary targets "
  927. "to specify link libraries, link directories, link options "
  928. "and link depends.");
  929. return std::string();
  930. }
  931. if (dagChecker->EvaluatingLinkLibraries() && parameters.empty()) {
  932. reportError(
  933. context, content->GetOriginalExpression(),
  934. "$<LINK_LANGUAGE> is not supported in link libraries expression.");
  935. return std::string();
  936. }
  937. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  938. std::string genName = gg->GetName();
  939. if (genName.find("Makefiles") == std::string::npos &&
  940. genName.find("Ninja") == std::string::npos &&
  941. genName.find("Visual Studio") == std::string::npos &&
  942. genName.find("Xcode") == std::string::npos &&
  943. genName.find("Watcom WMake") == std::string::npos) {
  944. reportError(context, content->GetOriginalExpression(),
  945. "$<LINK_LANGUAGE:...> not supported for this generator.");
  946. return std::string();
  947. }
  948. if (dagChecker->EvaluatingLinkLibraries()) {
  949. context->HadHeadSensitiveCondition = true;
  950. context->HadLinkLanguageSensitiveCondition = true;
  951. }
  952. if (parameters.empty()) {
  953. return context->Language;
  954. }
  955. for (auto const& param : parameters) {
  956. if (context->Language == param) {
  957. return "1";
  958. }
  959. }
  960. return "0";
  961. }
  962. } linkLanguageNode;
  963. namespace {
  964. struct LinkerId
  965. {
  966. static std::string Evaluate(const std::vector<std::string>& parameters,
  967. cmGeneratorExpressionContext* context,
  968. const GeneratorExpressionContent* content,
  969. const std::string& lang)
  970. {
  971. std::string const& linkerId =
  972. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  973. "_COMPILER_ID");
  974. if (parameters.empty()) {
  975. return linkerId;
  976. }
  977. if (linkerId.empty()) {
  978. return parameters.front().empty() ? "1" : "0";
  979. }
  980. static cmsys::RegularExpression linkerIdValidator("^[A-Za-z0-9_]*$");
  981. for (auto const& param : parameters) {
  982. if (!linkerIdValidator.find(param)) {
  983. reportError(context, content->GetOriginalExpression(),
  984. "Expression syntax not recognized.");
  985. return std::string();
  986. }
  987. if (param == linkerId) {
  988. return "1";
  989. }
  990. }
  991. return "0";
  992. }
  993. };
  994. }
  995. static const struct LinkLanguageAndIdNode : public cmGeneratorExpressionNode
  996. {
  997. LinkLanguageAndIdNode() {} // NOLINT(modernize-use-equals-default)
  998. int NumExpectedParameters() const override { return TwoOrMoreParameters; }
  999. std::string Evaluate(
  1000. const std::vector<std::string>& parameters,
  1001. cmGeneratorExpressionContext* context,
  1002. const GeneratorExpressionContent* content,
  1003. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1004. {
  1005. if (!context->HeadTarget || !dagChecker ||
  1006. !(dagChecker->EvaluatingLinkExpression() ||
  1007. dagChecker->EvaluatingLinkLibraries())) {
  1008. reportError(
  1009. context, content->GetOriginalExpression(),
  1010. "$<LINK_LANG_AND_ID:lang,id> may only be used with binary targets "
  1011. "to specify link libraries, link directories, link options, and link "
  1012. "depends.");
  1013. return std::string();
  1014. }
  1015. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1016. std::string genName = gg->GetName();
  1017. if (genName.find("Makefiles") == std::string::npos &&
  1018. genName.find("Ninja") == std::string::npos &&
  1019. genName.find("Visual Studio") == std::string::npos &&
  1020. genName.find("Xcode") == std::string::npos &&
  1021. genName.find("Watcom WMake") == std::string::npos) {
  1022. reportError(
  1023. context, content->GetOriginalExpression(),
  1024. "$<LINK_LANG_AND_ID:lang,id> not supported for this generator.");
  1025. return std::string();
  1026. }
  1027. if (dagChecker->EvaluatingLinkLibraries()) {
  1028. context->HadHeadSensitiveCondition = true;
  1029. context->HadLinkLanguageSensitiveCondition = true;
  1030. }
  1031. const std::string& lang = context->Language;
  1032. if (lang == parameters.front()) {
  1033. std::vector<std::string> idParameter((parameters.cbegin() + 1),
  1034. parameters.cend());
  1035. return LinkerId::Evaluate(idParameter, context, content, lang);
  1036. }
  1037. return "0";
  1038. }
  1039. } linkLanguageAndIdNode;
  1040. static const struct HostLinkNode : public cmGeneratorExpressionNode
  1041. {
  1042. HostLinkNode() {} // NOLINT(modernize-use-equals-default)
  1043. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1044. std::string Evaluate(
  1045. const std::vector<std::string>& parameters,
  1046. cmGeneratorExpressionContext* context,
  1047. const GeneratorExpressionContent* content,
  1048. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1049. {
  1050. if (!context->HeadTarget || !dagChecker ||
  1051. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1052. reportError(context, content->GetOriginalExpression(),
  1053. "$<HOST_LINK:...> may only be used with binary targets "
  1054. "to specify link options.");
  1055. return std::string();
  1056. }
  1057. return context->HeadTarget->IsDeviceLink() ? std::string()
  1058. : cmJoin(parameters, ";");
  1059. }
  1060. } hostLinkNode;
  1061. static const struct DeviceLinkNode : public cmGeneratorExpressionNode
  1062. {
  1063. DeviceLinkNode() {} // NOLINT(modernize-use-equals-default)
  1064. int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
  1065. std::string Evaluate(
  1066. const std::vector<std::string>& parameters,
  1067. cmGeneratorExpressionContext* context,
  1068. const GeneratorExpressionContent* content,
  1069. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1070. {
  1071. if (!context->HeadTarget || !dagChecker ||
  1072. !dagChecker->EvaluatingLinkOptionsExpression()) {
  1073. reportError(context, content->GetOriginalExpression(),
  1074. "$<DEVICE_LINK:...> may only be used with binary targets "
  1075. "to specify link options.");
  1076. return std::string();
  1077. }
  1078. if (context->HeadTarget->IsDeviceLink()) {
  1079. std::vector<std::string> list;
  1080. cmExpandLists(parameters.begin(), parameters.end(), list);
  1081. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  1082. const auto DL_END = "</DEVICE_LINK>"_s;
  1083. cm::erase_if(list, [&](const std::string& item) {
  1084. return item == DL_BEGIN || item == DL_END;
  1085. });
  1086. list.insert(list.begin(), static_cast<std::string>(DL_BEGIN));
  1087. list.push_back(static_cast<std::string>(DL_END));
  1088. return cmJoin(list, ";");
  1089. }
  1090. return std::string();
  1091. }
  1092. } deviceLinkNode;
  1093. std::string getLinkedTargetsContent(
  1094. cmGeneratorTarget const* target, std::string const& prop,
  1095. cmGeneratorExpressionContext* context,
  1096. cmGeneratorExpressionDAGChecker* dagChecker)
  1097. {
  1098. std::string result;
  1099. if (cmLinkImplementationLibraries const* impl =
  1100. target->GetLinkImplementationLibraries(context->Config)) {
  1101. for (cmLinkImplItem const& lib : impl->Libraries) {
  1102. if (lib.Target) {
  1103. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1104. // caller's property and hand-evaluate it as if it were compiled.
  1105. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1106. cmGeneratorExpressionContext libContext(
  1107. target->GetLocalGenerator(), context->Config, context->Quiet, target,
  1108. target, context->EvaluateForBuildsystem, lib.Backtrace,
  1109. context->Language);
  1110. std::string libResult =
  1111. lib.Target->EvaluateInterfaceProperty(prop, &libContext, dagChecker);
  1112. if (!libResult.empty()) {
  1113. if (result.empty()) {
  1114. result = std::move(libResult);
  1115. } else {
  1116. result.reserve(result.size() + 1 + libResult.size());
  1117. result += ";";
  1118. result += libResult;
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. return result;
  1125. }
  1126. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1127. {
  1128. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1129. // This node handles errors on parameter count itself.
  1130. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1131. static const char* GetErrorText(std::string const& targetName,
  1132. std::string const& propertyName)
  1133. {
  1134. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1135. if (targetName.empty() && propertyName.empty()) {
  1136. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1137. "target name and property name.";
  1138. }
  1139. if (targetName.empty()) {
  1140. return "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1141. "target name.";
  1142. }
  1143. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1144. if (!propertyNameValidator.find(propertyName)) {
  1145. return "Target name and property name not supported.";
  1146. }
  1147. return "Target name not supported.";
  1148. }
  1149. return nullptr;
  1150. }
  1151. std::string Evaluate(
  1152. const std::vector<std::string>& parameters,
  1153. cmGeneratorExpressionContext* context,
  1154. const GeneratorExpressionContent* content,
  1155. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1156. {
  1157. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1158. cmGeneratorTarget const* target = nullptr;
  1159. std::string targetName;
  1160. std::string propertyName;
  1161. if (parameters.size() == 2) {
  1162. targetName = parameters[0];
  1163. propertyName = parameters[1];
  1164. if (const char* e = GetErrorText(targetName, propertyName)) {
  1165. reportError(context, content->GetOriginalExpression(), e);
  1166. return std::string();
  1167. }
  1168. if (propertyName == "ALIASED_TARGET"_s) {
  1169. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1170. if (cmGeneratorTarget* tgt =
  1171. context->LG->FindGeneratorTargetToUse(targetName)) {
  1172. return tgt->GetName();
  1173. }
  1174. }
  1175. return std::string();
  1176. }
  1177. if (propertyName == "ALIAS_GLOBAL"_s) {
  1178. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1179. return context->LG->GetGlobalGenerator()->IsAlias(targetName)
  1180. ? "TRUE"
  1181. : "FALSE";
  1182. }
  1183. return std::string();
  1184. }
  1185. target = context->LG->FindGeneratorTargetToUse(targetName);
  1186. if (!target) {
  1187. std::ostringstream e;
  1188. e << "Target \"" << targetName << "\" not found.";
  1189. reportError(context, content->GetOriginalExpression(), e.str());
  1190. return std::string();
  1191. }
  1192. context->AllTargets.insert(target);
  1193. } else if (parameters.size() == 1) {
  1194. target = context->HeadTarget;
  1195. propertyName = parameters[0];
  1196. // Keep track of the properties seen while processing.
  1197. // The evaluation of the LINK_LIBRARIES generator expressions
  1198. // will check this to ensure that properties have one consistent
  1199. // value for all evaluations.
  1200. context->SeenTargetProperties.insert(propertyName);
  1201. context->HadHeadSensitiveCondition = true;
  1202. if (!target) {
  1203. reportError(
  1204. context, content->GetOriginalExpression(),
  1205. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1206. "It may not be used with add_custom_command or add_custom_target. "
  1207. " "
  1208. "Specify the target to read a property from using the "
  1209. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1210. return std::string();
  1211. }
  1212. } else {
  1213. reportError(
  1214. context, content->GetOriginalExpression(),
  1215. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1216. return std::string();
  1217. }
  1218. if (propertyName == "SOURCES") {
  1219. context->SourceSensitiveTargets.insert(target);
  1220. }
  1221. if (propertyName.empty()) {
  1222. reportError(
  1223. context, content->GetOriginalExpression(),
  1224. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1225. "name.");
  1226. return std::string();
  1227. }
  1228. if (!propertyNameValidator.find(propertyName)) {
  1229. ::reportError(context, content->GetOriginalExpression(),
  1230. "Property name not supported.");
  1231. return std::string();
  1232. }
  1233. assert(target);
  1234. if (propertyName == "LINKER_LANGUAGE") {
  1235. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1236. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1237. dagCheckerParent->EvaluatingSources())) {
  1238. reportError(
  1239. context, content->GetOriginalExpression(),
  1240. "LINKER_LANGUAGE target property can not be used while evaluating "
  1241. "link libraries for a static library");
  1242. return std::string();
  1243. }
  1244. return target->GetLinkerLanguage(context->Config);
  1245. }
  1246. std::string interfacePropertyName;
  1247. bool isInterfaceProperty = false;
  1248. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1249. if (propertyName == #prop) { \
  1250. interfacePropertyName = "INTERFACE_" #prop; \
  1251. } else if (propertyName == "INTERFACE_" #prop) { \
  1252. interfacePropertyName = "INTERFACE_" #prop; \
  1253. isInterfaceProperty = true; \
  1254. } else
  1255. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1256. // Note that the above macro terminates with an else
  1257. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1258. cmPolicies::PolicyStatus polSt =
  1259. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1260. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1261. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1262. }
  1263. }
  1264. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1265. bool evaluatingLinkLibraries = false;
  1266. if (dagCheckerParent) {
  1267. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1268. dagCheckerParent->EvaluatingPICExpression()) {
  1269. // No check required.
  1270. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1271. evaluatingLinkLibraries = true;
  1272. if (!interfacePropertyName.empty()) {
  1273. reportError(
  1274. context, content->GetOriginalExpression(),
  1275. "$<TARGET_PROPERTY:...> expression in link libraries "
  1276. "evaluation depends on target property which is transitive "
  1277. "over the link libraries, creating a recursion.");
  1278. return std::string();
  1279. }
  1280. } else {
  1281. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1282. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1283. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1284. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1285. }
  1286. }
  1287. if (isInterfaceProperty) {
  1288. return cmGeneratorExpression::StripEmptyListElements(
  1289. target->EvaluateInterfaceProperty(propertyName, context,
  1290. dagCheckerParent));
  1291. }
  1292. cmGeneratorExpressionDAGChecker dagChecker(
  1293. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1294. switch (dagChecker.Check()) {
  1295. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1296. dagChecker.ReportError(context, content->GetOriginalExpression());
  1297. return std::string();
  1298. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1299. // No error. We just skip cyclic references.
  1300. return std::string();
  1301. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1302. // We handle transitive properties above. For non-transitive
  1303. // properties we accept repeats anyway.
  1304. case cmGeneratorExpressionDAGChecker::DAG:
  1305. break;
  1306. }
  1307. std::string result;
  1308. bool haveProp = false;
  1309. if (cmProp p = target->GetProperty(propertyName)) {
  1310. result = *p;
  1311. haveProp = true;
  1312. } else if (evaluatingLinkLibraries) {
  1313. return std::string();
  1314. }
  1315. if (!haveProp && !target->IsImported() &&
  1316. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1317. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1318. context->Config)) {
  1319. context->HadContextSensitiveCondition = true;
  1320. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1321. context->Config)
  1322. ? "1"
  1323. : "0";
  1324. }
  1325. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1326. context->Config)) {
  1327. context->HadContextSensitiveCondition = true;
  1328. const char* propContent =
  1329. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1330. context->Config);
  1331. return propContent ? propContent : "";
  1332. }
  1333. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1334. context->Config)) {
  1335. context->HadContextSensitiveCondition = true;
  1336. const char* propContent =
  1337. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1338. context->Config);
  1339. return propContent ? propContent : "";
  1340. }
  1341. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1342. context->Config)) {
  1343. context->HadContextSensitiveCondition = true;
  1344. const char* propContent =
  1345. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1346. context->Config);
  1347. return propContent ? propContent : "";
  1348. }
  1349. }
  1350. if (!target->IsImported() && dagCheckerParent &&
  1351. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1352. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1353. context->Config)) {
  1354. context->HadContextSensitiveCondition = true;
  1355. const char* propContent =
  1356. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1357. context->Config);
  1358. return propContent ? propContent : "";
  1359. }
  1360. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1361. context->Config)) {
  1362. context->HadContextSensitiveCondition = true;
  1363. const char* propContent =
  1364. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1365. context->Config);
  1366. return propContent ? propContent : "";
  1367. }
  1368. }
  1369. if (!interfacePropertyName.empty()) {
  1370. result = cmGeneratorExpression::StripEmptyListElements(
  1371. this->EvaluateDependentExpression(result, context->LG, context, target,
  1372. &dagChecker, target));
  1373. std::string linkedTargetsContent = getLinkedTargetsContent(
  1374. target, interfacePropertyName, context, &dagChecker);
  1375. if (!linkedTargetsContent.empty()) {
  1376. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1377. }
  1378. }
  1379. return result;
  1380. }
  1381. } targetPropertyNode;
  1382. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1383. {
  1384. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1385. bool GeneratesContent() const override { return true; }
  1386. bool AcceptsArbitraryContentParameter() const override { return true; }
  1387. bool RequiresLiteralInput() const override { return true; }
  1388. std::string Evaluate(
  1389. const std::vector<std::string>& parameters,
  1390. cmGeneratorExpressionContext* /*context*/,
  1391. const GeneratorExpressionContent* /*content*/,
  1392. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1393. {
  1394. return parameters.front();
  1395. }
  1396. int NumExpectedParameters() const override { return 1; }
  1397. } targetNameNode;
  1398. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1399. {
  1400. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1401. std::string Evaluate(
  1402. const std::vector<std::string>& parameters,
  1403. cmGeneratorExpressionContext* context,
  1404. const GeneratorExpressionContent* content,
  1405. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1406. {
  1407. std::string tgtName = parameters.front();
  1408. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1409. if (!gt) {
  1410. std::ostringstream e;
  1411. e << "Objects of target \"" << tgtName
  1412. << "\" referenced but no such target exists.";
  1413. reportError(context, content->GetOriginalExpression(), e.str());
  1414. return std::string();
  1415. }
  1416. cmStateEnums::TargetType type = gt->GetType();
  1417. if (type != cmStateEnums::EXECUTABLE &&
  1418. type != cmStateEnums::STATIC_LIBRARY &&
  1419. type != cmStateEnums::SHARED_LIBRARY &&
  1420. type != cmStateEnums::MODULE_LIBRARY &&
  1421. type != cmStateEnums::OBJECT_LIBRARY) {
  1422. std::ostringstream e;
  1423. e << "Objects of target \"" << tgtName
  1424. << "\" referenced but is not one of the allowed target types "
  1425. << "(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT).";
  1426. reportError(context, content->GetOriginalExpression(), e.str());
  1427. return std::string();
  1428. }
  1429. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1430. {
  1431. std::string reason;
  1432. if (!context->EvaluateForBuildsystem &&
  1433. !gg->HasKnownObjectFileLocation(&reason)) {
  1434. std::ostringstream e;
  1435. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1436. "is only suitable for consumption by CMake (limited"
  1437. << reason
  1438. << "). "
  1439. "It is not suitable for writing out elsewhere.";
  1440. reportError(context, content->GetOriginalExpression(), e.str());
  1441. return std::string();
  1442. }
  1443. }
  1444. std::vector<std::string> objects;
  1445. if (gt->IsImported()) {
  1446. cmProp loc = nullptr;
  1447. cmProp imp = nullptr;
  1448. std::string suffix;
  1449. if (gt->Target->GetMappedConfig(context->Config, loc, imp, suffix)) {
  1450. cmExpandList(*loc, objects);
  1451. }
  1452. context->HadContextSensitiveCondition = true;
  1453. } else {
  1454. gt->GetTargetObjectNames(context->Config, objects);
  1455. std::string obj_dir;
  1456. if (context->EvaluateForBuildsystem && !gg->SupportsCrossConfigs()) {
  1457. // Use object file directory with buildsystem placeholder.
  1458. obj_dir = gt->ObjectDirectory;
  1459. context->HadContextSensitiveCondition =
  1460. gt->HasContextDependentSources();
  1461. } else {
  1462. // Use object file directory with per-config location.
  1463. obj_dir = gt->GetObjectDirectory(context->Config);
  1464. context->HadContextSensitiveCondition = true;
  1465. }
  1466. for (std::string& o : objects) {
  1467. o = cmStrCat(obj_dir, o);
  1468. }
  1469. }
  1470. // Create the cmSourceFile instances in the referencing directory.
  1471. cmMakefile* mf = context->LG->GetMakefile();
  1472. for (std::string const& o : objects) {
  1473. mf->AddTargetObject(tgtName, o);
  1474. }
  1475. return cmJoin(objects, ";");
  1476. }
  1477. } targetObjectsNode;
  1478. static const struct TargetRuntimeDllsNode : public cmGeneratorExpressionNode
  1479. {
  1480. TargetRuntimeDllsNode() {} // NOLINT(modernize-use-equals-default)
  1481. std::string Evaluate(
  1482. const std::vector<std::string>& parameters,
  1483. cmGeneratorExpressionContext* context,
  1484. const GeneratorExpressionContent* content,
  1485. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1486. {
  1487. std::string tgtName = parameters.front();
  1488. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1489. if (!gt) {
  1490. std::ostringstream e;
  1491. e << "Objects of target \"" << tgtName
  1492. << "\" referenced but no such target exists.";
  1493. reportError(context, content->GetOriginalExpression(), e.str());
  1494. return std::string();
  1495. }
  1496. cmStateEnums::TargetType type = gt->GetType();
  1497. if (type != cmStateEnums::EXECUTABLE &&
  1498. type != cmStateEnums::SHARED_LIBRARY &&
  1499. type != cmStateEnums::MODULE_LIBRARY) {
  1500. std::ostringstream e;
  1501. e << "Objects of target \"" << tgtName
  1502. << "\" referenced but is not one of the allowed target types "
  1503. << "(EXECUTABLE, SHARED, MODULE).";
  1504. reportError(context, content->GetOriginalExpression(), e.str());
  1505. return std::string();
  1506. }
  1507. if (auto* cli = gt->GetLinkInformation(context->Config)) {
  1508. std::vector<std::string> dllPaths;
  1509. auto const& dlls = cli->GetRuntimeDLLs();
  1510. for (auto const& dll : dlls) {
  1511. if (auto loc = dll->MaybeGetLocation(context->Config)) {
  1512. dllPaths.emplace_back(*loc);
  1513. }
  1514. }
  1515. return cmJoin(dllPaths, ";");
  1516. }
  1517. return "";
  1518. }
  1519. } targetRuntimeDllsNode;
  1520. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1521. {
  1522. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1523. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1524. std::string Evaluate(
  1525. const std::vector<std::string>& parameters,
  1526. cmGeneratorExpressionContext* context,
  1527. const GeneratorExpressionContent* content,
  1528. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1529. {
  1530. cmGeneratorTarget const* target = context->HeadTarget;
  1531. if (!target) {
  1532. reportError(
  1533. context, content->GetOriginalExpression(),
  1534. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1535. "not be used with add_custom_command or add_custom_target.");
  1536. return std::string();
  1537. }
  1538. context->HadHeadSensitiveCondition = true;
  1539. using LangMap = std::map<std::string, std::vector<std::string>>;
  1540. static LangMap availableFeatures;
  1541. LangMap testedFeatures;
  1542. cmStandardLevelResolver standardResolver(context->LG->GetMakefile());
  1543. for (std::string const& p : parameters) {
  1544. std::string error;
  1545. std::string lang;
  1546. if (!standardResolver.CompileFeatureKnown(
  1547. context->HeadTarget->Target->GetName(), p, lang, &error)) {
  1548. reportError(context, content->GetOriginalExpression(), error);
  1549. return std::string();
  1550. }
  1551. testedFeatures[lang].push_back(p);
  1552. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1553. cmProp featuresKnown =
  1554. standardResolver.CompileFeaturesAvailable(lang, &error);
  1555. if (!featuresKnown) {
  1556. reportError(context, content->GetOriginalExpression(), error);
  1557. return std::string();
  1558. }
  1559. cmExpandList(featuresKnown, availableFeatures[lang]);
  1560. }
  1561. }
  1562. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1563. for (auto const& lit : testedFeatures) {
  1564. std::vector<std::string> const& langAvailable =
  1565. availableFeatures[lit.first];
  1566. cmProp standardDefault = context->LG->GetMakefile()->GetDefinition(
  1567. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1568. for (std::string const& it : lit.second) {
  1569. if (!cm::contains(langAvailable, it)) {
  1570. return "0";
  1571. }
  1572. if (standardDefault && standardDefault->empty()) {
  1573. // This compiler has no notion of language standard levels.
  1574. // All features known for the language are always available.
  1575. continue;
  1576. }
  1577. if (!standardResolver.HaveStandardAvailable(target, lit.first,
  1578. context->Config, it)) {
  1579. if (evalLL) {
  1580. cmProp l = target->GetLanguageStandard(lit.first, context->Config);
  1581. if (!l) {
  1582. l = standardDefault;
  1583. }
  1584. assert(l);
  1585. context->MaxLanguageStandard[target][lit.first] = *l;
  1586. } else {
  1587. return "0";
  1588. }
  1589. }
  1590. }
  1591. }
  1592. return "1";
  1593. }
  1594. } compileFeaturesNode;
  1595. static const char* targetPolicyWhitelist[] = {
  1596. nullptr
  1597. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1598. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1599. #undef TARGET_POLICY_STRING
  1600. };
  1601. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1602. const char* policy)
  1603. {
  1604. #define RETURN_POLICY(POLICY) \
  1605. if (strcmp(policy, #POLICY) == 0) { \
  1606. return tgt->GetPolicyStatus##POLICY(); \
  1607. }
  1608. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1609. #undef RETURN_POLICY
  1610. assert(false && "Unreachable code. Not a valid policy");
  1611. return cmPolicies::WARN;
  1612. }
  1613. cmPolicies::PolicyID policyForString(const char* policy_id)
  1614. {
  1615. #define RETURN_POLICY_ID(POLICY_ID) \
  1616. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1617. return cmPolicies::POLICY_ID; \
  1618. }
  1619. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1620. #undef RETURN_POLICY_ID
  1621. assert(false && "Unreachable code. Not a valid policy");
  1622. return cmPolicies::CMP0002;
  1623. }
  1624. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1625. {
  1626. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1627. int NumExpectedParameters() const override { return 1; }
  1628. std::string Evaluate(
  1629. const std::vector<std::string>& parameters,
  1630. cmGeneratorExpressionContext* context,
  1631. const GeneratorExpressionContent* content,
  1632. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1633. {
  1634. if (!context->HeadTarget) {
  1635. reportError(
  1636. context, content->GetOriginalExpression(),
  1637. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1638. "may not be used with add_custom_command or add_custom_target.");
  1639. return std::string();
  1640. }
  1641. context->HadContextSensitiveCondition = true;
  1642. context->HadHeadSensitiveCondition = true;
  1643. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1644. const char* policy = targetPolicyWhitelist[i];
  1645. if (parameters.front() == policy) {
  1646. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1647. switch (statusForTarget(context->HeadTarget, policy)) {
  1648. case cmPolicies::WARN:
  1649. lg->IssueMessage(
  1650. MessageType::AUTHOR_WARNING,
  1651. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1652. CM_FALLTHROUGH;
  1653. case cmPolicies::REQUIRED_IF_USED:
  1654. case cmPolicies::REQUIRED_ALWAYS:
  1655. case cmPolicies::OLD:
  1656. return "0";
  1657. case cmPolicies::NEW:
  1658. return "1";
  1659. }
  1660. }
  1661. }
  1662. reportError(
  1663. context, content->GetOriginalExpression(),
  1664. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1665. "policies. Currently it may be used with the following policies:\n"
  1666. #define STRINGIFY_HELPER(X) #X
  1667. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1668. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1669. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1670. #undef TARGET_POLICY_LIST_ITEM
  1671. );
  1672. return std::string();
  1673. }
  1674. } targetPolicyNode;
  1675. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1676. {
  1677. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1678. bool GeneratesContent() const override { return true; }
  1679. int NumExpectedParameters() const override { return 0; }
  1680. std::string Evaluate(
  1681. const std::vector<std::string>& /*parameters*/,
  1682. cmGeneratorExpressionContext* context,
  1683. const GeneratorExpressionContent* content,
  1684. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1685. {
  1686. reportError(context, content->GetOriginalExpression(),
  1687. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1688. "should never be evaluated.");
  1689. return std::string();
  1690. }
  1691. } installPrefixNode;
  1692. class ArtifactDirTag;
  1693. class ArtifactLinkerTag;
  1694. class ArtifactNameTag;
  1695. class ArtifactPathTag;
  1696. class ArtifactPdbTag;
  1697. class ArtifactSonameTag;
  1698. class ArtifactBundleDirTag;
  1699. class ArtifactBundleContentDirTag;
  1700. template <typename ArtifactT, typename ComponentT>
  1701. struct TargetFilesystemArtifactDependency
  1702. {
  1703. static void AddDependency(cmGeneratorTarget* target,
  1704. cmGeneratorExpressionContext* context)
  1705. {
  1706. context->DependTargets.insert(target);
  1707. context->AllTargets.insert(target);
  1708. }
  1709. };
  1710. struct TargetFilesystemArtifactDependencyCMP0112
  1711. {
  1712. static void AddDependency(cmGeneratorTarget* target,
  1713. cmGeneratorExpressionContext* context)
  1714. {
  1715. context->AllTargets.insert(target);
  1716. cmLocalGenerator* lg = context->LG;
  1717. switch (target->GetPolicyStatusCMP0112()) {
  1718. case cmPolicies::WARN:
  1719. if (lg->GetMakefile()->PolicyOptionalWarningEnabled(
  1720. "CMAKE_POLICY_WARNING_CMP0112")) {
  1721. std::string err =
  1722. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0112),
  1723. "\nDependency being added to target:\n \"",
  1724. target->GetName(), "\"\n");
  1725. lg->GetCMakeInstance()->IssueMessage(MessageType ::AUTHOR_WARNING,
  1726. err, context->Backtrace);
  1727. }
  1728. CM_FALLTHROUGH;
  1729. case cmPolicies::OLD:
  1730. context->DependTargets.insert(target);
  1731. break;
  1732. case cmPolicies::REQUIRED_IF_USED:
  1733. case cmPolicies::REQUIRED_ALWAYS:
  1734. case cmPolicies::NEW:
  1735. break;
  1736. }
  1737. }
  1738. };
  1739. template <typename ArtifactT>
  1740. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactNameTag>
  1741. : TargetFilesystemArtifactDependencyCMP0112
  1742. {
  1743. };
  1744. template <typename ArtifactT>
  1745. struct TargetFilesystemArtifactDependency<ArtifactT, ArtifactDirTag>
  1746. : TargetFilesystemArtifactDependencyCMP0112
  1747. {
  1748. };
  1749. template <>
  1750. struct TargetFilesystemArtifactDependency<ArtifactBundleDirTag,
  1751. ArtifactPathTag>
  1752. : TargetFilesystemArtifactDependencyCMP0112
  1753. {
  1754. };
  1755. template <>
  1756. struct TargetFilesystemArtifactDependency<ArtifactBundleContentDirTag,
  1757. ArtifactPathTag>
  1758. : TargetFilesystemArtifactDependencyCMP0112
  1759. {
  1760. };
  1761. template <typename ArtifactT>
  1762. struct TargetFilesystemArtifactResultCreator
  1763. {
  1764. static std::string Create(cmGeneratorTarget* target,
  1765. cmGeneratorExpressionContext* context,
  1766. const GeneratorExpressionContent* content);
  1767. };
  1768. template <>
  1769. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1770. {
  1771. static std::string Create(cmGeneratorTarget* target,
  1772. cmGeneratorExpressionContext* context,
  1773. const GeneratorExpressionContent* content)
  1774. {
  1775. // The target soname file (.so.1).
  1776. if (target->IsDLLPlatform()) {
  1777. ::reportError(context, content->GetOriginalExpression(),
  1778. "TARGET_SONAME_FILE is not allowed "
  1779. "for DLL target platforms.");
  1780. return std::string();
  1781. }
  1782. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1783. ::reportError(context, content->GetOriginalExpression(),
  1784. "TARGET_SONAME_FILE is allowed only for "
  1785. "SHARED libraries.");
  1786. return std::string();
  1787. }
  1788. std::string result = cmStrCat(target->GetDirectory(context->Config), '/',
  1789. target->GetSOName(context->Config));
  1790. return result;
  1791. }
  1792. };
  1793. template <>
  1794. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1795. {
  1796. static std::string Create(cmGeneratorTarget* target,
  1797. cmGeneratorExpressionContext* context,
  1798. const GeneratorExpressionContent* content)
  1799. {
  1800. if (target->IsImported()) {
  1801. ::reportError(context, content->GetOriginalExpression(),
  1802. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1803. return std::string();
  1804. }
  1805. std::string language = target->GetLinkerLanguage(context->Config);
  1806. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1807. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1808. ::reportError(context, content->GetOriginalExpression(),
  1809. "TARGET_PDB_FILE is not supported by the target linker.");
  1810. return std::string();
  1811. }
  1812. cmStateEnums::TargetType targetType = target->GetType();
  1813. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1814. targetType != cmStateEnums::MODULE_LIBRARY &&
  1815. targetType != cmStateEnums::EXECUTABLE) {
  1816. ::reportError(context, content->GetOriginalExpression(),
  1817. "TARGET_PDB_FILE is allowed only for "
  1818. "targets with linker created artifacts.");
  1819. return std::string();
  1820. }
  1821. std::string result = cmStrCat(target->GetPDBDirectory(context->Config),
  1822. '/', target->GetPDBName(context->Config));
  1823. return result;
  1824. }
  1825. };
  1826. template <>
  1827. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1828. {
  1829. static std::string Create(cmGeneratorTarget* target,
  1830. cmGeneratorExpressionContext* context,
  1831. const GeneratorExpressionContent* content)
  1832. {
  1833. // The file used to link to the target (.so, .lib, .a).
  1834. if (!target->IsLinkable()) {
  1835. ::reportError(context, content->GetOriginalExpression(),
  1836. "TARGET_LINKER_FILE is allowed only for libraries and "
  1837. "executables with ENABLE_EXPORTS.");
  1838. return std::string();
  1839. }
  1840. cmStateEnums::ArtifactType artifact =
  1841. target->HasImportLibrary(context->Config)
  1842. ? cmStateEnums::ImportLibraryArtifact
  1843. : cmStateEnums::RuntimeBinaryArtifact;
  1844. return target->GetFullPath(context->Config, artifact);
  1845. }
  1846. };
  1847. template <>
  1848. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1849. {
  1850. static std::string Create(cmGeneratorTarget* target,
  1851. cmGeneratorExpressionContext* context,
  1852. const GeneratorExpressionContent* content)
  1853. {
  1854. if (target->IsImported()) {
  1855. ::reportError(context, content->GetOriginalExpression(),
  1856. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1857. return std::string();
  1858. }
  1859. if (!target->IsBundleOnApple()) {
  1860. ::reportError(context, content->GetOriginalExpression(),
  1861. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1862. return std::string();
  1863. }
  1864. std::string outpath = target->GetDirectory(context->Config) + '/';
  1865. return target->BuildBundleDirectory(outpath, context->Config,
  1866. cmGeneratorTarget::BundleDirLevel);
  1867. }
  1868. };
  1869. template <>
  1870. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1871. {
  1872. static std::string Create(cmGeneratorTarget* target,
  1873. cmGeneratorExpressionContext* context,
  1874. const GeneratorExpressionContent* content)
  1875. {
  1876. if (target->IsImported()) {
  1877. ::reportError(
  1878. context, content->GetOriginalExpression(),
  1879. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1880. return std::string();
  1881. }
  1882. if (!target->IsBundleOnApple()) {
  1883. ::reportError(
  1884. context, content->GetOriginalExpression(),
  1885. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1886. return std::string();
  1887. }
  1888. std::string outpath = target->GetDirectory(context->Config) + '/';
  1889. return target->BuildBundleDirectory(outpath, context->Config,
  1890. cmGeneratorTarget::ContentLevel);
  1891. }
  1892. };
  1893. template <>
  1894. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1895. {
  1896. static std::string Create(cmGeneratorTarget* target,
  1897. cmGeneratorExpressionContext* context,
  1898. const GeneratorExpressionContent* /*unused*/)
  1899. {
  1900. return target->GetFullPath(context->Config,
  1901. cmStateEnums::RuntimeBinaryArtifact, true);
  1902. }
  1903. };
  1904. template <typename ArtifactT>
  1905. struct TargetFilesystemArtifactResultGetter
  1906. {
  1907. static std::string Get(const std::string& result);
  1908. };
  1909. template <>
  1910. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1911. {
  1912. static std::string Get(const std::string& result)
  1913. {
  1914. return cmSystemTools::GetFilenameName(result);
  1915. }
  1916. };
  1917. template <>
  1918. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1919. {
  1920. static std::string Get(const std::string& result)
  1921. {
  1922. return cmSystemTools::GetFilenamePath(result);
  1923. }
  1924. };
  1925. template <>
  1926. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1927. {
  1928. static std::string Get(const std::string& result) { return result; }
  1929. };
  1930. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1931. {
  1932. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1933. protected:
  1934. cmGeneratorTarget* GetTarget(
  1935. const std::vector<std::string>& parameters,
  1936. cmGeneratorExpressionContext* context,
  1937. const GeneratorExpressionContent* content,
  1938. cmGeneratorExpressionDAGChecker* dagChecker) const
  1939. {
  1940. // Lookup the referenced target.
  1941. std::string name = parameters.front();
  1942. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1943. ::reportError(context, content->GetOriginalExpression(),
  1944. "Expression syntax not recognized.");
  1945. return nullptr;
  1946. }
  1947. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1948. if (!target) {
  1949. ::reportError(context, content->GetOriginalExpression(),
  1950. "No target \"" + name + "\"");
  1951. return nullptr;
  1952. }
  1953. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1954. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1955. ::reportError(context, content->GetOriginalExpression(),
  1956. "Target \"" + name +
  1957. "\" is not an executable or library.");
  1958. return nullptr;
  1959. }
  1960. if (dagChecker &&
  1961. (dagChecker->EvaluatingLinkLibraries(target) ||
  1962. (dagChecker->EvaluatingSources() &&
  1963. target == dagChecker->TopTarget()))) {
  1964. ::reportError(context, content->GetOriginalExpression(),
  1965. "Expressions which require the linker language may not "
  1966. "be used while evaluating link libraries");
  1967. return nullptr;
  1968. }
  1969. return target;
  1970. }
  1971. };
  1972. template <typename ArtifactT, typename ComponentT>
  1973. struct TargetFilesystemArtifact : public TargetArtifactBase
  1974. {
  1975. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1976. int NumExpectedParameters() const override { return 1; }
  1977. std::string Evaluate(
  1978. const std::vector<std::string>& parameters,
  1979. cmGeneratorExpressionContext* context,
  1980. const GeneratorExpressionContent* content,
  1981. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1982. {
  1983. cmGeneratorTarget* target =
  1984. this->GetTarget(parameters, context, content, dagChecker);
  1985. if (!target) {
  1986. return std::string();
  1987. }
  1988. // Not a dependent target if we are querying for ArtifactDirTag,
  1989. // ArtifactNameTag, ArtifactBundleDirTag, and ArtifactBundleContentDirTag
  1990. TargetFilesystemArtifactDependency<ArtifactT, ComponentT>::AddDependency(
  1991. target, context);
  1992. std::string result =
  1993. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1994. content);
  1995. if (context->HadError) {
  1996. return std::string();
  1997. }
  1998. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1999. }
  2000. };
  2001. template <typename ArtifactT>
  2002. struct TargetFilesystemArtifactNodeGroup
  2003. {
  2004. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  2005. {
  2006. }
  2007. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  2008. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  2009. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  2010. };
  2011. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  2012. targetNodeGroup;
  2013. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  2014. targetLinkerNodeGroup;
  2015. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  2016. targetSoNameNodeGroup;
  2017. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  2018. targetPdbNodeGroup;
  2019. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  2020. targetBundleDirNode;
  2021. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  2022. ArtifactPathTag>
  2023. targetBundleContentDirNode;
  2024. //
  2025. // To retrieve base name for various artifacts
  2026. //
  2027. template <typename ArtifactT>
  2028. struct TargetOutputNameArtifactResultGetter
  2029. {
  2030. static std::string Get(cmGeneratorTarget* target,
  2031. cmGeneratorExpressionContext* context,
  2032. const GeneratorExpressionContent* content);
  2033. };
  2034. template <>
  2035. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  2036. {
  2037. static std::string Get(cmGeneratorTarget* target,
  2038. cmGeneratorExpressionContext* context,
  2039. const GeneratorExpressionContent* /*unused*/)
  2040. {
  2041. return target->GetOutputName(context->Config,
  2042. cmStateEnums::RuntimeBinaryArtifact) +
  2043. target->GetFilePostfix(context->Config);
  2044. }
  2045. };
  2046. template <>
  2047. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  2048. {
  2049. static std::string Get(cmGeneratorTarget* target,
  2050. cmGeneratorExpressionContext* context,
  2051. const GeneratorExpressionContent* content)
  2052. {
  2053. // The file used to link to the target (.so, .lib, .a).
  2054. if (!target->IsLinkable()) {
  2055. ::reportError(context, content->GetOriginalExpression(),
  2056. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  2057. "libraries and executables with ENABLE_EXPORTS.");
  2058. return std::string();
  2059. }
  2060. cmStateEnums::ArtifactType artifact =
  2061. target->HasImportLibrary(context->Config)
  2062. ? cmStateEnums::ImportLibraryArtifact
  2063. : cmStateEnums::RuntimeBinaryArtifact;
  2064. return target->GetOutputName(context->Config, artifact) +
  2065. target->GetFilePostfix(context->Config);
  2066. }
  2067. };
  2068. template <>
  2069. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  2070. {
  2071. static std::string Get(cmGeneratorTarget* target,
  2072. cmGeneratorExpressionContext* context,
  2073. const GeneratorExpressionContent* content)
  2074. {
  2075. if (target->IsImported()) {
  2076. ::reportError(
  2077. context, content->GetOriginalExpression(),
  2078. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  2079. return std::string();
  2080. }
  2081. std::string language = target->GetLinkerLanguage(context->Config);
  2082. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  2083. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  2084. ::reportError(
  2085. context, content->GetOriginalExpression(),
  2086. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  2087. return std::string();
  2088. }
  2089. cmStateEnums::TargetType targetType = target->GetType();
  2090. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  2091. targetType != cmStateEnums::MODULE_LIBRARY &&
  2092. targetType != cmStateEnums::EXECUTABLE) {
  2093. ::reportError(context, content->GetOriginalExpression(),
  2094. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  2095. "targets with linker created artifacts.");
  2096. return std::string();
  2097. }
  2098. return target->GetPDBOutputName(context->Config) +
  2099. target->GetFilePostfix(context->Config);
  2100. }
  2101. };
  2102. template <typename ArtifactT>
  2103. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  2104. {
  2105. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  2106. int NumExpectedParameters() const override { return 1; }
  2107. std::string Evaluate(
  2108. const std::vector<std::string>& parameters,
  2109. cmGeneratorExpressionContext* context,
  2110. const GeneratorExpressionContent* content,
  2111. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2112. {
  2113. cmGeneratorTarget* target =
  2114. this->GetTarget(parameters, context, content, dagChecker);
  2115. if (!target) {
  2116. return std::string();
  2117. }
  2118. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2119. target, context, content);
  2120. if (context->HadError) {
  2121. return std::string();
  2122. }
  2123. return result;
  2124. }
  2125. };
  2126. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2127. targetFileBaseNameNode;
  2128. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2129. targetLinkerFileBaseNameNode;
  2130. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2131. targetPdbFileBaseNameNode;
  2132. class ArtifactFilePrefixTag;
  2133. class ArtifactLinkerFilePrefixTag;
  2134. class ArtifactFileSuffixTag;
  2135. class ArtifactLinkerFileSuffixTag;
  2136. template <typename ArtifactT>
  2137. struct TargetFileArtifactResultGetter
  2138. {
  2139. static std::string Get(cmGeneratorTarget* target,
  2140. cmGeneratorExpressionContext* context,
  2141. const GeneratorExpressionContent* content);
  2142. };
  2143. template <>
  2144. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2145. {
  2146. static std::string Get(cmGeneratorTarget* target,
  2147. cmGeneratorExpressionContext* context,
  2148. const GeneratorExpressionContent*)
  2149. {
  2150. return target->GetFilePrefix(context->Config);
  2151. }
  2152. };
  2153. template <>
  2154. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2155. {
  2156. static std::string Get(cmGeneratorTarget* target,
  2157. cmGeneratorExpressionContext* context,
  2158. const GeneratorExpressionContent* content)
  2159. {
  2160. if (!target->IsLinkable()) {
  2161. ::reportError(context, content->GetOriginalExpression(),
  2162. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2163. "executables with ENABLE_EXPORTS.");
  2164. return std::string();
  2165. }
  2166. cmStateEnums::ArtifactType artifact =
  2167. target->HasImportLibrary(context->Config)
  2168. ? cmStateEnums::ImportLibraryArtifact
  2169. : cmStateEnums::RuntimeBinaryArtifact;
  2170. return target->GetFilePrefix(context->Config, artifact);
  2171. }
  2172. };
  2173. template <>
  2174. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2175. {
  2176. static std::string Get(cmGeneratorTarget* target,
  2177. cmGeneratorExpressionContext* context,
  2178. const GeneratorExpressionContent*)
  2179. {
  2180. return target->GetFileSuffix(context->Config);
  2181. }
  2182. };
  2183. template <>
  2184. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2185. {
  2186. static std::string Get(cmGeneratorTarget* target,
  2187. cmGeneratorExpressionContext* context,
  2188. const GeneratorExpressionContent* content)
  2189. {
  2190. if (!target->IsLinkable()) {
  2191. ::reportError(context, content->GetOriginalExpression(),
  2192. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2193. "executables with ENABLE_EXPORTS.");
  2194. return std::string();
  2195. }
  2196. cmStateEnums::ArtifactType artifact =
  2197. target->HasImportLibrary(context->Config)
  2198. ? cmStateEnums::ImportLibraryArtifact
  2199. : cmStateEnums::RuntimeBinaryArtifact;
  2200. return target->GetFileSuffix(context->Config, artifact);
  2201. }
  2202. };
  2203. template <typename ArtifactT>
  2204. struct TargetFileArtifact : public TargetArtifactBase
  2205. {
  2206. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2207. int NumExpectedParameters() const override { return 1; }
  2208. std::string Evaluate(
  2209. const std::vector<std::string>& parameters,
  2210. cmGeneratorExpressionContext* context,
  2211. const GeneratorExpressionContent* content,
  2212. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2213. {
  2214. cmGeneratorTarget* target =
  2215. this->GetTarget(parameters, context, content, dagChecker);
  2216. if (!target) {
  2217. return std::string();
  2218. }
  2219. std::string result =
  2220. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2221. if (context->HadError) {
  2222. return std::string();
  2223. }
  2224. return result;
  2225. }
  2226. };
  2227. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2228. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2229. targetLinkerFilePrefixNode;
  2230. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2231. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2232. targetLinkerFileSuffixNode;
  2233. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2234. {
  2235. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2236. std::string Evaluate(
  2237. const std::vector<std::string>& parameters,
  2238. cmGeneratorExpressionContext* context,
  2239. const GeneratorExpressionContent* content,
  2240. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2241. {
  2242. std::vector<std::string> listIn = cmExpandedList(parameters.front());
  2243. if (listIn.empty()) {
  2244. reportError(context, content->GetOriginalExpression(),
  2245. "\"\" is not an absolute path.");
  2246. return std::string();
  2247. }
  2248. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2249. cmOutputConverter converter(snapshot);
  2250. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2251. std::vector<std::string> listOut;
  2252. listOut.reserve(listIn.size());
  2253. for (auto const& in : listIn) {
  2254. if (!cmSystemTools::FileIsFullPath(in)) {
  2255. reportError(context, content->GetOriginalExpression(),
  2256. "\"" + in + "\" is not an absolute path.");
  2257. return std::string();
  2258. }
  2259. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2260. }
  2261. return cmJoin(listOut, separator);
  2262. }
  2263. } shellPathNode;
  2264. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2265. const std::string& identifier)
  2266. {
  2267. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2268. { "0", &zeroNode },
  2269. { "1", &oneNode },
  2270. { "AND", &andNode },
  2271. { "OR", &orNode },
  2272. { "NOT", &notNode },
  2273. { "C_COMPILER_ID", &cCompilerIdNode },
  2274. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2275. { "OBJC_COMPILER_ID", &objcCompilerIdNode },
  2276. { "OBJCXX_COMPILER_ID", &objcxxCompilerIdNode },
  2277. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2278. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2279. { "HIP_COMPILER_ID", &hipCompilerIdNode },
  2280. { "VERSION_GREATER", &versionGreaterNode },
  2281. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2282. { "VERSION_LESS", &versionLessNode },
  2283. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2284. { "VERSION_EQUAL", &versionEqualNode },
  2285. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2286. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2287. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2288. { "OBJC_COMPILER_VERSION", &objcCompilerVersionNode },
  2289. { "OBJCXX_COMPILER_VERSION", &objcxxCompilerVersionNode },
  2290. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2291. { "HIP_COMPILER_VERSION", &hipCompilerVersionNode },
  2292. { "PLATFORM_ID", &platformIdNode },
  2293. { "COMPILE_FEATURES", &compileFeaturesNode },
  2294. { "CONFIGURATION", &configurationNode },
  2295. { "CONFIG", &configurationTestNode },
  2296. { "TARGET_FILE", &targetNodeGroup.File },
  2297. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2298. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2299. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2300. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2301. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2302. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2303. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2304. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2305. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2306. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2307. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2308. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2309. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2310. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2311. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2312. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2313. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2314. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2315. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2316. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2317. { "STREQUAL", &strEqualNode },
  2318. { "EQUAL", &equalNode },
  2319. { "IN_LIST", &inListNode },
  2320. { "FILTER", &filterNode },
  2321. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2322. { "LOWER_CASE", &lowerCaseNode },
  2323. { "UPPER_CASE", &upperCaseNode },
  2324. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2325. { "BOOL", &boolNode },
  2326. { "IF", &ifNode },
  2327. { "ANGLE-R", &angle_rNode },
  2328. { "COMMA", &commaNode },
  2329. { "SEMICOLON", &semicolonNode },
  2330. { "TARGET_PROPERTY", &targetPropertyNode },
  2331. { "TARGET_NAME", &targetNameNode },
  2332. { "TARGET_OBJECTS", &targetObjectsNode },
  2333. { "TARGET_POLICY", &targetPolicyNode },
  2334. { "TARGET_EXISTS", &targetExistsNode },
  2335. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2336. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2337. { "TARGET_RUNTIME_DLLS", &targetRuntimeDllsNode },
  2338. { "GENEX_EVAL", &genexEvalNode },
  2339. { "BUILD_INTERFACE", &buildInterfaceNode },
  2340. { "INSTALL_INTERFACE", &installInterfaceNode },
  2341. { "INSTALL_PREFIX", &installPrefixNode },
  2342. { "JOIN", &joinNode },
  2343. { "LINK_ONLY", &linkOnlyNode },
  2344. { "COMPILE_LANG_AND_ID", &languageAndIdNode },
  2345. { "COMPILE_LANGUAGE", &languageNode },
  2346. { "LINK_LANG_AND_ID", &linkLanguageAndIdNode },
  2347. { "LINK_LANGUAGE", &linkLanguageNode },
  2348. { "HOST_LINK", &hostLinkNode },
  2349. { "DEVICE_LINK", &deviceLinkNode },
  2350. { "SHELL_PATH", &shellPathNode }
  2351. };
  2352. {
  2353. auto itr = nodeMap.find(identifier);
  2354. if (itr != nodeMap.end()) {
  2355. return itr->second;
  2356. }
  2357. }
  2358. return nullptr;
  2359. }
  2360. void reportError(cmGeneratorExpressionContext* context,
  2361. const std::string& expr, const std::string& result)
  2362. {
  2363. context->HadError = true;
  2364. if (context->Quiet) {
  2365. return;
  2366. }
  2367. std::ostringstream e;
  2368. /* clang-format off */
  2369. e << "Error evaluating generator expression:\n"
  2370. << " " << expr << "\n"
  2371. << result;
  2372. /* clang-format on */
  2373. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2374. e.str(), context->Backtrace);
  2375. }