cmGeneratorExpression.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm/string_view>
  12. #include "cmListFileCache.h"
  13. #include "cmLocalGenerator.h"
  14. namespace cm {
  15. namespace GenEx {
  16. struct Context;
  17. }
  18. }
  19. class cmake;
  20. class cmCompiledGeneratorExpression;
  21. class cmGeneratorTarget;
  22. struct cmGeneratorExpressionDAGChecker;
  23. struct cmGeneratorExpressionEvaluator;
  24. /** \class cmGeneratorExpression
  25. * \brief Evaluate generate-time query expression syntax.
  26. *
  27. * cmGeneratorExpression instances are used by build system generator
  28. * implementations to evaluate the $<> generator expression syntax.
  29. * Generator expressions are evaluated just before the generate step
  30. * writes strings into the build system. They have knowledge of the
  31. * build configuration which is not available at configure time.
  32. */
  33. class cmGeneratorExpression
  34. {
  35. public:
  36. /** Construct. */
  37. cmGeneratorExpression(cmake& cmakeInstance,
  38. cmListFileBacktrace backtrace = cmListFileBacktrace());
  39. ~cmGeneratorExpression();
  40. cmGeneratorExpression(cmGeneratorExpression const&) = delete;
  41. cmGeneratorExpression& operator=(cmGeneratorExpression const&) = delete;
  42. std::unique_ptr<cmCompiledGeneratorExpression> Parse(
  43. std::string input) const;
  44. static std::string Evaluate(
  45. std::string input, cmLocalGenerator const* lg, std::string const& config,
  46. cmGeneratorTarget const* headTarget = nullptr,
  47. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  48. cmGeneratorTarget const* currentTarget = nullptr,
  49. std::string const& language = std::string());
  50. static bool ForbidGeneratorExpressions(
  51. cmGeneratorTarget const* target, std::string const& propertyName,
  52. std::string const& propertyValue, std::string& evaluatedValue,
  53. std::map<std::string, std::vector<std::string>>& allowList);
  54. static bool ForbidGeneratorExpressions(cmGeneratorTarget const* target,
  55. std::string const& propertyName,
  56. std::string const& propertyValue);
  57. enum PreprocessContext
  58. {
  59. StripAllGeneratorExpressions,
  60. BuildInterface,
  61. InstallInterface
  62. };
  63. static std::string Preprocess(cm::string_view input,
  64. PreprocessContext context,
  65. cm::string_view importPrefix = {});
  66. static std::string Collect(
  67. std::string const& input,
  68. std::map<std::string, std::vector<std::string>>& collected);
  69. static void Split(std::string const& input,
  70. std::vector<std::string>& output);
  71. static cm::string_view::size_type Find(cm::string_view input);
  72. static bool IsValidTargetName(std::string const& input);
  73. static std::string StripEmptyListElements(std::string const& input);
  74. static bool StartsWithGeneratorExpression(std::string const& input)
  75. {
  76. return input.length() >= 2 && input[0] == '$' && input[1] == '<';
  77. }
  78. static bool StartsWithGeneratorExpression(char const* input)
  79. {
  80. return input && input[0] == '$' && input[1] == '<';
  81. }
  82. static void ReplaceInstallPrefix(std::string& input,
  83. std::string const& replacement);
  84. private:
  85. cmake& CMakeInstance;
  86. cmListFileBacktrace Backtrace;
  87. };
  88. class cmCompiledGeneratorExpression
  89. {
  90. public:
  91. ~cmCompiledGeneratorExpression();
  92. cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
  93. cmCompiledGeneratorExpression& operator=(
  94. cmCompiledGeneratorExpression const&) = delete;
  95. std::string const& Evaluate(
  96. cmLocalGenerator const* lg, std::string const& config,
  97. cmGeneratorTarget const* headTarget = nullptr) const;
  98. std::string const& Evaluate(
  99. cm::GenEx::Context const& context,
  100. cmGeneratorExpressionDAGChecker* dagChecker,
  101. cmGeneratorTarget const* headTarget = nullptr,
  102. cmGeneratorTarget const* currentTarget = nullptr) const;
  103. /** Get set of targets found during evaluations. */
  104. std::set<cmGeneratorTarget*> const& GetTargets() const
  105. {
  106. return this->DependTargets;
  107. }
  108. std::set<std::string> const& GetSeenTargetProperties() const
  109. {
  110. return this->SeenTargetProperties;
  111. }
  112. std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
  113. {
  114. return this->AllTargetsSeen;
  115. }
  116. std::string const& GetInput() const { return this->Input; }
  117. cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
  118. bool GetHadContextSensitiveCondition() const
  119. {
  120. return this->HadContextSensitiveCondition;
  121. }
  122. bool GetHadHeadSensitiveCondition() const
  123. {
  124. return this->HadHeadSensitiveCondition;
  125. }
  126. bool GetHadLinkLanguageSensitiveCondition() const
  127. {
  128. return this->HadLinkLanguageSensitiveCondition;
  129. }
  130. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  131. {
  132. return this->SourceSensitiveTargets;
  133. }
  134. void SetEvaluateForBuildsystem(bool eval)
  135. {
  136. this->EvaluateForBuildsystem = eval;
  137. }
  138. void SetQuiet(bool quiet) { this->Quiet = quiet; }
  139. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  140. std::map<std::string, std::string>& mapping);
  141. private:
  142. cmCompiledGeneratorExpression(cmake& cmakeInstance,
  143. cmListFileBacktrace backtrace,
  144. std::string input);
  145. friend class cmGeneratorExpression;
  146. cmListFileBacktrace Backtrace;
  147. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
  148. std::string const Input;
  149. bool NeedsEvaluation;
  150. bool EvaluateForBuildsystem = false;
  151. bool Quiet = false;
  152. mutable std::set<cmGeneratorTarget*> DependTargets;
  153. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  154. mutable std::set<std::string> SeenTargetProperties;
  155. mutable std::map<cmGeneratorTarget const*,
  156. std::map<std::string, std::string>>
  157. MaxLanguageStandard;
  158. mutable std::string Output;
  159. mutable bool HadContextSensitiveCondition = false;
  160. mutable bool HadHeadSensitiveCondition = false;
  161. mutable bool HadLinkLanguageSensitiveCondition = false;
  162. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  163. };
  164. class cmGeneratorExpressionInterpreter
  165. {
  166. public:
  167. cmGeneratorExpressionInterpreter(cmLocalGenerator const* localGenerator,
  168. std::string config,
  169. cmGeneratorTarget const* headTarget,
  170. std::string language = std::string())
  171. : GeneratorExpression(*localGenerator->GetCMakeInstance())
  172. , LocalGenerator(localGenerator)
  173. , Config(std::move(config))
  174. , HeadTarget(headTarget)
  175. , Language(std::move(language))
  176. {
  177. }
  178. cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
  179. delete;
  180. cmGeneratorExpressionInterpreter& operator=(
  181. cmGeneratorExpressionInterpreter const&) = delete;
  182. std::string const& Evaluate(std::string expression,
  183. std::string const& property);
  184. protected:
  185. cmGeneratorExpression GeneratorExpression;
  186. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  187. cmLocalGenerator const* LocalGenerator = nullptr;
  188. std::string Config;
  189. cmGeneratorTarget const* HeadTarget = nullptr;
  190. std::string Language;
  191. };