vs2012.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. objpath = 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. if file_type == 'C' :
  93. ObjName = SubElement(File, 'ObjectFileName')
  94. ObjName.text = ''.join('$(IntDir)'+objpath+'\\')
  95. def VS_add_HeadFiles(program, elem, project_path):
  96. building.source_ext = []
  97. building.source_ext = ["h"]
  98. for item in program:
  99. building.walk_children(item)
  100. building.source_list.sort()
  101. # print building.source_list
  102. ItemGroup = SubElement(elem, 'ItemGroup')
  103. filter_h_ItemGroup = SubElement(filter_project, 'ItemGroup')
  104. for f in building.source_list:
  105. path = _make_path_relative(project_path, f)
  106. File = SubElement(ItemGroup, 'ClInclude')
  107. File.set('Include', path.decode(fs_encoding))
  108. # add project.vcxproj.filter
  109. ClInclude = SubElement(filter_h_ItemGroup, 'ClInclude')
  110. ClInclude.set('Include', path.decode(fs_encoding))
  111. Filter = SubElement(ClInclude, 'Filter')
  112. Filter.text='Header Files'
  113. def VS2012Project(target, script, program):
  114. project_path = os.path.dirname(os.path.abspath(target))
  115. tree = etree.parse('template_vs2012.vcxproj')
  116. root = tree.getroot()
  117. elem = root
  118. out = file(target, 'wb')
  119. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  120. ProjectFiles = []
  121. # add "*.c or *.h" files
  122. VS2012_CreateFilter(script, project_path)
  123. # add "*.c" files
  124. for group in script:
  125. VS_add_ItemGroup(elem, 'C', group['src'], project_path)
  126. # add "*.h" files
  127. VS_add_HeadFiles(program, elem, project_path)
  128. # write head include path
  129. if building.Env.has_key('CPPPATH'):
  130. cpp_path = building.Env['CPPPATH']
  131. paths = set()
  132. for path in cpp_path:
  133. inc = _make_path_relative(project_path, os.path.normpath(path))
  134. paths.add(inc) #.replace('\\', '/')
  135. paths = [i for i in paths]
  136. paths.sort()
  137. cpp_path = ';'.join(paths) + ';%(AdditionalIncludeDirectories)'
  138. # write include path
  139. for elem in tree.iter(tag='AdditionalIncludeDirectories'):
  140. elem.text = cpp_path
  141. break
  142. # write cppdefinitons flags
  143. if building.Env.has_key('CPPDEFINES'):
  144. for elem in tree.iter(tag='PreprocessorDefinitions'):
  145. definitions = ';'.join(building.Env['CPPDEFINES']) + ';%(PreprocessorDefinitions)'
  146. elem.text = definitions
  147. break
  148. # write link flags
  149. # write lib dependence (Link)
  150. if building.Env.has_key('LIBS'):
  151. for elem in tree.iter(tag='AdditionalDependencies'):
  152. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  153. libs = ';'.join(libs_with_extention) + ';%(AdditionalDependencies)'
  154. elem.text = libs
  155. break
  156. # write lib include path
  157. if building.Env.has_key('LIBPATH'):
  158. lib_path = building.Env['LIBPATH']
  159. paths = set()
  160. for path in lib_path:
  161. inc = _make_path_relative(project_path, os.path.normpath(path))
  162. paths.add(inc)
  163. paths = [i for i in paths]
  164. paths.sort()
  165. lib_paths = ';'.join(paths) + ';%(AdditionalLibraryDirectories)'
  166. for elem in tree.iter(tag='AdditionalLibraryDirectories'):
  167. elem.text = lib_paths
  168. break
  169. xml_indent(root)
  170. vcxproj_string = etree.tostring(root, encoding='utf-8')
  171. root_node=r'<Project DefaultTargets="Build" ToolsVersion="4.0">'
  172. out.write(r'<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  173. out.write(vcxproj_string[len(root_node):])
  174. out.close()
  175. xml_indent(filter_project)
  176. filter_string = etree.tostring(filter_project, encoding='utf-8')
  177. out = file('project.vcxproj.filters', 'wb')
  178. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  179. root_node=r'<Project ToolsVersion="4.0">'
  180. out.write(r'<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  181. out.write(filter_string[len(root_node):])
  182. out.close()