vs.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os
  2. import sys
  3. import string
  4. import building
  5. import xml.etree.ElementTree as etree
  6. from xml.etree.ElementTree import SubElement
  7. from utils import _make_path_relative
  8. from utils import xml_indent
  9. fs_encoding = sys.getfilesystemencoding()
  10. def VS_AddGroup(ProjectFiles, parent, name, files, project_path):
  11. Filter = SubElement(parent, 'Filter')
  12. Filter.set('Name', name) #set group name to group
  13. for f in files:
  14. fn = f.rfile()
  15. name = fn.name
  16. path = os.path.dirname(fn.abspath)
  17. path = _make_path_relative(project_path, path)
  18. path = os.path.join(path, name)
  19. File = SubElement(Filter, 'File')
  20. File.set('RelativePath', path.decode(fs_encoding))
  21. def VS_AddHeadFilesGroup(program, elem, project_path):
  22. building.source_ext = []
  23. building.source_ext = ["h"]
  24. for item in program:
  25. building.walk_children(item)
  26. building.source_list.sort()
  27. # print building.source_list
  28. for f in building.source_list:
  29. path = _make_path_relative(project_path, f)
  30. File = SubElement(elem, 'File')
  31. File.set('RelativePath', path.decode(fs_encoding))
  32. def VSProject(target, script, program):
  33. project_path = os.path.dirname(os.path.abspath(target))
  34. tree = etree.parse('template_vs2005.vcproj')
  35. root = tree.getroot()
  36. out = file(target, 'wb')
  37. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  38. ProjectFiles = []
  39. # add "*.c" files group
  40. for elem in tree.iter(tag='Filter'):
  41. if elem.attrib['Name'] == 'Source Files':
  42. #print elem.tag, elem.attrib
  43. break
  44. for group in script:
  45. group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], project_path)
  46. # add "*.h" files group
  47. for elem in tree.iter(tag='Filter'):
  48. if elem.attrib['Name'] == 'Header Files':
  49. break
  50. VS_AddHeadFilesGroup(program, elem, project_path)
  51. # write head include path
  52. if building.Env.has_key('CPPPATH'):
  53. cpp_path = building.Env['CPPPATH']
  54. paths = set()
  55. for path in cpp_path:
  56. inc = _make_path_relative(project_path, os.path.normpath(path))
  57. paths.add(inc) #.replace('\\', '/')
  58. paths = [i for i in paths]
  59. paths.sort()
  60. cpp_path = ';'.join(paths)
  61. # write include path, definitions
  62. for elem in tree.iter(tag='Tool'):
  63. if elem.attrib['Name'] == 'VCCLCompilerTool':
  64. #print elem.tag, elem.attrib
  65. break
  66. elem.set('AdditionalIncludeDirectories', cpp_path)
  67. # write cppdefinitons flags
  68. if building.Env.has_key('CPPDEFINES'):
  69. definitions = ';'.join(building.Env['CPPDEFINES'])
  70. elem.set('PreprocessorDefinitions', definitions)
  71. # write link flags
  72. # write lib dependence
  73. if building.Env.has_key('LIBS'):
  74. for elem in tree.iter(tag='Tool'):
  75. if elem.attrib['Name'] == 'VCLinkerTool':
  76. break
  77. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  78. libs = ' '.join(libs_with_extention)
  79. elem.set('AdditionalDependencies', libs)
  80. # write lib include path
  81. if building.Env.has_key('LIBPATH'):
  82. lib_path = building.Env['LIBPATH']
  83. paths = set()
  84. for path in lib_path:
  85. inc = _make_path_relative(project_path, os.path.normpath(path))
  86. paths.add(inc) #.replace('\\', '/')
  87. paths = [i for i in paths]
  88. paths.sort()
  89. lib_paths = ';'.join(paths)
  90. elem.set('AdditionalLibraryDirectories', lib_paths)
  91. xml_indent(root)
  92. out.write(etree.tostring(root, encoding='utf-8'))
  93. out.close()