keil.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #
  2. # File : keil.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, 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. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import os
  25. import sys
  26. import string
  27. import xml.etree.ElementTree as etree
  28. from xml.etree.ElementTree import SubElement
  29. from utils import _make_path_relative
  30. from utils import xml_indent
  31. fs_encoding = sys.getfilesystemencoding()
  32. def _get_filetype(fn):
  33. if fn.rfind('.c') != -1 or fn.rfind('.C') != -1 or fn.rfind('.cpp') != -1:
  34. return 1
  35. # assemble file type
  36. if fn.rfind('.s') != -1 or fn.rfind('.S') != -1:
  37. return 2
  38. # header type
  39. if fn.rfind('.h') != -1:
  40. return 5
  41. if fn.rfind('.lib') != -1:
  42. return 4
  43. # other filetype
  44. return 5
  45. def MDK4AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  46. group = SubElement(parent, 'Group')
  47. group_name = SubElement(group, 'GroupName')
  48. group_name.text = name
  49. name = os.path.basename(filename)
  50. path = os.path.dirname (filename)
  51. basename = os.path.basename(path)
  52. path = _make_path_relative(project_path, path)
  53. path = os.path.join(path, name)
  54. files = SubElement(group, 'Files')
  55. file = SubElement(files, 'File')
  56. file_name = SubElement(file, 'FileName')
  57. name = os.path.basename(path)
  58. if name.find('.cpp') != -1:
  59. obj_name = name.replace('.cpp', '.o')
  60. elif name.find('.c') != -1:
  61. obj_name = name.replace('.c', '.o')
  62. elif name.find('.s') != -1:
  63. obj_name = name.replace('.s', '.o')
  64. elif name.find('.S') != -1:
  65. obj_name = name.replace('.s', '.o')
  66. if ProjectFiles.count(obj_name):
  67. name = basename + '_' + name
  68. ProjectFiles.append(obj_name)
  69. file_name.text = name.decode(fs_encoding)
  70. file_type = SubElement(file, 'FileType')
  71. file_type.text = '%d' % _get_filetype(name)
  72. file_path = SubElement(file, 'FilePath')
  73. file_path.text = path.decode(fs_encoding)
  74. def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
  75. # don't add an empty group
  76. if len(files) == 0:
  77. return
  78. group = SubElement(parent, 'Group')
  79. group_name = SubElement(group, 'GroupName')
  80. group_name.text = name
  81. for f in files:
  82. fn = f.rfile()
  83. name = fn.name
  84. path = os.path.dirname(fn.abspath)
  85. basename = os.path.basename(path)
  86. path = _make_path_relative(project_path, path)
  87. path = os.path.join(path, name)
  88. files = SubElement(group, 'Files')
  89. file = SubElement(files, 'File')
  90. file_name = SubElement(file, 'FileName')
  91. name = os.path.basename(path)
  92. if name.find('.cpp') != -1:
  93. obj_name = name.replace('.cpp', '.o')
  94. elif name.find('.c') != -1:
  95. obj_name = name.replace('.c', '.o')
  96. elif name.find('.s') != -1:
  97. obj_name = name.replace('.s', '.o')
  98. elif name.find('.S') != -1:
  99. obj_name = name.replace('.s', '.o')
  100. if ProjectFiles.count(obj_name):
  101. name = basename + '_' + name
  102. ProjectFiles.append(obj_name)
  103. file_name.text = name.decode(fs_encoding)
  104. file_type = SubElement(file, 'FileType')
  105. file_type.text = '%d' % _get_filetype(name)
  106. file_path = SubElement(file, 'FilePath')
  107. file_path.text = path.decode(fs_encoding)
  108. def MDK4Project(target, script):
  109. project_path = os.path.dirname(os.path.abspath(target))
  110. project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
  111. if os.path.isfile(project_uvopt):
  112. os.unlink(project_uvopt)
  113. tree = etree.parse('template.uvproj')
  114. root = tree.getroot()
  115. out = file(target, 'wb')
  116. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  117. CPPPATH = []
  118. CPPDEFINES = []
  119. LINKFLAGS = ''
  120. CCFLAGS = ''
  121. ProjectFiles = []
  122. # add group
  123. groups = tree.find('Targets/Target/Groups')
  124. if groups is None:
  125. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  126. groups.clear() # clean old groups
  127. for group in script:
  128. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  129. # get each include path
  130. if group.has_key('CPPPATH') and group['CPPPATH']:
  131. if CPPPATH:
  132. CPPPATH += group['CPPPATH']
  133. else:
  134. CPPPATH += group['CPPPATH']
  135. # get each group's definitions
  136. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  137. if CPPDEFINES:
  138. CPPDEFINES += group['CPPDEFINES']
  139. else:
  140. CPPDEFINES += group['CPPDEFINES']
  141. # get each group's link flags
  142. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  143. if LINKFLAGS:
  144. LINKFLAGS += ' ' + group['LINKFLAGS']
  145. else:
  146. LINKFLAGS += group['LINKFLAGS']
  147. if group.has_key('LIBS') and group['LIBS']:
  148. for item in group['LIBS']:
  149. lib_path = ''
  150. for path_item in group['LIBPATH']:
  151. full_path = os.path.join(path_item, item + '.lib')
  152. if os.path.isfile(full_path): # has this library
  153. lib_path = full_path
  154. if lib_path != '':
  155. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  156. # write include path, definitions and link flags
  157. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  158. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
  159. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  160. Define.text = ', '.join(set(CPPDEFINES))
  161. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  162. Misc.text = LINKFLAGS
  163. xml_indent(root)
  164. out.write(etree.tostring(root, encoding='utf-8'))
  165. out.close()
  166. # copy uvopt file
  167. if os.path.exists('template.uvopt'):
  168. import shutil
  169. shutil.copy2('template.uvopt', 'project.uvopt')
  170. def MDK5AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  171. group = SubElement(parent, 'Group')
  172. group_name = SubElement(group, 'GroupName')
  173. group_name.text = name
  174. name = os.path.basename(filename)
  175. path = os.path.dirname (filename)
  176. basename = os.path.basename(path)
  177. path = _make_path_relative(project_path, path)
  178. path = os.path.join(path, name)
  179. files = SubElement(group, 'Files')
  180. file = SubElement(files, 'File')
  181. file_name = SubElement(file, 'FileName')
  182. name = os.path.basename(path)
  183. if ProjectFiles.count(name):
  184. name = basename + '_' + name
  185. ProjectFiles.append(name)
  186. file_name.text = name.decode(fs_encoding)
  187. file_type = SubElement(file, 'FileType')
  188. file_type.text = '%d' % _get_filetype(name)
  189. file_path = SubElement(file, 'FilePath')
  190. file_path.text = path.decode(fs_encoding)
  191. def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
  192. # don't add an empty group
  193. if len(files) == 0:
  194. return
  195. group = SubElement(parent, 'Group')
  196. group_name = SubElement(group, 'GroupName')
  197. group_name.text = name
  198. for f in files:
  199. fn = f.rfile()
  200. name = fn.name
  201. path = os.path.dirname(fn.abspath)
  202. basename = os.path.basename(path)
  203. path = _make_path_relative(project_path, path)
  204. path = os.path.join(path, name)
  205. files = SubElement(group, 'Files')
  206. file = SubElement(files, 'File')
  207. file_name = SubElement(file, 'FileName')
  208. name = os.path.basename(path)
  209. if ProjectFiles.count(name):
  210. name = basename + '_' + name
  211. ProjectFiles.append(name)
  212. file_name.text = name.decode(fs_encoding)
  213. file_type = SubElement(file, 'FileType')
  214. file_type.text = '%d' % _get_filetype(name)
  215. file_path = SubElement(file, 'FilePath')
  216. file_path.text = path.decode(fs_encoding)
  217. def MDK5Project(target, script):
  218. project_path = os.path.dirname(os.path.abspath(target))
  219. project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
  220. if os.path.isfile(project_uvopt):
  221. os.unlink(project_uvopt)
  222. tree = etree.parse('template.uvprojx')
  223. root = tree.getroot()
  224. out = file(target, 'wb')
  225. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  226. CPPPATH = []
  227. CPPDEFINES = []
  228. LINKFLAGS = ''
  229. CCFLAGS = ''
  230. ProjectFiles = []
  231. # add group
  232. groups = tree.find('Targets/Target/Groups')
  233. if groups is None:
  234. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  235. for group in script:
  236. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  237. # get each include path
  238. if group.has_key('CPPPATH') and group['CPPPATH']:
  239. if CPPPATH:
  240. CPPPATH += group['CPPPATH']
  241. else:
  242. CPPPATH += group['CPPPATH']
  243. # get each group's definitions
  244. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  245. if CPPDEFINES:
  246. CPPDEFINES += group['CPPDEFINES']
  247. else:
  248. CPPDEFINES += group['CPPDEFINES']
  249. # get each group's link flags
  250. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  251. if LINKFLAGS:
  252. LINKFLAGS += ' ' + group['LINKFLAGS']
  253. else:
  254. LINKFLAGS += group['LINKFLAGS']
  255. if group.has_key('LIBS') and group['LIBS']:
  256. for item in group['LIBS']:
  257. lib_path = ''
  258. for path_item in group['LIBPATH']:
  259. full_path = os.path.join(path_item, item + '.lib')
  260. if os.path.isfile(full_path): # has this library
  261. lib_path = full_path
  262. if lib_path != '':
  263. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  264. # remove repeat path
  265. paths = set()
  266. for path in CPPPATH:
  267. inc = _make_path_relative(project_path, os.path.normpath(path))
  268. paths.add(inc) #.replace('\\', '/')
  269. paths = [i for i in paths]
  270. paths.sort()
  271. CPPPATH = string.join(paths, ';')
  272. definitions = [i for i in set(CPPDEFINES)]
  273. CPPDEFINES = string.join(definitions, ', ')
  274. # write include path, definitions and link flags
  275. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  276. IncludePath.text = CPPPATH
  277. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  278. Define.text = CPPDEFINES
  279. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  280. Misc.text = LINKFLAGS
  281. xml_indent(root)
  282. out.write(etree.tostring(root, encoding='utf-8'))
  283. out.close()
  284. # copy uvopt file
  285. if os.path.exists('template.uvoptx'):
  286. import shutil
  287. shutil.copy2('template.uvoptx', 'project.uvoptx')
  288. def MDKProject(target, script):
  289. template = file('template.Uv2', "rb")
  290. lines = template.readlines()
  291. project = file(target, "wb")
  292. project_path = os.path.dirname(os.path.abspath(target))
  293. line_index = 5
  294. # write group
  295. for group in script:
  296. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  297. line_index += 1
  298. lines.insert(line_index, '\r\n')
  299. line_index += 1
  300. # write file
  301. ProjectFiles = []
  302. CPPPATH = []
  303. CPPDEFINES = []
  304. LINKFLAGS = ''
  305. CCFLAGS = ''
  306. # number of groups
  307. group_index = 1
  308. for group in script:
  309. # print group['name']
  310. # get each include path
  311. if group.has_key('CPPPATH') and group['CPPPATH']:
  312. if CPPPATH:
  313. CPPPATH += group['CPPPATH']
  314. else:
  315. CPPPATH += group['CPPPATH']
  316. # get each group's definitions
  317. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  318. if CPPDEFINES:
  319. CPPDEFINES += ';' + group['CPPDEFINES']
  320. else:
  321. CPPDEFINES += group['CPPDEFINES']
  322. # get each group's link flags
  323. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  324. if LINKFLAGS:
  325. LINKFLAGS += ' ' + group['LINKFLAGS']
  326. else:
  327. LINKFLAGS += group['LINKFLAGS']
  328. # generate file items
  329. for node in group['src']:
  330. fn = node.rfile()
  331. name = fn.name
  332. path = os.path.dirname(fn.abspath)
  333. basename = os.path.basename(path)
  334. path = _make_path_relative(project_path, path)
  335. path = os.path.join(path, name)
  336. if ProjectFiles.count(name):
  337. name = basename + '_' + name
  338. ProjectFiles.append(name)
  339. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  340. % (group_index, _get_filetype(name), path, name))
  341. line_index += 1
  342. group_index = group_index + 1
  343. lines.insert(line_index, '\r\n')
  344. line_index += 1
  345. # remove repeat path
  346. paths = set()
  347. for path in CPPPATH:
  348. inc = _make_path_relative(project_path, os.path.normpath(path))
  349. paths.add(inc) #.replace('\\', '/')
  350. paths = [i for i in paths]
  351. CPPPATH = string.join(paths, ';')
  352. definitions = [i for i in set(CPPDEFINES)]
  353. CPPDEFINES = string.join(definitions, ', ')
  354. while line_index < len(lines):
  355. if lines[line_index].startswith(' ADSCINCD '):
  356. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  357. if lines[line_index].startswith(' ADSLDMC ('):
  358. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  359. if lines[line_index].startswith(' ADSCDEFN ('):
  360. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  361. line_index += 1
  362. # write project
  363. for line in lines:
  364. project.write(line)
  365. project.close()