cmGeneratorExpressionEvaluator.cxx 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  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().c_str());
  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.empty())
  849. {
  850. reportError(context, content->GetOriginalExpression(),
  851. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  852. "name.");
  853. return std::string();
  854. }
  855. if (!propertyNameValidator.find(propertyName))
  856. {
  857. ::reportError(context, content->GetOriginalExpression(),
  858. "Property name not supported.");
  859. return std::string();
  860. }
  861. assert(target);
  862. if (propertyName == "LINKER_LANGUAGE")
  863. {
  864. if (target->LinkLanguagePropagatesToDependents() &&
  865. dagCheckerParent && (dagCheckerParent->EvaluatingLinkLibraries()
  866. || dagCheckerParent->EvaluatingSources()))
  867. {
  868. reportError(context, content->GetOriginalExpression(),
  869. "LINKER_LANGUAGE target property can not be used while evaluating "
  870. "link libraries for a static library");
  871. return std::string();
  872. }
  873. return target->GetLinkerLanguage(context->Config);
  874. }
  875. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  876. target->GetName(),
  877. propertyName,
  878. content,
  879. dagCheckerParent);
  880. switch (dagChecker.Check())
  881. {
  882. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  883. dagChecker.ReportError(context, content->GetOriginalExpression());
  884. return std::string();
  885. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  886. // No error. We just skip cyclic references.
  887. return std::string();
  888. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  889. for (size_t i = 1;
  890. i < cmArraySize(targetPropertyTransitiveWhitelist);
  891. ++i)
  892. {
  893. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  894. {
  895. // No error. We're not going to find anything new here.
  896. return std::string();
  897. }
  898. }
  899. case cmGeneratorExpressionDAGChecker::DAG:
  900. break;
  901. }
  902. const char *prop = target->GetProperty(propertyName);
  903. if (dagCheckerParent)
  904. {
  905. if (dagCheckerParent->EvaluatingLinkLibraries())
  906. {
  907. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  908. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  909. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_COMPARE)
  910. false)
  911. {
  912. reportError(context, content->GetOriginalExpression(),
  913. "$<TARGET_PROPERTY:...> expression in link libraries "
  914. "evaluation depends on target property which is transitive "
  915. "over the link libraries, creating a recursion.");
  916. return std::string();
  917. }
  918. #undef TRANSITIVE_PROPERTY_COMPARE
  919. if(!prop)
  920. {
  921. return std::string();
  922. }
  923. }
  924. else
  925. {
  926. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  927. dagCheckerParent->METHOD () ||
  928. assert(
  929. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  930. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  931. false);
  932. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  933. }
  934. }
  935. std::string linkedTargetsContent;
  936. std::string interfacePropertyName;
  937. bool isInterfaceProperty = false;
  938. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  939. if (propertyName == #prop) \
  940. { \
  941. interfacePropertyName = "INTERFACE_" #prop; \
  942. } \
  943. else if (propertyName == "INTERFACE_" #prop) \
  944. { \
  945. interfacePropertyName = "INTERFACE_" #prop; \
  946. isInterfaceProperty = true; \
  947. } \
  948. else
  949. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  950. // Note that the above macro terminates with an else
  951. /* else */ if (cmHasLiteralPrefix(propertyName.c_str(),
  952. "COMPILE_DEFINITIONS_"))
  953. {
  954. cmPolicies::PolicyStatus polSt =
  955. context->Makefile->GetPolicyStatus(cmPolicies::CMP0043);
  956. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD)
  957. {
  958. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  959. }
  960. }
  961. #undef POPULATE_INTERFACE_PROPERTY_NAME
  962. cmTarget const* headTarget = context->HeadTarget && isInterfaceProperty
  963. ? context->HeadTarget : target;
  964. if(isInterfaceProperty)
  965. {
  966. if(cmTarget::LinkInterfaceLibraries const* iface =
  967. target->GetLinkInterfaceLibraries(context->Config, headTarget, true))
  968. {
  969. linkedTargetsContent =
  970. getLinkedTargetsContent(iface->Libraries, target,
  971. headTarget,
  972. context, &dagChecker,
  973. interfacePropertyName);
  974. }
  975. }
  976. else if(!interfacePropertyName.empty())
  977. {
  978. if(cmTarget::LinkImplementationLibraries const* impl =
  979. target->GetLinkImplementationLibraries(context->Config))
  980. {
  981. linkedTargetsContent =
  982. getLinkedTargetsContent(impl->Libraries, target,
  983. target,
  984. context, &dagChecker,
  985. interfacePropertyName);
  986. }
  987. }
  988. if (!prop)
  989. {
  990. if (target->IsImported()
  991. || target->GetType() == cmTarget::INTERFACE_LIBRARY)
  992. {
  993. return linkedTargetsContent;
  994. }
  995. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  996. context->Config))
  997. {
  998. context->HadContextSensitiveCondition = true;
  999. return target->GetLinkInterfaceDependentBoolProperty(
  1000. propertyName,
  1001. context->Config) ? "1" : "0";
  1002. }
  1003. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1004. context->Config))
  1005. {
  1006. context->HadContextSensitiveCondition = true;
  1007. const char *propContent =
  1008. target->GetLinkInterfaceDependentStringProperty(
  1009. propertyName,
  1010. context->Config);
  1011. return propContent ? propContent : "";
  1012. }
  1013. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1014. context->Config))
  1015. {
  1016. context->HadContextSensitiveCondition = true;
  1017. const char *propContent =
  1018. target->GetLinkInterfaceDependentNumberMinProperty(
  1019. propertyName,
  1020. context->Config);
  1021. return propContent ? propContent : "";
  1022. }
  1023. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1024. context->Config))
  1025. {
  1026. context->HadContextSensitiveCondition = true;
  1027. const char *propContent =
  1028. target->GetLinkInterfaceDependentNumberMaxProperty(
  1029. propertyName,
  1030. context->Config);
  1031. return propContent ? propContent : "";
  1032. }
  1033. return linkedTargetsContent;
  1034. }
  1035. if (!target->IsImported()
  1036. && dagCheckerParent && !dagCheckerParent->EvaluatingLinkLibraries())
  1037. {
  1038. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1039. context->Config))
  1040. {
  1041. context->HadContextSensitiveCondition = true;
  1042. const char *propContent =
  1043. target->GetLinkInterfaceDependentNumberMinProperty(
  1044. propertyName,
  1045. context->Config);
  1046. return propContent ? propContent : "";
  1047. }
  1048. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1049. context->Config))
  1050. {
  1051. context->HadContextSensitiveCondition = true;
  1052. const char *propContent =
  1053. target->GetLinkInterfaceDependentNumberMaxProperty(
  1054. propertyName,
  1055. context->Config);
  1056. return propContent ? propContent : "";
  1057. }
  1058. }
  1059. if(!interfacePropertyName.empty())
  1060. {
  1061. std::string result = this->EvaluateDependentExpression(prop,
  1062. context->Makefile, context,
  1063. headTarget, target, &dagChecker);
  1064. if (!linkedTargetsContent.empty())
  1065. {
  1066. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1067. }
  1068. return result;
  1069. }
  1070. return prop;
  1071. }
  1072. } targetPropertyNode;
  1073. //----------------------------------------------------------------------------
  1074. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1075. {
  1076. TargetNameNode() {}
  1077. virtual bool GeneratesContent() const { return true; }
  1078. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  1079. virtual bool RequiresLiteralInput() const { return true; }
  1080. std::string Evaluate(const std::vector<std::string> &parameters,
  1081. cmGeneratorExpressionContext *,
  1082. const GeneratorExpressionContent *,
  1083. cmGeneratorExpressionDAGChecker *) const
  1084. {
  1085. return parameters.front();
  1086. }
  1087. virtual int NumExpectedParameters() const { return 1; }
  1088. } targetNameNode;
  1089. //----------------------------------------------------------------------------
  1090. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1091. {
  1092. TargetObjectsNode() {}
  1093. std::string Evaluate(const std::vector<std::string> &parameters,
  1094. cmGeneratorExpressionContext *context,
  1095. const GeneratorExpressionContent *content,
  1096. cmGeneratorExpressionDAGChecker *) const
  1097. {
  1098. if (!context->EvaluateForBuildsystem)
  1099. {
  1100. cmOStringStream e;
  1101. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1102. "is only suitable for consumption by CMake. It is not suitable "
  1103. "for writing out elsewhere.";
  1104. reportError(context, content->GetOriginalExpression(), e.str());
  1105. return std::string();
  1106. }
  1107. std::string tgtName = parameters.front();
  1108. cmGeneratorTarget* gt =
  1109. context->Makefile->FindGeneratorTargetToUse(tgtName.c_str());
  1110. if (!gt)
  1111. {
  1112. cmOStringStream e;
  1113. e << "Objects of target \"" << tgtName
  1114. << "\" referenced but no such target exists.";
  1115. reportError(context, content->GetOriginalExpression(), e.str());
  1116. return std::string();
  1117. }
  1118. if (gt->GetType() != cmTarget::OBJECT_LIBRARY)
  1119. {
  1120. cmOStringStream e;
  1121. e << "Objects of target \"" << tgtName
  1122. << "\" referenced but is not an OBJECT library.";
  1123. reportError(context, content->GetOriginalExpression(), e.str());
  1124. return std::string();
  1125. }
  1126. std::vector<cmSourceFile const*> objectSources;
  1127. gt->GetObjectSources(objectSources, context->Config);
  1128. std::map<cmSourceFile const*, std::string> mapping;
  1129. for(std::vector<cmSourceFile const*>::const_iterator it
  1130. = objectSources.begin(); it != objectSources.end(); ++it)
  1131. {
  1132. mapping[*it];
  1133. }
  1134. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  1135. std::string obj_dir = gt->ObjectDirectory;
  1136. std::string result;
  1137. const char* sep = "";
  1138. for(std::vector<cmSourceFile const*>::const_iterator it
  1139. = objectSources.begin(); it != objectSources.end(); ++it)
  1140. {
  1141. // Find the object file name corresponding to this source file.
  1142. std::map<cmSourceFile const*, std::string>::const_iterator
  1143. map_it = mapping.find(*it);
  1144. // It must exist because we populated the mapping just above.
  1145. assert(!map_it->second.empty());
  1146. result += sep;
  1147. std::string objFile = obj_dir + map_it->second;
  1148. cmSourceFile* sf = context->Makefile->GetOrCreateSource(objFile, true);
  1149. sf->SetObjectLibrary(tgtName);
  1150. sf->SetProperty("EXTERNAL_OBJECT", "1");
  1151. result += objFile;
  1152. sep = ";";
  1153. }
  1154. return result;
  1155. }
  1156. } targetObjectsNode;
  1157. //----------------------------------------------------------------------------
  1158. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1159. {
  1160. CompileFeaturesNode() {}
  1161. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  1162. std::string Evaluate(const std::vector<std::string> &parameters,
  1163. cmGeneratorExpressionContext *context,
  1164. const GeneratorExpressionContent *content,
  1165. cmGeneratorExpressionDAGChecker *dagChecker) const
  1166. {
  1167. cmTarget const* target = context->HeadTarget;
  1168. if (!target)
  1169. {
  1170. reportError(context, content->GetOriginalExpression(),
  1171. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1172. "not be used with add_custom_command or add_custom_target.");
  1173. return std::string();
  1174. }
  1175. context->HadHeadSensitiveCondition = true;
  1176. typedef std::map<std::string, std::vector<std::string> > LangMap;
  1177. static LangMap availableFeatures;
  1178. LangMap testedFeatures;
  1179. for (std::vector<std::string>::const_iterator it = parameters.begin();
  1180. it != parameters.end(); ++it)
  1181. {
  1182. std::string error;
  1183. std::string lang;
  1184. if (!context->Makefile->CompileFeatureKnown(context->HeadTarget,
  1185. *it, lang, &error))
  1186. {
  1187. reportError(context, content->GetOriginalExpression(), error);
  1188. return std::string();
  1189. }
  1190. testedFeatures[lang].push_back(*it);
  1191. if (availableFeatures.find(lang) == availableFeatures.end())
  1192. {
  1193. const char* featuresKnown
  1194. = context->Makefile->CompileFeaturesAvailable(lang, &error);
  1195. if (!featuresKnown)
  1196. {
  1197. reportError(context, content->GetOriginalExpression(), error);
  1198. return std::string();
  1199. }
  1200. cmSystemTools::ExpandListArgument(featuresKnown,
  1201. availableFeatures[lang]);
  1202. }
  1203. }
  1204. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1205. std::string result;
  1206. for (LangMap::const_iterator lit = testedFeatures.begin();
  1207. lit != testedFeatures.end(); ++lit)
  1208. {
  1209. std::vector<std::string> const& langAvailable
  1210. = availableFeatures[lit->first];
  1211. for (std::vector<std::string>::const_iterator it = lit->second.begin();
  1212. it != lit->second.end(); ++it)
  1213. {
  1214. if (std::find(langAvailable.begin(), langAvailable.end(), *it)
  1215. == langAvailable.end())
  1216. {
  1217. return "0";
  1218. }
  1219. if (!context->Makefile->HaveStandardAvailable(target,
  1220. lit->first, *it))
  1221. {
  1222. if (evalLL)
  1223. {
  1224. const char* l = target->GetProperty(lit->first + "_STANDARD");
  1225. if (!l)
  1226. {
  1227. l = context->Makefile
  1228. ->GetDefinition("CMAKE_" + lit->first + "_STANDARD_DEFAULT");
  1229. }
  1230. assert(l);
  1231. context->MaxLanguageStandard[target][lit->first] = l;
  1232. }
  1233. else
  1234. {
  1235. return "0";
  1236. }
  1237. }
  1238. }
  1239. }
  1240. return "1";
  1241. }
  1242. } compileFeaturesNode;
  1243. //----------------------------------------------------------------------------
  1244. static const char* targetPolicyWhitelist[] = {
  1245. 0
  1246. #define TARGET_POLICY_STRING(POLICY) \
  1247. , #POLICY
  1248. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1249. #undef TARGET_POLICY_STRING
  1250. };
  1251. cmPolicies::PolicyStatus statusForTarget(cmTarget const* tgt,
  1252. const char *policy)
  1253. {
  1254. #define RETURN_POLICY(POLICY) \
  1255. if (strcmp(policy, #POLICY) == 0) \
  1256. { \
  1257. return tgt->GetPolicyStatus ## POLICY (); \
  1258. } \
  1259. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1260. #undef RETURN_POLICY
  1261. assert("!Unreachable code. Not a valid policy");
  1262. return cmPolicies::WARN;
  1263. }
  1264. cmPolicies::PolicyID policyForString(const char *policy_id)
  1265. {
  1266. #define RETURN_POLICY_ID(POLICY_ID) \
  1267. if (strcmp(policy_id, #POLICY_ID) == 0) \
  1268. { \
  1269. return cmPolicies:: POLICY_ID; \
  1270. } \
  1271. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1272. #undef RETURN_POLICY_ID
  1273. assert("!Unreachable code. Not a valid policy");
  1274. return cmPolicies::CMP0002;
  1275. }
  1276. //----------------------------------------------------------------------------
  1277. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1278. {
  1279. TargetPolicyNode() {}
  1280. virtual int NumExpectedParameters() const { return 1; }
  1281. std::string Evaluate(const std::vector<std::string> &parameters,
  1282. cmGeneratorExpressionContext *context ,
  1283. const GeneratorExpressionContent *content,
  1284. cmGeneratorExpressionDAGChecker *) const
  1285. {
  1286. if (!context->HeadTarget)
  1287. {
  1288. reportError(context, content->GetOriginalExpression(),
  1289. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1290. "may not be used with add_custom_command or add_custom_target.");
  1291. return std::string();
  1292. }
  1293. context->HadContextSensitiveCondition = true;
  1294. context->HadHeadSensitiveCondition = true;
  1295. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i)
  1296. {
  1297. const char *policy = targetPolicyWhitelist[i];
  1298. if (parameters.front() == policy)
  1299. {
  1300. cmMakefile *mf = context->HeadTarget->GetMakefile();
  1301. switch(statusForTarget(context->HeadTarget, policy))
  1302. {
  1303. case cmPolicies::WARN:
  1304. mf->IssueMessage(cmake::AUTHOR_WARNING,
  1305. mf->GetPolicies()->
  1306. GetPolicyWarning(policyForString(policy)));
  1307. case cmPolicies::REQUIRED_IF_USED:
  1308. case cmPolicies::REQUIRED_ALWAYS:
  1309. case cmPolicies::OLD:
  1310. return "0";
  1311. case cmPolicies::NEW:
  1312. return "1";
  1313. }
  1314. }
  1315. }
  1316. reportError(context, content->GetOriginalExpression(),
  1317. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1318. "policies. Currently it may be used with the following policies:\n"
  1319. #define STRINGIFY_HELPER(X) #X
  1320. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1321. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  1322. " * " STRINGIFY(POLICY) "\n"
  1323. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1324. #undef TARGET_POLICY_LIST_ITEM
  1325. );
  1326. return std::string();
  1327. }
  1328. } targetPolicyNode;
  1329. //----------------------------------------------------------------------------
  1330. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1331. {
  1332. InstallPrefixNode() {}
  1333. virtual bool GeneratesContent() const { return true; }
  1334. virtual int NumExpectedParameters() const { return 0; }
  1335. std::string Evaluate(const std::vector<std::string> &,
  1336. cmGeneratorExpressionContext *context,
  1337. const GeneratorExpressionContent *content,
  1338. cmGeneratorExpressionDAGChecker *) const
  1339. {
  1340. reportError(context, content->GetOriginalExpression(),
  1341. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1342. "should never be evaluated.");
  1343. return std::string();
  1344. }
  1345. } installPrefixNode;
  1346. //----------------------------------------------------------------------------
  1347. class ArtifactNameTag;
  1348. class ArtifactLinkerTag;
  1349. class ArtifactSonameTag;
  1350. class ArtifactPdbTag;
  1351. class ArtifactPathTag;
  1352. class ArtifactDirTag;
  1353. class ArtifactNameTag;
  1354. //----------------------------------------------------------------------------
  1355. template<typename ArtifactT>
  1356. struct TargetFilesystemArtifactResultCreator
  1357. {
  1358. static std::string Create(cmTarget* target,
  1359. cmGeneratorExpressionContext *context,
  1360. const GeneratorExpressionContent *content);
  1361. };
  1362. //----------------------------------------------------------------------------
  1363. template<>
  1364. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1365. {
  1366. static std::string Create(cmTarget* target,
  1367. cmGeneratorExpressionContext *context,
  1368. const GeneratorExpressionContent *content)
  1369. {
  1370. // The target soname file (.so.1).
  1371. if(target->IsDLLPlatform())
  1372. {
  1373. ::reportError(context, content->GetOriginalExpression(),
  1374. "TARGET_SONAME_FILE is not allowed "
  1375. "for DLL target platforms.");
  1376. return std::string();
  1377. }
  1378. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1379. {
  1380. ::reportError(context, content->GetOriginalExpression(),
  1381. "TARGET_SONAME_FILE is allowed only for "
  1382. "SHARED libraries.");
  1383. return std::string();
  1384. }
  1385. std::string result = target->GetDirectory(context->Config);
  1386. result += "/";
  1387. result += target->GetSOName(context->Config);
  1388. return result;
  1389. }
  1390. };
  1391. //----------------------------------------------------------------------------
  1392. template<>
  1393. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1394. {
  1395. static std::string Create(cmTarget* target,
  1396. cmGeneratorExpressionContext *context,
  1397. const GeneratorExpressionContent *content)
  1398. {
  1399. std::string language = target->GetLinkerLanguage(context->Config);
  1400. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1401. if(!context->Makefile->IsOn(pdbSupportVar))
  1402. {
  1403. ::reportError(context, content->GetOriginalExpression(),
  1404. "TARGET_PDB_FILE is not supported by the target linker.");
  1405. return std::string();
  1406. }
  1407. cmTarget::TargetType targetType = target->GetType();
  1408. if(targetType != cmTarget::SHARED_LIBRARY &&
  1409. targetType != cmTarget::MODULE_LIBRARY &&
  1410. targetType != cmTarget::EXECUTABLE)
  1411. {
  1412. ::reportError(context, content->GetOriginalExpression(),
  1413. "TARGET_PDB_FILE is allowed only for "
  1414. "targets with linker created artifacts.");
  1415. return std::string();
  1416. }
  1417. std::string result = target->GetPDBDirectory(context->Config);
  1418. result += "/";
  1419. result += target->GetPDBName(context->Config);
  1420. return result;
  1421. }
  1422. };
  1423. //----------------------------------------------------------------------------
  1424. template<>
  1425. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1426. {
  1427. static std::string Create(cmTarget* target,
  1428. cmGeneratorExpressionContext *context,
  1429. const GeneratorExpressionContent *content)
  1430. {
  1431. // The file used to link to the target (.so, .lib, .a).
  1432. if(!target->IsLinkable())
  1433. {
  1434. ::reportError(context, content->GetOriginalExpression(),
  1435. "TARGET_LINKER_FILE is allowed only for libraries and "
  1436. "executables with ENABLE_EXPORTS.");
  1437. return std::string();
  1438. }
  1439. return target->GetFullPath(context->Config,
  1440. target->HasImportLibrary());
  1441. }
  1442. };
  1443. //----------------------------------------------------------------------------
  1444. template<>
  1445. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1446. {
  1447. static std::string Create(cmTarget* target,
  1448. cmGeneratorExpressionContext *context,
  1449. const GeneratorExpressionContent *)
  1450. {
  1451. return target->GetFullPath(context->Config, false, true);
  1452. }
  1453. };
  1454. //----------------------------------------------------------------------------
  1455. template<typename ArtifactT>
  1456. struct TargetFilesystemArtifactResultGetter
  1457. {
  1458. static std::string Get(const std::string &result);
  1459. };
  1460. //----------------------------------------------------------------------------
  1461. template<>
  1462. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1463. {
  1464. static std::string Get(const std::string &result)
  1465. { return cmSystemTools::GetFilenameName(result); }
  1466. };
  1467. //----------------------------------------------------------------------------
  1468. template<>
  1469. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1470. {
  1471. static std::string Get(const std::string &result)
  1472. { return cmSystemTools::GetFilenamePath(result); }
  1473. };
  1474. //----------------------------------------------------------------------------
  1475. template<>
  1476. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1477. {
  1478. static std::string Get(const std::string &result)
  1479. { return result; }
  1480. };
  1481. //----------------------------------------------------------------------------
  1482. template<typename ArtifactT, typename ComponentT>
  1483. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1484. {
  1485. TargetFilesystemArtifact() {}
  1486. virtual int NumExpectedParameters() const { return 1; }
  1487. std::string Evaluate(const std::vector<std::string> &parameters,
  1488. cmGeneratorExpressionContext *context,
  1489. const GeneratorExpressionContent *content,
  1490. cmGeneratorExpressionDAGChecker *dagChecker) const
  1491. {
  1492. // Lookup the referenced target.
  1493. std::string name = *parameters.begin();
  1494. if (!cmGeneratorExpression::IsValidTargetName(name))
  1495. {
  1496. ::reportError(context, content->GetOriginalExpression(),
  1497. "Expression syntax not recognized.");
  1498. return std::string();
  1499. }
  1500. cmTarget* target = context->Makefile->FindTargetToUse(name);
  1501. if(!target)
  1502. {
  1503. ::reportError(context, content->GetOriginalExpression(),
  1504. "No target \"" + name + "\"");
  1505. return std::string();
  1506. }
  1507. if(target->GetType() >= cmTarget::OBJECT_LIBRARY &&
  1508. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1509. {
  1510. ::reportError(context, content->GetOriginalExpression(),
  1511. "Target \"" + name + "\" is not an executable or library.");
  1512. return std::string();
  1513. }
  1514. if (dagChecker && (dagChecker->EvaluatingLinkLibraries(name.c_str())
  1515. || (dagChecker->EvaluatingSources()
  1516. && name == dagChecker->TopTarget())))
  1517. {
  1518. ::reportError(context, content->GetOriginalExpression(),
  1519. "Expressions which require the linker language may not "
  1520. "be used while evaluating link libraries");
  1521. return std::string();
  1522. }
  1523. context->DependTargets.insert(target);
  1524. context->AllTargets.insert(target);
  1525. std::string result =
  1526. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(
  1527. target,
  1528. context,
  1529. content);
  1530. if (context->HadError)
  1531. {
  1532. return std::string();
  1533. }
  1534. return
  1535. TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1536. }
  1537. };
  1538. //----------------------------------------------------------------------------
  1539. template<typename ArtifactT>
  1540. struct TargetFilesystemArtifactNodeGroup
  1541. {
  1542. TargetFilesystemArtifactNodeGroup()
  1543. {
  1544. }
  1545. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1546. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1547. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1548. };
  1549. //----------------------------------------------------------------------------
  1550. static const
  1551. TargetFilesystemArtifactNodeGroup<ArtifactNameTag> targetNodeGroup;
  1552. static const
  1553. TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag> targetLinkerNodeGroup;
  1554. static const
  1555. TargetFilesystemArtifactNodeGroup<ArtifactSonameTag> targetSoNameNodeGroup;
  1556. static const
  1557. TargetFilesystemArtifactNodeGroup<ArtifactPdbTag> targetPdbNodeGroup;
  1558. //----------------------------------------------------------------------------
  1559. static const
  1560. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1561. {
  1562. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1563. static NodeMap nodeMap;
  1564. if (nodeMap.empty())
  1565. {
  1566. nodeMap["0"] = &zeroNode;
  1567. nodeMap["1"] = &oneNode;
  1568. nodeMap["AND"] = &andNode;
  1569. nodeMap["OR"] = &orNode;
  1570. nodeMap["NOT"] = &notNode;
  1571. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1572. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1573. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1574. nodeMap["VERSION_LESS"] = &versionLessNode;
  1575. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1576. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1577. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1578. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1579. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1580. nodeMap["CONFIGURATION"] = &configurationNode;
  1581. nodeMap["CONFIG"] = &configurationTestNode;
  1582. nodeMap["TARGET_FILE"] = &targetNodeGroup.File;
  1583. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerNodeGroup.File;
  1584. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameNodeGroup.File;
  1585. nodeMap["TARGET_PDB_FILE"] = &targetPdbNodeGroup.File;
  1586. nodeMap["TARGET_FILE_NAME"] = &targetNodeGroup.FileName;
  1587. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerNodeGroup.FileName;
  1588. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameNodeGroup.FileName;
  1589. nodeMap["TARGET_PDB_FILE_NAME"] = &targetPdbNodeGroup.FileName;
  1590. nodeMap["TARGET_FILE_DIR"] = &targetNodeGroup.FileDir;
  1591. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir;
  1592. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir;
  1593. nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir;
  1594. nodeMap["STREQUAL"] = &strEqualNode;
  1595. nodeMap["EQUAL"] = &equalNode;
  1596. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1597. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1598. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1599. nodeMap["BOOL"] = &boolNode;
  1600. nodeMap["ANGLE-R"] = &angle_rNode;
  1601. nodeMap["COMMA"] = &commaNode;
  1602. nodeMap["SEMICOLON"] = &semicolonNode;
  1603. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1604. nodeMap["TARGET_NAME"] = &targetNameNode;
  1605. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1606. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1607. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1608. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1609. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1610. nodeMap["JOIN"] = &joinNode;
  1611. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1612. }
  1613. NodeMap::const_iterator i = nodeMap.find(identifier);
  1614. if (i == nodeMap.end())
  1615. {
  1616. return 0;
  1617. }
  1618. return i->second;
  1619. }
  1620. //----------------------------------------------------------------------------
  1621. GeneratorExpressionContent::GeneratorExpressionContent(
  1622. const char *startContent,
  1623. size_t length)
  1624. : StartContent(startContent), ContentLength(length)
  1625. {
  1626. }
  1627. //----------------------------------------------------------------------------
  1628. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1629. {
  1630. return std::string(this->StartContent, this->ContentLength);
  1631. }
  1632. //----------------------------------------------------------------------------
  1633. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1634. const cmGeneratorExpressionNode *node,
  1635. const std::string &identifier,
  1636. cmGeneratorExpressionContext *context,
  1637. cmGeneratorExpressionDAGChecker *dagChecker,
  1638. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1639. pit) const
  1640. {
  1641. std::string result;
  1642. const
  1643. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1644. pend = this->ParamChildren.end();
  1645. for ( ; pit != pend; ++pit)
  1646. {
  1647. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1648. = pit->begin();
  1649. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1650. = pit->end();
  1651. for ( ; it != end; ++it)
  1652. {
  1653. if (node->RequiresLiteralInput())
  1654. {
  1655. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1656. {
  1657. reportError(context, this->GetOriginalExpression(),
  1658. "$<" + identifier + "> expression requires literal input.");
  1659. return std::string();
  1660. }
  1661. }
  1662. result += (*it)->Evaluate(context, dagChecker);
  1663. if (context->HadError)
  1664. {
  1665. return std::string();
  1666. }
  1667. }
  1668. if ((pit + 1) != pend)
  1669. {
  1670. result += ",";
  1671. }
  1672. }
  1673. if (node->RequiresLiteralInput())
  1674. {
  1675. std::vector<std::string> parameters;
  1676. parameters.push_back(result);
  1677. return node->Evaluate(parameters, context, this, dagChecker);
  1678. }
  1679. return result;
  1680. }
  1681. //----------------------------------------------------------------------------
  1682. std::string GeneratorExpressionContent::Evaluate(
  1683. cmGeneratorExpressionContext *context,
  1684. cmGeneratorExpressionDAGChecker *dagChecker) const
  1685. {
  1686. std::string identifier;
  1687. {
  1688. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1689. = this->IdentifierChildren.begin();
  1690. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1691. = this->IdentifierChildren.end();
  1692. for ( ; it != end; ++it)
  1693. {
  1694. identifier += (*it)->Evaluate(context, dagChecker);
  1695. if (context->HadError)
  1696. {
  1697. return std::string();
  1698. }
  1699. }
  1700. }
  1701. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1702. if (!node)
  1703. {
  1704. reportError(context, this->GetOriginalExpression(),
  1705. "Expression did not evaluate to a known generator expression");
  1706. return std::string();
  1707. }
  1708. if (!node->GeneratesContent())
  1709. {
  1710. if (node->NumExpectedParameters() == 1
  1711. && node->AcceptsArbitraryContentParameter())
  1712. {
  1713. if (this->ParamChildren.empty())
  1714. {
  1715. reportError(context, this->GetOriginalExpression(),
  1716. "$<" + identifier + "> expression requires a parameter.");
  1717. }
  1718. }
  1719. else
  1720. {
  1721. std::vector<std::string> parameters;
  1722. this->EvaluateParameters(node, identifier, context, dagChecker,
  1723. parameters);
  1724. }
  1725. return std::string();
  1726. }
  1727. std::vector<std::string> parameters;
  1728. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1729. if (context->HadError)
  1730. {
  1731. return std::string();
  1732. }
  1733. return node->Evaluate(parameters, context, this, dagChecker);
  1734. }
  1735. //----------------------------------------------------------------------------
  1736. std::string GeneratorExpressionContent::EvaluateParameters(
  1737. const cmGeneratorExpressionNode *node,
  1738. const std::string &identifier,
  1739. cmGeneratorExpressionContext *context,
  1740. cmGeneratorExpressionDAGChecker *dagChecker,
  1741. std::vector<std::string> &parameters) const
  1742. {
  1743. const int numExpected = node->NumExpectedParameters();
  1744. {
  1745. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1746. pit = this->ParamChildren.begin();
  1747. const
  1748. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1749. pend = this->ParamChildren.end();
  1750. const bool acceptsArbitraryContent
  1751. = node->AcceptsArbitraryContentParameter();
  1752. int counter = 1;
  1753. for ( ; pit != pend; ++pit, ++counter)
  1754. {
  1755. if (acceptsArbitraryContent && counter == numExpected)
  1756. {
  1757. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1758. context,
  1759. dagChecker,
  1760. pit);
  1761. parameters.push_back(lastParam);
  1762. return std::string();
  1763. }
  1764. else
  1765. {
  1766. std::string parameter;
  1767. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1768. pit->begin();
  1769. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1770. pit->end();
  1771. for ( ; it != end; ++it)
  1772. {
  1773. parameter += (*it)->Evaluate(context, dagChecker);
  1774. if (context->HadError)
  1775. {
  1776. return std::string();
  1777. }
  1778. }
  1779. parameters.push_back(parameter);
  1780. }
  1781. }
  1782. }
  1783. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1784. && (unsigned int)numExpected != parameters.size()))
  1785. {
  1786. if (numExpected == 0)
  1787. {
  1788. reportError(context, this->GetOriginalExpression(),
  1789. "$<" + identifier + "> expression requires no parameters.");
  1790. }
  1791. else if (numExpected == 1)
  1792. {
  1793. reportError(context, this->GetOriginalExpression(),
  1794. "$<" + identifier + "> expression requires "
  1795. "exactly one parameter.");
  1796. }
  1797. else
  1798. {
  1799. cmOStringStream e;
  1800. e << "$<" + identifier + "> expression requires "
  1801. << numExpected
  1802. << " comma separated parameters, but got "
  1803. << parameters.size() << " instead.";
  1804. reportError(context, this->GetOriginalExpression(), e.str());
  1805. }
  1806. return std::string();
  1807. }
  1808. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1809. && parameters.empty())
  1810. {
  1811. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1812. + "> expression requires at least one parameter.");
  1813. }
  1814. if (numExpected == cmGeneratorExpressionNode::OneOrZeroParameters
  1815. && parameters.size() > 1)
  1816. {
  1817. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1818. + "> expression requires one or zero parameters.");
  1819. }
  1820. return std::string();
  1821. }
  1822. //----------------------------------------------------------------------------
  1823. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1824. {
  1825. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1826. = c.begin();
  1827. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1828. = c.end();
  1829. for ( ; it != end; ++it)
  1830. {
  1831. delete *it;
  1832. }
  1833. }
  1834. //----------------------------------------------------------------------------
  1835. GeneratorExpressionContent::~GeneratorExpressionContent()
  1836. {
  1837. deleteAll(this->IdentifierChildren);
  1838. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1839. std::vector<EvaluatorVector>::const_iterator pit =
  1840. this->ParamChildren.begin();
  1841. const std::vector<EvaluatorVector>::const_iterator pend =
  1842. this->ParamChildren.end();
  1843. for ( ; pit != pend; ++pit)
  1844. {
  1845. deleteAll(*pit);
  1846. }
  1847. }