xref: /nrf52832-nimble/rt-thread/tools/codeblocks.py (revision 104654410c56c573564690304ae786df310c91fc)
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
25import os
26import sys
27import string
28import building
29
30import xml.etree.ElementTree as etree
31from xml.etree.ElementTree import SubElement
32from utils import _make_path_relative
33from utils import xml_indent
34
35import utils
36
37fs_encoding = sys.getfilesystemencoding()
38
39def CB_AddHeadFiles(program, elem, project_path):
40    utils.source_ext = []
41    utils.source_ext = ["h"]
42    for item in program:
43        utils.walk_children(item)
44    utils.source_list.sort()
45    # print utils.source_list
46
47    for f in utils.source_list:
48        path = _make_path_relative(project_path, f)
49        Unit = SubElement(elem, 'Unit')
50        Unit.set('filename', path.decode(fs_encoding))
51
52def CB_AddCFiles(ProjectFiles, parent, gname, files, project_path):
53    for f in files:
54        fn = f.rfile()
55        name = fn.name
56        path = os.path.dirname(fn.abspath)
57
58        path = _make_path_relative(project_path, path)
59        path = os.path.join(path, name)
60
61        Unit = SubElement(parent, 'Unit')
62        Unit.set('filename', path.decode(fs_encoding))
63        Option = SubElement(Unit, 'Option')
64        Option.set('compilerVar', "CC")
65
66def CBProject(target, script, program):
67    project_path = os.path.dirname(os.path.abspath(target))
68
69    if os.path.isfile('template.cbp'):
70        tree = etree.parse('template.cbp')
71    else:
72        tree = etree.parse(os.path.join(os.path.dirname(__file__), 'template.cbp'))
73
74    root = tree.getroot()
75
76    out = open(target, 'w')
77    out.write('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n')
78
79    ProjectFiles = []
80
81    # SECTION 1. add "*.c|*.h" files group
82    for elem in tree.iter(tag='Project'):
83        # print elem.tag, elem.attrib
84        break
85    # add c files
86    for group in script:
87        group_xml = CB_AddCFiles(ProjectFiles, elem, group['name'], group['src'], project_path)
88    # add h files
89    CB_AddHeadFiles(program, elem, project_path)
90
91    # SECTION 2.
92    # write head include path
93    if 'CPPPATH' in building.Env:
94        cpp_path = building.Env['CPPPATH']
95        paths  = set()
96        for path in cpp_path:
97            inc = _make_path_relative(project_path, os.path.normpath(path))
98            paths.add(inc) #.replace('\\', '/')
99
100        paths = [i for i in paths]
101        paths.sort()
102        # write include path, definitions
103        for elem in tree.iter(tag='Compiler'):
104            break
105        for path in paths:
106            Add = SubElement(elem, 'Add')
107            Add.set('directory', path)
108
109        for macro in building.Env.get('CPPDEFINES', []):
110            Add = SubElement(elem, 'Add')
111            for d in macro:
112                Add.set('option', "-D"+d)
113
114        # write link flags
115    '''
116        # write lib dependence
117        if 'LIBS' in building.Env:
118            for elem in tree.iter(tag='Tool'):
119                if elem.attrib['Name'] == 'VCLinkerTool':
120                    break
121            libs_with_extention = [i+'.lib' for i in building.Env['LIBS']]
122            libs = ' '.join(libs_with_extention)
123            elem.set('AdditionalDependencies', libs)
124
125        # write lib include path
126        if 'LIBPATH' in building.Env:
127            lib_path = building.Env['LIBPATH']
128            paths  = set()
129            for path in lib_path:
130                inc = _make_path_relative(project_path, os.path.normpath(path))
131                paths.add(inc) #.replace('\\', '/')
132
133            paths = [i for i in paths]
134            paths.sort()
135            lib_paths = ';'.join(paths)
136            elem.set('AdditionalLibraryDirectories', lib_paths)
137    '''
138    xml_indent(root)
139    out.write(etree.tostring(root, encoding='utf-8'))
140    out.close()
141