cmGeneratorExpressionDAGChecker.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. const cmGeneratorExpressionDAGChecker* top = this;
  143. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  144. while (parent) {
  145. top = parent;
  146. parent = parent->Parent;
  147. }
  148. return top->Property == "TARGET_GENEX_EVAL" || top->Property == "GENEX_EVAL";
  149. }
  150. bool cmGeneratorExpressionDAGChecker::EvaluatingPICExpression()
  151. {
  152. const cmGeneratorExpressionDAGChecker* top = this;
  153. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  154. while (parent) {
  155. top = parent;
  156. parent = parent->Parent;
  157. }
  158. return top->Property == "INTERFACE_POSITION_INDEPENDENT_CODE";
  159. }
  160. bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries(
  161. cmGeneratorTarget const* tgt)
  162. {
  163. const cmGeneratorExpressionDAGChecker* top = this;
  164. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  165. while (parent) {
  166. top = parent;
  167. parent = parent->Parent;
  168. }
  169. const char* prop = top->Property.c_str();
  170. if (tgt) {
  171. return top->Target == tgt && strcmp(prop, "LINK_LIBRARIES") == 0;
  172. }
  173. return (strcmp(prop, "LINK_LIBRARIES") == 0 ||
  174. strcmp(prop, "LINK_INTERFACE_LIBRARIES") == 0 ||
  175. strcmp(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  176. cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES_") ||
  177. cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES_")) ||
  178. strcmp(prop, "INTERFACE_LINK_LIBRARIES") == 0;
  179. }
  180. cmGeneratorTarget const* cmGeneratorExpressionDAGChecker::TopTarget() const
  181. {
  182. const cmGeneratorExpressionDAGChecker* top = this;
  183. const cmGeneratorExpressionDAGChecker* parent = this->Parent;
  184. while (parent) {
  185. top = parent;
  186. parent = parent->Parent;
  187. }
  188. return top->Target;
  189. }
  190. enum TransitiveProperty
  191. {
  192. #define DEFINE_ENUM_ENTRY(NAME) NAME,
  193. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(DEFINE_ENUM_ENTRY)
  194. #undef DEFINE_ENUM_ENTRY
  195. TransitivePropertyTerminal
  196. };
  197. template <TransitiveProperty>
  198. bool additionalTest(const char* const /*unused*/)
  199. {
  200. return false;
  201. }
  202. template <>
  203. bool additionalTest<COMPILE_DEFINITIONS>(const char* const prop)
  204. {
  205. return cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_");
  206. }
  207. #define DEFINE_TRANSITIVE_PROPERTY_METHOD(METHOD, PROPERTY) \
  208. bool cmGeneratorExpressionDAGChecker::METHOD() const \
  209. { \
  210. const char* const prop = this->Property.c_str(); \
  211. if (strcmp(prop, #PROPERTY) == 0 || \
  212. strcmp(prop, "INTERFACE_" #PROPERTY) == 0) { \
  213. return true; \
  214. } \
  215. return additionalTest<PROPERTY>(prop); \
  216. }
  217. CM_FOR_EACH_TRANSITIVE_PROPERTY(DEFINE_TRANSITIVE_PROPERTY_METHOD)
  218. #undef DEFINE_TRANSITIVE_PROPERTY_METHOD