codeblocks.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if os.path.isfile('template.cbp'):
  35. tree = etree.parse('template.cbp')
  36. else:
  37. tree = etree.parse(os.path.join(os.path.dirname(__file__), 'template.cbp'))
  38. root = tree.getroot()
  39. out = file(target, 'wb')
  40. out.write('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n')
  41. ProjectFiles = []
  42. # SECTION 1. add "*.c|*.h" files group
  43. for elem in tree.iter(tag='Project'):
  44. # print elem.tag, elem.attrib
  45. break
  46. # add c files
  47. for group in script:
  48. group_xml = CB_AddCFiles(ProjectFiles, elem, group['name'], group['src'], project_path)
  49. # add h files
  50. CB_AddHeadFiles(program, elem, project_path)
  51. # SECTION 2.
  52. # write head include path
  53. if building.Env.has_key('CPPPATH'):
  54. cpp_path = building.Env['CPPPATH']
  55. paths = set()
  56. for path in cpp_path:
  57. inc = _make_path_relative(project_path, os.path.normpath(path))
  58. paths.add(inc) #.replace('\\', '/')
  59. paths = [i for i in paths]
  60. paths.sort()
  61. # write include path, definitions
  62. for elem in tree.iter(tag='Compiler'):
  63. break
  64. for path in paths:
  65. Add = SubElement(elem, 'Add')
  66. Add.set('directory', path)
  67. for macro in building.Env.get('CPPDEFINES', []):
  68. Add = SubElement(elem, 'Add')
  69. Add.set('option', "-D"+macro)
  70. # write link flags
  71. '''
  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. '''
  92. xml_indent(root)
  93. out.write(etree.tostring(root, encoding='utf-8'))
  94. out.close()