eclipse.py 9.7 KB

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