-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpackage-deps-graph.sh
executable file
·153 lines (138 loc) · 5.33 KB
/
package-deps-graph.sh
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
146
147
148
149
150
151
152
153
#!/bin/bash
# List all CMake package/module depenencies and create dot-graph.
# This will consider all `find_package` commands.
# The output file is named `package-graph.{dot,png}` by default.
#
# Usage: `./package-deps-graph.sh [root]`
# where root must contain at least one `CMakeLists.txt` file,
# but it can be a Kasper subdirectory as well.
BASE=${1:-${PWD}}
if [ ! -f "${BASE}/CMakeLists.txt" -o ! -d "${PWD}/.git" ]; then
printf "Script needs to be run in a source directory! Exiting...\n"
exit 1
fi
NAME="${BASE}/package-deps-graph"
cmake_files=$(git ls-tree --full-tree -r --name-only HEAD -- "${BASE}" \
| egrep 'CMakeLists\.txt$' \
| egrep -v '([Ee]xamples)\/' \
)
# List of module names, based on CMakeLists.txt project definitions.
# This trick makes all actual CMake projects to be drawn as boxes.
# Unreferenced projects are hidden from the graph.
project_names=$(
echo "${cmake_files}" \
| xargs grep -H 'project' \
| sed -nr \
-e 's/^([a-zA-Z0-9_\/]+)\/CMakeLists\.txt:\s*project\s*\(\s*([a-zA-Z0-9_-]+)\s*.*\)/"\2"/p' \
| sort | uniq \
| sed 's/\//\/\\n/g' \
)
# List of package names, based on CMakeLists.txt location.
# If the package name is a CMake project, it will be drawn as a box (see above).
package_names=$(
echo "${cmake_files}" \
| xargs grep -H 'find_package' \
| sed -nr \
-e 's/^([a-zA-Z0-9_\/]+)\/CMakeLists\.txt:\s*find_package\s*\(\s*([a-zA-Z0-9_-]+)\s*.*\)/"\1"/p' \
| sort | uniq \
| sed 's/\//\/\\n/g' \
)
# List of package targets, based on kasper_find_module() expressions.
# If the package target is a CMake project, it will be drawn as a box (see above).
# All package targets are drawn in red color.
package_targets=$(
echo "${cmake_files}" \
| xargs grep -H 'find_package' \
| sed -nr \
-e 's/^([a-zA-Z0-9_\/]+)\/CMakeLists\.txt:\s*find_package\s*\(\s*([a-zA-Z0-9_-]+)\s*.*\)/"\2"/p' \
| sort | uniq \
| sed 's/\//\/\\n/g' \
)
# Graph of CMake files to packages; combines package names and package targets.
package_deps_required=$( \
echo "${cmake_files}" \
| xargs grep -H 'find_package' \
| egrep "\s+REQUIRED\s?" \
| sed -nr \
-e 's/([a-zA-Z0-9_\/]+)\/CMakeLists\.txt:\s*find_package\s*\(\s*([a-zA-Z0-9_-]+)\s.*\)/"\1"->"\2"/p' \
| sort | uniq \
| sed 's/\//\/\\n/g' \
)
package_deps_optional=$( \
echo "${cmake_files}" \
| xargs grep -H 'find_package' \
| egrep -v "\s+REQUIRED\s?" \
| sed -nr \
-e 's/^([a-zA-Z0-9_\/]+)\/CMakeLists\.txt:\s*find_package\s*\(\s*([a-zA-Z0-9_-]+)\s?.*\)/"\1"->"\2"/p' \
| sort | uniq \
| sed 's/\//\/\\n/g' \
)
if [ -z "${package_deps_required}" -a -z "${package_deps_optional}" ]; then
echo "Found NO dependencies for ${BASE}"
exit -1
fi
echo > $NAME.dot
echo "digraph \"${NAME}\" {" >> $NAME.dot
echo " root=\"Kommon\"; model=subset; epsilon=0.0001; splines=true; overlap=false; rankdir=LR; ordering=out;" >> $NAME.dot
echo " node[shape=box];" >> $NAME.dot
echo "# project_names" >> $NAME.dot
for node in ${project_names}; do
echo " ${node} [style=invis,shape=box];" >> $NAME.dot
done
echo "# package_names" >> $NAME.dot
for node in ${package_names}; do
echo " ${node} [style=filled,color=lightgrey];" >> $NAME.dot
done
for node in ${project_names}; do
echo " ${node} [color=lightgreen];" >> $NAME.dot
done
echo "# package_targets" >> $NAME.dot
echo "subgraph cluster_targets {" >> $NAME.dot
echo " style=filled; color=none;" >> $NAME.dot
for node in ${package_targets}; do
color="lightpink"
for proj in ${project_names}; do
# color internal packages differently
if [ "$node" == "$proj" ]; then
color="lightblue"
fi
done
echo " ${node} [style=filled,color=${color}];" >> $NAME.dot
done
echo "}" >> $NAME.dot
echo "# package_deps_required" >> $NAME.dot
for edge in ${package_deps_required}; do
echo " ${edge} [style=solid,color=black];" >> $NAME.dot
done
echo "# package_deps_optional" >> $NAME.dot
for edge in ${package_deps_optional}; do
echo " ${edge} [style=dashed];" >> $NAME.dot
done
echo "}" >> $NAME.dot
# Make list of external package dependencies that can be sorted later
for edge in ${package_deps_required}; do
name=$(echo -E ${edge} | sed 's/"\(.*\)"->"\(.*\)"/\2/g')
for proj in ${project_names}; do
if [ "\"${name}\"" == "${proj}" ]; then
name=""
fi
done
package_names_required+="${name}\n"
done
for edge in ${package_deps_optional}; do
name=$(echo -E ${edge} | sed 's/"\(.*\)"->"\(.*\)"/\2/g')
for proj in ${project_names}; do
if [ "\"${name}\"" == "${proj}" ]; then
name=""
fi
done
package_names_optional+="${name}\n"
done
neato -Tpng -o$NAME.png $NAME.dot
echo "Dependency scan of ${BASE} completed:"
echo " $(echo "${project_names}" | wc -l) CMake projects"
echo " $(echo "${package_targets}" | wc -l) referenced packages"
echo " $(echo "${package_deps_required}" | wc -l) required + $(echo "${package_deps_optional}" | wc -l) optional dependencies:"
echo " Required: $(echo -e ${package_names_required} | sort -u | tr '\n' ' ')"
echo " Optional: $(echo -e ${package_names_optional} | sort -u | tr '\n' ' ')"
echo "Output created in: ${NAME}.{dot,png}"