vs.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #
  2. # File : vs.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 building
  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. def VS_AddGroup(ProjectFiles, parent, name, files, libs, project_path):
  34. Filter = SubElement(parent, 'Filter')
  35. Filter.set('Name', name) #set group name to group
  36. for f in files:
  37. fn = f.rfile()
  38. name = fn.name
  39. path = os.path.dirname(fn.abspath)
  40. path = _make_path_relative(project_path, path)
  41. path = os.path.join(path, name)
  42. File = SubElement(Filter, 'File')
  43. File.set('RelativePath', path.decode(fs_encoding))
  44. for lib in libs:
  45. name = os.path.basename(lib)
  46. path = os.path.dirname(lib)
  47. path = _make_path_relative(project_path, path)
  48. path = os.path.join(path, name)
  49. File = SubElement(Filter, 'File')
  50. File.set('RelativePath', path.decode(fs_encoding))
  51. def VS_AddHeadFilesGroup(program, elem, project_path):
  52. building.source_ext = []
  53. building.source_ext = ["h"]
  54. for item in program:
  55. building.walk_children(item)
  56. building.source_list.sort()
  57. # print building.source_list
  58. for f in building.source_list:
  59. path = _make_path_relative(project_path, f)
  60. File = SubElement(elem, 'File')
  61. File.set('RelativePath', path.decode(fs_encoding))
  62. def VSProject(target, script, program):
  63. project_path = os.path.dirname(os.path.abspath(target))
  64. tree = etree.parse('template_vs2005.vcproj')
  65. root = tree.getroot()
  66. out = file(target, 'wb')
  67. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  68. ProjectFiles = []
  69. # add "*.c" files group
  70. for elem in tree.iter(tag='Filter'):
  71. if elem.attrib['Name'] == 'Source Files':
  72. #print elem.tag, elem.attrib
  73. break
  74. for group in script:
  75. libs = []
  76. if group.has_key('LIBS') and group['LIBS']:
  77. for item in group['LIBS']:
  78. lib_path = ''
  79. for path_item in group['LIBPATH']:
  80. full_path = os.path.join(path_item, item + '.lib')
  81. if os.path.isfile(full_path): # has this library
  82. lib_path = full_path
  83. if lib_path != '':
  84. libs.append(lib_path)
  85. group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], libs, project_path)
  86. # add "*.h" files group
  87. for elem in tree.iter(tag='Filter'):
  88. if elem.attrib['Name'] == 'Header Files':
  89. break
  90. VS_AddHeadFilesGroup(program, elem, project_path)
  91. # write head include path
  92. if building.Env.has_key('CPPPATH'):
  93. cpp_path = building.Env['CPPPATH']
  94. paths = set()
  95. for path in cpp_path:
  96. inc = _make_path_relative(project_path, os.path.normpath(path))
  97. paths.add(inc) #.replace('\\', '/')
  98. paths = [i for i in paths]
  99. paths.sort()
  100. cpp_path = ';'.join(paths)
  101. # write include path, definitions
  102. for elem in tree.iter(tag='Tool'):
  103. if elem.attrib['Name'] == 'VCCLCompilerTool':
  104. #print elem.tag, elem.attrib
  105. break
  106. elem.set('AdditionalIncludeDirectories', cpp_path)
  107. # write cppdefinitons flags
  108. if building.Env.has_key('CPPDEFINES'):
  109. CPPDEFINES = building.Env['CPPDEFINES']
  110. definitions = []
  111. if type(CPPDEFINES[0]) == type(()):
  112. for item in CPPDEFINES:
  113. definitions += [i for i in item]
  114. definitions = ';'.join(definitions)
  115. else:
  116. definitions = ';'.join(building.Env['CPPDEFINES'])
  117. elem.set('PreprocessorDefinitions', definitions)
  118. # write link flags
  119. # write lib dependence
  120. if building.Env.has_key('LIBS'):
  121. for elem in tree.iter(tag='Tool'):
  122. if elem.attrib['Name'] == 'VCLinkerTool':
  123. break
  124. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  125. libs = ' '.join(libs_with_extention)
  126. elem.set('AdditionalDependencies', libs)
  127. # write lib include path
  128. if building.Env.has_key('LIBPATH'):
  129. lib_path = building.Env['LIBPATH']
  130. paths = set()
  131. for path in lib_path:
  132. inc = _make_path_relative(project_path, os.path.normpath(path))
  133. paths.add(inc) #.replace('\\', '/')
  134. paths = [i for i in paths]
  135. paths.sort()
  136. lib_paths = ';'.join(paths)
  137. elem.set('AdditionalLibraryDirectories', lib_paths)
  138. xml_indent(root)
  139. out.write(etree.tostring(root, encoding='utf-8'))
  140. out.close()