cmGeneratorExpressionDAGChecker.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "cmAlgorithms.h"
  5. #include "cmGeneratorExpressionContext.h"
  6. #include "cmGeneratorExpressionEvaluator.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmake.h"
  10. #include <sstream>
  11. #include <string.h>
  12. #include <utility>
  13. cmGeneratorExpressionDAGChecker::cmGeneratorExpressionDAGChecker(
  14. const cmListFileBacktrace& backtrace, const std::string& target,
  15. const std::string& property, const GeneratorExpressionContent* content,
  16. cmGeneratorExpressionDAGChecker* parent)
  17. : Parent(parent)
  18. , Target(target)
  19. , Property(property)
  20. , Content(content)
  21. , Backtrace(backtrace)
  22. , TransitivePropertiesOnly(false)
  23. {
  24. Initialize();
  25. }
  26. cmGeneratorExpressionDAGChecker::cmGeneratorExpressionDAGChecker(
  27. const std::string& target, const std::string& property,
  28. const GeneratorExpressionContent* content,
  29. cmGeneratorExpressionDAGChecker* parent)
  30. : Parent(parent)
  31. , Target(target)
  32. , Property(property)
  33. , Content(content)
  34. , Backtrace()
  35. , TransitivePropertiesOnly(false)
  36. {
  37. Initialize();
  38. }
  39. void cmGeneratorExpressionDAGChecker::Initialize()
  40. {
  41. const cmGeneratorExpressionDAGChecker* top = this;
  42. const cmGeneratorExpressionDAGChecker* p = this->Parent;
  43. while (p) {
  44. top = p;
  45. p = p->Parent;
  46. }
  47. this->CheckResult = this->CheckGraph();
  48. #define TEST_TRANSITIVE_PROPERTY_METHOD(METHOD) top->METHOD() ||
  49. if (CheckResult == DAG &&
  50. (CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  51. TEST_TRANSITIVE_PROPERTY_METHOD) false)) // NOLINT(clang-tidy)
  52. #undef TEST_TRANSITIVE_PROPERTY_METHOD
  53. {
  54. std::map<std::string, std::set<std::string>>::const_iterator it =
  55. top->Seen.find(this->Target);
  56. if (it != top->Seen.end()) {
  57. const std::set<std::string>& propSet = it->second;
  58. if (propSet.find(this->Property) != propSet.end()) {
  59. this->CheckResult = ALREADY_SEEN;
  60. return;
  61. }
  62. }
  63. const_cast<cmGeneratorExpressionDAGChecker*>(top)
  64. ->Seen[this->Target]
  65. .insert(this->Property);
  66. }
  67. }
  68. cmGeneratorExpressionDAGChecker::Result
  69. cmGeneratorExpressionDAGChecker::Check() const
  70. {
  71. return this->CheckResult;
  72. }
  73. void cmGeneratorExpressionDAGChecker::ReportError(
  74. cmGeneratorExpressionContext* context, const std::string& expr)
  75. {
  76. if (this->CheckResult == DAG) {
  77. return;
  78. }
  79. context->HadError = true;
  80. if (context->Quiet) {
  81. return;
  82. }
  83. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  84. if (parent && !parent->Parent) {
  85. std::ostringstream e;
  86. e << "Error evaluating generator expression:\n"
  87. << " " << expr << "\n"
  88. << "Self reference on target \"" << context->HeadTarget->GetName()
  89. << "\".\n";
  90. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  91. parent->Backtrace);
  92. return;
  93. }
  94. {
  95. std::ostringstream e;
  96. /* clang-format off */
  97. e << "Error evaluating generator expression:\n"
  98. << " " << expr << "\n"
  99. << "Dependency loop found.";
  100. /* clang-format on */
  101. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  102. context->Backtrace);
  103. }
  104. int loopStep = 1;
  105. while (parent) {
  106. std::ostringstream e;
  107. e << "Loop step " << loopStep << "\n"
  108. << " "
  109. << (parent->Content ? parent->Content->GetOriginalExpression() : expr)
  110. << "\n";
  111. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  112. parent->Backtrace);
  113. parent = parent->Parent;
  114. ++loopStep;
  115. }
  116. }
  117. cmGeneratorExpressionDAGChecker::Result
  118. cmGeneratorExpressionDAGChecker::CheckGraph() const
  119. {
  120. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  121. while (parent) {
  122. if (this->Target == parent->Target && this->Property == parent->Property) {
  123. return (parent == this->Parent) ? SELF_REFERENCE : CYCLIC_REFERENCE;
  124. }
  125. parent = parent->Parent;
  126. }
  127. return DAG;
  128. }
  129. bool cmGeneratorExpressionDAGChecker::GetTransitivePropertiesOnly()
  130. {
  131. const cmGeneratorExpressionDAGChecker* top = this;
  132. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  133. while (parent) {
  134. top = parent;
  135. parent = parent->Parent;
  136. }
  137. return top->TransitivePropertiesOnly;
  138. }
  139. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries(const char* tgt)
  140. {
  141. const cmGeneratorExpressionDAGChecker* top = this;
  142. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  143. while (parent) {
  144. top = parent;
  145. parent = parent->Parent;
  146. }
  147. const char* prop = top->Property.c_str();
  148. if (tgt) {
  149. return top->Target == tgt && strcmp(prop, "LINK_LIBRARIES") == 0;
  150. }
  151. return (strcmp(prop, "LINK_LIBRARIES") == 0 ||
  152. strcmp(prop, "LINK_INTERFACE_LIBRARIES") == 0 ||
  153. strcmp(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  154. cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES_") ||
  155. cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES_")) ||
  156. strcmp(prop, "INTERFACE_LINK_LIBRARIES") == 0;
  157. }
  158. std::string cmGeneratorExpressionDAGChecker::TopTarget() const
  159. {
  160. const cmGeneratorExpressionDAGChecker* top = this;
  161. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  162. while (parent) {
  163. top = parent;
  164. parent = parent->Parent;
  165. }
  166. return top->Target;
  167. }
  168. enum TransitiveProperty
  169. {
  170. #define DEFINE_ENUM_ENTRY(NAME) NAME,
  171. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(DEFINE_ENUM_ENTRY)
  172. #undef DEFINE_ENUM_ENTRY
  173. TransitivePropertyTerminal
  174. };
  175. template <TransitiveProperty>
  176. bool additionalTest(const char* const /*unused*/)
  177. {
  178. return false;
  179. }
  180. template <>
  181. bool additionalTest<COMPILE_DEFINITIONS>(const char* const prop)
  182. {
  183. return cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_");
  184. }
  185. #define DEFINE_TRANSITIVE_PROPERTY_METHOD(METHOD, PROPERTY) \
  186. bool cmGeneratorExpressionDAGChecker::METHOD() const \
  187. { \
  188. const char* const prop = this->Property.c_str(); \
  189. if (strcmp(prop, #PROPERTY) == 0 || \
  190. strcmp(prop, "INTERFACE_" #PROPERTY) == 0) { \
  191. return true; \
  192. } \
  193. return additionalTest<PROPERTY>(prop); \
  194. }
  195. CM_FOR_EACH_TRANSITIVE_PROPERTY(DEFINE_TRANSITIVE_PROPERTY_METHOD)
  196. #undef DEFINE_TRANSITIVE_PROPERTY_METHOD