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