cmGeneratorExpressionEvaluator.cxx 59 KB

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