summaryrefslogtreecommitdiff
path: root/Source/cmGraphVizWriter.h
blob: 9766068a2ad7892fa287fe784f75930044d8ba99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#ifndef CMGRAPHVIZWRITER_H
#define CMGRAPHVIZWRITER_H

#include "cmConfigure.h" // IWYU pragma: keep

#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "cmsys/RegularExpression.hxx"

#include "cmGeneratedFileStream.h"
#include "cmLinkItem.h"
#include "cmLinkItemGraphVisitor.h"
#include "cmStateTypes.h"

class cmGlobalGenerator;

/** This class implements writing files for graphviz (dot) for graphs
 * representing the dependencies between the targets in the project. */
class cmGraphVizWriter : public cmLinkItemGraphVisitor
{
public:
  cmGraphVizWriter(std::string const& fileName,
                   const cmGlobalGenerator* globalGenerator);
  ~cmGraphVizWriter() override;

  void VisitGraph(std::string const& name) override;

  void OnItem(cmLinkItem const& item) override;

  void OnDirectLink(cmLinkItem const& depender, cmLinkItem const& dependee,
                    DependencyType dt) override;

  void OnIndirectLink(cmLinkItem const& depender,
                      cmLinkItem const& dependee) override;

  void ReadSettings(const std::string& settingsFileName,
                    const std::string& fallbackSettingsFileName);

  void Write();

private:
  using FileStreamMap =
    std::map<std::string, std::unique_ptr<cmGeneratedFileStream>>;

  struct Connection
  {
    Connection(cmLinkItem s, cmLinkItem d, std::string scope)
      : src(std::move(s))
      , dst(std::move(d))
      , scopeType(std::move(scope))
    {
    }

    cmLinkItem src;
    cmLinkItem dst;
    std::string scopeType;
  };
  using Connections = std::vector<Connection>;
  using ConnectionsMap = std::map<cmLinkItem, Connections>;

  void VisitLink(cmLinkItem const& depender, cmLinkItem const& dependee,
                 bool isDirectLink, std::string const& scopeType = "");

  void WriteHeader(cmGeneratedFileStream& fs, std::string const& name);

  void WriteFooter(cmGeneratedFileStream& fs);

  void WriteLegend(cmGeneratedFileStream& fs);

  void WriteNode(cmGeneratedFileStream& fs, cmLinkItem const& item);

  void CreateTargetFile(FileStreamMap& fileStreamMap, cmLinkItem const& target,
                        std::string const& fileNameSuffix = "");

  void WriteConnection(cmGeneratedFileStream& fs,
                       cmLinkItem const& dependerTargetName,
                       cmLinkItem const& dependeeTargetName,
                       std::string const& edgeStyle);

  void FindAllConnections(const ConnectionsMap& connectionMap,
                          const cmLinkItem& rootItem,
                          Connections& extendedCons,
                          std::set<cmLinkItem>& visitedItems);

  void FindAllConnections(const ConnectionsMap& connectionMap,
                          const cmLinkItem& rootItem,
                          Connections& extendedCons);

  template <typename DirFunc>
  void WritePerTargetConnections(const ConnectionsMap& connections,
                                 const FileStreamMap& streams);

  bool ItemExcluded(cmLinkItem const& item);
  bool ItemNameFilteredOut(std::string const& itemName);
  bool TargetTypeEnabled(cmStateEnums::TargetType targetType) const;

  std::string ItemNameWithAliases(std::string const& itemName) const;

  static std::string GetEdgeStyle(DependencyType dt);

  static std::string EscapeForDotFile(std::string const& str);

  static std::string PathSafeString(std::string const& str);

  std::string FileName;
  cmGeneratedFileStream GlobalFileStream;
  FileStreamMap PerTargetFileStreams;
  FileStreamMap TargetDependersFileStreams;

  ConnectionsMap PerTargetConnections;
  ConnectionsMap TargetDependersConnections;

  std::string GraphName;
  std::string GraphHeader;
  std::string GraphNodePrefix;

  std::vector<cmsys::RegularExpression> TargetsToIgnoreRegex;

  cmGlobalGenerator const* GlobalGenerator;

  int NextNodeId;
  // maps from the actual item names to node names in dot:
  std::map<std::string, std::string> NodeNames;

  bool GenerateForExecutables;
  bool GenerateForStaticLibs;
  bool GenerateForSharedLibs;
  bool GenerateForModuleLibs;
  bool GenerateForInterfaceLibs;
  bool GenerateForObjectLibs;
  bool GenerateForUnknownLibs;
  bool GenerateForCustomTargets;
  bool GenerateForExternals;
  bool GeneratePerTarget;
  bool GenerateDependers;
};

#endif