1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 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
16import sys
17import argparse
18import os
19
20from kernel_permission import KernelPermission
21sys.path.append(
22    os.path.dirname(os.path.dirname(os.path.dirname(
23        os.path.abspath(__file__)))))
24from scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402
25from scripts.util import build_utils  # noqa: E402
26
27
28def get_platform_parts(current_platform: str, platforms_parts_file: str):
29    all_platforms_parts = read_json_file(platforms_parts_file)
30    if all_platforms_parts is None:
31        raise Exception("read file '{}' failed.".format(all_platforms_parts))
32    platforms_parts = all_platforms_parts.get(current_platform)
33    return platforms_parts
34
35
36def platform_parts(all_parts_info_file: str, current_platform_parts: dict):
37    _parts_list = []
38    all_parts_info = read_json_file(all_parts_info_file)
39    if all_parts_info is None:
40        raise Exception("read file '{}' failed.".format(all_parts_info_file))
41
42    for part_name in current_platform_parts.keys():
43        if part_name not in all_parts_info:
44            raise Exception(
45                "required part '{}' doesn't exist.".format(part_name))
46        _parts_list.append(all_parts_info.get(part_name))
47    return _parts_list
48
49
50def main():
51    parser = argparse.ArgumentParser()
52    parser.add_argument('--all-parts-info-file', required=True)
53    parser.add_argument('--platforms-parts-file', required=True)
54    parser.add_argument('--system-install-info-file', required=True)
55    parser.add_argument('--current-platform', required=True)
56    parser.add_argument('--depfile', required=False)
57    parser.add_argument('--root-build-dir', required=False)
58    parser.add_argument('--root-code-dir', required=False)
59    args = parser.parse_args()
60
61    KernelPermission.run(args.root_build_dir, args.root_code_dir)
62
63    current_platform_parts = get_platform_parts(args.current_platform,
64                                                args.platforms_parts_file)
65
66    _parts_list = platform_parts(args.all_parts_info_file,
67                                 current_platform_parts)
68
69    platform_out_dir = os.path.dirname(args.system_install_info_file)
70    if not os.path.exists(platform_out_dir):
71        os.makedirs(platform_out_dir, exist_ok=True)
72    write_json_file(args.system_install_info_file, _parts_list)
73
74    if args.depfile:
75        _dep_files = []
76        _dep_files.append(args.all_parts_info_file)
77        _dep_files.append(args.platforms_parts_file)
78        build_utils.write_depfile(args.depfile,
79                                 args.system_install_info_file,
80                                 _dep_files,
81                                 add_pydeps=False)
82    return 0
83
84
85if __name__ == '__main__':
86    sys.exit(main())
87