1e509ee18Sopenharmony_ci#!/usr/bin/env python3
2e509ee18Sopenharmony_ci# -*- coding: utf-8 -*-
3e509ee18Sopenharmony_ci#
4e509ee18Sopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd.
5e509ee18Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6e509ee18Sopenharmony_ci# you may not use this file except in compliance with the License.
7e509ee18Sopenharmony_ci# You may obtain a copy of the License at
8e509ee18Sopenharmony_ci#
9e509ee18Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10e509ee18Sopenharmony_ci#
11e509ee18Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12e509ee18Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
13e509ee18Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14e509ee18Sopenharmony_ci# See the License for the specific language governing permissions and
15e509ee18Sopenharmony_ci# limitations under the License.
16e509ee18Sopenharmony_ci#
17e509ee18Sopenharmony_ci
18e509ee18Sopenharmony_ciimport argparse
19e509ee18Sopenharmony_ciimport hashlib
20e509ee18Sopenharmony_ciimport json
21e509ee18Sopenharmony_ciimport os
22e509ee18Sopenharmony_ciimport sys
23e509ee18Sopenharmony_ci
24e509ee18Sopenharmony_ci
25e509ee18Sopenharmony_cidef __check_changes(output_file, content):
26e509ee18Sopenharmony_ci    if os.path.exists(output_file) and os.path.isfile(output_file):
27e509ee18Sopenharmony_ci        # file content md5 val
28e509ee18Sopenharmony_ci        sha256_obj = hashlib.sha256()
29e509ee18Sopenharmony_ci        sha256_obj.update(str(read_json_file(output_file)).encode())
30e509ee18Sopenharmony_ci        hash_value = sha256_obj.hexdigest()
31e509ee18Sopenharmony_ci        # new content md5 val
32e509ee18Sopenharmony_ci        sha256_obj_new = hashlib.sha256()
33e509ee18Sopenharmony_ci        sha256_obj_new.update(str(content).encode())
34e509ee18Sopenharmony_ci        hash_value_new = sha256_obj_new.hexdigest()
35e509ee18Sopenharmony_ci        if hash_value_new == hash_value:
36e509ee18Sopenharmony_ci            return False
37e509ee18Sopenharmony_ci    return True
38e509ee18Sopenharmony_ci
39e509ee18Sopenharmony_ci
40e509ee18Sopenharmony_ci# Read json file data
41e509ee18Sopenharmony_cidef read_json_file(input_file):
42e509ee18Sopenharmony_ci    if not os.path.exists(input_file):
43e509ee18Sopenharmony_ci        print("file '{}' doesn't exist.".format(input_file))
44e509ee18Sopenharmony_ci        return None
45e509ee18Sopenharmony_ci
46e509ee18Sopenharmony_ci    data = None
47e509ee18Sopenharmony_ci    try:
48e509ee18Sopenharmony_ci        with open(input_file, 'r') as input_f:
49e509ee18Sopenharmony_ci            data = json.load(input_f)
50e509ee18Sopenharmony_ci    except json.decoder.JSONDecodeError:
51e509ee18Sopenharmony_ci        print("The file '{}' format is incorrect.".format(input_file))
52e509ee18Sopenharmony_ci        raise
53e509ee18Sopenharmony_ci    return data
54e509ee18Sopenharmony_ci
55e509ee18Sopenharmony_ci
56e509ee18Sopenharmony_ci# Write json file data
57e509ee18Sopenharmony_cidef write_json_file(output_file, content, check_changes=False):
58e509ee18Sopenharmony_ci    file_dir = os.path.dirname(os.path.abspath(output_file))
59e509ee18Sopenharmony_ci    if not os.path.exists(file_dir):
60e509ee18Sopenharmony_ci        os.makedirs(file_dir, exist_ok=True)
61e509ee18Sopenharmony_ci
62e509ee18Sopenharmony_ci    if check_changes is True:
63e509ee18Sopenharmony_ci        changed = __check_changes(output_file, content)
64e509ee18Sopenharmony_ci    else:
65e509ee18Sopenharmony_ci        changed = True
66e509ee18Sopenharmony_ci    if changed is True:
67e509ee18Sopenharmony_ci        with open(output_file, 'w') as output_f:
68e509ee18Sopenharmony_ci            json.dump(content, output_f, sort_keys=True, indent=2)
69e509ee18Sopenharmony_ci
70e509ee18Sopenharmony_ci
71e509ee18Sopenharmony_cidef get_full_path_from_target_name(config_info, target_name) -> str:
72e509ee18Sopenharmony_ci    inner_kits = config_info["component"]["build"]["inner_kits"]
73e509ee18Sopenharmony_ci    for inner_kit in inner_kits:
74e509ee18Sopenharmony_ci        inner_kit_parts = inner_kit["name"].split(":")
75e509ee18Sopenharmony_ci        if inner_kit_parts[1].startswith(target_name):
76e509ee18Sopenharmony_ci            return inner_kit["name"]
77e509ee18Sopenharmony_ci    print("Attemp to get a target({}) which is not in the component's inner_kits!".format(target_name))
78e509ee18Sopenharmony_ci    sys.exit(1)
79e509ee18Sopenharmony_ci    return ""
80e509ee18Sopenharmony_ci
81e509ee18Sopenharmony_ci
82e509ee18Sopenharmony_cidef main():
83e509ee18Sopenharmony_ci    parser = argparse.ArgumentParser()
84e509ee18Sopenharmony_ci    parser.add_argument("--root-src-dir", required=True)
85e509ee18Sopenharmony_ci    parser.add_argument("--external-deps-temp-file", required=True)
86e509ee18Sopenharmony_ci    parser.add_argument("--external-deps", nargs='+', required=True)
87e509ee18Sopenharmony_ci    args = parser.parse_args()
88e509ee18Sopenharmony_ci
89e509ee18Sopenharmony_ci    deps = []
90e509ee18Sopenharmony_ci    for dep in args.external_deps:
91e509ee18Sopenharmony_ci        if dep.startswith("ets_runtime"):
92e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/ets_runtime/bundle.json".format(args.root_src_dir))
93e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
94e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
95e509ee18Sopenharmony_ci        elif dep.startswith("runtime_core"):
96e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/runtime_core/bundle.json".format(args.root_src_dir))
97e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
98e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
99e509ee18Sopenharmony_ci        elif dep.startswith("ets_frontend"):
100e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/ets_frontend/bundle.json".format(args.root_src_dir))
101e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
102e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
103e509ee18Sopenharmony_ci        elif dep.startswith("toolchain"):
104e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/bundle.json".format(args.root_src_dir))
105e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
106e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
107e509ee18Sopenharmony_ci        elif dep.startswith("libuv"):
108e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/" \
109e509ee18Sopenharmony_ci              "third_party_gn/libuv/dummy_bundle.json".format(args.root_src_dir))
110e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
111e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
112e509ee18Sopenharmony_ci        elif dep.startswith("bounds_checking_function"):
113e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
114e509ee18Sopenharmony_ci                    "bounds_checking_function/dummy_bundle.json".format(args.root_src_dir))
115e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
116e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
117e509ee18Sopenharmony_ci        elif dep.startswith("icu"):
118e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
119e509ee18Sopenharmony_ci                    "icu/dummy_bundle.json".format(args.root_src_dir))
120e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
121e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
122e509ee18Sopenharmony_ci        elif dep.startswith("cJSON"):
123e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
124e509ee18Sopenharmony_ci                    "cJSON/dummy_bundle.json".format(args.root_src_dir))
125e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
126e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
127e509ee18Sopenharmony_ci        elif dep.startswith("openssl"):
128e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
129e509ee18Sopenharmony_ci                    "openssl/dummy_bundle.json".format(args.root_src_dir))
130e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
131e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
132e509ee18Sopenharmony_ci        elif dep.startswith("zlib"):
133e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
134e509ee18Sopenharmony_ci                    "zlib/dummy_bundle.json".format(args.root_src_dir))
135e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
136e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
137e509ee18Sopenharmony_ci        elif dep.startswith("json"):
138e509ee18Sopenharmony_ci            config_info = read_json_file("{}arkcompiler/toolchain/build/third_party_gn/" \
139e509ee18Sopenharmony_ci                    "json/dummy_bundle.json".format(args.root_src_dir))
140e509ee18Sopenharmony_ci            target_name = dep.split(":")[1]
141e509ee18Sopenharmony_ci            deps.append(get_full_path_from_target_name(config_info, target_name))
142e509ee18Sopenharmony_ci        else:
143e509ee18Sopenharmony_ci            print("Component in which the external_dep defined is ommited in the logic of {}!".format(__file__))
144e509ee18Sopenharmony_ci            sys.exit(1)
145e509ee18Sopenharmony_ci            return
146e509ee18Sopenharmony_ci
147e509ee18Sopenharmony_ci    result = {}
148e509ee18Sopenharmony_ci    if deps:
149e509ee18Sopenharmony_ci        result['deps'] = deps
150e509ee18Sopenharmony_ci    write_json_file(args.external_deps_temp_file, result)
151e509ee18Sopenharmony_ci    return 0
152e509ee18Sopenharmony_ci
153e509ee18Sopenharmony_ci
154e509ee18Sopenharmony_ciif __name__ == "__main__":
155e509ee18Sopenharmony_ci    main()
156