cmGeneratorExpressionEvaluator.cxx 73 KB

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