cmGeneratorExpressionEvaluator.cxx 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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 LinkOnlyNode : public cmGeneratorExpressionNode
  426. {
  427. LinkOnlyNode() {}
  428. std::string Evaluate(const std::vector<std::string> &parameters,
  429. cmGeneratorExpressionContext *,
  430. const GeneratorExpressionContent *,
  431. cmGeneratorExpressionDAGChecker *dagChecker) const
  432. {
  433. if(!dagChecker->GetTransitivePropertiesOnly())
  434. {
  435. return parameters.front();
  436. }
  437. return "";
  438. }
  439. } linkOnlyNode;
  440. //----------------------------------------------------------------------------
  441. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  442. {
  443. ConfigurationNode() {}
  444. virtual int NumExpectedParameters() const { return 0; }
  445. std::string Evaluate(const std::vector<std::string> &,
  446. cmGeneratorExpressionContext *context,
  447. const GeneratorExpressionContent *,
  448. cmGeneratorExpressionDAGChecker *) const
  449. {
  450. context->HadContextSensitiveCondition = true;
  451. return context->Config ? context->Config : "";
  452. }
  453. } configurationNode;
  454. //----------------------------------------------------------------------------
  455. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  456. {
  457. ConfigurationTestNode() {}
  458. virtual int NumExpectedParameters() const { return 1; }
  459. std::string Evaluate(const std::vector<std::string> &parameters,
  460. cmGeneratorExpressionContext *context,
  461. const GeneratorExpressionContent *content,
  462. cmGeneratorExpressionDAGChecker *) const
  463. {
  464. cmsys::RegularExpression configValidator;
  465. configValidator.compile("^[A-Za-z0-9_]*$");
  466. if (!configValidator.find(parameters.begin()->c_str()))
  467. {
  468. reportError(context, content->GetOriginalExpression(),
  469. "Expression syntax not recognized.");
  470. return std::string();
  471. }
  472. context->HadContextSensitiveCondition = true;
  473. if (!context->Config)
  474. {
  475. return parameters.front().empty() ? "1" : "0";
  476. }
  477. if (cmsysString_strcasecmp(parameters.begin()->c_str(),
  478. context->Config) == 0)
  479. {
  480. return "1";
  481. }
  482. if (context->CurrentTarget
  483. && context->CurrentTarget->IsImported())
  484. {
  485. const char* loc = 0;
  486. const char* imp = 0;
  487. std::string suffix;
  488. if (context->CurrentTarget->GetMappedConfig(context->Config,
  489. &loc,
  490. &imp,
  491. suffix))
  492. {
  493. // This imported target has an appropriate location
  494. // for this (possibly mapped) config.
  495. // Check if there is a proper config mapping for the tested config.
  496. std::vector<std::string> mappedConfigs;
  497. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  498. mapProp += context->Config;
  499. if(const char* mapValue =
  500. context->CurrentTarget->GetProperty(mapProp.c_str()))
  501. {
  502. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  503. mappedConfigs);
  504. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  505. context->Config) != mappedConfigs.end() ? "1" : "0";
  506. }
  507. }
  508. }
  509. return "0";
  510. }
  511. } configurationTestNode;
  512. static const struct JoinNode : public cmGeneratorExpressionNode
  513. {
  514. JoinNode() {}
  515. virtual int NumExpectedParameters() const { return 2; }
  516. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  517. std::string Evaluate(const std::vector<std::string> &parameters,
  518. cmGeneratorExpressionContext *,
  519. const GeneratorExpressionContent *,
  520. cmGeneratorExpressionDAGChecker *) const
  521. {
  522. std::string result;
  523. std::vector<std::string> list;
  524. cmSystemTools::ExpandListArgument(parameters.front(), list);
  525. std::string sep;
  526. for(std::vector<std::string>::const_iterator li = list.begin();
  527. li != list.end(); ++li)
  528. {
  529. result += sep + *li;
  530. sep = parameters[1];
  531. }
  532. return result;
  533. }
  534. } joinNode;
  535. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) \
  536. , #PROPERTY
  537. //----------------------------------------------------------------------------
  538. static const char* targetPropertyTransitiveWhitelist[] = {
  539. 0
  540. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  541. };
  542. std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
  543. cmTarget *target,
  544. cmTarget *headTarget,
  545. cmGeneratorExpressionContext *context,
  546. cmGeneratorExpressionDAGChecker *dagChecker,
  547. const std::string &interfacePropertyName)
  548. {
  549. cmGeneratorExpression ge(context->Backtrace);
  550. std::string sep;
  551. std::string depString;
  552. for (std::vector<std::string>::const_iterator
  553. it = libraries.begin();
  554. it != libraries.end(); ++it)
  555. {
  556. if (*it == target->GetName())
  557. {
  558. // Broken code can have a target in its own link interface.
  559. // Don't follow such link interface entries so as not to create a
  560. // self-referencing loop.
  561. continue;
  562. }
  563. if (context->Makefile->FindTargetToUse(it->c_str()))
  564. {
  565. depString +=
  566. sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
  567. sep = ";";
  568. }
  569. }
  570. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  571. std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
  572. context->Config,
  573. context->Quiet,
  574. headTarget,
  575. target,
  576. dagChecker);
  577. if (cge->GetHadContextSensitiveCondition())
  578. {
  579. context->HadContextSensitiveCondition = true;
  580. }
  581. return linkedTargetsContent;
  582. }
  583. //----------------------------------------------------------------------------
  584. struct TransitiveWhitelistCompare
  585. {
  586. explicit TransitiveWhitelistCompare(const std::string &needle)
  587. : Needle(needle) {}
  588. bool operator() (const char *item)
  589. { return strcmp(item, this->Needle.c_str()) == 0; }
  590. private:
  591. std::string Needle;
  592. };
  593. //----------------------------------------------------------------------------
  594. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  595. {
  596. TargetPropertyNode() {}
  597. // This node handles errors on parameter count itself.
  598. virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
  599. std::string Evaluate(const std::vector<std::string> &parameters,
  600. cmGeneratorExpressionContext *context,
  601. const GeneratorExpressionContent *content,
  602. cmGeneratorExpressionDAGChecker *dagCheckerParent
  603. ) const
  604. {
  605. if (parameters.size() != 1 && parameters.size() != 2)
  606. {
  607. reportError(context, content->GetOriginalExpression(),
  608. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  609. return std::string();
  610. }
  611. cmsys::RegularExpression propertyNameValidator;
  612. propertyNameValidator.compile("^[A-Za-z0-9_]+$");
  613. cmTarget* target = context->HeadTarget;
  614. std::string propertyName = *parameters.begin();
  615. if (!target && parameters.size() == 1)
  616. {
  617. reportError(context, content->GetOriginalExpression(),
  618. "$<TARGET_PROPERTY:prop> may only be used with targets. It may not "
  619. "be used with add_custom_command. Specify the target to read a "
  620. "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
  621. "instead.");
  622. return std::string();
  623. }
  624. if (parameters.size() == 2)
  625. {
  626. if (parameters.begin()->empty() && parameters[1].empty())
  627. {
  628. reportError(context, content->GetOriginalExpression(),
  629. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  630. "target name and property name.");
  631. return std::string();
  632. }
  633. if (parameters.begin()->empty())
  634. {
  635. reportError(context, content->GetOriginalExpression(),
  636. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  637. "target name.");
  638. return std::string();
  639. }
  640. std::string targetName = parameters.front();
  641. propertyName = parameters[1];
  642. if (!cmGeneratorExpression::IsValidTargetName(targetName))
  643. {
  644. if (!propertyNameValidator.find(propertyName.c_str()))
  645. {
  646. ::reportError(context, content->GetOriginalExpression(),
  647. "Target name and property name not supported.");
  648. return std::string();
  649. }
  650. ::reportError(context, content->GetOriginalExpression(),
  651. "Target name not supported.");
  652. return std::string();
  653. }
  654. target = context->Makefile->FindTargetToUse(
  655. targetName.c_str());
  656. if (!target)
  657. {
  658. cmOStringStream e;
  659. e << "Target \""
  660. << targetName
  661. << "\" not found.";
  662. reportError(context, content->GetOriginalExpression(), e.str());
  663. return std::string();
  664. }
  665. context->AllTargets.insert(target);
  666. }
  667. if (target == context->HeadTarget)
  668. {
  669. // Keep track of the properties seen while processing.
  670. // The evaluation of the LINK_LIBRARIES generator expressions
  671. // will check this to ensure that properties have one consistent
  672. // value for all evaluations.
  673. context->SeenTargetProperties.insert(propertyName);
  674. }
  675. if (propertyName.empty())
  676. {
  677. reportError(context, content->GetOriginalExpression(),
  678. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  679. "name.");
  680. return std::string();
  681. }
  682. if (!propertyNameValidator.find(propertyName.c_str()))
  683. {
  684. ::reportError(context, content->GetOriginalExpression(),
  685. "Property name not supported.");
  686. return std::string();
  687. }
  688. assert(target);
  689. if (propertyName == "LINKER_LANGUAGE")
  690. {
  691. if (target->LinkLanguagePropagatesToDependents() &&
  692. dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries())
  693. {
  694. reportError(context, content->GetOriginalExpression(),
  695. "LINKER_LANGUAGE target property can not be used while evaluating "
  696. "link libraries for a static library");
  697. return std::string();
  698. }
  699. const char *lang = target->GetLinkerLanguage(context->Config);
  700. return lang ? lang : "";
  701. }
  702. cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
  703. target->GetName(),
  704. propertyName,
  705. content,
  706. dagCheckerParent);
  707. switch (dagChecker.check())
  708. {
  709. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  710. dagChecker.reportError(context, content->GetOriginalExpression());
  711. return std::string();
  712. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  713. // No error. We just skip cyclic references.
  714. return std::string();
  715. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  716. for (size_t i = 1;
  717. i < (sizeof(targetPropertyTransitiveWhitelist) /
  718. sizeof(*targetPropertyTransitiveWhitelist));
  719. ++i)
  720. {
  721. if (targetPropertyTransitiveWhitelist[i] == propertyName)
  722. {
  723. // No error. We're not going to find anything new here.
  724. return std::string();
  725. }
  726. }
  727. case cmGeneratorExpressionDAGChecker::DAG:
  728. break;
  729. }
  730. const char *prop = target->GetProperty(propertyName.c_str());
  731. if (dagCheckerParent)
  732. {
  733. if (dagCheckerParent->EvaluatingLinkLibraries())
  734. {
  735. if(!prop)
  736. {
  737. return std::string();
  738. }
  739. }
  740. else
  741. {
  742. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  743. dagCheckerParent->METHOD () ||
  744. assert(
  745. CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  746. ASSERT_TRANSITIVE_PROPERTY_METHOD)
  747. false);
  748. }
  749. }
  750. std::string linkedTargetsContent;
  751. std::string interfacePropertyName;
  752. if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
  753. || propertyName == "INCLUDE_DIRECTORIES")
  754. {
  755. interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
  756. }
  757. else if (propertyName == "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")
  758. {
  759. interfacePropertyName = "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES";
  760. }
  761. else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
  762. || propertyName == "COMPILE_DEFINITIONS"
  763. || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
  764. {
  765. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  766. }
  767. else if (propertyName == "INTERFACE_COMPILE_OPTIONS"
  768. || propertyName == "COMPILE_OPTIONS")
  769. {
  770. interfacePropertyName = "INTERFACE_COMPILE_OPTIONS";
  771. }
  772. cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;
  773. const char **transBegin = targetPropertyTransitiveWhitelist + 1;
  774. const char **transEnd = targetPropertyTransitiveWhitelist
  775. + (sizeof(targetPropertyTransitiveWhitelist) /
  776. sizeof(*targetPropertyTransitiveWhitelist));
  777. if (std::find_if(transBegin, transEnd,
  778. TransitiveWhitelistCompare(propertyName)) != transEnd)
  779. {
  780. std::vector<std::string> libs;
  781. target->GetTransitivePropertyLinkLibraries(context->Config,
  782. headTarget, libs);
  783. if (!libs.empty())
  784. {
  785. linkedTargetsContent =
  786. getLinkedTargetsContent(libs, target,
  787. headTarget,
  788. context, &dagChecker,
  789. interfacePropertyName);
  790. }
  791. }
  792. else if (std::find_if(transBegin, transEnd,
  793. TransitiveWhitelistCompare(interfacePropertyName)) != transEnd)
  794. {
  795. const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
  796. context->Config,
  797. headTarget);
  798. if(impl)
  799. {
  800. linkedTargetsContent =
  801. getLinkedTargetsContent(impl->Libraries, target,
  802. headTarget,
  803. context, &dagChecker,
  804. interfacePropertyName);
  805. }
  806. }
  807. linkedTargetsContent =
  808. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  809. if (!prop)
  810. {
  811. if (target->IsImported())
  812. {
  813. return linkedTargetsContent;
  814. }
  815. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  816. context->Config))
  817. {
  818. context->HadContextSensitiveCondition = true;
  819. return target->GetLinkInterfaceDependentBoolProperty(
  820. propertyName,
  821. context->Config) ? "1" : "0";
  822. }
  823. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  824. context->Config))
  825. {
  826. context->HadContextSensitiveCondition = true;
  827. const char *propContent =
  828. target->GetLinkInterfaceDependentStringProperty(
  829. propertyName,
  830. context->Config);
  831. return propContent ? propContent : "";
  832. }
  833. return linkedTargetsContent;
  834. }
  835. for (size_t i = 1;
  836. i < (sizeof(targetPropertyTransitiveWhitelist) /
  837. sizeof(*targetPropertyTransitiveWhitelist));
  838. ++i)
  839. {
  840. if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
  841. {
  842. cmGeneratorExpression ge(context->Backtrace);
  843. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  844. std::string result = cge->Evaluate(context->Makefile,
  845. context->Config,
  846. context->Quiet,
  847. headTarget,
  848. target,
  849. &dagChecker);
  850. if (cge->GetHadContextSensitiveCondition())
  851. {
  852. context->HadContextSensitiveCondition = true;
  853. }
  854. if (!linkedTargetsContent.empty())
  855. {
  856. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  857. }
  858. return result;
  859. }
  860. }
  861. return prop;
  862. }
  863. } targetPropertyNode;
  864. //----------------------------------------------------------------------------
  865. static const struct TargetNameNode : public cmGeneratorExpressionNode
  866. {
  867. TargetNameNode() {}
  868. virtual bool GeneratesContent() const { return true; }
  869. virtual bool AcceptsArbitraryContentParameter() const { return true; }
  870. virtual bool RequiresLiteralInput() const { return true; }
  871. std::string Evaluate(const std::vector<std::string> &parameters,
  872. cmGeneratorExpressionContext *,
  873. const GeneratorExpressionContent *,
  874. cmGeneratorExpressionDAGChecker *) const
  875. {
  876. return parameters.front();
  877. }
  878. virtual int NumExpectedParameters() const { return 1; }
  879. } targetNameNode;
  880. //----------------------------------------------------------------------------
  881. static const char* targetPolicyWhitelist[] = {
  882. 0
  883. #define TARGET_POLICY_STRING(POLICY) \
  884. , #POLICY
  885. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  886. #undef TARGET_POLICY_STRING
  887. };
  888. cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
  889. {
  890. #define RETURN_POLICY(POLICY) \
  891. if (strcmp(policy, #POLICY) == 0) \
  892. { \
  893. return tgt->GetPolicyStatus ## POLICY (); \
  894. } \
  895. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  896. #undef RETURN_POLICY
  897. assert("!Unreachable code. Not a valid policy");
  898. return cmPolicies::WARN;
  899. }
  900. cmPolicies::PolicyID policyForString(const char *policy_id)
  901. {
  902. #define RETURN_POLICY_ID(POLICY_ID) \
  903. if (strcmp(policy_id, #POLICY_ID) == 0) \
  904. { \
  905. return cmPolicies:: POLICY_ID; \
  906. } \
  907. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  908. #undef RETURN_POLICY_ID
  909. assert("!Unreachable code. Not a valid policy");
  910. return cmPolicies::CMP0002;
  911. }
  912. //----------------------------------------------------------------------------
  913. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  914. {
  915. TargetPolicyNode() {}
  916. virtual int NumExpectedParameters() const { return 1; }
  917. std::string Evaluate(const std::vector<std::string> &parameters,
  918. cmGeneratorExpressionContext *context ,
  919. const GeneratorExpressionContent *content,
  920. cmGeneratorExpressionDAGChecker *) const
  921. {
  922. if (!context->HeadTarget)
  923. {
  924. reportError(context, content->GetOriginalExpression(),
  925. "$<TARGET_POLICY:prop> may only be used with targets. It may not "
  926. "be used with add_custom_command.");
  927. return std::string();
  928. }
  929. context->HadContextSensitiveCondition = true;
  930. for (size_t i = 1;
  931. i < (sizeof(targetPolicyWhitelist) /
  932. sizeof(*targetPolicyWhitelist));
  933. ++i)
  934. {
  935. const char *policy = targetPolicyWhitelist[i];
  936. if (parameters.front() == policy)
  937. {
  938. cmMakefile *mf = context->HeadTarget->GetMakefile();
  939. switch(statusForTarget(context->HeadTarget, policy))
  940. {
  941. case cmPolicies::WARN:
  942. mf->IssueMessage(cmake::AUTHOR_WARNING,
  943. mf->GetPolicies()->
  944. GetPolicyWarning(policyForString(policy)));
  945. case cmPolicies::REQUIRED_IF_USED:
  946. case cmPolicies::REQUIRED_ALWAYS:
  947. case cmPolicies::OLD:
  948. return "0";
  949. case cmPolicies::NEW:
  950. return "1";
  951. }
  952. }
  953. }
  954. reportError(context, content->GetOriginalExpression(),
  955. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  956. "policies. Currently it may be used with the following policies:\n"
  957. #define STRINGIFY_HELPER(X) #X
  958. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  959. #define TARGET_POLICY_LIST_ITEM(POLICY) \
  960. " * " STRINGIFY(POLICY) "\n"
  961. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  962. #undef TARGET_POLICY_LIST_ITEM
  963. );
  964. return std::string();
  965. }
  966. } targetPolicyNode;
  967. //----------------------------------------------------------------------------
  968. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  969. {
  970. InstallPrefixNode() {}
  971. virtual bool GeneratesContent() const { return true; }
  972. virtual int NumExpectedParameters() const { return 0; }
  973. std::string Evaluate(const std::vector<std::string> &,
  974. cmGeneratorExpressionContext *context,
  975. const GeneratorExpressionContent *content,
  976. cmGeneratorExpressionDAGChecker *) const
  977. {
  978. reportError(context, content->GetOriginalExpression(),
  979. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  980. "should never be evaluated.");
  981. return std::string();
  982. }
  983. } installPrefixNode;
  984. //----------------------------------------------------------------------------
  985. template<bool linker, bool soname>
  986. struct TargetFilesystemArtifactResultCreator
  987. {
  988. static std::string Create(cmTarget* target,
  989. cmGeneratorExpressionContext *context,
  990. const GeneratorExpressionContent *content);
  991. };
  992. //----------------------------------------------------------------------------
  993. template<>
  994. struct TargetFilesystemArtifactResultCreator<false, true>
  995. {
  996. static std::string Create(cmTarget* target,
  997. cmGeneratorExpressionContext *context,
  998. const GeneratorExpressionContent *content)
  999. {
  1000. // The target soname file (.so.1).
  1001. if(target->IsDLLPlatform())
  1002. {
  1003. ::reportError(context, content->GetOriginalExpression(),
  1004. "TARGET_SONAME_FILE is not allowed "
  1005. "for DLL target platforms.");
  1006. return std::string();
  1007. }
  1008. if(target->GetType() != cmTarget::SHARED_LIBRARY)
  1009. {
  1010. ::reportError(context, content->GetOriginalExpression(),
  1011. "TARGET_SONAME_FILE is allowed only for "
  1012. "SHARED libraries.");
  1013. return std::string();
  1014. }
  1015. std::string result = target->GetDirectory(context->Config);
  1016. result += "/";
  1017. result += target->GetSOName(context->Config);
  1018. return result;
  1019. }
  1020. };
  1021. //----------------------------------------------------------------------------
  1022. template<>
  1023. struct TargetFilesystemArtifactResultCreator<true, false>
  1024. {
  1025. static std::string Create(cmTarget* target,
  1026. cmGeneratorExpressionContext *context,
  1027. const GeneratorExpressionContent *content)
  1028. {
  1029. // The file used to link to the target (.so, .lib, .a).
  1030. if(!target->IsLinkable())
  1031. {
  1032. ::reportError(context, content->GetOriginalExpression(),
  1033. "TARGET_LINKER_FILE is allowed only for libraries and "
  1034. "executables with ENABLE_EXPORTS.");
  1035. return std::string();
  1036. }
  1037. return target->GetFullPath(context->Config,
  1038. target->HasImportLibrary());
  1039. }
  1040. };
  1041. //----------------------------------------------------------------------------
  1042. template<>
  1043. struct TargetFilesystemArtifactResultCreator<false, false>
  1044. {
  1045. static std::string Create(cmTarget* target,
  1046. cmGeneratorExpressionContext *context,
  1047. const GeneratorExpressionContent *)
  1048. {
  1049. return target->GetFullPath(context->Config, false, true);
  1050. }
  1051. };
  1052. //----------------------------------------------------------------------------
  1053. template<bool dirQual, bool nameQual>
  1054. struct TargetFilesystemArtifactResultGetter
  1055. {
  1056. static std::string Get(const std::string &result);
  1057. };
  1058. //----------------------------------------------------------------------------
  1059. template<>
  1060. struct TargetFilesystemArtifactResultGetter<false, true>
  1061. {
  1062. static std::string Get(const std::string &result)
  1063. { return cmSystemTools::GetFilenameName(result); }
  1064. };
  1065. //----------------------------------------------------------------------------
  1066. template<>
  1067. struct TargetFilesystemArtifactResultGetter<true, false>
  1068. {
  1069. static std::string Get(const std::string &result)
  1070. { return cmSystemTools::GetFilenamePath(result); }
  1071. };
  1072. //----------------------------------------------------------------------------
  1073. template<>
  1074. struct TargetFilesystemArtifactResultGetter<false, false>
  1075. {
  1076. static std::string Get(const std::string &result)
  1077. { return result; }
  1078. };
  1079. //----------------------------------------------------------------------------
  1080. template<bool linker, bool soname, bool dirQual, bool nameQual>
  1081. struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
  1082. {
  1083. TargetFilesystemArtifact() {}
  1084. virtual int NumExpectedParameters() const { return 1; }
  1085. std::string Evaluate(const std::vector<std::string> &parameters,
  1086. cmGeneratorExpressionContext *context,
  1087. const GeneratorExpressionContent *content,
  1088. cmGeneratorExpressionDAGChecker *dagChecker) const
  1089. {
  1090. // Lookup the referenced target.
  1091. std::string name = *parameters.begin();
  1092. if (!cmGeneratorExpression::IsValidTargetName(name))
  1093. {
  1094. ::reportError(context, content->GetOriginalExpression(),
  1095. "Expression syntax not recognized.");
  1096. return std::string();
  1097. }
  1098. cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
  1099. if(!target)
  1100. {
  1101. ::reportError(context, content->GetOriginalExpression(),
  1102. "No target \"" + name + "\"");
  1103. return std::string();
  1104. }
  1105. if(target->GetType() >= cmTarget::UTILITY &&
  1106. target->GetType() != cmTarget::UNKNOWN_LIBRARY)
  1107. {
  1108. ::reportError(context, content->GetOriginalExpression(),
  1109. "Target \"" + name + "\" is not an executable or library.");
  1110. return std::string();
  1111. }
  1112. if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
  1113. {
  1114. ::reportError(context, content->GetOriginalExpression(),
  1115. "Expressions which require the linker language may not "
  1116. "be used while evaluating link libraries");
  1117. return std::string();
  1118. }
  1119. context->DependTargets.insert(target);
  1120. context->AllTargets.insert(target);
  1121. std::string result =
  1122. TargetFilesystemArtifactResultCreator<linker, soname>::Create(
  1123. target,
  1124. context,
  1125. content);
  1126. if (context->HadError)
  1127. {
  1128. return std::string();
  1129. }
  1130. return
  1131. TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  1132. }
  1133. };
  1134. //----------------------------------------------------------------------------
  1135. static const
  1136. TargetFilesystemArtifact<false, false, false, false> targetFileNode;
  1137. static const
  1138. TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
  1139. static const
  1140. TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
  1141. static const
  1142. TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
  1143. static const
  1144. TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
  1145. static const
  1146. TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
  1147. static const
  1148. TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
  1149. static const
  1150. TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
  1151. static const
  1152. TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;
  1153. //----------------------------------------------------------------------------
  1154. static const
  1155. cmGeneratorExpressionNode* GetNode(const std::string &identifier)
  1156. {
  1157. if (identifier == "0")
  1158. return &zeroNode;
  1159. else if (identifier == "1")
  1160. return &oneNode;
  1161. else if (identifier == "AND")
  1162. return &andNode;
  1163. else if (identifier == "OR")
  1164. return &orNode;
  1165. else if (identifier == "NOT")
  1166. return &notNode;
  1167. else if (identifier == "C_COMPILER_ID")
  1168. return &cCompilerIdNode;
  1169. else if (identifier == "CXX_COMPILER_ID")
  1170. return &cxxCompilerIdNode;
  1171. else if (identifier == "VERSION_GREATER")
  1172. return &versionGreaterNode;
  1173. else if (identifier == "VERSION_LESS")
  1174. return &versionLessNode;
  1175. else if (identifier == "VERSION_EQUAL")
  1176. return &versionEqualNode;
  1177. else if (identifier == "C_COMPILER_VERSION")
  1178. return &cCompilerVersionNode;
  1179. else if (identifier == "CXX_COMPILER_VERSION")
  1180. return &cxxCompilerVersionNode;
  1181. else if (identifier == "CONFIGURATION")
  1182. return &configurationNode;
  1183. else if (identifier == "CONFIG")
  1184. return &configurationTestNode;
  1185. else if (identifier == "TARGET_FILE")
  1186. return &targetFileNode;
  1187. else if (identifier == "TARGET_LINKER_FILE")
  1188. return &targetLinkerFileNode;
  1189. else if (identifier == "TARGET_SONAME_FILE")
  1190. return &targetSoNameFileNode;
  1191. else if (identifier == "TARGET_FILE_NAME")
  1192. return &targetFileNameNode;
  1193. else if (identifier == "TARGET_LINKER_FILE_NAME")
  1194. return &targetLinkerFileNameNode;
  1195. else if (identifier == "TARGET_SONAME_FILE_NAME")
  1196. return &targetSoNameFileNameNode;
  1197. else if (identifier == "TARGET_FILE_DIR")
  1198. return &targetFileDirNode;
  1199. else if (identifier == "TARGET_LINKER_FILE_DIR")
  1200. return &targetLinkerFileDirNode;
  1201. else if (identifier == "TARGET_SONAME_FILE_DIR")
  1202. return &targetSoNameFileDirNode;
  1203. else if (identifier == "STREQUAL")
  1204. return &strEqualNode;
  1205. else if (identifier == "BOOL")
  1206. return &boolNode;
  1207. else if (identifier == "ANGLE-R")
  1208. return &angle_rNode;
  1209. else if (identifier == "COMMA")
  1210. return &commaNode;
  1211. else if (identifier == "SEMICOLON")
  1212. return &semicolonNode;
  1213. else if (identifier == "TARGET_PROPERTY")
  1214. return &targetPropertyNode;
  1215. else if (identifier == "TARGET_NAME")
  1216. return &targetNameNode;
  1217. else if (identifier == "TARGET_POLICY")
  1218. return &targetPolicyNode;
  1219. else if (identifier == "BUILD_INTERFACE")
  1220. return &buildInterfaceNode;
  1221. else if (identifier == "INSTALL_INTERFACE")
  1222. return &installInterfaceNode;
  1223. else if (identifier == "INSTALL_PREFIX")
  1224. return &installPrefixNode;
  1225. else if (identifier == "JOIN")
  1226. return &joinNode;
  1227. else if (identifier == "LINK_ONLY")
  1228. return &linkOnlyNode;
  1229. return 0;
  1230. }
  1231. //----------------------------------------------------------------------------
  1232. GeneratorExpressionContent::GeneratorExpressionContent(
  1233. const char *startContent,
  1234. unsigned int length)
  1235. : StartContent(startContent), ContentLength(length)
  1236. {
  1237. }
  1238. //----------------------------------------------------------------------------
  1239. std::string GeneratorExpressionContent::GetOriginalExpression() const
  1240. {
  1241. return std::string(this->StartContent, this->ContentLength);
  1242. }
  1243. //----------------------------------------------------------------------------
  1244. std::string GeneratorExpressionContent::ProcessArbitraryContent(
  1245. const cmGeneratorExpressionNode *node,
  1246. const std::string &identifier,
  1247. cmGeneratorExpressionContext *context,
  1248. cmGeneratorExpressionDAGChecker *dagChecker,
  1249. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1250. pit) const
  1251. {
  1252. std::string result;
  1253. const
  1254. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1255. pend = this->ParamChildren.end();
  1256. for ( ; pit != pend; ++pit)
  1257. {
  1258. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1259. = pit->begin();
  1260. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1261. = pit->end();
  1262. for ( ; it != end; ++it)
  1263. {
  1264. if (node->RequiresLiteralInput())
  1265. {
  1266. if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
  1267. {
  1268. reportError(context, this->GetOriginalExpression(),
  1269. "$<" + identifier + "> expression requires literal input.");
  1270. return std::string();
  1271. }
  1272. }
  1273. result += (*it)->Evaluate(context, dagChecker);
  1274. if (context->HadError)
  1275. {
  1276. return std::string();
  1277. }
  1278. }
  1279. if ((pit + 1) != pend)
  1280. {
  1281. result += ",";
  1282. }
  1283. }
  1284. if (node->RequiresLiteralInput())
  1285. {
  1286. std::vector<std::string> parameters;
  1287. parameters.push_back(result);
  1288. return node->Evaluate(parameters, context, this, dagChecker);
  1289. }
  1290. return result;
  1291. }
  1292. //----------------------------------------------------------------------------
  1293. std::string GeneratorExpressionContent::Evaluate(
  1294. cmGeneratorExpressionContext *context,
  1295. cmGeneratorExpressionDAGChecker *dagChecker) const
  1296. {
  1297. std::string identifier;
  1298. {
  1299. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1300. = this->IdentifierChildren.begin();
  1301. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1302. = this->IdentifierChildren.end();
  1303. for ( ; it != end; ++it)
  1304. {
  1305. identifier += (*it)->Evaluate(context, dagChecker);
  1306. if (context->HadError)
  1307. {
  1308. return std::string();
  1309. }
  1310. }
  1311. }
  1312. const cmGeneratorExpressionNode *node = GetNode(identifier);
  1313. if (!node)
  1314. {
  1315. reportError(context, this->GetOriginalExpression(),
  1316. "Expression did not evaluate to a known generator expression");
  1317. return std::string();
  1318. }
  1319. if (!node->GeneratesContent())
  1320. {
  1321. if (node->NumExpectedParameters() == 1
  1322. && node->AcceptsArbitraryContentParameter())
  1323. {
  1324. if (this->ParamChildren.empty())
  1325. {
  1326. reportError(context, this->GetOriginalExpression(),
  1327. "$<" + identifier + "> expression requires a parameter.");
  1328. }
  1329. }
  1330. else
  1331. {
  1332. std::vector<std::string> parameters;
  1333. this->EvaluateParameters(node, identifier, context, dagChecker,
  1334. parameters);
  1335. }
  1336. return std::string();
  1337. }
  1338. if (node->NumExpectedParameters() == 1
  1339. && node->AcceptsArbitraryContentParameter())
  1340. {
  1341. return this->ProcessArbitraryContent(node, identifier, context,
  1342. dagChecker,
  1343. this->ParamChildren.begin());
  1344. }
  1345. std::vector<std::string> parameters;
  1346. this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  1347. if (context->HadError)
  1348. {
  1349. return std::string();
  1350. }
  1351. return node->Evaluate(parameters, context, this, dagChecker);
  1352. }
  1353. //----------------------------------------------------------------------------
  1354. std::string GeneratorExpressionContent::EvaluateParameters(
  1355. const cmGeneratorExpressionNode *node,
  1356. const std::string &identifier,
  1357. cmGeneratorExpressionContext *context,
  1358. cmGeneratorExpressionDAGChecker *dagChecker,
  1359. std::vector<std::string> &parameters) const
  1360. {
  1361. const int numExpected = node->NumExpectedParameters();
  1362. {
  1363. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1364. pit = this->ParamChildren.begin();
  1365. const
  1366. std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
  1367. pend = this->ParamChildren.end();
  1368. const bool acceptsArbitraryContent
  1369. = node->AcceptsArbitraryContentParameter();
  1370. for ( ; pit != pend; ++pit)
  1371. {
  1372. std::string parameter;
  1373. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  1374. pit->begin();
  1375. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  1376. pit->end();
  1377. for ( ; it != end; ++it)
  1378. {
  1379. parameter += (*it)->Evaluate(context, dagChecker);
  1380. if (context->HadError)
  1381. {
  1382. return std::string();
  1383. }
  1384. }
  1385. parameters.push_back(parameter);
  1386. if (acceptsArbitraryContent
  1387. && parameters.size() == (unsigned int)numExpected - 1)
  1388. {
  1389. assert(pit != pend);
  1390. std::string lastParam = this->ProcessArbitraryContent(node, identifier,
  1391. context,
  1392. dagChecker,
  1393. pit + 1);
  1394. parameters.push_back(lastParam);
  1395. return std::string();
  1396. }
  1397. }
  1398. }
  1399. if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
  1400. && (unsigned int)numExpected != parameters.size()))
  1401. {
  1402. if (numExpected == 0)
  1403. {
  1404. reportError(context, this->GetOriginalExpression(),
  1405. "$<" + identifier + "> expression requires no parameters.");
  1406. }
  1407. else if (numExpected == 1)
  1408. {
  1409. reportError(context, this->GetOriginalExpression(),
  1410. "$<" + identifier + "> expression requires "
  1411. "exactly one parameter.");
  1412. }
  1413. else
  1414. {
  1415. cmOStringStream e;
  1416. e << "$<" + identifier + "> expression requires "
  1417. << numExpected
  1418. << " comma separated parameters, but got "
  1419. << parameters.size() << " instead.";
  1420. reportError(context, this->GetOriginalExpression(), e.str());
  1421. }
  1422. return std::string();
  1423. }
  1424. if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters
  1425. && parameters.empty())
  1426. {
  1427. reportError(context, this->GetOriginalExpression(), "$<" + identifier
  1428. + "> expression requires at least one parameter.");
  1429. }
  1430. return std::string();
  1431. }
  1432. //----------------------------------------------------------------------------
  1433. static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
  1434. {
  1435. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
  1436. = c.begin();
  1437. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
  1438. = c.end();
  1439. for ( ; it != end; ++it)
  1440. {
  1441. delete *it;
  1442. }
  1443. }
  1444. //----------------------------------------------------------------------------
  1445. GeneratorExpressionContent::~GeneratorExpressionContent()
  1446. {
  1447. deleteAll(this->IdentifierChildren);
  1448. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  1449. std::vector<EvaluatorVector>::const_iterator pit =
  1450. this->ParamChildren.begin();
  1451. const std::vector<EvaluatorVector>::const_iterator pend =
  1452. this->ParamChildren.end();
  1453. for ( ; pit != pend; ++pit)
  1454. {
  1455. deleteAll(*pit);
  1456. }
  1457. }