keil.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. # write include path, definitions and link flags
  118. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  119. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
  120. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  121. Define.text = ', '.join(set(CPPDEFINES))
  122. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  123. Misc.text = LINKFLAGS
  124. xml_indent(root)
  125. out.write(etree.tostring(root, encoding='utf-8'))
  126. out.close()
  127. # copy uvopt file
  128. if os.path.exists('template.uvopt'):
  129. import shutil
  130. shutil.copy2('template.uvopt', 'project.uvopt')
  131. def MDKProject(target, script):
  132. template = file('template.Uv2', "rb")
  133. lines = template.readlines()
  134. project = file(target, "wb")
  135. project_path = os.path.dirname(os.path.abspath(target))
  136. line_index = 5
  137. # write group
  138. for group in script:
  139. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  140. line_index += 1
  141. lines.insert(line_index, '\r\n')
  142. line_index += 1
  143. # write file
  144. ProjectFiles = []
  145. CPPPATH = []
  146. CPPDEFINES = []
  147. LINKFLAGS = ''
  148. CCFLAGS = ''
  149. # number of groups
  150. group_index = 1
  151. for group in script:
  152. # print group['name']
  153. # get each include path
  154. if group.has_key('CPPPATH') and group['CPPPATH']:
  155. if CPPPATH:
  156. CPPPATH += group['CPPPATH']
  157. else:
  158. CPPPATH += group['CPPPATH']
  159. # get each group's definitions
  160. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  161. if CPPDEFINES:
  162. CPPDEFINES += ';' + group['CPPDEFINES']
  163. else:
  164. CPPDEFINES += group['CPPDEFINES']
  165. # get each group's link flags
  166. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  167. if LINKFLAGS:
  168. LINKFLAGS += ' ' + group['LINKFLAGS']
  169. else:
  170. LINKFLAGS += group['LINKFLAGS']
  171. # generate file items
  172. for node in group['src']:
  173. fn = node.rfile()
  174. name = fn.name
  175. path = os.path.dirname(fn.abspath)
  176. basename = os.path.basename(path)
  177. path = _make_path_relative(project_path, path)
  178. path = os.path.join(path, name)
  179. if ProjectFiles.count(name):
  180. name = basename + '_' + name
  181. ProjectFiles.append(name)
  182. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  183. % (group_index, _get_filetype(name), path, name))
  184. line_index += 1
  185. group_index = group_index + 1
  186. lines.insert(line_index, '\r\n')
  187. line_index += 1
  188. # remove repeat path
  189. paths = set()
  190. for path in CPPPATH:
  191. inc = _make_path_relative(project_path, os.path.normpath(path))
  192. paths.add(inc) #.replace('\\', '/')
  193. paths = [i for i in paths]
  194. CPPPATH = string.join(paths, ';')
  195. definitions = [i for i in set(CPPDEFINES)]
  196. CPPDEFINES = string.join(definitions, ', ')
  197. while line_index < len(lines):
  198. if lines[line_index].startswith(' ADSCINCD '):
  199. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  200. if lines[line_index].startswith(' ADSLDMC ('):
  201. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  202. if lines[line_index].startswith(' ADSCDEFN ('):
  203. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  204. line_index += 1
  205. # write project
  206. for line in lines:
  207. project.write(line)
  208. project.close()