iar.py 6.1 KB

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