eclipse.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #
  2. # Copyright (c) 2006-2019, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2019-03-21 Bernard the first version
  9. # 2019-04-15 armink fix project update error
  10. #
  11. import os
  12. import sys
  13. import glob
  14. from utils import *
  15. from utils import _make_path_relative
  16. from utils import xml_indent
  17. import xml.etree.ElementTree as etree
  18. from xml.etree.ElementTree import SubElement
  19. source_pattern = ['*.c', '*.cpp', '*.cxx', '*.s', '*.S', '*.asm']
  20. def OSPath(path):
  21. import platform
  22. if type(path) == type('str'):
  23. if platform.system() == 'Windows':
  24. return path.replace('/', '\\')
  25. else:
  26. return path.replace('\\', '/')
  27. else:
  28. if platform.system() == 'Windows':
  29. return [item.replace('/', '\\') for item in path]
  30. else:
  31. return [item.replace('\\', '/') for item in path]
  32. # collect the build source code path and parent path
  33. def CollectPaths(paths):
  34. all_paths = []
  35. def ParentPaths(path):
  36. ret = os.path.dirname(path)
  37. if ret == path or ret == '':
  38. return []
  39. return [ret] + ParentPaths(ret)
  40. for path in paths:
  41. # path = os.path.abspath(path)
  42. path = path.replace('\\', '/')
  43. all_paths = all_paths + [path] + ParentPaths(path)
  44. all_paths = list(set(all_paths))
  45. return sorted(all_paths)
  46. '''
  47. Collect all of files under paths
  48. '''
  49. def CollectFiles(paths, pattern):
  50. files = []
  51. for path in paths:
  52. if type(pattern) == type(''):
  53. files = files + glob.glob(path + '/' + pattern)
  54. else:
  55. for item in pattern:
  56. # print('--> %s' % (path + '/' + item))
  57. files = files + glob.glob(path + '/' + item)
  58. return sorted(files)
  59. def CollectAllFilesinPath(path, pattern):
  60. files = []
  61. for item in pattern:
  62. files += glob.glob(path + '/' + item)
  63. list = os.listdir(path)
  64. if len(list):
  65. for item in list:
  66. if item.startswith('.'):
  67. continue
  68. if item == 'bsp':
  69. continue
  70. if os.path.isdir(os.path.join(path, item)):
  71. files = files + CollectAllFilesinPath(os.path.join(path, item), pattern)
  72. return files
  73. '''
  74. Exclude files from infiles
  75. '''
  76. def ExcludeFiles(infiles, files):
  77. in_files = set([OSPath(file) for file in infiles])
  78. exl_files = set([OSPath(file) for file in files])
  79. exl_files = in_files - exl_files
  80. return exl_files
  81. # caluclate the exclude path for project
  82. def ExcludePaths(filepath, paths):
  83. ret = []
  84. files = os.listdir(filepath)
  85. for file in files:
  86. if file.startswith('.'):
  87. continue
  88. fullname = os.path.join(filepath, file)
  89. if os.path.isdir(fullname):
  90. # print(fullname)
  91. if not fullname in paths:
  92. ret = ret + [fullname]
  93. else:
  94. ret = ret + ExcludePaths(fullname, paths)
  95. return ret
  96. def ConverToEclipsePathFormat(path):
  97. if path.startswith('.'):
  98. path = path[1:]
  99. return '"${workspace_loc:/${ProjName}/' + path + '}"'
  100. def HandleToolOption(tools, env, project):
  101. BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
  102. CPPDEFINES = project['CPPDEFINES']
  103. paths = [ConverToEclipsePathFormat(RelativeProjectPath(env, os.path.normpath(i)).replace('\\', '/')) for i in project['CPPPATH']]
  104. for tool in tools:
  105. if tool.get('id').find('c.compile') != 1:
  106. options = tool.findall('option')
  107. for option in options:
  108. if option.get('id').find('c.compiler.include.paths') != -1:
  109. # find all of paths in this project
  110. include_paths = option.findall('listOptionValue')
  111. project_paths = []
  112. for item in include_paths:
  113. project_paths += [item.get('value')]
  114. if len(project_paths) > 0:
  115. cproject_paths = set(paths) - set(project_paths)
  116. else:
  117. cproject_paths = paths
  118. # print('c.compiler.include.paths')
  119. cproject_paths = sorted(cproject_paths)
  120. for item in cproject_paths:
  121. SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
  122. if option.get('id').find('c.compiler.defs') != -1:
  123. defs = option.findall('listOptionValue')
  124. project_defs = []
  125. for item in defs:
  126. project_defs += [item.get('value')]
  127. if len(project_defs) > 0:
  128. cproject_defs = set(CPPDEFINES) - set(project_defs)
  129. else:
  130. cproject_defs = CPPDEFINES
  131. # print('c.compiler.defs')
  132. cproject_defs = sorted(cproject_defs)
  133. for item in cproject_defs:
  134. SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
  135. if tool.get('id').find('c.linker') != -1:
  136. options = tool.findall('option')
  137. for option in options:
  138. if option.get('id').find('c.linker.scriptfile') != -1:
  139. linker_script = 'link.lds'
  140. items = env['LINKFLAGS'].split(' ')
  141. if '-T' in items:
  142. linker_script = items[items.index('-T') + 1]
  143. linker_script = ConverToEclipsePathFormat(linker_script)
  144. listOptionValue = option.find('listOptionValue')
  145. if listOptionValue != None:
  146. listOptionValue.set('value', linker_script)
  147. else:
  148. SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': linker_script})
  149. if option.get('id').find('c.linker.nostart') != -1:
  150. if env['LINKFLAGS'].find('-nostartfiles') != -1:
  151. option.set('value', 'true')
  152. else:
  153. option.set('value', 'false')
  154. return
  155. def UpdateProjectStructure(env):
  156. bsp_root = env['BSP_ROOT']
  157. rtt_root = env['RTT_ROOT']
  158. if not rtt_root.startswith(bsp_root):
  159. to_SubElement = True
  160. # print('handle virtual root')
  161. # always use '/' path separator
  162. rtt_root = rtt_root.replace('\\', '/')
  163. # TODO create the virtual folder
  164. # project = etree.parse('.project')
  165. # root = project.getroot()
  166. #
  167. # linkedResources = root.find('linkedResources')
  168. # if linkedResources == None:
  169. # # add linkedResources
  170. # linkedResources = SubElement(root, 'linkedResources')
  171. # # print('add linkedResources')
  172. # else:
  173. # links = linkedResources.findall('link')
  174. # # search exist 'rt-thread' virtual folder
  175. # for link in links:
  176. # if link.find('name').text == 'rt-thread':
  177. # # handle location
  178. # to_SubElement = False
  179. # location = link.find('location')
  180. # location.text = rtt_root
  181. #
  182. # if to_SubElement:
  183. # # print('to subelement for virtual folder')
  184. # link = SubElement(linkedResources, 'link')
  185. # name = SubElement(link, 'name')
  186. # name.text = 'rt-thread'
  187. # type = SubElement(link, 'type')
  188. # type.text = '2'
  189. # location = SubElement(link, 'location')
  190. # location.text = rtt_root
  191. #
  192. # out = open('.project', 'w')
  193. # out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  194. # xml_indent(root)
  195. # out.write(etree.tostring(root, encoding='utf-8'))
  196. # out.close()
  197. return
  198. def GenExcluding(env, project):
  199. rtt_root = os.path.abspath(env['RTT_ROOT'])
  200. coll_dirs = CollectPaths(project['DIRS'])
  201. all_paths = [OSPath(path) for path in coll_dirs]
  202. exclude_paths = ExcludePaths(rtt_root, all_paths)
  203. paths = exclude_paths
  204. exclude_paths = []
  205. # remove the folder which not has source code by source_pattern
  206. for path in paths:
  207. # add bsp and libcpu folder and not collect source files (too more files)
  208. if path.endswith('rt-thread\\bsp') or path.endswith('rt-thread\\libcpu'):
  209. exclude_paths += [path]
  210. continue
  211. set = CollectAllFilesinPath(path, source_pattern)
  212. if len(set):
  213. exclude_paths += [path]
  214. exclude_paths = [RelativeProjectPath(env, path).replace('\\', '/') for path in exclude_paths]
  215. env['ExPaths'] = exclude_paths
  216. all_files = CollectFiles(all_paths, source_pattern)
  217. src_files = project['FILES']
  218. exclude_files = ExcludeFiles(all_files, src_files)
  219. exclude_files = [RelativeProjectPath(env, file).replace('\\', '/') for file in exclude_files]
  220. env['ExFiles'] = exclude_files
  221. return exclude_paths + exclude_files
  222. def RelativeProjectPath(env, path):
  223. project_root = os.path.abspath(env['BSP_ROOT'])
  224. rtt_root = os.path.abspath(env['RTT_ROOT'])
  225. if path.startswith(project_root):
  226. return _make_path_relative(project_root, path)
  227. if path.startswith(rtt_root):
  228. return 'rt-thread/' + _make_path_relative(rtt_root, path)
  229. # TODO add others folder
  230. print('ERROR: the ' + path + 'not support')
  231. return path
  232. def UpdateCproject(env, project, excluding):
  233. excluding = sorted(excluding)
  234. cproject = etree.parse('.cproject')
  235. root = cproject.getroot()
  236. cconfigurations = root.findall('storageModule/cconfiguration')
  237. for cconfiguration in cconfigurations:
  238. tools = cconfiguration.findall('storageModule/configuration/folderInfo/toolChain/tool')
  239. HandleToolOption(tools, env, project)
  240. sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
  241. entry = sourceEntries.find('entry')
  242. if entry != None:
  243. sourceEntries.remove(entry)
  244. value = ''
  245. for item in excluding:
  246. if value == '':
  247. value = item
  248. else:
  249. value += '|' + item
  250. SubElement(sourceEntries, 'entry', {'excluding': value, 'flags': 'VALUE_WORKSPACE_PATH|RESOLVED', 'kind':'sourcePath', 'name':""})
  251. # write back to .cproject
  252. out = open('.cproject', 'w')
  253. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
  254. out.write('<?fileVersion 4.0.0?>')
  255. xml_indent(root)
  256. out.write(etree.tostring(root, encoding='utf-8'))
  257. out.close()
  258. def TargetEclipse(env):
  259. global source_pattern
  260. print('Update eclipse setting...')
  261. if not os.path.exists('.cproject'):
  262. print('no eclipse CDT project found!')
  263. return
  264. project = ProjectInfo(env)
  265. # update the project file structure info on '.project' file
  266. UpdateProjectStructure(env)
  267. # generate the exclude paths and files
  268. excluding = GenExcluding(env, project)
  269. # update the project configuration on '.cproject' file
  270. UpdateCproject(env, project, excluding)
  271. print('done!')
  272. return