cmGeneratorExpressionNode.cxx 68 KB

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