cmGeneratorExpressionDAGChecker.cxx 7.2 KB

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