keil.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. groups.clear() # clean old groups
  88. for group in script:
  89. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  90. # get each include path
  91. if group.has_key('CPPPATH') and group['CPPPATH']:
  92. if CPPPATH:
  93. CPPPATH += group['CPPPATH']
  94. else:
  95. CPPPATH += group['CPPPATH']
  96. # get each group's definitions
  97. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  98. if CPPDEFINES:
  99. CPPDEFINES += group['CPPDEFINES']
  100. else:
  101. CPPDEFINES += group['CPPDEFINES']
  102. # get each group's link flags
  103. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  104. if LINKFLAGS:
  105. LINKFLAGS += ' ' + group['LINKFLAGS']
  106. else:
  107. LINKFLAGS += group['LINKFLAGS']
  108. if group.has_key('LIBS') and group['LIBS']:
  109. for item in group['LIBS']:
  110. lib_path = ''
  111. for path_item in group['LIBPATH']:
  112. full_path = os.path.join(path_item, item + '.lib')
  113. if os.path.isfile(full_path): # has this library
  114. lib_path = full_path
  115. if lib_path != '':
  116. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  117. # remove repeat path
  118. paths = set()
  119. for path in CPPPATH:
  120. inc = _make_path_relative(project_path, os.path.normpath(path))
  121. paths.add(inc) #.replace('\\', '/')
  122. paths = [i for i in paths]
  123. paths.sort()
  124. CPPPATH = string.join(paths, ';')
  125. definitions = [i for i in set(CPPDEFINES)]
  126. CPPDEFINES = string.join(definitions, ', ')
  127. # write include path, definitions and link flags
  128. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  129. IncludePath.text = CPPPATH
  130. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  131. Define.text = CPPDEFINES
  132. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  133. Misc.text = LINKFLAGS
  134. xml_indent(root)
  135. out.write(etree.tostring(root, encoding='utf-8'))
  136. out.close()
  137. # copy uvopt file
  138. if os.path.exists('template.uvopt'):
  139. import shutil
  140. shutil.copy2('template.uvopt', 'project.uvopt')
  141. def MDKProject(target, script):
  142. template = file('template.Uv2', "rb")
  143. lines = template.readlines()
  144. project = file(target, "wb")
  145. project_path = os.path.dirname(os.path.abspath(target))
  146. line_index = 5
  147. # write group
  148. for group in script:
  149. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  150. line_index += 1
  151. lines.insert(line_index, '\r\n')
  152. line_index += 1
  153. # write file
  154. ProjectFiles = []
  155. CPPPATH = []
  156. CPPDEFINES = []
  157. LINKFLAGS = ''
  158. CCFLAGS = ''
  159. # number of groups
  160. group_index = 1
  161. for group in script:
  162. # print group['name']
  163. # get each include path
  164. if group.has_key('CPPPATH') and group['CPPPATH']:
  165. if CPPPATH:
  166. CPPPATH += group['CPPPATH']
  167. else:
  168. CPPPATH += group['CPPPATH']
  169. # get each group's definitions
  170. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  171. if CPPDEFINES:
  172. CPPDEFINES += ';' + group['CPPDEFINES']
  173. else:
  174. CPPDEFINES += group['CPPDEFINES']
  175. # get each group's link flags
  176. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  177. if LINKFLAGS:
  178. LINKFLAGS += ' ' + group['LINKFLAGS']
  179. else:
  180. LINKFLAGS += group['LINKFLAGS']
  181. # generate file items
  182. for node in group['src']:
  183. fn = node.rfile()
  184. name = fn.name
  185. path = os.path.dirname(fn.abspath)
  186. basename = os.path.basename(path)
  187. path = _make_path_relative(project_path, path)
  188. path = os.path.join(path, name)
  189. if ProjectFiles.count(name):
  190. name = basename + '_' + name
  191. ProjectFiles.append(name)
  192. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  193. % (group_index, _get_filetype(name), path, name))
  194. line_index += 1
  195. group_index = group_index + 1
  196. lines.insert(line_index, '\r\n')
  197. line_index += 1
  198. # remove repeat path
  199. paths = set()
  200. for path in CPPPATH:
  201. inc = _make_path_relative(project_path, os.path.normpath(path))
  202. paths.add(inc) #.replace('\\', '/')
  203. paths = [i for i in paths]
  204. CPPPATH = string.join(paths, ';')
  205. definitions = [i for i in set(CPPDEFINES)]
  206. CPPDEFINES = string.join(definitions, ', ')
  207. while line_index < len(lines):
  208. if lines[line_index].startswith(' ADSCINCD '):
  209. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  210. if lines[line_index].startswith(' ADSLDMC ('):
  211. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  212. if lines[line_index].startswith(' ADSCDEFN ('):
  213. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  214. line_index += 1
  215. # write project
  216. for line in lines:
  217. project.write(line)
  218. project.close()