cmGeneratorExpression.h 6.4 KB

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