cmGeneratorExpressionDAGChecker.cxx 7.2 KB

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