cmGeneratorExpressionNode.cxx 66 KB

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