cmGeneratorExpressionEvaluator.cxx 63 KB

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