cmGeneratorExpressionEvaluator.cxx 57 KB

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