iar.py 6.3 KB

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