codeblocks.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 CB_AddHeadFiles(program, elem, project_path):
  11. building.source_ext = []
  12. building.source_ext = ["h"]
  13. for item in program:
  14. building.walk_children(item)
  15. building.source_list.sort()
  16. # print building.source_list
  17. for f in building.source_list:
  18. path = _make_path_relative(project_path, f)
  19. Unit = SubElement(elem, 'Unit')
  20. Unit.set('filename', path.decode(fs_encoding))
  21. def CB_AddCFiles(ProjectFiles, parent, gname, files, project_path):
  22. for f in files:
  23. fn = f.rfile()
  24. name = fn.name
  25. path = os.path.dirname(fn.abspath)
  26. path = _make_path_relative(project_path, path)
  27. path = os.path.join(path, name)
  28. Unit = SubElement(parent, 'Unit')
  29. Unit.set('filename', path.decode(fs_encoding))
  30. Option = SubElement(Unit, 'Option')
  31. Option.set('compilerVar', "CC")
  32. def CBProject(target, script, program):
  33. project_path = os.path.dirname(os.path.abspath(target))
  34. tree = etree.parse('template.cbp')
  35. root = tree.getroot()
  36. out = file(target, 'wb')
  37. out.write('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n')
  38. ProjectFiles = []
  39. # SECTION 1. add "*.c|*.h" files group
  40. for elem in tree.iter(tag='Project'):
  41. # print elem.tag, elem.attrib
  42. break
  43. # add c files
  44. for group in script:
  45. group_xml = CB_AddCFiles(ProjectFiles, elem, group['name'], group['src'], project_path)
  46. # add h files
  47. CB_AddHeadFiles(program, elem, project_path)
  48. # SECTION 2.
  49. # write head include path
  50. if building.Env.has_key('CPPPATH'):
  51. cpp_path = building.Env['CPPPATH']
  52. paths = set()
  53. for path in cpp_path:
  54. inc = _make_path_relative(project_path, os.path.normpath(path))
  55. paths.add(inc) #.replace('\\', '/')
  56. paths = [i for i in paths]
  57. paths.sort()
  58. # write include path, definitions
  59. for elem in tree.iter(tag='Compiler'):
  60. break
  61. for path in paths:
  62. Add = SubElement(elem, 'Add')
  63. Add.set('directory', path)
  64. for macro in building.Env.get('CPPDEFINES', []):
  65. Add = SubElement(elem, 'Add')
  66. Add.set('option', "-D"+macro)
  67. # write link flags
  68. '''
  69. # write lib dependence
  70. if building.Env.has_key('LIBS'):
  71. for elem in tree.iter(tag='Tool'):
  72. if elem.attrib['Name'] == 'VCLinkerTool':
  73. break
  74. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  75. libs = ' '.join(libs_with_extention)
  76. elem.set('AdditionalDependencies', libs)
  77. # write lib include path
  78. if building.Env.has_key('LIBPATH'):
  79. lib_path = building.Env['LIBPATH']
  80. paths = set()
  81. for path in lib_path:
  82. inc = _make_path_relative(project_path, os.path.normpath(path))
  83. paths.add(inc) #.replace('\\', '/')
  84. paths = [i for i in paths]
  85. paths.sort()
  86. lib_paths = ';'.join(paths)
  87. elem.set('AdditionalLibraryDirectories', lib_paths)
  88. '''
  89. xml_indent(root)
  90. out.write(etree.tostring(root, encoding='utf-8'))
  91. out.close()