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
16import argparse
17import os
18import sys
19
20sys.path.append(
21    os.path.dirname(os.path.dirname(os.path.dirname(
22        os.path.abspath(__file__)))))
23from scripts.util.file_utils import read_json_file  # noqa: E402
24
25
26def check(args) -> list:
27    depfiles = []
28    # ignore test related parts
29    part_allow_set = {'libc-test', 'libc-test-lib', 'libc-gtest-lib'}
30    if args.part_name in part_allow_set:
31        return depfiles
32
33    compile_standard_allow_file = args.compile_standard_allow_file
34    compile_standard_allow_info = read_json_file(compile_standard_allow_file)
35    bundle_file_allow_list = compile_standard_allow_info.get("gn_part_or_subsystem_error", [])
36    part_subsystem_info_file = 'build_configs/parts_info/part_subsystem.json'
37    data = read_json_file(part_subsystem_info_file)
38    if data is None:
39        raise Exception(
40            "read file '{}' failed.".format(part_subsystem_info_file))
41    depfiles.append(part_subsystem_info_file)
42
43    subsystems_name = data.get(args.part_name)
44    if subsystems_name is None or subsystems_name == '' or subsystems_name != args.subsystem_name:
45        message = f"subsystem name or part name is incorrect, " \
46           f"target is {args.target_path}, subsystem name is {args.subsystem_name}, " \
47           f"part name is {args.part_name}"
48        if args.target_path in bundle_file_allow_list:
49            print(f"[0/0] warning: {message}")
50        else:
51            raise Exception(message)
52
53    return depfiles
54
55
56def main():
57    parser = argparse.ArgumentParser()
58    parser.add_argument('--part-name', required=True)
59    parser.add_argument('--subsystem-name', required=True)
60    parser.add_argument('--target-path', required=True)
61    args = parser.parse_args()
62
63    check(args)
64
65    return 0
66
67if __name__ == '__main__':
68    sys.exit(main())
69