cmGeneratorExpressionEvaluator.cxx 73 KB

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