cmGeneratorExpressionEvaluator.cxx 63 KB

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