keil.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. if fn.rfind('.o') != -1:
  46. return 3
  47. # other filetype
  48. return 5
  49. def MDK4AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
  50. group = SubElement(parent, 'Group')
  51. group_name = SubElement(group, 'GroupName')
  52. group_name.text = name
  53. name = os.path.basename(filename)
  54. path = os.path.dirname (filename)
  55. basename = os.path.basename(path)
  56. path = _make_path_relative(project_path, path)
  57. path = os.path.join(path, name)
  58. files = SubElement(group, 'Files')
  59. file = SubElement(files, 'File')
  60. file_name = SubElement(file, 'FileName')
  61. name = os.path.basename(path)
  62. if name.find('.cpp') != -1:
  63. obj_name = name.replace('.cpp', '.o')
  64. elif name.find('.c') != -1:
  65. obj_name = name.replace('.c', '.o')
  66. elif name.find('.s') != -1:
  67. obj_name = name.replace('.s', '.o')
  68. elif name.find('.S') != -1:
  69. obj_name = name.replace('.s', '.o')
  70. else:
  71. obj_name = name
  72. if ProjectFiles.count(obj_name):
  73. name = basename + '_' + name
  74. ProjectFiles.append(obj_name)
  75. try: # python 2
  76. file_name.text = name.decode(fs_encoding)
  77. except: # python 3
  78. file_name.text = name
  79. file_type = SubElement(file, 'FileType')
  80. file_type.text = '%d' % _get_filetype(name)
  81. file_path = SubElement(file, 'FilePath')
  82. try: # python 2
  83. file_path.text = path.decode(fs_encoding)
  84. except: # python 3
  85. file_path.text = path
  86. return group
  87. def MDK4AddLibToGroup(ProjectFiles, group, name, filename, project_path):
  88. name = os.path.basename(filename)
  89. path = os.path.dirname (filename)
  90. basename = os.path.basename(path)
  91. path = _make_path_relative(project_path, path)
  92. path = os.path.join(path, name)
  93. files = SubElement(group, 'Files')
  94. file = SubElement(files, 'File')
  95. file_name = SubElement(file, 'FileName')
  96. name = os.path.basename(path)
  97. if name.find('.cpp') != -1:
  98. obj_name = name.replace('.cpp', '.o')
  99. elif name.find('.c') != -1:
  100. obj_name = name.replace('.c', '.o')
  101. elif name.find('.s') != -1:
  102. obj_name = name.replace('.s', '.o')
  103. elif name.find('.S') != -1:
  104. obj_name = name.replace('.s', '.o')
  105. else:
  106. obj_name = name
  107. if ProjectFiles.count(obj_name):
  108. name = basename + '_' + name
  109. ProjectFiles.append(obj_name)
  110. try:
  111. file_name.text = name.decode(fs_encoding)
  112. except:
  113. file_name.text = name
  114. file_type = SubElement(file, 'FileType')
  115. file_type.text = '%d' % _get_filetype(name)
  116. file_path = SubElement(file, 'FilePath')
  117. try:
  118. file_path.text = path.decode(fs_encoding)
  119. except:
  120. file_path.text = path
  121. return group
  122. def MDK4AddGroup(ProjectFiles, parent, name, files, project_path, group_scons):
  123. # don't add an empty group
  124. if len(files) == 0:
  125. return
  126. group = SubElement(parent, 'Group')
  127. group_name = SubElement(group, 'GroupName')
  128. group_name.text = name
  129. for f in files:
  130. fn = f.rfile()
  131. name = fn.name
  132. path = os.path.dirname(fn.abspath)
  133. basename = os.path.basename(path)
  134. path = _make_path_relative(project_path, path)
  135. path = os.path.join(path, name)
  136. files = SubElement(group, 'Files')
  137. file = SubElement(files, 'File')
  138. file_name = SubElement(file, 'FileName')
  139. name = os.path.basename(path)
  140. if name.find('.cpp') != -1:
  141. obj_name = name.replace('.cpp', '.o')
  142. elif name.find('.c') != -1:
  143. obj_name = name.replace('.c', '.o')
  144. elif name.find('.s') != -1:
  145. obj_name = name.replace('.s', '.o')
  146. elif name.find('.S') != -1:
  147. obj_name = name.replace('.s', '.o')
  148. if ProjectFiles.count(obj_name):
  149. name = basename + '_' + name
  150. ProjectFiles.append(obj_name)
  151. file_name.text = name # name.decode(fs_encoding)
  152. file_type = SubElement(file, 'FileType')
  153. file_type.text = '%d' % _get_filetype(name)
  154. file_path = SubElement(file, 'FilePath')
  155. file_path.text = path # path.decode(fs_encoding)
  156. # for local LOCAL_CFLAGS/LOCAL_CXXFLAGS/LOCAL_CCFLAGS/LOCAL_CPPPATH/LOCAL_CPPDEFINES
  157. MiscControls_text = ' '
  158. if file_type.text == '1' and 'LOCAL_CFLAGS' in group_scons:
  159. MiscControls_text = MiscControls_text + group_scons['LOCAL_CFLAGS']
  160. elif file_type.text == '8' and 'LOCAL_CXXFLAGS' in group_scons:
  161. MiscControls_text = MiscControls_text + group_scons['LOCAL_CXXFLAGS']
  162. if 'LOCAL_CCFLAGS' in group_scons:
  163. MiscControls_text = MiscControls_text + group_scons['LOCAL_CCFLAGS']
  164. if MiscControls_text != ' ' or ('LOCAL_CPPDEFINES' in group_scons):
  165. FileOption = SubElement(file, 'FileOption')
  166. FileArmAds = SubElement(FileOption, 'FileArmAds')
  167. Cads = SubElement(FileArmAds, 'Cads')
  168. VariousControls = SubElement(Cads, 'VariousControls')
  169. MiscControls = SubElement(VariousControls, 'MiscControls')
  170. MiscControls.text = MiscControls_text
  171. Define = SubElement(VariousControls, 'Define')
  172. if 'LOCAL_CPPDEFINES' in group_scons:
  173. Define.text = ', '.join(set(group_scons['LOCAL_CPPDEFINES']))
  174. else:
  175. Define.text = ' '
  176. Undefine = SubElement(VariousControls, 'Undefine')
  177. Undefine.text = ' '
  178. IncludePath = SubElement(VariousControls, 'IncludePath')
  179. if 'LOCAL_CPPPATH' in group_scons:
  180. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in group_scons['LOCAL_CPPPATH']])
  181. else:
  182. IncludePath.text = ' '
  183. return group
  184. # The common part of making MDK4/5 project
  185. def MDK45Project(tree, target, script):
  186. project_path = os.path.dirname(os.path.abspath(target))
  187. root = tree.getroot()
  188. out = open(target, 'w')
  189. out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
  190. CPPPATH = []
  191. CPPDEFINES = []
  192. LINKFLAGS = ''
  193. CXXFLAGS = ''
  194. CCFLAGS = ''
  195. CFLAGS = ''
  196. ProjectFiles = []
  197. # add group
  198. groups = tree.find('Targets/Target/Groups')
  199. if groups is None:
  200. groups = SubElement(tree.find('Targets/Target'), 'Groups')
  201. groups.clear() # clean old groups
  202. for group in script:
  203. group_tree = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path, group)
  204. # get each include path
  205. if 'CPPPATH' in group and group['CPPPATH']:
  206. if CPPPATH:
  207. CPPPATH += group['CPPPATH']
  208. else:
  209. CPPPATH += group['CPPPATH']
  210. # get each group's definitions
  211. if 'CPPDEFINES' in group and group['CPPDEFINES']:
  212. if CPPDEFINES:
  213. CPPDEFINES += group['CPPDEFINES']
  214. else:
  215. CPPDEFINES = group['CPPDEFINES']
  216. # get each group's link flags
  217. if 'LINKFLAGS' in group and group['LINKFLAGS']:
  218. if LINKFLAGS:
  219. LINKFLAGS += ' ' + group['LINKFLAGS']
  220. else:
  221. LINKFLAGS += group['LINKFLAGS']
  222. # get each group's CXXFLAGS flags
  223. if 'CXXFLAGS' in group and group['CXXFLAGS']:
  224. if CXXFLAGS:
  225. CXXFLAGS += ' ' + group['CXXFLAGS']
  226. else:
  227. CXXFLAGS += group['CXXFLAGS']
  228. # get each group's CCFLAGS flags
  229. if 'CCFLAGS' in group and group['CCFLAGS']:
  230. if CCFLAGS:
  231. CCFLAGS += ' ' + group['CCFLAGS']
  232. else:
  233. CCFLAGS += group['CCFLAGS']
  234. # get each group's CFLAGS flags
  235. if 'CFLAGS' in group and group['CFLAGS']:
  236. if CFLAGS:
  237. CFLAGS += ' ' + group['CFLAGS']
  238. else:
  239. CFLAGS += group['CFLAGS']
  240. # get each group's LIBS flags
  241. if 'LIBS' in group and group['LIBS']:
  242. for item in group['LIBS']:
  243. lib_path = ''
  244. for path_item in group['LIBPATH']:
  245. full_path = os.path.join(path_item, item + '.lib')
  246. if os.path.isfile(full_path): # has this library
  247. lib_path = full_path
  248. break
  249. if lib_path != '':
  250. if group_tree != None:
  251. MDK4AddLibToGroup(ProjectFiles, group_tree, group['name'], lib_path, project_path)
  252. else:
  253. group_tree = MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
  254. # write include path, definitions and link flags
  255. IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
  256. IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in set(CPPPATH)])
  257. Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
  258. Define.text = ', '.join(set(CPPDEFINES))
  259. if 'c99' in CXXFLAGS or 'c99' in CCFLAGS or 'c99' in CFLAGS:
  260. uC99 = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/uC99')
  261. uC99.text = '1'
  262. if 'gnu' in CXXFLAGS or 'gnu' in CCFLAGS or 'gnu' in CFLAGS:
  263. uGnu = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/uGnu')
  264. uGnu.text = '1'
  265. Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
  266. Misc.text = LINKFLAGS
  267. xml_indent(root)
  268. out.write(etree.tostring(root, encoding='utf-8').decode())
  269. out.close()
  270. def MDK4Project(target, script):
  271. if os.path.isfile('template.uvproj') is False:
  272. print ('Warning: The template project file [template.uvproj] not found!')
  273. return
  274. template_tree = etree.parse('template.uvproj')
  275. MDK45Project(template_tree, target, script)
  276. # remove project.uvopt file
  277. project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
  278. if os.path.isfile(project_uvopt):
  279. os.unlink(project_uvopt)
  280. # copy uvopt file
  281. if os.path.exists('template.uvopt'):
  282. import shutil
  283. shutil.copy2('template.uvopt', '{}.uvopt'.format(os.path.splitext(target)[0]))
  284. def MDK5Project(target, script):
  285. if os.path.isfile('template.uvprojx') is False:
  286. print ('Warning: The template project file [template.uvprojx] not found!')
  287. return
  288. template_tree = etree.parse('template.uvprojx')
  289. MDK45Project(template_tree, target, script)
  290. # remove project.uvopt file
  291. project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
  292. if os.path.isfile(project_uvopt):
  293. os.unlink(project_uvopt)
  294. # copy uvopt file
  295. if os.path.exists('template.uvoptx'):
  296. import shutil
  297. shutil.copy2('template.uvoptx', '{}.uvoptx'.format(os.path.splitext(target)[0]))
  298. def MDK2Project(target, script):
  299. template = open('template.Uv2', "r")
  300. lines = template.readlines()
  301. project = open(target, "w")
  302. project_path = os.path.dirname(os.path.abspath(target))
  303. line_index = 5
  304. # write group
  305. for group in script:
  306. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  307. line_index += 1
  308. lines.insert(line_index, '\r\n')
  309. line_index += 1
  310. # write file
  311. ProjectFiles = []
  312. CPPPATH = []
  313. CPPDEFINES = []
  314. LINKFLAGS = ''
  315. CFLAGS = ''
  316. # number of groups
  317. group_index = 1
  318. for group in script:
  319. # print group['name']
  320. # get each include path
  321. if 'CPPPATH' in group and group['CPPPATH']:
  322. if CPPPATH:
  323. CPPPATH += group['CPPPATH']
  324. else:
  325. CPPPATH += group['CPPPATH']
  326. # get each group's definitions
  327. if 'CPPDEFINES' in group and group['CPPDEFINES']:
  328. if CPPDEFINES:
  329. CPPDEFINES += group['CPPDEFINES']
  330. else:
  331. CPPDEFINES = group['CPPDEFINES']
  332. # get each group's link flags
  333. if 'LINKFLAGS' in group and group['LINKFLAGS']:
  334. if LINKFLAGS:
  335. LINKFLAGS += ' ' + group['LINKFLAGS']
  336. else:
  337. LINKFLAGS += group['LINKFLAGS']
  338. # generate file items
  339. for node in group['src']:
  340. fn = node.rfile()
  341. name = fn.name
  342. path = os.path.dirname(fn.abspath)
  343. basename = os.path.basename(path)
  344. path = _make_path_relative(project_path, path)
  345. path = os.path.join(path, name)
  346. if ProjectFiles.count(name):
  347. name = basename + '_' + name
  348. ProjectFiles.append(name)
  349. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  350. % (group_index, _get_filetype(name), path, name))
  351. line_index += 1
  352. group_index = group_index + 1
  353. lines.insert(line_index, '\r\n')
  354. line_index += 1
  355. # remove repeat path
  356. paths = set()
  357. for path in CPPPATH:
  358. inc = _make_path_relative(project_path, os.path.normpath(path))
  359. paths.add(inc) #.replace('\\', '/')
  360. paths = [i for i in paths]
  361. CPPPATH = string.join(paths, ';')
  362. definitions = [i for i in set(CPPDEFINES)]
  363. CPPDEFINES = string.join(definitions, ', ')
  364. while line_index < len(lines):
  365. if lines[line_index].startswith(' ADSCINCD '):
  366. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  367. if lines[line_index].startswith(' ADSLDMC ('):
  368. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  369. if lines[line_index].startswith(' ADSCDEFN ('):
  370. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  371. line_index += 1
  372. # write project
  373. for line in lines:
  374. project.write(line)
  375. project.close()
  376. def ARMCC_Version():
  377. import rtconfig
  378. import subprocess
  379. import re
  380. path = rtconfig.EXEC_PATH
  381. if(rtconfig.PLATFORM == 'armcc'):
  382. path = os.path.join(path, 'armcc.exe')
  383. elif(rtconfig.PLATFORM == 'armclang'):
  384. path = os.path.join(path, 'armlink.exe')
  385. if os.path.exists(path):
  386. cmd = path
  387. else:
  388. return "0.0"
  389. child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  390. stdout, stderr = child.communicate()
  391. '''
  392. example stdout:
  393. Product: MDK Plus 5.24
  394. Component: ARM Compiler 5.06 update 5 (build 528)
  395. Tool: armcc [4d3621]
  396. return version: MDK Plus 5.24/ARM Compiler 5.06 update 5 (build 528)/armcc [4d3621]
  397. '''
  398. if not isinstance(stdout, str):
  399. stdout = str(stdout, 'utf8') # Patch for Python 3
  400. version_Product = re.search(r'Product: (.+)', stdout).group(1)
  401. version_Product = version_Product[:-1]
  402. version_Component = re.search(r'Component: (.*)', stdout).group(1)
  403. version_Component = version_Component[:-1]
  404. version_Tool = re.search(r'Tool: (.*)', stdout).group(1)
  405. version_Tool = version_Tool[:-1]
  406. version_str_format = '%s/%s/%s'
  407. version_str = version_str_format % (version_Product, version_Component, version_Tool)
  408. return version_str