vs.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ProjectFiles = []
  28. # add group
  29. for elem in tree.iter(tag='Filter'):
  30. if elem.attrib['Name'] == 'Source Files':
  31. #print elem.tag, elem.attrib
  32. break
  33. for group in script:
  34. group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], project_path)
  35. # write head include path
  36. if building.Env.has_key('CPPPATH'):
  37. cpp_path = building.Env['CPPPATH']
  38. paths = set()
  39. for path in cpp_path:
  40. inc = _make_path_relative(project_path, os.path.normpath(path))
  41. paths.add(inc) #.replace('\\', '/')
  42. paths = [i for i in paths]
  43. paths.sort()
  44. cpp_path = ';'.join(paths)
  45. # write include path, definitions
  46. for elem in tree.iter(tag='Tool'):
  47. if elem.attrib['Name'] == 'VCCLCompilerTool':
  48. #print elem.tag, elem.attrib
  49. break
  50. elem.set('AdditionalIncludeDirectories', cpp_path)
  51. # write cppdefinitons flags
  52. if building.Env.has_key('CPPDEFINES'):
  53. definitions = ';'.join(building.Env['CPPDEFINES'])
  54. elem.set('PreprocessorDefinitions', definitions)
  55. # write link flags
  56. # write lib dependence
  57. if building.Env.has_key('LIBS'):
  58. for elem in tree.iter(tag='Tool'):
  59. if elem.attrib['Name'] == 'VCLinkerTool':
  60. break
  61. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  62. libs = ' '.join(libs_with_extention)
  63. elem.set('AdditionalDependencies', libs)
  64. # write lib include path
  65. if building.Env.has_key('LIBPATH'):
  66. lib_path = building.Env['LIBPATH']
  67. paths = set()
  68. for path in lib_path:
  69. inc = _make_path_relative(project_path, os.path.normpath(path))
  70. paths.add(inc) #.replace('\\', '/')
  71. paths = [i for i in paths]
  72. paths.sort()
  73. lib_paths = ';'.join(paths)
  74. elem.set('AdditionalLibraryDirectories', lib_paths)
  75. xml_indent(root)
  76. out.write(etree.tostring(root, encoding='utf-8'))
  77. out.close()