cmGeneratorExpressionDAGChecker.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGeneratorExpressionDAGChecker.h"
  4. #include <sstream>
  5. #include <utility>
  6. #include <cm/optional>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmGeneratorExpressionContext.h"
  10. #include "cmGeneratorExpressionEvaluator.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMessageType.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmake.h"
  16. cmGeneratorExpressionDAGChecker::cmGeneratorExpressionDAGChecker(
  17. cmGeneratorTarget const* target, std::string property,
  18. GeneratorExpressionContent const* content,
  19. cmGeneratorExpressionDAGChecker* parent, cmLocalGenerator const* contextLG,
  20. std::string const& contextConfig, cmListFileBacktrace backtrace)
  21. : Parent(parent)
  22. , Top(parent ? parent->Top : this)
  23. , Target(target)
  24. , Property(std::move(property))
  25. , Content(content)
  26. , Backtrace(std::move(backtrace))
  27. {
  28. if (parent) {
  29. this->TopIsTransitiveProperty = parent->TopIsTransitiveProperty;
  30. } else {
  31. this->TopIsTransitiveProperty =
  32. this->Target
  33. ->IsTransitiveProperty(this->Property, contextLG, contextConfig,
  34. this->EvaluatingLinkLibraries())
  35. .has_value();
  36. }
  37. this->CheckResult = this->CheckGraph();
  38. if (this->CheckResult == DAG && this->EvaluatingTransitiveProperty()) {
  39. const auto* top = this->Top;
  40. auto it = top->Seen.find(this->Target);
  41. if (it != top->Seen.end()) {
  42. const std::set<std::string>& propSet = it->second;
  43. if (propSet.find(this->Property) != propSet.end()) {
  44. this->CheckResult = ALREADY_SEEN;
  45. return;
  46. }
  47. }
  48. top->Seen[this->Target].insert(this->Property);
  49. }
  50. }
  51. cmGeneratorExpressionDAGChecker::Result
  52. cmGeneratorExpressionDAGChecker::Check() const
  53. {
  54. return this->CheckResult;
  55. }
  56. void cmGeneratorExpressionDAGChecker::ReportError(
  57. cmGeneratorExpressionContext* context, const std::string& expr)
  58. {
  59. if (this->CheckResult == DAG) {
  60. return;
  61. }
  62. context->HadError = true;
  63. if (context->Quiet) {
  64. return;
  65. }
  66. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  67. if (parent && !parent->Parent) {
  68. std::ostringstream e;
  69. e << "Error evaluating generator expression:\n"
  70. << " " << expr << "\n"
  71. << "Self reference on target \"" << context->HeadTarget->GetName()
  72. << "\".\n";
  73. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  74. e.str(), parent->Backtrace);
  75. return;
  76. }
  77. {
  78. std::ostringstream e;
  79. /* clang-format off */
  80. e << "Error evaluating generator expression:\n"
  81. << " " << expr << "\n"
  82. << "Dependency loop found.";
  83. /* clang-format on */
  84. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  85. e.str(), context->Backtrace);
  86. }
  87. int loopStep = 1;
  88. while (parent) {
  89. std::ostringstream e;
  90. e << "Loop step " << loopStep << "\n"
  91. << " "
  92. << (parent->Content ? parent->Content->GetOriginalExpression() : expr)
  93. << "\n";
  94. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  95. e.str(), parent->Backtrace);
  96. parent = parent->Parent;
  97. ++loopStep;
  98. }
  99. }
  100. cmGeneratorExpressionDAGChecker::Result
  101. cmGeneratorExpressionDAGChecker::CheckGraph() const
  102. {
  103. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  104. while (parent) {
  105. if (this->Target == parent->Target && this->Property == parent->Property) {
  106. return (parent == this->Parent) ? SELF_REFERENCE : CYCLIC_REFERENCE;
  107. }
  108. parent = parent->Parent;
  109. }
  110. return DAG;
  111. }
  112. bool cmGeneratorExpressionDAGChecker::GetTransitivePropertiesOnly() const
  113. {
  114. return this->Top->TransitivePropertiesOnly;
  115. }
  116. bool cmGeneratorExpressionDAGChecker::GetTransitivePropertiesOnlyCMP0131()
  117. const
  118. {
  119. return this->Top->CMP0131;
  120. }
  121. bool cmGeneratorExpressionDAGChecker::EvaluatingTransitiveProperty() const
  122. {
  123. return this->TopIsTransitiveProperty;
  124. }
  125. bool cmGeneratorExpressionDAGChecker::EvaluatingGenexExpression() const
  126. {
  127. // Corresponds to GenexEvaluator::EvaluateExpression.
  128. return cmHasLiteralPrefix(this->Property, "TARGET_GENEX_EVAL:") ||
  129. cmHasLiteralPrefix(this->Property, "GENEX_EVAL:");
  130. }
  131. bool cmGeneratorExpressionDAGChecker::EvaluatingPICExpression() const
  132. {
  133. // Corresponds to checkInterfacePropertyCompatibility's special case
  134. // that evaluates the value of POSITION_INDEPENDENT_CODE as a genex.
  135. return this->Top->Property == "INTERFACE_POSITION_INDEPENDENT_CODE";
  136. }
  137. bool cmGeneratorExpressionDAGChecker::EvaluatingCompileExpression() const
  138. {
  139. cm::string_view property(this->Top->Property);
  140. return property == "INCLUDE_DIRECTORIES"_s ||
  141. property == "COMPILE_DEFINITIONS"_s || property == "COMPILE_OPTIONS"_s;
  142. }
  143. bool cmGeneratorExpressionDAGChecker::EvaluatingSources() const
  144. {
  145. return this->Property == "SOURCES"_s ||
  146. this->Property == "INTERFACE_SOURCES"_s;
  147. }
  148. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkExpression() const
  149. {
  150. cm::string_view property(this->Top->Property);
  151. return property == "LINK_DIRECTORIES"_s || property == "LINK_OPTIONS"_s ||
  152. property == "LINK_DEPENDS"_s || property == "LINK_LIBRARY_OVERRIDE"_s ||
  153. property == "LINKER_TYPE"_s;
  154. }
  155. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkOptionsExpression() const
  156. {
  157. cm::string_view property(this->Top->Property);
  158. return property == "LINK_OPTIONS"_s || property == "LINKER_TYPE"_s;
  159. }
  160. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkerLauncher() const
  161. {
  162. cm::string_view property(this->Top->Property);
  163. return property.length() > cmStrLen("_LINKER_LAUNCHER") &&
  164. property.substr(property.length() - cmStrLen("_LINKER_LAUNCHER")) ==
  165. "_LINKER_LAUNCHER"_s;
  166. }
  167. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries(
  168. cmGeneratorTarget const* tgt, ForGenex genex) const
  169. {
  170. const auto* top = this->Top;
  171. cm::string_view prop(top->Property);
  172. if (tgt) {
  173. return top->Target == tgt && prop == "LINK_LIBRARIES"_s;
  174. }
  175. auto result = prop == "LINK_LIBRARIES"_s ||
  176. prop == "INTERFACE_LINK_LIBRARIES"_s ||
  177. prop == "INTERFACE_LINK_LIBRARIES_DIRECT"_s ||
  178. prop == "LINK_INTERFACE_LIBRARIES"_s ||
  179. prop == "IMPORTED_LINK_INTERFACE_LIBRARIES"_s ||
  180. cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES_") ||
  181. cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES_");
  182. return genex == ForGenex::LINK_LIBRARY || genex == ForGenex::LINK_GROUP
  183. ? result
  184. : (result || prop == "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE"_s);
  185. }
  186. cmGeneratorTarget const* cmGeneratorExpressionDAGChecker::TopTarget() const
  187. {
  188. return this->Top->Target;
  189. }