cmGeneratorExpressionNode.cxx 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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 "cmAlgorithms.h"
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGeneratorExpressionContext.h"
  7. #include "cmGeneratorExpressionDAGChecker.h"
  8. #include "cmGeneratorExpressionEvaluator.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmLinkItem.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmOutputConverter.h"
  15. #include "cmPolicies.h"
  16. #include "cmStateTypes.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. #include "cmake.h"
  20. #include "cmsys/RegularExpression.hxx"
  21. #include "cmsys/String.h"
  22. #include <algorithm>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <map>
  26. #include <memory> // IWYU pragma: keep
  27. #include <set>
  28. #include <sstream>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <utility>
  32. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  33. std::string const& prop, cmLocalGenerator* lg,
  34. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  35. cmGeneratorTarget const* currentTarget,
  36. cmGeneratorExpressionDAGChecker* dagChecker)
  37. {
  38. cmGeneratorExpression ge(context->Backtrace);
  39. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  40. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  41. std::string result =
  42. cge->Evaluate(lg, context->Config, context->Quiet, headTarget,
  43. currentTarget, dagChecker, context->Language);
  44. if (cge->GetHadContextSensitiveCondition()) {
  45. context->HadContextSensitiveCondition = true;
  46. }
  47. if (cge->GetHadHeadSensitiveCondition()) {
  48. context->HadHeadSensitiveCondition = true;
  49. }
  50. return result;
  51. }
  52. static const struct ZeroNode : public cmGeneratorExpressionNode
  53. {
  54. ZeroNode() {}
  55. bool GeneratesContent() const override { return false; }
  56. bool AcceptsArbitraryContentParameter() const override { return true; }
  57. std::string Evaluate(
  58. const std::vector<std::string>& /*parameters*/,
  59. cmGeneratorExpressionContext* /*context*/,
  60. const GeneratorExpressionContent* /*content*/,
  61. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  62. {
  63. return std::string();
  64. }
  65. } zeroNode;
  66. static const struct OneNode : public cmGeneratorExpressionNode
  67. {
  68. OneNode() {}
  69. bool AcceptsArbitraryContentParameter() const override { return true; }
  70. std::string Evaluate(
  71. const std::vector<std::string>& parameters,
  72. cmGeneratorExpressionContext* /*context*/,
  73. const GeneratorExpressionContent* /*content*/,
  74. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  75. {
  76. return parameters.front();
  77. }
  78. } oneNode;
  79. static const struct OneNode buildInterfaceNode;
  80. static const struct ZeroNode installInterfaceNode;
  81. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  82. static const struct OP##Node : public cmGeneratorExpressionNode \
  83. { \
  84. OP##Node() {} \
  85. virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
  86. \
  87. std::string Evaluate(const std::vector<std::string>& parameters, \
  88. cmGeneratorExpressionContext* context, \
  89. const GeneratorExpressionContent* content, \
  90. cmGeneratorExpressionDAGChecker*) const \
  91. { \
  92. std::vector<std::string>::const_iterator it = parameters.begin(); \
  93. const std::vector<std::string>::const_iterator end = parameters.end(); \
  94. for (; it != end; ++it) { \
  95. if (*it == #FAILURE_VALUE) { \
  96. return #FAILURE_VALUE; \
  97. } \
  98. if (*it != #SUCCESS_VALUE) { \
  99. reportError(context, content->GetOriginalExpression(), \
  100. "Parameters to $<" #OP \
  101. "> must resolve to either '0' or '1'."); \
  102. return std::string(); \
  103. } \
  104. } \
  105. return #SUCCESS_VALUE; \
  106. } \
  107. } OPNAME;
  108. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  109. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  110. #undef BOOLEAN_OP_NODE
  111. static const struct NotNode : public cmGeneratorExpressionNode
  112. {
  113. NotNode() {}
  114. std::string Evaluate(
  115. const std::vector<std::string>& parameters,
  116. cmGeneratorExpressionContext* context,
  117. const GeneratorExpressionContent* content,
  118. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  119. {
  120. if (*parameters.begin() != "0" && *parameters.begin() != "1") {
  121. reportError(
  122. context, content->GetOriginalExpression(),
  123. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  124. return std::string();
  125. }
  126. return *parameters.begin() == "0" ? "1" : "0";
  127. }
  128. } notNode;
  129. static const struct BoolNode : public cmGeneratorExpressionNode
  130. {
  131. BoolNode() {}
  132. int NumExpectedParameters() const override { return 1; }
  133. std::string Evaluate(
  134. const std::vector<std::string>& parameters,
  135. cmGeneratorExpressionContext* /*context*/,
  136. const GeneratorExpressionContent* /*content*/,
  137. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  138. {
  139. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  140. }
  141. } boolNode;
  142. static const struct IfNode : public cmGeneratorExpressionNode
  143. {
  144. IfNode() {}
  145. int NumExpectedParameters() const override { return 3; }
  146. std::string Evaluate(const std::vector<std::string>& parameters,
  147. cmGeneratorExpressionContext* context,
  148. const GeneratorExpressionContent* content,
  149. cmGeneratorExpressionDAGChecker*) const override
  150. {
  151. if (parameters[0] != "1" && parameters[0] != "0") {
  152. reportError(context, content->GetOriginalExpression(),
  153. "First parameter to $<IF> must resolve to exactly one '0' "
  154. "or '1' value.");
  155. return std::string();
  156. }
  157. return parameters[0] == "1" ? parameters[1] : parameters[2];
  158. }
  159. } ifNode;
  160. static const struct StrEqualNode : public cmGeneratorExpressionNode
  161. {
  162. StrEqualNode() {}
  163. int NumExpectedParameters() const override { return 2; }
  164. std::string Evaluate(
  165. const std::vector<std::string>& parameters,
  166. cmGeneratorExpressionContext* /*context*/,
  167. const GeneratorExpressionContent* /*content*/,
  168. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  169. {
  170. return *parameters.begin() == parameters[1] ? "1" : "0";
  171. }
  172. } strEqualNode;
  173. static const struct EqualNode : public cmGeneratorExpressionNode
  174. {
  175. EqualNode() {}
  176. int NumExpectedParameters() const override { return 2; }
  177. std::string Evaluate(
  178. const std::vector<std::string>& parameters,
  179. cmGeneratorExpressionContext* context,
  180. const GeneratorExpressionContent* content,
  181. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  182. {
  183. char* pEnd;
  184. int base = 0;
  185. bool flipSign = false;
  186. const char* lhs = parameters[0].c_str();
  187. if (cmHasLiteralPrefix(lhs, "0b") || cmHasLiteralPrefix(lhs, "0B")) {
  188. base = 2;
  189. lhs += 2;
  190. }
  191. if (cmHasLiteralPrefix(lhs, "-0b") || cmHasLiteralPrefix(lhs, "-0B")) {
  192. base = 2;
  193. lhs += 3;
  194. flipSign = true;
  195. }
  196. if (cmHasLiteralPrefix(lhs, "+0b") || cmHasLiteralPrefix(lhs, "+0B")) {
  197. base = 2;
  198. lhs += 3;
  199. }
  200. long lnum = strtol(lhs, &pEnd, base);
  201. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  202. reportError(context, content->GetOriginalExpression(),
  203. "$<EQUAL> parameter " + parameters[0] +
  204. " is not a valid integer.");
  205. return std::string();
  206. }
  207. if (flipSign) {
  208. lnum = -lnum;
  209. }
  210. base = 0;
  211. flipSign = false;
  212. const char* rhs = parameters[1].c_str();
  213. if (cmHasLiteralPrefix(rhs, "0b") || cmHasLiteralPrefix(rhs, "0B")) {
  214. base = 2;
  215. rhs += 2;
  216. }
  217. if (cmHasLiteralPrefix(rhs, "-0b") || cmHasLiteralPrefix(rhs, "-0B")) {
  218. base = 2;
  219. rhs += 3;
  220. flipSign = true;
  221. }
  222. if (cmHasLiteralPrefix(rhs, "+0b") || cmHasLiteralPrefix(rhs, "+0B")) {
  223. base = 2;
  224. rhs += 3;
  225. }
  226. long rnum = strtol(rhs, &pEnd, base);
  227. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  228. reportError(context, content->GetOriginalExpression(),
  229. "$<EQUAL> parameter " + parameters[1] +
  230. " is not a valid integer.");
  231. return std::string();
  232. }
  233. if (flipSign) {
  234. rnum = -rnum;
  235. }
  236. return lnum == rnum ? "1" : "0";
  237. }
  238. } equalNode;
  239. static const struct InListNode : public cmGeneratorExpressionNode
  240. {
  241. InListNode() {}
  242. int NumExpectedParameters() const override { return 2; }
  243. std::string Evaluate(
  244. const std::vector<std::string>& parameters,
  245. cmGeneratorExpressionContext* /*context*/,
  246. const GeneratorExpressionContent* /*content*/,
  247. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  248. {
  249. std::vector<std::string> values;
  250. cmSystemTools::ExpandListArgument(parameters[1], values);
  251. if (values.empty()) {
  252. return "0";
  253. }
  254. return std::find(values.cbegin(), values.cend(), parameters.front()) ==
  255. values.cend()
  256. ? "0"
  257. : "1";
  258. }
  259. } inListNode;
  260. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  261. {
  262. TargetExistsNode() {}
  263. int NumExpectedParameters() const override { return 1; }
  264. std::string Evaluate(
  265. const std::vector<std::string>& parameters,
  266. cmGeneratorExpressionContext* context,
  267. const GeneratorExpressionContent* content,
  268. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  269. {
  270. if (parameters.size() != 1) {
  271. reportError(context, content->GetOriginalExpression(),
  272. "$<TARGET_EXISTS:...> expression requires one parameter");
  273. return std::string();
  274. }
  275. std::string targetName = parameters.front();
  276. if (targetName.empty() ||
  277. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  278. reportError(context, content->GetOriginalExpression(),
  279. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  280. "valid target name.");
  281. return std::string();
  282. }
  283. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  284. }
  285. } targetExistsNode;
  286. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  287. {
  288. TargetNameIfExistsNode() {}
  289. int NumExpectedParameters() const override { return 1; }
  290. std::string Evaluate(
  291. const std::vector<std::string>& parameters,
  292. cmGeneratorExpressionContext* context,
  293. const GeneratorExpressionContent* content,
  294. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  295. {
  296. if (parameters.size() != 1) {
  297. reportError(context, content->GetOriginalExpression(),
  298. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  299. "parameter");
  300. return std::string();
  301. }
  302. std::string targetName = parameters.front();
  303. if (targetName.empty() ||
  304. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  305. reportError(context, content->GetOriginalExpression(),
  306. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  307. "non-empty valid target name.");
  308. return std::string();
  309. }
  310. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  311. ? targetName
  312. : std::string();
  313. }
  314. } targetNameIfExistsNode;
  315. struct GenexEvaluator : public cmGeneratorExpressionNode
  316. {
  317. GenexEvaluator() {}
  318. protected:
  319. std::string EvaluateExpression(
  320. const std::string& genexOperator, const std::string& expression,
  321. cmGeneratorExpressionContext* context,
  322. const GeneratorExpressionContent* content,
  323. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  324. {
  325. if (context->HeadTarget) {
  326. cmGeneratorExpressionDAGChecker dagChecker(
  327. context->Backtrace, context->HeadTarget->GetName(), genexOperator,
  328. content, dagCheckerParent);
  329. switch (dagChecker.Check()) {
  330. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  331. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  332. dagChecker.ReportError(context, content->GetOriginalExpression());
  333. return std::string();
  334. }
  335. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  336. case cmGeneratorExpressionDAGChecker::DAG:
  337. break;
  338. }
  339. return this->EvaluateDependentExpression(
  340. expression, context->LG, context, context->HeadTarget,
  341. context->CurrentTarget, &dagChecker);
  342. }
  343. return this->EvaluateDependentExpression(
  344. expression, context->LG, context, context->HeadTarget,
  345. context->CurrentTarget, dagCheckerParent);
  346. }
  347. };
  348. static const struct TargetGenexEvalNode : public GenexEvaluator
  349. {
  350. TargetGenexEvalNode() {}
  351. int NumExpectedParameters() const override { return 2; }
  352. bool AcceptsArbitraryContentParameter() const override { return true; }
  353. std::string Evaluate(
  354. const std::vector<std::string>& parameters,
  355. cmGeneratorExpressionContext* context,
  356. const GeneratorExpressionContent* content,
  357. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  358. {
  359. const std::string& targetName = parameters.front();
  360. if (targetName.empty() ||
  361. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  362. reportError(context, content->GetOriginalExpression(),
  363. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  364. "non-empty valid target name.");
  365. return std::string();
  366. }
  367. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  368. if (!target) {
  369. std::ostringstream e;
  370. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  371. << "\" not found.";
  372. reportError(context, content->GetOriginalExpression(), e.str());
  373. return std::string();
  374. }
  375. const std::string& expression = parameters[1];
  376. if (expression.empty()) {
  377. return expression;
  378. }
  379. cmGeneratorExpressionContext targetContext(
  380. context->LG, context->Config, context->Quiet, target, target,
  381. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  382. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  383. &targetContext, content, dagCheckerParent);
  384. }
  385. } targetGenexEvalNode;
  386. static const struct GenexEvalNode : public GenexEvaluator
  387. {
  388. GenexEvalNode() {}
  389. int NumExpectedParameters() const override { return 1; }
  390. bool AcceptsArbitraryContentParameter() const override { return true; }
  391. std::string Evaluate(
  392. const std::vector<std::string>& parameters,
  393. cmGeneratorExpressionContext* context,
  394. const GeneratorExpressionContent* content,
  395. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  396. {
  397. const std::string& expression = parameters[0];
  398. if (expression.empty()) {
  399. return expression;
  400. }
  401. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  402. dagCheckerParent);
  403. }
  404. } genexEvalNode;
  405. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  406. {
  407. LowerCaseNode() {}
  408. bool AcceptsArbitraryContentParameter() const override { return true; }
  409. std::string Evaluate(
  410. const std::vector<std::string>& parameters,
  411. cmGeneratorExpressionContext* /*context*/,
  412. const GeneratorExpressionContent* /*content*/,
  413. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  414. {
  415. return cmSystemTools::LowerCase(parameters.front());
  416. }
  417. } lowerCaseNode;
  418. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  419. {
  420. UpperCaseNode() {}
  421. bool AcceptsArbitraryContentParameter() const override { return true; }
  422. std::string Evaluate(
  423. const std::vector<std::string>& parameters,
  424. cmGeneratorExpressionContext* /*context*/,
  425. const GeneratorExpressionContent* /*content*/,
  426. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  427. {
  428. return cmSystemTools::UpperCase(parameters.front());
  429. }
  430. } upperCaseNode;
  431. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  432. {
  433. MakeCIdentifierNode() {}
  434. bool AcceptsArbitraryContentParameter() const override { return true; }
  435. std::string Evaluate(
  436. const std::vector<std::string>& parameters,
  437. cmGeneratorExpressionContext* /*context*/,
  438. const GeneratorExpressionContent* /*content*/,
  439. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  440. {
  441. return cmSystemTools::MakeCidentifier(parameters.front());
  442. }
  443. } makeCIdentifierNode;
  444. static const struct Angle_RNode : public cmGeneratorExpressionNode
  445. {
  446. Angle_RNode() {}
  447. int NumExpectedParameters() const override { return 0; }
  448. std::string Evaluate(
  449. const std::vector<std::string>& /*parameters*/,
  450. cmGeneratorExpressionContext* /*context*/,
  451. const GeneratorExpressionContent* /*content*/,
  452. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  453. {
  454. return ">";
  455. }
  456. } angle_rNode;
  457. static const struct CommaNode : public cmGeneratorExpressionNode
  458. {
  459. CommaNode() {}
  460. int NumExpectedParameters() const override { return 0; }
  461. std::string Evaluate(
  462. const std::vector<std::string>& /*parameters*/,
  463. cmGeneratorExpressionContext* /*context*/,
  464. const GeneratorExpressionContent* /*content*/,
  465. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  466. {
  467. return ",";
  468. }
  469. } commaNode;
  470. static const struct SemicolonNode : public cmGeneratorExpressionNode
  471. {
  472. SemicolonNode() {}
  473. int NumExpectedParameters() const override { return 0; }
  474. std::string Evaluate(
  475. const std::vector<std::string>& /*parameters*/,
  476. cmGeneratorExpressionContext* /*context*/,
  477. const GeneratorExpressionContent* /*content*/,
  478. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  479. {
  480. return ";";
  481. }
  482. } semicolonNode;
  483. struct CompilerIdNode : public cmGeneratorExpressionNode
  484. {
  485. CompilerIdNode() {}
  486. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  487. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  488. cmGeneratorExpressionContext* context,
  489. const GeneratorExpressionContent* content,
  490. cmGeneratorExpressionDAGChecker* /*unused*/,
  491. const std::string& lang) const
  492. {
  493. const char* compilerId = context->LG->GetMakefile()->GetSafeDefinition(
  494. "CMAKE_" + lang + "_COMPILER_ID");
  495. if (parameters.empty()) {
  496. return compilerId ? compilerId : "";
  497. }
  498. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  499. if (!compilerIdValidator.find(*parameters.begin())) {
  500. reportError(context, content->GetOriginalExpression(),
  501. "Expression syntax not recognized.");
  502. return std::string();
  503. }
  504. if (!compilerId) {
  505. return parameters.front().empty() ? "1" : "0";
  506. }
  507. if (strcmp(parameters.begin()->c_str(), compilerId) == 0) {
  508. return "1";
  509. }
  510. if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0) {
  511. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  512. case cmPolicies::WARN: {
  513. std::ostringstream e;
  514. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
  515. context->LG->GetCMakeInstance()->IssueMessage(
  516. cmake::AUTHOR_WARNING, e.str(), context->Backtrace);
  517. CM_FALLTHROUGH;
  518. }
  519. case cmPolicies::OLD:
  520. return "1";
  521. case cmPolicies::NEW:
  522. case cmPolicies::REQUIRED_ALWAYS:
  523. case cmPolicies::REQUIRED_IF_USED:
  524. break;
  525. }
  526. }
  527. return "0";
  528. }
  529. };
  530. static const struct CCompilerIdNode : public CompilerIdNode
  531. {
  532. CCompilerIdNode() {}
  533. std::string Evaluate(
  534. const std::vector<std::string>& parameters,
  535. cmGeneratorExpressionContext* context,
  536. const GeneratorExpressionContent* content,
  537. cmGeneratorExpressionDAGChecker* dagChecker) const override
  538. {
  539. if (!context->HeadTarget) {
  540. reportError(
  541. context, content->GetOriginalExpression(),
  542. "$<C_COMPILER_ID> may only be used with binary targets. It may "
  543. "not be used with add_custom_command or add_custom_target.");
  544. return std::string();
  545. }
  546. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  547. "C");
  548. }
  549. } cCompilerIdNode;
  550. static const struct CXXCompilerIdNode : public CompilerIdNode
  551. {
  552. CXXCompilerIdNode() {}
  553. std::string Evaluate(
  554. const std::vector<std::string>& parameters,
  555. cmGeneratorExpressionContext* context,
  556. const GeneratorExpressionContent* content,
  557. cmGeneratorExpressionDAGChecker* dagChecker) const override
  558. {
  559. if (!context->HeadTarget) {
  560. reportError(
  561. context, content->GetOriginalExpression(),
  562. "$<CXX_COMPILER_ID> may only be used with binary targets. It may "
  563. "not be used with add_custom_command or add_custom_target.");
  564. return std::string();
  565. }
  566. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  567. "CXX");
  568. }
  569. } cxxCompilerIdNode;
  570. struct CompilerVersionNode : public cmGeneratorExpressionNode
  571. {
  572. CompilerVersionNode() {}
  573. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  574. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  575. cmGeneratorExpressionContext* context,
  576. const GeneratorExpressionContent* content,
  577. cmGeneratorExpressionDAGChecker* /*unused*/,
  578. const std::string& lang) const
  579. {
  580. const char* compilerVersion =
  581. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  582. "_COMPILER_VERSION");
  583. if (parameters.empty()) {
  584. return compilerVersion ? compilerVersion : "";
  585. }
  586. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  587. if (!compilerIdValidator.find(*parameters.begin())) {
  588. reportError(context, content->GetOriginalExpression(),
  589. "Expression syntax not recognized.");
  590. return std::string();
  591. }
  592. if (!compilerVersion) {
  593. return parameters.front().empty() ? "1" : "0";
  594. }
  595. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  596. parameters.begin()->c_str(),
  597. compilerVersion)
  598. ? "1"
  599. : "0";
  600. }
  601. };
  602. static const struct CCompilerVersionNode : public CompilerVersionNode
  603. {
  604. CCompilerVersionNode() {}
  605. std::string Evaluate(
  606. const std::vector<std::string>& parameters,
  607. cmGeneratorExpressionContext* context,
  608. const GeneratorExpressionContent* content,
  609. cmGeneratorExpressionDAGChecker* dagChecker) const override
  610. {
  611. if (!context->HeadTarget) {
  612. reportError(
  613. context, content->GetOriginalExpression(),
  614. "$<C_COMPILER_VERSION> may only be used with binary targets. It "
  615. "may not be used with add_custom_command or add_custom_target.");
  616. return std::string();
  617. }
  618. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  619. "C");
  620. }
  621. } cCompilerVersionNode;
  622. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  623. {
  624. CxxCompilerVersionNode() {}
  625. std::string Evaluate(
  626. const std::vector<std::string>& parameters,
  627. cmGeneratorExpressionContext* context,
  628. const GeneratorExpressionContent* content,
  629. cmGeneratorExpressionDAGChecker* dagChecker) const override
  630. {
  631. if (!context->HeadTarget) {
  632. reportError(
  633. context, content->GetOriginalExpression(),
  634. "$<CXX_COMPILER_VERSION> may only be used with binary targets. It "
  635. "may not be used with add_custom_command or add_custom_target.");
  636. return std::string();
  637. }
  638. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  639. "CXX");
  640. }
  641. } cxxCompilerVersionNode;
  642. struct PlatformIdNode : public cmGeneratorExpressionNode
  643. {
  644. PlatformIdNode() {}
  645. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  646. std::string Evaluate(
  647. const std::vector<std::string>& parameters,
  648. cmGeneratorExpressionContext* context,
  649. const GeneratorExpressionContent* /*content*/,
  650. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  651. {
  652. const char* platformId =
  653. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  654. if (parameters.empty()) {
  655. return platformId ? platformId : "";
  656. }
  657. if (!platformId) {
  658. return parameters.front().empty() ? "1" : "0";
  659. }
  660. if (strcmp(parameters.begin()->c_str(), platformId) == 0) {
  661. return "1";
  662. }
  663. return "0";
  664. }
  665. } platformIdNode;
  666. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  667. {
  668. VersionGreaterNode() {}
  669. int NumExpectedParameters() const override { return 2; }
  670. std::string Evaluate(
  671. const std::vector<std::string>& parameters,
  672. cmGeneratorExpressionContext* /*context*/,
  673. const GeneratorExpressionContent* /*content*/,
  674. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  675. {
  676. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  677. parameters.front().c_str(),
  678. parameters[1].c_str())
  679. ? "1"
  680. : "0";
  681. }
  682. } versionGreaterNode;
  683. static const struct VersionGreaterEqNode : public cmGeneratorExpressionNode
  684. {
  685. VersionGreaterEqNode() {}
  686. int NumExpectedParameters() const override { return 2; }
  687. std::string Evaluate(
  688. const std::vector<std::string>& parameters,
  689. cmGeneratorExpressionContext* /*context*/,
  690. const GeneratorExpressionContent* /*content*/,
  691. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  692. {
  693. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  694. parameters.front().c_str(),
  695. parameters[1].c_str())
  696. ? "1"
  697. : "0";
  698. }
  699. } versionGreaterEqNode;
  700. static const struct VersionLessNode : public cmGeneratorExpressionNode
  701. {
  702. VersionLessNode() {}
  703. int NumExpectedParameters() const override { return 2; }
  704. std::string Evaluate(
  705. const std::vector<std::string>& parameters,
  706. cmGeneratorExpressionContext* /*context*/,
  707. const GeneratorExpressionContent* /*content*/,
  708. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  709. {
  710. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  711. parameters.front().c_str(),
  712. parameters[1].c_str())
  713. ? "1"
  714. : "0";
  715. }
  716. } versionLessNode;
  717. static const struct VersionLessEqNode : public cmGeneratorExpressionNode
  718. {
  719. VersionLessEqNode() {}
  720. int NumExpectedParameters() const override { return 2; }
  721. std::string Evaluate(
  722. const std::vector<std::string>& parameters,
  723. cmGeneratorExpressionContext* /*context*/,
  724. const GeneratorExpressionContent* /*content*/,
  725. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  726. {
  727. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS_EQUAL,
  728. parameters.front().c_str(),
  729. parameters[1].c_str())
  730. ? "1"
  731. : "0";
  732. }
  733. } versionLessEqNode;
  734. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  735. {
  736. VersionEqualNode() {}
  737. int NumExpectedParameters() const override { return 2; }
  738. std::string Evaluate(
  739. const std::vector<std::string>& parameters,
  740. cmGeneratorExpressionContext* /*context*/,
  741. const GeneratorExpressionContent* /*content*/,
  742. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  743. {
  744. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  745. parameters.front().c_str(),
  746. parameters[1].c_str())
  747. ? "1"
  748. : "0";
  749. }
  750. } versionEqualNode;
  751. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  752. {
  753. LinkOnlyNode() {}
  754. std::string Evaluate(
  755. const std::vector<std::string>& parameters,
  756. cmGeneratorExpressionContext* context,
  757. const GeneratorExpressionContent* content,
  758. cmGeneratorExpressionDAGChecker* dagChecker) const override
  759. {
  760. if (!dagChecker) {
  761. reportError(context, content->GetOriginalExpression(),
  762. "$<LINK_ONLY:...> may only be used for linking");
  763. return std::string();
  764. }
  765. if (!dagChecker->GetTransitivePropertiesOnly()) {
  766. return parameters.front();
  767. }
  768. return std::string();
  769. }
  770. } linkOnlyNode;
  771. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  772. {
  773. ConfigurationNode() {}
  774. int NumExpectedParameters() const override { return 0; }
  775. std::string Evaluate(
  776. const std::vector<std::string>& /*parameters*/,
  777. cmGeneratorExpressionContext* context,
  778. const GeneratorExpressionContent* /*content*/,
  779. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  780. {
  781. context->HadContextSensitiveCondition = true;
  782. return context->Config;
  783. }
  784. } configurationNode;
  785. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  786. {
  787. ConfigurationTestNode() {}
  788. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  789. std::string Evaluate(
  790. const std::vector<std::string>& parameters,
  791. cmGeneratorExpressionContext* context,
  792. const GeneratorExpressionContent* content,
  793. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  794. {
  795. if (parameters.empty()) {
  796. return configurationNode.Evaluate(parameters, context, content, nullptr);
  797. }
  798. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  799. if (!configValidator.find(*parameters.begin())) {
  800. reportError(context, content->GetOriginalExpression(),
  801. "Expression syntax not recognized.");
  802. return std::string();
  803. }
  804. context->HadContextSensitiveCondition = true;
  805. if (context->Config.empty()) {
  806. return parameters.front().empty() ? "1" : "0";
  807. }
  808. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  809. context->Config.c_str()) == 0) {
  810. return "1";
  811. }
  812. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  813. const char* loc = nullptr;
  814. const char* imp = nullptr;
  815. std::string suffix;
  816. if (context->CurrentTarget->Target->GetMappedConfig(
  817. context->Config, &loc, &imp, suffix)) {
  818. // This imported target has an appropriate location
  819. // for this (possibly mapped) config.
  820. // Check if there is a proper config mapping for the tested config.
  821. std::vector<std::string> mappedConfigs;
  822. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  823. mapProp += cmSystemTools::UpperCase(context->Config);
  824. if (const char* mapValue =
  825. context->CurrentTarget->GetProperty(mapProp)) {
  826. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  827. mappedConfigs);
  828. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  829. cmSystemTools::UpperCase(parameters.front())) !=
  830. mappedConfigs.end()
  831. ? "1"
  832. : "0";
  833. }
  834. }
  835. }
  836. return "0";
  837. }
  838. } configurationTestNode;
  839. static const struct JoinNode : public cmGeneratorExpressionNode
  840. {
  841. JoinNode() {}
  842. int NumExpectedParameters() const override { return 2; }
  843. bool AcceptsArbitraryContentParameter() const override { return true; }
  844. std::string Evaluate(
  845. const std::vector<std::string>& parameters,
  846. cmGeneratorExpressionContext* /*context*/,
  847. const GeneratorExpressionContent* /*content*/,
  848. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  849. {
  850. std::vector<std::string> list;
  851. cmSystemTools::ExpandListArgument(parameters.front(), list);
  852. return cmJoin(list, parameters[1]);
  853. }
  854. } joinNode;
  855. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  856. {
  857. CompileLanguageNode() {}
  858. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  859. std::string Evaluate(
  860. const std::vector<std::string>& parameters,
  861. cmGeneratorExpressionContext* context,
  862. const GeneratorExpressionContent* content,
  863. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  864. {
  865. if (context->Language.empty()) {
  866. reportError(
  867. context, content->GetOriginalExpression(),
  868. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  869. "directories, compile definitions, compile options, and to evaluate "
  870. "components of the file(GENERATE) command.");
  871. return std::string();
  872. }
  873. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  874. std::string genName = gg->GetName();
  875. if (genName.find("Makefiles") == std::string::npos &&
  876. genName.find("Ninja") == std::string::npos &&
  877. genName.find("Visual Studio") == std::string::npos &&
  878. genName.find("Xcode") == std::string::npos &&
  879. genName.find("Watcom WMake") == std::string::npos) {
  880. reportError(context, content->GetOriginalExpression(),
  881. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  882. return std::string();
  883. }
  884. if (parameters.empty()) {
  885. return context->Language;
  886. }
  887. return context->Language == parameters.front() ? "1" : "0";
  888. }
  889. } languageNode;
  890. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) , "INTERFACE_" #PROPERTY
  891. static const char* targetPropertyTransitiveWhitelist[] = {
  892. nullptr CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  893. };
  894. #undef TRANSITIVE_PROPERTY_NAME
  895. template <typename T>
  896. std::string getLinkedTargetsContent(
  897. std::vector<T> const& libraries, cmGeneratorTarget const* target,
  898. cmGeneratorTarget const* headTarget, cmGeneratorExpressionContext* context,
  899. cmGeneratorExpressionDAGChecker* dagChecker,
  900. const std::string& interfacePropertyName)
  901. {
  902. std::string linkedTargetsContent;
  903. std::string sep;
  904. std::string depString;
  905. for (T const& l : libraries) {
  906. // Broken code can have a target in its own link interface.
  907. // Don't follow such link interface entries so as not to create a
  908. // self-referencing loop.
  909. if (l.Target && l.Target != target) {
  910. depString += sep + "$<TARGET_PROPERTY:" + l.Target->GetName() + "," +
  911. interfacePropertyName + ">";
  912. sep = ";";
  913. }
  914. }
  915. if (!depString.empty()) {
  916. linkedTargetsContent =
  917. cmGeneratorExpressionNode::EvaluateDependentExpression(
  918. depString, target->GetLocalGenerator(), context, headTarget, target,
  919. dagChecker);
  920. }
  921. linkedTargetsContent =
  922. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  923. return linkedTargetsContent;
  924. }
  925. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  926. {
  927. TargetPropertyNode() {}
  928. // This node handles errors on parameter count itself.
  929. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  930. std::string Evaluate(
  931. const std::vector<std::string>& parameters,
  932. cmGeneratorExpressionContext* context,
  933. const GeneratorExpressionContent* content,
  934. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  935. {
  936. if (parameters.size() != 1 && parameters.size() != 2) {
  937. reportError(
  938. context, content->GetOriginalExpression(),
  939. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  940. return std::string();
  941. }
  942. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  943. cmGeneratorTarget const* target = context->HeadTarget;
  944. std::string propertyName = *parameters.begin();
  945. if (parameters.size() == 1) {
  946. context->HadHeadSensitiveCondition = true;
  947. }
  948. if (!target && parameters.size() == 1) {
  949. reportError(
  950. context, content->GetOriginalExpression(),
  951. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  952. "It may not be used with add_custom_command or add_custom_target. "
  953. "Specify the target to read a property from using the "
  954. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  955. return std::string();
  956. }
  957. if (parameters.size() == 2) {
  958. if (parameters.begin()->empty() && parameters[1].empty()) {
  959. reportError(
  960. context, content->GetOriginalExpression(),
  961. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  962. "target name and property name.");
  963. return std::string();
  964. }
  965. if (parameters.begin()->empty()) {
  966. reportError(
  967. context, content->GetOriginalExpression(),
  968. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  969. "target name.");
  970. return std::string();
  971. }
  972. std::string targetName = parameters.front();
  973. propertyName = parameters[1];
  974. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  975. if (!propertyNameValidator.find(propertyName.c_str())) {
  976. ::reportError(context, content->GetOriginalExpression(),
  977. "Target name and property name not supported.");
  978. return std::string();
  979. }
  980. ::reportError(context, content->GetOriginalExpression(),
  981. "Target name not supported.");
  982. return std::string();
  983. }
  984. static const std::string propALIASED_TARGET = "ALIASED_TARGET";
  985. if (propertyName == propALIASED_TARGET) {
  986. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  987. if (cmGeneratorTarget* tgt =
  988. context->LG->FindGeneratorTargetToUse(targetName)) {
  989. return tgt->GetName();
  990. }
  991. }
  992. return "";
  993. }
  994. target = context->LG->FindGeneratorTargetToUse(targetName);
  995. if (!target) {
  996. std::ostringstream e;
  997. e << "Target \"" << targetName << "\" not found.";
  998. reportError(context, content->GetOriginalExpression(), e.str());
  999. return std::string();
  1000. }
  1001. context->AllTargets.insert(target);
  1002. }
  1003. if (target == context->HeadTarget) {
  1004. // Keep track of the properties seen while processing.
  1005. // The evaluation of the LINK_LIBRARIES generator expressions
  1006. // will check this to ensure that properties have one consistent
  1007. // value for all evaluations.
  1008. context->SeenTargetProperties.insert(propertyName);
  1009. }
  1010. if (propertyName == "SOURCES") {
  1011. context->SourceSensitiveTargets.insert(target);
  1012. }
  1013. if (propertyName.empty()) {
  1014. reportError(
  1015. context, content->GetOriginalExpression(),
  1016. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1017. "name.");
  1018. return std::string();
  1019. }
  1020. if (!propertyNameValidator.find(propertyName)) {
  1021. ::reportError(context, content->GetOriginalExpression(),
  1022. "Property name not supported.");
  1023. return std::string();
  1024. }
  1025. assert(target);
  1026. if (propertyName == "LINKER_LANGUAGE") {
  1027. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1028. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1029. dagCheckerParent->EvaluatingSources())) {
  1030. reportError(
  1031. context, content->GetOriginalExpression(),
  1032. "LINKER_LANGUAGE target property can not be used while evaluating "
  1033. "link libraries for a static library");
  1034. return std::string();
  1035. }
  1036. return target->GetLinkerLanguage(context->Config);
  1037. }
  1038. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  1039. target->GetName(), propertyName,
  1040. content, dagCheckerParent);
  1041. switch (dagChecker.Check()) {
  1042. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1043. dagChecker.ReportError(context, content->GetOriginalExpression());
  1044. return std::string();
  1045. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1046. // No error. We just skip cyclic references.
  1047. return std::string();
  1048. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1049. for (size_t i = 1; i < cm::size(targetPropertyTransitiveWhitelist);
  1050. ++i) {
  1051. if (targetPropertyTransitiveWhitelist[i] == propertyName) {
  1052. // No error. We're not going to find anything new here.
  1053. return std::string();
  1054. }
  1055. }
  1056. case cmGeneratorExpressionDAGChecker::DAG:
  1057. break;
  1058. }
  1059. const char* prop = target->GetProperty(propertyName);
  1060. if (dagCheckerParent) {
  1061. if (dagCheckerParent->EvaluatingGenexExpression()) {
  1062. // No check required.
  1063. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1064. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  1065. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  1066. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(
  1067. TRANSITIVE_PROPERTY_COMPARE) false) { // NOLINT(clang-tidy)
  1068. reportError(
  1069. context, content->GetOriginalExpression(),
  1070. "$<TARGET_PROPERTY:...> expression in link libraries "
  1071. "evaluation depends on target property which is transitive "
  1072. "over the link libraries, creating a recursion.");
  1073. return std::string();
  1074. }
  1075. #undef TRANSITIVE_PROPERTY_COMPARE
  1076. if (!prop) {
  1077. return std::string();
  1078. }
  1079. } else {
  1080. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1081. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1082. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1083. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1084. }
  1085. }
  1086. std::string linkedTargetsContent;
  1087. std::string interfacePropertyName;
  1088. bool isInterfaceProperty = false;
  1089. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1090. if (propertyName == #prop) { \
  1091. interfacePropertyName = "INTERFACE_" #prop; \
  1092. } else if (propertyName == "INTERFACE_" #prop) { \
  1093. interfacePropertyName = "INTERFACE_" #prop; \
  1094. isInterfaceProperty = true; \
  1095. } else
  1096. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1097. // Note that the above macro terminates with an else
  1098. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1099. cmPolicies::PolicyStatus polSt =
  1100. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1101. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1102. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1103. }
  1104. }
  1105. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1106. cmGeneratorTarget const* headTarget =
  1107. context->HeadTarget && isInterfaceProperty ? context->HeadTarget
  1108. : target;
  1109. if (isInterfaceProperty) {
  1110. if (cmLinkInterfaceLibraries const* iface =
  1111. target->GetLinkInterfaceLibraries(context->Config, headTarget,
  1112. true)) {
  1113. linkedTargetsContent =
  1114. getLinkedTargetsContent(iface->Libraries, target, headTarget,
  1115. context, &dagChecker, interfacePropertyName);
  1116. }
  1117. } else if (!interfacePropertyName.empty()) {
  1118. if (cmLinkImplementationLibraries const* impl =
  1119. target->GetLinkImplementationLibraries(context->Config)) {
  1120. linkedTargetsContent =
  1121. getLinkedTargetsContent(impl->Libraries, target, target, context,
  1122. &dagChecker, interfacePropertyName);
  1123. }
  1124. }
  1125. if (!prop) {
  1126. if (target->IsImported() ||
  1127. target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1128. return linkedTargetsContent;
  1129. }
  1130. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1131. context->Config)) {
  1132. context->HadContextSensitiveCondition = true;
  1133. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1134. context->Config)
  1135. ? "1"
  1136. : "0";
  1137. }
  1138. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1139. context->Config)) {
  1140. context->HadContextSensitiveCondition = true;
  1141. const char* propContent =
  1142. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1143. context->Config);
  1144. return propContent ? propContent : "";
  1145. }
  1146. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1147. context->Config)) {
  1148. context->HadContextSensitiveCondition = true;
  1149. const char* propContent =
  1150. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1151. context->Config);
  1152. return propContent ? propContent : "";
  1153. }
  1154. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1155. context->Config)) {
  1156. context->HadContextSensitiveCondition = true;
  1157. const char* propContent =
  1158. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1159. context->Config);
  1160. return propContent ? propContent : "";
  1161. }
  1162. return linkedTargetsContent;
  1163. }
  1164. if (!target->IsImported() && dagCheckerParent &&
  1165. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1166. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1167. context->Config)) {
  1168. context->HadContextSensitiveCondition = true;
  1169. const char* propContent =
  1170. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1171. context->Config);
  1172. return propContent ? propContent : "";
  1173. }
  1174. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1175. context->Config)) {
  1176. context->HadContextSensitiveCondition = true;
  1177. const char* propContent =
  1178. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1179. context->Config);
  1180. return propContent ? propContent : "";
  1181. }
  1182. }
  1183. if (!interfacePropertyName.empty()) {
  1184. std::string result = this->EvaluateDependentExpression(
  1185. prop, context->LG, context, headTarget, target, &dagChecker);
  1186. if (!linkedTargetsContent.empty()) {
  1187. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1188. }
  1189. return result;
  1190. }
  1191. return prop;
  1192. }
  1193. } targetPropertyNode;
  1194. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1195. {
  1196. TargetNameNode() {}
  1197. bool GeneratesContent() const override { return true; }
  1198. bool AcceptsArbitraryContentParameter() const override { return true; }
  1199. bool RequiresLiteralInput() const override { return true; }
  1200. std::string Evaluate(
  1201. const std::vector<std::string>& parameters,
  1202. cmGeneratorExpressionContext* /*context*/,
  1203. const GeneratorExpressionContent* /*content*/,
  1204. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1205. {
  1206. return parameters.front();
  1207. }
  1208. int NumExpectedParameters() const override { return 1; }
  1209. } targetNameNode;
  1210. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1211. {
  1212. TargetObjectsNode() {}
  1213. std::string Evaluate(
  1214. const std::vector<std::string>& parameters,
  1215. cmGeneratorExpressionContext* context,
  1216. const GeneratorExpressionContent* content,
  1217. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1218. {
  1219. std::string tgtName = parameters.front();
  1220. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1221. if (!gt) {
  1222. std::ostringstream e;
  1223. e << "Objects of target \"" << tgtName
  1224. << "\" referenced but no such target exists.";
  1225. reportError(context, content->GetOriginalExpression(), e.str());
  1226. return std::string();
  1227. }
  1228. if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1229. std::ostringstream e;
  1230. e << "Objects of target \"" << tgtName
  1231. << "\" referenced but is not an OBJECT library.";
  1232. reportError(context, content->GetOriginalExpression(), e.str());
  1233. return std::string();
  1234. }
  1235. if (!context->EvaluateForBuildsystem) {
  1236. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1237. std::string reason;
  1238. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1239. std::ostringstream e;
  1240. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1241. "is only suitable for consumption by CMake (limited"
  1242. << reason << "). "
  1243. "It is not suitable for writing out elsewhere.";
  1244. reportError(context, content->GetOriginalExpression(), e.str());
  1245. return std::string();
  1246. }
  1247. }
  1248. std::vector<std::string> objects;
  1249. if (gt->IsImported()) {
  1250. const char* loc = nullptr;
  1251. const char* imp = nullptr;
  1252. std::string suffix;
  1253. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1254. cmSystemTools::ExpandListArgument(loc, objects);
  1255. }
  1256. context->HadContextSensitiveCondition = true;
  1257. } else {
  1258. gt->GetTargetObjectNames(context->Config, objects);
  1259. std::string obj_dir;
  1260. if (context->EvaluateForBuildsystem) {
  1261. // Use object file directory with buildsystem placeholder.
  1262. obj_dir = gt->ObjectDirectory;
  1263. // Here we assume that the set of object files produced
  1264. // by an object library does not vary with configuration
  1265. // and do not set HadContextSensitiveCondition to true.
  1266. } else {
  1267. // Use object file directory with per-config location.
  1268. obj_dir = gt->GetObjectDirectory(context->Config);
  1269. context->HadContextSensitiveCondition = true;
  1270. }
  1271. for (std::string& o : objects) {
  1272. o = obj_dir + o;
  1273. }
  1274. }
  1275. // Create the cmSourceFile instances in the referencing directory.
  1276. cmMakefile* mf = context->LG->GetMakefile();
  1277. for (std::string& o : objects) {
  1278. mf->AddTargetObject(tgtName, o);
  1279. }
  1280. return cmJoin(objects, ";");
  1281. }
  1282. } targetObjectsNode;
  1283. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1284. {
  1285. CompileFeaturesNode() {}
  1286. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1287. std::string Evaluate(
  1288. const std::vector<std::string>& parameters,
  1289. cmGeneratorExpressionContext* context,
  1290. const GeneratorExpressionContent* content,
  1291. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1292. {
  1293. cmGeneratorTarget const* target = context->HeadTarget;
  1294. if (!target) {
  1295. reportError(
  1296. context, content->GetOriginalExpression(),
  1297. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1298. "not be used with add_custom_command or add_custom_target.");
  1299. return std::string();
  1300. }
  1301. context->HadHeadSensitiveCondition = true;
  1302. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1303. static LangMap availableFeatures;
  1304. LangMap testedFeatures;
  1305. for (std::string const& p : parameters) {
  1306. std::string error;
  1307. std::string lang;
  1308. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1309. context->HeadTarget->Target, p, lang, &error)) {
  1310. reportError(context, content->GetOriginalExpression(), error);
  1311. return std::string();
  1312. }
  1313. testedFeatures[lang].push_back(p);
  1314. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1315. const char* featuresKnown =
  1316. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1317. if (!featuresKnown) {
  1318. reportError(context, content->GetOriginalExpression(), error);
  1319. return std::string();
  1320. }
  1321. cmSystemTools::ExpandListArgument(featuresKnown,
  1322. availableFeatures[lang]);
  1323. }
  1324. }
  1325. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1326. for (auto const& lit : testedFeatures) {
  1327. std::vector<std::string> const& langAvailable =
  1328. availableFeatures[lit.first];
  1329. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1330. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1331. for (std::string const& it : lit.second) {
  1332. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1333. langAvailable.end()) {
  1334. return "0";
  1335. }
  1336. if (standardDefault && !*standardDefault) {
  1337. // This compiler has no notion of language standard levels.
  1338. // All features known for the language are always available.
  1339. continue;
  1340. }
  1341. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1342. target->Target, lit.first, it)) {
  1343. if (evalLL) {
  1344. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1345. if (!l) {
  1346. l = standardDefault;
  1347. }
  1348. assert(l);
  1349. context->MaxLanguageStandard[target][lit.first] = l;
  1350. } else {
  1351. return "0";
  1352. }
  1353. }
  1354. }
  1355. }
  1356. return "1";
  1357. }
  1358. } compileFeaturesNode;
  1359. static const char* targetPolicyWhitelist[] = {
  1360. nullptr
  1361. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1362. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1363. #undef TARGET_POLICY_STRING
  1364. };
  1365. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1366. const char* policy)
  1367. {
  1368. #define RETURN_POLICY(POLICY) \
  1369. if (strcmp(policy, #POLICY) == 0) { \
  1370. return tgt->GetPolicyStatus##POLICY(); \
  1371. }
  1372. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1373. #undef RETURN_POLICY
  1374. assert(false && "Unreachable code. Not a valid policy");
  1375. return cmPolicies::WARN;
  1376. }
  1377. cmPolicies::PolicyID policyForString(const char* policy_id)
  1378. {
  1379. #define RETURN_POLICY_ID(POLICY_ID) \
  1380. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1381. return cmPolicies::POLICY_ID; \
  1382. }
  1383. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1384. #undef RETURN_POLICY_ID
  1385. assert(false && "Unreachable code. Not a valid policy");
  1386. return cmPolicies::CMP0002;
  1387. }
  1388. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1389. {
  1390. TargetPolicyNode() {}
  1391. int NumExpectedParameters() const override { return 1; }
  1392. std::string Evaluate(
  1393. const std::vector<std::string>& parameters,
  1394. cmGeneratorExpressionContext* context,
  1395. const GeneratorExpressionContent* content,
  1396. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1397. {
  1398. if (!context->HeadTarget) {
  1399. reportError(
  1400. context, content->GetOriginalExpression(),
  1401. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1402. "may not be used with add_custom_command or add_custom_target.");
  1403. return std::string();
  1404. }
  1405. context->HadContextSensitiveCondition = true;
  1406. context->HadHeadSensitiveCondition = true;
  1407. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1408. const char* policy = targetPolicyWhitelist[i];
  1409. if (parameters.front() == policy) {
  1410. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1411. switch (statusForTarget(context->HeadTarget, policy)) {
  1412. case cmPolicies::WARN:
  1413. lg->IssueMessage(
  1414. cmake::AUTHOR_WARNING,
  1415. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1416. CM_FALLTHROUGH;
  1417. case cmPolicies::REQUIRED_IF_USED:
  1418. case cmPolicies::REQUIRED_ALWAYS:
  1419. case cmPolicies::OLD:
  1420. return "0";
  1421. case cmPolicies::NEW:
  1422. return "1";
  1423. }
  1424. }
  1425. }
  1426. reportError(
  1427. context, content->GetOriginalExpression(),
  1428. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1429. "policies. Currently it may be used with the following policies:\n"
  1430. #define STRINGIFY_HELPER(X) #X
  1431. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1432. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1433. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1434. #undef TARGET_POLICY_LIST_ITEM
  1435. );
  1436. return std::string();
  1437. }
  1438. } targetPolicyNode;
  1439. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1440. {
  1441. InstallPrefixNode() {}
  1442. bool GeneratesContent() const override { return true; }
  1443. int NumExpectedParameters() const override { return 0; }
  1444. std::string Evaluate(
  1445. const std::vector<std::string>& /*parameters*/,
  1446. cmGeneratorExpressionContext* context,
  1447. const GeneratorExpressionContent* content,
  1448. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1449. {
  1450. reportError(context, content->GetOriginalExpression(),
  1451. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1452. "should never be evaluated.");
  1453. return std::string();
  1454. }
  1455. } installPrefixNode;
  1456. class ArtifactDirTag;
  1457. class ArtifactLinkerTag;
  1458. class ArtifactNameTag;
  1459. class ArtifactPathTag;
  1460. class ArtifactPdbTag;
  1461. class ArtifactSonameTag;
  1462. class ArtifactBundleDirTag;
  1463. class ArtifactBundleContentDirTag;
  1464. template <typename ArtifactT>
  1465. struct TargetFilesystemArtifactResultCreator
  1466. {
  1467. static std::string Create(cmGeneratorTarget* target,
  1468. cmGeneratorExpressionContext* context,
  1469. const GeneratorExpressionContent* content);
  1470. };
  1471. template <>
  1472. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1473. {
  1474. static std::string Create(cmGeneratorTarget* target,
  1475. cmGeneratorExpressionContext* context,
  1476. const GeneratorExpressionContent* content)
  1477. {
  1478. // The target soname file (.so.1).
  1479. if (target->IsDLLPlatform()) {
  1480. ::reportError(context, content->GetOriginalExpression(),
  1481. "TARGET_SONAME_FILE is not allowed "
  1482. "for DLL target platforms.");
  1483. return std::string();
  1484. }
  1485. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1486. ::reportError(context, content->GetOriginalExpression(),
  1487. "TARGET_SONAME_FILE is allowed only for "
  1488. "SHARED libraries.");
  1489. return std::string();
  1490. }
  1491. std::string result = target->GetDirectory(context->Config);
  1492. result += "/";
  1493. result += target->GetSOName(context->Config);
  1494. return result;
  1495. }
  1496. };
  1497. template <>
  1498. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1499. {
  1500. static std::string Create(cmGeneratorTarget* target,
  1501. cmGeneratorExpressionContext* context,
  1502. const GeneratorExpressionContent* content)
  1503. {
  1504. if (target->IsImported()) {
  1505. ::reportError(context, content->GetOriginalExpression(),
  1506. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1507. return std::string();
  1508. }
  1509. std::string language = target->GetLinkerLanguage(context->Config);
  1510. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1511. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1512. ::reportError(context, content->GetOriginalExpression(),
  1513. "TARGET_PDB_FILE is not supported by the target linker.");
  1514. return std::string();
  1515. }
  1516. cmStateEnums::TargetType targetType = target->GetType();
  1517. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1518. targetType != cmStateEnums::MODULE_LIBRARY &&
  1519. targetType != cmStateEnums::EXECUTABLE) {
  1520. ::reportError(context, content->GetOriginalExpression(),
  1521. "TARGET_PDB_FILE is allowed only for "
  1522. "targets with linker created artifacts.");
  1523. return std::string();
  1524. }
  1525. std::string result = target->GetPDBDirectory(context->Config);
  1526. result += "/";
  1527. result += target->GetPDBName(context->Config);
  1528. return result;
  1529. }
  1530. };
  1531. template <>
  1532. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1533. {
  1534. static std::string Create(cmGeneratorTarget* target,
  1535. cmGeneratorExpressionContext* context,
  1536. const GeneratorExpressionContent* content)
  1537. {
  1538. // The file used to link to the target (.so, .lib, .a).
  1539. if (!target->IsLinkable()) {
  1540. ::reportError(context, content->GetOriginalExpression(),
  1541. "TARGET_LINKER_FILE is allowed only for libraries and "
  1542. "executables with ENABLE_EXPORTS.");
  1543. return std::string();
  1544. }
  1545. cmStateEnums::ArtifactType artifact =
  1546. target->HasImportLibrary(context->Config)
  1547. ? cmStateEnums::ImportLibraryArtifact
  1548. : cmStateEnums::RuntimeBinaryArtifact;
  1549. return target->GetFullPath(context->Config, artifact);
  1550. }
  1551. };
  1552. template <>
  1553. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1554. {
  1555. static std::string Create(cmGeneratorTarget* target,
  1556. cmGeneratorExpressionContext* context,
  1557. const GeneratorExpressionContent* content)
  1558. {
  1559. if (target->IsImported()) {
  1560. ::reportError(context, content->GetOriginalExpression(),
  1561. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1562. return std::string();
  1563. }
  1564. if (!target->IsBundleOnApple()) {
  1565. ::reportError(context, content->GetOriginalExpression(),
  1566. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1567. return std::string();
  1568. }
  1569. std::string outpath = target->GetDirectory(context->Config) + '/';
  1570. return target->BuildBundleDirectory(outpath, context->Config,
  1571. cmGeneratorTarget::BundleDirLevel);
  1572. }
  1573. };
  1574. template <>
  1575. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1576. {
  1577. static std::string Create(cmGeneratorTarget* target,
  1578. cmGeneratorExpressionContext* context,
  1579. const GeneratorExpressionContent* content)
  1580. {
  1581. if (target->IsImported()) {
  1582. ::reportError(
  1583. context, content->GetOriginalExpression(),
  1584. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1585. return std::string();
  1586. }
  1587. if (!target->IsBundleOnApple()) {
  1588. ::reportError(
  1589. context, content->GetOriginalExpression(),
  1590. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1591. return std::string();
  1592. }
  1593. std::string outpath = target->GetDirectory(context->Config) + '/';
  1594. return target->BuildBundleDirectory(outpath, context->Config,
  1595. cmGeneratorTarget::ContentLevel);
  1596. }
  1597. };
  1598. template <>
  1599. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1600. {
  1601. static std::string Create(cmGeneratorTarget* target,
  1602. cmGeneratorExpressionContext* context,
  1603. const GeneratorExpressionContent* /*unused*/)
  1604. {
  1605. return target->GetFullPath(context->Config,
  1606. cmStateEnums::RuntimeBinaryArtifact, true);
  1607. }
  1608. };
  1609. template <typename ArtifactT>
  1610. struct TargetFilesystemArtifactResultGetter
  1611. {
  1612. static std::string Get(const std::string& result);
  1613. };
  1614. template <>
  1615. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1616. {
  1617. static std::string Get(const std::string& result)
  1618. {
  1619. return cmSystemTools::GetFilenameName(result);
  1620. }
  1621. };
  1622. template <>
  1623. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1624. {
  1625. static std::string Get(const std::string& result)
  1626. {
  1627. return cmSystemTools::GetFilenamePath(result);
  1628. }
  1629. };
  1630. template <>
  1631. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1632. {
  1633. static std::string Get(const std::string& result) { return result; }
  1634. };
  1635. template <typename ArtifactT, typename ComponentT>
  1636. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1637. {
  1638. TargetFilesystemArtifact() {}
  1639. int NumExpectedParameters() const override { return 1; }
  1640. std::string Evaluate(
  1641. const std::vector<std::string>& parameters,
  1642. cmGeneratorExpressionContext* context,
  1643. const GeneratorExpressionContent* content,
  1644. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1645. {
  1646. // Lookup the referenced target.
  1647. std::string name = *parameters.begin();
  1648. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1649. ::reportError(context, content->GetOriginalExpression(),
  1650. "Expression syntax not recognized.");
  1651. return std::string();
  1652. }
  1653. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1654. if (!target) {
  1655. ::reportError(context, content->GetOriginalExpression(),
  1656. "No target \"" + name + "\"");
  1657. return std::string();
  1658. }
  1659. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1660. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1661. ::reportError(context, content->GetOriginalExpression(), "Target \"" +
  1662. name + "\" is not an executable or library.");
  1663. return std::string();
  1664. }
  1665. if (dagChecker && (dagChecker->EvaluatingLinkLibraries(name.c_str()) ||
  1666. (dagChecker->EvaluatingSources() &&
  1667. name == dagChecker->TopTarget()))) {
  1668. ::reportError(context, content->GetOriginalExpression(),
  1669. "Expressions which require the linker language may not "
  1670. "be used while evaluating link libraries");
  1671. return std::string();
  1672. }
  1673. context->DependTargets.insert(target);
  1674. context->AllTargets.insert(target);
  1675. std::string result =
  1676. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1677. content);
  1678. if (context->HadError) {
  1679. return std::string();
  1680. }
  1681. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1682. }
  1683. };
  1684. template <typename ArtifactT>
  1685. struct TargetFilesystemArtifactNodeGroup
  1686. {
  1687. TargetFilesystemArtifactNodeGroup() {}
  1688. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1689. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1690. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1691. };
  1692. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1693. targetNodeGroup;
  1694. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1695. targetLinkerNodeGroup;
  1696. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1697. targetSoNameNodeGroup;
  1698. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1699. targetPdbNodeGroup;
  1700. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1701. targetBundleDirNode;
  1702. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1703. ArtifactPathTag>
  1704. targetBundleContentDirNode;
  1705. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1706. {
  1707. ShellPathNode() {}
  1708. std::string Evaluate(
  1709. const std::vector<std::string>& parameters,
  1710. cmGeneratorExpressionContext* context,
  1711. const GeneratorExpressionContent* content,
  1712. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1713. {
  1714. if (!cmSystemTools::FileIsFullPath(parameters.front())) {
  1715. reportError(context, content->GetOriginalExpression(),
  1716. "\"" + parameters.front() + "\" is not an absolute path.");
  1717. return std::string();
  1718. }
  1719. cmOutputConverter converter(context->LG->GetStateSnapshot());
  1720. return converter.ConvertDirectorySeparatorsForShell(parameters.front());
  1721. }
  1722. } shellPathNode;
  1723. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1724. const std::string& identifier)
  1725. {
  1726. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1727. static NodeMap nodeMap;
  1728. if (nodeMap.empty()) {
  1729. nodeMap["0"] = &zeroNode;
  1730. nodeMap["1"] = &oneNode;
  1731. nodeMap["AND"] = &andNode;
  1732. nodeMap["OR"] = &orNode;
  1733. nodeMap["NOT"] = &notNode;
  1734. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1735. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1736. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1737. nodeMap["VERSION_GREATER_EQUAL"] = &versionGreaterEqNode;
  1738. nodeMap["VERSION_LESS"] = &versionLessNode;
  1739. nodeMap["VERSION_LESS_EQUAL"] = &versionLessEqNode;
  1740. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1741. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1742. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1743. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1744. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1745. nodeMap["CONFIGURATION"] = &configurationNode;
  1746. nodeMap["CONFIG"] = &configurationTestNode;
  1747. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1748. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1749. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1750. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1751. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1752. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1753. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1754. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1755. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1756. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1757. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1758. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1759. nodeMap["TARGET_BUNDLE_DIR"] = &targetBundleDirNode;
  1760. nodeMap["TARGET_BUNDLE_CONTENT_DIR"] = &targetBundleContentDirNode;
  1761. nodeMap["STREQUAL"] = &strEqualNode;
  1762. nodeMap["EQUAL"] = &equalNode;
  1763. nodeMap["IN_LIST"] = &inListNode;
  1764. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1765. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1766. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1767. nodeMap["BOOL"] = &boolNode;
  1768. nodeMap["IF"] = &ifNode;
  1769. nodeMap["ANGLE-R"] = &angle_rNode;
  1770. nodeMap["COMMA"] = &commaNode;
  1771. nodeMap["SEMICOLON"] = &semicolonNode;
  1772. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1773. nodeMap["TARGET_NAME"] = &targetNameNode;
  1774. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1775. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1776. nodeMap["TARGET_EXISTS"] = &targetExistsNode;
  1777. nodeMap["TARGET_NAME_IF_EXISTS"] = &targetNameIfExistsNode;
  1778. nodeMap["TARGET_GENEX_EVAL"] = &targetGenexEvalNode;
  1779. nodeMap["GENEX_EVAL"] = &genexEvalNode;
  1780. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1781. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1782. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1783. nodeMap["JOIN"] = &joinNode;
  1784. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1785. nodeMap["COMPILE_LANGUAGE"] = &languageNode;
  1786. nodeMap["SHELL_PATH"] = &shellPathNode;
  1787. }
  1788. NodeMap::const_iterator i = nodeMap.find(identifier);
  1789. if (i == nodeMap.end()) {
  1790. return nullptr;
  1791. }
  1792. return i->second;
  1793. }
  1794. void reportError(cmGeneratorExpressionContext* context,
  1795. const std::string& expr, const std::string& result)
  1796. {
  1797. context->HadError = true;
  1798. if (context->Quiet) {
  1799. return;
  1800. }
  1801. std::ostringstream e;
  1802. /* clang-format off */
  1803. e << "Error evaluating generator expression:\n"
  1804. << " " << expr << "\n"
  1805. << result;
  1806. /* clang-format on */
  1807. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1808. context->Backtrace);
  1809. }