cmGeneratorExpression.h 6.9 KB

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