cmGeneratorExpression.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "cmListFileCache.h"
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  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 const& input);
  38. std::unique_ptr<cmCompiledGeneratorExpression> Parse(const char* input);
  39. enum PreprocessContext
  40. {
  41. StripAllGeneratorExpressions,
  42. BuildInterface,
  43. InstallInterface
  44. };
  45. static std::string Preprocess(const std::string& input,
  46. PreprocessContext context,
  47. bool resolveRelative = false);
  48. static void Split(const std::string& input,
  49. std::vector<std::string>& output);
  50. static std::string::size_type Find(const std::string& input);
  51. static bool IsValidTargetName(const std::string& input);
  52. static std::string StripEmptyListElements(const std::string& input);
  53. static inline bool StartsWithGeneratorExpression(const std::string& input)
  54. {
  55. return input.length() >= 2 && input[0] == '$' && input[1] == '<';
  56. }
  57. static inline bool StartsWithGeneratorExpression(const char* input)
  58. {
  59. return input != nullptr && input[0] == '$' && input[1] == '<';
  60. }
  61. private:
  62. cmListFileBacktrace Backtrace;
  63. };
  64. class cmCompiledGeneratorExpression
  65. {
  66. public:
  67. ~cmCompiledGeneratorExpression();
  68. cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
  69. cmCompiledGeneratorExpression& operator=(
  70. cmCompiledGeneratorExpression const&) = delete;
  71. const std::string& Evaluate(
  72. cmLocalGenerator* lg, const std::string& config, bool quiet = false,
  73. cmGeneratorTarget const* headTarget = nullptr,
  74. cmGeneratorTarget const* currentTarget = nullptr,
  75. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  76. std::string const& language = std::string()) const;
  77. const std::string& Evaluate(
  78. cmLocalGenerator* lg, const std::string& config, bool quiet,
  79. cmGeneratorTarget const* headTarget,
  80. cmGeneratorExpressionDAGChecker* dagChecker,
  81. std::string const& language = std::string()) const;
  82. /** Get set of targets found during evaluations. */
  83. std::set<cmGeneratorTarget*> const& GetTargets() const
  84. {
  85. return this->DependTargets;
  86. }
  87. std::set<std::string> const& GetSeenTargetProperties() const
  88. {
  89. return this->SeenTargetProperties;
  90. }
  91. std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
  92. {
  93. return this->AllTargetsSeen;
  94. }
  95. std::string const& GetInput() const { return this->Input; }
  96. cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
  97. bool GetHadContextSensitiveCondition() const
  98. {
  99. return this->HadContextSensitiveCondition;
  100. }
  101. bool GetHadHeadSensitiveCondition() const
  102. {
  103. return this->HadHeadSensitiveCondition;
  104. }
  105. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  106. {
  107. return this->SourceSensitiveTargets;
  108. }
  109. void SetEvaluateForBuildsystem(bool eval)
  110. {
  111. this->EvaluateForBuildsystem = eval;
  112. }
  113. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  114. std::map<std::string, std::string>& mapping);
  115. private:
  116. const std::string& EvaluateWithContext(
  117. cmGeneratorExpressionContext& context,
  118. cmGeneratorExpressionDAGChecker* dagChecker) const;
  119. cmCompiledGeneratorExpression(cmListFileBacktrace backtrace,
  120. std::string input);
  121. friend class cmGeneratorExpression;
  122. cmListFileBacktrace Backtrace;
  123. std::vector<cmGeneratorExpressionEvaluator*> Evaluators;
  124. const std::string Input;
  125. bool NeedsEvaluation;
  126. mutable std::set<cmGeneratorTarget*> DependTargets;
  127. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  128. mutable std::set<std::string> SeenTargetProperties;
  129. mutable std::map<cmGeneratorTarget const*,
  130. std::map<std::string, std::string>>
  131. MaxLanguageStandard;
  132. mutable std::string Output;
  133. mutable bool HadContextSensitiveCondition;
  134. mutable bool HadHeadSensitiveCondition;
  135. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  136. bool EvaluateForBuildsystem;
  137. };
  138. class cmGeneratorExpressionInterpreter
  139. {
  140. public:
  141. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  142. std::string config,
  143. cmGeneratorTarget const* headTarget,
  144. std::string lang = std::string())
  145. : LocalGenerator(localGenerator)
  146. , Config(std::move(config))
  147. , HeadTarget(headTarget)
  148. , Language(std::move(lang))
  149. {
  150. }
  151. cmGeneratorExpressionInterpreter(cmGeneratorExpressionInterpreter const&) =
  152. delete;
  153. cmGeneratorExpressionInterpreter& operator=(
  154. cmGeneratorExpressionInterpreter const&) = delete;
  155. const std::string& Evaluate(const char* expression,
  156. const std::string& property);
  157. const std::string& Evaluate(const std::string& expression,
  158. const std::string& property)
  159. {
  160. return this->Evaluate(expression.c_str(), property);
  161. }
  162. protected:
  163. cmGeneratorExpression GeneratorExpression;
  164. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  165. cmLocalGenerator* LocalGenerator = nullptr;
  166. std::string Config;
  167. cmGeneratorTarget const* HeadTarget = nullptr;
  168. std::string Language;
  169. };
  170. #endif