cmGeneratorExpressionDAGChecker.cxx 7.1 KB

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