cmGeneratorExpressionDAGChecker.cxx 7.3 KB

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