cmGeneratorExpressionEvaluator.cxx 70 KB

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