cmGeneratorExpressionEvaluator.cxx 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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. template <typename T>
  688. std::string
  689. getLinkedTargetsContent(
  690. std::vector<T> const &libraries,
  691. cmTarget const* target,
  692. cmTarget const* headTarget,
  693. cmGeneratorExpressionContext *context,
  694. cmGeneratorExpressionDAGChecker *dagChecker,
  695. const std::string &interfacePropertyName)
  696. {
  697. std::string linkedTargetsContent;
  698. std::string sep;
  699. std::string depString;
  700. for (typename std::vector<T>::const_iterator it = libraries.begin();
  701. it != libraries.end(); ++it)
  702. {
  703. // Broken code can have a target in its own link interface.
  704. // Don't follow such link interface entries so as not to create a
  705. // self-referencing loop.
  706. if (it->Target && it->Target != target)
  707. {
  708. depString +=
  709. sep + "$<TARGET_PROPERTY:" +
  710. it->Target->GetName() + "," + interfacePropertyName + ">";
  711. sep = ";";
  712. }
  713. }
  714. if(!depString.empty())
  715. {
  716. cmGeneratorExpression ge(&context->Backtrace);
  717. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  718. linkedTargetsContent = cge->Evaluate(target->GetMakefile(),
  719. context->Config,
  720. context->Quiet,
  721. headTarget,
  722. target,
  723. dagChecker);
  724. if (cge->GetHadContextSensitiveCondition())
  725. {
  726. context->HadContextSensitiveCondition = true;
  727. }
  728. }
  729. return linkedTargetsContent;
  730. }
  731. //----------------------------------------------------------------------------
  732. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  733. {
  734. TargetPropertyNode() {}
  735. // This node handles errors on parameter count itself.
  736. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  737. std::string Evaluate(const std::vector<std::string> &parameters,
  738. cmGeneratorExpressionContext *context,
  739. const GeneratorExpressionContent *content,
  740. cmGeneratorExpressionDAGChecker *dagCheckerParent
  741. ) const
  742. {
  743. if (parameters.size() != 1 && parameters.size() != 2)
  744. {
  745. reportError(context, content->GetOriginalExpression(),
  746. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  747. return std::string();
  748. }
  749. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  750. cmTarget const* target = context->HeadTarget;
  751. std::string propertyName = *parameters.begin();
  752. if (!target && parameters.size() == 1)
  753. {
  754. reportError(context, content->GetOriginalExpression(),
  755. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  756. "It may not be used with add_custom_command or add_custom_target. "
  757. "Specify the target to read a property from using the "
  758. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  759. return std::string();
  760. }
  761. if (parameters.size() == 2)
  762. {
  763. if (parameters.begin()->empty() && parameters[1].empty())
  764. {
  765. reportError(context, content->GetOriginalExpression(),
  766. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  767. "target name and property name.");
  768. return std::string();
  769. }
  770. if (parameters.begin()->empty())
  771. {
  772. reportError(context, content->GetOriginalExpression(),
  773. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  774. "target name.");
  775. return std::string();
  776. }
  777. std::string targetName = parameters.front();
  778. propertyName = parameters[1];
  779. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  780. {
  781. if (!propertyNameValidator.find(propertyName.c_str()))
  782. {
  783. ::reportError(context, content->GetOriginalExpression(),
  784. "Target name and property name not supported.");
  785. return std::string();
  786. }
  787. ::reportError(context, content->GetOriginalExpression(),
  788. "Target name not supported.");
  789. return std::string();
  790. }
  791. if(propertyName == "ALIASED_TARGET")
  792. {
  793. if(context->Makefile->IsAlias(targetName))
  794. {
  795. if(cmTarget* tgt = context->Makefile->FindTargetToUse(targetName))
  796. {
  797. return tgt->GetName();
  798. }
  799. }
  800. return "";
  801. }
  802. target = context->Makefile->FindTargetToUse(targetName);
  803. if (!target)
  804. {
  805. cmOStringStream e;
  806. e << "Target \""
  807. << targetName
  808. << "\" not found.";
  809. reportError(context, content->GetOriginalExpression(), e.str());
  810. return std::string();
  811. }
  812. context->AllTargets.insert(target);
  813. }
  814. if (target == context->HeadTarget)
  815. {
  816. // Keep track of the properties seen while processing.
  817. // The evaluation of the LINK_LIBRARIES generator expressions
  818. // will check this to ensure that properties have one consistent
  819. // value for all evaluations.
  820. context->SeenTargetProperties.insert(propertyName);
  821. }
  822. if (propertyName.empty())
  823. {
  824. reportError(context, content->GetOriginalExpression(),
  825. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  826. "name.");
  827. return std::string();
  828. }
  829. if (!propertyNameValidator.find(propertyName))
  830. {
  831. ::reportError(context, content->GetOriginalExpression(),
  832. "Property name not supported.");
  833. return std::string();
  834. }
  835. assert(target);
  836. if (propertyName == "LINKER_LANGUAGE")
  837. {
  838. if (target->LinkLanguagePropagatesToDependents() &&
  839. dagCheckerParent && (dagCheckerParent->EvaluatingLinkLibraries()
  840. || dagCheckerParent->EvaluatingSources()))
  841. {
  842. reportError(context, content->GetOriginalExpression(),
  843. "LINKER_LANGUAGE target property can not be used while evaluating "
  844. "link libraries for a static library");
  845. return std::string();
  846. }
  847. return target->GetLinkerLanguage(context->Config);
  848. }
  849. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  850. target->GetName(),
  851. propertyName,
  852. content,
  853. dagCheckerParent);
  854. switch (dagChecker.Check())
  855. {
  856. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  857. dagChecker.ReportError(context, content->GetOriginalExpression());
  858. return std::string();
  859. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  860. // No error. We just skip cyclic references.
  861. return std::string();
  862. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  863. for (size_t i = 1;
  864. i < cmArraySize(targetPropertyTransitiveWhitelist);
  865. ++i)
  866. {
  867. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  868. {
  869. // No error. We're not going to find anything new here.
  870. return std::string();
  871. }
  872. }
  873. case cmGeneratorExpressionDAGChecker::DAG:
  874. break;
  875. }
  876. const char *prop = target->GetProperty(propertyName);
  877. if (dagCheckerParent)
  878. {
  879. if (dagCheckerParent->EvaluatingLinkLibraries())
  880. {
  881. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  882. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  883. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_COMPARE)
  884. false)
  885. {
  886. reportError(context, content->GetOriginalExpression(),
  887. "$<TARGET_PROPERTY:...> expression in link libraries "
  888. "evaluation depends on target property which is transitive "
  889. "over the link libraries, creating a recursion.");
  890. return std::string();
  891. }
  892. #undef TRANSITIVE_PROPERTY_COMPARE
  893. if(!prop)
  894. {
  895. return std::string();
  896. }
  897. }
  898. else
  899. {
  900. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  901. dagCheckerParent->METHOD () ||
  902. assert(
  903. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  904. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  905. false);
  906. }
  907. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  908. }
  909. std::string linkedTargetsContent;
  910. std::string interfacePropertyName;
  911. bool isInterfaceProperty = false;
  912. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  913. if (propertyName == #prop) \
  914. { \
  915. interfacePropertyName = "INTERFACE_" #prop; \
  916. } \
  917. else if (propertyName == "INTERFACE_" #prop) \
  918. { \
  919. interfacePropertyName = "INTERFACE_" #prop; \
  920. isInterfaceProperty = true; \
  921. } \
  922. else
  923. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  924. // Note that the above macro terminates with an else
  925. /* else */ if (cmHasLiteralPrefix(propertyName.c_str(),
  926. "COMPILE_DEFINITIONS_"))
  927. {
  928. cmPolicies::PolicyStatus polSt =
  929. context->Makefile->GetPolicyStatus(cmPolicies::CMP0043);
  930. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD)
  931. {
  932. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  933. }
  934. }
  935. #undef POPULATE_INTERFACE_PROPERTY_NAME
  936. cmTarget const* headTarget = context->HeadTarget
  937. ? context->HeadTarget : target;
  938. if(isInterfaceProperty)
  939. {
  940. if(cmTarget::LinkInterfaceLibraries const* iface =
  941. target->GetLinkInterfaceLibraries(context->Config, headTarget, true))
  942. {
  943. linkedTargetsContent =
  944. getLinkedTargetsContent(iface->Libraries, target,
  945. headTarget,
  946. context, &dagChecker,
  947. interfacePropertyName);
  948. }
  949. }
  950. else if(!interfacePropertyName.empty())
  951. {
  952. const cmTarget::LinkImplementationLibraries *impl
  953. = target->GetLinkImplementationLibraries(context->Config);
  954. if(impl)
  955. {
  956. linkedTargetsContent =
  957. getLinkedTargetsContent(impl->Libraries, target,
  958. headTarget,
  959. context, &dagChecker,
  960. interfacePropertyName);
  961. }
  962. }
  963. linkedTargetsContent =
  964. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  965. if (!prop)
  966. {
  967. if (target->IsImported()
  968. || target->GetType() == cmTarget::INTERFACE_LIBRARY)
  969. {
  970. return linkedTargetsContent;
  971. }
  972. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  973. context->Config))
  974. {
  975. context->HadContextSensitiveCondition = true;
  976. return target->GetLinkInterfaceDependentBoolProperty(
  977. propertyName,
  978. context->Config) ? "1" : "0";
  979. }
  980. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  981. context->Config))
  982. {
  983. context->HadContextSensitiveCondition = true;
  984. const char *propContent =
  985. target->GetLinkInterfaceDependentStringProperty(
  986. propertyName,
  987. context->Config);
  988. return propContent ? propContent : "";
  989. }
  990. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  991. context->Config))
  992. {
  993. context->HadContextSensitiveCondition = true;
  994. const char *propContent =
  995. target->GetLinkInterfaceDependentNumberMinProperty(
  996. propertyName,
  997. context->Config);
  998. return propContent ? propContent : "";
  999. }
  1000. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1001. context->Config))
  1002. {
  1003. context->HadContextSensitiveCondition = true;
  1004. const char *propContent =
  1005. target->GetLinkInterfaceDependentNumberMaxProperty(
  1006. propertyName,
  1007. context->Config);
  1008. return propContent ? propContent : "";
  1009. }
  1010. return linkedTargetsContent;
  1011. }
  1012. if (!target->IsImported()
  1013. && dagCheckerParent && !dagCheckerParent->EvaluatingLinkLibraries())
  1014. {
  1015. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1016. context->Config))
  1017. {
  1018. context->HadContextSensitiveCondition = true;
  1019. const char *propContent =
  1020. target->GetLinkInterfaceDependentNumberMinProperty(
  1021. propertyName,
  1022. context->Config);
  1023. return propContent ? propContent : "";
  1024. }
  1025. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1026. context->Config))
  1027. {
  1028. context->HadContextSensitiveCondition = true;
  1029. const char *propContent =
  1030. target->GetLinkInterfaceDependentNumberMaxProperty(
  1031. propertyName,
  1032. context->Config);
  1033. return propContent ? propContent : "";
  1034. }
  1035. }
  1036. if(!interfacePropertyName.empty())
  1037. {
  1038. cmGeneratorExpression ge(&context->Backtrace);
  1039. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  1040. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  1041. std::string result = cge->Evaluate(context->Makefile,
  1042. context->Config,
  1043. context->Quiet,
  1044. headTarget,
  1045. target,
  1046. &dagChecker);
  1047. if (cge->GetHadContextSensitiveCondition())
  1048. {
  1049. context->HadContextSensitiveCondition = true;
  1050. }
  1051. if (!linkedTargetsContent.empty())
  1052. {
  1053. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1054. }
  1055. return result;
  1056. }
  1057. return prop;
  1058. }
  1059. } targetPropertyNode;
  1060. //----------------------------------------------------------------------------
  1061. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1062. {
  1063. TargetNameNode() {}
  1064. virtual bool GeneratesContent() const { return true; }
  1065. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  1066. virtual bool RequiresLiteralInput() const { return true; }
  1067. std::string Evaluate(const std::vector<std::string> &parameters,
  1068. cmGeneratorExpressionContext *,
  1069. const GeneratorExpressionContent *,
  1070. cmGeneratorExpressionDAGChecker *) const
  1071. {
  1072. return parameters.front();
  1073. }
  1074. virtual int NumExpectedParameters() const { return 1; }
  1075. } targetNameNode;
  1076. //----------------------------------------------------------------------------
  1077. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1078. {
  1079. TargetObjectsNode() {}
  1080. std::string Evaluate(const std::vector<std::string> &parameters,
  1081. cmGeneratorExpressionContext *context,
  1082. const GeneratorExpressionContent *content,
  1083. cmGeneratorExpressionDAGChecker *) const
  1084. {
  1085. if (!context->EvaluateForBuildsystem)
  1086. {
  1087. cmOStringStream e;
  1088. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1089. "is only suitable for consumption by CMake. It is not suitable "
  1090. "for writing out elsewhere.";
  1091. reportError(context, content->GetOriginalExpression(), e.str());
  1092. return std::string();
  1093. }
  1094. std::string tgtName = parameters.front();
  1095. cmGeneratorTarget* gt =
  1096. context->Makefile->FindGeneratorTargetToUse(tgtName.c_str());
  1097. if (!gt)
  1098. {
  1099. cmOStringStream e;
  1100. e << "Objects of target \"" << tgtName
  1101. << "\" referenced but no such target exists.";
  1102. reportError(context, content->GetOriginalExpression(), e.str());
  1103. return std::string();
  1104. }
  1105. if (gt->GetType() != cmTarget::OBJECT_LIBRARY)
  1106. {
  1107. cmOStringStream e;
  1108. e << "Objects of target \"" << tgtName
  1109. << "\" referenced but is not an OBJECT library.";
  1110. reportError(context, content->GetOriginalExpression(), e.str());
  1111. return std::string();
  1112. }
  1113. std::vector<cmSourceFile const*> objectSources;
  1114. gt->GetObjectSources(objectSources, context->Config);
  1115. std::map<cmSourceFile const*, std::string> mapping;
  1116. for(std::vector<cmSourceFile const*>::const_iterator it
  1117. = objectSources.begin(); it != objectSources.end(); ++it)
  1118. {
  1119. mapping[*it];
  1120. }
  1121. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  1122. std::string obj_dir = gt->ObjectDirectory;
  1123. std::string result;
  1124. const char* sep = "";
  1125. for(std::map<cmSourceFile const*, std::string>::const_iterator it
  1126. = mapping.begin(); it != mapping.end(); ++it)
  1127. {
  1128. assert(!it->second.empty());
  1129. result += sep;
  1130. std::string objFile = obj_dir + it->second;
  1131. cmSourceFile* sf = context->Makefile->GetOrCreateSource(objFile, true);
  1132. sf->SetObjectLibrary(tgtName);
  1133. sf->SetProperty("EXTERNAL_OBJECT", "1");
  1134. result += objFile;
  1135. sep = ";";
  1136. }
  1137. return result;
  1138. }
  1139. } targetObjectsNode;
  1140. //----------------------------------------------------------------------------
  1141. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1142. {
  1143. CompileFeaturesNode() {}
  1144. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  1145. std::string Evaluate(const std::vector<std::string> &parameters,
  1146. cmGeneratorExpressionContext *context,
  1147. const GeneratorExpressionContent *content,
  1148. cmGeneratorExpressionDAGChecker *dagChecker) const
  1149. {
  1150. cmTarget const* target = context->HeadTarget;
  1151. if (!target)
  1152. {
  1153. reportError(context, content->GetOriginalExpression(),
  1154. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1155. "not be used with add_custom_command or add_custom_target.");
  1156. return std::string();
  1157. }
  1158. typedef std::map<std::string, std::vector<std::string> > LangMap;
  1159. static LangMap availableFeatures;
  1160. LangMap testedFeatures;
  1161. for (std::vector<std::string>::const_iterator it = parameters.begin();
  1162. it != parameters.end(); ++it)
  1163. {
  1164. std::string error;
  1165. std::string lang;
  1166. if (!context->Makefile->CompileFeatureKnown(context->HeadTarget,
  1167. *it, lang, &error))
  1168. {
  1169. reportError(context, content->GetOriginalExpression(), error);
  1170. return std::string();
  1171. }
  1172. testedFeatures[lang].push_back(*it);
  1173. if (availableFeatures.find(lang) == availableFeatures.end())
  1174. {
  1175. const char* featuresKnown
  1176. = context->Makefile->CompileFeaturesAvailable(lang, &error);
  1177. if (!featuresKnown)
  1178. {
  1179. reportError(context, content->GetOriginalExpression(), error);
  1180. return std::string();
  1181. }
  1182. cmSystemTools::ExpandListArgument(featuresKnown,
  1183. availableFeatures[lang]);
  1184. }
  1185. }
  1186. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1187. std::string result;
  1188. for (LangMap::const_iterator lit = testedFeatures.begin();
  1189. lit != testedFeatures.end(); ++lit)
  1190. {
  1191. for (std::vector<std::string>::const_iterator it = lit->second.begin();
  1192. it != lit->second.end(); ++it)
  1193. {
  1194. if (!context->Makefile->HaveFeatureAvailable(target,
  1195. lit->first, *it))
  1196. {
  1197. if (evalLL)
  1198. {
  1199. const char* l = target->GetProperty(lit->first + "_STANDARD");
  1200. if (!l)
  1201. {
  1202. l = context->Makefile
  1203. ->GetDefinition("CMAKE_" + lit->first + "_STANDARD_DEFAULT");
  1204. }
  1205. assert(l);
  1206. context->MaxLanguageStandard[target][lit->first] = l;
  1207. }
  1208. else
  1209. {
  1210. return "0";
  1211. }
  1212. }
  1213. }
  1214. }
  1215. return "1";
  1216. }
  1217. } compileFeaturesNode;
  1218. //----------------------------------------------------------------------------
  1219. static const char* targetPolicyWhitelist[] = {
  1220. 0
  1221. #define TARGET_POLICY_STRING(POLICY) \
  1222. , #POLICY
  1223. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1224. #undef TARGET_POLICY_STRING
  1225. };
  1226. cmPolicies::PolicyStatus statusForTarget(cmTarget const* tgt,
  1227. const char *policy)
  1228. {
  1229. #define RETURN_POLICY(POLICY) \
  1230. if (strcmp(policy, #POLICY) == 0) \
  1231. { \
  1232. return tgt->GetPolicyStatus ## POLICY (); \
  1233. } \
  1234. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1235. #undef RETURN_POLICY
  1236. assert("!Unreachable code. Not a valid policy");
  1237. return cmPolicies::WARN;
  1238. }
  1239. cmPolicies::PolicyID policyForString(const char *policy_id)
  1240. {
  1241. #define RETURN_POLICY_ID(POLICY_ID) \
  1242. if (strcmp(policy_id, #POLICY_ID) == 0) \
  1243. { \
  1244. return cmPolicies:: POLICY_ID; \
  1245. } \
  1246. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1247. #undef RETURN_POLICY_ID
  1248. assert("!Unreachable code. Not a valid policy");
  1249. return cmPolicies::CMP0002;
  1250. }
  1251. //----------------------------------------------------------------------------
  1252. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1253. {
  1254. TargetPolicyNode() {}
  1255. virtual int NumExpectedParameters() const { return 1; }
  1256. std::string Evaluate(const std::vector<std::string> &parameters,
  1257. cmGeneratorExpressionContext *context ,
  1258. const GeneratorExpressionContent *content,
  1259. cmGeneratorExpressionDAGChecker *) const
  1260. {
  1261. if (!context->HeadTarget)
  1262. {
  1263. reportError(context, content->GetOriginalExpression(),
  1264. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1265. "may not be used with add_custom_command or add_custom_target.");
  1266. return std::string();
  1267. }
  1268. context->HadContextSensitiveCondition = true;
  1269. for (size_t i = 1; i < cmArraySize(targetPolicyWhitelist); ++i)
  1270. {
  1271. const char *policy = targetPolicyWhitelist[i];
  1272. if (parameters.front() == policy)
  1273. {
  1274. cmMakefile *mf = context->HeadTarget->GetMakefile();
  1275. switch(statusForTarget(context->HeadTarget, policy))
  1276. {
  1277. case cmPolicies::WARN:
  1278. mf->IssueMessage(cmake::AUTHOR_WARNING,
  1279. mf->GetPolicies()->
  1280. GetPolicyWarning(policyForString(policy)));
  1281. case cmPolicies::REQUIRED_IF_USED:
  1282. case cmPolicies::REQUIRED_ALWAYS:
  1283. case cmPolicies::OLD:
  1284. return "0";
  1285. case cmPolicies::NEW:
  1286. return "1";
  1287. }
  1288. }
  1289. }
  1290. reportError(context, content->GetOriginalExpression(),
  1291. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1292. "policies. Currently it may be used with the following policies:\n"
  1293. #define STRINGIFY_HELPER(X) #X
  1294. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1295. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  1296. " * " STRINGIFY(POLICY) "\n"
  1297. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1298. #undef TARGET_POLICY_LIST_ITEM
  1299. );
  1300. return std::string();
  1301. }
  1302. } targetPolicyNode;
  1303. //----------------------------------------------------------------------------
  1304. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1305. {
  1306. InstallPrefixNode() {}
  1307. virtual bool GeneratesContent() const { return true; }
  1308. virtual int NumExpectedParameters() const { return 0; }
  1309. std::string Evaluate(const std::vector<std::string> &,
  1310. cmGeneratorExpressionContext *context,
  1311. const GeneratorExpressionContent *content,
  1312. cmGeneratorExpressionDAGChecker *) const
  1313. {
  1314. reportError(context, content->GetOriginalExpression(),
  1315. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1316. "should never be evaluated.");
  1317. return std::string();
  1318. }
  1319. } installPrefixNode;
  1320. //----------------------------------------------------------------------------
  1321. template<bool linker, bool soname>
  1322. struct TargetFilesystemArtifactResultCreator
  1323. {
  1324. static std::string Create(cmTarget* target,
  1325. cmGeneratorExpressionContext *context,
  1326. const GeneratorExpressionContent *content);
  1327. };
  1328. //----------------------------------------------------------------------------
  1329. template<>
  1330. struct TargetFilesystemArtifactResultCreator<false, true>
  1331. {
  1332. static std::string Create(cmTarget* target,
  1333. cmGeneratorExpressionContext *context,
  1334. const GeneratorExpressionContent *content)
  1335. {
  1336. // The target soname file (.so.1).
  1337. if(target->IsDLLPlatform())
  1338. {
  1339. ::reportError(context, content->GetOriginalExpression(),
  1340. "TARGET_SONAME_FILE is not allowed "
  1341. "for DLL target platforms.");
  1342. return std::string();
  1343. }
  1344. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1345. {
  1346. ::reportError(context, content->GetOriginalExpression(),
  1347. "TARGET_SONAME_FILE is allowed only for "
  1348. "SHARED libraries.");
  1349. return std::string();
  1350. }
  1351. std::string result = target->GetDirectory(context->Config);
  1352. result += "/";
  1353. result += target->GetSOName(context->Config);
  1354. return result;
  1355. }
  1356. };
  1357. //----------------------------------------------------------------------------
  1358. template<>
  1359. struct TargetFilesystemArtifactResultCreator<true, false>
  1360. {
  1361. static std::string Create(cmTarget* target,
  1362. cmGeneratorExpressionContext *context,
  1363. const GeneratorExpressionContent *content)
  1364. {
  1365. // The file used to link to the target (.so, .lib, .a).
  1366. if(!target->IsLinkable())
  1367. {
  1368. ::reportError(context, content->GetOriginalExpression(),
  1369. "TARGET_LINKER_FILE is allowed only for libraries and "
  1370. "executables with ENABLE_EXPORTS.");
  1371. return std::string();
  1372. }
  1373. return target->GetFullPath(context->Config,
  1374. target->HasImportLibrary());
  1375. }
  1376. };
  1377. //----------------------------------------------------------------------------
  1378. template<>
  1379. struct TargetFilesystemArtifactResultCreator<false, false>
  1380. {
  1381. static std::string Create(cmTarget* target,
  1382. cmGeneratorExpressionContext *context,
  1383. const GeneratorExpressionContent *)
  1384. {
  1385. return target->GetFullPath(context->Config, false, true);
  1386. }
  1387. };
  1388. //----------------------------------------------------------------------------
  1389. template<bool dirQual, bool nameQual>
  1390. struct TargetFilesystemArtifactResultGetter
  1391. {
  1392. static std::string Get(const std::string &result);
  1393. };
  1394. //----------------------------------------------------------------------------
  1395. template<>
  1396. struct TargetFilesystemArtifactResultGetter<false, true>
  1397. {
  1398. static std::string Get(const std::string &result)
  1399. { return cmSystemTools::GetFilenameName(result); }
  1400. };
  1401. //----------------------------------------------------------------------------
  1402. template<>
  1403. struct TargetFilesystemArtifactResultGetter<true, false>
  1404. {
  1405. static std::string Get(const std::string &result)
  1406. { return cmSystemTools::GetFilenamePath(result); }
  1407. };
  1408. //----------------------------------------------------------------------------
  1409. template<>
  1410. struct TargetFilesystemArtifactResultGetter<false, false>
  1411. {
  1412. static std::string Get(const std::string &result)
  1413. { return result; }
  1414. };
  1415. //----------------------------------------------------------------------------
  1416. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1417. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1418. {
  1419. TargetFilesystemArtifact() {}
  1420. virtual int NumExpectedParameters() const { return 1; }
  1421. std::string Evaluate(const std::vector<std::string> &parameters,
  1422. cmGeneratorExpressionContext *context,
  1423. const GeneratorExpressionContent *content,
  1424. cmGeneratorExpressionDAGChecker *dagChecker) const
  1425. {
  1426. // Lookup the referenced target.
  1427. std::string name = *parameters.begin();
  1428. if (!cmGeneratorExpression::IsValidTargetName(name))
  1429. {
  1430. ::reportError(context, content->GetOriginalExpression(),
  1431. "Expression syntax not recognized.");
  1432. return std::string();
  1433. }
  1434. cmTarget* target = context->Makefile->FindTargetToUse(name);
  1435. if(!target)
  1436. {
  1437. ::reportError(context, content->GetOriginalExpression(),
  1438. "No target \"" + name + "\"");
  1439. return std::string();
  1440. }
  1441. if(target->GetType() >= cmTarget::OBJECT_LIBRARY &&
  1442. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1443. {
  1444. ::reportError(context, content->GetOriginalExpression(),
  1445. "Target \"" + name + "\" is not an executable or library.");
  1446. return std::string();
  1447. }
  1448. if (dagChecker && (dagChecker->EvaluatingLinkLibraries(name.c_str())
  1449. || (dagChecker->EvaluatingSources()
  1450. && name == dagChecker->TopTarget())))
  1451. {
  1452. ::reportError(context, content->GetOriginalExpression(),
  1453. "Expressions which require the linker language may not "
  1454. "be used while evaluating link libraries");
  1455. return std::string();
  1456. }
  1457. context->DependTargets.insert(target);
  1458. context->AllTargets.insert(target);
  1459. std::string result =
  1460. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1461. target,
  1462. context,
  1463. content);
  1464. if (context->HadError)
  1465. {
  1466. return std::string();
  1467. }
  1468. return
  1469. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1470. }
  1471. };
  1472. //----------------------------------------------------------------------------
  1473. static const
  1474. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1475. static const
  1476. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1477. static const
  1478. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1479. static const
  1480. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1481. static const
  1482. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1483. static const
  1484. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1485. static const
  1486. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1487. static const
  1488. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1489. static const
  1490. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1491. //----------------------------------------------------------------------------
  1492. static const
  1493. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1494. {
  1495. typedef std::map<std::string, const cmGeneratorExpressionNode*> NodeMap;
  1496. static NodeMap nodeMap;
  1497. if (nodeMap.empty())
  1498. {
  1499. nodeMap["0"] = &zeroNode;
  1500. nodeMap["1"] = &oneNode;
  1501. nodeMap["AND"] = &andNode;
  1502. nodeMap["OR"] = &orNode;
  1503. nodeMap["NOT"] = &notNode;
  1504. nodeMap["C_COMPILER_ID"] = &cCompilerIdNode;
  1505. nodeMap["CXX_COMPILER_ID"] = &cxxCompilerIdNode;
  1506. nodeMap["VERSION_GREATER"] = &versionGreaterNode;
  1507. nodeMap["VERSION_LESS"] = &versionLessNode;
  1508. nodeMap["VERSION_EQUAL"] = &versionEqualNode;
  1509. nodeMap["C_COMPILER_VERSION"] = &cCompilerVersionNode;
  1510. nodeMap["CXX_COMPILER_VERSION"] = &cxxCompilerVersionNode;
  1511. nodeMap["PLATFORM_ID"] = &platformIdNode;
  1512. nodeMap["COMPILE_FEATURES"] = &compileFeaturesNode;
  1513. nodeMap["CONFIGURATION"] = &configurationNode;
  1514. nodeMap["CONFIG"] = &configurationTestNode;
  1515. nodeMap["TARGET_FILE"] = &targetFileNode;
  1516. nodeMap["TARGET_LINKER_FILE"] = &targetLinkerFileNode;
  1517. nodeMap["TARGET_SONAME_FILE"] = &targetSoNameFileNode;
  1518. nodeMap["TARGET_FILE_NAME"] = &targetFileNameNode;
  1519. nodeMap["TARGET_LINKER_FILE_NAME"] = &targetLinkerFileNameNode;
  1520. nodeMap["TARGET_SONAME_FILE_NAME"] = &targetSoNameFileNameNode;
  1521. nodeMap["TARGET_FILE_DIR"] = &targetFileDirNode;
  1522. nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerFileDirNode;
  1523. nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameFileDirNode;
  1524. nodeMap["STREQUAL"] = &strEqualNode;
  1525. nodeMap["EQUAL"] = &equalNode;
  1526. nodeMap["LOWER_CASE"] = &lowerCaseNode;
  1527. nodeMap["UPPER_CASE"] = &upperCaseNode;
  1528. nodeMap["MAKE_C_IDENTIFIER"] = &makeCIdentifierNode;
  1529. nodeMap["BOOL"] = &boolNode;
  1530. nodeMap["ANGLE-R"] = &angle_rNode;
  1531. nodeMap["COMMA"] = &commaNode;
  1532. nodeMap["SEMICOLON"] = &semicolonNode;
  1533. nodeMap["TARGET_PROPERTY"] = &targetPropertyNode;
  1534. nodeMap["TARGET_NAME"] = &targetNameNode;
  1535. nodeMap["TARGET_OBJECTS"] = &targetObjectsNode;
  1536. nodeMap["TARGET_POLICY"] = &targetPolicyNode;
  1537. nodeMap["BUILD_INTERFACE"] = &buildInterfaceNode;
  1538. nodeMap["INSTALL_INTERFACE"] = &installInterfaceNode;
  1539. nodeMap["INSTALL_PREFIX"] = &installPrefixNode;
  1540. nodeMap["JOIN"] = &joinNode;
  1541. nodeMap["LINK_ONLY"] = &linkOnlyNode;
  1542. }
  1543. NodeMap::const_iterator i = nodeMap.find(identifier);
  1544. if (i == nodeMap.end())
  1545. {
  1546. return 0;
  1547. }
  1548. return i->second;
  1549. }
  1550. //----------------------------------------------------------------------------
  1551. GeneratorExpressionContent::GeneratorExpressionContent(
  1552. const char *startContent,
  1553. size_t length)
  1554. : StartContent(startContent), ContentLength(length)
  1555. {
  1556. }
  1557. //----------------------------------------------------------------------------
  1558. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1559. {
  1560. return std::string(this->StartContent, this->ContentLength);
  1561. }
  1562. //----------------------------------------------------------------------------
  1563. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1564. const cmGeneratorExpressionNode *node,
  1565. const std::string &identifier,
  1566. cmGeneratorExpressionContext *context,
  1567. cmGeneratorExpressionDAGChecker *dagChecker,
  1568. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1569. pit) const
  1570. {
  1571. std::string result;
  1572. const
  1573. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1574. pend = this->ParamChildren.end();
  1575. for ( ; pit != pend; ++pit)
  1576. {
  1577. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1578. = pit->begin();
  1579. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1580. = pit->end();
  1581. for ( ; it != end; ++it)
  1582. {
  1583. if (node->RequiresLiteralInput())
  1584. {
  1585. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1586. {
  1587. reportError(context, this->GetOriginalExpression(),
  1588. "$<" + identifier + "> expression requires literal input.");
  1589. return std::string();
  1590. }
  1591. }
  1592. result += (*it)->Evaluate(context, dagChecker);
  1593. if (context->HadError)
  1594. {
  1595. return std::string();
  1596. }
  1597. }
  1598. if ((pit + 1) != pend)
  1599. {
  1600. result += ",";
  1601. }
  1602. }
  1603. if (node->RequiresLiteralInput())
  1604. {
  1605. std::vector<std::string> parameters;
  1606. parameters.push_back(result);
  1607. return node->Evaluate(parameters, context, this, dagChecker);
  1608. }
  1609. return result;
  1610. }
  1611. //----------------------------------------------------------------------------
  1612. std::string GeneratorExpressionContent::Evaluate(
  1613. cmGeneratorExpressionContext *context,
  1614. cmGeneratorExpressionDAGChecker *dagChecker) const
  1615. {
  1616. std::string identifier;
  1617. {
  1618. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1619. = this->IdentifierChildren.begin();
  1620. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1621. = this->IdentifierChildren.end();
  1622. for ( ; it != end; ++it)
  1623. {
  1624. identifier += (*it)->Evaluate(context, dagChecker);
  1625. if (context->HadError)
  1626. {
  1627. return std::string();
  1628. }
  1629. }
  1630. }
  1631. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1632. if (!node)
  1633. {
  1634. reportError(context, this->GetOriginalExpression(),
  1635. "Expression did not evaluate to a known generator expression");
  1636. return std::string();
  1637. }
  1638. if (!node->GeneratesContent())
  1639. {
  1640. if (node->NumExpectedParameters() == 1
  1641. && node->AcceptsArbitraryContentParameter())
  1642. {
  1643. if (this->ParamChildren.empty())
  1644. {
  1645. reportError(context, this->GetOriginalExpression(),
  1646. "$<" + identifier + "> expression requires a parameter.");
  1647. }
  1648. }
  1649. else
  1650. {
  1651. std::vector<std::string> parameters;
  1652. this->EvaluateParameters(node, identifier, context, dagChecker,
  1653. parameters);
  1654. }
  1655. return std::string();
  1656. }
  1657. std::vector<std::string> parameters;
  1658. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1659. if (context->HadError)
  1660. {
  1661. return std::string();
  1662. }
  1663. return node->Evaluate(parameters, context, this, dagChecker);
  1664. }
  1665. //----------------------------------------------------------------------------
  1666. std::string GeneratorExpressionContent::EvaluateParameters(
  1667. const cmGeneratorExpressionNode *node,
  1668. const std::string &identifier,
  1669. cmGeneratorExpressionContext *context,
  1670. cmGeneratorExpressionDAGChecker *dagChecker,
  1671. std::vector<std::string> &parameters) const
  1672. {
  1673. const int numExpected = node->NumExpectedParameters();
  1674. {
  1675. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1676. pit = this->ParamChildren.begin();
  1677. const
  1678. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1679. pend = this->ParamChildren.end();
  1680. const bool acceptsArbitraryContent
  1681. = node->AcceptsArbitraryContentParameter();
  1682. int counter = 1;
  1683. for ( ; pit != pend; ++pit, ++counter)
  1684. {
  1685. if (acceptsArbitraryContent && counter == numExpected)
  1686. {
  1687. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1688. context,
  1689. dagChecker,
  1690. pit);
  1691. parameters.push_back(lastParam);
  1692. return std::string();
  1693. }
  1694. else
  1695. {
  1696. std::string parameter;
  1697. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1698. pit->begin();
  1699. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1700. pit->end();
  1701. for ( ; it != end; ++it)
  1702. {
  1703. parameter += (*it)->Evaluate(context, dagChecker);
  1704. if (context->HadError)
  1705. {
  1706. return std::string();
  1707. }
  1708. }
  1709. parameters.push_back(parameter);
  1710. }
  1711. }
  1712. }
  1713. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1714. && (unsigned int)numExpected != parameters.size()))
  1715. {
  1716. if (numExpected == 0)
  1717. {
  1718. reportError(context, this->GetOriginalExpression(),
  1719. "$<" + identifier + "> expression requires no parameters.");
  1720. }
  1721. else if (numExpected == 1)
  1722. {
  1723. reportError(context, this->GetOriginalExpression(),
  1724. "$<" + identifier + "> expression requires "
  1725. "exactly one parameter.");
  1726. }
  1727. else
  1728. {
  1729. cmOStringStream e;
  1730. e << "$<" + identifier + "> expression requires "
  1731. << numExpected
  1732. << " comma separated parameters, but got "
  1733. << parameters.size() << " instead.";
  1734. reportError(context, this->GetOriginalExpression(), e.str());
  1735. }
  1736. return std::string();
  1737. }
  1738. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1739. && parameters.empty())
  1740. {
  1741. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1742. + "> expression requires at least one parameter.");
  1743. }
  1744. if (numExpected == cmGeneratorExpressionNode::OneOrZeroParameters
  1745. && parameters.size() > 1)
  1746. {
  1747. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1748. + "> expression requires one or zero parameters.");
  1749. }
  1750. return std::string();
  1751. }
  1752. //----------------------------------------------------------------------------
  1753. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1754. {
  1755. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1756. = c.begin();
  1757. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1758. = c.end();
  1759. for ( ; it != end; ++it)
  1760. {
  1761. delete *it;
  1762. }
  1763. }
  1764. //----------------------------------------------------------------------------
  1765. GeneratorExpressionContent::~GeneratorExpressionContent()
  1766. {
  1767. deleteAll(this->IdentifierChildren);
  1768. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1769. std::vector<EvaluatorVector>::const_iterator pit =
  1770. this->ParamChildren.begin();
  1771. const std::vector<EvaluatorVector>::const_iterator pend =
  1772. this->ParamChildren.end();
  1773. for ( ; pit != pend; ++pit)
  1774. {
  1775. deleteAll(*pit);
  1776. }
  1777. }