1
0

vs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 building
  28. import xml.etree.ElementTree as etree
  29. from xml.etree.ElementTree import SubElement
  30. from utils import _make_path_relative
  31. from utils import xml_indent
  32. fs_encoding = sys.getfilesystemencoding()
  33. def VS_AddGroup(ProjectFiles, parent, name, files, project_path):
  34. Filter = SubElement(parent, 'Filter')
  35. Filter.set('Name', name) #set group name to group
  36. for f in files:
  37. fn = f.rfile()
  38. name = fn.name
  39. path = os.path.dirname(fn.abspath)
  40. path = _make_path_relative(project_path, path)
  41. path = os.path.join(path, name)
  42. File = SubElement(Filter, 'File')
  43. File.set('RelativePath', path.decode(fs_encoding))
  44. def VS_AddHeadFilesGroup(program, elem, project_path):
  45. building.source_ext = []
  46. building.source_ext = ["h"]
  47. for item in program:
  48. building.walk_children(item)
  49. building.source_list.sort()
  50. # print building.source_list
  51. for f in building.source_list:
  52. path = _make_path_relative(project_path, f)
  53. File = SubElement(elem, 'File')
  54. File.set('RelativePath', path.decode(fs_encoding))
  55. def VSProject(target, script, program):
  56. project_path = os.path.dirname(os.path.abspath(target))
  57. tree = etree.parse('template_vs2005.vcproj')
  58. root = tree.getroot()
  59. out = file(target, 'wb')
  60. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  61. ProjectFiles = []
  62. # add "*.c" files group
  63. for elem in tree.iter(tag='Filter'):
  64. if elem.attrib['Name'] == 'Source Files':
  65. #print elem.tag, elem.attrib
  66. break
  67. for group in script:
  68. group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], project_path)
  69. # add "*.h" files group
  70. for elem in tree.iter(tag='Filter'):
  71. if elem.attrib['Name'] == 'Header Files':
  72. break
  73. VS_AddHeadFilesGroup(program, elem, project_path)
  74. # write head include path
  75. if building.Env.has_key('CPPPATH'):
  76. cpp_path = building.Env['CPPPATH']
  77. paths = set()
  78. for path in cpp_path:
  79. inc = _make_path_relative(project_path, os.path.normpath(path))
  80. paths.add(inc) #.replace('\\', '/')
  81. paths = [i for i in paths]
  82. paths.sort()
  83. cpp_path = ';'.join(paths)
  84. # write include path, definitions
  85. for elem in tree.iter(tag='Tool'):
  86. if elem.attrib['Name'] == 'VCCLCompilerTool':
  87. #print elem.tag, elem.attrib
  88. break
  89. elem.set('AdditionalIncludeDirectories', cpp_path)
  90. # write cppdefinitons flags
  91. if building.Env.has_key('CPPDEFINES'):
  92. definitions = ';'.join(building.Env['CPPDEFINES'])
  93. elem.set('PreprocessorDefinitions', definitions)
  94. # write link flags
  95. # write lib dependence
  96. if building.Env.has_key('LIBS'):
  97. for elem in tree.iter(tag='Tool'):
  98. if elem.attrib['Name'] == 'VCLinkerTool':
  99. break
  100. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  101. libs = ' '.join(libs_with_extention)
  102. elem.set('AdditionalDependencies', libs)
  103. # write lib include path
  104. if building.Env.has_key('LIBPATH'):
  105. lib_path = building.Env['LIBPATH']
  106. paths = set()
  107. for path in lib_path:
  108. inc = _make_path_relative(project_path, os.path.normpath(path))
  109. paths.add(inc) #.replace('\\', '/')
  110. paths = [i for i in paths]
  111. paths.sort()
  112. lib_paths = ';'.join(paths)
  113. elem.set('AdditionalLibraryDirectories', lib_paths)
  114. xml_indent(root)
  115. out.write(etree.tostring(root, encoding='utf-8'))
  116. out.close()