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