cmGeneratorExpressionDAGChecker.cxx 7.1 KB

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