1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 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 tarfile 17import argparse 18import os 19import subprocess 20import sys 21import shutil 22 23 24def untar_file(tar_file_path, extract_path): 25 try: 26 tar_cmd = ['tar', '-zxf', tar_file_path, '-C', extract_path] 27 subprocess.run(tar_cmd, check=True) 28 except Exception as e: 29 print("tar error!") 30 return 31 32def move_file(src_path, dst_path): 33 files = [ 34 "0001-fix-byte_order_is_valid-function-logic.patch", 35 "backport-check-namespace.sh-adjust-aarch64-symbols.patch", 36 "backport-tests-run-coredump-unwind-Skip-test-if-no-coredump-h.patch", 37 "backport-aarch64-unw_step-validates-address-before-calling-dwarf_get.patch", 38 "backport-avoid-calling-printf-because-OE-glibc-2.34-used-mno-.patch", 39 "backport-fix-run-ptrace-mapper-test-case-failed.patch", 40 "ohos-configure.patch", 41 "ohos-access-mem-crash-fix.patch" 42 ] 43 for file in files: 44 src_file = os.path.join(src_path, file) 45 dst_file = os.path.join(dst_path, file) 46 shutil.copy(src_file, dst_file) 47 48 49def apply_patch(patch_file, target_dir): 50 try: 51 if not os.path.exists(target_dir): 52 return 53 patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir] 54 subprocess.run(patch_cmd, check=True) 55 except Exception as e: 56 print(f"apply_patch error! patch: {patch_file}") 57 return 58 59 60def do_patch(target_dir): 61 patch_file = [ 62 "0001-fix-byte_order_is_valid-function-logic.patch", 63 "backport-check-namespace.sh-adjust-aarch64-symbols.patch", 64 "backport-tests-run-coredump-unwind-Skip-test-if-no-coredump-h.patch", 65 "backport-aarch64-unw_step-validates-address-before-calling-dwarf_get.patch", 66 "backport-avoid-calling-printf-because-OE-glibc-2.34-used-mno-.patch", 67 "backport-fix-run-ptrace-mapper-test-case-failed.patch", 68 "ohos-configure.patch", 69 "ohos-access-mem-crash-fix.patch" 70 ] 71 72 for patch in patch_file: 73 apply_patch(patch, target_dir) 74 75 76def main(): 77 libunwind_path = argparse.ArgumentParser() 78 libunwind_path.add_argument('--gen-dir', help='generate path of log', required=True) 79 libunwind_path.add_argument('--source-dir', help='generate path of log', required=True) 80 args = libunwind_path.parse_args() 81 tar_file_path = os.path.join(args.source_dir, "libunwind-1.6.2.tar.gz") 82 target_dir = os.path.join(args.gen_dir, "libunwind-1.6.2") 83 84 untar_file(tar_file_path, args.gen_dir) 85 move_file(args.source_dir, target_dir) 86 do_patch(target_dir) 87 return 0 88 89if __name__ == '__main__': 90 sys.exit(main()) 91 92