keil.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # other filetype
  19. return 5
  20. def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
  21. group = SubElement(parent, 'Group')
  22. group_name = SubElement(group, 'GroupName')
  23. group_name.text = name
  24. for f in files:
  25. fn = f.rfile()
  26. name = fn.name
  27. path = os.path.dirname(fn.abspath)
  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 MDK4Project(target, script):
  44. project_path = os.path.dirname(os.path.abspath(target))
  45. project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
  46. if os.path.isfile(project_uvopt):
  47. os.unlink(project_uvopt)
  48. tree = etree.parse('template.uvproj')
  49. root = tree.getroot()
  50. out = file(target, 'wb')
  51. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  52. CPPPATH = []
  53. CPPDEFINES = []
  54. LINKFLAGS = ''
  55. CCFLAGS = ''
  56. ProjectFiles = []
  57. # add group
  58. groups = tree.find('Targets/Target/Groups')
  59. if groups is None:
  60. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  61. for group in script:
  62. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  63. # get each include path
  64. if group.has_key('CPPPATH') and group['CPPPATH']:
  65. if CPPPATH:
  66. CPPPATH += group['CPPPATH']
  67. else:
  68. CPPPATH += group['CPPPATH']
  69. # get each group's definitions
  70. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  71. if CPPDEFINES:
  72. CPPDEFINES += group['CPPDEFINES']
  73. else:
  74. CPPDEFINES += group['CPPDEFINES']
  75. # get each group's link flags
  76. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  77. if LINKFLAGS:
  78. LINKFLAGS += ' ' + group['LINKFLAGS']
  79. else:
  80. LINKFLAGS += group['LINKFLAGS']
  81. # remove repeat path
  82. paths = set()
  83. for path in CPPPATH:
  84. inc = _make_path_relative(project_path, os.path.normpath(path))
  85. paths.add(inc) #.replace('\\', '/')
  86. paths = [i for i in paths]
  87. paths.sort()
  88. CPPPATH = string.join(paths, ';')
  89. definitions = [i for i in set(CPPDEFINES)]
  90. CPPDEFINES = string.join(definitions, ', ')
  91. # write include path, definitions and link flags
  92. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  93. IncludePath.text = CPPPATH
  94. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  95. Define.text = CPPDEFINES
  96. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  97. Misc.text = LINKFLAGS
  98. xml_indent(root)
  99. out.write(etree.tostring(root, encoding='utf-8'))
  100. out.close()
  101. def MDKProject(target, script):
  102. template = file('template.Uv2', "rb")
  103. lines = template.readlines()
  104. project = file(target, "wb")
  105. project_path = os.path.dirname(os.path.abspath(target))
  106. line_index = 5
  107. # write group
  108. for group in script:
  109. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  110. line_index += 1
  111. lines.insert(line_index, '\r\n')
  112. line_index += 1
  113. # write file
  114. ProjectFiles = []
  115. CPPPATH = []
  116. CPPDEFINES = []
  117. LINKFLAGS = ''
  118. CCFLAGS = ''
  119. # number of groups
  120. group_index = 1
  121. for group in script:
  122. # print group['name']
  123. # get each include path
  124. if group.has_key('CPPPATH') and group['CPPPATH']:
  125. if CPPPATH:
  126. CPPPATH += group['CPPPATH']
  127. else:
  128. CPPPATH += group['CPPPATH']
  129. # get each group's definitions
  130. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  131. if CPPDEFINES:
  132. CPPDEFINES += ';' + group['CPPDEFINES']
  133. else:
  134. CPPDEFINES += group['CPPDEFINES']
  135. # get each group's link flags
  136. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  137. if LINKFLAGS:
  138. LINKFLAGS += ' ' + group['LINKFLAGS']
  139. else:
  140. LINKFLAGS += group['LINKFLAGS']
  141. # generate file items
  142. for node in group['src']:
  143. fn = node.rfile()
  144. name = fn.name
  145. path = os.path.dirname(fn.abspath)
  146. basename = os.path.basename(path)
  147. path = _make_path_relative(project_path, path)
  148. path = os.path.join(path, name)
  149. if ProjectFiles.count(name):
  150. name = basename + '_' + name
  151. ProjectFiles.append(name)
  152. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  153. % (group_index, _get_filetype(name), path, name))
  154. line_index += 1
  155. group_index = group_index + 1
  156. lines.insert(line_index, '\r\n')
  157. line_index += 1
  158. # remove repeat path
  159. paths = set()
  160. for path in CPPPATH:
  161. inc = _make_path_relative(project_path, os.path.normpath(path))
  162. paths.add(inc) #.replace('\\', '/')
  163. paths = [i for i in paths]
  164. CPPPATH = string.join(paths, ';')
  165. definitions = [i for i in set(CPPDEFINES)]
  166. CPPDEFINES = string.join(definitions, ', ')
  167. while line_index < len(lines):
  168. if lines[line_index].startswith(' ADSCINCD '):
  169. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  170. if lines[line_index].startswith(' ADSLDMC ('):
  171. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  172. if lines[line_index].startswith(' ADSCDEFN ('):
  173. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  174. line_index += 1
  175. # write project
  176. for line in lines:
  177. project.write(line)
  178. project.close()