keil.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #
  2. # File : keil.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import os
  25. import sys
  26. import string
  27. import xml.etree.ElementTree as etree
  28. from xml.etree.ElementTree import SubElement
  29. from utils import _make_path_relative
  30. from utils import xml_indent
  31. fs_encoding = sys.getfilesystemencoding()
  32. def _get_filetype(fn):
  33. if fn.rfind('.cpp') != -1 or fn.rfind('.cxx') != -1:
  34. return 8
  35. if fn.rfind('.c') != -1 or fn.rfind('.C') != -1:
  36. return 1
  37. # assemble file type
  38. if fn.rfind('.s') != -1 or fn.rfind('.S') != -1:
  39. return 2
  40. # header type
  41. if fn.rfind('.h') != -1:
  42. return 5
  43. if fn.rfind('.lib') != -1:
  44. return 4
  45. # other filetype
  46. return 5
  47. def MDK4AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  48. group = SubElement(parent, 'Group')
  49. group_name = SubElement(group, 'GroupName')
  50. group_name.text = name
  51. name = os.path.basename(filename)
  52. path = os.path.dirname (filename)
  53. basename = os.path.basename(path)
  54. path = _make_path_relative(project_path, path)
  55. path = os.path.join(path, name)
  56. files = SubElement(group, 'Files')
  57. file = SubElement(files, 'File')
  58. file_name = SubElement(file, 'FileName')
  59. name = os.path.basename(path)
  60. if name.find('.cpp') != -1:
  61. obj_name = name.replace('.cpp', '.o')
  62. elif name.find('.c') != -1:
  63. obj_name = name.replace('.c', '.o')
  64. elif name.find('.s') != -1:
  65. obj_name = name.replace('.s', '.o')
  66. elif name.find('.S') != -1:
  67. obj_name = name.replace('.s', '.o')
  68. else:
  69. obj_name = name
  70. if ProjectFiles.count(obj_name):
  71. name = basename + '_' + name
  72. ProjectFiles.append(obj_name)
  73. file_name.text = name.decode(fs_encoding)
  74. file_type = SubElement(file, 'FileType')
  75. file_type.text = '%d' % _get_filetype(name)
  76. file_path = SubElement(file, 'FilePath')
  77. file_path.text = path.decode(fs_encoding)
  78. return group
  79. def MDK4AddLibToGroup(ProjectFiles, group, name, filename, project_path):
  80. name = os.path.basename(filename)
  81. path = os.path.dirname (filename)
  82. basename = os.path.basename(path)
  83. path = _make_path_relative(project_path, path)
  84. path = os.path.join(path, name)
  85. files = SubElement(group, 'Files')
  86. file = SubElement(files, 'File')
  87. file_name = SubElement(file, 'FileName')
  88. name = os.path.basename(path)
  89. if name.find('.cpp') != -1:
  90. obj_name = name.replace('.cpp', '.o')
  91. elif name.find('.c') != -1:
  92. obj_name = name.replace('.c', '.o')
  93. elif name.find('.s') != -1:
  94. obj_name = name.replace('.s', '.o')
  95. elif name.find('.S') != -1:
  96. obj_name = name.replace('.s', '.o')
  97. else:
  98. obj_name = name
  99. if ProjectFiles.count(obj_name):
  100. name = basename + '_' + name
  101. ProjectFiles.append(obj_name)
  102. file_name.text = name.decode(fs_encoding)
  103. file_type = SubElement(file, 'FileType')
  104. file_type.text = '%d' % _get_filetype(name)
  105. file_path = SubElement(file, 'FilePath')
  106. file_path.text = path.decode(fs_encoding)
  107. return group
  108. def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
  109. # don't add an empty group
  110. if len(files) == 0:
  111. return
  112. group = SubElement(parent, 'Group')
  113. group_name = SubElement(group, 'GroupName')
  114. group_name.text = name
  115. for f in files:
  116. fn = f.rfile()
  117. name = fn.name
  118. path = os.path.dirname(fn.abspath)
  119. basename = os.path.basename(path)
  120. path = _make_path_relative(project_path, path)
  121. path = os.path.join(path, name)
  122. files = SubElement(group, 'Files')
  123. file = SubElement(files, 'File')
  124. file_name = SubElement(file, 'FileName')
  125. name = os.path.basename(path)
  126. if name.find('.cpp') != -1:
  127. obj_name = name.replace('.cpp', '.o')
  128. elif name.find('.c') != -1:
  129. obj_name = name.replace('.c', '.o')
  130. elif name.find('.s') != -1:
  131. obj_name = name.replace('.s', '.o')
  132. elif name.find('.S') != -1:
  133. obj_name = name.replace('.s', '.o')
  134. if ProjectFiles.count(obj_name):
  135. name = basename + '_' + name
  136. ProjectFiles.append(obj_name)
  137. file_name.text = name # name.decode(fs_encoding)
  138. file_type = SubElement(file, 'FileType')
  139. file_type.text = '%d' % _get_filetype(name)
  140. file_path = SubElement(file, 'FilePath')
  141. file_path.text = path # path.decode(fs_encoding)
  142. return group
  143. # The common part of making MDK4/5 project
  144. def MDK45Project(tree, target, script):
  145. project_path = os.path.dirname(os.path.abspath(target))
  146. root = tree.getroot()
  147. out = open(target, 'w')
  148. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  149. CPPPATH = []
  150. CPPDEFINES = []
  151. LINKFLAGS = ''
  152. CCFLAGS = ''
  153. ProjectFiles = []
  154. # add group
  155. groups = tree.find('Targets/Target/Groups')
  156. if groups is None:
  157. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  158. groups.clear() # clean old groups
  159. for group in script:
  160. group_tree = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
  161. # for local CPPPATH/CPPDEFINES
  162. if (group_tree != None) and ('LOCAL_CPPPATH' in group or 'LOCAL_CCFLAGS' in group or 'LOCAL_CPPDEFINES' in group):
  163. GroupOption = SubElement(group_tree, 'GroupOption')
  164. GroupArmAds = SubElement(GroupOption, 'GroupArmAds')
  165. Cads = SubElement(GroupArmAds, 'Cads')
  166. VariousControls = SubElement(Cads, 'VariousControls')
  167. MiscControls = SubElement(VariousControls, 'MiscControls')
  168. if 'LOCAL_CCFLAGS' in group:
  169. MiscControls.text = group['LOCAL_CCFLAGS']
  170. else:
  171. MiscControls.text = ' '
  172. Define = SubElement(VariousControls, 'Define')
  173. if 'LOCAL_CPPDEFINES' in group:
  174. Define.text = ', '.join(set(group['LOCAL_CPPDEFINES']))
  175. else:
  176. Define.text = ' '
  177. Undefine = SubElement(VariousControls, 'Undefine')
  178. Undefine.text = ' '
  179. IncludePath = SubElement(VariousControls, 'IncludePath')
  180. if 'LOCAL_CPPPATH' in group:
  181. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in group['LOCAL_CPPPATH']])
  182. else:
  183. IncludePath.text = ' '
  184. # get each include path
  185. if 'CPPPATH' in group and group['CPPPATH']:
  186. if CPPPATH:
  187. CPPPATH += group['CPPPATH']
  188. else:
  189. CPPPATH += group['CPPPATH']
  190. # get each group's definitions
  191. if 'CPPDEFINES' in group and group['CPPDEFINES']:
  192. if CPPDEFINES:
  193. CPPDEFINES += group['CPPDEFINES']
  194. else:
  195. CPPDEFINES = group['CPPDEFINES']
  196. # get each group's link flags
  197. if 'LINKFLAGS' in group and group['LINKFLAGS']:
  198. if LINKFLAGS:
  199. LINKFLAGS += ' ' + group['LINKFLAGS']
  200. else:
  201. LINKFLAGS += group['LINKFLAGS']
  202. if 'LIBS' in group and group['LIBS']:
  203. for item in group['LIBS']:
  204. lib_path = ''
  205. for path_item in group['LIBPATH']:
  206. full_path = os.path.join(path_item, item + '.lib')
  207. if os.path.isfile(full_path): # has this library
  208. lib_path = full_path
  209. break
  210. if lib_path != '':
  211. if group_tree != None:
  212. MDK4AddLibToGroup(ProjectFiles, group_tree, group['name'], lib_path, project_path)
  213. else:
  214. group_tree = MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  215. # write include path, definitions and link flags
  216. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  217. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
  218. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  219. Define.text = ', '.join(set(CPPDEFINES))
  220. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  221. Misc.text = LINKFLAGS
  222. xml_indent(root)
  223. out.write(etree.tostring(root, encoding='utf-8').decode())
  224. out.close()
  225. def MDK4Project(target, script):
  226. template_tree = etree.parse('template.uvproj')
  227. MDK45Project(template_tree, target, script)
  228. # remove project.uvopt file
  229. project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
  230. if os.path.isfile(project_uvopt):
  231. os.unlink(project_uvopt)
  232. # copy uvopt file
  233. if os.path.exists('template.uvopt'):
  234. import shutil
  235. shutil.copy2('template.uvopt', 'project.uvopt')
  236. def MDK5Project(target, script):
  237. template_tree = etree.parse('template.uvprojx')
  238. MDK45Project(template_tree, target, script)
  239. # remove project.uvopt file
  240. project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
  241. if os.path.isfile(project_uvopt):
  242. os.unlink(project_uvopt)
  243. # copy uvopt file
  244. if os.path.exists('template.uvoptx'):
  245. import shutil
  246. shutil.copy2('template.uvoptx', 'project.uvoptx')
  247. def MDKProject(target, script):
  248. template = open('template.Uv2', "r")
  249. lines = template.readlines()
  250. project = open(target, "w")
  251. project_path = os.path.dirname(os.path.abspath(target))
  252. line_index = 5
  253. # write group
  254. for group in script:
  255. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  256. line_index += 1
  257. lines.insert(line_index, '\r\n')
  258. line_index += 1
  259. # write file
  260. ProjectFiles = []
  261. CPPPATH = []
  262. CPPDEFINES = []
  263. LINKFLAGS = ''
  264. CCFLAGS = ''
  265. # number of groups
  266. group_index = 1
  267. for group in script:
  268. # print group['name']
  269. # get each include path
  270. if 'CPPPATH' in group and group['CPPPATH']:
  271. if CPPPATH:
  272. CPPPATH += group['CPPPATH']
  273. else:
  274. CPPPATH += group['CPPPATH']
  275. # get each group's definitions
  276. if 'CPPDEFINES' in group and group['CPPDEFINES']:
  277. if CPPDEFINES:
  278. CPPDEFINES += group['CPPDEFINES']
  279. else:
  280. CPPDEFINES = group['CPPDEFINES']
  281. # get each group's link flags
  282. if 'LINKFLAGS' in group and group['LINKFLAGS']:
  283. if LINKFLAGS:
  284. LINKFLAGS += ' ' + group['LINKFLAGS']
  285. else:
  286. LINKFLAGS += group['LINKFLAGS']
  287. # generate file items
  288. for node in group['src']:
  289. fn = node.rfile()
  290. name = fn.name
  291. path = os.path.dirname(fn.abspath)
  292. basename = os.path.basename(path)
  293. path = _make_path_relative(project_path, path)
  294. path = os.path.join(path, name)
  295. if ProjectFiles.count(name):
  296. name = basename + '_' + name
  297. ProjectFiles.append(name)
  298. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  299. % (group_index, _get_filetype(name), path, name))
  300. line_index += 1
  301. group_index = group_index + 1
  302. lines.insert(line_index, '\r\n')
  303. line_index += 1
  304. # remove repeat path
  305. paths = set()
  306. for path in CPPPATH:
  307. inc = _make_path_relative(project_path, os.path.normpath(path))
  308. paths.add(inc) #.replace('\\', '/')
  309. paths = [i for i in paths]
  310. CPPPATH = string.join(paths, ';')
  311. definitions = [i for i in set(CPPDEFINES)]
  312. CPPDEFINES = string.join(definitions, ', ')
  313. while line_index < len(lines):
  314. if lines[line_index].startswith(' ADSCINCD '):
  315. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  316. if lines[line_index].startswith(' ADSLDMC ('):
  317. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  318. if lines[line_index].startswith(' ADSCDEFN ('):
  319. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  320. line_index += 1
  321. # write project
  322. for line in lines:
  323. project.write(line)
  324. project.close()
  325. def ARMCC_Version():
  326. import rtconfig
  327. import subprocess
  328. import re
  329. path = rtconfig.EXEC_PATH
  330. path = os.path.join(path, 'armcc.exe')
  331. if os.path.exists(path):
  332. cmd = path
  333. else:
  334. print('Error: get armcc version failed. Please update the KEIL MDK installation path in rtconfig.py!')
  335. return "0.0"
  336. child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  337. stdout, stderr = child.communicate()
  338. '''
  339. example stdout:
  340. Product: MDK Plus 5.24
  341. Component: ARM Compiler 5.06 update 5 (build 528)
  342. Tool: armcc [4d3621]
  343. return version: MDK Plus 5.24/ARM Compiler 5.06 update 5 (build 528)/armcc [4d3621]
  344. '''
  345. version_Product = re.search(r'Product: (.+)', stdout).group(1)
  346. version_Product = version_Product[:-1]
  347. version_Component = re.search(r'Component: (.*)', stdout).group(1)
  348. version_Component = version_Component[:-1]
  349. version_Tool = re.search(r'Tool: (.*)', stdout).group(1)
  350. version_Tool = version_Tool[:-1]
  351. version_str_format = '%s/%s/%s'
  352. version_str = version_str_format % (version_Product, version_Component, version_Tool)
  353. #print('version_str:' + version_str)
  354. return version_str