vsc.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. """
  25. Utils for VSCode
  26. """
  27. import os
  28. import json
  29. import utils
  30. import rtconfig
  31. from utils import _make_path_relative
  32. def delete_repeatelist(data):
  33. temp_dict = set([str(item) for item in data])
  34. data = [eval(i) for i in temp_dict]
  35. return data
  36. def GenerateCFiles(env):
  37. """
  38. Generate c_cpp_properties files
  39. """
  40. if not os.path.exists('.vscode'):
  41. os.mkdir('.vscode')
  42. vsc_file = open('.vscode/c_cpp_properties.json', 'w')
  43. if vsc_file:
  44. info = utils.ProjectInfo(env)
  45. cc = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
  46. cc = os.path.abspath(cc).replace('\\', '/')
  47. config_obj = {}
  48. config_obj['name'] = 'Win32'
  49. config_obj['defines'] = info['CPPDEFINES']
  50. config_obj['intelliSenseMode'] = 'clang-x64'
  51. config_obj['compilerPath'] = cc
  52. config_obj['cStandard'] = "c99"
  53. config_obj['cppStandard'] = "c++11"
  54. # format "a/b," to a/b. remove first quotation mark("),and remove end (",)
  55. includePath = []
  56. for i in info['CPPPATH']:
  57. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  58. includePath.append(_make_path_relative(os.getcwd(), i[1:len(i) - 2]))
  59. else:
  60. includePath.append(_make_path_relative(os.getcwd(), i))
  61. config_obj['includePath'] = includePath
  62. json_obj = {}
  63. json_obj['configurations'] = [config_obj]
  64. vsc_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  65. vsc_file.close()
  66. """
  67. Generate vscode.code-workspace files
  68. """
  69. vsc_space_file = open('vscode.code-workspace', 'w')
  70. if vsc_space_file:
  71. info = utils.ProjectInfo(env)
  72. path_list = []
  73. for i in info['CPPPATH']:
  74. if _make_path_relative(os.getcwd(), i)[0] == '.':
  75. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  76. path_list.append({'path':_make_path_relative(os.getcwd(), i[1:len(i) - 2])})
  77. else:
  78. path_list.append({'path':_make_path_relative(os.getcwd(), i)})
  79. for i in info['DIRS']:
  80. if _make_path_relative(os.getcwd(), i)[0] == '.':
  81. if i[0] == '\"' and i[len(i) - 2:len(i)] == '\",':
  82. path_list.append({'path':_make_path_relative(os.getcwd(), i[1:len(i) - 2])})
  83. else:
  84. path_list.append({'path':_make_path_relative(os.getcwd(), i)})
  85. json_obj = {}
  86. path_list = delete_repeatelist(path_list)
  87. path_list = sorted(path_list, key=lambda x: x["path"])
  88. target_path_list = []
  89. for path in path_list:
  90. if path['path'] != '.':
  91. path['name'] = 'rtthread/' + '/'.join([p for p in path['path'].split('\\') if p != '..'])
  92. json_obj['folders'] = path_list
  93. vsc_space_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  94. vsc_space_file.close()
  95. return
  96. def GenerateVSCode(env):
  97. print('Update setting files for VSCode...')
  98. GenerateCFiles(env)
  99. print('Done!')
  100. return