cmGeneratorExpressionEvaluator.cxx 71 KB

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