1"""
2Utils for CMake
3Author: https://github.com/klivelinux
4"""
5
6import os
7import utils
8from string import Template
9import rtconfig
10
11from utils import _make_path_relative
12
13
14class XmakeProject:
15    def __init__(self, env, project):
16        self.env = env
17        self.project = project
18        self.sdkdir = ""
19        self.bindir = ""
20        self.toolchain = ""
21        self.src_path = ""
22        self.inc_path = ""
23        self.cflags = ""
24        self.cxxflags = ""
25        self.ldflags = ""
26        self.asflags = ""
27        self.define = ""
28
29    def set_toolchain_path(self):
30        self.bindir = os.path.abspath(rtconfig.EXEC_PATH).replace('\\', "/")
31        self.sdkdir = self.bindir[:-4]
32        # delete -
33        self.toolchain = rtconfig.PREFIX[:-1]
34
35    def set_target_config(self):
36        info = utils.ProjectInfo(self.env)
37        # 1. config src path
38        for group in self.project:
39            for f in group['src']:
40                # use relative path
41                path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath))
42                self.src_path += "\t\"{0}\",\n".format(path.replace("\\", "/"))
43        self.src_path = self.src_path[:-2]
44        # 2. config dir path
45        for i in info['CPPPATH']:
46            # use relative path
47            path = _make_path_relative(os.getcwd(), i)
48            self.inc_path += "\t\"{0}\",\n".format(path.replace("\\", "/"))
49        self.inc_path = self.inc_path[:-2]
50        # 3. config cflags
51        self.cflags = rtconfig.CFLAGS.replace('\\', "/").replace('\"', "\\\"")
52        # 4. config cxxflags
53        if 'CXXFLAGS' in dir(rtconfig):
54            self.cxxflags = rtconfig.CXXFLAGS.replace('\\', "/").replace('\"', "\\\"")
55        else:
56            self.cxxflags = self.cflags
57        # 5. config asflags
58        self.asflags = rtconfig.AFLAGS.replace('\\', "/").replace('\"', "\\\"")
59        # 6. config lflags
60        self.ldflags = rtconfig.LFLAGS.replace('\\', "/").replace('\"', "\\\"")
61        # 7. config define
62        for i in info['CPPDEFINES']:
63            self.define += "\t\"{0}\",\n".format(i)
64        self.define = self.define[:-2]
65
66    def generate_xmake_file(self):
67        if os.getenv('RTT_ROOT'):
68            RTT_ROOT = os.getenv('RTT_ROOT')
69        else:
70            RTT_ROOT = os.path.normpath(os.getcwd() + '/../../..')
71
72        template_path = os.path.join(RTT_ROOT, "tools", "targets", "xmake.lua")
73        with open(template_path, "r") as f:
74            data = f.read()
75        data = Template(data)
76        data = data.safe_substitute(toolchain=self.toolchain, sdkdir=self.sdkdir, bindir=self.bindir, src_path=self.src_path, inc_path=self.inc_path,
77                                    define=self.define, cflags=self.cflags, cxxflags=self.cxxflags, asflags=self.asflags,
78                                    ldflags=self.ldflags, target="rt-thread")
79        with open(os.path.join(os.path.dirname(__file__), "xmake.lua"), "w") as f:
80            f.write(data)
81
82
83def XMakeProject(env,project):
84    print('Update setting files for xmake.lua...')
85
86    xmake_project = XmakeProject(env, project)
87    xmake_project.set_toolchain_path()
88    xmake_project.set_target_config()
89    xmake_project.generate_xmake_file()
90
91    print('Done!')
92
93    return
94