• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import os
18
19from resources.config import Config
20from util.log_util import LogUtil
21from util.io_util import IoUtil
22from util.preloader.parse_vendor_product_config import get_vendor_parts_list
23
24
25class Outputs:
26
27    def __init__(self, output_dir):
28        self.__post_init__(output_dir)
29
30    def __post_init__(self, output_dir):
31        os.makedirs(output_dir, exist_ok=True)
32        self.build_prop = os.path.join(output_dir, 'build.prop')
33        self.build_config_json = os.path.join(output_dir, 'build_config.json')
34        self.parts_json = os.path.join(output_dir, 'parts.json')
35        self.parts_config_json = os.path.join(output_dir, 'parts_config.json')
36        self.build_gnargs_prop = os.path.join(output_dir, 'build_gnargs.prop')
37        self.features_json = os.path.join(output_dir, 'features.json')
38        self.syscap_json = os.path.join(output_dir, 'syscap.json')
39        self.exclusion_modules_json = os.path.join(output_dir,
40                                                   'exclusion_modules.json')
41        self.subsystem_config_json = os.path.join(output_dir,
42                                                  'subsystem_config.json')
43        self.subsystem_config_overlay_json = os.path.join(output_dir,
44                                                  'subsystem_config_overlay.json')
45        self.platforms_build = os.path.join(output_dir, 'platforms.build')
46        self.systemcapability_json = os.path.join(
47            output_dir, 'SystemCapability.json')
48        self.compile_standard_whitelist_json = os.path.join(output_dir, 'compile_standard_whitelist.json')
49
50class Dirs:
51
52    def __init__(self, config):
53        self.__post_init__(config)
54
55    def __post_init__(self, config):
56        self.source_root_dir = config.root_path
57        self.built_in_product_dir = config.built_in_product_path
58        self.productdefine_dir = os.path.join(
59            self.source_root_dir, 'productdefine/common')
60        self.built_in_base_dir = os.path.join(self.productdefine_dir, 'base')
61
62        # Configs of vendor specified products are stored in ${vendor_dir} directory.
63        self.vendor_dir = config.vendor_path
64        # Configs of device specified products are stored in ${device_dir} directory.
65        self.device_dir = os.path.join(config.root_path, 'device')
66
67        self.subsystem_config_json = os.path.join(
68            config.root_path, config.subsystem_config_json)
69        self.subsystem_config_overlay_json = os.path.join(config.product_path,
70                                                          'subsystem_config_overlay.json')
71        self.lite_components_dir = os.path.join(
72            config.root_path, 'build/lite/components')
73
74        self.preloader_output_dir = os.path.join(
75            config.root_path, 'out/preloader', config.product)
76
77
78class Product():
79
80    def __init__(self, config_dirs: Dirs, ohos_config: Config):
81        self._ohos_config = None
82        self._dirs = None
83        self._name = ""
84        self._config = {}
85        self._build_vars = {}
86        self._parts = {}
87        self._syscap_info = {}
88        self._device_name = ""
89        self._device_info = {}
90        self._config_file = ""
91        self._version = ''
92        self.__post_init__(config_dirs, ohos_config)
93
94    def __post_init__(self, config_dirs: Dirs, config: Config):
95        self._ohos_config = config
96        self._dirs = config_dirs
97        self._name = config.product
98        self._config_file = config.product_json
99        self._config = self._get_full_product_config()
100        self._version = self._config.get('version', '3.0')
101        self._do_parse()
102
103    # parse product configuration, then generate parts list and build vars
104    def _do_parse(self):
105        self._update_syscap_info()
106        self._update_device()
107        self._update_parts()
108        self._update_build_vars()
109        self._remove_excluded_components()
110
111# update and remove
112
113    # Update the syscap info
114    def _update_syscap_info(self):
115        product_name = self._config.get('product_name')
116        if product_name is None:
117            product_name = ""
118        os_level = self._config.get('type')
119        if os_level is None:
120            os_level = ""
121        api_version = self._config.get('api_version')
122        if api_version is None:
123            api_version = 0
124        manufacturer_id = self._config.get('manufacturer_id')
125        if manufacturer_id is None:
126            manufacturer_id = 0
127        self._syscap_info = {'product': product_name, 'api_version': api_version,
128                             'system_type': os_level, 'manufacturer_id': manufacturer_id}
129
130    # Update the _device_name and _device_info based on the product configuration in the vendor warehouse
131    def _update_device(self):
132        if self._version == "2.0":
133            device_name = self._config.get('product_device')
134            if device_name:
135                self._device_name = device_name
136                self._device_info = self._get_device_info_v2(
137                    device_name, self._dirs.built_in_device_dir)
138        else:
139            device_name = self._config.get('board')
140            if device_name:
141                self._device_name = device_name
142                self._device_info = self._get_device_info_v3(self._config)
143        if self._ohos_config.target_cpu:
144            self._device_info["target_cpu"] = self._ohos_config.target_cpu
145        if self._ohos_config.target_os:
146            self._device_info["target_os"] = self._ohos_config.target_os
147        if self._ohos_config.compile_config:
148            self._device_info[self._ohos_config["compile_config"]] = True
149
150    # Update the _parts based on the product configuration in the vendor warehouse
151    def _update_parts(self):
152        if self._version == "1.0":
153            _parts = {}
154            self._parts = _parts
155        else:
156            # 1. inherit parts information from base config
157            if self._version == "2.0":
158                os_level = self._config.get("type", "standard")
159            else:
160                os_level = self._config.get("type", "mini")
161            # 2. product config based on default minimum system
162            based_on_mininum_system = self._config.get(
163                'based_on_mininum_system')
164            if based_on_mininum_system == "true":
165                self._parts = self._get_base_parts(
166                    self._dirs.built_in_base_dir, os_level)
167            # 3. inherit parts information from inherit config
168            inherit = self._config.get('inherit')
169            if inherit:
170                self._parts.update(
171                    self._get_inherit_parts(inherit, self._dirs.source_root_dir))
172
173            # 4. chipset products relate system parts config
174            sys_info_path = self._config.get('system_component')
175            if sys_info_path:
176                sys_parts = self._get_sys_relate_parts(
177                    sys_info_path, self._parts, self._dirs.source_root_dir)
178                self._parts.update(sys_parts)
179            all_parts = {}
180            if self._version == "2.0":
181                current_product_parts = self._config.get("parts")
182                if current_product_parts:
183                    all_parts.update(current_product_parts)
184            else:
185                all_parts.update(get_vendor_parts_list(self._config))
186                all_parts.update(self._get_product_specific_parts())
187
188                device_name = self._config.get('board')
189                if device_name:
190                    all_parts.update(self._get_device_specific_parts())
191            self._parts.update(all_parts)
192
193    # Update the _build_vars based on the product configuration in the vendor warehouse
194    def _update_build_vars(self):
195        config = self._config
196        build_vars = {}
197        if self._version == "1.0":
198            build_vars = {"os_level": 'large'}
199        else:
200            if self._version == "2.0":
201                build_vars['os_level'] = config.get("type", "standard")
202                device_name = config.get('product_device')
203                if device_name:
204                    build_vars['device_name'] = device_name
205                else:
206                    build_vars['device_name'] = ''
207                build_vars['product_company'] = config.get('product_company')
208            else:
209                build_vars['os_level'] = config.get('type', 'mini')
210                build_vars['device_name'] = config.get('board')
211                if config.get('product_company'):
212                    build_vars['product_company'] = config.get(
213                        'product_company')
214                elif os.path.dirname(self._config_file) != self._dirs.built_in_product_dir:
215                    relpath = os.path.relpath(
216                        self._config_file, self._dirs.vendor_dir)
217                    build_vars['product_company'] = relpath.split('/')[0]
218                else:
219                    build_vars['product_company'] = config.get(
220                        'device_company')
221            build_vars['product_name'] = config.get('product_name')
222            if 'ext_root_proc_conf_path' in config:
223                ext_root_proc_conf_path = os.path.join(
224                    self._dirs.source_root_dir, config.get('ext_root_proc_conf_path'))
225                if os.path.exists(ext_root_proc_conf_path):
226                    build_vars['ext_root_proc_conf_path'] = ext_root_proc_conf_path
227            if 'ext_critical_proc_conf_path' in config:
228                ext_critical_proc_conf_path = os.path.join(
229                    self._dirs.source_root_dir, config.get('ext_critical_proc_conf_path'))
230                if os.path.exists(ext_critical_proc_conf_path):
231                    build_vars['ext_critical_proc_conf_path'] = ext_critical_proc_conf_path
232            if 'enable_ramdisk' in config:
233                build_vars['enable_ramdisk'] = config.get('enable_ramdisk')
234            if 'enable_absystem' in config:
235                build_vars['enable_absystem'] = config.get('enable_absystem')
236            if 'build_selinux' in config:
237                build_vars['build_selinux'] = config.get('build_selinux')
238            if 'build_seccomp' in config:
239                build_vars['build_seccomp'] = config.get('build_seccomp')
240            if 'support_jsapi' in config:
241                build_vars['support_jsapi'] = config.get('support_jsapi')
242            if 'chipprod_config_path' in config:
243                chipprod_config_path = os.path.join(
244                    self._dirs.source_root_dir, config.get('chipprod_config_path'))
245                if os.path.exists(chipprod_config_path):
246                    build_vars['chipprod_config_path'] = chipprod_config_path
247            if 'ext_sdk_config_file' in config:
248                ext_sdk_config_file = os.path.join(
249                    self._dirs.source_root_dir, config.get('ext_sdk_config_file'))
250                if os.path.exists(ext_sdk_config_file):
251                    build_vars['ext_sdk_config_file'] = ext_sdk_config_file
252            if 'ext_ndk_config_file' in config:
253                ext_ndk_config_file = os.path.join(
254                    self._dirs.source_root_dir, config.get('ext_ndk_config_file'))
255                if os.path.exists(ext_ndk_config_file):
256                    build_vars['ext_ndk_config_file'] = ext_ndk_config_file
257            if 'ext_sign_hap_py_path' in config:
258                path = os.path.join(
259                    self._dirs.source_root_dir, config.get('ext_sign_hap_py_path'))
260                if os.path.exists(path):
261                    build_vars['ext_sign_hap_py_path'] = path
262
263        build_vars.update(self._device_info)
264        if build_vars['os_level'] == 'mini' or build_vars['os_level'] == 'small':
265            toolchain_label = ""
266        else:
267            toolchain_label = '//build/toolchain/{0}:{0}_clang_{1}'.format(
268                self._device_info.get('target_os'), self._device_info.get('target_cpu'))
269        build_vars['product_toolchain_label'] = toolchain_label
270        self._build_vars = build_vars
271
272    # Remove excluded components
273    def _remove_excluded_components(self):
274        items_to_remove = []
275        for part, val in self._parts.items():
276            if "exclude" in val and val["exclude"] == "true":
277                items_to_remove.append(part)
278        for item in items_to_remove:
279            del self._parts[item]
280
281# get method
282
283    # Generate build_info needed for V2 configuration
284    def _get_device_info_v2(self, device_name, config_dir) -> dict:
285        device_config_file = os.path.join(config_dir,
286                                          '{}.json'.format(device_name))
287        device_info = IoUtil.read_json_file(device_config_file)
288        if device_info and device_info.get('device_name') != device_name:
289            raise Exception("device name configuration incorrect in '{}'".format(
290                device_config_file))
291        return device_info
292
293    # Generate build_info needed for V3 configuration
294    def _get_device_info_v3(self, config) -> dict:
295        # NOTE:
296        # Product_name, device_company are necessary for
297        # config.json, DON NOT use .get to replace []
298        device_info = {
299            'device_name': config['board'],
300            'device_company': config['device_company']
301        }
302        if config.get('target_os'):
303            device_info['target_os'] = config.get('target_os')
304        else:
305            device_info['target_os'] = 'ohos'
306        if config.get('target_cpu'):
307            device_info['target_cpu'] = config['target_cpu']
308        else:
309            # Target cpu is used to set default toolchain for standard system.
310            LogUtil.hb_warning(
311                "The target_cpu needs to be specified, default target_cpu=arm")
312            device_info['target_cpu'] = 'arm'
313        if config.get('kernel_version'):
314            device_info['kernel_version'] = config.get('kernel_version')
315        if config.get('device_build_path'):
316            device_info['device_build_path'] = config.get('device_build_path')
317        else:
318            device_build_path = os.path.join(self._dirs.device_dir,
319                                             config['device_company'],
320                                             config['board'])
321            if not os.path.exists(device_build_path):
322                device_build_path = os.path.join(self._dirs.device_dir,
323                                                 'board',
324                                                 config['device_company'],
325                                                 config['board'])
326            device_info['device_build_path'] = device_build_path
327        return device_info
328
329    def _get_device_specific_parts(self) -> dict:
330        info = {}
331        if self._device_info and self._device_info.get('device_build_path'):
332            subsystem_name = 'device_{}'.format(self._device_name)
333            part_name = subsystem_name
334            info['{}:{}'.format(subsystem_name, part_name)] = {}
335        return info
336
337    def _get_device_specific_subsystem(self) -> dict:
338        info = {}
339        subsystem_name = 'device_{}'.format(self._device_name)
340        if self._device_info and self._device_info.get('device_build_path'):
341            info[subsystem_name] = {
342                'name': subsystem_name,
343                'path': self._device_info.get('device_build_path')
344            }
345        return info
346
347    def _get_base_parts(self, base_config_dir, os_level) -> dict:
348        system_base_config_file = os.path.join(base_config_dir,
349                                               '{}_system.json'.format(os_level))
350        if not os.path.exists(system_base_config_file):
351            raise Exception("product configuration '{}' doesn't exist.".format(
352                system_base_config_file))
353        return IoUtil.read_json_file(system_base_config_file)
354
355    def _get_inherit_parts(self, inherit, source_root_dir) -> dict:
356        inherit_parts = {}
357        for _config in inherit:
358            _file = os.path.join(source_root_dir, _config)
359            _info = IoUtil.read_json_file(_file)
360            parts = _info.get('parts')
361            if parts:
362                inherit_parts.update(parts)
363            else:
364                inherit_parts.update(get_vendor_parts_list(_info))
365        return inherit_parts
366
367    def _get_sys_relate_parts(self, system_component_info, _parts, source_root_dir) -> dict:
368        _info = IoUtil.read_json_file(os.path.join(
369            source_root_dir, system_component_info))
370        ret = {}
371        parts = _info.get('parts')
372        if not parts:
373            parts = get_vendor_parts_list(_info)
374        for part, featrue in parts.items():
375            if not _parts.get(part):
376                ret[part] = featrue
377        return ret
378
379    def _get_product_specific_parts(self) -> dict:
380        part_name = 'product_{}'.format(self._name)
381        subsystem_name = part_name
382        info = {}
383        info['{}:{}'.format(subsystem_name, part_name)] = {}
384        return info
385
386    def _get_product_specific_subsystem(self) -> dict:
387        info = {}
388        subsystem_name = 'product_{}'.format(self._name)
389        product_build_path = self._config.get('product_build_path')
390        if product_build_path:
391            info[subsystem_name] = {
392                'name': subsystem_name,
393                'path': product_build_path
394            }
395        return info
396
397    def _get_full_product_config(self) -> dict:
398        config = IoUtil.read_json_file(self._config_file)
399        if config.get("version") != '2.0':
400            if os.path.dirname(self._config_file) != self._dirs.built_in_product_dir \
401                    and not hasattr(self._config, 'product_build_path'):
402                config['product_build_path'] = os.path.relpath(
403                    os.path.dirname(self._config_file), self._dirs.source_root_dir)
404        return config
405