cmGeneratorExpressionEvaluator.cxx 69 KB

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