cmGeneratorExpressionDAGChecker.cxx 7.0 KB

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