iar.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #
  2. # File : iar.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. iar_workspace = '''<?xml version="1.0" encoding="iso-8859-1"?>
  33. <workspace>
  34. <project>
  35. <path>$WS_DIR$\%s</path>
  36. </project>
  37. <batchBuild/>
  38. </workspace>
  39. '''
  40. def IARAddGroup(parent, name, files, project_path):
  41. group = SubElement(parent, 'group')
  42. group_name = SubElement(group, 'name')
  43. group_name.text = name
  44. for f in files:
  45. fn = f.rfile()
  46. name = fn.name
  47. path = os.path.dirname(fn.abspath)
  48. basename = os.path.basename(path)
  49. path = _make_path_relative(project_path, path)
  50. path = os.path.join(path, name)
  51. file = SubElement(group, 'file')
  52. file_name = SubElement(file, 'name')
  53. if os.path.isabs(path):
  54. file_name.text = path.decode(fs_encoding)
  55. else:
  56. file_name.text = ('$PROJ_DIR$\\' + path).decode(fs_encoding)
  57. def IARWorkspace(target):
  58. # make an workspace
  59. workspace = target.replace('.ewp', '.eww')
  60. out = file(workspace, 'wb')
  61. xml = iar_workspace % target
  62. out.write(xml)
  63. out.close()
  64. def IARProject(target, script):
  65. project_path = os.path.dirname(os.path.abspath(target))
  66. tree = etree.parse('template.ewp')
  67. root = tree.getroot()
  68. out = file(target, 'wb')
  69. CPPPATH = []
  70. CPPDEFINES = []
  71. LINKFLAGS = ''
  72. CCFLAGS = ''
  73. Libs = []
  74. lib_prefix = ['lib', '']
  75. lib_suffix = ['.a', '.o', '']
  76. def searchLib(group):
  77. for path_item in group['LIBPATH']:
  78. for prefix_item in lib_prefix:
  79. for suffix_item in lib_suffix:
  80. lib_full_path = os.path.join(path_item, prefix_item + item + suffix_item)
  81. if os.path.isfile(lib_full_path):
  82. return lib_full_path
  83. else:
  84. return ''
  85. # add group
  86. for group in script:
  87. IARAddGroup(root, group['name'], group['src'], project_path)
  88. # get each include path
  89. if group.has_key('CPPPATH') and group['CPPPATH']:
  90. CPPPATH += group['CPPPATH']
  91. # get each group's definitions
  92. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  93. CPPDEFINES += group['CPPDEFINES']
  94. # get each group's link flags
  95. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  96. LINKFLAGS += group['LINKFLAGS']
  97. if group.has_key('LIBS') and group['LIBS']:
  98. for item in group['LIBS']:
  99. lib_path = searchLib(group)
  100. if lib_path != '':
  101. lib_path = _make_path_relative(project_path, lib_path)
  102. Libs += [lib_path]
  103. # print('found lib isfile: ' + lib_path)
  104. else:
  105. print('not found LIB: ' + item)
  106. # make relative path
  107. paths = set()
  108. for path in CPPPATH:
  109. inc = _make_path_relative(project_path, os.path.normpath(path))
  110. paths.add(inc) #.replace('\\', '/')
  111. # setting options
  112. options = tree.findall('configuration/settings/data/option')
  113. for option in options:
  114. # print option.text
  115. name = option.find('name')
  116. if name.text == 'CCIncludePath2' or name.text == 'newCCIncludePaths':
  117. for path in paths:
  118. state = SubElement(option, 'state')
  119. if os.path.isabs(path) or path.startswith('$'):
  120. state.text = path
  121. else:
  122. state.text = '$PROJ_DIR$\\' + path
  123. if name.text == 'CCDefines':
  124. for define in CPPDEFINES:
  125. state = SubElement(option, 'state')
  126. state.text = define
  127. if name.text == 'IlinkAdditionalLibs':
  128. for path in Libs:
  129. state = SubElement(option, 'state')
  130. if os.path.isabs(path) or path.startswith('$'):
  131. path = path.decode(fs_encoding)
  132. else:
  133. path = ('$PROJ_DIR$\\' + path).decode(fs_encoding)
  134. state.text = path
  135. xml_indent(root)
  136. out.write(etree.tostring(root, encoding='utf-8'))
  137. out.close()
  138. IARWorkspace(target)
  139. def IARVersion():
  140. import subprocess
  141. import re
  142. def IARPath():
  143. import rtconfig
  144. # backup environ
  145. old_environ = os.environ
  146. os.environ['RTT_CC'] = 'iar'
  147. reload(rtconfig)
  148. # get iar path
  149. path = rtconfig.EXEC_PATH
  150. # restore environ
  151. os.environ = old_environ
  152. reload(rtconfig)
  153. return path
  154. path = IARPath();
  155. if os.path.exists(path):
  156. cmd = os.path.join(path, 'iccarm.exe')
  157. else:
  158. print('Error: get IAR version failed. Please update the IAR installation path in rtconfig.py!')
  159. return "0.0"
  160. child = subprocess.Popen([cmd, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  161. stdout, stderr = child.communicate()
  162. # example stdout: IAR ANSI C/C++ Compiler V8.20.1.14183/W32 for ARM
  163. return re.search('[\d\.]+', stdout).group(0)