cmGeneratorExpression.h 6.2 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> // 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. : LocalGenerator(localGenerator)
  134. , GeneratorTarget(generatorTarget)
  135. , Config(config)
  136. {
  137. }
  138. const char* Evaluate(const char* expression)
  139. {
  140. this->CompiledGeneratorExpression =
  141. this->GeneratorExpression.Parse(expression);
  142. return this->CompiledGeneratorExpression->Evaluate(
  143. this->LocalGenerator, this->Config, false, this->GeneratorTarget);
  144. }
  145. const char* Evaluate(const std::string& expression)
  146. {
  147. return this->Evaluate(expression.c_str());
  148. }
  149. protected:
  150. cmGeneratorExpression& GetGeneratorExpression()
  151. {
  152. return this->GeneratorExpression;
  153. }
  154. cmCompiledGeneratorExpression& GetCompiledGeneratorExpression()
  155. {
  156. return *(this->CompiledGeneratorExpression);
  157. }
  158. cmLocalGenerator* GetLocalGenerator() { return this->LocalGenerator; }
  159. cmGeneratorTarget* GetGeneratorTarget() { return this->GeneratorTarget; }
  160. private:
  161. cmGeneratorExpression GeneratorExpression;
  162. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  163. cmLocalGenerator* LocalGenerator = nullptr;
  164. cmGeneratorTarget* GeneratorTarget = nullptr;
  165. std::string Config;
  166. };
  167. #endif