cmComputeTargetDepends.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <set>
  7. #include <string>
  8. #include <vector>
  9. #include "cmGraphAdjacencyList.h"
  10. class cmComputeComponentGraph;
  11. class cmGeneratorTarget;
  12. class cmGlobalGenerator;
  13. class cmLinkItem;
  14. class cmListFileBacktrace;
  15. class cmSourceFile;
  16. class cmTargetDependSet;
  17. /** \class cmComputeTargetDepends
  18. * \brief Compute global interdependencies among targets.
  19. *
  20. * Static libraries may form cycles in the target dependency graph.
  21. * This class evaluates target dependencies globally and adjusts them
  22. * to remove cycles while preserving a safe build order.
  23. */
  24. class cmComputeTargetDepends
  25. {
  26. public:
  27. cmComputeTargetDepends(cmGlobalGenerator* gg);
  28. ~cmComputeTargetDepends();
  29. bool Compute();
  30. std::vector<cmGeneratorTarget const*> const& GetTargets() const
  31. {
  32. return this->Targets;
  33. }
  34. void GetTargetDirectDepends(cmGeneratorTarget const* t,
  35. cmTargetDependSet& deps);
  36. private:
  37. struct TargetSideEffects
  38. {
  39. std::set<cmGeneratorTarget const*> CustomCommandSideEffects;
  40. std::map<std::string, std::set<cmGeneratorTarget const*>>
  41. LanguageSideEffects;
  42. };
  43. void CollectTargets();
  44. void CollectDepends();
  45. void CollectTargetDepends(int depender_index);
  46. void AddTargetDepend(int depender_index, cmLinkItem const& dependee_name,
  47. bool linking, bool cross);
  48. void AddTargetDepend(int depender_index, cmGeneratorTarget const* dependee,
  49. cmListFileBacktrace const& dependee_backtrace,
  50. bool linking, bool cross);
  51. void CollectSideEffects();
  52. void CollectSideEffectsForTarget(std::set<int>& visited, int depender_index);
  53. void ComputeIntermediateGraph();
  54. void OptimizeLinkDependencies(cmGeneratorTarget const* gt,
  55. cmGraphEdgeList& outputEdges,
  56. cmGraphEdgeList const& inputEdges);
  57. bool ComputeFinalDepends(cmComputeComponentGraph const& ccg);
  58. void AddInterfaceDepends(int depender_index, cmLinkItem const& dependee_name,
  59. const std::string& config,
  60. std::set<cmLinkItem>& emitted);
  61. void AddInterfaceDepends(int depender_index,
  62. cmGeneratorTarget const* dependee,
  63. cmListFileBacktrace const& dependee_backtrace,
  64. const std::string& config,
  65. std::set<cmLinkItem>& emitted);
  66. void AddObjectDepends(int depender_index, cmSourceFile const* o,
  67. std::set<cmLinkItem>& emitted);
  68. cmGlobalGenerator* GlobalGenerator;
  69. bool DebugMode;
  70. bool NoCycles;
  71. // Collect all targets.
  72. std::vector<cmGeneratorTarget const*> Targets;
  73. std::map<cmGeneratorTarget const*, int> TargetIndex;
  74. // Represent the target dependency graph. The entry at each
  75. // top-level index corresponds to a depender whose dependencies are
  76. // listed.
  77. using NodeList = cmGraphNodeList;
  78. using EdgeList = cmGraphEdgeList;
  79. using Graph = cmGraphAdjacencyList;
  80. Graph InitialGraph;
  81. Graph IntermediateGraph;
  82. Graph FinalGraph;
  83. std::vector<TargetSideEffects> SideEffects;
  84. void DisplayGraph(Graph const& graph, const std::string& name);
  85. void DisplaySideEffects();
  86. // Deal with connected components.
  87. void DisplayComponents(cmComputeComponentGraph const& ccg,
  88. const std::string& name);
  89. bool CheckComponents(cmComputeComponentGraph const& ccg);
  90. void ComplainAboutBadComponent(cmComputeComponentGraph const& ccg, int c,
  91. bool strong = false);
  92. std::vector<int> ComponentHead;
  93. std::vector<int> ComponentTail;
  94. bool IntraComponent(std::vector<int> const& cmap, int c, int i, int* head,
  95. std::set<int>& emitted, std::set<int>& visited);
  96. };