Skip to content

Commit 74b2d3d

Browse files
supperthomasmysterywolf
authored andcommitted
[tools] generate workspace by compile_commands.json
1 parent 23256d3 commit 74b2d3d

File tree

1 file changed

+171
-1
lines changed

1 file changed

+171
-1
lines changed

tools/vsc.py

+171-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Date Author Notes
2222
# 2018-05-30 Bernard The first version
2323
# 2023-03-03 Supperthomas Add the vscode workspace config file
24-
24+
# 2024-12-13 Supperthomas covert compile_commands.json to vscode workspace file
2525
"""
2626
Utils for VSCode
2727
"""
@@ -32,6 +32,169 @@
3232
import rtconfig
3333

3434
from utils import _make_path_relative
35+
def find_first_node_with_two_children(tree):
36+
for key, subtree in tree.items():
37+
if len(subtree) >= 2:
38+
return key, subtree
39+
result = find_first_node_with_two_children(subtree)
40+
if result:
41+
return result
42+
return None, None
43+
44+
45+
def filt_tree(tree):
46+
key, subtree = find_first_node_with_two_children(tree)
47+
if key:
48+
return {key: subtree}
49+
return {}
50+
51+
52+
def add_path_to_tree(tree, path):
53+
parts = path.split(os.sep)
54+
current_level = tree
55+
for part in parts:
56+
if part not in current_level:
57+
current_level[part] = {}
58+
current_level = current_level[part]
59+
60+
61+
def build_tree(paths):
62+
tree = {}
63+
current_working_directory = os.getcwd()
64+
current_folder_name = os.path.basename(current_working_directory)
65+
#过滤异常和不存在的路径
66+
relative_dirs = []
67+
for path in paths:
68+
normalized_path = os.path.normpath(path)
69+
try:
70+
rel_path = os.path.relpath(normalized_path, start=current_working_directory)
71+
add_path_to_tree(tree, normalized_path)
72+
except ValueError:
73+
print(f"Remove unexcpect dir:{path}")
74+
75+
return tree
76+
77+
def print_tree(tree, indent=''):
78+
for key, subtree in sorted(tree.items()):
79+
print(indent + key)
80+
print_tree(subtree, indent + ' ')
81+
82+
83+
def extract_source_dirs(compile_commands):
84+
source_dirs = set()
85+
86+
for entry in compile_commands:
87+
file_path = os.path.abspath(entry['file'])
88+
89+
if file_path.endswith('.c'):
90+
dir_path = os.path.dirname(file_path)
91+
source_dirs.add(dir_path)
92+
# command 或者arguments
93+
command = entry.get('command') or entry.get('arguments')
94+
95+
if isinstance(command, str):
96+
parts = command.split()
97+
else:
98+
parts = command
99+
# 读取-I或者/I
100+
for i, part in enumerate(parts):
101+
if part.startswith('-I'):
102+
include_dir = part[2:] if len(part) > 2 else parts[i + 1]
103+
source_dirs.add(os.path.abspath(include_dir))
104+
elif part.startswith('/I'):
105+
include_dir = part[2:] if len(part) > 2 else parts[i + 1]
106+
source_dirs.add(os.path.abspath(include_dir))
107+
#print(f"Source Directories: {source_dirs}")
108+
return sorted(source_dirs)
109+
110+
111+
def is_path_in_tree(path, tree):
112+
parts = path.split(os.sep)
113+
current_level = tree
114+
found_first_node = False
115+
root_key = list(tree.keys())[0]
116+
#print(root_key)
117+
#print(path)
118+
index_start = parts.index(root_key)
119+
length = len(parts)
120+
try:
121+
for i in range(index_start, length):
122+
current_level = current_level[parts[i]]
123+
return True
124+
except KeyError:
125+
return False
126+
127+
128+
def generate_code_workspace_file(source_dirs,command_json_path,root_path):
129+
current_working_directory = os.getcwd()
130+
current_folder_name = os.path.basename(current_working_directory)
131+
132+
relative_dirs = []
133+
for dir_path in source_dirs:
134+
try:
135+
rel_path = os.path.relpath(dir_path, root_path)
136+
relative_dirs.append(rel_path)
137+
except ValueError:
138+
continue
139+
140+
root_rel_path = os.path.relpath(root_path, current_working_directory)
141+
command_json_path = os.path.relpath(current_working_directory, root_path) + os.sep
142+
workspace_data = {
143+
"folders": [
144+
{
145+
"path": f"{root_rel_path}"
146+
}
147+
],
148+
"settings": {
149+
"clangd.arguments": [
150+
f"--compile-commands-dir={command_json_path}",
151+
"--header-insertion=never"
152+
],
153+
"files.exclude": {dir.replace('\\','/'): True for dir in sorted(relative_dirs)}
154+
}
155+
}
156+
workspace_filename = f'{current_folder_name}.code-workspace'
157+
# print(workspace_data)
158+
with open(workspace_filename, 'w') as f:
159+
json.dump(workspace_data, f, indent=4)
160+
161+
print(f'Workspace file {workspace_filename} created.')
162+
163+
def command_json_to_workspace(root_path,command_json_path):
164+
165+
with open('compile_commands.json', 'r') as f:
166+
compile_commands = json.load(f)
167+
168+
source_dirs = extract_source_dirs(compile_commands)
169+
tree = build_tree(source_dirs)
170+
#print_tree(tree)
171+
filtered_tree = filt_tree(tree)
172+
print("Filtered Directory Tree:")
173+
#print_tree(filtered_tree)
174+
175+
# 打印filtered_tree的root节点的相对路径
176+
root_key = list(filtered_tree.keys())[0]
177+
print(f"Root node relative path: {root_key}")
178+
179+
# 初始化exclude_fold集合
180+
exclude_fold = set()
181+
182+
# os.chdir(root_path)
183+
# 轮询root文件夹下面的每一个文件夹和子文件夹
184+
for root, dirs, files in os.walk(root_path):
185+
# 检查当前root是否在filtered_tree中
186+
if not is_path_in_tree(root, filtered_tree):
187+
exclude_fold.add(root)
188+
dirs[:] = [] # 不往下轮询子文件夹
189+
continue
190+
for dir in dirs:
191+
dir_path = os.path.join(root, dir)
192+
if not is_path_in_tree(dir_path, filtered_tree):
193+
exclude_fold.add(dir_path)
194+
195+
#print("Excluded Folders:")
196+
#print(exclude_fold)
197+
generate_code_workspace_file(exclude_fold,command_json_path,root_path)
35198

36199
def delete_repeatelist(data):
37200
temp_dict = set([str(item) for item in data])
@@ -83,6 +246,13 @@ def GenerateCFiles(env):
83246
vsc_file.close()
84247

85248
"""
249+
Generate vscode.code-workspace files by compile_commands.json
250+
"""
251+
if os.path.exists('compile_commands.json'):
252+
253+
command_json_to_workspace(env['RTT_ROOT'],'compile_commands.json')
254+
return
255+
"""
86256
Generate vscode.code-workspace files
87257
"""
88258
vsc_space_file = open('vscode.code-workspace', 'w')

0 commit comments

Comments
 (0)