-
Notifications
You must be signed in to change notification settings - Fork 1
/
dep.sh
181 lines (152 loc) · 5.31 KB
/
dep.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
#################################################################################
# MIT License
#
# Copyright (c) 2020 Jia Lihong
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#################################################################################
#
# Usage: $(basename $0) [OPTION]... DIRECTORY
# Scan and visualize C/C++ source file dependencies.
#
# Options:
# -h show help
# -e show extern dependencies
# -o FILENAME specify the output png name, default is "dep.png"
#
err() {
echo "ERROR: $*" >&2
exit 1
}
display_help() {
echo "Usage: $(basename "$0") [OPTION]... DIRECTORY"
echo "Scan and visualize C/C++ source file dependencies."
echo ""
echo "Options:"
echo " -h show help"
echo " -e show extern dependencies"
echo " -o FILENAME specify the output png name, default is \"dep.png\""
echo ""
}
contains() {
local result="false"
local grep_result
grep_result="$(echo "$1" | grep "^$2$")"
if [ -n "${grep_result}" ]; then
result="true"
fi
echo "${result}"
}
check_dot() {
if ! type "dot" >/dev/null 2>&1; then
err "graphviz is not installed"
fi
}
main() {
DISPLAY_HELP="false"
SHOW_EXTERN="false"
OUTPUT_NAME="dep.png"
while getopts "eho:" opt
do
case "$opt" in
e) SHOW_EXTERN="true" ;;
h) DISPLAY_HELP="true" ;;
o) OUTPUT_NAME="${OPTARG}" ;;
*) echo "Unknown option $opt"; exit 1 ;;
esac
done
readonly DISPLAY_HELP
readonly SHOW_EXTERN
readonly OUTPUT_NAME
if [[ "${DISPLAY_HELP}" == "true" ]]; then
display_help
exit 0
fi
check_dot
shift $(( OPTIND - 1))
if [ -n "$1" ]; then
readonly DIR="$1"
else
echo "Missing directory"
display_help
exit 1
fi
readonly NEWLINE=$'\n'
echo "SHOW_EXTERN: ${SHOW_EXTERN}"
echo "OUTPUT_NAME: ${OUTPUT_NAME}"
echo "DIR = ${DIR}"
readonly FILEPATH_LIST="$(find "${DIR}" -regex ".*\.\(h\|hxx\|hpp\|hh\|c\|cxx\|cpp\|cc\)")"
readonly FILENAME_LIST="$(echo "${FILEPATH_LIST}" | awk -F'/' '{print $NF}')"
readonly FILE_NUM="$(echo "${FILEPATH_LIST}" | wc -l)"
extern_list=""
dep_inner_list=""
dep_extern_list=""
echo "FILE_NUM = ${FILE_NUM}"
i=0
for filepath in ${FILEPATH_LIST}; do
i="$(( "$i" + 1 ))"
echo -n "[$i/${FILE_NUM}] Scanning ${filepath} ... "
filename="$(echo "${filepath}" | awk -F'/' '{print $NF}')"
include_list="$(cpp -fpreprocessed "${filepath}" | sed -ne 's/^\s*#include\s*<\(.*\)>\s*$/\1/p; s/^\s*#include\s*\"\(.*\)\"\s*$/\1/p')"
for include_path in ${include_list}; do
include_name="$(echo "${include_path}" | awk -F'/' '{print $NF}')"
if [[ "$(contains "${FILENAME_LIST}" "${include_name}")" == "false" ]]; then
dep_extern_list="${dep_extern_list}${NEWLINE}\"${filename}\" -> \"${include_name}\""
if [[ "$(contains "${extern_list}" "${include_name}")" == "false" ]]; then
extern_list="${extern_list}${NEWLINE}${include_name}"
fi
else
dep_inner_list="${dep_inner_list}${NEWLINE}\"${filename}\" -> \"${include_name}\""
fi
done
echo "done"
done
IFS=$'\n'
echo -n "Generating .dot file ... "
dot_result="digraph dep {"
for filename in ${FILENAME_LIST}; do
if [[ -n "${filename}" ]]; then
dot_result="${dot_result}${NEWLINE}\"${filename}\";"
fi
done
for dep in ${dep_inner_list}; do
dot_result="${dot_result}${NEWLINE}${dep};"
done
if [[ "${SHOW_EXTERN}" == "true" ]]; then
for filename in ${extern_list}; do
if [[ -n "${filename}" ]]; then
dot_result="${dot_result}${NEWLINE}\"${filename}\" [color=red];"
fi
done
for dep in ${dep_extern_list}; do
dot_result="${dot_result}${NEWLINE}${dep};"
done
fi
dot_result="${dot_result}${NEWLINE}}"
echo "done"
readonly TEMP_DOT="$(mktemp)"
echo -n "Writing .dot file ... "
echo "${dot_result}" > "${TEMP_DOT}"
echo "done"
echo -n "Generating .png file ... "
dot -o "${OUTPUT_NAME}" -Tpng "${TEMP_DOT}"
echo "done"
}
main "$@"