vs2012.py 8.3 KB

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