codelite.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #
  2. # File : codelite.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2020, 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. # 2020-10-14 LiuMin Add copyright information
  23. #
  24. import os
  25. import sys
  26. import string
  27. import uuid
  28. import utils
  29. from xml.etree.ElementTree import SubElement
  30. from utils import _make_path_relative
  31. from utils import xml_indent
  32. # Add parent directory to path to import building
  33. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  34. import building
  35. import xml.etree.ElementTree as etree
  36. fs_encoding = sys.getfilesystemencoding()
  37. def CLSetCFlags(root, flags):
  38. node = root.find('Settings').find('Configuration').find('Compiler')
  39. node.attrib['C_Options'] = flags
  40. def CLSetCxxFlags(root, flags):
  41. node = root.find('Settings').find('Configuration').find('Compiler')
  42. node.attrib['Options'] = flags
  43. def CLSetAsFlags(root, flags):
  44. node = root.find('Settings').find('Configuration').find('Compiler')
  45. node.attrib['Assembler'] = flags
  46. def CLAddIncludePath(root, path):
  47. node = root.find('Settings').find('Configuration').find('Compiler')
  48. node = SubElement(node, 'IncludePath')
  49. node.attrib['Value'] = path
  50. def CLAddPreprocessor(root, value):
  51. node = root.find('Settings').find('Configuration').find('Compiler')
  52. node = SubElement(node, 'Preprocessor')
  53. node.attrib['Value'] = value
  54. def CLSetLdFlags(root, flags):
  55. node = root.find('Settings').find('Configuration').find('Linker')
  56. node.attrib['Options'] = flags
  57. def CLAddLibrary_path(root, path):
  58. node = root.find('Settings').find('Configuration').find('Linker')
  59. node = SubElement(node, 'LibraryPath')
  60. node.attrib['Value'] = path
  61. def CLAddLibrary(root, lib):
  62. node = root.find('Settings').find('Configuration').find('Linker')
  63. node = SubElement(node, 'Library')
  64. node.attrib['Value'] = lib
  65. def CLAddFile(root, file_path):
  66. file_path = file_path.replace('\\', '/')
  67. dir_list = file_path.split('/')
  68. dir_list.pop()
  69. if not len(dir_list):
  70. dir_list.append(os.path.abspath('.').replace('\\', '/').split('/')[-1])
  71. parent = root
  72. for dir_name in dir_list:
  73. if dir_name == '..':
  74. continue
  75. node = None
  76. nodes = parent.findall('VirtualDirectory')
  77. for iter in nodes:
  78. if iter.attrib['Name'] == dir_name:
  79. node = iter
  80. break
  81. if node is None:
  82. node = SubElement(parent, 'VirtualDirectory')
  83. node.attrib['Name'] = dir_name
  84. parent = node
  85. if parent != root:
  86. node = SubElement(parent, 'File')
  87. node.attrib['Name'] = file_path
  88. def CLAddHeaderFiles(parent, program, project_path):
  89. utils.source_ext = []
  90. utils.source_ext = ["h"]
  91. for item in program:
  92. utils.walk_children(item)
  93. utils.source_list.sort()
  94. for f in utils.source_list:
  95. path = _make_path_relative(project_path, f)
  96. CLAddFile(parent, path)
  97. def CLAddCFiles(parent, files, project_path):
  98. for f in files:
  99. fn = f.rfile()
  100. name = fn.name
  101. path = os.path.dirname(fn.abspath)
  102. path = _make_path_relative(project_path, path)
  103. path = os.path.join(path, name)
  104. CLAddFile(parent, path)
  105. def CLGenWorkspace(project_name, project_path):
  106. if os.path.isfile('codelite_template.workspace'):
  107. tree = etree.parse('codelite_template.workspace')
  108. else:
  109. tree = etree.parse(os.path.join(os.path.dirname(__file__), 'codelite_template.workspace'))
  110. root = tree.getroot()
  111. root.attrib['Name'] = project_name
  112. node = root.find('Project')
  113. node.attrib['Name'] = project_name
  114. node.attrib['Path'] = project_name + '.project'
  115. node = root.find('BuildMatrix').find('WorkspaceConfiguration').find('Project')
  116. node.attrib['Name'] = project_name
  117. out = open(project_name + '.workspace', 'w')
  118. out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  119. xml_indent(root)
  120. out.write(etree.tostring(root, encoding='utf-8'))
  121. out.close()
  122. def TargetCodelite(script, program):
  123. project_name = os.path.abspath('.').replace('\\', '/').split('/')[-1]
  124. #project_name.replace('-', '_')
  125. project_path = os.path.abspath('.')
  126. CLGenWorkspace(project_name, project_path)
  127. if os.path.isfile('codelite_template.project'):
  128. tree = etree.parse('codelite_template.project')
  129. else:
  130. tree = etree.parse(os.path.join(os.path.dirname(__file__), 'codelite_template.project'))
  131. root = tree.getroot()
  132. root.attrib['Name'] = project_name
  133. out = open(project_name + '.project', 'w')
  134. out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  135. # add files
  136. for group in script:
  137. CLAddCFiles(root, group['src'], project_path)
  138. # add header file
  139. CLAddHeaderFiles(root, program, project_path)
  140. # SECTION 2.
  141. # write head include path
  142. if 'CPPPATH' in building.Env:
  143. cpp_path = building.Env['CPPPATH']
  144. paths = set()
  145. for path in cpp_path:
  146. inc = _make_path_relative(project_path, os.path.normpath(path))
  147. paths.add(inc) #.replace('\\', '/')
  148. paths = [i for i in paths]
  149. paths.sort()
  150. # write include path, definitions
  151. for elem in tree.iter(tag='Compiler'):
  152. break
  153. for path in paths:
  154. CLAddIncludePath(root, path)
  155. #print building.Env.get('LIBPATH', [])
  156. #print building.Env.get('LIBS', [])
  157. CLSetCFlags(root, building.Env.get('CFLAGS', []))
  158. CLSetCxxFlags(root, building.Env.get('CFLAGS', []))
  159. asflags = building.Env.get('ASFLAGS', [])
  160. asflags = asflags.replace('-ffunction-sections', '')
  161. asflags = asflags.replace('-fdata-sections', '')
  162. asflags = asflags.replace('-x', '')
  163. asflags = asflags.replace('-Wa,', '')
  164. asflags = asflags.replace('assembler-with-cpp', '')
  165. CLSetAsFlags(root, asflags)
  166. CLSetLdFlags(root, building.Env.get('LINKFLAGS', []))
  167. for macro in building.Env.get('CPPDEFINES', []):
  168. for d in macro:
  169. CLAddPreprocessor(root, d)
  170. xml_indent(root)
  171. out.write(etree.tostring(root, encoding='utf-8'))
  172. out.close()