cmGeneratorExpressionNode.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. CM_FALLTHROUGH;
  353. }
  354. case cmPolicies::OLD:
  355. return "1";
  356. case cmPolicies::NEW:
  357. case cmPolicies::REQUIRED_ALWAYS:
  358. case cmPolicies::REQUIRED_IF_USED:
  359. break;
  360. }
  361. }
  362. return "0";
  363. }
  364. };
  365. static const struct CCompilerIdNode : public CompilerIdNode
  366. {
  367. CCompilerIdNode() {}
  368. std::string Evaluate(const std::vector<std::string>& parameters,
  369. cmGeneratorExpressionContext* context,
  370. const GeneratorExpressionContent* content,
  371. cmGeneratorExpressionDAGChecker* dagChecker) const
  372. CM_OVERRIDE
  373. {
  374. if (!context->HeadTarget) {
  375. reportError(
  376. context, content->GetOriginalExpression(),
  377. "$<C_COMPILER_ID> may only be used with binary targets. It may "
  378. "not be used with add_custom_command or add_custom_target.");
  379. return std::string();
  380. }
  381. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  382. "C");
  383. }
  384. } cCompilerIdNode;
  385. static const struct CXXCompilerIdNode : public CompilerIdNode
  386. {
  387. CXXCompilerIdNode() {}
  388. std::string Evaluate(const std::vector<std::string>& parameters,
  389. cmGeneratorExpressionContext* context,
  390. const GeneratorExpressionContent* content,
  391. cmGeneratorExpressionDAGChecker* dagChecker) const
  392. CM_OVERRIDE
  393. {
  394. if (!context->HeadTarget) {
  395. reportError(
  396. context, content->GetOriginalExpression(),
  397. "$<CXX_COMPILER_ID> may only be used with binary targets. It may "
  398. "not be used with add_custom_command or add_custom_target.");
  399. return std::string();
  400. }
  401. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  402. "CXX");
  403. }
  404. } cxxCompilerIdNode;
  405. struct CompilerVersionNode : public cmGeneratorExpressionNode
  406. {
  407. CompilerVersionNode() {}
  408. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  409. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  410. cmGeneratorExpressionContext* context,
  411. const GeneratorExpressionContent* content,
  412. cmGeneratorExpressionDAGChecker* /*unused*/,
  413. const std::string& lang) const
  414. {
  415. const char* compilerVersion =
  416. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  417. "_COMPILER_VERSION");
  418. if (parameters.empty()) {
  419. return compilerVersion ? compilerVersion : "";
  420. }
  421. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  422. if (!compilerIdValidator.find(*parameters.begin())) {
  423. reportError(context, content->GetOriginalExpression(),
  424. "Expression syntax not recognized.");
  425. return std::string();
  426. }
  427. if (!compilerVersion) {
  428. return parameters.front().empty() ? "1" : "0";
  429. }
  430. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  431. parameters.begin()->c_str(),
  432. compilerVersion)
  433. ? "1"
  434. : "0";
  435. }
  436. };
  437. static const struct CCompilerVersionNode : public CompilerVersionNode
  438. {
  439. CCompilerVersionNode() {}
  440. std::string Evaluate(const std::vector<std::string>& parameters,
  441. cmGeneratorExpressionContext* context,
  442. const GeneratorExpressionContent* content,
  443. cmGeneratorExpressionDAGChecker* dagChecker) const
  444. CM_OVERRIDE
  445. {
  446. if (!context->HeadTarget) {
  447. reportError(
  448. context, content->GetOriginalExpression(),
  449. "$<C_COMPILER_VERSION> may only be used with binary targets. It "
  450. "may not be used with add_custom_command or add_custom_target.");
  451. return std::string();
  452. }
  453. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  454. "C");
  455. }
  456. } cCompilerVersionNode;
  457. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  458. {
  459. CxxCompilerVersionNode() {}
  460. std::string Evaluate(const std::vector<std::string>& parameters,
  461. cmGeneratorExpressionContext* context,
  462. const GeneratorExpressionContent* content,
  463. cmGeneratorExpressionDAGChecker* dagChecker) const
  464. CM_OVERRIDE
  465. {
  466. if (!context->HeadTarget) {
  467. reportError(
  468. context, content->GetOriginalExpression(),
  469. "$<CXX_COMPILER_VERSION> may only be used with binary targets. It "
  470. "may not be used with add_custom_command or add_custom_target.");
  471. return std::string();
  472. }
  473. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  474. "CXX");
  475. }
  476. } cxxCompilerVersionNode;
  477. struct PlatformIdNode : public cmGeneratorExpressionNode
  478. {
  479. PlatformIdNode() {}
  480. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  481. std::string Evaluate(const std::vector<std::string>& parameters,
  482. cmGeneratorExpressionContext* context,
  483. const GeneratorExpressionContent* /*content*/,
  484. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  485. CM_OVERRIDE
  486. {
  487. const char* platformId =
  488. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  489. if (parameters.empty()) {
  490. return platformId ? platformId : "";
  491. }
  492. if (!platformId) {
  493. return parameters.front().empty() ? "1" : "0";
  494. }
  495. if (strcmp(parameters.begin()->c_str(), platformId) == 0) {
  496. return "1";
  497. }
  498. return "0";
  499. }
  500. } platformIdNode;
  501. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  502. {
  503. VersionGreaterNode() {}
  504. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  505. std::string Evaluate(const std::vector<std::string>& parameters,
  506. cmGeneratorExpressionContext* /*context*/,
  507. const GeneratorExpressionContent* /*content*/,
  508. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  509. CM_OVERRIDE
  510. {
  511. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  512. parameters.front().c_str(),
  513. parameters[1].c_str())
  514. ? "1"
  515. : "0";
  516. }
  517. } versionGreaterNode;
  518. static const struct VersionGreaterEqNode : public cmGeneratorExpressionNode
  519. {
  520. VersionGreaterEqNode() {}
  521. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  522. std::string Evaluate(const std::vector<std::string>& parameters,
  523. cmGeneratorExpressionContext* /*context*/,
  524. const GeneratorExpressionContent* /*content*/,
  525. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  526. CM_OVERRIDE
  527. {
  528. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  529. parameters.front().c_str(),
  530. parameters[1].c_str())
  531. ? "1"
  532. : "0";
  533. }
  534. } versionGreaterEqNode;
  535. static const struct VersionLessNode : public cmGeneratorExpressionNode
  536. {
  537. VersionLessNode() {}
  538. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  539. std::string Evaluate(const std::vector<std::string>& parameters,
  540. cmGeneratorExpressionContext* /*context*/,
  541. const GeneratorExpressionContent* /*content*/,
  542. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  543. CM_OVERRIDE
  544. {
  545. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  546. parameters.front().c_str(),
  547. parameters[1].c_str())
  548. ? "1"
  549. : "0";
  550. }
  551. } versionLessNode;
  552. static const struct VersionLessEqNode : public cmGeneratorExpressionNode
  553. {
  554. VersionLessEqNode() {}
  555. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  556. std::string Evaluate(const std::vector<std::string>& parameters,
  557. cmGeneratorExpressionContext* /*context*/,
  558. const GeneratorExpressionContent* /*content*/,
  559. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  560. CM_OVERRIDE
  561. {
  562. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS_EQUAL,
  563. parameters.front().c_str(),
  564. parameters[1].c_str())
  565. ? "1"
  566. : "0";
  567. }
  568. } versionLessEqNode;
  569. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  570. {
  571. VersionEqualNode() {}
  572. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  573. std::string Evaluate(const std::vector<std::string>& parameters,
  574. cmGeneratorExpressionContext* /*context*/,
  575. const GeneratorExpressionContent* /*content*/,
  576. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  577. CM_OVERRIDE
  578. {
  579. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  580. parameters.front().c_str(),
  581. parameters[1].c_str())
  582. ? "1"
  583. : "0";
  584. }
  585. } versionEqualNode;
  586. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  587. {
  588. LinkOnlyNode() {}
  589. std::string Evaluate(const std::vector<std::string>& parameters,
  590. cmGeneratorExpressionContext* context,
  591. const GeneratorExpressionContent* content,
  592. cmGeneratorExpressionDAGChecker* dagChecker) const
  593. CM_OVERRIDE
  594. {
  595. if (!dagChecker) {
  596. reportError(context, content->GetOriginalExpression(),
  597. "$<LINK_ONLY:...> may only be used for linking");
  598. return std::string();
  599. }
  600. if (!dagChecker->GetTransitivePropertiesOnly()) {
  601. return parameters.front();
  602. }
  603. return std::string();
  604. }
  605. } linkOnlyNode;
  606. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  607. {
  608. ConfigurationNode() {}
  609. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  610. std::string Evaluate(const std::vector<std::string>& /*parameters*/,
  611. cmGeneratorExpressionContext* context,
  612. const GeneratorExpressionContent* /*content*/,
  613. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  614. CM_OVERRIDE
  615. {
  616. context->HadContextSensitiveCondition = true;
  617. return context->Config;
  618. }
  619. } configurationNode;
  620. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  621. {
  622. ConfigurationTestNode() {}
  623. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  624. std::string Evaluate(const std::vector<std::string>& parameters,
  625. cmGeneratorExpressionContext* context,
  626. const GeneratorExpressionContent* content,
  627. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  628. CM_OVERRIDE
  629. {
  630. if (parameters.empty()) {
  631. return configurationNode.Evaluate(parameters, context, content,
  632. CM_NULLPTR);
  633. }
  634. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  635. if (!configValidator.find(*parameters.begin())) {
  636. reportError(context, content->GetOriginalExpression(),
  637. "Expression syntax not recognized.");
  638. return std::string();
  639. }
  640. context->HadContextSensitiveCondition = true;
  641. if (context->Config.empty()) {
  642. return parameters.front().empty() ? "1" : "0";
  643. }
  644. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  645. context->Config.c_str()) == 0) {
  646. return "1";
  647. }
  648. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  649. const char* loc = CM_NULLPTR;
  650. const char* imp = CM_NULLPTR;
  651. std::string suffix;
  652. if (context->CurrentTarget->Target->GetMappedConfig(
  653. context->Config, &loc, &imp, suffix)) {
  654. // This imported target has an appropriate location
  655. // for this (possibly mapped) config.
  656. // Check if there is a proper config mapping for the tested config.
  657. std::vector<std::string> mappedConfigs;
  658. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  659. mapProp += cmSystemTools::UpperCase(context->Config);
  660. if (const char* mapValue =
  661. context->CurrentTarget->GetProperty(mapProp)) {
  662. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  663. mappedConfigs);
  664. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  665. cmSystemTools::UpperCase(parameters.front())) !=
  666. mappedConfigs.end()
  667. ? "1"
  668. : "0";
  669. }
  670. }
  671. }
  672. return "0";
  673. }
  674. } configurationTestNode;
  675. static const struct JoinNode : public cmGeneratorExpressionNode
  676. {
  677. JoinNode() {}
  678. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  679. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  680. std::string Evaluate(const std::vector<std::string>& parameters,
  681. cmGeneratorExpressionContext* /*context*/,
  682. const GeneratorExpressionContent* /*content*/,
  683. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  684. CM_OVERRIDE
  685. {
  686. std::vector<std::string> list;
  687. cmSystemTools::ExpandListArgument(parameters.front(), list);
  688. return cmJoin(list, parameters[1]);
  689. }
  690. } joinNode;
  691. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  692. {
  693. CompileLanguageNode() {}
  694. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  695. std::string Evaluate(const std::vector<std::string>& parameters,
  696. cmGeneratorExpressionContext* context,
  697. const GeneratorExpressionContent* content,
  698. cmGeneratorExpressionDAGChecker* dagChecker) const
  699. CM_OVERRIDE
  700. {
  701. if (context->Language.empty()) {
  702. reportError(
  703. context, content->GetOriginalExpression(),
  704. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  705. "directories compile definitions, compile options and to evaluate "
  706. "components of the file(GENERATE) command.");
  707. return std::string();
  708. }
  709. std::vector<std::string> enabledLanguages;
  710. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  711. gg->GetEnabledLanguages(enabledLanguages);
  712. if (!parameters.empty() &&
  713. std::find(enabledLanguages.begin(), enabledLanguages.end(),
  714. parameters.front()) == enabledLanguages.end()) {
  715. reportError(context, content->GetOriginalExpression(),
  716. "$<COMPILE_LANGUAGE:...> Unknown language.");
  717. return std::string();
  718. }
  719. std::string genName = gg->GetName();
  720. if (genName.find("Visual Studio") != std::string::npos) {
  721. reportError(context, content->GetOriginalExpression(),
  722. "$<COMPILE_LANGUAGE:...> may not be used with Visual Studio "
  723. "generators.");
  724. return std::string();
  725. }
  726. if (genName.find("Xcode") != std::string::npos) {
  727. if (dagChecker && (dagChecker->EvaluatingCompileDefinitions() ||
  728. dagChecker->EvaluatingIncludeDirectories())) {
  729. reportError(
  730. context, content->GetOriginalExpression(),
  731. "$<COMPILE_LANGUAGE:...> may only be used with COMPILE_OPTIONS "
  732. "with the Xcode generator.");
  733. return std::string();
  734. }
  735. } else {
  736. if (genName.find("Makefiles") == std::string::npos &&
  737. genName.find("Ninja") == std::string::npos &&
  738. genName.find("Watcom WMake") == std::string::npos) {
  739. reportError(
  740. context, content->GetOriginalExpression(),
  741. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  742. return std::string();
  743. }
  744. }
  745. if (parameters.empty()) {
  746. return context->Language;
  747. }
  748. return context->Language == parameters.front() ? "1" : "0";
  749. }
  750. } languageNode;
  751. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) , "INTERFACE_" #PROPERTY
  752. static const char* targetPropertyTransitiveWhitelist[] = {
  753. CM_NULLPTR CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  754. };
  755. #undef TRANSITIVE_PROPERTY_NAME
  756. template <typename T>
  757. std::string getLinkedTargetsContent(
  758. std::vector<T> const& libraries, cmGeneratorTarget const* target,
  759. cmGeneratorTarget const* headTarget, cmGeneratorExpressionContext* context,
  760. cmGeneratorExpressionDAGChecker* dagChecker,
  761. const std::string& interfacePropertyName)
  762. {
  763. std::string linkedTargetsContent;
  764. std::string sep;
  765. std::string depString;
  766. for (typename std::vector<T>::const_iterator it = libraries.begin();
  767. it != libraries.end(); ++it) {
  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 (it->Target && it->Target != target) {
  772. depString += sep + "$<TARGET_PROPERTY:" + it->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 CM_OVERRIDE { return OneOrMoreParameters; }
  792. std::string Evaluate(const std::vector<std::string>& parameters,
  793. cmGeneratorExpressionContext* context,
  794. const GeneratorExpressionContent* content,
  795. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  796. CM_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 < cmArraySize(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 CM_OVERRIDE { return true; }
  1058. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  1059. bool RequiresLiteralInput() const CM_OVERRIDE { return true; }
  1060. std::string Evaluate(const std::vector<std::string>& parameters,
  1061. cmGeneratorExpressionContext* /*context*/,
  1062. const GeneratorExpressionContent* /*content*/,
  1063. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  1064. CM_OVERRIDE
  1065. {
  1066. return parameters.front();
  1067. }
  1068. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  1069. } targetNameNode;
  1070. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1071. {
  1072. TargetObjectsNode() {}
  1073. std::string Evaluate(const std::vector<std::string>& parameters,
  1074. cmGeneratorExpressionContext* context,
  1075. const GeneratorExpressionContent* content,
  1076. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  1077. CM_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 = CM_NULLPTR;
  1111. const char* imp = CM_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::vector<std::string>::iterator oi = objects.begin();
  1132. oi != objects.end(); ++oi) {
  1133. *oi = obj_dir + *oi;
  1134. }
  1135. }
  1136. // Create the cmSourceFile instances in the referencing directory.
  1137. cmMakefile* mf = context->LG->GetMakefile();
  1138. for (std::vector<std::string>::iterator oi = objects.begin();
  1139. oi != objects.end(); ++oi) {
  1140. mf->AddTargetObject(tgtName, *oi);
  1141. }
  1142. return cmJoin(objects, ";");
  1143. }
  1144. } targetObjectsNode;
  1145. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1146. {
  1147. CompileFeaturesNode() {}
  1148. int NumExpectedParameters() const CM_OVERRIDE { return OneOrMoreParameters; }
  1149. std::string Evaluate(const std::vector<std::string>& parameters,
  1150. cmGeneratorExpressionContext* context,
  1151. const GeneratorExpressionContent* content,
  1152. cmGeneratorExpressionDAGChecker* dagChecker) const
  1153. CM_OVERRIDE
  1154. {
  1155. cmGeneratorTarget const* target = context->HeadTarget;
  1156. if (!target) {
  1157. reportError(
  1158. context, content->GetOriginalExpression(),
  1159. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1160. "not be used with add_custom_command or add_custom_target.");
  1161. return std::string();
  1162. }
  1163. context->HadHeadSensitiveCondition = true;
  1164. typedef std::map<std::string, std::vector<std::string> > LangMap;
  1165. static LangMap availableFeatures;
  1166. LangMap testedFeatures;
  1167. for (std::vector<std::string>::const_iterator it = parameters.begin();
  1168. it != parameters.end(); ++it) {
  1169. std::string error;
  1170. std::string lang;
  1171. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1172. context->HeadTarget->Target, *it, lang, &error)) {
  1173. reportError(context, content->GetOriginalExpression(), error);
  1174. return std::string();
  1175. }
  1176. testedFeatures[lang].push_back(*it);
  1177. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1178. const char* featuresKnown =
  1179. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1180. if (!featuresKnown) {
  1181. reportError(context, content->GetOriginalExpression(), error);
  1182. return std::string();
  1183. }
  1184. cmSystemTools::ExpandListArgument(featuresKnown,
  1185. availableFeatures[lang]);
  1186. }
  1187. }
  1188. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1189. for (LangMap::const_iterator lit = testedFeatures.begin();
  1190. lit != testedFeatures.end(); ++lit) {
  1191. std::vector<std::string> const& langAvailable =
  1192. availableFeatures[lit->first];
  1193. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1194. "CMAKE_" + lit->first + "_STANDARD_DEFAULT");
  1195. for (std::vector<std::string>::const_iterator it = lit->second.begin();
  1196. it != lit->second.end(); ++it) {
  1197. if (std::find(langAvailable.begin(), langAvailable.end(), *it) ==
  1198. langAvailable.end()) {
  1199. return "0";
  1200. }
  1201. if (standardDefault && !*standardDefault) {
  1202. // This compiler has no notion of language standard levels.
  1203. // All features known for the language are always available.
  1204. continue;
  1205. }
  1206. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1207. target->Target, lit->first, *it)) {
  1208. if (evalLL) {
  1209. const char* l = target->GetProperty(lit->first + "_STANDARD");
  1210. if (!l) {
  1211. l = standardDefault;
  1212. }
  1213. assert(l);
  1214. context->MaxLanguageStandard[target][lit->first] = l;
  1215. } else {
  1216. return "0";
  1217. }
  1218. }
  1219. }
  1220. }
  1221. return "1";
  1222. }
  1223. } compileFeaturesNode;
  1224. static const char* targetPolicyWhitelist[] = {
  1225. CM_NULLPTR
  1226. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1227. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1228. #undef TARGET_POLICY_STRING
  1229. };
  1230. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1231. const char* policy)
  1232. {
  1233. #define RETURN_POLICY(POLICY) \
  1234. if (strcmp(policy, #POLICY) == 0) { \
  1235. return tgt->GetPolicyStatus##POLICY(); \
  1236. }
  1237. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1238. #undef RETURN_POLICY
  1239. assert(false && "Unreachable code. Not a valid policy");
  1240. return cmPolicies::WARN;
  1241. }
  1242. cmPolicies::PolicyID policyForString(const char* policy_id)
  1243. {
  1244. #define RETURN_POLICY_ID(POLICY_ID) \
  1245. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1246. return cmPolicies::POLICY_ID; \
  1247. }
  1248. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1249. #undef RETURN_POLICY_ID
  1250. assert(false && "Unreachable code. Not a valid policy");
  1251. return cmPolicies::CMP0002;
  1252. }
  1253. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1254. {
  1255. TargetPolicyNode() {}
  1256. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  1257. std::string Evaluate(const std::vector<std::string>& parameters,
  1258. cmGeneratorExpressionContext* context,
  1259. const GeneratorExpressionContent* content,
  1260. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  1261. CM_OVERRIDE
  1262. {
  1263. if (!context->HeadTarget) {
  1264. reportError(
  1265. context, content->GetOriginalExpression(),
  1266. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1267. "may not be used with add_custom_command or add_custom_target.");
  1268. return std::string();
  1269. }
  1270. context->HadContextSensitiveCondition = true;
  1271. context->HadHeadSensitiveCondition = true;
  1272. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i) {
  1273. const char* policy = targetPolicyWhitelist[i];
  1274. if (parameters.front() == policy) {
  1275. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1276. switch (statusForTarget(context->HeadTarget, policy)) {
  1277. case cmPolicies::WARN:
  1278. lg->IssueMessage(
  1279. cmake::AUTHOR_WARNING,
  1280. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1281. CM_FALLTHROUGH;
  1282. case cmPolicies::REQUIRED_IF_USED:
  1283. case cmPolicies::REQUIRED_ALWAYS:
  1284. case cmPolicies::OLD:
  1285. return "0";
  1286. case cmPolicies::NEW:
  1287. return "1";
  1288. }
  1289. }
  1290. }
  1291. reportError(
  1292. context, content->GetOriginalExpression(),
  1293. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1294. "policies. Currently it may be used with the following policies:\n"
  1295. #define STRINGIFY_HELPER(X) #X
  1296. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1297. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1298. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1299. #undef TARGET_POLICY_LIST_ITEM
  1300. );
  1301. return std::string();
  1302. }
  1303. } targetPolicyNode;
  1304. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1305. {
  1306. InstallPrefixNode() {}
  1307. bool GeneratesContent() const CM_OVERRIDE { return true; }
  1308. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  1309. std::string Evaluate(const std::vector<std::string>& /*parameters*/,
  1310. cmGeneratorExpressionContext* context,
  1311. const GeneratorExpressionContent* content,
  1312. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  1313. CM_OVERRIDE
  1314. {
  1315. reportError(context, content->GetOriginalExpression(),
  1316. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1317. "should never be evaluated.");
  1318. return std::string();
  1319. }
  1320. } installPrefixNode;
  1321. class ArtifactDirTag;
  1322. class ArtifactLinkerTag;
  1323. class ArtifactNameTag;
  1324. class ArtifactPathTag;
  1325. class ArtifactPdbTag;
  1326. class ArtifactSonameTag;
  1327. class ArtifactBundleDirTag;
  1328. class ArtifactBundleContentDirTag;
  1329. template <typename ArtifactT>
  1330. struct TargetFilesystemArtifactResultCreator
  1331. {
  1332. static std::string Create(cmGeneratorTarget* target,
  1333. cmGeneratorExpressionContext* context,
  1334. const GeneratorExpressionContent* content);
  1335. };
  1336. template <>
  1337. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1338. {
  1339. static std::string Create(cmGeneratorTarget* target,
  1340. cmGeneratorExpressionContext* context,
  1341. const GeneratorExpressionContent* content)
  1342. {
  1343. // The target soname file (.so.1).
  1344. if (target->IsDLLPlatform()) {
  1345. ::reportError(context, content->GetOriginalExpression(),
  1346. "TARGET_SONAME_FILE is not allowed "
  1347. "for DLL target platforms.");
  1348. return std::string();
  1349. }
  1350. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1351. ::reportError(context, content->GetOriginalExpression(),
  1352. "TARGET_SONAME_FILE is allowed only for "
  1353. "SHARED libraries.");
  1354. return std::string();
  1355. }
  1356. std::string result = target->GetDirectory(context->Config);
  1357. result += "/";
  1358. result += target->GetSOName(context->Config);
  1359. return result;
  1360. }
  1361. };
  1362. template <>
  1363. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1364. {
  1365. static std::string Create(cmGeneratorTarget* target,
  1366. cmGeneratorExpressionContext* context,
  1367. const GeneratorExpressionContent* content)
  1368. {
  1369. if (target->IsImported()) {
  1370. ::reportError(context, content->GetOriginalExpression(),
  1371. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1372. return std::string();
  1373. }
  1374. std::string language = target->GetLinkerLanguage(context->Config);
  1375. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1376. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1377. ::reportError(context, content->GetOriginalExpression(),
  1378. "TARGET_PDB_FILE is not supported by the target linker.");
  1379. return std::string();
  1380. }
  1381. cmStateEnums::TargetType targetType = target->GetType();
  1382. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1383. targetType != cmStateEnums::MODULE_LIBRARY &&
  1384. targetType != cmStateEnums::EXECUTABLE) {
  1385. ::reportError(context, content->GetOriginalExpression(),
  1386. "TARGET_PDB_FILE is allowed only for "
  1387. "targets with linker created artifacts.");
  1388. return std::string();
  1389. }
  1390. std::string result = target->GetPDBDirectory(context->Config);
  1391. result += "/";
  1392. result += target->GetPDBName(context->Config);
  1393. return result;
  1394. }
  1395. };
  1396. template <>
  1397. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1398. {
  1399. static std::string Create(cmGeneratorTarget* target,
  1400. cmGeneratorExpressionContext* context,
  1401. const GeneratorExpressionContent* content)
  1402. {
  1403. // The file used to link to the target (.so, .lib, .a).
  1404. if (!target->IsLinkable()) {
  1405. ::reportError(context, content->GetOriginalExpression(),
  1406. "TARGET_LINKER_FILE is allowed only for libraries and "
  1407. "executables with ENABLE_EXPORTS.");
  1408. return std::string();
  1409. }
  1410. cmStateEnums::ArtifactType artifact = target->HasImportLibrary()
  1411. ? cmStateEnums::ImportLibraryArtifact
  1412. : cmStateEnums::RuntimeBinaryArtifact;
  1413. return target->GetFullPath(context->Config, artifact);
  1414. }
  1415. };
  1416. template <>
  1417. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1418. {
  1419. static std::string Create(cmGeneratorTarget* target,
  1420. cmGeneratorExpressionContext* context,
  1421. const GeneratorExpressionContent* content)
  1422. {
  1423. if (target->IsImported()) {
  1424. ::reportError(context, content->GetOriginalExpression(),
  1425. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1426. return std::string();
  1427. }
  1428. if (!target->IsBundleOnApple()) {
  1429. ::reportError(context, content->GetOriginalExpression(),
  1430. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1431. return std::string();
  1432. }
  1433. std::string outpath = target->GetDirectory(context->Config) + '/';
  1434. return target->BuildBundleDirectory(outpath, context->Config,
  1435. cmGeneratorTarget::BundleDirLevel);
  1436. }
  1437. };
  1438. template <>
  1439. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1440. {
  1441. static std::string Create(cmGeneratorTarget* target,
  1442. cmGeneratorExpressionContext* context,
  1443. const GeneratorExpressionContent* content)
  1444. {
  1445. if (target->IsImported()) {
  1446. ::reportError(
  1447. context, content->GetOriginalExpression(),
  1448. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1449. return std::string();
  1450. }
  1451. if (!target->IsBundleOnApple()) {
  1452. ::reportError(
  1453. context, content->GetOriginalExpression(),
  1454. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1455. return std::string();
  1456. }
  1457. std::string outpath = target->GetDirectory(context->Config) + '/';
  1458. return target->BuildBundleDirectory(outpath, context->Config,
  1459. cmGeneratorTarget::ContentLevel);
  1460. }
  1461. };
  1462. template <>
  1463. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1464. {
  1465. static std::string Create(cmGeneratorTarget* target,
  1466. cmGeneratorExpressionContext* context,
  1467. const GeneratorExpressionContent* /*unused*/)
  1468. {
  1469. return target->GetFullPath(context->Config,
  1470. cmStateEnums::RuntimeBinaryArtifact, true);
  1471. }
  1472. };
  1473. template <typename ArtifactT>
  1474. struct TargetFilesystemArtifactResultGetter
  1475. {
  1476. static std::string Get(const std::string& result);
  1477. };
  1478. template <>
  1479. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1480. {
  1481. static std::string Get(const std::string& result)
  1482. {
  1483. return cmSystemTools::GetFilenameName(result);
  1484. }
  1485. };
  1486. template <>
  1487. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1488. {
  1489. static std::string Get(const std::string& result)
  1490. {
  1491. return cmSystemTools::GetFilenamePath(result);
  1492. }
  1493. };
  1494. template <>
  1495. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1496. {
  1497. static std::string Get(const std::string& result) { return result; }
  1498. };
  1499. template <typename ArtifactT, typename ComponentT>
  1500. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1501. {
  1502. TargetFilesystemArtifact() {}
  1503. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  1504. std::string Evaluate(const std::vector<std::string>& parameters,
  1505. cmGeneratorExpressionContext* context,
  1506. const GeneratorExpressionContent* content,
  1507. cmGeneratorExpressionDAGChecker* dagChecker) const
  1508. CM_OVERRIDE
  1509. {
  1510. // Lookup the referenced target.
  1511. std::string name = *parameters.begin();
  1512. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1513. ::reportError(context, content->GetOriginalExpression(),
  1514. "Expression syntax not recognized.");
  1515. return std::string();
  1516. }
  1517. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1518. if (!target) {
  1519. ::reportError(context, content->GetOriginalExpression(),
  1520. "No target \"" + name + "\"");
  1521. return std::string();
  1522. }
  1523. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1524. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1525. ::reportError(context, content->GetOriginalExpression(), "Target \"" +
  1526. name + "\" is not an executable or library.");
  1527. return std::string();
  1528. }
  1529. if (dagChecker && (dagChecker->EvaluatingLinkLibraries(name.c_str()) ||
  1530. (dagChecker->EvaluatingSources() &&
  1531. name == dagChecker->TopTarget()))) {
  1532. ::reportError(context, content->GetOriginalExpression(),
  1533. "Expressions which require the linker language may not "
  1534. "be used while evaluating link libraries");
  1535. return std::string();
  1536. }
  1537. context->DependTargets.insert(target);
  1538. context->AllTargets.insert(target);
  1539. std::string result =
  1540. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1541. content);
  1542. if (context->HadError) {
  1543. return std::string();
  1544. }
  1545. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1546. }
  1547. };
  1548. template <typename ArtifactT>
  1549. struct TargetFilesystemArtifactNodeGroup
  1550. {
  1551. TargetFilesystemArtifactNodeGroup() {}
  1552. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1553. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1554. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1555. };
  1556. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1557. targetNodeGroup;
  1558. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1559. targetLinkerNodeGroup;
  1560. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1561. targetSoNameNodeGroup;
  1562. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1563. targetPdbNodeGroup;
  1564. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1565. targetBundleDirNode;
  1566. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1567. ArtifactPathTag>
  1568. targetBundleContentDirNode;
  1569. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1570. {
  1571. ShellPathNode() {}
  1572. std::string Evaluate(const std::vector<std::string>& parameters,
  1573. cmGeneratorExpressionContext* context,
  1574. const GeneratorExpressionContent* content,
  1575. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const
  1576. CM_OVERRIDE
  1577. {
  1578. if (!cmSystemTools::FileIsFullPath(parameters.front())) {
  1579. reportError(context, content->GetOriginalExpression(),
  1580. "\"" + parameters.front() + "\" is not an absolute path.");
  1581. return std::string();
  1582. }
  1583. cmOutputConverter converter(context->LG->GetStateSnapshot());
  1584. return converter.ConvertDirectorySeparatorsForShell(parameters.front());
  1585. }
  1586. } shellPathNode;
  1587. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1588. const std::string& identifier)
  1589. {
  1590. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1591. static NodeMap nodeMap;
  1592. if (nodeMap.empty()) {
  1593. nodeMap["0"] = &zeroNode;
  1594. nodeMap["1"] = &oneNode;
  1595. nodeMap["AND"] = &andNode;
  1596. nodeMap["OR"] = &orNode;
  1597. nodeMap["NOT"] = &notNode;
  1598. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1599. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1600. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1601. nodeMap["VERSION_GREATER_EQUAL"] = &versionGreaterEqNode;
  1602. nodeMap["VERSION_LESS"] = &versionLessNode;
  1603. nodeMap["VERSION_LESS_EQUAL"] = &versionLessEqNode;
  1604. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1605. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1606. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1607. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1608. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1609. nodeMap["CONFIGURATION"] = &configurationNode;
  1610. nodeMap["CONFIG"] = &configurationTestNode;
  1611. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1612. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1613. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1614. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1615. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1616. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1617. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1618. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1619. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1620. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1621. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1622. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1623. nodeMap["TARGET_BUNDLE_DIR"] = &targetBundleDirNode;
  1624. nodeMap["TARGET_BUNDLE_CONTENT_DIR"] = &targetBundleContentDirNode;
  1625. nodeMap["STREQUAL"] = &strEqualNode;
  1626. nodeMap["EQUAL"] = &equalNode;
  1627. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1628. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1629. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1630. nodeMap["BOOL"] = &boolNode;
  1631. nodeMap["IF"] = &ifNode;
  1632. nodeMap["ANGLE-R"] = &angle_rNode;
  1633. nodeMap["COMMA"] = &commaNode;
  1634. nodeMap["SEMICOLON"] = &semicolonNode;
  1635. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1636. nodeMap["TARGET_NAME"] = &targetNameNode;
  1637. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1638. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1639. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1640. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1641. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1642. nodeMap["JOIN"] = &joinNode;
  1643. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1644. nodeMap["COMPILE_LANGUAGE"] = &languageNode;
  1645. nodeMap["SHELL_PATH"] = &shellPathNode;
  1646. }
  1647. NodeMap::const_iterator i = nodeMap.find(identifier);
  1648. if (i == nodeMap.end()) {
  1649. return CM_NULLPTR;
  1650. }
  1651. return i->second;
  1652. }
  1653. void reportError(cmGeneratorExpressionContext* context,
  1654. const std::string& expr, const std::string& result)
  1655. {
  1656. context->HadError = true;
  1657. if (context->Quiet) {
  1658. return;
  1659. }
  1660. std::ostringstream e;
  1661. /* clang-format off */
  1662. e << "Error evaluating generator expression:\n"
  1663. << " " << expr << "\n"
  1664. << result;
  1665. /* clang-format on */
  1666. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1667. context->Backtrace);
  1668. }