cmGeneratorExpressionDAGChecker.cxx 7.2 KB

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