cmComputeLinkDepends.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <memory>
  7. #include <queue>
  8. #include <set>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "cmGraphAdjacencyList.h"
  13. #include "cmLinkItem.h"
  14. #include "cmListFileCache.h"
  15. #include "cmTargetLinkLibraryType.h"
  16. class cmComputeComponentGraph;
  17. class cmGeneratorTarget;
  18. class cmGlobalGenerator;
  19. class cmMakefile;
  20. class cmake;
  21. /** \class cmComputeLinkDepends
  22. * \brief Compute link dependencies for targets.
  23. */
  24. class cmComputeLinkDepends
  25. {
  26. public:
  27. cmComputeLinkDepends(cmGeneratorTarget const* target,
  28. const std::string& config,
  29. const std::string& linkLanguage);
  30. ~cmComputeLinkDepends();
  31. cmComputeLinkDepends(const cmComputeLinkDepends&) = delete;
  32. cmComputeLinkDepends& operator=(const cmComputeLinkDepends&) = delete;
  33. // Basic information about each link item.
  34. struct LinkEntry
  35. {
  36. LinkEntry() = default;
  37. LinkEntry(BT<std::string> item, cmGeneratorTarget const* target = nullptr)
  38. : Item(std::move(item))
  39. , Target(target)
  40. {
  41. }
  42. static const std::string DEFAULT;
  43. enum EntryKind
  44. {
  45. Library,
  46. Object,
  47. SharedDep,
  48. Flag,
  49. // The following member is for the management of items specified
  50. // through genex $<LINK_GROUP:...>
  51. Group
  52. };
  53. BT<std::string> Item;
  54. cmGeneratorTarget const* Target = nullptr;
  55. EntryKind Kind = Library;
  56. // The following member is for the management of items specified
  57. // through genex $<LINK_LIBRARY:...>
  58. std::string Feature = std::string(DEFAULT);
  59. };
  60. using EntryVector = std::vector<LinkEntry>;
  61. EntryVector const& Compute();
  62. void SetOldLinkDirMode(bool b);
  63. std::set<cmGeneratorTarget const*> const& GetOldWrongConfigItems() const
  64. {
  65. return this->OldWrongConfigItems;
  66. }
  67. private:
  68. // Context information.
  69. cmGeneratorTarget const* Target;
  70. cmMakefile* Makefile;
  71. cmGlobalGenerator const* GlobalGenerator;
  72. cmake* CMakeInstance;
  73. std::string LinkLanguage;
  74. std::string Config;
  75. EntryVector FinalLinkEntries;
  76. std::map<std::string, std::string> LinkLibraryOverride;
  77. std::string const& GetCurrentFeature(
  78. std::string const& item, std::string const& defaultFeature) const;
  79. std::pair<std::map<cmLinkItem, int>::iterator, bool> AllocateLinkEntry(
  80. cmLinkItem const& item);
  81. std::pair<int, bool> AddLinkEntry(cmLinkItem const& item,
  82. int groupIndex = -1);
  83. void AddLinkObject(cmLinkItem const& item);
  84. void AddVarLinkEntries(int depender_index, const char* value);
  85. void AddDirectLinkEntries();
  86. template <typename T>
  87. void AddLinkEntries(int depender_index, std::vector<T> const& libs);
  88. void AddLinkObjects(std::vector<cmLinkItem> const& objs);
  89. cmLinkItem ResolveLinkItem(int depender_index, const std::string& name);
  90. // One entry for each unique item.
  91. std::vector<LinkEntry> EntryList;
  92. std::map<cmLinkItem, int> LinkEntryIndex;
  93. // map storing, for each group, the list of items
  94. std::map<int, std::vector<int>> GroupItems;
  95. // BFS of initial dependencies.
  96. struct BFSEntry
  97. {
  98. int Index;
  99. int GroupIndex;
  100. const char* LibDepends;
  101. };
  102. std::queue<BFSEntry> BFSQueue;
  103. void FollowLinkEntry(BFSEntry qe);
  104. // Shared libraries that are included only because they are
  105. // dependencies of other shared libraries, not because they are part
  106. // of the interface.
  107. struct SharedDepEntry
  108. {
  109. cmLinkItem Item;
  110. int DependerIndex;
  111. };
  112. std::queue<SharedDepEntry> SharedDepQueue;
  113. std::set<int> SharedDepFollowed;
  114. void FollowSharedDeps(int depender_index, cmLinkInterface const* iface,
  115. bool follow_interface = false);
  116. void QueueSharedDependencies(int depender_index,
  117. std::vector<cmLinkItem> const& deps);
  118. void HandleSharedDependency(SharedDepEntry const& dep);
  119. // Dependency inferral for each link item.
  120. struct DependSet : public std::set<int>
  121. {
  122. };
  123. struct DependSetList : public std::vector<DependSet>
  124. {
  125. bool Initialized = false;
  126. };
  127. std::vector<DependSetList> InferredDependSets;
  128. void InferDependencies();
  129. // To finalize dependencies over groups in place of raw items
  130. void UpdateGroupDependencies();
  131. // Ordering constraint graph adjacency list.
  132. using NodeList = cmGraphNodeList;
  133. using EdgeList = cmGraphEdgeList;
  134. using Graph = cmGraphAdjacencyList;
  135. Graph EntryConstraintGraph;
  136. void CleanConstraintGraph();
  137. bool CheckCircularDependencies() const;
  138. void DisplayConstraintGraph();
  139. // Ordering algorithm.
  140. void OrderLinkEntries();
  141. std::vector<char> ComponentVisited;
  142. std::vector<int> ComponentOrder;
  143. struct PendingComponent
  144. {
  145. // The real component id. Needed because the map is indexed by
  146. // component topological index.
  147. int Id;
  148. // The number of times the component needs to be seen. This is
  149. // always 1 for trivial components and is initially 2 for
  150. // non-trivial components.
  151. int Count;
  152. // The entries yet to be seen to complete the component.
  153. std::set<int> Entries;
  154. };
  155. std::map<int, PendingComponent> PendingComponents;
  156. std::unique_ptr<cmComputeComponentGraph> CCG;
  157. std::vector<int> FinalLinkOrder;
  158. void DisplayComponents();
  159. void VisitComponent(unsigned int c);
  160. void VisitEntry(int index);
  161. PendingComponent& MakePendingComponent(unsigned int component);
  162. int ComputeComponentCount(NodeList const& nl);
  163. void DisplayFinalEntries();
  164. // Record of the original link line.
  165. std::vector<int> OriginalEntries;
  166. std::set<cmGeneratorTarget const*> OldWrongConfigItems;
  167. void CheckWrongConfigItem(cmLinkItem const& item);
  168. // Record of explicitly linked object files.
  169. std::vector<int> ObjectEntries;
  170. int ComponentOrderId;
  171. cmTargetLinkLibraryType LinkType;
  172. bool HasConfig;
  173. bool DebugMode;
  174. bool OldLinkDirMode;
  175. };