cmGeneratorExpressionNode.cxx 68 KB

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