cmComputeLinkDepends.h 6.5 KB

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