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    LIBS = []
67    ProjectFiles = []
68
69    for child in root:
70        if child.tag == 'VirtualDirectory':
71            root.remove(child)
72
73    for group in script:
74        group_tree = SDKAddGroup(ProjectFiles, root, group['name'], group['src'], project_path)
75
76        # get each include path
77        if 'CPPPATH' in group and group['CPPPATH']:
78            if CPPPATH:
79                CPPPATH += group['CPPPATH']
80            else:
81                CPPPATH += group['CPPPATH']
82
83        # get each group's definitions
84        if 'CPPDEFINES' in group and group['CPPDEFINES']:
85            if CPPDEFINES:
86                CPPDEFINES += group['CPPDEFINES']
87            else:
88                CPPDEFINES += group['CPPDEFINES']
89
90        # get each group's cc flags
91        if 'CCFLAGS' in group and group['CCFLAGS']:
92            if CCFLAGS:
93                CCFLAGS += ' ' + group['CCFLAGS']
94            else:
95                CCFLAGS += group['CCFLAGS']
96
97        # get each group's link flags
98        if 'LINKFLAGS' in group and group['LINKFLAGS']:
99            if LINKFLAGS:
100                LINKFLAGS += ' ' + group['LINKFLAGS']
101            else:
102                LINKFLAGS += group['LINKFLAGS']
103
104        # todo: cdk add lib
105        if 'LIBS' in group and group['LIBS']:
106            LIBS += group['LIBS']
107
108    # write include path, definitions and link flags
109    text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
110    IncludePath = tree.find('BuildConfigs/BuildConfig/Compiler/IncludePath')
111    IncludePath.text = text
112    IncludePath = tree.find('BuildConfigs/BuildConfig/Asm/IncludePath')
113    IncludePath.text = text
114
115    Define = tree.find('BuildConfigs/BuildConfig/Compiler/Define')
116    Define.text = '; '.join(set(CPPDEFINES))
117
118    Define = tree.find('BuildConfigs/BuildConfig/Asm/Define')
119    Define.text = '; '.join(set(CPPDEFINES))
120
121    CC_Misc = tree.find('BuildConfigs/BuildConfig/Compiler/OtherFlags')
122    CC_Misc.text = CCFLAGS
123
124    LK_Misc = tree.find('BuildConfigs/BuildConfig/Linker/OtherFlags')
125    LK_Misc.text = LINKFLAGS
126
127    LibName = tree.find('BuildConfigs/BuildConfig/Linker/LibName')
128    if LibName.text:
129        LibName.text=LibName.text+';'+';'.join(LIBS)
130    else:
131        LibName.text=';'.join(LIBS)
132
133    xml_indent(root)
134    out.write(etree.tostring(root, encoding='utf-8').decode('utf-8'))
135    out.close()
136
137def CDKProject(target, script):
138    template_tree = etree.parse('template.cdkproj')
139
140    _CDKProject(template_tree, target, script)
141