cmGeneratorExpressionNode.cxx 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2012 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorExpressionNode.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmOutputConverter.h"
  15. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  16. std::string const& prop, cmLocalGenerator* lg,
  17. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  18. cmGeneratorTarget const* currentTarget,
  19. cmGeneratorExpressionDAGChecker* dagChecker)
  20. {
  21. cmGeneratorExpression ge(context->Backtrace);
  22. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  23. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  24. std::string result =
  25. cge->Evaluate(lg, context->Config, context->Quiet, headTarget,
  26. currentTarget, dagChecker, context->Language);
  27. if (cge->GetHadContextSensitiveCondition()) {
  28. context->HadContextSensitiveCondition = true;
  29. }
  30. if (cge->GetHadHeadSensitiveCondition()) {
  31. context->HadHeadSensitiveCondition = true;
  32. }
  33. return result;
  34. }
  35. static const struct ZeroNode : public cmGeneratorExpressionNode
  36. {
  37. ZeroNode() {}
  38. bool GeneratesContent() const CM_OVERRIDE { return false; }
  39. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  40. std::string Evaluate(const std::vector<std::string>&,
  41. cmGeneratorExpressionContext*,
  42. const GeneratorExpressionContent*,
  43. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  44. {
  45. return std::string();
  46. }
  47. } zeroNode;
  48. static const struct OneNode : public cmGeneratorExpressionNode
  49. {
  50. OneNode() {}
  51. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  52. std::string Evaluate(const std::vector<std::string>& parameters,
  53. cmGeneratorExpressionContext*,
  54. const GeneratorExpressionContent*,
  55. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  56. {
  57. return parameters.front();
  58. }
  59. } oneNode;
  60. static const struct OneNode buildInterfaceNode;
  61. static const struct ZeroNode installInterfaceNode;
  62. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  63. static const struct OP##Node : public cmGeneratorExpressionNode \
  64. { \
  65. OP##Node() {} \
  66. virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
  67. \
  68. std::string Evaluate(const std::vector<std::string>& parameters, \
  69. cmGeneratorExpressionContext* context, \
  70. const GeneratorExpressionContent* content, \
  71. cmGeneratorExpressionDAGChecker*) const \
  72. { \
  73. std::vector<std::string>::const_iterator it = parameters.begin(); \
  74. const std::vector<std::string>::const_iterator end = parameters.end(); \
  75. for (; it != end; ++it) { \
  76. if (*it == #FAILURE_VALUE) { \
  77. return #FAILURE_VALUE; \
  78. } else if (*it != #SUCCESS_VALUE) { \
  79. reportError(context, content->GetOriginalExpression(), \
  80. "Parameters to $<" #OP \
  81. "> must resolve to either '0' or '1'."); \
  82. return std::string(); \
  83. } \
  84. } \
  85. return #SUCCESS_VALUE; \
  86. } \
  87. } OPNAME;
  88. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  89. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  90. #undef BOOLEAN_OP_NODE
  91. static const struct NotNode : public cmGeneratorExpressionNode
  92. {
  93. NotNode() {}
  94. std::string Evaluate(const std::vector<std::string>& parameters,
  95. cmGeneratorExpressionContext* context,
  96. const GeneratorExpressionContent* content,
  97. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  98. {
  99. if (*parameters.begin() != "0" && *parameters.begin() != "1") {
  100. reportError(
  101. context, content->GetOriginalExpression(),
  102. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  103. return std::string();
  104. }
  105. return *parameters.begin() == "0" ? "1" : "0";
  106. }
  107. } notNode;
  108. static const struct BoolNode : public cmGeneratorExpressionNode
  109. {
  110. BoolNode() {}
  111. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  112. std::string Evaluate(const std::vector<std::string>& parameters,
  113. cmGeneratorExpressionContext*,
  114. const GeneratorExpressionContent*,
  115. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  116. {
  117. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  118. }
  119. } boolNode;
  120. static const struct StrEqualNode : public cmGeneratorExpressionNode
  121. {
  122. StrEqualNode() {}
  123. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  124. std::string Evaluate(const std::vector<std::string>& parameters,
  125. cmGeneratorExpressionContext*,
  126. const GeneratorExpressionContent*,
  127. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  128. {
  129. return *parameters.begin() == parameters[1] ? "1" : "0";
  130. }
  131. } strEqualNode;
  132. static const struct EqualNode : public cmGeneratorExpressionNode
  133. {
  134. EqualNode() {}
  135. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  136. std::string Evaluate(const std::vector<std::string>& parameters,
  137. cmGeneratorExpressionContext* context,
  138. const GeneratorExpressionContent* content,
  139. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  140. {
  141. char* pEnd;
  142. int base = 0;
  143. bool flipSign = false;
  144. const char* lhs = parameters[0].c_str();
  145. if (cmHasLiteralPrefix(lhs, "0b") || cmHasLiteralPrefix(lhs, "0B")) {
  146. base = 2;
  147. lhs += 2;
  148. }
  149. if (cmHasLiteralPrefix(lhs, "-0b") || cmHasLiteralPrefix(lhs, "-0B")) {
  150. base = 2;
  151. lhs += 3;
  152. flipSign = true;
  153. }
  154. if (cmHasLiteralPrefix(lhs, "+0b") || cmHasLiteralPrefix(lhs, "+0B")) {
  155. base = 2;
  156. lhs += 3;
  157. }
  158. long lnum = strtol(lhs, &pEnd, base);
  159. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  160. reportError(context, content->GetOriginalExpression(),
  161. "$<EQUAL> parameter " + parameters[0] +
  162. " is not a valid integer.");
  163. return std::string();
  164. }
  165. if (flipSign) {
  166. lnum = -lnum;
  167. }
  168. base = 0;
  169. flipSign = false;
  170. const char* rhs = parameters[1].c_str();
  171. if (cmHasLiteralPrefix(rhs, "0b") || cmHasLiteralPrefix(rhs, "0B")) {
  172. base = 2;
  173. rhs += 2;
  174. }
  175. if (cmHasLiteralPrefix(rhs, "-0b") || cmHasLiteralPrefix(rhs, "-0B")) {
  176. base = 2;
  177. rhs += 3;
  178. flipSign = true;
  179. }
  180. if (cmHasLiteralPrefix(rhs, "+0b") || cmHasLiteralPrefix(rhs, "+0B")) {
  181. base = 2;
  182. rhs += 3;
  183. }
  184. long rnum = strtol(rhs, &pEnd, base);
  185. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  186. reportError(context, content->GetOriginalExpression(),
  187. "$<EQUAL> parameter " + parameters[1] +
  188. " is not a valid integer.");
  189. return std::string();
  190. }
  191. if (flipSign) {
  192. rnum = -rnum;
  193. }
  194. return lnum == rnum ? "1" : "0";
  195. }
  196. } equalNode;
  197. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  198. {
  199. LowerCaseNode() {}
  200. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  201. std::string Evaluate(const std::vector<std::string>& parameters,
  202. cmGeneratorExpressionContext*,
  203. const GeneratorExpressionContent*,
  204. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  205. {
  206. return cmSystemTools::LowerCase(parameters.front());
  207. }
  208. } lowerCaseNode;
  209. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  210. {
  211. UpperCaseNode() {}
  212. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  213. std::string Evaluate(const std::vector<std::string>& parameters,
  214. cmGeneratorExpressionContext*,
  215. const GeneratorExpressionContent*,
  216. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  217. {
  218. return cmSystemTools::UpperCase(parameters.front());
  219. }
  220. } upperCaseNode;
  221. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  222. {
  223. MakeCIdentifierNode() {}
  224. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  225. std::string Evaluate(const std::vector<std::string>& parameters,
  226. cmGeneratorExpressionContext*,
  227. const GeneratorExpressionContent*,
  228. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  229. {
  230. return cmSystemTools::MakeCidentifier(parameters.front());
  231. }
  232. } makeCIdentifierNode;
  233. static const struct Angle_RNode : public cmGeneratorExpressionNode
  234. {
  235. Angle_RNode() {}
  236. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  237. std::string Evaluate(const std::vector<std::string>&,
  238. cmGeneratorExpressionContext*,
  239. const GeneratorExpressionContent*,
  240. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  241. {
  242. return ">";
  243. }
  244. } angle_rNode;
  245. static const struct CommaNode : public cmGeneratorExpressionNode
  246. {
  247. CommaNode() {}
  248. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  249. std::string Evaluate(const std::vector<std::string>&,
  250. cmGeneratorExpressionContext*,
  251. const GeneratorExpressionContent*,
  252. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  253. {
  254. return ",";
  255. }
  256. } commaNode;
  257. static const struct SemicolonNode : public cmGeneratorExpressionNode
  258. {
  259. SemicolonNode() {}
  260. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  261. std::string Evaluate(const std::vector<std::string>&,
  262. cmGeneratorExpressionContext*,
  263. const GeneratorExpressionContent*,
  264. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  265. {
  266. return ";";
  267. }
  268. } semicolonNode;
  269. struct CompilerIdNode : public cmGeneratorExpressionNode
  270. {
  271. CompilerIdNode() {}
  272. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  273. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  274. cmGeneratorExpressionContext* context,
  275. const GeneratorExpressionContent* content,
  276. cmGeneratorExpressionDAGChecker*,
  277. const std::string& lang) const
  278. {
  279. const char* compilerId = context->LG->GetMakefile()->GetSafeDefinition(
  280. "CMAKE_" + lang + "_COMPILER_ID");
  281. if (parameters.empty()) {
  282. return compilerId ? compilerId : "";
  283. }
  284. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  285. if (!compilerIdValidator.find(*parameters.begin())) {
  286. reportError(context, content->GetOriginalExpression(),
  287. "Expression syntax not recognized.");
  288. return std::string();
  289. }
  290. if (!compilerId) {
  291. return parameters.front().empty() ? "1" : "0";
  292. }
  293. if (strcmp(parameters.begin()->c_str(), compilerId) == 0) {
  294. return "1";
  295. }
  296. if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0) {
  297. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  298. case cmPolicies::WARN: {
  299. std::ostringstream e;
  300. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
  301. context->LG->GetCMakeInstance()->IssueMessage(
  302. cmake::AUTHOR_WARNING, e.str(), context->Backtrace);
  303. }
  304. case cmPolicies::OLD:
  305. return "1";
  306. case cmPolicies::NEW:
  307. case cmPolicies::REQUIRED_ALWAYS:
  308. case cmPolicies::REQUIRED_IF_USED:
  309. break;
  310. }
  311. }
  312. return "0";
  313. }
  314. };
  315. static const struct CCompilerIdNode : public CompilerIdNode
  316. {
  317. CCompilerIdNode() {}
  318. std::string Evaluate(const std::vector<std::string>& parameters,
  319. cmGeneratorExpressionContext* context,
  320. const GeneratorExpressionContent* content,
  321. cmGeneratorExpressionDAGChecker* dagChecker) const
  322. CM_OVERRIDE
  323. {
  324. if (!context->HeadTarget) {
  325. reportError(
  326. context, content->GetOriginalExpression(),
  327. "$<C_COMPILER_ID> may only be used with binary targets. It may "
  328. "not be used with add_custom_command or add_custom_target.");
  329. return std::string();
  330. }
  331. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  332. "C");
  333. }
  334. } cCompilerIdNode;
  335. static const struct CXXCompilerIdNode : public CompilerIdNode
  336. {
  337. CXXCompilerIdNode() {}
  338. std::string Evaluate(const std::vector<std::string>& parameters,
  339. cmGeneratorExpressionContext* context,
  340. const GeneratorExpressionContent* content,
  341. cmGeneratorExpressionDAGChecker* dagChecker) const
  342. CM_OVERRIDE
  343. {
  344. if (!context->HeadTarget) {
  345. reportError(
  346. context, content->GetOriginalExpression(),
  347. "$<CXX_COMPILER_ID> may only be used with binary targets. It may "
  348. "not be used with add_custom_command or add_custom_target.");
  349. return std::string();
  350. }
  351. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  352. "CXX");
  353. }
  354. } cxxCompilerIdNode;
  355. struct CompilerVersionNode : public cmGeneratorExpressionNode
  356. {
  357. CompilerVersionNode() {}
  358. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  359. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  360. cmGeneratorExpressionContext* context,
  361. const GeneratorExpressionContent* content,
  362. cmGeneratorExpressionDAGChecker*,
  363. const std::string& lang) const
  364. {
  365. const char* compilerVersion =
  366. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  367. "_COMPILER_VERSION");
  368. if (parameters.empty()) {
  369. return compilerVersion ? compilerVersion : "";
  370. }
  371. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  372. if (!compilerIdValidator.find(*parameters.begin())) {
  373. reportError(context, content->GetOriginalExpression(),
  374. "Expression syntax not recognized.");
  375. return std::string();
  376. }
  377. if (!compilerVersion) {
  378. return parameters.front().empty() ? "1" : "0";
  379. }
  380. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  381. parameters.begin()->c_str(),
  382. compilerVersion)
  383. ? "1"
  384. : "0";
  385. }
  386. };
  387. static const struct CCompilerVersionNode : public CompilerVersionNode
  388. {
  389. CCompilerVersionNode() {}
  390. std::string Evaluate(const std::vector<std::string>& parameters,
  391. cmGeneratorExpressionContext* context,
  392. const GeneratorExpressionContent* content,
  393. cmGeneratorExpressionDAGChecker* dagChecker) const
  394. CM_OVERRIDE
  395. {
  396. if (!context->HeadTarget) {
  397. reportError(
  398. context, content->GetOriginalExpression(),
  399. "$<C_COMPILER_VERSION> may only be used with binary targets. It "
  400. "may not be used with add_custom_command or add_custom_target.");
  401. return std::string();
  402. }
  403. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  404. "C");
  405. }
  406. } cCompilerVersionNode;
  407. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  408. {
  409. CxxCompilerVersionNode() {}
  410. std::string Evaluate(const std::vector<std::string>& parameters,
  411. cmGeneratorExpressionContext* context,
  412. const GeneratorExpressionContent* content,
  413. cmGeneratorExpressionDAGChecker* dagChecker) const
  414. CM_OVERRIDE
  415. {
  416. if (!context->HeadTarget) {
  417. reportError(
  418. context, content->GetOriginalExpression(),
  419. "$<CXX_COMPILER_VERSION> may only be used with binary targets. It "
  420. "may not be used with add_custom_command or add_custom_target.");
  421. return std::string();
  422. }
  423. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  424. "CXX");
  425. }
  426. } cxxCompilerVersionNode;
  427. struct PlatformIdNode : public cmGeneratorExpressionNode
  428. {
  429. PlatformIdNode() {}
  430. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  431. std::string Evaluate(const std::vector<std::string>& parameters,
  432. cmGeneratorExpressionContext* context,
  433. const GeneratorExpressionContent*,
  434. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  435. {
  436. const char* platformId =
  437. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  438. if (parameters.empty()) {
  439. return platformId ? platformId : "";
  440. }
  441. if (!platformId) {
  442. return parameters.front().empty() ? "1" : "0";
  443. }
  444. if (strcmp(parameters.begin()->c_str(), platformId) == 0) {
  445. return "1";
  446. }
  447. return "0";
  448. }
  449. } platformIdNode;
  450. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  451. {
  452. VersionGreaterNode() {}
  453. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  454. std::string Evaluate(const std::vector<std::string>& parameters,
  455. cmGeneratorExpressionContext*,
  456. const GeneratorExpressionContent*,
  457. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  458. {
  459. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  460. parameters.front().c_str(),
  461. parameters[1].c_str())
  462. ? "1"
  463. : "0";
  464. }
  465. } versionGreaterNode;
  466. static const struct VersionLessNode : public cmGeneratorExpressionNode
  467. {
  468. VersionLessNode() {}
  469. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  470. std::string Evaluate(const std::vector<std::string>& parameters,
  471. cmGeneratorExpressionContext*,
  472. const GeneratorExpressionContent*,
  473. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  474. {
  475. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  476. parameters.front().c_str(),
  477. parameters[1].c_str())
  478. ? "1"
  479. : "0";
  480. }
  481. } versionLessNode;
  482. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  483. {
  484. VersionEqualNode() {}
  485. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  486. std::string Evaluate(const std::vector<std::string>& parameters,
  487. cmGeneratorExpressionContext*,
  488. const GeneratorExpressionContent*,
  489. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  490. {
  491. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  492. parameters.front().c_str(),
  493. parameters[1].c_str())
  494. ? "1"
  495. : "0";
  496. }
  497. } versionEqualNode;
  498. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  499. {
  500. LinkOnlyNode() {}
  501. std::string Evaluate(
  502. const std::vector<std::string>& parameters, cmGeneratorExpressionContext*,
  503. const GeneratorExpressionContent*,
  504. cmGeneratorExpressionDAGChecker* dagChecker) const CM_OVERRIDE
  505. {
  506. if (!dagChecker->GetTransitivePropertiesOnly()) {
  507. return parameters.front();
  508. }
  509. return "";
  510. }
  511. } linkOnlyNode;
  512. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  513. {
  514. ConfigurationNode() {}
  515. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  516. std::string Evaluate(const std::vector<std::string>&,
  517. cmGeneratorExpressionContext* context,
  518. const GeneratorExpressionContent*,
  519. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  520. {
  521. context->HadContextSensitiveCondition = true;
  522. return context->Config;
  523. }
  524. } configurationNode;
  525. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  526. {
  527. ConfigurationTestNode() {}
  528. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  529. std::string Evaluate(const std::vector<std::string>& parameters,
  530. cmGeneratorExpressionContext* context,
  531. const GeneratorExpressionContent* content,
  532. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  533. {
  534. if (parameters.empty()) {
  535. return configurationNode.Evaluate(parameters, context, content,
  536. CM_NULLPTR);
  537. }
  538. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  539. if (!configValidator.find(*parameters.begin())) {
  540. reportError(context, content->GetOriginalExpression(),
  541. "Expression syntax not recognized.");
  542. return std::string();
  543. }
  544. context->HadContextSensitiveCondition = true;
  545. if (context->Config.empty()) {
  546. return parameters.front().empty() ? "1" : "0";
  547. }
  548. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  549. context->Config.c_str()) == 0) {
  550. return "1";
  551. }
  552. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  553. const char* loc = CM_NULLPTR;
  554. const char* imp = CM_NULLPTR;
  555. std::string suffix;
  556. if (context->CurrentTarget->Target->GetMappedConfig(
  557. context->Config, &loc, &imp, suffix)) {
  558. // This imported target has an appropriate location
  559. // for this (possibly mapped) config.
  560. // Check if there is a proper config mapping for the tested config.
  561. std::vector<std::string> mappedConfigs;
  562. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  563. mapProp += cmSystemTools::UpperCase(context->Config);
  564. if (const char* mapValue =
  565. context->CurrentTarget->GetProperty(mapProp)) {
  566. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  567. mappedConfigs);
  568. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  569. cmSystemTools::UpperCase(parameters.front())) !=
  570. mappedConfigs.end()
  571. ? "1"
  572. : "0";
  573. }
  574. }
  575. }
  576. return "0";
  577. }
  578. } configurationTestNode;
  579. static const struct JoinNode : public cmGeneratorExpressionNode
  580. {
  581. JoinNode() {}
  582. int NumExpectedParameters() const CM_OVERRIDE { return 2; }
  583. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  584. std::string Evaluate(const std::vector<std::string>& parameters,
  585. cmGeneratorExpressionContext*,
  586. const GeneratorExpressionContent*,
  587. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  588. {
  589. std::vector<std::string> list;
  590. cmSystemTools::ExpandListArgument(parameters.front(), list);
  591. return cmJoin(list, parameters[1]);
  592. }
  593. } joinNode;
  594. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  595. {
  596. CompileLanguageNode() {}
  597. int NumExpectedParameters() const CM_OVERRIDE { return OneOrZeroParameters; }
  598. std::string Evaluate(const std::vector<std::string>& parameters,
  599. cmGeneratorExpressionContext* context,
  600. const GeneratorExpressionContent* content,
  601. cmGeneratorExpressionDAGChecker* dagChecker) const
  602. CM_OVERRIDE
  603. {
  604. if (context->Language.empty()) {
  605. reportError(
  606. context, content->GetOriginalExpression(),
  607. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  608. "directories compile definitions, compile options and to evaluate "
  609. "components of the file(GENERATE) command.");
  610. return std::string();
  611. }
  612. std::vector<std::string> enabledLanguages;
  613. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  614. gg->GetEnabledLanguages(enabledLanguages);
  615. if (!parameters.empty() &&
  616. std::find(enabledLanguages.begin(), enabledLanguages.end(),
  617. parameters.front()) == enabledLanguages.end()) {
  618. reportError(context, content->GetOriginalExpression(),
  619. "$<COMPILE_LANGUAGE:...> Unknown language.");
  620. return std::string();
  621. }
  622. std::string genName = gg->GetName();
  623. if (genName.find("Visual Studio") != std::string::npos) {
  624. reportError(context, content->GetOriginalExpression(),
  625. "$<COMPILE_LANGUAGE:...> may not be used with Visual Studio "
  626. "generators.");
  627. return std::string();
  628. } else if (genName.find("Xcode") != std::string::npos) {
  629. if (dagChecker && (dagChecker->EvaluatingCompileDefinitions() ||
  630. dagChecker->EvaluatingIncludeDirectories())) {
  631. reportError(
  632. context, content->GetOriginalExpression(),
  633. "$<COMPILE_LANGUAGE:...> may only be used with COMPILE_OPTIONS "
  634. "with the Xcode generator.");
  635. return std::string();
  636. }
  637. } else {
  638. if (genName.find("Makefiles") == std::string::npos &&
  639. genName.find("Ninja") == std::string::npos &&
  640. genName.find("Watcom WMake") == std::string::npos) {
  641. reportError(
  642. context, content->GetOriginalExpression(),
  643. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  644. return std::string();
  645. }
  646. }
  647. if (parameters.empty()) {
  648. return context->Language;
  649. }
  650. return context->Language == parameters.front() ? "1" : "0";
  651. }
  652. } languageNode;
  653. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) , "INTERFACE_" #PROPERTY
  654. static const char* targetPropertyTransitiveWhitelist[] = {
  655. CM_NULLPTR CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  656. };
  657. #undef TRANSITIVE_PROPERTY_NAME
  658. template <typename T>
  659. std::string getLinkedTargetsContent(
  660. std::vector<T> const& libraries, cmGeneratorTarget const* target,
  661. cmGeneratorTarget const* headTarget, cmGeneratorExpressionContext* context,
  662. cmGeneratorExpressionDAGChecker* dagChecker,
  663. const std::string& interfacePropertyName)
  664. {
  665. std::string linkedTargetsContent;
  666. std::string sep;
  667. std::string depString;
  668. for (typename std::vector<T>::const_iterator it = libraries.begin();
  669. it != libraries.end(); ++it) {
  670. // Broken code can have a target in its own link interface.
  671. // Don't follow such link interface entries so as not to create a
  672. // self-referencing loop.
  673. if (it->Target && it->Target != target) {
  674. depString += sep + "$<TARGET_PROPERTY:" + it->Target->GetName() + "," +
  675. interfacePropertyName + ">";
  676. sep = ";";
  677. }
  678. }
  679. if (!depString.empty()) {
  680. linkedTargetsContent =
  681. cmGeneratorExpressionNode::EvaluateDependentExpression(
  682. depString, target->GetLocalGenerator(), context, headTarget, target,
  683. dagChecker);
  684. }
  685. linkedTargetsContent =
  686. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  687. return linkedTargetsContent;
  688. }
  689. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  690. {
  691. TargetPropertyNode() {}
  692. // This node handles errors on parameter count itself.
  693. int NumExpectedParameters() const CM_OVERRIDE { return OneOrMoreParameters; }
  694. std::string Evaluate(const std::vector<std::string>& parameters,
  695. cmGeneratorExpressionContext* context,
  696. const GeneratorExpressionContent* content,
  697. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  698. CM_OVERRIDE
  699. {
  700. if (parameters.size() != 1 && parameters.size() != 2) {
  701. reportError(
  702. context, content->GetOriginalExpression(),
  703. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  704. return std::string();
  705. }
  706. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  707. cmGeneratorTarget const* target = context->HeadTarget;
  708. std::string propertyName = *parameters.begin();
  709. if (parameters.size() == 1) {
  710. context->HadHeadSensitiveCondition = true;
  711. }
  712. if (!target && parameters.size() == 1) {
  713. reportError(
  714. context, content->GetOriginalExpression(),
  715. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  716. "It may not be used with add_custom_command or add_custom_target. "
  717. "Specify the target to read a property from using the "
  718. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  719. return std::string();
  720. }
  721. if (parameters.size() == 2) {
  722. if (parameters.begin()->empty() && parameters[1].empty()) {
  723. reportError(
  724. context, content->GetOriginalExpression(),
  725. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  726. "target name and property name.");
  727. return std::string();
  728. }
  729. if (parameters.begin()->empty()) {
  730. reportError(
  731. context, content->GetOriginalExpression(),
  732. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  733. "target name.");
  734. return std::string();
  735. }
  736. std::string targetName = parameters.front();
  737. propertyName = parameters[1];
  738. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  739. if (!propertyNameValidator.find(propertyName.c_str())) {
  740. ::reportError(context, content->GetOriginalExpression(),
  741. "Target name and property name not supported.");
  742. return std::string();
  743. }
  744. ::reportError(context, content->GetOriginalExpression(),
  745. "Target name not supported.");
  746. return std::string();
  747. }
  748. if (propertyName == "ALIASED_TARGET") {
  749. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  750. if (cmGeneratorTarget* tgt =
  751. context->LG->FindGeneratorTargetToUse(targetName)) {
  752. return tgt->GetName();
  753. }
  754. }
  755. return "";
  756. }
  757. target = context->LG->FindGeneratorTargetToUse(targetName);
  758. if (!target) {
  759. std::ostringstream e;
  760. e << "Target \"" << targetName << "\" not found.";
  761. reportError(context, content->GetOriginalExpression(), e.str());
  762. return std::string();
  763. }
  764. context->AllTargets.insert(target);
  765. }
  766. if (target == context->HeadTarget) {
  767. // Keep track of the properties seen while processing.
  768. // The evaluation of the LINK_LIBRARIES generator expressions
  769. // will check this to ensure that properties have one consistent
  770. // value for all evaluations.
  771. context->SeenTargetProperties.insert(propertyName);
  772. }
  773. if (propertyName == "SOURCES") {
  774. context->SourceSensitiveTargets.insert(target);
  775. }
  776. if (propertyName.empty()) {
  777. reportError(
  778. context, content->GetOriginalExpression(),
  779. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  780. "name.");
  781. return std::string();
  782. }
  783. if (!propertyNameValidator.find(propertyName)) {
  784. ::reportError(context, content->GetOriginalExpression(),
  785. "Property name not supported.");
  786. return std::string();
  787. }
  788. assert(target);
  789. if (propertyName == "LINKER_LANGUAGE") {
  790. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  791. (dagCheckerParent->EvaluatingLinkLibraries() ||
  792. dagCheckerParent->EvaluatingSources())) {
  793. reportError(
  794. context, content->GetOriginalExpression(),
  795. "LINKER_LANGUAGE target property can not be used while evaluating "
  796. "link libraries for a static library");
  797. return std::string();
  798. }
  799. return target->GetLinkerLanguage(context->Config);
  800. }
  801. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  802. target->GetName(), propertyName,
  803. content, dagCheckerParent);
  804. switch (dagChecker.Check()) {
  805. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  806. dagChecker.ReportError(context, content->GetOriginalExpression());
  807. return std::string();
  808. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  809. // No error. We just skip cyclic references.
  810. return std::string();
  811. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  812. for (size_t i = 1; i < cmArraySize(targetPropertyTransitiveWhitelist);
  813. ++i) {
  814. if (targetPropertyTransitiveWhitelist[i] == propertyName) {
  815. // No error. We're not going to find anything new here.
  816. return std::string();
  817. }
  818. }
  819. case cmGeneratorExpressionDAGChecker::DAG:
  820. break;
  821. }
  822. const char* prop = target->GetProperty(propertyName);
  823. if (dagCheckerParent) {
  824. if (dagCheckerParent->EvaluatingLinkLibraries()) {
  825. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  826. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  827. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(
  828. TRANSITIVE_PROPERTY_COMPARE) false) {
  829. reportError(
  830. context, content->GetOriginalExpression(),
  831. "$<TARGET_PROPERTY:...> expression in link libraries "
  832. "evaluation depends on target property which is transitive "
  833. "over the link libraries, creating a recursion.");
  834. return std::string();
  835. }
  836. #undef TRANSITIVE_PROPERTY_COMPARE
  837. if (!prop) {
  838. return std::string();
  839. }
  840. } else {
  841. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  842. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  843. ASSERT_TRANSITIVE_PROPERTY_METHOD) false);
  844. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  845. }
  846. }
  847. std::string linkedTargetsContent;
  848. std::string interfacePropertyName;
  849. bool isInterfaceProperty = false;
  850. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  851. if (propertyName == #prop) { \
  852. interfacePropertyName = "INTERFACE_" #prop; \
  853. } else if (propertyName == "INTERFACE_" #prop) { \
  854. interfacePropertyName = "INTERFACE_" #prop; \
  855. isInterfaceProperty = true; \
  856. } else
  857. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  858. // Note that the above macro terminates with an else
  859. /* else */ if (cmHasLiteralPrefix(propertyName.c_str(),
  860. "COMPILE_DEFINITIONS_")) {
  861. cmPolicies::PolicyStatus polSt =
  862. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  863. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  864. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  865. }
  866. }
  867. #undef POPULATE_INTERFACE_PROPERTY_NAME
  868. cmGeneratorTarget const* headTarget =
  869. context->HeadTarget && isInterfaceProperty ? context->HeadTarget
  870. : target;
  871. if (isInterfaceProperty) {
  872. if (cmLinkInterfaceLibraries const* iface =
  873. target->GetLinkInterfaceLibraries(context->Config, headTarget,
  874. true)) {
  875. linkedTargetsContent =
  876. getLinkedTargetsContent(iface->Libraries, target, headTarget,
  877. context, &dagChecker, interfacePropertyName);
  878. }
  879. } else if (!interfacePropertyName.empty()) {
  880. if (cmLinkImplementationLibraries const* impl =
  881. target->GetLinkImplementationLibraries(context->Config)) {
  882. linkedTargetsContent =
  883. getLinkedTargetsContent(impl->Libraries, target, target, context,
  884. &dagChecker, interfacePropertyName);
  885. }
  886. }
  887. if (!prop) {
  888. if (target->IsImported() ||
  889. target->GetType() == cmState::INTERFACE_LIBRARY) {
  890. return linkedTargetsContent;
  891. }
  892. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  893. context->Config)) {
  894. context->HadContextSensitiveCondition = true;
  895. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  896. context->Config)
  897. ? "1"
  898. : "0";
  899. }
  900. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  901. context->Config)) {
  902. context->HadContextSensitiveCondition = true;
  903. const char* propContent =
  904. target->GetLinkInterfaceDependentStringProperty(propertyName,
  905. context->Config);
  906. return propContent ? propContent : "";
  907. }
  908. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  909. context->Config)) {
  910. context->HadContextSensitiveCondition = true;
  911. const char* propContent =
  912. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  913. context->Config);
  914. return propContent ? propContent : "";
  915. }
  916. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  917. context->Config)) {
  918. context->HadContextSensitiveCondition = true;
  919. const char* propContent =
  920. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  921. context->Config);
  922. return propContent ? propContent : "";
  923. }
  924. return linkedTargetsContent;
  925. }
  926. if (!target->IsImported() && dagCheckerParent &&
  927. !dagCheckerParent->EvaluatingLinkLibraries()) {
  928. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  929. context->Config)) {
  930. context->HadContextSensitiveCondition = true;
  931. const char* propContent =
  932. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  933. context->Config);
  934. return propContent ? propContent : "";
  935. }
  936. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  937. context->Config)) {
  938. context->HadContextSensitiveCondition = true;
  939. const char* propContent =
  940. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  941. context->Config);
  942. return propContent ? propContent : "";
  943. }
  944. }
  945. if (!interfacePropertyName.empty()) {
  946. std::string result = this->EvaluateDependentExpression(
  947. prop, context->LG, context, headTarget, target, &dagChecker);
  948. if (!linkedTargetsContent.empty()) {
  949. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  950. }
  951. return result;
  952. }
  953. return prop;
  954. }
  955. } targetPropertyNode;
  956. static const struct TargetNameNode : public cmGeneratorExpressionNode
  957. {
  958. TargetNameNode() {}
  959. bool GeneratesContent() const CM_OVERRIDE { return true; }
  960. bool AcceptsArbitraryContentParameter() const CM_OVERRIDE { return true; }
  961. bool RequiresLiteralInput() const CM_OVERRIDE { return true; }
  962. std::string Evaluate(const std::vector<std::string>& parameters,
  963. cmGeneratorExpressionContext*,
  964. const GeneratorExpressionContent*,
  965. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  966. {
  967. return parameters.front();
  968. }
  969. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  970. } targetNameNode;
  971. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  972. {
  973. TargetObjectsNode() {}
  974. std::string Evaluate(const std::vector<std::string>& parameters,
  975. cmGeneratorExpressionContext* context,
  976. const GeneratorExpressionContent* content,
  977. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  978. {
  979. if (!context->EvaluateForBuildsystem) {
  980. std::ostringstream e;
  981. e << "The evaluation of the TARGET_OBJECTS generator expression "
  982. "is only suitable for consumption by CMake. It is not suitable "
  983. "for writing out elsewhere.";
  984. reportError(context, content->GetOriginalExpression(), e.str());
  985. return std::string();
  986. }
  987. std::string tgtName = parameters.front();
  988. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  989. if (!gt) {
  990. std::ostringstream e;
  991. e << "Objects of target \"" << tgtName
  992. << "\" referenced but no such target exists.";
  993. reportError(context, content->GetOriginalExpression(), e.str());
  994. return std::string();
  995. }
  996. if (gt->GetType() != cmState::OBJECT_LIBRARY) {
  997. std::ostringstream e;
  998. e << "Objects of target \"" << tgtName
  999. << "\" referenced but is not an OBJECT library.";
  1000. reportError(context, content->GetOriginalExpression(), e.str());
  1001. return std::string();
  1002. }
  1003. std::vector<cmSourceFile const*> objectSources;
  1004. gt->GetObjectSources(objectSources, context->Config);
  1005. std::map<cmSourceFile const*, std::string> mapping;
  1006. for (std::vector<cmSourceFile const*>::const_iterator it =
  1007. objectSources.begin();
  1008. it != objectSources.end(); ++it) {
  1009. mapping[*it];
  1010. }
  1011. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  1012. std::string obj_dir = gt->ObjectDirectory;
  1013. std::string result;
  1014. const char* sep = "";
  1015. for (std::vector<cmSourceFile const*>::const_iterator it =
  1016. objectSources.begin();
  1017. it != objectSources.end(); ++it) {
  1018. // Find the object file name corresponding to this source file.
  1019. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  1020. mapping.find(*it);
  1021. // It must exist because we populated the mapping just above.
  1022. assert(!map_it->second.empty());
  1023. result += sep;
  1024. std::string objFile = obj_dir + map_it->second;
  1025. cmSourceFile* sf =
  1026. context->LG->GetMakefile()->GetOrCreateSource(objFile, true);
  1027. sf->SetObjectLibrary(tgtName);
  1028. sf->SetProperty("EXTERNAL_OBJECT", "1");
  1029. result += objFile;
  1030. sep = ";";
  1031. }
  1032. return result;
  1033. }
  1034. } targetObjectsNode;
  1035. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1036. {
  1037. CompileFeaturesNode() {}
  1038. int NumExpectedParameters() const CM_OVERRIDE { return OneOrMoreParameters; }
  1039. std::string Evaluate(const std::vector<std::string>& parameters,
  1040. cmGeneratorExpressionContext* context,
  1041. const GeneratorExpressionContent* content,
  1042. cmGeneratorExpressionDAGChecker* dagChecker) const
  1043. CM_OVERRIDE
  1044. {
  1045. cmGeneratorTarget const* target = context->HeadTarget;
  1046. if (!target) {
  1047. reportError(
  1048. context, content->GetOriginalExpression(),
  1049. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1050. "not be used with add_custom_command or add_custom_target.");
  1051. return std::string();
  1052. }
  1053. context->HadHeadSensitiveCondition = true;
  1054. typedef std::map<std::string, std::vector<std::string> > LangMap;
  1055. static LangMap availableFeatures;
  1056. LangMap testedFeatures;
  1057. for (std::vector<std::string>::const_iterator it = parameters.begin();
  1058. it != parameters.end(); ++it) {
  1059. std::string error;
  1060. std::string lang;
  1061. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1062. context->HeadTarget->Target, *it, lang, &error)) {
  1063. reportError(context, content->GetOriginalExpression(), error);
  1064. return std::string();
  1065. }
  1066. testedFeatures[lang].push_back(*it);
  1067. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1068. const char* featuresKnown =
  1069. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1070. if (!featuresKnown) {
  1071. reportError(context, content->GetOriginalExpression(), error);
  1072. return std::string();
  1073. }
  1074. cmSystemTools::ExpandListArgument(featuresKnown,
  1075. availableFeatures[lang]);
  1076. }
  1077. }
  1078. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1079. for (LangMap::const_iterator lit = testedFeatures.begin();
  1080. lit != testedFeatures.end(); ++lit) {
  1081. std::vector<std::string> const& langAvailable =
  1082. availableFeatures[lit->first];
  1083. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1084. "CMAKE_" + lit->first + "_STANDARD_DEFAULT");
  1085. for (std::vector<std::string>::const_iterator it = lit->second.begin();
  1086. it != lit->second.end(); ++it) {
  1087. if (std::find(langAvailable.begin(), langAvailable.end(), *it) ==
  1088. langAvailable.end()) {
  1089. return "0";
  1090. }
  1091. if (standardDefault && !*standardDefault) {
  1092. // This compiler has no notion of language standard levels.
  1093. // All features known for the language are always available.
  1094. continue;
  1095. }
  1096. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1097. target->Target, lit->first, *it)) {
  1098. if (evalLL) {
  1099. const char* l = target->GetProperty(lit->first + "_STANDARD");
  1100. if (!l) {
  1101. l = standardDefault;
  1102. }
  1103. assert(l);
  1104. context->MaxLanguageStandard[target][lit->first] = l;
  1105. } else {
  1106. return "0";
  1107. }
  1108. }
  1109. }
  1110. }
  1111. return "1";
  1112. }
  1113. } compileFeaturesNode;
  1114. static const char* targetPolicyWhitelist[] = {
  1115. CM_NULLPTR
  1116. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1117. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1118. #undef TARGET_POLICY_STRING
  1119. };
  1120. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1121. const char* policy)
  1122. {
  1123. #define RETURN_POLICY(POLICY) \
  1124. if (strcmp(policy, #POLICY) == 0) { \
  1125. return tgt->GetPolicyStatus##POLICY(); \
  1126. }
  1127. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1128. #undef RETURN_POLICY
  1129. assert(0 && "Unreachable code. Not a valid policy");
  1130. return cmPolicies::WARN;
  1131. }
  1132. cmPolicies::PolicyID policyForString(const char* policy_id)
  1133. {
  1134. #define RETURN_POLICY_ID(POLICY_ID) \
  1135. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1136. return cmPolicies::POLICY_ID; \
  1137. }
  1138. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1139. #undef RETURN_POLICY_ID
  1140. assert(0 && "Unreachable code. Not a valid policy");
  1141. return cmPolicies::CMP0002;
  1142. }
  1143. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1144. {
  1145. TargetPolicyNode() {}
  1146. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  1147. std::string Evaluate(const std::vector<std::string>& parameters,
  1148. cmGeneratorExpressionContext* context,
  1149. const GeneratorExpressionContent* content,
  1150. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  1151. {
  1152. if (!context->HeadTarget) {
  1153. reportError(
  1154. context, content->GetOriginalExpression(),
  1155. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1156. "may not be used with add_custom_command or add_custom_target.");
  1157. return std::string();
  1158. }
  1159. context->HadContextSensitiveCondition = true;
  1160. context->HadHeadSensitiveCondition = true;
  1161. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i) {
  1162. const char* policy = targetPolicyWhitelist[i];
  1163. if (parameters.front() == policy) {
  1164. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1165. switch (statusForTarget(context->HeadTarget, policy)) {
  1166. case cmPolicies::WARN:
  1167. lg->IssueMessage(
  1168. cmake::AUTHOR_WARNING,
  1169. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1170. case cmPolicies::REQUIRED_IF_USED:
  1171. case cmPolicies::REQUIRED_ALWAYS:
  1172. case cmPolicies::OLD:
  1173. return "0";
  1174. case cmPolicies::NEW:
  1175. return "1";
  1176. }
  1177. }
  1178. }
  1179. reportError(
  1180. context, content->GetOriginalExpression(),
  1181. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1182. "policies. Currently it may be used with the following policies:\n"
  1183. #define STRINGIFY_HELPER(X) #X
  1184. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1185. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1186. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1187. #undef TARGET_POLICY_LIST_ITEM
  1188. );
  1189. return std::string();
  1190. }
  1191. } targetPolicyNode;
  1192. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1193. {
  1194. InstallPrefixNode() {}
  1195. bool GeneratesContent() const CM_OVERRIDE { return true; }
  1196. int NumExpectedParameters() const CM_OVERRIDE { return 0; }
  1197. std::string Evaluate(const std::vector<std::string>&,
  1198. cmGeneratorExpressionContext* context,
  1199. const GeneratorExpressionContent* content,
  1200. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  1201. {
  1202. reportError(context, content->GetOriginalExpression(),
  1203. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1204. "should never be evaluated.");
  1205. return std::string();
  1206. }
  1207. } installPrefixNode;
  1208. class ArtifactNameTag;
  1209. class ArtifactLinkerTag;
  1210. class ArtifactSonameTag;
  1211. class ArtifactPdbTag;
  1212. class ArtifactPathTag;
  1213. class ArtifactDirTag;
  1214. template <typename ArtifactT>
  1215. struct TargetFilesystemArtifactResultCreator
  1216. {
  1217. static std::string Create(cmGeneratorTarget* target,
  1218. cmGeneratorExpressionContext* context,
  1219. const GeneratorExpressionContent* content);
  1220. };
  1221. template <>
  1222. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1223. {
  1224. static std::string Create(cmGeneratorTarget* target,
  1225. cmGeneratorExpressionContext* context,
  1226. const GeneratorExpressionContent* content)
  1227. {
  1228. // The target soname file (.so.1).
  1229. if (target->IsDLLPlatform()) {
  1230. ::reportError(context, content->GetOriginalExpression(),
  1231. "TARGET_SONAME_FILE is not allowed "
  1232. "for DLL target platforms.");
  1233. return std::string();
  1234. }
  1235. if (target->GetType() != cmState::SHARED_LIBRARY) {
  1236. ::reportError(context, content->GetOriginalExpression(),
  1237. "TARGET_SONAME_FILE is allowed only for "
  1238. "SHARED libraries.");
  1239. return std::string();
  1240. }
  1241. std::string result = target->GetDirectory(context->Config);
  1242. result += "/";
  1243. result += target->GetSOName(context->Config);
  1244. return result;
  1245. }
  1246. };
  1247. template <>
  1248. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1249. {
  1250. static std::string Create(cmGeneratorTarget* target,
  1251. cmGeneratorExpressionContext* context,
  1252. const GeneratorExpressionContent* content)
  1253. {
  1254. if (target->IsImported()) {
  1255. ::reportError(context, content->GetOriginalExpression(),
  1256. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1257. return std::string();
  1258. }
  1259. std::string language = target->GetLinkerLanguage(context->Config);
  1260. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1261. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1262. ::reportError(context, content->GetOriginalExpression(),
  1263. "TARGET_PDB_FILE is not supported by the target linker.");
  1264. return std::string();
  1265. }
  1266. cmState::TargetType targetType = target->GetType();
  1267. if (targetType != cmState::SHARED_LIBRARY &&
  1268. targetType != cmState::MODULE_LIBRARY &&
  1269. targetType != cmState::EXECUTABLE) {
  1270. ::reportError(context, content->GetOriginalExpression(),
  1271. "TARGET_PDB_FILE is allowed only for "
  1272. "targets with linker created artifacts.");
  1273. return std::string();
  1274. }
  1275. std::string result = target->GetPDBDirectory(context->Config);
  1276. result += "/";
  1277. result += target->GetPDBName(context->Config);
  1278. return result;
  1279. }
  1280. };
  1281. template <>
  1282. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1283. {
  1284. static std::string Create(cmGeneratorTarget* target,
  1285. cmGeneratorExpressionContext* context,
  1286. const GeneratorExpressionContent* content)
  1287. {
  1288. // The file used to link to the target (.so, .lib, .a).
  1289. if (!target->IsLinkable()) {
  1290. ::reportError(context, content->GetOriginalExpression(),
  1291. "TARGET_LINKER_FILE is allowed only for libraries and "
  1292. "executables with ENABLE_EXPORTS.");
  1293. return std::string();
  1294. }
  1295. return target->GetFullPath(context->Config, target->HasImportLibrary());
  1296. }
  1297. };
  1298. template <>
  1299. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1300. {
  1301. static std::string Create(cmGeneratorTarget* target,
  1302. cmGeneratorExpressionContext* context,
  1303. const GeneratorExpressionContent*)
  1304. {
  1305. return target->GetFullPath(context->Config, false, true);
  1306. }
  1307. };
  1308. template <typename ArtifactT>
  1309. struct TargetFilesystemArtifactResultGetter
  1310. {
  1311. static std::string Get(const std::string& result);
  1312. };
  1313. template <>
  1314. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1315. {
  1316. static std::string Get(const std::string& result)
  1317. {
  1318. return cmSystemTools::GetFilenameName(result);
  1319. }
  1320. };
  1321. template <>
  1322. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1323. {
  1324. static std::string Get(const std::string& result)
  1325. {
  1326. return cmSystemTools::GetFilenamePath(result);
  1327. }
  1328. };
  1329. template <>
  1330. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1331. {
  1332. static std::string Get(const std::string& result) { return result; }
  1333. };
  1334. template <typename ArtifactT, typename ComponentT>
  1335. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1336. {
  1337. TargetFilesystemArtifact() {}
  1338. int NumExpectedParameters() const CM_OVERRIDE { return 1; }
  1339. std::string Evaluate(const std::vector<std::string>& parameters,
  1340. cmGeneratorExpressionContext* context,
  1341. const GeneratorExpressionContent* content,
  1342. cmGeneratorExpressionDAGChecker* dagChecker) const
  1343. CM_OVERRIDE
  1344. {
  1345. // Lookup the referenced target.
  1346. std::string name = *parameters.begin();
  1347. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1348. ::reportError(context, content->GetOriginalExpression(),
  1349. "Expression syntax not recognized.");
  1350. return std::string();
  1351. }
  1352. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1353. if (!target) {
  1354. ::reportError(context, content->GetOriginalExpression(),
  1355. "No target \"" + name + "\"");
  1356. return std::string();
  1357. }
  1358. if (target->GetType() >= cmState::OBJECT_LIBRARY &&
  1359. target->GetType() != cmState::UNKNOWN_LIBRARY) {
  1360. ::reportError(context, content->GetOriginalExpression(), "Target \"" +
  1361. name + "\" is not an executable or library.");
  1362. return std::string();
  1363. }
  1364. if (dagChecker && (dagChecker->EvaluatingLinkLibraries(name.c_str()) ||
  1365. (dagChecker->EvaluatingSources() &&
  1366. name == dagChecker->TopTarget()))) {
  1367. ::reportError(context, content->GetOriginalExpression(),
  1368. "Expressions which require the linker language may not "
  1369. "be used while evaluating link libraries");
  1370. return std::string();
  1371. }
  1372. context->DependTargets.insert(target);
  1373. context->AllTargets.insert(target);
  1374. std::string result =
  1375. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1376. content);
  1377. if (context->HadError) {
  1378. return std::string();
  1379. }
  1380. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1381. }
  1382. };
  1383. template <typename ArtifactT>
  1384. struct TargetFilesystemArtifactNodeGroup
  1385. {
  1386. TargetFilesystemArtifactNodeGroup() {}
  1387. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1388. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1389. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1390. };
  1391. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1392. targetNodeGroup;
  1393. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1394. targetLinkerNodeGroup;
  1395. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1396. targetSoNameNodeGroup;
  1397. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1398. targetPdbNodeGroup;
  1399. static const struct ShellPathNode : public cmGeneratorExpressionNode
  1400. {
  1401. ShellPathNode() {}
  1402. std::string Evaluate(const std::vector<std::string>& parameters,
  1403. cmGeneratorExpressionContext* context,
  1404. const GeneratorExpressionContent* content,
  1405. cmGeneratorExpressionDAGChecker*) const CM_OVERRIDE
  1406. {
  1407. if (!cmSystemTools::FileIsFullPath(parameters.front())) {
  1408. reportError(context, content->GetOriginalExpression(),
  1409. "\"" + parameters.front() + "\" is not an absolute path.");
  1410. return std::string();
  1411. }
  1412. cmOutputConverter converter(context->LG->GetStateSnapshot());
  1413. return converter.ConvertDirectorySeparatorsForShell(parameters.front());
  1414. }
  1415. } shellPathNode;
  1416. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  1417. const std::string& identifier)
  1418. {
  1419. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1420. static NodeMap nodeMap;
  1421. if (nodeMap.empty()) {
  1422. nodeMap["0"] = &zeroNode;
  1423. nodeMap["1"] = &oneNode;
  1424. nodeMap["AND"] = &andNode;
  1425. nodeMap["OR"] = &orNode;
  1426. nodeMap["NOT"] = &notNode;
  1427. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1428. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1429. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1430. nodeMap["VERSION_LESS"] = &versionLessNode;
  1431. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1432. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1433. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1434. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1435. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1436. nodeMap["CONFIGURATION"] = &configurationNode;
  1437. nodeMap["CONFIG"] = &configurationTestNode;
  1438. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1439. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1440. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1441. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1442. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1443. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1444. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1445. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1446. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1447. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1448. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1449. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1450. nodeMap["STREQUAL"] = &strEqualNode;
  1451. nodeMap["EQUAL"] = &equalNode;
  1452. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1453. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1454. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1455. nodeMap["BOOL"] = &boolNode;
  1456. nodeMap["ANGLE-R"] = &angle_rNode;
  1457. nodeMap["COMMA"] = &commaNode;
  1458. nodeMap["SEMICOLON"] = &semicolonNode;
  1459. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1460. nodeMap["TARGET_NAME"] = &targetNameNode;
  1461. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1462. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1463. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1464. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1465. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1466. nodeMap["JOIN"] = &joinNode;
  1467. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1468. nodeMap["COMPILE_LANGUAGE"] = &languageNode;
  1469. nodeMap["SHELL_PATH"] = &shellPathNode;
  1470. }
  1471. NodeMap::const_iterator i = nodeMap.find(identifier);
  1472. if (i == nodeMap.end()) {
  1473. return CM_NULLPTR;
  1474. }
  1475. return i->second;
  1476. }
  1477. void reportError(cmGeneratorExpressionContext* context,
  1478. const std::string& expr, const std::string& result)
  1479. {
  1480. context->HadError = true;
  1481. if (context->Quiet) {
  1482. return;
  1483. }
  1484. std::ostringstream e;
  1485. /* clang-format off */
  1486. e << "Error evaluating generator expression:\n"
  1487. << " " << expr << "\n"
  1488. << result;
  1489. /* clang-format on */
  1490. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1491. context->Backtrace);
  1492. }