cmGeneratorExpression.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmListFileCache.h"
  12. #include "cmLocalGenerator.h"
  13. class cmake;
  14. class cmCompiledGeneratorExpression;
  15. class cmGeneratorTarget;
  16. struct cmGeneratorExpressionDAGChecker;
  17. struct cmGeneratorExpressionEvaluator;
  18. /** \class cmGeneratorExpression
  19. * \brief Evaluate generate-time query expression syntax.
  20. *
  21. * cmGeneratorExpression instances are used by build system generator
  22. * implementations to evaluate the $<> generator expression syntax.
  23. * Generator expressions are evaluated just before the generate step
  24. * writes strings into the build system. They have knowledge of the
  25. * build configuration which is not available at configure time.
  26. */
  27. class cmGeneratorExpression
  28. {
  29. public:
  30. /** Construct. */
  31. cmGeneratorExpression(cmake& cmakeInstance,
  32. 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. cmake& CMakeInstance;
  70. cmListFileBacktrace Backtrace;
  71. };
  72. class cmCompiledGeneratorExpression
  73. {
  74. public:
  75. ~cmCompiledGeneratorExpression();
  76. cmCompiledGeneratorExpression(cmCompiledGeneratorExpression const&) = delete;
  77. cmCompiledGeneratorExpression& operator=(
  78. cmCompiledGeneratorExpression const&) = delete;
  79. const std::string& Evaluate(
  80. cmLocalGenerator* lg, const std::string& config,
  81. cmGeneratorTarget const* headTarget = nullptr,
  82. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  83. cmGeneratorTarget const* currentTarget = nullptr,
  84. std::string const& language = std::string()) const;
  85. /** Get set of targets found during evaluations. */
  86. std::set<cmGeneratorTarget*> const& GetTargets() const
  87. {
  88. return this->DependTargets;
  89. }
  90. std::set<std::string> const& GetSeenTargetProperties() const
  91. {
  92. return this->SeenTargetProperties;
  93. }
  94. std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
  95. {
  96. return this->AllTargetsSeen;
  97. }
  98. std::string const& GetInput() const { return this->Input; }
  99. cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
  100. bool GetHadContextSensitiveCondition() const
  101. {
  102. return this->HadContextSensitiveCondition;
  103. }
  104. bool GetHadHeadSensitiveCondition() const
  105. {
  106. return this->HadHeadSensitiveCondition;
  107. }
  108. bool GetHadLinkLanguageSensitiveCondition() const
  109. {
  110. return this->HadLinkLanguageSensitiveCondition;
  111. }
  112. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  113. {
  114. return this->SourceSensitiveTargets;
  115. }
  116. void SetEvaluateForBuildsystem(bool eval)
  117. {
  118. this->EvaluateForBuildsystem = eval;
  119. }
  120. void SetQuiet(bool quiet) { this->Quiet = quiet; }
  121. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  122. std::map<std::string, std::string>& mapping);
  123. private:
  124. cmCompiledGeneratorExpression(cmake& cmakeInstance,
  125. cmListFileBacktrace backtrace,
  126. std::string input);
  127. friend class cmGeneratorExpression;
  128. cmListFileBacktrace Backtrace;
  129. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluator>> Evaluators;
  130. const std::string Input;
  131. bool NeedsEvaluation;
  132. bool EvaluateForBuildsystem = false;
  133. bool Quiet = false;
  134. mutable std::set<cmGeneratorTarget*> DependTargets;
  135. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  136. mutable std::set<std::string> SeenTargetProperties;
  137. mutable std::map<cmGeneratorTarget const*,
  138. std::map<std::string, std::string>>
  139. MaxLanguageStandard;
  140. mutable std::string Output;
  141. mutable bool HadContextSensitiveCondition = false;
  142. mutable bool HadHeadSensitiveCondition = false;
  143. mutable bool HadLinkLanguageSensitiveCondition = false;
  144. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  145. };
  146. class cmGeneratorExpressionInterpreter
  147. {
  148. public:
  149. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  150. std::string config,
  151. cmGeneratorTarget const* headTarget,
  152. std::string language = std::string())
  153. : GeneratorExpression(*localGenerator->GetCMakeInstance())
  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. protected:
  167. cmGeneratorExpression GeneratorExpression;
  168. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  169. cmLocalGenerator* LocalGenerator = nullptr;
  170. std::string Config;
  171. cmGeneratorTarget const* HeadTarget = nullptr;
  172. std::string Language;
  173. };