cmGeneratorExpression.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 std::string& Evaluate(
  60. cmLocalGenerator* lg, const std::string& config, 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 std::string& Evaluate(
  66. cmLocalGenerator* lg, const std::string& config, bool quiet,
  67. cmGeneratorTarget const* headTarget,
  68. cmGeneratorExpressionDAGChecker* dagChecker,
  69. std::string const& language = std::string()) const;
  70. /** Get set of targets found during evaluations. */
  71. std::set<cmGeneratorTarget*> const& GetTargets() const
  72. {
  73. return this->DependTargets;
  74. }
  75. std::set<std::string> const& GetSeenTargetProperties() const
  76. {
  77. return this->SeenTargetProperties;
  78. }
  79. std::set<cmGeneratorTarget const*> const& GetAllTargetsSeen() const
  80. {
  81. return this->AllTargetsSeen;
  82. }
  83. ~cmCompiledGeneratorExpression();
  84. std::string const& GetInput() const { return this->Input; }
  85. cmListFileBacktrace GetBacktrace() const { return this->Backtrace; }
  86. bool GetHadContextSensitiveCondition() const
  87. {
  88. return this->HadContextSensitiveCondition;
  89. }
  90. bool GetHadHeadSensitiveCondition() const
  91. {
  92. return this->HadHeadSensitiveCondition;
  93. }
  94. std::set<cmGeneratorTarget const*> GetSourceSensitiveTargets() const
  95. {
  96. return this->SourceSensitiveTargets;
  97. }
  98. void SetEvaluateForBuildsystem(bool eval)
  99. {
  100. this->EvaluateForBuildsystem = eval;
  101. }
  102. void GetMaxLanguageStandard(cmGeneratorTarget const* tgt,
  103. std::map<std::string, std::string>& mapping);
  104. private:
  105. const std::string& EvaluateWithContext(
  106. cmGeneratorExpressionContext& context,
  107. cmGeneratorExpressionDAGChecker* dagChecker) const;
  108. cmCompiledGeneratorExpression(cmListFileBacktrace const& backtrace,
  109. const std::string& input);
  110. friend class cmGeneratorExpression;
  111. cmListFileBacktrace Backtrace;
  112. std::vector<cmGeneratorExpressionEvaluator*> Evaluators;
  113. const std::string Input;
  114. bool NeedsEvaluation;
  115. mutable std::set<cmGeneratorTarget*> DependTargets;
  116. mutable std::set<cmGeneratorTarget const*> AllTargetsSeen;
  117. mutable std::set<std::string> SeenTargetProperties;
  118. mutable std::map<cmGeneratorTarget const*,
  119. std::map<std::string, std::string>>
  120. MaxLanguageStandard;
  121. mutable std::string Output;
  122. mutable bool HadContextSensitiveCondition;
  123. mutable bool HadHeadSensitiveCondition;
  124. mutable std::set<cmGeneratorTarget const*> SourceSensitiveTargets;
  125. bool EvaluateForBuildsystem;
  126. };
  127. class cmGeneratorExpressionInterpreter
  128. {
  129. CM_DISABLE_COPY(cmGeneratorExpressionInterpreter)
  130. public:
  131. cmGeneratorExpressionInterpreter(cmLocalGenerator* localGenerator,
  132. std::string const& config,
  133. cmGeneratorTarget const* headTarget,
  134. std::string const& lang = std::string())
  135. : LocalGenerator(localGenerator)
  136. , Config(config)
  137. , HeadTarget(headTarget)
  138. , Language(lang)
  139. {
  140. }
  141. const std::string& Evaluate(const char* expression,
  142. const std::string& property);
  143. const std::string& Evaluate(const std::string& expression,
  144. const std::string& property)
  145. {
  146. return this->Evaluate(expression.c_str(), property);
  147. }
  148. protected:
  149. cmGeneratorExpression GeneratorExpression;
  150. std::unique_ptr<cmCompiledGeneratorExpression> CompiledGeneratorExpression;
  151. cmLocalGenerator* LocalGenerator = nullptr;
  152. std::string Config;
  153. cmGeneratorTarget const* HeadTarget = nullptr;
  154. std::string Language;
  155. };
  156. #endif