cmGeneratorExpression.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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> // IWYU pragma: keep
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. class cmCompiledGeneratorExpression;
  13. class cmGeneratorTarget;
  14. class cmLocalGenerator;
  15. struct cmGeneratorExpressionContext;
  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. CM_DISABLE_COPY(cmGeneratorExpression)
  30. public:
  31. /** Construct. */
  32. cmGeneratorExpression(
  33. cmListFileBacktrace const& backtrace = cmListFileBacktrace());
  34. ~cmGeneratorExpression();
  35. std::unique_ptr<cmCompiledGeneratorExpression> Parse(
  36. std::string const& input);
  37. std::unique_ptr<cmCompiledGeneratorExpression> Parse(const char* input);
  38. enum PreprocessContext
  39. {
  40. StripAllGeneratorExpressions,
  41. BuildInterface,
  42. InstallInterface
  43. };
  44. static std::string Preprocess(const std::string& input,
  45. PreprocessContext context,
  46. bool resolveRelative = false);
  47. static void Split(const std::string& input,
  48. std::vector<std::string>& output);
  49. static std::string::size_type Find(const std::string& input);
  50. static bool IsValidTargetName(const std::string& input);
  51. static std::string StripEmptyListElements(const std::string& input);
  52. private:
  53. cmListFileBacktrace Backtrace;
  54. };
  55. class cmCompiledGeneratorExpression
  56. {
  57. CM_DISABLE_COPY(cmCompiledGeneratorExpression)
  58. public:
  59. const char* Evaluate(cmLocalGenerator* lg, const std::string& config,
  60. bool quiet = false,
  61. cmGeneratorTarget const* headTarget = nullptr,
  62. cmGeneratorTarget const* currentTarget = nullptr,
  63. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  64. std::string const& language = std::string()) const;
  65. const char* Evaluate(cmLocalGenerator* lg, const std::string& config,
  66. bool quiet, cmGeneratorTarget const* headTarget,
  67. cmGeneratorExpressionDAGChecker* dagChecker,
  68. std::string const& language = std::string()) const;
  69. /** Get set of targets found during evaluations. */
  70. std::set<cmGeneratorTarget*> const& GetTargets() const
  71. {
  72. return this->DependTargets;
  73. }
  74. std::set<std::string> const& GetSeenTargetProperties() const
  75. {
  76. return this->SeenTargetProperties;
  77. }
  78. std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
  79. {
  80. return this->AllTargetsSeen;
  81. }
  82. ~cmCompiledGeneratorExpression();
  83. std::string const& GetInput() const { return this->Input; }
  84. cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
  85. bool GetHadContextSensitiveCondition() const
  86. {
  87. return this->HadContextSensitiveCondition;
  88. }
  89. bool GetHadHeadSensitiveCondition() const
  90. {
  91. return this->HadHeadSensitiveCondition;
  92. }
  93. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  94. {
  95. return this->SourceSensitiveTargets;
  96. }
  97. void SetEvaluateForBuildsystem(bool eval)
  98. {
  99. this->EvaluateForBuildsystem = eval;
  100. }
  101. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  102. std::map<std::string, std::string>& mapping);
  103. private:
  104. const char* EvaluateWithContext(
  105. cmGeneratorExpressionContext& context,
  106. cmGeneratorExpressionDAGChecker* dagChecker) const;
  107. cmCompiledGeneratorExpression(cmListFileBacktrace const& backtrace,
  108. const std::string& input);
  109. friend class cmGeneratorExpression;
  110. cmListFileBacktrace Backtrace;
  111. std::vector<cmGeneratorExpressionEvaluator*> Evaluators;
  112. const std::string Input;
  113. bool NeedsEvaluation;
  114. mutable std::set<cmGeneratorTarget*> DependTargets;
  115. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  116. mutable std::set<std::string> SeenTargetProperties;
  117. mutable std::map<cmGeneratorTarget const*,
  118. std::map<std::string, std::string>>
  119. MaxLanguageStandard;
  120. mutable std::string Output;
  121. mutable bool HadContextSensitiveCondition;
  122. mutable bool HadHeadSensitiveCondition;
  123. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  124. bool EvaluateForBuildsystem;
  125. };
  126. class cmGeneratorExpressionInterpreter
  127. {
  128. CM_DISABLE_COPY(cmGeneratorExpressionInterpreter)
  129. public:
  130. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  131. cmGeneratorTarget* generatorTarget,
  132. const std::string& config,
  133. const std::string& target,
  134. const std::string& lang)
  135. : LocalGenerator(localGenerator)
  136. , GeneratorTarget(generatorTarget)
  137. , Config(config)
  138. , Target(target)
  139. , Language(lang)
  140. {
  141. }
  142. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  143. cmGeneratorTarget* generatorTarget,
  144. const std::string& config)
  145. : cmGeneratorExpressionInterpreter(localGenerator, generatorTarget, config,
  146. std::string(), std::string())
  147. {
  148. }
  149. const char* Evaluate(const char* expression)
  150. {
  151. return this->EvaluateExpression(expression);
  152. }
  153. const char* Evaluate(const std::string& expression)
  154. {
  155. return this->Evaluate(expression.c_str());
  156. }
  157. const char* Evaluate(const char* expression, const std::string& property);
  158. const char* Evaluate(const std::string& expression,
  159. const std::string& property)
  160. {
  161. return this->Evaluate(expression.c_str(), property);
  162. }
  163. protected:
  164. cmGeneratorExpression& GetGeneratorExpression()
  165. {
  166. return this->GeneratorExpression;
  167. }
  168. cmCompiledGeneratorExpression& GetCompiledGeneratorExpression()
  169. {
  170. return *(this->CompiledGeneratorExpression);
  171. }
  172. cmLocalGenerator* GetLocalGenerator() { return this->LocalGenerator; }
  173. cmGeneratorTarget* GetGeneratorTarget() { return this->GeneratorTarget; }
  174. const std::string& GetTargetName() const { return this->Target; }
  175. const std::string& GetLanguage() const { return this->Language; }
  176. const char* EvaluateExpression(
  177. const char* expression,
  178. cmGeneratorExpressionDAGChecker* dagChecker = nullptr)
  179. {
  180. this->CompiledGeneratorExpression =
  181. this->GeneratorExpression.Parse(expression);
  182. if (dagChecker == nullptr) {
  183. return this->CompiledGeneratorExpression->Evaluate(
  184. this->LocalGenerator, this->Config, false, this->GeneratorTarget);
  185. }
  186. return this->CompiledGeneratorExpression->Evaluate(
  187. this->LocalGenerator, this->Config, false, this->GeneratorTarget,
  188. dagChecker, this->Language);
  189. }
  190. private:
  191. cmGeneratorExpression GeneratorExpression;
  192. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  193. cmLocalGenerator* LocalGenerator = nullptr;
  194. cmGeneratorTarget* GeneratorTarget = nullptr;
  195. std::string Config;
  196. std::string Target;
  197. std::string Language;
  198. };
  199. #endif