1# 2# File : keil.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# 2017-10-16 Tanek Add CDK IDE support 23# 24 25import os 26import sys 27import string 28 29import xml.etree.ElementTree as etree 30from xml.etree.ElementTree import SubElement 31from utils import _make_path_relative 32from utils import xml_indent 33 34def SDKAddGroup(ProjectFiles, parent, name, files, project_path): 35 # don't add an empty group 36 if len(files) == 0: 37 return 38 39 group = SubElement(parent, 'VirtualDirectory', attrib={'Name': name}) 40 41 for f in files: 42 fn = f.rfile() 43 name = fn.name 44 path = os.path.dirname(fn.abspath) 45 46 basename = os.path.basename(path) 47 path = _make_path_relative(project_path, path) 48 elm_attr_name = os.path.join(path, name) 49 50 file = SubElement(group, 'File', attrib={'Name': elm_attr_name}) 51 52 return group 53 54def _CDKProject(tree, target, script): 55 56 project_path = os.path.dirname(os.path.abspath(target)) 57 58 root = tree.getroot() 59 out = open(target, 'w') 60 out.write('<?xml version="1.0" encoding="UTF-8"?>\n') 61 62 CPPPATH = [] 63 CPPDEFINES = [] 64 LINKFLAGS = '' 65 CCFLAGS = '' 66 ProjectFiles = [] 67 68 for child in root: 69 if child.tag == 'VirtualDirectory': 70 root.remove(child) 71 72 for group in script: 73 group_tree = SDKAddGroup(ProjectFiles, root, group['name'], group['src'], project_path) 74 75 # get each include path 76 if 'CPPPATH' in group and group['CPPPATH']: 77 if CPPPATH: 78 CPPPATH += group['CPPPATH'] 79 else: 80 CPPPATH += group['CPPPATH'] 81 82 # get each group's definitions 83 if 'CPPDEFINES' in group and group['CPPDEFINES']: 84 if CPPDEFINES: 85 CPPDEFINES += group['CPPDEFINES'] 86 else: 87 CPPDEFINES += group['CPPDEFINES'] 88 89 # get each group's cc flags 90 if 'CCFLAGS' in group and group['CCFLAGS']: 91 if CCFLAGS: 92 CCFLAGS += ' ' + group['CCFLAGS'] 93 else: 94 CCFLAGS += group['CCFLAGS'] 95 96 # get each group's link flags 97 if 'LINKFLAGS' in group and group['LINKFLAGS']: 98 if LINKFLAGS: 99 LINKFLAGS += ' ' + group['LINKFLAGS'] 100 else: 101 LINKFLAGS += group['LINKFLAGS'] 102 103 # todo: cdk add lib 104 105 # write include path, definitions and link flags 106 text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH]) 107 IncludePath = tree.find('BuildConfigs/BuildConfig/Compiler/IncludePath') 108 IncludePath.text = text 109 IncludePath = tree.find('BuildConfigs/BuildConfig/Asm/IncludePath') 110 IncludePath.text = text 111 112 Define = tree.find('BuildConfigs/BuildConfig/Compiler/Define') 113 Define.text = ', '.join(set(CPPDEFINES)) 114 115 CC_Misc = tree.find('BuildConfigs/BuildConfig/Compiler/OtherFlags') 116 CC_Misc.text = CCFLAGS 117 118 LK_Misc = tree.find('BuildConfigs/BuildConfig/Linker/OtherFlags') 119 LK_Misc.text = LINKFLAGS 120 121 xml_indent(root) 122 out.write(etree.tostring(root, encoding='utf-8')) 123 out.close() 124 125def CDKProject(target, script): 126 template_tree = etree.parse('template.cdkproj') 127 128 _CDKProject(template_tree, target, script) 129