cmGeneratorExpressionEvaluator.cxx 70 KB

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