vsc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #
  2. # File : vsc.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2018-05-30 Bernard The first version
  23. # 2023-03-03 Supperthomas Add the vscode workspace config file
  24. # 2024-12-13 Supperthomas covert compile_commands.json to vscode workspace file
  25. """
  26. Utils for VSCode
  27. """
  28. import os
  29. import json
  30. import utils
  31. import rtconfig
  32. from utils import _make_path_relative
  33. def find_first_node_with_two_children(tree):
  34. for key, subtree in tree.items():
  35. if len(subtree) >= 2:
  36. return key, subtree
  37. result = find_first_node_with_two_children(subtree)
  38. if result:
  39. return result
  40. return None, None
  41. def filt_tree(tree):
  42. key, subtree = find_first_node_with_two_children(tree)
  43. if key:
  44. return {key: subtree}
  45. return {}
  46. def add_path_to_tree(tree, path):
  47. parts = path.split(os.sep)
  48. current_level = tree
  49. for part in parts:
  50. if part not in current_level:
  51. current_level[part] = {}
  52. current_level = current_level[part]
  53. def build_tree(paths):
  54. tree = {}
  55. current_working_directory = os.getcwd()
  56. current_folder_name = os.path.basename(current_working_directory)
  57. #过滤异常和不存在的路径
  58. relative_dirs = []
  59. for path in paths:
  60. normalized_path = os.path.normpath(path)
  61. try:
  62. rel_path = os.path.relpath(normalized_path, start=current_working_directory)
  63. add_path_to_tree(tree, normalized_path)
  64. except ValueError:
  65. print(f"Remove unexcpect dir:{path}")
  66. return tree
  67. def print_tree(tree, indent=''):
  68. for key, subtree in sorted(tree.items()):
  69. print(indent + key)
  70. print_tree(subtree, indent + ' ')
  71. def extract_source_dirs(compile_commands):
  72. source_dirs = set()
  73. for entry in compile_commands:
  74. file_path = os.path.abspath(entry['file'])
  75. if file_path.endswith('.c'):
  76. dir_path = os.path.dirname(file_path)
  77. source_dirs.add(dir_path)
  78. # command 或者arguments
  79. command = entry.get('command') or entry.get('arguments')
  80. if isinstance(command, str):
  81. parts = command.split()
  82. else:
  83. parts = command
  84. # 读取-I或者/I
  85. for i, part in enumerate(parts):
  86. if part.startswith('-I'):
  87. include_dir = part[2:] if len(part) > 2 else parts[i + 1]
  88. source_dirs.add(os.path.abspath(include_dir))
  89. elif part.startswith('/I'):
  90. include_dir = part[2:] if len(part) > 2 else parts[i + 1]
  91. source_dirs.add(os.path.abspath(include_dir))
  92. #print(f"Source Directories: {source_dirs}")
  93. return sorted(source_dirs)
  94. def is_path_in_tree(path, tree):
  95. parts = path.split(os.sep)
  96. current_level = tree
  97. found_first_node = False
  98. root_key = list(tree.keys())[0]
  99. #print(root_key)
  100. #print(path)
  101. index_start = parts.index(root_key)
  102. length = len(parts)
  103. try:
  104. for i in range(index_start, length):
  105. current_level = current_level[parts[i]]
  106. return True
  107. except KeyError:
  108. return False
  109. def generate_code_workspace_file(source_dirs,command_json_path,root_path):
  110. current_working_directory = os.getcwd()
  111. current_folder_name = os.path.basename(current_working_directory)
  112. relative_dirs = []
  113. for dir_path in source_dirs:
  114. try:
  115. rel_path = os.path.relpath(dir_path, root_path)
  116. relative_dirs.append(rel_path)
  117. except ValueError:
  118. continue
  119. root_rel_path = os.path.relpath(root_path, current_working_directory)
  120. command_json_path = os.path.relpath(current_working_directory, root_path) + os.sep
  121. workspace_data = {
  122. "folders": [
  123. {
  124. "path": f"{root_rel_path}"
  125. }
  126. ],
  127. "settings": {
  128. "clangd.arguments": [
  129. f"--compile-commands-dir={command_json_path}",
  130. "--header-insertion=never"
  131. ],
  132. "files.exclude": {dir.replace('\\','/'): True for dir in sorted(relative_dirs)}
  133. }
  134. }
  135. workspace_filename = f'{current_folder_name}.code-workspace'
  136. # print(workspace_data)
  137. with open(workspace_filename, 'w') as f:
  138. json.dump(workspace_data, f, indent=4)
  139. print(f'Workspace file {workspace_filename} created.')
  140. def command_json_to_workspace(root_path,command_json_path):
  141. with open('build/compile_commands.json', 'r') as f:
  142. compile_commands = json.load(f)
  143. source_dirs = extract_source_dirs(compile_commands)
  144. tree = build_tree(source_dirs)
  145. #print_tree(tree)
  146. filtered_tree = filt_tree(tree)
  147. print("Filtered Directory Tree:")
  148. #print_tree(filtered_tree)
  149. # 打印filtered_tree的root节点的相对路径
  150. root_key = list(filtered_tree.keys())[0]
  151. print(f"Root node relative path: {root_key}")
  152. # 初始化exclude_fold集合
  153. exclude_fold = set()
  154. # os.chdir(root_path)
  155. # 轮询root文件夹下面的每一个文件夹和子文件夹
  156. for root, dirs, files in os.walk(root_path):
  157. # 检查当前root是否在filtered_tree中
  158. if not is_path_in_tree(root, filtered_tree):
  159. exclude_fold.add(root)
  160. dirs[:] = [] # 不往下轮询子文件夹
  161. continue
  162. for dir in dirs:
  163. dir_path = os.path.join(root, dir)
  164. if not is_path_in_tree(dir_path, filtered_tree):
  165. exclude_fold.add(dir_path)
  166. #print("Excluded Folders:")
  167. #print(exclude_fold)
  168. generate_code_workspace_file(exclude_fold,command_json_path,root_path)
  169. def delete_repeatelist(data):
  170. temp_dict = set([str(item) for item in data])
  171. data = [eval(i) for i in temp_dict]
  172. return data
  173. def GenerateCFiles(env):
  174. """
  175. Generate c_cpp_properties.json and build/compile_commands.json files
  176. """
  177. if not os.path.exists('.vscode'):
  178. os.mkdir('.vscode')
  179. vsc_file = open('.vscode/c_cpp_properties.json', 'w')
  180. if vsc_file:
  181. info = utils.ProjectInfo(env)
  182. cc = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
  183. cc = os.path.abspath(cc).replace('\\', '/')
  184. config_obj = {}
  185. config_obj['name'] = 'rt-thread'
  186. config_obj['defines'] = info['CPPDEFINES']
  187. intelliSenseMode = 'gcc-arm'
  188. if cc.find('aarch64') != -1:
  189. intelliSenseMode = 'gcc-arm64'
  190. elif cc.find('arm') != -1:
  191. intelliSenseMode = 'gcc-arm'
  192. config_obj['intelliSenseMode'] = intelliSenseMode
  193. config_obj['compilerPath'] = cc
  194. config_obj['cStandard'] = "c99"
  195. config_obj['cppStandard'] = "c++11"
  196. config_obj['compileCommands'] ="build/compile_commands.json"
  197. # format "a/b," to a/b. remove first quotation mark("),and remove end (",)
  198. includePath = []
  199. for i in info['CPPPATH']:
  200. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  201. includePath.append(_make_path_relative(os.getcwd(), i[1:len(i) - 2]))
  202. else:
  203. includePath.append(_make_path_relative(os.getcwd(), i))
  204. config_obj['includePath'] = includePath
  205. json_obj = {}
  206. json_obj['configurations'] = [config_obj]
  207. vsc_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  208. vsc_file.close()
  209. """
  210. Generate vscode.code-workspace files by build/compile_commands.json
  211. """
  212. if os.path.exists('build/compile_commands.json'):
  213. command_json_to_workspace(env['RTT_ROOT'],'build/compile_commands.json')
  214. return
  215. """
  216. Generate vscode.code-workspace files
  217. """
  218. vsc_space_file = open('vscode.code-workspace', 'w')
  219. if vsc_space_file:
  220. info = utils.ProjectInfo(env)
  221. path_list = []
  222. for i in info['CPPPATH']:
  223. if _make_path_relative(os.getcwd(), i)[0] == '.':
  224. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  225. path_list.append({'path':_make_path_relative(os.getcwd(), i[1:len(i) - 2])})
  226. else:
  227. path_list.append({'path':_make_path_relative(os.getcwd(), i)})
  228. for i in info['DIRS']:
  229. if _make_path_relative(os.getcwd(), i)[0] == '.':
  230. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  231. path_list.append({'path':_make_path_relative(os.getcwd(), i[1:len(i) - 2])})
  232. else:
  233. path_list.append({'path':_make_path_relative(os.getcwd(), i)})
  234. json_obj = {}
  235. path_list = delete_repeatelist(path_list)
  236. path_list = sorted(path_list, key=lambda x: x["path"])
  237. target_path_list = []
  238. for path in path_list:
  239. if path['path'] != '.':
  240. normalized_path = path['path'].replace('\\', os.path.sep)
  241. segments = [p for p in normalized_path.split(os.path.sep) if p != '..']
  242. path['name'] = 'rtthread/' + '/'.join(segments)
  243. json_obj['folders'] = path_list
  244. if os.path.exists('build/compile_commands.json'):
  245. json_obj['settings'] = {
  246. "clangd.arguments": [
  247. "--compile-commands-dir=.",
  248. "--header-insertion=never"
  249. ]
  250. }
  251. vsc_space_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  252. vsc_space_file.close()
  253. return
  254. def GenerateProjectFiles(env):
  255. """
  256. Generate project.json file
  257. """
  258. if not os.path.exists('.vscode'):
  259. os.mkdir('.vscode')
  260. project = env['project']
  261. vsc_file = open('.vscode/project.json', 'w')
  262. if vsc_file:
  263. groups = []
  264. for group in project:
  265. if len(group['src']) > 0:
  266. item = {}
  267. item['name'] = group['name']
  268. item['path'] = _make_path_relative(os.getcwd(), group['path'])
  269. item['files'] = []
  270. for fn in group['src']:
  271. item['files'].append(str(fn))
  272. # append SConscript if exist
  273. if os.path.exists(os.path.join(item['path'], 'SConscript')):
  274. item['files'].append(os.path.join(item['path'], 'SConscript'))
  275. groups.append(item)
  276. json_dict = {}
  277. json_dict['RT-Thread'] = env['RTT_ROOT']
  278. json_dict['Groups'] = groups
  279. # write groups to project.json
  280. vsc_file.write(json.dumps(json_dict, ensure_ascii=False, indent=4))
  281. vsc_file.close()
  282. return
  283. def GenerateVSCode(env):
  284. print('Update setting files for VSCode...')
  285. GenerateProjectFiles(env)
  286. GenerateCFiles(env)
  287. print('Done!')
  288. return