codeblocks.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #
  2. # File : codeblocks.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import os
  25. import sys
  26. import string
  27. import building
  28. import xml.etree.ElementTree as etree
  29. from xml.etree.ElementTree import SubElement
  30. from utils import _make_path_relative
  31. from utils import xml_indent
  32. fs_encoding = sys.getfilesystemencoding()
  33. def CB_AddHeadFiles(program, elem, project_path):
  34. building.source_ext = []
  35. building.source_ext = ["h"]
  36. for item in program:
  37. building.walk_children(item)
  38. building.source_list.sort()
  39. # print building.source_list
  40. for f in building.source_list:
  41. path = _make_path_relative(project_path, f)
  42. Unit = SubElement(elem, 'Unit')
  43. Unit.set('filename', path.decode(fs_encoding))
  44. def CB_AddCFiles(ProjectFiles, parent, gname, files, project_path):
  45. for f in files:
  46. fn = f.rfile()
  47. name = fn.name
  48. path = os.path.dirname(fn.abspath)
  49. path = _make_path_relative(project_path, path)
  50. path = os.path.join(path, name)
  51. Unit = SubElement(parent, 'Unit')
  52. Unit.set('filename', path.decode(fs_encoding))
  53. Option = SubElement(Unit, 'Option')
  54. Option.set('compilerVar', "CC")
  55. def CBProject(target, script, program):
  56. project_path = os.path.dirname(os.path.abspath(target))
  57. if os.path.isfile('template.cbp'):
  58. tree = etree.parse('template.cbp')
  59. else:
  60. tree = etree.parse(os.path.join(os.path.dirname(__file__), 'template.cbp'))
  61. root = tree.getroot()
  62. out = file(target, 'wb')
  63. out.write('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n')
  64. ProjectFiles = []
  65. # SECTION 1. add "*.c|*.h" files group
  66. for elem in tree.iter(tag='Project'):
  67. # print elem.tag, elem.attrib
  68. break
  69. # add c files
  70. for group in script:
  71. group_xml = CB_AddCFiles(ProjectFiles, elem, group['name'], group['src'], project_path)
  72. # add h files
  73. CB_AddHeadFiles(program, elem, project_path)
  74. # SECTION 2.
  75. # write head include path
  76. if building.Env.has_key('CPPPATH'):
  77. cpp_path = building.Env['CPPPATH']
  78. paths = set()
  79. for path in cpp_path:
  80. inc = _make_path_relative(project_path, os.path.normpath(path))
  81. paths.add(inc) #.replace('\\', '/')
  82. paths = [i for i in paths]
  83. paths.sort()
  84. # write include path, definitions
  85. for elem in tree.iter(tag='Compiler'):
  86. break
  87. for path in paths:
  88. Add = SubElement(elem, 'Add')
  89. Add.set('directory', path)
  90. for macro in building.Env.get('CPPDEFINES', []):
  91. Add = SubElement(elem, 'Add')
  92. Add.set('option', "-D"+macro)
  93. # write link flags
  94. '''
  95. # write lib dependence
  96. if building.Env.has_key('LIBS'):
  97. for elem in tree.iter(tag='Tool'):
  98. if elem.attrib['Name'] == 'VCLinkerTool':
  99. break
  100. libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
  101. libs = ' '.join(libs_with_extention)
  102. elem.set('AdditionalDependencies', libs)
  103. # write lib include path
  104. if building.Env.has_key('LIBPATH'):
  105. lib_path = building.Env['LIBPATH']
  106. paths = set()
  107. for path in lib_path:
  108. inc = _make_path_relative(project_path, os.path.normpath(path))
  109. paths.add(inc) #.replace('\\', '/')
  110. paths = [i for i in paths]
  111. paths.sort()
  112. lib_paths = ';'.join(paths)
  113. elem.set('AdditionalLibraryDirectories', lib_paths)
  114. '''
  115. xml_indent(root)
  116. out.write(etree.tostring(root, encoding='utf-8'))
  117. out.close()