keil.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 MDK5AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  132. group = SubElement(parent, 'Group')
  133. group_name = SubElement(group, 'GroupName')
  134. group_name.text = name
  135. name = os.path.basename(filename)
  136. path = os.path.dirname (filename)
  137. basename = os.path.basename(path)
  138. path = _make_path_relative(project_path, path)
  139. path = os.path.join(path, name)
  140. files = SubElement(group, 'Files')
  141. file = SubElement(files, 'File')
  142. file_name = SubElement(file, 'FileName')
  143. name = os.path.basename(path)
  144. if ProjectFiles.count(name):
  145. name = basename + '_' + name
  146. ProjectFiles.append(name)
  147. file_name.text = name.decode(fs_encoding)
  148. file_type = SubElement(file, 'FileType')
  149. file_type.text = '%d' % _get_filetype(name)
  150. file_path = SubElement(file, 'FilePath')
  151. file_path.text = path.decode(fs_encoding)
  152. def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
  153. # don't add an empty group
  154. if len(files) == 0:
  155. return
  156. group = SubElement(parent, 'Group')
  157. group_name = SubElement(group, 'GroupName')
  158. group_name.text = name
  159. for f in files:
  160. fn = f.rfile()
  161. name = fn.name
  162. path = os.path.dirname(fn.abspath)
  163. basename = os.path.basename(path)
  164. path = _make_path_relative(project_path, path)
  165. path = os.path.join(path, name)
  166. files = SubElement(group, 'Files')
  167. file = SubElement(files, 'File')
  168. file_name = SubElement(file, 'FileName')
  169. name = os.path.basename(path)
  170. if ProjectFiles.count(name):
  171. name = basename + '_' + name
  172. ProjectFiles.append(name)
  173. file_name.text = name.decode(fs_encoding)
  174. file_type = SubElement(file, 'FileType')
  175. file_type.text = '%d' % _get_filetype(name)
  176. file_path = SubElement(file, 'FilePath')
  177. file_path.text = path.decode(fs_encoding)
  178. def MDK5Project(target, script):
  179. project_path = os.path.dirname(os.path.abspath(target))
  180. project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
  181. if os.path.isfile(project_uvopt):
  182. os.unlink(project_uvopt)
  183. tree = etree.parse('template.uvprojx')
  184. root = tree.getroot()
  185. out = file(target, 'wb')
  186. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  187. CPPPATH = []
  188. CPPDEFINES = []
  189. LINKFLAGS = ''
  190. CCFLAGS = ''
  191. ProjectFiles = []
  192. # add group
  193. groups = tree.find('Targets/Target/Groups')
  194. if groups is None:
  195. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  196. for group in script:
  197. group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  198. # get each include path
  199. if group.has_key('CPPPATH') and group['CPPPATH']:
  200. if CPPPATH:
  201. CPPPATH += group['CPPPATH']
  202. else:
  203. CPPPATH += group['CPPPATH']
  204. # get each group's definitions
  205. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  206. if CPPDEFINES:
  207. CPPDEFINES += group['CPPDEFINES']
  208. else:
  209. CPPDEFINES += group['CPPDEFINES']
  210. # get each group's link flags
  211. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  212. if LINKFLAGS:
  213. LINKFLAGS += ' ' + group['LINKFLAGS']
  214. else:
  215. LINKFLAGS += group['LINKFLAGS']
  216. if group.has_key('LIBS') and group['LIBS']:
  217. for item in group['LIBS']:
  218. lib_path = ''
  219. for path_item in group['LIBPATH']:
  220. full_path = os.path.join(path_item, item + '.lib')
  221. if os.path.isfile(full_path): # has this library
  222. lib_path = full_path
  223. if lib_path != '':
  224. MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  225. # remove repeat path
  226. paths = set()
  227. for path in CPPPATH:
  228. inc = _make_path_relative(project_path, os.path.normpath(path))
  229. paths.add(inc) #.replace('\\', '/')
  230. paths = [i for i in paths]
  231. paths.sort()
  232. CPPPATH = string.join(paths, ';')
  233. definitions = [i for i in set(CPPDEFINES)]
  234. CPPDEFINES = string.join(definitions, ', ')
  235. # write include path, definitions and link flags
  236. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  237. IncludePath.text = CPPPATH
  238. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  239. Define.text = CPPDEFINES
  240. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  241. Misc.text = LINKFLAGS
  242. xml_indent(root)
  243. out.write(etree.tostring(root, encoding='utf-8'))
  244. out.close()
  245. # copy uvopt file
  246. if os.path.exists('template.uvoptx'):
  247. import shutil
  248. shutil.copy2('template.uvoptx', 'project.uvoptx')
  249. def MDKProject(target, script):
  250. template = file('template.Uv2', "rb")
  251. lines = template.readlines()
  252. project = file(target, "wb")
  253. project_path = os.path.dirname(os.path.abspath(target))
  254. line_index = 5
  255. # write group
  256. for group in script:
  257. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  258. line_index += 1
  259. lines.insert(line_index, '\r\n')
  260. line_index += 1
  261. # write file
  262. ProjectFiles = []
  263. CPPPATH = []
  264. CPPDEFINES = []
  265. LINKFLAGS = ''
  266. CCFLAGS = ''
  267. # number of groups
  268. group_index = 1
  269. for group in script:
  270. # print group['name']
  271. # get each include path
  272. if group.has_key('CPPPATH') and group['CPPPATH']:
  273. if CPPPATH:
  274. CPPPATH += group['CPPPATH']
  275. else:
  276. CPPPATH += group['CPPPATH']
  277. # get each group's definitions
  278. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  279. if CPPDEFINES:
  280. CPPDEFINES += ';' + group['CPPDEFINES']
  281. else:
  282. CPPDEFINES += group['CPPDEFINES']
  283. # get each group's link flags
  284. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  285. if LINKFLAGS:
  286. LINKFLAGS += ' ' + group['LINKFLAGS']
  287. else:
  288. LINKFLAGS += group['LINKFLAGS']
  289. # generate file items
  290. for node in group['src']:
  291. fn = node.rfile()
  292. name = fn.name
  293. path = os.path.dirname(fn.abspath)
  294. basename = os.path.basename(path)
  295. path = _make_path_relative(project_path, path)
  296. path = os.path.join(path, name)
  297. if ProjectFiles.count(name):
  298. name = basename + '_' + name
  299. ProjectFiles.append(name)
  300. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  301. % (group_index, _get_filetype(name), path, name))
  302. line_index += 1
  303. group_index = group_index + 1
  304. lines.insert(line_index, '\r\n')
  305. line_index += 1
  306. # remove repeat path
  307. paths = set()
  308. for path in CPPPATH:
  309. inc = _make_path_relative(project_path, os.path.normpath(path))
  310. paths.add(inc) #.replace('\\', '/')
  311. paths = [i for i in paths]
  312. CPPPATH = string.join(paths, ';')
  313. definitions = [i for i in set(CPPDEFINES)]
  314. CPPDEFINES = string.join(definitions, ', ')
  315. while line_index < len(lines):
  316. if lines[line_index].startswith(' ADSCINCD '):
  317. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  318. if lines[line_index].startswith(' ADSLDMC ('):
  319. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  320. if lines[line_index].startswith(' ADSCDEFN ('):
  321. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  322. line_index += 1
  323. # write project
  324. for line in lines:
  325. project.write(line)
  326. project.close()