cmGeneratorExpressionDAGChecker.cxx 6.3 KB

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