cmGeneratorExpressionEvaluator.cxx 63 KB

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