1
0

cmGeneratorExpressionDAGChecker.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "cmLocalGenerator.h"
  12. #include "cmAlgorithms.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. e << "Error evaluating generator expression:\n"
  107. << " " << expr << "\n"
  108. << "Dependency loop found.";
  109. context->LG->GetCMakeInstance()
  110. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  111. context->Backtrace);
  112. }
  113. int loopStep = 1;
  114. while (parent)
  115. {
  116. std::ostringstream e;
  117. e << "Loop step " << loopStep << "\n"
  118. << " "
  119. << (parent->Content ? parent->Content->GetOriginalExpression() : expr)
  120. << "\n";
  121. context->LG->GetCMakeInstance()
  122. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  123. parent->Backtrace);
  124. parent = parent->Parent;
  125. ++loopStep;
  126. }
  127. }
  128. //----------------------------------------------------------------------------
  129. cmGeneratorExpressionDAGChecker::Result
  130. cmGeneratorExpressionDAGChecker::CheckGraph() const
  131. {
  132. const cmGeneratorExpressionDAGChecker *parent = this->Parent;
  133. while (parent)
  134. {
  135. if (this->Target == parent->Target && this->Property == parent->Property)
  136. {
  137. return (parent == this->Parent) ? SELF_REFERENCE : CYCLIC_REFERENCE;
  138. }
  139. parent = parent->Parent;
  140. }
  141. return DAG;
  142. }
  143. //----------------------------------------------------------------------------
  144. bool cmGeneratorExpressionDAGChecker::GetTransitivePropertiesOnly()
  145. {
  146. const cmGeneratorExpressionDAGChecker *top = this;
  147. const cmGeneratorExpressionDAGChecker *parent = this->Parent;
  148. while (parent)
  149. {
  150. top = parent;
  151. parent = parent->Parent;
  152. }
  153. return top->TransitivePropertiesOnly;
  154. }
  155. //----------------------------------------------------------------------------
  156. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries(const char *tgt)
  157. {
  158. const cmGeneratorExpressionDAGChecker *top = this;
  159. const cmGeneratorExpressionDAGChecker *parent = this->Parent;
  160. while (parent)
  161. {
  162. top = parent;
  163. parent = parent->Parent;
  164. }
  165. const char *prop = top->Property.c_str();
  166. if (tgt)
  167. {
  168. return top->Target == tgt && strcmp(prop, "LINK_LIBRARIES") == 0;
  169. }
  170. return (strcmp(prop, "LINK_LIBRARIES") == 0
  171. || strcmp(prop, "LINK_INTERFACE_LIBRARIES") == 0
  172. || strcmp(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES") == 0
  173. || cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES_")
  174. || cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES_"))
  175. || strcmp(prop, "INTERFACE_LINK_LIBRARIES") == 0;
  176. }
  177. std::string cmGeneratorExpressionDAGChecker::TopTarget() const
  178. {
  179. const cmGeneratorExpressionDAGChecker *top = this;
  180. const cmGeneratorExpressionDAGChecker *parent = this->Parent;
  181. while (parent)
  182. {
  183. top = parent;
  184. parent = parent->Parent;
  185. }
  186. return top->Target;
  187. }
  188. enum TransitiveProperty {
  189. #define DEFINE_ENUM_ENTRY(NAME) NAME,
  190. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(DEFINE_ENUM_ENTRY)
  191. #undef DEFINE_ENUM_ENTRY
  192. TransitivePropertyTerminal
  193. };
  194. template<TransitiveProperty>
  195. bool additionalTest(const char* const)
  196. {
  197. return false;
  198. }
  199. template<>
  200. bool additionalTest<COMPILE_DEFINITIONS>(const char* const prop)
  201. {
  202. return cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_");
  203. }
  204. #define DEFINE_TRANSITIVE_PROPERTY_METHOD(METHOD, PROPERTY) \
  205. bool cmGeneratorExpressionDAGChecker::METHOD() const \
  206. { \
  207. const char* const prop = this->Property.c_str(); \
  208. if (strcmp(prop, #PROPERTY) == 0 \
  209. || strcmp(prop, "INTERFACE_" #PROPERTY) == 0) \
  210. { \
  211. return true; \
  212. } \
  213. return additionalTest<PROPERTY>(prop); \
  214. }
  215. CM_FOR_EACH_TRANSITIVE_PROPERTY(DEFINE_TRANSITIVE_PROPERTY_METHOD)
  216. #undef DEFINE_TRANSITIVE_PROPERTY_METHOD