keil.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. # copy uvopt file
  137. if os.path.exists('template.uvopt'):
  138. import shutil
  139. shutil.copy2('template.uvopt', 'project.uvopt')
  140. def MDKProject(target, script):
  141. template = file('template.Uv2', "rb")
  142. lines = template.readlines()
  143. project = file(target, "wb")
  144. project_path = os.path.dirname(os.path.abspath(target))
  145. line_index = 5
  146. # write group
  147. for group in script:
  148. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  149. line_index += 1
  150. lines.insert(line_index, '\r\n')
  151. line_index += 1
  152. # write file
  153. ProjectFiles = []
  154. CPPPATH = []
  155. CPPDEFINES = []
  156. LINKFLAGS = ''
  157. CCFLAGS = ''
  158. # number of groups
  159. group_index = 1
  160. for group in script:
  161. # print group['name']
  162. # get each include path
  163. if group.has_key('CPPPATH') and group['CPPPATH']:
  164. if CPPPATH:
  165. CPPPATH += group['CPPPATH']
  166. else:
  167. CPPPATH += group['CPPPATH']
  168. # get each group's definitions
  169. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  170. if CPPDEFINES:
  171. CPPDEFINES += ';' + group['CPPDEFINES']
  172. else:
  173. CPPDEFINES += group['CPPDEFINES']
  174. # get each group's link flags
  175. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  176. if LINKFLAGS:
  177. LINKFLAGS += ' ' + group['LINKFLAGS']
  178. else:
  179. LINKFLAGS += group['LINKFLAGS']
  180. # generate file items
  181. for node in group['src']:
  182. fn = node.rfile()
  183. name = fn.name
  184. path = os.path.dirname(fn.abspath)
  185. basename = os.path.basename(path)
  186. path = _make_path_relative(project_path, path)
  187. path = os.path.join(path, name)
  188. if ProjectFiles.count(name):
  189. name = basename + '_' + name
  190. ProjectFiles.append(name)
  191. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  192. % (group_index, _get_filetype(name), path, name))
  193. line_index += 1
  194. group_index = group_index + 1
  195. lines.insert(line_index, '\r\n')
  196. line_index += 1
  197. # remove repeat path
  198. paths = set()
  199. for path in CPPPATH:
  200. inc = _make_path_relative(project_path, os.path.normpath(path))
  201. paths.add(inc) #.replace('\\', '/')
  202. paths = [i for i in paths]
  203. CPPPATH = string.join(paths, ';')
  204. definitions = [i for i in set(CPPDEFINES)]
  205. CPPDEFINES = string.join(definitions, ', ')
  206. while line_index < len(lines):
  207. if lines[line_index].startswith(' ADSCINCD '):
  208. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  209. if lines[line_index].startswith(' ADSLDMC ('):
  210. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  211. if lines[line_index].startswith(' ADSCDEFN ('):
  212. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  213. line_index += 1
  214. # write project
  215. for line in lines:
  216. project.write(line)
  217. project.close()