cmGeneratorExpressionEvaluator.cxx 71 KB

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