cmGeneratorExpressionDAGChecker.cxx 8.1 KB

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