cmGeneratorExpressionDAGChecker.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2012 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorExpressionDAGChecker.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmLocalGenerator.h"
  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 && (CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  50. TEST_TRANSITIVE_PROPERTY_METHOD) false))
  51. #undef TEST_TRANSITIVE_PROPERTY_METHOD
  52. {
  53. std::map<std::string, std::set<std::string> >::const_iterator it =
  54. top->Seen.find(this->Target);
  55. if (it != top->Seen.end()) {
  56. const std::set<std::string>& propSet = it->second;
  57. if (propSet.find(this->Property) != propSet.end()) {
  58. this->CheckResult = ALREADY_SEEN;
  59. return;
  60. }
  61. }
  62. const_cast<cmGeneratorExpressionDAGChecker*>(top)
  63. ->Seen[this->Target]
  64. .insert(this->Property);
  65. }
  66. }
  67. cmGeneratorExpressionDAGChecker::Result
  68. cmGeneratorExpressionDAGChecker::Check() const
  69. {
  70. return this->CheckResult;
  71. }
  72. void cmGeneratorExpressionDAGChecker::ReportError(
  73. cmGeneratorExpressionContext* context, const std::string& expr)
  74. {
  75. if (this->CheckResult == DAG) {
  76. return;
  77. }
  78. context->HadError = true;
  79. if (context->Quiet) {
  80. return;
  81. }
  82. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  83. if (parent && !parent->Parent) {
  84. std::ostringstream e;
  85. e << "Error evaluating generator expression:\n"
  86. << " " << expr << "\n"
  87. << "Self reference on target \"" << context->HeadTarget->GetName()
  88. << "\".\n";
  89. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  90. parent->Backtrace);
  91. return;
  92. }
  93. {
  94. std::ostringstream e;
  95. /* clang-format off */
  96. e << "Error evaluating generator expression:\n"
  97. << " " << expr << "\n"
  98. << "Dependency loop found.";
  99. /* clang-format on */
  100. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  101. context->Backtrace);
  102. }
  103. int loopStep = 1;
  104. while (parent) {
  105. std::ostringstream e;
  106. e << "Loop step " << loopStep << "\n"
  107. << " "
  108. << (parent->Content ? parent->Content->GetOriginalExpression() : expr)
  109. << "\n";
  110. context->LG->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  111. parent->Backtrace);
  112. parent = parent->Parent;
  113. ++loopStep;
  114. }
  115. }
  116. cmGeneratorExpressionDAGChecker::Result
  117. cmGeneratorExpressionDAGChecker::CheckGraph() const
  118. {
  119. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  120. while (parent) {
  121. if (this->Target == parent->Target && this->Property == parent->Property) {
  122. return (parent == this->Parent) ? SELF_REFERENCE : CYCLIC_REFERENCE;
  123. }
  124. parent = parent->Parent;
  125. }
  126. return DAG;
  127. }
  128. bool cmGeneratorExpressionDAGChecker::GetTransitivePropertiesOnly()
  129. {
  130. const cmGeneratorExpressionDAGChecker* top = this;
  131. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  132. while (parent) {
  133. top = parent;
  134. parent = parent->Parent;
  135. }
  136. return top->TransitivePropertiesOnly;
  137. }
  138. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries(const char* tgt)
  139. {
  140. const cmGeneratorExpressionDAGChecker* top = this;
  141. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  142. while (parent) {
  143. top = parent;
  144. parent = parent->Parent;
  145. }
  146. const char* prop = top->Property.c_str();
  147. if (tgt) {
  148. return top->Target == tgt && strcmp(prop, "LINK_LIBRARIES") == 0;
  149. }
  150. return (strcmp(prop, "LINK_LIBRARIES") == 0 ||
  151. strcmp(prop, "LINK_INTERFACE_LIBRARIES") == 0 ||
  152. strcmp(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  153. cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES_") ||
  154. cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES_")) ||
  155. strcmp(prop, "INTERFACE_LINK_LIBRARIES") == 0;
  156. }
  157. std::string cmGeneratorExpressionDAGChecker::TopTarget() const
  158. {
  159. const cmGeneratorExpressionDAGChecker* top = this;
  160. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  161. while (parent) {
  162. top = parent;
  163. parent = parent->Parent;
  164. }
  165. return top->Target;
  166. }
  167. enum TransitiveProperty
  168. {
  169. #define DEFINE_ENUM_ENTRY(NAME) NAME,
  170. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(DEFINE_ENUM_ENTRY)
  171. #undef DEFINE_ENUM_ENTRY
  172. TransitivePropertyTerminal
  173. };
  174. template <TransitiveProperty>
  175. bool additionalTest(const char* const /*unused*/)
  176. {
  177. return false;
  178. }
  179. template <>
  180. bool additionalTest<COMPILE_DEFINITIONS>(const char* const prop)
  181. {
  182. return cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_");
  183. }
  184. #define DEFINE_TRANSITIVE_PROPERTY_METHOD(METHOD, PROPERTY) \
  185. bool cmGeneratorExpressionDAGChecker::METHOD() const \
  186. { \
  187. const char* const prop = this->Property.c_str(); \
  188. if (strcmp(prop, #PROPERTY) == 0 || \
  189. strcmp(prop, "INTERFACE_" #PROPERTY) == 0) { \
  190. return true; \
  191. } \
  192. return additionalTest<PROPERTY>(prop); \
  193. }
  194. CM_FOR_EACH_TRANSITIVE_PROPERTY(DEFINE_TRANSITIVE_PROPERTY_METHOD)
  195. #undef DEFINE_TRANSITIVE_PROPERTY_METHOD