cmGeneratorExpressionEvaluator.cxx 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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 <cmsys/String.h>
  16. #include <assert.h>
  17. //----------------------------------------------------------------------------
  18. #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x510
  19. static
  20. #endif
  21. void reportError(cmGeneratorExpressionContext *context,
  22. const std::string &expr, const std::string &result)
  23. {
  24. context->HadError = true;
  25. if (context->Quiet)
  26. {
  27. return;
  28. }
  29. cmOStringStream e;
  30. e << "Error evaluating generator expression:\n"
  31. << " " << expr << "\n"
  32. << result;
  33. context->Makefile->GetCMakeInstance()
  34. ->IssueMessage(cmake::FATAL_ERROR, e.str().c_str(),
  35. context->Backtrace);
  36. }
  37. //----------------------------------------------------------------------------
  38. struct cmGeneratorExpressionNode
  39. {
  40. enum {
  41. DynamicParameters = 0,
  42. OneOrMoreParameters = -1,
  43. ZeroOrMoreParameters = -2
  44. };
  45. virtual ~cmGeneratorExpressionNode() {}
  46. virtual bool GeneratesContent() const { return true; }
  47. virtual bool RequiresLiteralInput() const { return false; }
  48. virtual bool AcceptsArbitraryContentParameter() const
  49. { return false; }
  50. virtual int NumExpectedParameters() const { return 1; }
  51. virtual std::string Evaluate(const std::vector<std::string> &parameters,
  52. cmGeneratorExpressionContext *context,
  53. const GeneratorExpressionContent *content,
  54. cmGeneratorExpressionDAGChecker *dagChecker
  55. ) const = 0;
  56. };
  57. //----------------------------------------------------------------------------
  58. static const struct ZeroNode : public cmGeneratorExpressionNode
  59. {
  60. ZeroNode() {}
  61. virtual bool GeneratesContent() const { return false; }
  62. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  63. std::string Evaluate(const std::vector<std::string> &,
  64. cmGeneratorExpressionContext *,
  65. const GeneratorExpressionContent *,
  66. cmGeneratorExpressionDAGChecker *) const
  67. {
  68. // Unreachable
  69. return std::string();
  70. }
  71. } zeroNode;
  72. //----------------------------------------------------------------------------
  73. static const struct OneNode : public cmGeneratorExpressionNode
  74. {
  75. OneNode() {}
  76. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  77. std::string Evaluate(const std::vector<std::string> &,
  78. cmGeneratorExpressionContext *,
  79. const GeneratorExpressionContent *,
  80. cmGeneratorExpressionDAGChecker *) const
  81. {
  82. // Unreachable
  83. return std::string();
  84. }
  85. } oneNode;
  86. //----------------------------------------------------------------------------
  87. static const struct OneNode buildInterfaceNode;
  88. //----------------------------------------------------------------------------
  89. static const struct ZeroNode installInterfaceNode;
  90. //----------------------------------------------------------------------------
  91. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  92. static const struct OP ## Node : public cmGeneratorExpressionNode \
  93. { \
  94. OP ## Node () {} \
  95. virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
  96. \
  97. std::string Evaluate(const std::vector<std::string> &parameters, \
  98. cmGeneratorExpressionContext *context, \
  99. const GeneratorExpressionContent *content, \
  100. cmGeneratorExpressionDAGChecker *) const \
  101. { \
  102. std::vector<std::string>::const_iterator it = parameters.begin(); \
  103. const std::vector<std::string>::const_iterator end = parameters.end(); \
  104. for ( ; it != end; ++it) \
  105. { \
  106. if (*it == #FAILURE_VALUE) \
  107. { \
  108. return #FAILURE_VALUE; \
  109. } \
  110. else if (*it != #SUCCESS_VALUE) \
  111. { \
  112. reportError(context, content->GetOriginalExpression(), \
  113. "Parameters to $<" #OP "> must resolve to either '0' or '1'."); \
  114. return std::string(); \
  115. } \
  116. } \
  117. return #SUCCESS_VALUE; \
  118. } \
  119. } OPNAME;
  120. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  121. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  122. #undef BOOLEAN_OP_NODE
  123. //----------------------------------------------------------------------------
  124. static const struct NotNode : public cmGeneratorExpressionNode
  125. {
  126. NotNode() {}
  127. std::string Evaluate(const std::vector<std::string> &parameters,
  128. cmGeneratorExpressionContext *context,
  129. const GeneratorExpressionContent *content,
  130. cmGeneratorExpressionDAGChecker *) const
  131. {
  132. if (*parameters.begin() != "0" && *parameters.begin() != "1")
  133. {
  134. reportError(context, content->GetOriginalExpression(),
  135. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  136. return std::string();
  137. }
  138. return *parameters.begin() == "0" ? "1" : "0";
  139. }
  140. } notNode;
  141. //----------------------------------------------------------------------------
  142. static const struct BoolNode : public cmGeneratorExpressionNode
  143. {
  144. BoolNode() {}
  145. virtual int NumExpectedParameters() const { return 1; }
  146. std::string Evaluate(const std::vector<std::string> &parameters,
  147. cmGeneratorExpressionContext *,
  148. const GeneratorExpressionContent *,
  149. cmGeneratorExpressionDAGChecker *) const
  150. {
  151. return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  152. }
  153. } boolNode;
  154. //----------------------------------------------------------------------------
  155. static const struct StrEqualNode : public cmGeneratorExpressionNode
  156. {
  157. StrEqualNode() {}
  158. virtual int NumExpectedParameters() const { return 2; }
  159. std::string Evaluate(const std::vector<std::string> &parameters,
  160. cmGeneratorExpressionContext *,
  161. const GeneratorExpressionContent *,
  162. cmGeneratorExpressionDAGChecker *) const
  163. {
  164. return *parameters.begin() == parameters[1] ? "1" : "0";
  165. }
  166. } strEqualNode;
  167. //----------------------------------------------------------------------------
  168. static const struct Angle_RNode : public cmGeneratorExpressionNode
  169. {
  170. Angle_RNode() {}
  171. virtual int NumExpectedParameters() const { return 0; }
  172. std::string Evaluate(const std::vector<std::string> &,
  173. cmGeneratorExpressionContext *,
  174. const GeneratorExpressionContent *,
  175. cmGeneratorExpressionDAGChecker *) const
  176. {
  177. return ">";
  178. }
  179. } angle_rNode;
  180. //----------------------------------------------------------------------------
  181. static const struct CommaNode : public cmGeneratorExpressionNode
  182. {
  183. CommaNode() {}
  184. virtual int NumExpectedParameters() const { return 0; }
  185. std::string Evaluate(const std::vector<std::string> &,
  186. cmGeneratorExpressionContext *,
  187. const GeneratorExpressionContent *,
  188. cmGeneratorExpressionDAGChecker *) const
  189. {
  190. return ",";
  191. }
  192. } commaNode;
  193. //----------------------------------------------------------------------------
  194. static const struct SemicolonNode : public cmGeneratorExpressionNode
  195. {
  196. SemicolonNode() {}
  197. virtual int NumExpectedParameters() const { return 0; }
  198. std::string Evaluate(const std::vector<std::string> &,
  199. cmGeneratorExpressionContext *,
  200. const GeneratorExpressionContent *,
  201. cmGeneratorExpressionDAGChecker *) const
  202. {
  203. return ";";
  204. }
  205. } semicolonNode;
  206. //----------------------------------------------------------------------------
  207. struct CompilerIdNode : public cmGeneratorExpressionNode
  208. {
  209. CompilerIdNode() {}
  210. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  211. std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
  212. cmGeneratorExpressionContext *context,
  213. const GeneratorExpressionContent *content,
  214. cmGeneratorExpressionDAGChecker *,
  215. const std::string &lang) const
  216. {
  217. const char *compilerId = context->Makefile ?
  218. context->Makefile->GetSafeDefinition((
  219. "CMAKE_" + lang + "_COMPILER_ID").c_str()) : "";
  220. if (parameters.size() == 0)
  221. {
  222. return compilerId ? compilerId : "";
  223. }
  224. cmsys::RegularExpression compilerIdValidator;
  225. compilerIdValidator.compile("^[A-Za-z0-9_]*$");
  226. if (!compilerIdValidator.find(parameters.begin()->c_str()))
  227. {
  228. reportError(context, content->GetOriginalExpression(),
  229. "Expression syntax not recognized.");
  230. return std::string();
  231. }
  232. if (!compilerId)
  233. {
  234. return parameters.front().empty() ? "1" : "0";
  235. }
  236. if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0)
  237. {
  238. return "1";
  239. }
  240. return "0";
  241. }
  242. };
  243. //----------------------------------------------------------------------------
  244. static const struct CCompilerIdNode : public CompilerIdNode
  245. {
  246. CCompilerIdNode() {}
  247. std::string Evaluate(const std::vector<std::string> &parameters,
  248. cmGeneratorExpressionContext *context,
  249. const GeneratorExpressionContent *content,
  250. cmGeneratorExpressionDAGChecker *dagChecker) const
  251. {
  252. if (parameters.size() != 0 && parameters.size() != 1)
  253. {
  254. reportError(context, content->GetOriginalExpression(),
  255. "$<C_COMPILER_ID> expression requires one or two parameters");
  256. return std::string();
  257. }
  258. if (!context->HeadTarget)
  259. {
  260. reportError(context, content->GetOriginalExpression(),
  261. "$<C_COMPILER_ID> may only be used with targets. It may not "
  262. "be used with add_custom_command.");
  263. }
  264. return this->EvaluateWithLanguage(parameters, context, content,
  265. dagChecker, "C");
  266. }
  267. } cCompilerIdNode;
  268. //----------------------------------------------------------------------------
  269. static const struct CXXCompilerIdNode : public CompilerIdNode
  270. {
  271. CXXCompilerIdNode() {}
  272. std::string Evaluate(const std::vector<std::string> &parameters,
  273. cmGeneratorExpressionContext *context,
  274. const GeneratorExpressionContent *content,
  275. cmGeneratorExpressionDAGChecker *dagChecker) const
  276. {
  277. if (parameters.size() != 0 && parameters.size() != 1)
  278. {
  279. reportError(context, content->GetOriginalExpression(),
  280. "$<CXX_COMPILER_ID> expression requires one or two parameters");
  281. return std::string();
  282. }
  283. if (!context->HeadTarget)
  284. {
  285. reportError(context, content->GetOriginalExpression(),
  286. "$<CXX_COMPILER_ID> may only be used with targets. It may not "
  287. "be used with add_custom_command.");
  288. }
  289. return this->EvaluateWithLanguage(parameters, context, content,
  290. dagChecker, "CXX");
  291. }
  292. } cxxCompilerIdNode;
  293. //----------------------------------------------------------------------------
  294. struct CompilerVersionNode : public cmGeneratorExpressionNode
  295. {
  296. CompilerVersionNode() {}
  297. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  298. std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
  299. cmGeneratorExpressionContext *context,
  300. const GeneratorExpressionContent *content,
  301. cmGeneratorExpressionDAGChecker *,
  302. const std::string &lang) const
  303. {
  304. const char *compilerVersion = context->Makefile ?
  305. context->Makefile->GetSafeDefinition((
  306. "CMAKE_" + lang + "_COMPILER_VERSION").c_str()) : "";
  307. if (parameters.size() == 0)
  308. {
  309. return compilerVersion ? compilerVersion : "";
  310. }
  311. cmsys::RegularExpression compilerIdValidator;
  312. compilerIdValidator.compile("^[0-9\\.]*$");
  313. if (!compilerIdValidator.find(parameters.begin()->c_str()))
  314. {
  315. reportError(context, content->GetOriginalExpression(),
  316. "Expression syntax not recognized.");
  317. return std::string();
  318. }
  319. if (!compilerVersion)
  320. {
  321. return parameters.front().empty() ? "1" : "0";
  322. }
  323. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  324. parameters.begin()->c_str(),
  325. compilerVersion) ? "1" : "0";
  326. }
  327. };
  328. //----------------------------------------------------------------------------
  329. static const struct CCompilerVersionNode : public CompilerVersionNode
  330. {
  331. CCompilerVersionNode() {}
  332. std::string Evaluate(const std::vector<std::string> &parameters,
  333. cmGeneratorExpressionContext *context,
  334. const GeneratorExpressionContent *content,
  335. cmGeneratorExpressionDAGChecker *dagChecker) const
  336. {
  337. if (parameters.size() != 0 && parameters.size() != 1)
  338. {
  339. reportError(context, content->GetOriginalExpression(),
  340. "$<C_COMPILER_VERSION> expression requires one or two parameters");
  341. return std::string();
  342. }
  343. if (!context->HeadTarget)
  344. {
  345. reportError(context, content->GetOriginalExpression(),
  346. "$<C_COMPILER_VERSION> may only be used with targets. It may not "
  347. "be used with add_custom_command.");
  348. }
  349. return this->EvaluateWithLanguage(parameters, context, content,
  350. dagChecker, "C");
  351. }
  352. } cCompilerVersionNode;
  353. //----------------------------------------------------------------------------
  354. static const struct CxxCompilerVersionNode : public CompilerVersionNode
  355. {
  356. CxxCompilerVersionNode() {}
  357. std::string Evaluate(const std::vector<std::string> &parameters,
  358. cmGeneratorExpressionContext *context,
  359. const GeneratorExpressionContent *content,
  360. cmGeneratorExpressionDAGChecker *dagChecker) const
  361. {
  362. if (parameters.size() != 0 && parameters.size() != 1)
  363. {
  364. reportError(context, content->GetOriginalExpression(),
  365. "$<CXX_COMPILER_VERSION> expression requires one or two "
  366. "parameters");
  367. return std::string();
  368. }
  369. if (!context->HeadTarget)
  370. {
  371. reportError(context, content->GetOriginalExpression(),
  372. "$<CXX_COMPILER_VERSION> may only be used with targets. It may "
  373. "not be used with add_custom_command.");
  374. }
  375. return this->EvaluateWithLanguage(parameters, context, content,
  376. dagChecker, "CXX");
  377. }
  378. } cxxCompilerVersionNode;
  379. //----------------------------------------------------------------------------
  380. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  381. {
  382. VersionGreaterNode() {}
  383. virtual int NumExpectedParameters() const { return 2; }
  384. std::string Evaluate(const std::vector<std::string> &parameters,
  385. cmGeneratorExpressionContext *,
  386. const GeneratorExpressionContent *,
  387. cmGeneratorExpressionDAGChecker *) const
  388. {
  389. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  390. parameters.front().c_str(),
  391. parameters[1].c_str()) ? "1" : "0";
  392. }
  393. } versionGreaterNode;
  394. //----------------------------------------------------------------------------
  395. static const struct VersionLessNode : public cmGeneratorExpressionNode
  396. {
  397. VersionLessNode() {}
  398. virtual int NumExpectedParameters() const { return 2; }
  399. std::string Evaluate(const std::vector<std::string> &parameters,
  400. cmGeneratorExpressionContext *,
  401. const GeneratorExpressionContent *,
  402. cmGeneratorExpressionDAGChecker *) const
  403. {
  404. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  405. parameters.front().c_str(),
  406. parameters[1].c_str()) ? "1" : "0";
  407. }
  408. } versionLessNode;
  409. //----------------------------------------------------------------------------
  410. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  411. {
  412. VersionEqualNode() {}
  413. virtual int NumExpectedParameters() const { return 2; }
  414. std::string Evaluate(const std::vector<std::string> &parameters,
  415. cmGeneratorExpressionContext *,
  416. const GeneratorExpressionContent *,
  417. cmGeneratorExpressionDAGChecker *) const
  418. {
  419. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  420. parameters.front().c_str(),
  421. parameters[1].c_str()) ? "1" : "0";
  422. }
  423. } versionEqualNode;
  424. //----------------------------------------------------------------------------
  425. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  426. {
  427. ConfigurationNode() {}
  428. virtual int NumExpectedParameters() const { return 0; }
  429. std::string Evaluate(const std::vector<std::string> &,
  430. cmGeneratorExpressionContext *context,
  431. const GeneratorExpressionContent *,
  432. cmGeneratorExpressionDAGChecker *) const
  433. {
  434. context->HadContextSensitiveCondition = true;
  435. return context->Config ? context->Config : "";
  436. }
  437. } configurationNode;
  438. //----------------------------------------------------------------------------
  439. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  440. {
  441. ConfigurationTestNode() {}
  442. virtual int NumExpectedParameters() const { return 1; }
  443. std::string Evaluate(const std::vector<std::string> &parameters,
  444. cmGeneratorExpressionContext *context,
  445. const GeneratorExpressionContent *content,
  446. cmGeneratorExpressionDAGChecker *) const
  447. {
  448. cmsys::RegularExpression configValidator;
  449. configValidator.compile("^[A-Za-z0-9_]*$");
  450. if (!configValidator.find(parameters.begin()->c_str()))
  451. {
  452. reportError(context, content->GetOriginalExpression(),
  453. "Expression syntax not recognized.");
  454. return std::string();
  455. }
  456. context->HadContextSensitiveCondition = true;
  457. if (!context->Config)
  458. {
  459. return parameters.front().empty() ? "1" : "0";
  460. }
  461. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  462. context->Config) == 0)
  463. {
  464. return "1";
  465. }
  466. if (context->CurrentTarget
  467. && context->CurrentTarget->IsImported())
  468. {
  469. const char* loc = 0;
  470. const char* imp = 0;
  471. std::string suffix;
  472. return context->CurrentTarget->GetMappedConfig(context->Config,
  473. &loc,
  474. &imp,
  475. suffix) ? "1" : "0";
  476. }
  477. return "0";
  478. }
  479. } configurationTestNode;
  480. //----------------------------------------------------------------------------
  481. static const struct LinkLanguageNode : public cmGeneratorExpressionNode
  482. {
  483. LinkLanguageNode() {}
  484. virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }
  485. std::string Evaluate(const std::vector<std::string> &parameters,
  486. cmGeneratorExpressionContext *context,
  487. const GeneratorExpressionContent *content,
  488. cmGeneratorExpressionDAGChecker *dagChecker) const
  489. {
  490. if (dagChecker && dagChecker->EvaluatingLinkLibraries())
  491. {
  492. reportError(context, content->GetOriginalExpression(),
  493. "$<LINK_LANGUAGE> expression can not be used while evaluating "
  494. "link libraries");
  495. return std::string();
  496. }
  497. if (parameters.size() != 0 && parameters.size() != 1)
  498. {
  499. reportError(context, content->GetOriginalExpression(),
  500. "$<LINK_LANGUAGE> expression requires one or two parameters");
  501. return std::string();
  502. }
  503. cmTarget* target = context->HeadTarget;
  504. if (!target)
  505. {
  506. reportError(context, content->GetOriginalExpression(),
  507. "$<LINK_LANGUAGE> may only be used with targets. It may not "
  508. "be used with add_custom_command.");
  509. return std::string();
  510. }
  511. const char *lang = target->GetLinkerLanguage(context->Config);
  512. if (parameters.size() == 0)
  513. {
  514. return lang ? lang : "";
  515. }
  516. else
  517. {
  518. cmsys::RegularExpression langValidator;
  519. langValidator.compile("^[A-Za-z0-9_]*$");
  520. if (!langValidator.find(parameters.begin()->c_str()))
  521. {
  522. reportError(context, content->GetOriginalExpression(),
  523. "Expression syntax not recognized.");
  524. return std::string();
  525. }
  526. if (!lang)
  527. {
  528. return parameters.front().empty() ? "1" : "0";
  529. }
  530. if (strcmp(parameters.begin()->c_str(), lang) == 0)
  531. {
  532. return "1";
  533. }
  534. return "0";
  535. }
  536. }
  537. } linkLanguageNode;
  538. static const struct JoinNode : public cmGeneratorExpressionNode
  539. {
  540. JoinNode() {}
  541. virtual int NumExpectedParameters() const { return 2; }
  542. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  543. std::string Evaluate(const std::vector<std::string> &parameters,
  544. cmGeneratorExpressionContext *,
  545. const GeneratorExpressionContent *,
  546. cmGeneratorExpressionDAGChecker *) const
  547. {
  548. std::string result;
  549. std::vector<std::string> list;
  550. cmSystemTools::ExpandListArgument(parameters.front(), list);
  551. std::string sep;
  552. for(std::vector<std::string>::const_iterator li = list.begin();
  553. li != list.end(); ++li)
  554. {
  555. result += sep + *li;
  556. sep = parameters[1];
  557. }
  558. return result;
  559. }
  560. } joinNode;
  561. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) \
  562. , #PROPERTY
  563. //----------------------------------------------------------------------------
  564. static const char* targetPropertyTransitiveWhitelist[] = {
  565. 0
  566. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  567. };
  568. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  569. cmTarget *target,
  570. cmTarget *headTarget,
  571. cmGeneratorExpressionContext *context,
  572. cmGeneratorExpressionDAGChecker *dagChecker,
  573. const std::string &interfacePropertyName)
  574. {
  575. cmGeneratorExpression ge(context->Backtrace);
  576. std::string sep;
  577. std::string depString;
  578. for (std::vector<std::string>::const_iterator
  579. it = libraries.begin();
  580. it != libraries.end(); ++it)
  581. {
  582. if (*it == target->GetName())
  583. {
  584. // Broken code can have a target in its own link interface.
  585. // Don't follow such link interface entries so as not to create a
  586. // self-referencing loop.
  587. continue;
  588. }
  589. if (context->Makefile->FindTargetToUse(it->c_str()))
  590. {
  591. depString +=
  592. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  593. sep = ";";
  594. }
  595. }
  596. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  597. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  598. context->Config,
  599. context->Quiet,
  600. headTarget,
  601. target,
  602. dagChecker);
  603. if (cge->GetHadContextSensitiveCondition())
  604. {
  605. context->HadContextSensitiveCondition = true;
  606. }
  607. return linkedTargetsContent;
  608. }
  609. //----------------------------------------------------------------------------
  610. struct TransitiveWhitelistCompare
  611. {
  612. explicit TransitiveWhitelistCompare(const std::string &needle)
  613. : Needle(needle) {}
  614. bool operator() (const char *item)
  615. { return strcmp(item, this->Needle.c_str()) == 0; }
  616. private:
  617. std::string Needle;
  618. };
  619. //----------------------------------------------------------------------------
  620. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  621. {
  622. TargetPropertyNode() {}
  623. // This node handles errors on parameter count itself.
  624. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  625. std::string Evaluate(const std::vector<std::string> &parameters,
  626. cmGeneratorExpressionContext *context,
  627. const GeneratorExpressionContent *content,
  628. cmGeneratorExpressionDAGChecker *dagCheckerParent
  629. ) const
  630. {
  631. if (parameters.size() != 1 && parameters.size() != 2)
  632. {
  633. reportError(context, content->GetOriginalExpression(),
  634. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  635. return std::string();
  636. }
  637. cmsys::RegularExpression propertyNameValidator;
  638. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  639. cmTarget* target = context->HeadTarget;
  640. std::string propertyName = *parameters.begin();
  641. if (!target && parameters.size() == 1)
  642. {
  643. reportError(context, content->GetOriginalExpression(),
  644. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  645. "be used with add_custom_command. Specify the target to read a "
  646. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  647. "instead.");
  648. return std::string();
  649. }
  650. if (parameters.size() == 2)
  651. {
  652. if (parameters.begin()->empty() && parameters[1].empty())
  653. {
  654. reportError(context, content->GetOriginalExpression(),
  655. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  656. "target name and property name.");
  657. return std::string();
  658. }
  659. if (parameters.begin()->empty())
  660. {
  661. reportError(context, content->GetOriginalExpression(),
  662. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  663. "target name.");
  664. return std::string();
  665. }
  666. std::string targetName = parameters.front();
  667. propertyName = parameters[1];
  668. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  669. {
  670. if (!propertyNameValidator.find(propertyName.c_str()))
  671. {
  672. ::reportError(context, content->GetOriginalExpression(),
  673. "Target name and property name not supported.");
  674. return std::string();
  675. }
  676. ::reportError(context, content->GetOriginalExpression(),
  677. "Target name not supported.");
  678. return std::string();
  679. }
  680. target = context->Makefile->FindTargetToUse(
  681. targetName.c_str());
  682. if (!target)
  683. {
  684. cmOStringStream e;
  685. e << "Target \""
  686. << targetName
  687. << "\" not found.";
  688. reportError(context, content->GetOriginalExpression(), e.str());
  689. return std::string();
  690. }
  691. context->AllTargets.insert(target);
  692. }
  693. if (target == context->HeadTarget)
  694. {
  695. // Keep track of the properties seen while processing.
  696. // The evaluation of the LINK_LIBRARIES generator expressions
  697. // will check this to ensure that properties have one consistent
  698. // value for all evaluations.
  699. context->SeenTargetProperties.insert(propertyName);
  700. }
  701. if (propertyName.empty())
  702. {
  703. reportError(context, content->GetOriginalExpression(),
  704. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  705. "name.");
  706. return std::string();
  707. }
  708. if (!propertyNameValidator.find(propertyName.c_str()))
  709. {
  710. ::reportError(context, content->GetOriginalExpression(),
  711. "Property name not supported.");
  712. return std::string();
  713. }
  714. assert(target);
  715. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  716. target->GetName(),
  717. propertyName,
  718. content,
  719. dagCheckerParent);
  720. switch (dagChecker.check())
  721. {
  722. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  723. dagChecker.reportError(context, content->GetOriginalExpression());
  724. return std::string();
  725. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  726. // No error. We just skip cyclic references.
  727. return std::string();
  728. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  729. for (size_t i = 1;
  730. i < (sizeof(targetPropertyTransitiveWhitelist) /
  731. sizeof(*targetPropertyTransitiveWhitelist));
  732. ++i)
  733. {
  734. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  735. {
  736. // No error. We're not going to find anything new here.
  737. return std::string();
  738. }
  739. }
  740. case cmGeneratorExpressionDAGChecker::DAG:
  741. break;
  742. }
  743. const char *prop = target->GetProperty(propertyName.c_str());
  744. if (dagCheckerParent)
  745. {
  746. if (dagCheckerParent->EvaluatingLinkLibraries())
  747. {
  748. if(!prop)
  749. {
  750. return std::string();
  751. }
  752. }
  753. else
  754. {
  755. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  756. dagCheckerParent->METHOD () ||
  757. assert(
  758. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  759. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  760. false);
  761. }
  762. }
  763. std::string linkedTargetsContent;
  764. std::string interfacePropertyName;
  765. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  766. || propertyName == "INCLUDE_DIRECTORIES")
  767. {
  768. interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
  769. }
  770. else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
  771. || propertyName == "COMPILE_DEFINITIONS"
  772. || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
  773. {
  774. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  775. }
  776. else if (propertyName == "INTERFACE_COMPILE_OPTIONS"
  777. || propertyName == "COMPILE_OPTIONS")
  778. {
  779. interfacePropertyName = "INTERFACE_COMPILE_OPTIONS";
  780. }
  781. cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;
  782. const char **transBegin = targetPropertyTransitiveWhitelist + 1;
  783. const char **transEnd = targetPropertyTransitiveWhitelist
  784. + (sizeof(targetPropertyTransitiveWhitelist) /
  785. sizeof(*targetPropertyTransitiveWhitelist));
  786. if (std::find_if(transBegin, transEnd,
  787. TransitiveWhitelistCompare(propertyName)) != transEnd)
  788. {
  789. std::vector<std::string> libs;
  790. target->GetTransitivePropertyLinkLibraries(context->Config,
  791. headTarget, libs);
  792. if (!libs.empty())
  793. {
  794. linkedTargetsContent =
  795. getLinkedTargetsContent(libs, target,
  796. headTarget,
  797. context, &dagChecker,
  798. interfacePropertyName);
  799. }
  800. }
  801. else if (std::find_if(transBegin, transEnd,
  802. TransitiveWhitelistCompare(interfacePropertyName)) != transEnd)
  803. {
  804. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  805. context->Config,
  806. headTarget);
  807. if(impl)
  808. {
  809. linkedTargetsContent =
  810. getLinkedTargetsContent(impl->Libraries, target,
  811. headTarget,
  812. context, &dagChecker,
  813. interfacePropertyName);
  814. }
  815. }
  816. linkedTargetsContent =
  817. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  818. if (!prop)
  819. {
  820. if (target->IsImported())
  821. {
  822. return linkedTargetsContent;
  823. }
  824. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  825. context->Config))
  826. {
  827. context->HadContextSensitiveCondition = true;
  828. return target->GetLinkInterfaceDependentBoolProperty(
  829. propertyName,
  830. context->Config) ? "1" : "0";
  831. }
  832. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  833. context->Config))
  834. {
  835. context->HadContextSensitiveCondition = true;
  836. const char *propContent =
  837. target->GetLinkInterfaceDependentStringProperty(
  838. propertyName,
  839. context->Config);
  840. return propContent ? propContent : "";
  841. }
  842. return linkedTargetsContent;
  843. }
  844. for (size_t i = 1;
  845. i < (sizeof(targetPropertyTransitiveWhitelist) /
  846. sizeof(*targetPropertyTransitiveWhitelist));
  847. ++i)
  848. {
  849. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  850. {
  851. cmGeneratorExpression ge(context->Backtrace);
  852. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  853. std::string result = cge->Evaluate(context->Makefile,
  854. context->Config,
  855. context->Quiet,
  856. headTarget,
  857. target,
  858. &dagChecker);
  859. if (cge->GetHadContextSensitiveCondition())
  860. {
  861. context->HadContextSensitiveCondition = true;
  862. }
  863. if (!linkedTargetsContent.empty())
  864. {
  865. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  866. }
  867. return result;
  868. }
  869. }
  870. return prop;
  871. }
  872. } targetPropertyNode;
  873. //----------------------------------------------------------------------------
  874. static const struct TargetNameNode : public cmGeneratorExpressionNode
  875. {
  876. TargetNameNode() {}
  877. virtual bool GeneratesContent() const { return true; }
  878. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  879. virtual bool RequiresLiteralInput() const { return true; }
  880. std::string Evaluate(const std::vector<std::string> &parameters,
  881. cmGeneratorExpressionContext *,
  882. const GeneratorExpressionContent *,
  883. cmGeneratorExpressionDAGChecker *) const
  884. {
  885. return parameters.front();
  886. }
  887. virtual int NumExpectedParameters() const { return 1; }
  888. } targetNameNode;
  889. //----------------------------------------------------------------------------
  890. static const char* targetPolicyWhitelist[] = {
  891. "CMP0003"
  892. , "CMP0004"
  893. , "CMP0008"
  894. , "CMP0020"
  895. };
  896. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  897. {
  898. #define RETURN_POLICY(POLICY) \
  899. if (strcmp(policy, #POLICY) == 0) \
  900. { \
  901. return tgt->GetPolicyStatus ## POLICY (); \
  902. } \
  903. RETURN_POLICY(CMP0003)
  904. RETURN_POLICY(CMP0004)
  905. RETURN_POLICY(CMP0008)
  906. RETURN_POLICY(CMP0020)
  907. #undef RETURN_POLICY
  908. assert("!Unreachable code. Not a valid policy");
  909. return cmPolicies::WARN;
  910. }
  911. cmPolicies::PolicyID policyForString(const char *policy_id)
  912. {
  913. #define RETURN_POLICY_ID(POLICY_ID) \
  914. if (strcmp(policy_id, #POLICY_ID) == 0) \
  915. { \
  916. return cmPolicies:: POLICY_ID; \
  917. } \
  918. RETURN_POLICY_ID(CMP0003)
  919. RETURN_POLICY_ID(CMP0004)
  920. RETURN_POLICY_ID(CMP0008)
  921. RETURN_POLICY_ID(CMP0020)
  922. #undef RETURN_POLICY_ID
  923. assert("!Unreachable code. Not a valid policy");
  924. return cmPolicies::CMP0002;
  925. }
  926. //----------------------------------------------------------------------------
  927. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  928. {
  929. TargetPolicyNode() {}
  930. virtual int NumExpectedParameters() const { return 1; }
  931. std::string Evaluate(const std::vector<std::string> &parameters,
  932. cmGeneratorExpressionContext *context ,
  933. const GeneratorExpressionContent *content,
  934. cmGeneratorExpressionDAGChecker *) const
  935. {
  936. if (!context->HeadTarget)
  937. {
  938. reportError(context, content->GetOriginalExpression(),
  939. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  940. "be used with add_custom_command.");
  941. return std::string();
  942. }
  943. context->HadContextSensitiveCondition = true;
  944. for (size_t i = 0;
  945. i < (sizeof(targetPolicyWhitelist) /
  946. sizeof(*targetPolicyWhitelist));
  947. ++i)
  948. {
  949. const char *policy = targetPolicyWhitelist[i];
  950. if (parameters.front() == policy)
  951. {
  952. cmMakefile *mf = context->HeadTarget->GetMakefile();
  953. switch(statusForTarget(context->HeadTarget, policy))
  954. {
  955. case cmPolicies::WARN:
  956. mf->IssueMessage(cmake::AUTHOR_WARNING,
  957. mf->GetPolicies()->
  958. GetPolicyWarning(policyForString(policy)));
  959. case cmPolicies::REQUIRED_IF_USED:
  960. case cmPolicies::REQUIRED_ALWAYS:
  961. case cmPolicies::OLD:
  962. return "0";
  963. case cmPolicies::NEW:
  964. return "1";
  965. }
  966. }
  967. }
  968. reportError(context, content->GetOriginalExpression(),
  969. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  970. "policies. Currently it may be used with policies CMP0003, CMP0004, "
  971. "CMP0008 and CMP0020."
  972. );
  973. return std::string();
  974. }
  975. } targetPolicyNode;
  976. //----------------------------------------------------------------------------
  977. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  978. {
  979. InstallPrefixNode() {}
  980. virtual bool GeneratesContent() const { return true; }
  981. virtual int NumExpectedParameters() const { return 0; }
  982. std::string Evaluate(const std::vector<std::string> &,
  983. cmGeneratorExpressionContext *context,
  984. const GeneratorExpressionContent *content,
  985. cmGeneratorExpressionDAGChecker *) const
  986. {
  987. reportError(context, content->GetOriginalExpression(),
  988. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  989. "should never be evaluated.");
  990. return std::string();
  991. }
  992. } installPrefixNode;
  993. //----------------------------------------------------------------------------
  994. template<bool linker, bool soname>
  995. struct TargetFilesystemArtifactResultCreator
  996. {
  997. static std::string Create(cmTarget* target,
  998. cmGeneratorExpressionContext *context,
  999. const GeneratorExpressionContent *content);
  1000. };
  1001. //----------------------------------------------------------------------------
  1002. template<>
  1003. struct TargetFilesystemArtifactResultCreator<false, true>
  1004. {
  1005. static std::string Create(cmTarget* target,
  1006. cmGeneratorExpressionContext *context,
  1007. const GeneratorExpressionContent *content)
  1008. {
  1009. // The target soname file (.so.1).
  1010. if(target->IsDLLPlatform())
  1011. {
  1012. ::reportError(context, content->GetOriginalExpression(),
  1013. "TARGET_SONAME_FILE is not allowed "
  1014. "for DLL target platforms.");
  1015. return std::string();
  1016. }
  1017. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1018. {
  1019. ::reportError(context, content->GetOriginalExpression(),
  1020. "TARGET_SONAME_FILE is allowed only for "
  1021. "SHARED libraries.");
  1022. return std::string();
  1023. }
  1024. std::string result = target->GetDirectory(context->Config);
  1025. result += "/";
  1026. result += target->GetSOName(context->Config);
  1027. return result;
  1028. }
  1029. };
  1030. //----------------------------------------------------------------------------
  1031. template<>
  1032. struct TargetFilesystemArtifactResultCreator<true, false>
  1033. {
  1034. static std::string Create(cmTarget* target,
  1035. cmGeneratorExpressionContext *context,
  1036. const GeneratorExpressionContent *content)
  1037. {
  1038. // The file used to link to the target (.so, .lib, .a).
  1039. if(!target->IsLinkable())
  1040. {
  1041. ::reportError(context, content->GetOriginalExpression(),
  1042. "TARGET_LINKER_FILE is allowed only for libraries and "
  1043. "executables with ENABLE_EXPORTS.");
  1044. return std::string();
  1045. }
  1046. return target->GetFullPath(context->Config,
  1047. target->HasImportLibrary());
  1048. }
  1049. };
  1050. //----------------------------------------------------------------------------
  1051. template<>
  1052. struct TargetFilesystemArtifactResultCreator<false, false>
  1053. {
  1054. static std::string Create(cmTarget* target,
  1055. cmGeneratorExpressionContext *context,
  1056. const GeneratorExpressionContent *)
  1057. {
  1058. return target->GetFullPath(context->Config, false, true);
  1059. }
  1060. };
  1061. //----------------------------------------------------------------------------
  1062. template<bool dirQual, bool nameQual>
  1063. struct TargetFilesystemArtifactResultGetter
  1064. {
  1065. static std::string Get(const std::string &result);
  1066. };
  1067. //----------------------------------------------------------------------------
  1068. template<>
  1069. struct TargetFilesystemArtifactResultGetter<false, true>
  1070. {
  1071. static std::string Get(const std::string &result)
  1072. { return cmSystemTools::GetFilenameName(result); }
  1073. };
  1074. //----------------------------------------------------------------------------
  1075. template<>
  1076. struct TargetFilesystemArtifactResultGetter<true, false>
  1077. {
  1078. static std::string Get(const std::string &result)
  1079. { return cmSystemTools::GetFilenamePath(result); }
  1080. };
  1081. //----------------------------------------------------------------------------
  1082. template<>
  1083. struct TargetFilesystemArtifactResultGetter<false, false>
  1084. {
  1085. static std::string Get(const std::string &result)
  1086. { return result; }
  1087. };
  1088. //----------------------------------------------------------------------------
  1089. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1090. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1091. {
  1092. TargetFilesystemArtifact() {}
  1093. virtual int NumExpectedParameters() const { return 1; }
  1094. std::string Evaluate(const std::vector<std::string> &parameters,
  1095. cmGeneratorExpressionContext *context,
  1096. const GeneratorExpressionContent *content,
  1097. cmGeneratorExpressionDAGChecker *dagChecker) const
  1098. {
  1099. // Lookup the referenced target.
  1100. std::string name = *parameters.begin();
  1101. if (!cmGeneratorExpression::IsValidTargetName(name))
  1102. {
  1103. ::reportError(context, content->GetOriginalExpression(),
  1104. "Expression syntax not recognized.");
  1105. return std::string();
  1106. }
  1107. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1108. if(!target)
  1109. {
  1110. ::reportError(context, content->GetOriginalExpression(),
  1111. "No target \"" + name + "\"");
  1112. return std::string();
  1113. }
  1114. if(target->GetType() >= cmTarget::UTILITY &&
  1115. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1116. {
  1117. ::reportError(context, content->GetOriginalExpression(),
  1118. "Target \"" + name + "\" is not an executable or library.");
  1119. return std::string();
  1120. }
  1121. if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
  1122. {
  1123. ::reportError(context, content->GetOriginalExpression(),
  1124. "Expressions which require the linker language may not "
  1125. "be used while evaluating link libraries");
  1126. return std::string();
  1127. }
  1128. context->DependTargets.insert(target);
  1129. context->AllTargets.insert(target);
  1130. std::string result =
  1131. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1132. target,
  1133. context,
  1134. content);
  1135. if (context->HadError)
  1136. {
  1137. return std::string();
  1138. }
  1139. return
  1140. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1141. }
  1142. };
  1143. //----------------------------------------------------------------------------
  1144. static const
  1145. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1146. static const
  1147. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1148. static const
  1149. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1150. static const
  1151. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1152. static const
  1153. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1154. static const
  1155. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1156. static const
  1157. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1158. static const
  1159. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1160. static const
  1161. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1162. //----------------------------------------------------------------------------
  1163. static const
  1164. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1165. {
  1166. if (identifier == "0")
  1167. return &zeroNode;
  1168. else if (identifier == "1")
  1169. return &oneNode;
  1170. else if (identifier == "AND")
  1171. return &andNode;
  1172. else if (identifier == "OR")
  1173. return &orNode;
  1174. else if (identifier == "NOT")
  1175. return &notNode;
  1176. else if (identifier == "C_COMPILER_ID")
  1177. return &cCompilerIdNode;
  1178. else if (identifier == "CXX_COMPILER_ID")
  1179. return &cxxCompilerIdNode;
  1180. else if (identifier == "VERSION_GREATER")
  1181. return &versionGreaterNode;
  1182. else if (identifier == "VERSION_LESS")
  1183. return &versionLessNode;
  1184. else if (identifier == "VERSION_EQUAL")
  1185. return &versionEqualNode;
  1186. else if (identifier == "C_COMPILER_VERSION")
  1187. return &cCompilerVersionNode;
  1188. else if (identifier == "CXX_COMPILER_VERSION")
  1189. return &cxxCompilerVersionNode;
  1190. else if (identifier == "CONFIGURATION")
  1191. return &configurationNode;
  1192. else if (identifier == "CONFIG")
  1193. return &configurationTestNode;
  1194. else if (identifier == "LINK_LANGUAGE")
  1195. return &linkLanguageNode;
  1196. else if (identifier == "TARGET_FILE")
  1197. return &targetFileNode;
  1198. else if (identifier == "TARGET_LINKER_FILE")
  1199. return &targetLinkerFileNode;
  1200. else if (identifier == "TARGET_SONAME_FILE")
  1201. return &targetSoNameFileNode;
  1202. else if (identifier == "TARGET_FILE_NAME")
  1203. return &targetFileNameNode;
  1204. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1205. return &targetLinkerFileNameNode;
  1206. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1207. return &targetSoNameFileNameNode;
  1208. else if (identifier == "TARGET_FILE_DIR")
  1209. return &targetFileDirNode;
  1210. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1211. return &targetLinkerFileDirNode;
  1212. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1213. return &targetSoNameFileDirNode;
  1214. else if (identifier == "STREQUAL")
  1215. return &strEqualNode;
  1216. else if (identifier == "BOOL")
  1217. return &boolNode;
  1218. else if (identifier == "ANGLE-R")
  1219. return &angle_rNode;
  1220. else if (identifier == "COMMA")
  1221. return &commaNode;
  1222. else if (identifier == "SEMICOLON")
  1223. return &semicolonNode;
  1224. else if (identifier == "TARGET_PROPERTY")
  1225. return &targetPropertyNode;
  1226. else if (identifier == "TARGET_NAME")
  1227. return &targetNameNode;
  1228. else if (identifier == "TARGET_POLICY")
  1229. return &targetPolicyNode;
  1230. else if (identifier == "BUILD_INTERFACE")
  1231. return &buildInterfaceNode;
  1232. else if (identifier == "INSTALL_INTERFACE")
  1233. return &installInterfaceNode;
  1234. else if (identifier == "INSTALL_PREFIX")
  1235. return &installPrefixNode;
  1236. else if (identifier == "JOIN")
  1237. return &joinNode;
  1238. return 0;
  1239. }
  1240. //----------------------------------------------------------------------------
  1241. GeneratorExpressionContent::GeneratorExpressionContent(
  1242. const char *startContent,
  1243. unsigned int length)
  1244. : StartContent(startContent), ContentLength(length)
  1245. {
  1246. }
  1247. //----------------------------------------------------------------------------
  1248. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1249. {
  1250. return std::string(this->StartContent, this->ContentLength);
  1251. }
  1252. //----------------------------------------------------------------------------
  1253. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1254. const cmGeneratorExpressionNode *node,
  1255. const std::string &identifier,
  1256. cmGeneratorExpressionContext *context,
  1257. cmGeneratorExpressionDAGChecker *dagChecker,
  1258. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1259. pit) const
  1260. {
  1261. std::string result;
  1262. const
  1263. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1264. pend = this->ParamChildren.end();
  1265. for ( ; pit != pend; ++pit)
  1266. {
  1267. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1268. = pit->begin();
  1269. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1270. = pit->end();
  1271. for ( ; it != end; ++it)
  1272. {
  1273. if (node->RequiresLiteralInput())
  1274. {
  1275. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1276. {
  1277. reportError(context, this->GetOriginalExpression(),
  1278. "$<" + identifier + "> expression requires literal input.");
  1279. return std::string();
  1280. }
  1281. }
  1282. result += (*it)->Evaluate(context, dagChecker);
  1283. if (context->HadError)
  1284. {
  1285. return std::string();
  1286. }
  1287. }
  1288. if ((pit + 1) != pend)
  1289. {
  1290. result += ",";
  1291. }
  1292. }
  1293. if (node->RequiresLiteralInput())
  1294. {
  1295. std::vector<std::string> parameters;
  1296. parameters.push_back(result);
  1297. return node->Evaluate(parameters, context, this, dagChecker);
  1298. }
  1299. return result;
  1300. }
  1301. //----------------------------------------------------------------------------
  1302. std::string GeneratorExpressionContent::Evaluate(
  1303. cmGeneratorExpressionContext *context,
  1304. cmGeneratorExpressionDAGChecker *dagChecker) const
  1305. {
  1306. std::string identifier;
  1307. {
  1308. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1309. = this->IdentifierChildren.begin();
  1310. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1311. = this->IdentifierChildren.end();
  1312. for ( ; it != end; ++it)
  1313. {
  1314. identifier += (*it)->Evaluate(context, dagChecker);
  1315. if (context->HadError)
  1316. {
  1317. return std::string();
  1318. }
  1319. }
  1320. }
  1321. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1322. if (!node)
  1323. {
  1324. reportError(context, this->GetOriginalExpression(),
  1325. "Expression did not evaluate to a known generator expression");
  1326. return std::string();
  1327. }
  1328. if (!node->GeneratesContent())
  1329. {
  1330. if (node->NumExpectedParameters() == 1
  1331. && node->AcceptsArbitraryContentParameter())
  1332. {
  1333. if (this->ParamChildren.empty())
  1334. {
  1335. reportError(context, this->GetOriginalExpression(),
  1336. "$<" + identifier + "> expression requires a parameter.");
  1337. }
  1338. }
  1339. else
  1340. {
  1341. std::vector<std::string> parameters;
  1342. this->EvaluateParameters(node, identifier, context, dagChecker,
  1343. parameters);
  1344. }
  1345. return std::string();
  1346. }
  1347. if (node->NumExpectedParameters() == 1
  1348. && node->AcceptsArbitraryContentParameter())
  1349. {
  1350. return this->ProcessArbitraryContent(node, identifier, context,
  1351. dagChecker,
  1352. this->ParamChildren.begin());
  1353. }
  1354. std::vector<std::string> parameters;
  1355. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1356. if (context->HadError)
  1357. {
  1358. return std::string();
  1359. }
  1360. return node->Evaluate(parameters, context, this, dagChecker);
  1361. }
  1362. //----------------------------------------------------------------------------
  1363. std::string GeneratorExpressionContent::EvaluateParameters(
  1364. const cmGeneratorExpressionNode *node,
  1365. const std::string &identifier,
  1366. cmGeneratorExpressionContext *context,
  1367. cmGeneratorExpressionDAGChecker *dagChecker,
  1368. std::vector<std::string> &parameters) const
  1369. {
  1370. const int numExpected = node->NumExpectedParameters();
  1371. {
  1372. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1373. pit = this->ParamChildren.begin();
  1374. const
  1375. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1376. pend = this->ParamChildren.end();
  1377. const bool acceptsArbitraryContent
  1378. = node->AcceptsArbitraryContentParameter();
  1379. for ( ; pit != pend; ++pit)
  1380. {
  1381. std::string parameter;
  1382. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1383. pit->begin();
  1384. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1385. pit->end();
  1386. for ( ; it != end; ++it)
  1387. {
  1388. parameter += (*it)->Evaluate(context, dagChecker);
  1389. if (context->HadError)
  1390. {
  1391. return std::string();
  1392. }
  1393. }
  1394. parameters.push_back(parameter);
  1395. if (acceptsArbitraryContent
  1396. && parameters.size() == (unsigned int)numExpected - 1)
  1397. {
  1398. assert(pit != pend);
  1399. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1400. context,
  1401. dagChecker,
  1402. pit + 1);
  1403. parameters.push_back(lastParam);
  1404. return std::string();
  1405. }
  1406. }
  1407. }
  1408. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1409. && (unsigned int)numExpected != parameters.size()))
  1410. {
  1411. if (numExpected == 0)
  1412. {
  1413. reportError(context, this->GetOriginalExpression(),
  1414. "$<" + identifier + "> expression requires no parameters.");
  1415. }
  1416. else if (numExpected == 1)
  1417. {
  1418. reportError(context, this->GetOriginalExpression(),
  1419. "$<" + identifier + "> expression requires "
  1420. "exactly one parameter.");
  1421. }
  1422. else
  1423. {
  1424. cmOStringStream e;
  1425. e << "$<" + identifier + "> expression requires "
  1426. << numExpected
  1427. << " comma separated parameters, but got "
  1428. << parameters.size() << " instead.";
  1429. reportError(context, this->GetOriginalExpression(), e.str());
  1430. }
  1431. return std::string();
  1432. }
  1433. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1434. && parameters.empty())
  1435. {
  1436. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1437. + "> expression requires at least one parameter.");
  1438. }
  1439. return std::string();
  1440. }
  1441. //----------------------------------------------------------------------------
  1442. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1443. {
  1444. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1445. = c.begin();
  1446. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1447. = c.end();
  1448. for ( ; it != end; ++it)
  1449. {
  1450. delete *it;
  1451. }
  1452. }
  1453. //----------------------------------------------------------------------------
  1454. GeneratorExpressionContent::~GeneratorExpressionContent()
  1455. {
  1456. deleteAll(this->IdentifierChildren);
  1457. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1458. std::vector<EvaluatorVector>::const_iterator pit =
  1459. this->ParamChildren.begin();
  1460. const std::vector<EvaluatorVector>::const_iterator pend =
  1461. this->ParamChildren.end();
  1462. for ( ; pit != pend; ++pit)
  1463. {
  1464. deleteAll(*pit);
  1465. }
  1466. }