vs.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 VSProject(target, script):
  22. project_path = os.path.dirname(os.path.abspath(target))
  23. tree = etree.parse('template.vcproj')
  24. root = tree.getroot()
  25. out = file(target, 'wb')
  26. out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
  27. CPPPATH = []
  28. CPPDEFINES = []
  29. LINKFLAGS = ''
  30. CCFLAGS = ''
  31. ProjectFiles = []
  32. # add group
  33. for elem in tree.iter(tag='Filter'):
  34. if elem.attrib['Name'] == 'Source Files':
  35. #print elem.tag, elem.attrib
  36. break
  37. for group in script:
  38. group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], project_path)
  39. # get each include path
  40. if group.has_key('CPPPATH') and group['CPPPATH']:
  41. if CPPPATH:
  42. CPPPATH += group['CPPPATH']
  43. else:
  44. CPPPATH += group['CPPPATH']
  45. # get each group's definitions
  46. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  47. if CPPDEFINES:
  48. CPPDEFINES += group['CPPDEFINES']
  49. else:
  50. CPPDEFINES += group['CPPDEFINES']
  51. # get each group's link flags
  52. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  53. if LINKFLAGS:
  54. LINKFLAGS += ' ' + group['LINKFLAGS']
  55. else:
  56. LINKFLAGS += group['LINKFLAGS']
  57. # remove repeat path
  58. paths = set()
  59. for path in CPPPATH:
  60. inc = _make_path_relative(project_path, os.path.normpath(path))
  61. paths.add(inc) #.replace('\\', '/')
  62. paths = [i for i in paths]
  63. paths.sort()
  64. CPPPATH = string.join(paths, ';')
  65. # write include path, definitions
  66. for elem in tree.iter(tag='Tool'):
  67. if elem.attrib['Name'] == 'VCCLCompilerTool':
  68. #print elem.tag, elem.attrib
  69. break
  70. elem.set('AdditionalIncludeDirectories', CPPPATH)
  71. definitions = ';'.join(building.Env['CPPDEFINES'])
  72. elem.set('PreprocessorDefinitions', definitions)
  73. # write link flags
  74. # write lib dependence
  75. for elem in tree.iter(tag='Tool'):
  76. if elem.attrib['Name'] == 'VCLinkerTool':
  77. break
  78. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  79. libs = ' '.join(libs_with_extention)
  80. elem.set('AdditionalDependencies', libs)
  81. if building.Env.has_key('LIBPATH'):
  82. #if building.Env['LIBPATH']:
  83. lib_path = building.Env['LIBPATH']
  84. paths = set()
  85. for path in lib_path:
  86. inc = _make_path_relative(project_path, os.path.normpath(path))
  87. paths.add(inc) #.replace('\\', '/')
  88. paths = [i for i in paths]
  89. paths.sort()
  90. lib_paths = ';'.join(paths)
  91. elem.set('AdditionalLibraryDirectories', lib_paths)
  92. xml_indent(root)
  93. out.write(etree.tostring(root, encoding='utf-8'))
  94. out.close()