1
0

vs.py 5.9 KB

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