cmGeneratorExpressionDAGChecker.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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<cmStdString, std::set<cmStdString> >::const_iterator it
  39. = top->Seen.find(target);
  40. if (it != top->Seen.end())
  41. {
  42. const std::set<cmStdString> &propSet = it->second;
  43. const std::set<cmStdString>::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. //----------------------------------------------------------------------------
  161. bool cmGeneratorExpressionDAGChecker::EvaluatingIncludeDirectories() const
  162. {
  163. const char *prop = this->Property.c_str();
  164. return (strcmp(prop, "INCLUDE_DIRECTORIES") == 0
  165. || strcmp(prop, "INTERFACE_INCLUDE_DIRECTORIES") == 0 );
  166. }
  167. //----------------------------------------------------------------------------
  168. bool
  169. cmGeneratorExpressionDAGChecker::EvaluatingSystemIncludeDirectories() const
  170. {
  171. const char *prop = this->Property.c_str();
  172. return (strcmp(prop, "SYSTEM_INCLUDE_DIRECTORIES") == 0
  173. || strcmp(prop, "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES") == 0);
  174. }
  175. //----------------------------------------------------------------------------
  176. bool cmGeneratorExpressionDAGChecker::EvaluatingCompileDefinitions() const
  177. {
  178. const char *prop = this->Property.c_str();
  179. return (strcmp(prop, "COMPILE_DEFINITIONS") == 0
  180. || strcmp(prop, "INTERFACE_COMPILE_DEFINITIONS") == 0
  181. || cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_"));
  182. }
  183. //----------------------------------------------------------------------------
  184. bool cmGeneratorExpressionDAGChecker::EvaluatingCompileOptions() const
  185. {
  186. const char *prop = this->Property.c_str();
  187. return (strcmp(prop, "COMPILE_OPTIONS") == 0
  188. || strcmp(prop, "INTERFACE_COMPILE_OPTIONS") == 0 );
  189. }
  190. //----------------------------------------------------------------------------
  191. bool cmGeneratorExpressionDAGChecker::EvaluatingAutoUicOptions() const
  192. {
  193. const char *prop = this->Property.c_str();
  194. return (strcmp(prop, "AUTOUIC_OPTIONS") == 0
  195. || strcmp(prop, "INTERFACE_AUTOUIC_OPTIONS") == 0 );
  196. }