iar.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import sys
  3. import string
  4. import xml.etree.ElementTree as etree
  5. from xml.etree.ElementTree import SubElement
  6. from utils import _make_path_relative
  7. from utils import xml_indent
  8. fs_encoding = sys.getfilesystemencoding()
  9. iar_workspace = '''<?xml version="1.0" encoding="iso-8859-1"?>
  10. <workspace>
  11. <project>
  12. <path>$WS_DIR$\%s</path>
  13. </project>
  14. <batchBuild/>
  15. </workspace>
  16. '''
  17. def IARAddGroup(parent, name, files, project_path):
  18. group = SubElement(parent, 'group')
  19. group_name = SubElement(group, 'name')
  20. group_name.text = name
  21. for f in files:
  22. fn = f.rfile()
  23. name = fn.name
  24. path = os.path.dirname(fn.abspath)
  25. basename = os.path.basename(path)
  26. path = _make_path_relative(project_path, path)
  27. path = os.path.join(path, name)
  28. file = SubElement(group, 'file')
  29. file_name = SubElement(file, 'name')
  30. if os.path.isabs(path):
  31. file_name.text = path.decode(fs_encoding)
  32. else:
  33. file_name.text = ('$PROJ_DIR$\\' + path).decode(fs_encoding)
  34. def IARWorkspace(target):
  35. # make an workspace
  36. workspace = target.replace('.ewp', '.eww')
  37. out = file(workspace, 'wb')
  38. xml = iar_workspace % target
  39. out.write(xml)
  40. out.close()
  41. def IARProject(target, script):
  42. project_path = os.path.dirname(os.path.abspath(target))
  43. tree = etree.parse('template.ewp')
  44. root = tree.getroot()
  45. out = file(target, 'wb')
  46. CPPPATH = []
  47. CPPDEFINES = []
  48. LINKFLAGS = ''
  49. CCFLAGS = ''
  50. # add group
  51. for group in script:
  52. IARAddGroup(root, group['name'], group['src'], project_path)
  53. # get each include path
  54. if group.has_key('CPPPATH') and group['CPPPATH']:
  55. CPPPATH += group['CPPPATH']
  56. # get each group's definitions
  57. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  58. CPPDEFINES += group['CPPDEFINES']
  59. # get each group's link flags
  60. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  61. LINKFLAGS += group['LINKFLAGS']
  62. # make relative path
  63. paths = set()
  64. for path in CPPPATH:
  65. inc = _make_path_relative(project_path, os.path.normpath(path))
  66. paths.add(inc) #.replace('\\', '/')
  67. # setting options
  68. options = tree.findall('configuration/settings/data/option')
  69. for option in options:
  70. # print option.text
  71. name = option.find('name')
  72. if name.text == 'CCIncludePath2' or name.text == 'newCCIncludePaths':
  73. for path in paths:
  74. state = SubElement(option, 'state')
  75. if os.path.isabs(path):
  76. state.text = path
  77. else:
  78. state.text = '$PROJ_DIR$\\' + path
  79. if name.text == 'CCDefines':
  80. for define in CPPDEFINES:
  81. state = SubElement(option, 'state')
  82. state.text = define
  83. xml_indent(root)
  84. out.write(etree.tostring(root, encoding='utf-8'))
  85. out.close()
  86. IARWorkspace(target)