1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2025 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. 15import os 16import sys 17import argparse 18import subprocess 19from contextlib import contextmanager 20 21 22@contextmanager 23def cwd(path): 24 oldpwd = os.getcwd() 25 os.chdir(path) 26 try: 27 yield 28 finally: 29 os.chdir(oldpwd) 30 31 32def build_sym_script(args): 33 if not os.path.exists(args.lib_dir): 34 print(f'adlt_tools build_sym_script: input lib dir {args.lib_dir} is not exists') 35 return False 36 37 with cwd(args.lib_dir): 38 with open('adlt_build_syms.sh', 'w') as f: 39 f.write( 40 f'work_dir=`pwd`\n' 41 f'cd {args.target_lib_dir}\n' 42 f'for i in libadlt*.sh ; do\n' 43 f'sh $i;\n' 44 f'done\n' 45 f'cd $work_dir\n' 46 ) 47 48 return True 49 50 51def build_sym_links(args): 52 if not os.path.exists(args.lib_dir): 53 print(f'adlt_tools build_sym_links: input lib dir {args.lib_dir} is not exists') 54 return False 55 56 with cwd(args.lib_dir): 57 if not os.path.exists(args.adlt_lib): 58 print(f'adlt_tools build_sym_links: input adlt lib {args.adlt_lib} is not exists') 59 return False 60 61 orig_libs = [] 62 for orig_lib in args.orig_lib_list: 63 orig_lib = ''.join(orig_lib) 64 orig_libs.append(orig_lib) 65 if not os.path.exists(orig_lib): 66 print(f'adlt_tools build_sym_links: input adlt orig lib {orig_lib} is not exists') 67 return False 68 69 orig_lib_dir = 'adlt_orig_libs' 70 os.makedirs(orig_lib_dir, exist_ok=True) 71 72 for orig_lib in orig_libs: 73 os.rename(orig_lib, f'{orig_lib_dir}{os.sep}{orig_lib}') 74 75 orig_libs = ' '.join(orig_libs) 76 with open(args.adlt_lib.replace('.so', '.sh'), 'w') as f: 77 f.write( 78 f'work_dir=`pwd`\n' 79 f'cd {args.target_lib_dir}\n' 80 f'for i in {orig_libs} ; do\n' 81 f'ln -s {args.adlt_lib} $i ;\n' 82 f'done\n' 83 f'cd $work_dir\n' 84 ) 85 86 return True 87 88 89def get_build_id(args): 90 if not os.path.exists(args.tool_path): 91 print(f'adlt_tools get_build_id: tool path {args.tool_path} is not exists') 92 return False 93 94 files = [] 95 for file in args.file_list: 96 file = ''.join(file) 97 files.append(file) 98 if not os.path.exists(file): 99 print(f'adlt_tools get_build_id: input file {file} is not exists') 100 return False 101 102 with open(args.output_file, 'w') as f: 103 for file in files: 104 ret = subprocess.run([args.tool_path, '-n', file], 105 stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8') 106 107 if ret.returncode != 0: 108 print(f'adlt_tools get_build_id: read file {file} failed ret: {ret.returncode}') 109 if ret.stderr: 110 print(ret.stderr) 111 return False 112 113 for line in ret.stdout.splitlines(): 114 if 'Build ID:' in line: 115 f.write(f'{line}\n') 116 break 117 118 return True 119 120 121def main(): 122 parser = argparse.ArgumentParser() 123 subparsers = parser.add_subparsers() 124 125 subparser = subparsers.add_parser('build_sym_script') 126 subparser.set_defaults(func=build_sym_script) 127 subparser.add_argument('--lib-dir', type=str, required=True) 128 subparser.add_argument('--target-lib-dir', type=str, required=True) 129 130 subparser = subparsers.add_parser('build_sym_links') 131 subparser.set_defaults(func=build_sym_links) 132 subparser.add_argument('--lib-dir', type=str, required=True) 133 subparser.add_argument('--target-lib-dir', type=str, required=True) 134 subparser.add_argument('--adlt-lib', type=str, required=True) 135 subparser.add_argument('--orig-lib-list', type=list, nargs='+', required=True) 136 137 subparser = subparsers.add_parser('get_build_id') 138 subparser.set_defaults(func=get_build_id) 139 subparser.add_argument('--tool-path', type=str, required=True) 140 subparser.add_argument('--output-file', type=str, required=True) 141 subparser.add_argument('--file-list', type=list, nargs='+', required=True) 142 143 args = parser.parse_args() 144 if not args.func(args): 145 sys.exit(1) 146 147 148if __name__ == '__main__': 149 main()