cmGeneratorExpression.h 6.7 KB

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