cmGeneratorExpressionNode.cxx 76 KB

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