-
Notifications
You must be signed in to change notification settings - Fork 3
/
makedot.py
50 lines (43 loc) · 1.62 KB
/
makedot.py
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
# SPDX-License-Identifier: Apache-2.0
from cmakefileapi import TargetType
# Create a Graphviz DOT file corresponding to the relationships
# among targets in a CMake Config.
# takes: Config, DOT output filename
# returns: True on success, False on error
def makeDot(cfg, outfile):
try:
with open(outfile, "w") as f:
f.write("digraph D {\n")
# create node for each target
nodeNum = 0
nodeMap = {}
for cfgTgt in cfg.configTargets:
f.write(f"node{nodeNum} [label=\"{cfgTgt.name}\" shape={getShapeType(cfgTgt.target.type)}]\n")
nodeMap[cfgTgt.target.id] = nodeNum
nodeNum += 1
# second pass, create edge for each dependency
toNodeNum = 0
for cfgTgt in cfg.configTargets:
for dep in cfgTgt.target.dependencies:
fromNodeNum = nodeMap[dep.id]
f.write(f"node{fromNodeNum} -> node{toNodeNum}\n")
toNodeNum += 1
f.write("}\n")
except OSError as e:
print(f"Error writing to {outfile}: {str(e)}")
return False
def getShapeType(targetType):
if targetType == TargetType.EXECUTABLE:
return "hexagon"
elif targetType == TargetType.STATIC_LIBRARY:
return "box"
elif targetType == TargetType.SHARED_LIBRARY:
return "octagon"
elif targetType == TargetType.MODULE_LIBRARY:
return "house"
elif targetType == TargetType.OBJECT_LIBRARY:
return "parallelogram"
elif targetType == TargetType.UTILITY:
return "oval"
else:
return "diamond"