vs2012.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #
  2. # File : vs2012.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 uuid
  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. #reference
  35. # http://woodpecker.org.cn/diveintopython3/xml.html
  36. # https://pycoders-weekly-chinese.readthedocs.org/en/latest/issue6/processing-xml-in-python-with-element-tree.html
  37. # http://www.cnblogs.com/ifantastic/archive/2013/04/12/3017110.html
  38. filter_project = etree.Element('Project', attrib={'ToolsVersion':'4.0'})
  39. def get_uuid():
  40. id = uuid.uuid1() # UUID('3e5526c0-2841-11e3-a376-20cf3048bcb3')
  41. idstr = id.get_urn()[9:] #'urn:uuid:3e5526c0-2841-11e3-a376-20cf3048bcb3'[9:]
  42. return '{'+idstr+'}'
  43. def VS2012_AddGroup(parent, group_name, files, project_path):
  44. for f in files:
  45. fn = f.rfile()
  46. name = fn.name
  47. path = os.path.dirname(fn.abspath)
  48. path = _make_path_relative(project_path, path)
  49. path = os.path.join(path, name)
  50. ClCompile = SubElement(parent, 'ClCompile')
  51. ClCompile.set('Include', path.decode(fs_encoding))
  52. Filter = SubElement(ClCompile, 'Filter')
  53. Filter.text='Source Files\\'+group_name
  54. def VS2012_CreateFilter(script, project_path):
  55. c_ItemGroup = SubElement(filter_project, 'ItemGroup')
  56. filter_ItemGroup = SubElement(filter_project, 'ItemGroup')
  57. Filter = SubElement(filter_ItemGroup, 'Filter')
  58. Filter.set('Include', 'Source Files')
  59. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  60. UniqueIdentifier.text = get_uuid()
  61. Extensions = SubElement(Filter, 'Extensions')
  62. Extensions.text = 'cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx'
  63. Filter = SubElement(filter_ItemGroup, 'Filter')
  64. Filter.set('Include', 'Header Files')
  65. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  66. UniqueIdentifier.text = get_uuid()
  67. Extensions = SubElement(Filter, 'Extensions')
  68. Extensions.text = 'h;hpp;hxx;hm;inl;inc;xsd'
  69. for group in script:
  70. VS2012_AddGroup(c_ItemGroup, group['name'], group['src'], project_path)
  71. Filter = SubElement(filter_ItemGroup, 'Filter')
  72. Filter.set('Include', 'Source Files\\'+group['name'])
  73. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  74. UniqueIdentifier.text = get_uuid()
  75. #program: object from scons
  76. # parent: xml node
  77. # file_type: C or H
  78. # files: c/h list
  79. # project_path
  80. def VS_add_ItemGroup(parent, file_type, files, project_path):
  81. file_dict = {'C':"ClCompile", 'H':'ClInclude'}
  82. item_tag = file_dict[file_type]
  83. ItemGroup = SubElement(parent, 'ItemGroup')
  84. for f in files:
  85. fn = f.rfile()
  86. name = fn.name
  87. path = os.path.dirname(fn.abspath)
  88. path = _make_path_relative(project_path, path)
  89. path = os.path.join(path, name)
  90. File = SubElement(ItemGroup, item_tag)
  91. File.set('Include', path.decode(fs_encoding))
  92. def VS_add_HeadFiles(program, elem, project_path):
  93. building.source_ext = []
  94. building.source_ext = ["h"]
  95. for item in program:
  96. building.walk_children(item)
  97. building.source_list.sort()
  98. # print building.source_list
  99. ItemGroup = SubElement(elem, 'ItemGroup')
  100. filter_h_ItemGroup = SubElement(filter_project, 'ItemGroup')
  101. for f in building.source_list:
  102. path = _make_path_relative(project_path, f)
  103. File = SubElement(ItemGroup, 'ClInclude')
  104. File.set('Include', path.decode(fs_encoding))
  105. # add project.vcxproj.filter
  106. ClInclude = SubElement(filter_h_ItemGroup, 'ClInclude')
  107. ClInclude.set('Include', path.decode(fs_encoding))
  108. Filter = SubElement(ClInclude, 'Filter')
  109. Filter.text='Header Files'
  110. def VS2012Project(target, script, program):
  111. project_path = os.path.dirname(os.path.abspath(target))
  112. tree = etree.parse('template_vs2012.vcxproj')
  113. root = tree.getroot()
  114. elem = root
  115. out = file(target, 'wb')
  116. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  117. ProjectFiles = []
  118. # add "*.c or *.h" files
  119. VS2012_CreateFilter(script, project_path)
  120. # add "*.c" files
  121. for group in script:
  122. VS_add_ItemGroup(elem, 'C', group['src'], project_path)
  123. # add "*.h" files
  124. VS_add_HeadFiles(program, elem, project_path)
  125. # write head include path
  126. if building.Env.has_key('CPPPATH'):
  127. cpp_path = building.Env['CPPPATH']
  128. paths = set()
  129. for path in cpp_path:
  130. inc = _make_path_relative(project_path, os.path.normpath(path))
  131. paths.add(inc) #.replace('\\', '/')
  132. paths = [i for i in paths]
  133. paths.sort()
  134. cpp_path = ';'.join(paths) + ';%(AdditionalIncludeDirectories)'
  135. # write include path
  136. for elem in tree.iter(tag='AdditionalIncludeDirectories'):
  137. elem.text = cpp_path
  138. break
  139. # write cppdefinitons flags
  140. if building.Env.has_key('CPPDEFINES'):
  141. for elem in tree.iter(tag='PreprocessorDefinitions'):
  142. definitions = ';'.join(building.Env['CPPDEFINES']) + ';%(PreprocessorDefinitions)'
  143. elem.text = definitions
  144. break
  145. # write link flags
  146. # write lib dependence (Link)
  147. if building.Env.has_key('LIBS'):
  148. for elem in tree.iter(tag='AdditionalDependencies'):
  149. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  150. libs = ';'.join(libs_with_extention) + ';%(AdditionalDependencies)'
  151. elem.text = libs
  152. break
  153. # write lib include path
  154. if building.Env.has_key('LIBPATH'):
  155. lib_path = building.Env['LIBPATH']
  156. paths = set()
  157. for path in lib_path:
  158. inc = _make_path_relative(project_path, os.path.normpath(path))
  159. paths.add(inc)
  160. paths = [i for i in paths]
  161. paths.sort()
  162. lib_paths = ';'.join(paths) + ';%(AdditionalLibraryDirectories)'
  163. for elem in tree.iter(tag='AdditionalLibraryDirectories'):
  164. elem.text = lib_paths
  165. break
  166. xml_indent(root)
  167. vcxproj_string = etree.tostring(root, encoding='utf-8')
  168. root_node=r'<Project DefaultTargets="Build" ToolsVersion="4.0">'
  169. out.write(r'<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  170. out.write(vcxproj_string[len(root_node):])
  171. out.close()
  172. xml_indent(filter_project)
  173. filter_string = etree.tostring(filter_project, encoding='utf-8')
  174. out = file('project.vcxproj.filters', 'wb')
  175. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  176. root_node=r'<Project ToolsVersion="4.0">'
  177. out.write(r'<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  178. out.write(filter_string[len(root_node):])
  179. out.close()