keil.py 6.6 KB

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