• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 tarfile
17import argparse
18import os
19import subprocess
20import sys
21import shutil
22
23patch_files = [
24    "backport-x86-64-Always-double-jump-table-slot-size-for-CET-71.patch",
25    "backport-Fix-check-for-invalid-varargs-arguments-707.patch",
26    "libffi-Add-sw64-architecture.patch",
27    "backport-Fix-signed-vs-unsigned-comparison.patch",
28    "riscv-extend-return-types-smaller-than-ffi_arg-680.patch",
29    "fix-AARCH64EB-support.patch",
30    "backport-openharmony-adapt.patch",
31    "backport-openharmony-dummy.patch"
32]
33
34def untar_file(tar_file_path, extract_path):
35    try:
36        tar_cmd = ['tar', '-zxf', tar_file_path, '-C', extract_path]
37        subprocess.run(tar_cmd, check=True)
38    except Exception as e:
39        print("tar error!")
40        return
41
42def move_file(src_path, dst_path):
43    patch_dir = os.path.join(src_path, "patch")
44    for file in patch_files:
45        src_file = os.path.join(patch_dir, file)
46        dst_file = os.path.join(dst_path, file)
47        shutil.copy(src_file, dst_file)
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("apply_patch error!")
57        return
58
59def do_patch(target_dir):
60    for patch in patch_files:
61        apply_patch(patch, target_dir)
62
63def main():
64    libffi_path = argparse.ArgumentParser()
65    libffi_path.add_argument('--gen-dir', help='generate path of log', required=True)
66    libffi_path.add_argument('--source-dir', help='generate path of log', required=True)
67    args = libffi_path.parse_args()
68    tar_file_path = os.path.join(args.source_dir, "libffi-3.4.2.tar.gz")
69    target_dir = os.path.join(args.gen_dir, "libffi-3.4.2")
70
71    untar_file(tar_file_path, args.gen_dir)
72    move_file(args.source_dir, target_dir)
73    do_patch(target_dir)
74    return 0
75
76if __name__ == '__main__':
77    sys.exit(main())
78