cmGeneratorExpressionEvaluator.cxx 63 KB

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