keil.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 name.find('.cpp') != -1:
  184. obj_name = name.replace('.cpp', '.o')
  185. elif name.find('.c') != -1:
  186. obj_name = name.replace('.c', '.o')
  187. elif name.find('.s') != -1:
  188. obj_name = name.replace('.s', '.o')
  189. elif name.find('.S') != -1:
  190. obj_name = name.replace('.s', '.o')
  191. if ProjectFiles.count(obj_name):
  192. name = basename + '_' + name
  193. ProjectFiles.append(obj_name)
  194. file_name.text = name.decode(fs_encoding)
  195. file_type = SubElement(file, 'FileType')
  196. file_type.text = '%d' % _get_filetype(name)
  197. file_path = SubElement(file, 'FilePath')
  198. file_path.text = path.decode(fs_encoding)
  199. def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
  200. # don't add an empty group
  201. if len(files) == 0:
  202. return
  203. group = SubElement(parent, 'Group')
  204. group_name = SubElement(group, 'GroupName')
  205. group_name.text = name
  206. for f in files:
  207. fn = f.rfile()
  208. name = fn.name
  209. path = os.path.dirname(fn.abspath)
  210. basename = os.path.basename(path)
  211. path = _make_path_relative(project_path, path)
  212. path = os.path.join(path, name)
  213. files = SubElement(group, 'Files')
  214. file = SubElement(files, 'File')
  215. file_name = SubElement(file, 'FileName')
  216. name = os.path.basename(path)
  217. if name.find('.cpp') != -1:
  218. obj_name = name.replace('.cpp', '.o')
  219. elif name.find('.c') != -1:
  220. obj_name = name.replace('.c', '.o')
  221. elif name.find('.s') != -1:
  222. obj_name = name.replace('.s', '.o')
  223. elif name.find('.S') != -1:
  224. obj_name = name.replace('.s', '.o')
  225. if ProjectFiles.count(obj_name):
  226. name = basename + '_' + name
  227. ProjectFiles.append(obj_name)
  228. file_name.text = name.decode(fs_encoding)
  229. file_type = SubElement(file, 'FileType')
  230. file_type.text = '%d' % _get_filetype(name)
  231. file_path = SubElement(file, 'FilePath')
  232. file_path.text = path.decode(fs_encoding)
  233. def MDK5Project(target, script):
  234. project_path = os.path.dirname(os.path.abspath(target))
  235. project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
  236. if os.path.isfile(project_uvopt):
  237. os.unlink(project_uvopt)
  238. tree = etree.parse('template.uvprojx')
  239. root = tree.getroot()
  240. out = file(target, 'wb')
  241. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  242. CPPPATH = []
  243. CPPDEFINES = []
  244. LINKFLAGS = ''
  245. CCFLAGS = ''
  246. ProjectFiles = []
  247. # add group
  248. groups = tree.find('Targets/Target/Groups')
  249. if groups is None:
  250. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  251. for group in script:
  252. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  253. # get each include path
  254. if group.has_key('CPPPATH') and group['CPPPATH']:
  255. if CPPPATH:
  256. CPPPATH += group['CPPPATH']
  257. else:
  258. CPPPATH += group['CPPPATH']
  259. # get each group's definitions
  260. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  261. if CPPDEFINES:
  262. CPPDEFINES += group['CPPDEFINES']
  263. else:
  264. CPPDEFINES += group['CPPDEFINES']
  265. # get each group's link flags
  266. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  267. if LINKFLAGS:
  268. LINKFLAGS += ' ' + group['LINKFLAGS']
  269. else:
  270. LINKFLAGS += group['LINKFLAGS']
  271. if group.has_key('LIBS') and group['LIBS']:
  272. for item in group['LIBS']:
  273. lib_path = ''
  274. for path_item in group['LIBPATH']:
  275. full_path = os.path.join(path_item, item + '.lib')
  276. if os.path.isfile(full_path): # has this library
  277. lib_path = full_path
  278. if lib_path != '':
  279. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  280. # remove repeat path
  281. paths = set()
  282. for path in CPPPATH:
  283. inc = _make_path_relative(project_path, os.path.normpath(path))
  284. paths.add(inc) #.replace('\\', '/')
  285. paths = [i for i in paths]
  286. paths.sort()
  287. CPPPATH = string.join(paths, ';')
  288. definitions = [i for i in set(CPPDEFINES)]
  289. CPPDEFINES = string.join(definitions, ', ')
  290. # write include path, definitions and link flags
  291. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  292. IncludePath.text = CPPPATH
  293. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  294. Define.text = CPPDEFINES
  295. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  296. Misc.text = LINKFLAGS
  297. xml_indent(root)
  298. out.write(etree.tostring(root, encoding='utf-8'))
  299. out.close()
  300. # copy uvopt file
  301. if os.path.exists('template.uvoptx'):
  302. import shutil
  303. shutil.copy2('template.uvoptx', 'project.uvoptx')
  304. def MDKProject(target, script):
  305. template = file('template.Uv2', "rb")
  306. lines = template.readlines()
  307. project = file(target, "wb")
  308. project_path = os.path.dirname(os.path.abspath(target))
  309. line_index = 5
  310. # write group
  311. for group in script:
  312. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  313. line_index += 1
  314. lines.insert(line_index, '\r\n')
  315. line_index += 1
  316. # write file
  317. ProjectFiles = []
  318. CPPPATH = []
  319. CPPDEFINES = []
  320. LINKFLAGS = ''
  321. CCFLAGS = ''
  322. # number of groups
  323. group_index = 1
  324. for group in script:
  325. # print group['name']
  326. # get each include path
  327. if group.has_key('CPPPATH') and group['CPPPATH']:
  328. if CPPPATH:
  329. CPPPATH += group['CPPPATH']
  330. else:
  331. CPPPATH += group['CPPPATH']
  332. # get each group's definitions
  333. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  334. if CPPDEFINES:
  335. CPPDEFINES += ';' + group['CPPDEFINES']
  336. else:
  337. CPPDEFINES += group['CPPDEFINES']
  338. # get each group's link flags
  339. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  340. if LINKFLAGS:
  341. LINKFLAGS += ' ' + group['LINKFLAGS']
  342. else:
  343. LINKFLAGS += group['LINKFLAGS']
  344. # generate file items
  345. for node in group['src']:
  346. fn = node.rfile()
  347. name = fn.name
  348. path = os.path.dirname(fn.abspath)
  349. basename = os.path.basename(path)
  350. path = _make_path_relative(project_path, path)
  351. path = os.path.join(path, name)
  352. if ProjectFiles.count(name):
  353. name = basename + '_' + name
  354. ProjectFiles.append(name)
  355. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  356. % (group_index, _get_filetype(name), path, name))
  357. line_index += 1
  358. group_index = group_index + 1
  359. lines.insert(line_index, '\r\n')
  360. line_index += 1
  361. # remove repeat path
  362. paths = set()
  363. for path in CPPPATH:
  364. inc = _make_path_relative(project_path, os.path.normpath(path))
  365. paths.add(inc) #.replace('\\', '/')
  366. paths = [i for i in paths]
  367. CPPPATH = string.join(paths, ';')
  368. definitions = [i for i in set(CPPDEFINES)]
  369. CPPDEFINES = string.join(definitions, ', ')
  370. while line_index < len(lines):
  371. if lines[line_index].startswith(' ADSCINCD '):
  372. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  373. if lines[line_index].startswith(' ADSLDMC ('):
  374. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  375. if lines[line_index].startswith(' ADSCDEFN ('):
  376. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  377. line_index += 1
  378. # write project
  379. for line in lines:
  380. project.write(line)
  381. project.close()