xref: /nrf52832-nimble/rt-thread/tools/ses.py (revision 104654410c56c573564690304ae786df310c91fc)
1# SEGGER Embedded Studio Project Generator
2
3import os
4import sys
5
6import xml.etree.ElementTree as etree
7from xml.etree.ElementTree import SubElement
8from utils import _make_path_relative
9from utils import xml_indent
10from utils import ProjectInfo
11
12def SDKAddGroup(parent, name, files, project_path):
13    # don't add an empty group
14    if len(files) == 0:
15        return
16
17    group = SubElement(parent, 'folder', attrib={'Name': name})
18
19    for f in files:
20        fn = f.rfile()
21        name = fn.name
22        path = os.path.dirname(fn.abspath)
23
24        basename = os.path.basename(path)
25        path = _make_path_relative(project_path, path)
26        elm_attr_name = os.path.join(path, name)
27
28        file = SubElement(group, 'file', attrib={'file_name': elm_attr_name})
29
30    return group
31
32def SESProject(env) :
33    target = 'project.emProject'
34    tree = etree.parse('template.emProject')
35    # print(etree.dump(tree.getroot()))
36    # etree.dump(tree.getroot())
37
38    project = ProjectInfo(env)
39    # print(project)
40    # return
41
42    project_path = os.path.abspath(env['BSP_ROOT'])
43    script = env['project']
44
45    root = tree.getroot()
46    out = file(target, 'w')
47    out.write('<!DOCTYPE CrossStudio_Project_File>\n')
48
49    CPPPATH = []
50    CPPDEFINES = []
51    LINKFLAGS = ''
52    CCFLAGS = ''
53
54    project_node = tree.find('project')
55
56    for group in script:
57        # print(group)
58
59        group_tree = SDKAddGroup(project_node, group['name'], group['src'], project_path)
60
61        # get each group's cc flags
62        if group.has_key('CCFLAGS') and group['CCFLAGS']:
63            if CCFLAGS:
64                CCFLAGS += ' ' + group['CCFLAGS']
65            else:
66                CCFLAGS += group['CCFLAGS']
67
68        # get each group's link flags
69        if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
70            if LINKFLAGS:
71                LINKFLAGS += ' ' + group['LINKFLAGS']
72            else:
73                LINKFLAGS += group['LINKFLAGS']
74
75    # write include path, definitions and link flags
76    path = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in project['CPPPATH']])
77    path = path.replace('\\', '/')
78    defines = ';'.join(set(project['CPPDEFINES']))
79
80    node = tree.findall('project/configuration')
81    for item in node:
82        if item.get('c_preprocessor_definitions'):
83            item.set('c_preprocessor_definitions', defines)
84
85        if item.get('c_user_include_directories'):
86            item.set('c_user_include_directories', path)
87
88    xml_indent(root)
89    out.write(etree.tostring(root, encoding='utf-8'))
90    out.close()
91
92    return
93