cmGeneratorExpressionDAGChecker.cxx 7.0 KB

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