• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4'''
5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10*     http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Description: APP utilities
19'''
20
21import os
22import json
23from scripts.common_env import insert_module
24from scripts.common_env import insert_module_dir
25from scripts.common_env import insert_lib_cfg
26from scripts.common_env import insert_env_defs
27from scripts.common_env import insert_os_include
28from scripts.common_env import insert_common_include
29from scripts.common_env import insert_ar_flags
30from scripts.common_env import insert_as_flags
31from scripts.common_env import insert_ld_flags
32from scripts.common_env import insert_cc_flags
33from scripts.common_env import set_factory_mode
34from scons_utils import colors
35
36__all__ = ["AppTarget"]
37
38class AppBuildError(Exception):
39    """
40    Build exception.
41    """
42    pass
43
44class AppTarget:
45    """
46    APP config
47    """
48    def __init__(self, app_name, factory_mode):
49        self.app_name = app_name
50        self.factory_mode = factory_mode
51        self.app_env = None
52        self.app_cfg_file = None
53        self.proj_root = os.path.realpath(os.path.join(__file__, '..', '..', '..'))
54        self.app_root = self.app_lookup()
55        self.settings = {}
56        self.app_ld_dirs = []
57
58    def app_lookup(self):
59        """
60        Found app folder.
61        """
62        dirs = os.listdir(os.path.join(self.proj_root, 'app'))
63        if self.app_name in dirs:
64            return os.path.join('app', self.app_name)
65        raise AppBuildError("%s============== Can NOT found the APP project --- %s! =============%s" %
66            (colors['red'], self.app_name, colors['end']))
67
68    def read_env(self):
69        """
70        Parse app settings
71        """
72        if self.app_pre_check() is False:
73            return;
74        try:
75            with open(self.app_cfg_file) as app_cfg:
76                self.settings = json.load(app_cfg)
77        except Exception as e:
78            raise AppBuildError("%s============== Read APP settings error: %s! =============%s" %
79                (colors['red'], e, colors['end']))
80
81        self.app_env_parse(self.settings)
82        pass
83
84    def get_app_libs(self):
85        """
86        Parse app libraries
87        """
88        libs = []
89        for folder in self.app_ld_dirs:
90            libs.extend([lib[3:-2] for lib in os.listdir(folder) if lib.startswith('lib') and lib.endswith('.a')])
91        return list(map(lambda x:'-l%s'%x, libs))
92
93    def get_app_lib_path(self):
94        """
95        Parse app library path
96        """
97        if len(self.app_ld_dirs) > 0:
98            return list(map(lambda x:'-L%s'%x, self.app_ld_dirs))
99        return []
100
101    def get_app_name(self):
102        return self.app_name
103
104    def get_app_cfg(self, key):
105        return self.settings.get(key, None)
106
107    def app_pre_check(self):
108        app_scons = os.path.join(self.proj_root, self.app_root, 'SConscript')
109        if os.path.isfile(app_scons) == False:
110            raise AppBuildError("%s============== Can NOT found the APP %s! =============%s" %
111                (colors['red'], app_scons, colors['end']))
112        self.app_cfg_file = os.path.join(self.proj_root, self.app_root, 'app.json')
113        if os.path.isfile(self.app_cfg_file) == False:
114            raise AppBuildError("%s============== Can NOT found the APP %s! =============%s" %
115                (colors['red'], self.app_cfg_file, colors['end']))
116        return True
117
118    def app_env_parse(self, settings):
119        try:
120            insert_module(self.app_name)
121            insert_module_dir(self.app_name, self.app_root)
122            insert_lib_cfg(self.app_name, {settings["TARGET_LIB"] : settings["APP_SRCS"]})
123
124            if settings.get("DEFINES") is not None:
125                insert_env_defs(self.app_name, settings["DEFINES"])
126
127            if settings.get("OS_INCLUDE") is not None:
128                insert_os_include(self.app_name, settings["OS_INCLUDE"])
129            if settings.get("INCLUDE") is not None:
130                incs = []
131                for inc in settings["INCLUDE"]:
132                    incs.append(os.path.join('#', inc));
133                insert_common_include(self.app_name, incs)
134
135            if settings.get("LD_DIRS") is not None:
136                self.app_ld_dirs.extend(settings.get("LD_DIRS"))
137            if settings.get("AR_FLAGS") is not None:
138                insert_ar_flags(self.app_name, settings["AR_FLAGS"])
139            if settings.get("LD_FLAGS") is not None:
140                insert_ld_flags(self.app_name, settings["LD_FLAGS"])
141            if settings.get("CC_FLAGS") is not None:
142                insert_cc_flags(self.app_name, settings["CC_FLAGS"])
143            if settings.get("AS_FLAGS") is not None:
144                insert_as_flags(self.app_name, settings["AS_FLAGS"])
145            if self.factory_mode == 'yes':
146                set_factory_mode()
147        except Exception as e:
148            raise AppBuildError("%s============== APP settings error: %s! =============%s" %
149                (colors['red'], e, colors['end']))
150
151"""
152for test only
153"""
154def test():
155    app_obj = AppTarget('demo')
156    app_obj.read_env()
157
158if __name__ == '__main__':
159    test()
160
161