cmGeneratorExpressionDAGChecker.cxx 7.1 KB

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