-
Notifications
You must be signed in to change notification settings - Fork 6
/
deps.py
executable file
·348 lines (294 loc) · 12.1 KB
/
deps.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/python3
"""
* Copyright 2024 Centreon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
"""
import argparse
import re
import os
import json
import sys
from collections import defaultdict
ESC = '\x1b'
YELLOW = ESC + '[1;33m'
CYAN = ESC + '[1;36m'
GREEN = ESC + '[1;32m'
RESET = ESC + '[0m'
home = os.getcwd()
parser = argparse.ArgumentParser(
prog="deps.py", description='Draw a header dependency tree from one file.')
parser.add_argument('filename', nargs='+',
help='Specify the file whose headers are to analyze.')
parser.add_argument('--output', '-o', type=str,
help='Specify the DOT file to store the result graph.')
parser.add_argument('--depth', '-d', type=int, default=2,
help='Specify the depth to look up headers.')
parser.add_argument('--compile-commands', '-c', type=str, default="compile_commands.json",
help='Specify the depth to look up headers.')
parser.add_argument('--explain', '-e', action='store_true', default=False,
help='Explain what header to remove from the file.')
parser.add_argument('--fix', '-f', action='store_true', default=False,
help='Removed headers that seems not needed (this action is dangerous).')
args = parser.parse_args()
# Graph class to represent a directed graph
class Graph:
def __init__(self):
self.graph = defaultdict(list)
# Method to add an edge between two nodes u and v (strings)
def add_edge(self, u, v):
self.graph[u].append(v)
# Method to find all paths from source to destination
def find_all_paths(self, source, destination):
paths = []
current_path = []
# Check for a direct path (single edge path)
if destination in self.graph[source]:
paths.append([source, destination])
# Find all other paths using DFS
self.dfs(source, destination, current_path, paths)
return paths
# Recursive DFS function to explore paths
def dfs(self, current_node, destination, current_path, paths):
# Add the current node to the current path
current_path.append(current_node)
# If we reach the destination node, store the current path
if current_node == destination and len(current_path) > 2: # Ensure this is not the single-edge path
paths.append(list(current_path))
else:
# Otherwise, explore all neighbors of the current node
for neighbor in self.graph[current_node]:
if neighbor not in current_path: # Avoid immediate cycles
self.dfs(neighbor, destination, current_path, paths)
# Backtrack: remove the current node from the path before returning
current_path.pop()
# Method to check if at least two paths exist from source to destination
def find_paths_with_at_least_two(self, source, destination):
all_paths = self.find_all_paths(source, destination)
# Return paths only if there are at least two
if len(all_paths) >= 2:
return all_paths
else:
return None
# Method to find all node pairs with at least two paths
def find_all_pairs_with_at_least_two_paths(self):
result = {}
destinations = set()
# We get all the leafs.
for v in self.graph.values():
destinations.update(v)
nodes = set(self.graph.keys()) # All the nodes except leafs.
destinations.update(nodes) # And the union of them.
# Iterate through all pairs of nodes in the graph
for source in nodes:
for destination in destinations:
if source != destination:
paths = self.find_paths_with_at_least_two(source, destination)
if paths:
for path in paths:
if len(path) == 2:
result[(source, destination)] = paths
return result
def parse_command(entry):
"""
Returns all the include directories used in the compilation command and also
a file to get all the precompiled files. They are stored in a dictionary,
the first one with key 'include_dirs' and the second one with key 'pch'.
Args:
entry: An entry of the compile_commands.json
Returns: A list of the directories with absolute paths.
"""
command = entry['command']
args = command.split(' ')
retval = {}
if "clang" in args[0]:
for a in args:
if 'cmake_pch.hxx.pch' in a:
retval['pch'] = a[:4]
break
elif "g++" in args[0]:
for a in args:
if 'cmake_pch.hxx' in a:
retval['pch'] = a
break
# -I at first
retval['include_dirs'] = [a[2:].strip()
for a in args if a.startswith('-I')]
# -isystem at second
try:
idx = args.index("-isystem")
retval['include_dirs'].append(args[idx + 1])
except ValueError:
pass
# and system headers at last
retval['include_dirs'].append("/usr/include")
if os.path.exists("/usr/include/c++/12"):
retval['include_dirs'].append("/usr/include/c++/12")
elif os.path.exists("/usr/include/c++/10"):
retval['include_dirs'].append("/usr/include/c++/10")
return retval
def get_headers(full_name):
headers = []
with open(full_name, 'r') as f:
lines = f.readlines()
r_include = re.compile(r"^\s*#include\s*[\"<](.*)[\">]")
for line in lines:
m = r_include.match(line)
if m:
headers.append(m.group(1))
return headers
def build_full_headers(includes, headers):
retval = []
for header in headers:
for inc in includes:
file = f"{inc}/{header}"
if os.path.isfile(file):
retval.append((file, header))
break
return retval
def get_precomp_headers(precomp):
try:
with open(precomp, 'r') as f:
lines = f.readlines()
r_include = re.compile(r"^\s*#include\s*[\"<](.*)[\">]")
for line in lines:
m = r_include.match(line)
if m:
my_precomp = m.group(1)
return get_headers(my_precomp)
except FileNotFoundError:
return []
def build_recursive_headers(parent, includes, headers, precomp_headers, level, output):
if level == 0:
return
level -= 1
full_precomp_headers = build_full_headers(includes, precomp_headers)
for pair in full_precomp_headers:
output.add(f"\"{parent[1]}\" -> \"{pair[1]}\" [color=red]\n")
full_headers = build_full_headers(includes, headers)
for pair in full_headers:
if level == args.depth - 1:
output.add(f"\"{parent[1]}\" -> \"{pair[1]}\" [color=blue]\n")
else:
output.add(f"\"{parent[1]}\" -> \"{pair[1]}\"\n")
new_headers = get_headers(pair[0])
if not pair[0].startswith("/usr/include") and "vcpkg" not in pair[0]:
build_recursive_headers(
pair, includes, new_headers, [], level, output)
def build_recursive_headers_explain(parent, includes, headers, precomp_headers, level, output):
global args
if level == 0:
return
if level == args.depth:
full_precomp_headers = build_full_headers(includes, precomp_headers)
for pair in full_precomp_headers:
output.add_edge(parent[0], pair[0])
level -= 1
full_headers = build_full_headers(includes, headers)
for pair in full_headers:
output.add_edge(parent[0], pair[0])
new_headers = get_headers(pair[0])
if not pair[0].startswith("/usr/include") and "vcpkg" not in pair[0] and ".pb." not in pair[0]:
build_recursive_headers_explain(
pair, includes, new_headers, [], level, output)
def remove_header_from_file(header, filename):
print(f" * {YELLOW}{header}{RESET} removed from {CYAN}{filename}{RESET}.")
r = re.compile(r"^#include\s*[\"<](.*)[\">]")
with open(filename, "r") as f:
lines = f.readlines()
with open(filename, "w") as f:
for l in lines:
ls = l.strip()
if l.startswith("#include"):
m = r.match(ls)
if m and header.endswith(m.group(1)):
continue
f.write(l)
if not os.path.isfile(args.compile_commands):
print("the compile_commands.json file must be provided, by default deps looks for it in the current path, otherwise you can provide it with the -c option.", file=sys.stderr)
sys.exit(1)
with open(args.compile_commands, "r") as f:
js = json.load(f)
# An array of pairs with (fullname, shortname) of the files given on the command line.
filename = []
# new_js contains only files matching those in filename.
new_js = []
for f in args.filename:
if f.startswith(home):
full_name = (f, f)
else:
full_name = (home + '/' + f, f)
for ff in js:
if ff['file'] == full_name[0]:
filename.append(full_name)
new_js.append(ff)
if not args.explain:
for full_name in filename:
output = set()
for entry in new_js:
if entry["file"] == full_name[0]:
result = parse_command(entry)
if 'pch' in result:
precomp_headers = get_precomp_headers(result['pch'])
else:
precomp_headers = []
includes = result['include_dirs']
headers = get_headers(full_name[0])
build_recursive_headers(
full_name, includes, headers, precomp_headers, args.depth, output)
break
if args.output:
output_file = args.output
else:
output_file = "/tmp/deps.dot"
with open(output_file, "w") as f:
f.write("digraph deps {\n")
for o in output:
f.write(o)
f.write("}\n")
if os.path.exists("/usr/bin/dot"):
os.system(f"/usr/bin/dot -Tpng {output_file} -o /tmp/deps.png")
if os.path.exists("/usr/bin/lximage-qt"):
os.system("/usr/bin/lximage-qt /tmp/deps.png")
elif os.path.exists("/usr/bin/eog"):
os.system("/usr/bin/eog /tmp/deps.png")
else:
print(f"Output written at '{output_file}'.")
else:
for full_name in filename:
print(f"Analyzing '{full_name[0]}'")
output = Graph()
for entry in new_js:
# A little hack so that if we specify no file, they are all analyzed.
if entry["file"] == full_name[0]:
result = parse_command(entry)
if 'pch' in result:
precomp_headers = get_precomp_headers(result['pch'])
else:
precomp_headers = []
includes = result['include_dirs']
headers = get_headers(full_name[0])
build_recursive_headers_explain(
full_name, includes, headers, precomp_headers, args.depth, output)
result = output.find_all_pairs_with_at_least_two_paths()
if result:
if args.fix:
for (source, destination), paths in result.items():
remove_header_from_file(destination, source)
else:
print(f"{GREEN}{full_name[0]}{RESET}:")
for (source, destination), paths in result.items():
print(f" * {YELLOW}{destination}{RESET} can be removed from {CYAN}{source}{RESET}.")
break