1
0

vs2012.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 uuid
  28. import utils
  29. from xml.etree.ElementTree import SubElement
  30. from utils import _make_path_relative
  31. from utils import xml_indent
  32. # Add parent directory to path to import building
  33. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  34. import building
  35. import xml.etree.ElementTree as etree
  36. fs_encoding = sys.getfilesystemencoding()
  37. #reference
  38. # http://woodpecker.org.cn/diveintopython3/xml.html
  39. # https://pycoders-weekly-chinese.readthedocs.org/en/latest/issue6/processing-xml-in-python-with-element-tree.html
  40. # http://www.cnblogs.com/ifantastic/archive/2013/04/12/3017110.html
  41. filter_project = etree.Element('Project', attrib={'ToolsVersion':'4.0'})
  42. def get_uuid():
  43. id = uuid.uuid1() # UUID('3e5526c0-2841-11e3-a376-20cf3048bcb3')
  44. if sys.version > '3':
  45. idstr = id.urn[9:] #'urn:uuid:3e5526c0-2841-11e3-a376-20cf3048bcb3'[9:]
  46. else:
  47. # python3 is no decode function
  48. idstr = id.get_urn()[9:] #'urn:uuid:3e5526c0-2841-11e3-a376-20cf3048bcb3'[9:]
  49. return '{'+idstr+'}'
  50. def VS2012_AddGroup(parent, group_name, files, project_path):
  51. for f in files:
  52. fn = f.rfile()
  53. name = fn.name
  54. path = os.path.dirname(fn.abspath)
  55. path = _make_path_relative(project_path, path)
  56. path = os.path.join(path, name)
  57. ClCompile = SubElement(parent, 'ClCompile')
  58. if sys.version > '3':
  59. ClCompile.set('Include', path)
  60. else:
  61. # python3 is no decode function
  62. ClCompile.set('Include', path.decode(fs_encoding))
  63. Filter = SubElement(ClCompile, 'Filter')
  64. Filter.text='Source Files\\'+group_name
  65. def VS2012_CreateFilter(script, project_path):
  66. c_ItemGroup = SubElement(filter_project, 'ItemGroup')
  67. filter_ItemGroup = SubElement(filter_project, 'ItemGroup')
  68. Filter = SubElement(filter_ItemGroup, 'Filter')
  69. Filter.set('Include', 'Source Files')
  70. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  71. UniqueIdentifier.text = get_uuid()
  72. Extensions = SubElement(Filter, 'Extensions')
  73. Extensions.text = 'cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx'
  74. Filter = SubElement(filter_ItemGroup, 'Filter')
  75. Filter.set('Include', 'Header Files')
  76. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  77. UniqueIdentifier.text = get_uuid()
  78. Extensions = SubElement(Filter, 'Extensions')
  79. Extensions.text = 'h;hpp;hxx;hm;inl;inc;xsd'
  80. for group in script:
  81. VS2012_AddGroup(c_ItemGroup, group['name'], group['src'], project_path)
  82. Filter = SubElement(filter_ItemGroup, 'Filter')
  83. Filter.set('Include', 'Source Files\\'+group['name'])
  84. UniqueIdentifier = SubElement(Filter, 'UniqueIdentifier')
  85. UniqueIdentifier.text = get_uuid()
  86. #program: object from scons
  87. # parent: xml node
  88. # file_type: C or H
  89. # files: c/h list
  90. # project_path
  91. def VS_add_ItemGroup(parent, file_type, files, project_path):
  92. from building import Rtt_Root
  93. RTT_ROOT = os.path.normpath(Rtt_Root)
  94. file_dict = {'C':"ClCompile", 'H':'ClInclude'}
  95. item_tag = file_dict[file_type]
  96. ItemGroup = SubElement(parent, 'ItemGroup')
  97. for f in files:
  98. fn = f.rfile()
  99. name = fn.name
  100. path = os.path.dirname(fn.abspath)
  101. objpath = path.lower()
  102. if len(project_path) >= len(RTT_ROOT) :
  103. if objpath.startswith(project_path.lower()) :
  104. objpath = ''.join('bsp'+objpath[len(project_path):])
  105. else :
  106. objpath = ''.join('kernel'+objpath[len(RTT_ROOT):])
  107. else :
  108. if objpath.startswith(RTT_ROOT.lower()) :
  109. objpath = ''.join('kernel'+objpath[len(RTT_ROOT):])
  110. else :
  111. objpath = ''.join('bsp'+objpath[len(project_path):])
  112. path = _make_path_relative(project_path, path)
  113. path = os.path.join(path, name)
  114. File = SubElement(ItemGroup, item_tag)
  115. if sys.version > '3':
  116. File.set('Include', path)
  117. else:
  118. # python3 is no decode function
  119. File.set('Include', path.decode(fs_encoding))
  120. if file_type == 'C' :
  121. ObjName = SubElement(File, 'ObjectFileName')
  122. ObjName.text = ''.join('$(IntDir)'+objpath+'\\')
  123. def VS_add_HeadFiles(program, elem, project_path):
  124. utils.source_ext = []
  125. utils.source_ext = ["h"]
  126. for item in program:
  127. utils.walk_children(item)
  128. utils.source_list.sort()
  129. # print utils.source_list
  130. ItemGroup = SubElement(elem, 'ItemGroup')
  131. filter_h_ItemGroup = SubElement(filter_project, 'ItemGroup')
  132. for f in utils.source_list:
  133. path = _make_path_relative(project_path, f)
  134. File = SubElement(ItemGroup, 'ClInclude')
  135. if sys.version > '3':
  136. File.set('Include', path)
  137. else:
  138. # python3 is no decode function
  139. File.set('Include', path.decode(fs_encoding))
  140. # add project.vcxproj.filter
  141. ClInclude = SubElement(filter_h_ItemGroup, 'ClInclude')
  142. if sys.version > '3':
  143. ClInclude.set('Include', path)
  144. else:
  145. # python3 is no decode function
  146. ClInclude.set('Include', path.decode(fs_encoding))
  147. Filter = SubElement(ClInclude, 'Filter')
  148. Filter.text='Header Files'
  149. def VS2012Project(target, script, program):
  150. project_path = os.path.dirname(os.path.abspath(target))
  151. tree = etree.parse('template_vs2012.vcxproj')
  152. root = tree.getroot()
  153. elem = root
  154. out = open(target, 'w')
  155. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  156. ProjectFiles = []
  157. # add "*.c or *.h" files
  158. VS2012_CreateFilter(script, project_path)
  159. # add "*.c" files
  160. for group in script:
  161. VS_add_ItemGroup(elem, 'C', group['src'], project_path)
  162. # add "*.h" files
  163. VS_add_HeadFiles(program, elem, project_path)
  164. # write head include path
  165. if 'CPPPATH' in building.Env:
  166. cpp_path = building.Env['CPPPATH']
  167. paths = set()
  168. for path in cpp_path:
  169. inc = _make_path_relative(project_path, os.path.normpath(path))
  170. paths.add(inc) #.replace('\\', '/')
  171. paths = [i for i in paths]
  172. paths.sort()
  173. cpp_path = ';'.join(paths) + ';%(AdditionalIncludeDirectories)'
  174. # write include path
  175. for elem in tree.iter(tag='AdditionalIncludeDirectories'):
  176. elem.text = cpp_path
  177. break
  178. # write cppdefinitons flags
  179. if 'CPPDEFINES' in building.Env:
  180. for elem in tree.iter(tag='PreprocessorDefinitions'):
  181. CPPDEFINES = building.Env['CPPDEFINES']
  182. definitions = []
  183. if type(CPPDEFINES[0]) == type(()):
  184. for item in CPPDEFINES:
  185. definitions += [i for i in item]
  186. definitions = ';'.join(definitions)
  187. else:
  188. definitions = ';'.join(building.Env['CPPDEFINES'])
  189. definitions = definitions + ';%(PreprocessorDefinitions)'
  190. elem.text = definitions
  191. break
  192. # write link flags
  193. # write lib dependence (Link)
  194. if 'LIBS' in building.Env:
  195. for elem in tree.iter(tag='AdditionalDependencies'):
  196. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  197. libs = ';'.join(libs_with_extention) + ';%(AdditionalDependencies)'
  198. elem.text = libs
  199. break
  200. # write lib include path
  201. if 'LIBPATH' in building.Env:
  202. lib_path = building.Env['LIBPATH']
  203. paths = set()
  204. for path in lib_path:
  205. inc = _make_path_relative(project_path, os.path.normpath(path))
  206. paths.add(inc)
  207. paths = [i for i in paths]
  208. paths.sort()
  209. lib_paths = ';'.join(paths) + ';%(AdditionalLibraryDirectories)'
  210. for elem in tree.iter(tag='AdditionalLibraryDirectories'):
  211. elem.text = lib_paths
  212. break
  213. xml_indent(root)
  214. if sys.version > '3':
  215. vcxproj_string = etree.tostring(root, encoding='unicode')
  216. else:
  217. # python3 is no decode function
  218. vcxproj_string = etree.tostring(root, encoding='utf-8')
  219. root_node=r'<Project DefaultTargets="Build" ToolsVersion="4.0">'
  220. out.write(r'<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  221. out.write(vcxproj_string[len(root_node):])
  222. out.close()
  223. xml_indent(filter_project)
  224. if sys.version > '3':
  225. filter_string = etree.tostring(filter_project, encoding='unicode')
  226. else:
  227. # python3 is no decode function
  228. filter_string = etree.tostring(filter_project, encoding='utf-8')
  229. out = open('project.vcxproj.filters', 'w')
  230. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  231. root_node=r'<Project ToolsVersion="4.0">'
  232. out.write(r'<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">')
  233. out.write(filter_string[len(root_node):])
  234. out.close()