1
0

iar.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # add group
  74. for group in script:
  75. IARAddGroup(root, group['name'], group['src'], project_path)
  76. # get each include path
  77. if group.has_key('CPPPATH') and group['CPPPATH']:
  78. CPPPATH += group['CPPPATH']
  79. # get each group's definitions
  80. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  81. CPPDEFINES += group['CPPDEFINES']
  82. # get each group's link flags
  83. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  84. LINKFLAGS += group['LINKFLAGS']
  85. # make relative path
  86. paths = set()
  87. for path in CPPPATH:
  88. inc = _make_path_relative(project_path, os.path.normpath(path))
  89. paths.add(inc) #.replace('\\', '/')
  90. # setting options
  91. options = tree.findall('configuration/settings/data/option')
  92. for option in options:
  93. # print option.text
  94. name = option.find('name')
  95. if name.text == 'CCIncludePath2' or name.text == 'newCCIncludePaths':
  96. for path in paths:
  97. state = SubElement(option, 'state')
  98. if os.path.isabs(path):
  99. state.text = path
  100. else:
  101. state.text = '$PROJ_DIR$\\' + path
  102. if name.text == 'CCDefines':
  103. for define in CPPDEFINES:
  104. state = SubElement(option, 'state')
  105. state.text = define
  106. xml_indent(root)
  107. out.write(etree.tostring(root, encoding='utf-8'))
  108. out.close()
  109. IARWorkspace(target)