keil.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import os
  2. import sys
  3. import string
  4. import xml.etree.ElementTree as etree
  5. from xml.etree.ElementTree import SubElement
  6. from utils import _make_path_relative
  7. from utils import xml_indent
  8. fs_encoding = sys.getfilesystemencoding()
  9. def _get_filetype(fn):
  10. if fn.rfind('.c') != -1 or fn.rfind('.C') != -1 or fn.rfind('.cpp') != -1:
  11. return 1
  12. # assemble file type
  13. if fn.rfind('.s') != -1 or fn.rfind('.S') != -1:
  14. return 2
  15. # header type
  16. if fn.rfind('.h') != -1:
  17. return 5
  18. if fn.rfind('.lib') != -1:
  19. return 4
  20. # other filetype
  21. return 5
  22. def MDK4AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  23. group = SubElement(parent, 'Group')
  24. group_name = SubElement(group, 'GroupName')
  25. group_name.text = name
  26. name = os.path.basename(filename)
  27. path = os.path.dirname (filename)
  28. basename = os.path.basename(path)
  29. path = _make_path_relative(project_path, path)
  30. path = os.path.join(path, name)
  31. files = SubElement(group, 'Files')
  32. file = SubElement(files, 'File')
  33. file_name = SubElement(file, 'FileName')
  34. name = os.path.basename(path)
  35. if ProjectFiles.count(name):
  36. name = basename + '_' + name
  37. ProjectFiles.append(name)
  38. file_name.text = name.decode(fs_encoding)
  39. file_type = SubElement(file, 'FileType')
  40. file_type.text = '%d' % _get_filetype(name)
  41. file_path = SubElement(file, 'FilePath')
  42. file_path.text = path.decode(fs_encoding)
  43. def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
  44. # don't add an empty group
  45. if len(files) == 0:
  46. return
  47. group = SubElement(parent, 'Group')
  48. group_name = SubElement(group, 'GroupName')
  49. group_name.text = name
  50. for f in files:
  51. fn = f.rfile()
  52. name = fn.name
  53. path = os.path.dirname(fn.abspath)
  54. basename = os.path.basename(path)
  55. path = _make_path_relative(project_path, path)
  56. path = os.path.join(path, name)
  57. files = SubElement(group, 'Files')
  58. file = SubElement(files, 'File')
  59. file_name = SubElement(file, 'FileName')
  60. name = os.path.basename(path)
  61. if ProjectFiles.count(name):
  62. name = basename + '_' + name
  63. ProjectFiles.append(name)
  64. file_name.text = name.decode(fs_encoding)
  65. file_type = SubElement(file, 'FileType')
  66. file_type.text = '%d' % _get_filetype(name)
  67. file_path = SubElement(file, 'FilePath')
  68. file_path.text = path.decode(fs_encoding)
  69. def MDK4Project(target, script):
  70. project_path = os.path.dirname(os.path.abspath(target))
  71. project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
  72. if os.path.isfile(project_uvopt):
  73. os.unlink(project_uvopt)
  74. tree = etree.parse('template.uvproj')
  75. root = tree.getroot()
  76. out = file(target, 'wb')
  77. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  78. CPPPATH = []
  79. CPPDEFINES = []
  80. LINKFLAGS = ''
  81. CCFLAGS = ''
  82. ProjectFiles = []
  83. # add group
  84. groups = tree.find('Targets/Target/Groups')
  85. if groups is None:
  86. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  87. for group in script:
  88. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  89. # get each include path
  90. if group.has_key('CPPPATH') and group['CPPPATH']:
  91. if CPPPATH:
  92. CPPPATH += group['CPPPATH']
  93. else:
  94. CPPPATH += group['CPPPATH']
  95. # get each group's definitions
  96. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  97. if CPPDEFINES:
  98. CPPDEFINES += group['CPPDEFINES']
  99. else:
  100. CPPDEFINES += group['CPPDEFINES']
  101. # get each group's link flags
  102. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  103. if LINKFLAGS:
  104. LINKFLAGS += ' ' + group['LINKFLAGS']
  105. else:
  106. LINKFLAGS += group['LINKFLAGS']
  107. if group.has_key('LIBS') and group['LIBS']:
  108. for item in group['LIBS']:
  109. lib_path = ''
  110. for path_item in group['LIBPATH']:
  111. full_path = os.path.join(path_item, item + '.lib')
  112. if os.path.isfile(full_path): # has this library
  113. lib_path = full_path
  114. if lib_path != '':
  115. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  116. # remove repeat path
  117. paths = set()
  118. for path in CPPPATH:
  119. inc = _make_path_relative(project_path, os.path.normpath(path))
  120. paths.add(inc) #.replace('\\', '/')
  121. paths = [i for i in paths]
  122. paths.sort()
  123. CPPPATH = string.join(paths, ';')
  124. definitions = [i for i in set(CPPDEFINES)]
  125. CPPDEFINES = string.join(definitions, ', ')
  126. # write include path, definitions and link flags
  127. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  128. IncludePath.text = CPPPATH
  129. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  130. Define.text = CPPDEFINES
  131. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  132. Misc.text = LINKFLAGS
  133. xml_indent(root)
  134. out.write(etree.tostring(root, encoding='utf-8'))
  135. out.close()
  136. def MDKProject(target, script):
  137. template = file('template.Uv2', "rb")
  138. lines = template.readlines()
  139. project = file(target, "wb")
  140. project_path = os.path.dirname(os.path.abspath(target))
  141. line_index = 5
  142. # write group
  143. for group in script:
  144. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  145. line_index += 1
  146. lines.insert(line_index, '\r\n')
  147. line_index += 1
  148. # write file
  149. ProjectFiles = []
  150. CPPPATH = []
  151. CPPDEFINES = []
  152. LINKFLAGS = ''
  153. CCFLAGS = ''
  154. # number of groups
  155. group_index = 1
  156. for group in script:
  157. # print group['name']
  158. # get each include path
  159. if group.has_key('CPPPATH') and group['CPPPATH']:
  160. if CPPPATH:
  161. CPPPATH += group['CPPPATH']
  162. else:
  163. CPPPATH += group['CPPPATH']
  164. # get each group's definitions
  165. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  166. if CPPDEFINES:
  167. CPPDEFINES += ';' + group['CPPDEFINES']
  168. else:
  169. CPPDEFINES += group['CPPDEFINES']
  170. # get each group's link flags
  171. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  172. if LINKFLAGS:
  173. LINKFLAGS += ' ' + group['LINKFLAGS']
  174. else:
  175. LINKFLAGS += group['LINKFLAGS']
  176. # generate file items
  177. for node in group['src']:
  178. fn = node.rfile()
  179. name = fn.name
  180. path = os.path.dirname(fn.abspath)
  181. basename = os.path.basename(path)
  182. path = _make_path_relative(project_path, path)
  183. path = os.path.join(path, name)
  184. if ProjectFiles.count(name):
  185. name = basename + '_' + name
  186. ProjectFiles.append(name)
  187. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  188. % (group_index, _get_filetype(name), path, name))
  189. line_index += 1
  190. group_index = group_index + 1
  191. lines.insert(line_index, '\r\n')
  192. line_index += 1
  193. # remove repeat path
  194. paths = set()
  195. for path in CPPPATH:
  196. inc = _make_path_relative(project_path, os.path.normpath(path))
  197. paths.add(inc) #.replace('\\', '/')
  198. paths = [i for i in paths]
  199. CPPPATH = string.join(paths, ';')
  200. definitions = [i for i in set(CPPDEFINES)]
  201. CPPDEFINES = string.join(definitions, ', ')
  202. while line_index < len(lines):
  203. if lines[line_index].startswith(' ADSCINCD '):
  204. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  205. if lines[line_index].startswith(' ADSLDMC ('):
  206. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  207. if lines[line_index].startswith(' ADSCDEFN ('):
  208. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  209. line_index += 1
  210. # write project
  211. for line in lines:
  212. project.write(line)
  213. project.close()