cmGeneratorExpression.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. bool GetHadLinkLanguageSensitiveCondition() const
  116. {
  117. return this->HadLinkLanguageSensitiveCondition;
  118. }
  119. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  120. {
  121. return this->SourceSensitiveTargets;
  122. }
  123. void SetEvaluateForBuildsystem(bool eval)
  124. {
  125. this->EvaluateForBuildsystem = eval;
  126. }
  127. void SetQuiet(bool quiet) { this->Quiet = quiet; }
  128. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  129. std::map<std::string, std::string>& mapping);
  130. private:
  131. const std::string& EvaluateWithContext(
  132. cmGeneratorExpressionContext& context,
  133. cmGeneratorExpressionDAGChecker* dagChecker) const;
  134. cmCompiledGeneratorExpression(cmListFileBacktrace backtrace,
  135. std::string input);
  136. friend class cmGeneratorExpression;
  137. cmListFileBacktrace Backtrace;
  138. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
  139. const std::string Input;
  140. bool NeedsEvaluation;
  141. bool EvaluateForBuildsystem;
  142. bool Quiet;
  143. mutable std::set<cmGeneratorTarget*> DependTargets;
  144. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  145. mutable std::set<std::string> SeenTargetProperties;
  146. mutable std::map<cmGeneratorTarget const*,
  147. std::map<std::string, std::string>>
  148. MaxLanguageStandard;
  149. mutable std::string Output;
  150. mutable bool HadContextSensitiveCondition;
  151. mutable bool HadHeadSensitiveCondition;
  152. mutable bool HadLinkLanguageSensitiveCondition;
  153. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  154. };
  155. class cmGeneratorExpressionInterpreter
  156. {
  157. public:
  158. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  159. std::string config,
  160. cmGeneratorTarget const* headTarget,
  161. std::string language = std::string())
  162. : LocalGenerator(localGenerator)
  163. , Config(std::move(config))
  164. , HeadTarget(headTarget)
  165. , Language(std::move(language))
  166. {
  167. }
  168. cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
  169. delete;
  170. cmGeneratorExpressionInterpreter& operator=(
  171. cmGeneratorExpressionInterpreter const&) = delete;
  172. const std::string& Evaluate(std::string expression,
  173. const std::string& property);
  174. const std::string& Evaluate(const char* expression,
  175. const std::string& property);
  176. protected:
  177. cmGeneratorExpression GeneratorExpression;
  178. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  179. cmLocalGenerator* LocalGenerator = nullptr;
  180. std::string Config;
  181. cmGeneratorTarget const* HeadTarget = nullptr;
  182. std::string Language;
  183. };
  184. #endif