13b921b29Sopenharmony_ci#!/usr/bin/env python
23b921b29Sopenharmony_ci# -*- coding: utf-8 -*-
33b921b29Sopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
43b921b29Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
53b921b29Sopenharmony_ci# you may not use this file except in compliance with the License.
63b921b29Sopenharmony_ci# You may obtain a copy of the License at
73b921b29Sopenharmony_ci#
83b921b29Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
93b921b29Sopenharmony_ci#
103b921b29Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
113b921b29Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
123b921b29Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b921b29Sopenharmony_ci# See the License for the specific language governing permissions and
143b921b29Sopenharmony_ci# limitations under the License.
153b921b29Sopenharmony_ci
163b921b29Sopenharmony_ciimport tarfile
173b921b29Sopenharmony_ciimport argparse
183b921b29Sopenharmony_ciimport os
193b921b29Sopenharmony_ciimport subprocess
203b921b29Sopenharmony_ciimport sys
213b921b29Sopenharmony_ciimport shutil
223b921b29Sopenharmony_ci
233b921b29Sopenharmony_ci
243b921b29Sopenharmony_cidef untar_file(tar_file_path, extract_path):
253b921b29Sopenharmony_ci    try:
263b921b29Sopenharmony_ci        tar_cmd = ['tar', '-zxf', tar_file_path, '-C', extract_path]
273b921b29Sopenharmony_ci        subprocess.run(tar_cmd, check=True)
283b921b29Sopenharmony_ci    except Exception as e:
293b921b29Sopenharmony_ci        print("tar error!")
303b921b29Sopenharmony_ci        return
313b921b29Sopenharmony_ci
323b921b29Sopenharmony_ci
333b921b29Sopenharmony_cidef move_file(src_path, dst_path):
343b921b29Sopenharmony_ci    files = [
353b921b29Sopenharmony_ci        "Add-loongarch64.patch",
363b921b29Sopenharmony_ci        "libjpeg-turbo-2.1.x-bugfix.patch",
373b921b29Sopenharmony_ci        "liteos_adapting.patch",
383b921b29Sopenharmony_ci        "libjpeg-turbo-optimize.patch",
393b921b29Sopenharmony_ci        "jconfig.h",
403b921b29Sopenharmony_ci        "jconfigint.h",
413b921b29Sopenharmony_ci        "jversion.h.in"
423b921b29Sopenharmony_ci    ]
433b921b29Sopenharmony_ci    neon_compat_src_path = os.path.join(src_path, "neon-compat.h")
443b921b29Sopenharmony_ci    neon_compat_dst_path = os.path.join(dst_path, "simd/arm/neon-compat.h")
453b921b29Sopenharmony_ci    shutil.copy(neon_compat_src_path, neon_compat_dst_path)
463b921b29Sopenharmony_ci    for file in files:
473b921b29Sopenharmony_ci        src_file = os.path.join(src_path, file)
483b921b29Sopenharmony_ci        dst_file = os.path.join(dst_path, file)
493b921b29Sopenharmony_ci        shutil.copy(src_file, dst_file)
503b921b29Sopenharmony_ci
513b921b29Sopenharmony_ci
523b921b29Sopenharmony_cidef apply_patch(patch_file, target_dir):
533b921b29Sopenharmony_ci    try:
543b921b29Sopenharmony_ci        if not os.path.exists(target_dir):
553b921b29Sopenharmony_ci            return
563b921b29Sopenharmony_ci        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
573b921b29Sopenharmony_ci        subprocess.run(patch_cmd, check=True)
583b921b29Sopenharmony_ci    except Exception as e:
593b921b29Sopenharmony_ci        print("apply_patch error!")
603b921b29Sopenharmony_ci        return
613b921b29Sopenharmony_ci
623b921b29Sopenharmony_ci
633b921b29Sopenharmony_cidef do_patch(target_dir):
643b921b29Sopenharmony_ci    patch_file = [
653b921b29Sopenharmony_ci        "Add-loongarch64.patch",
663b921b29Sopenharmony_ci        "libjpeg-turbo-2.1.x-bugfix.patch",
673b921b29Sopenharmony_ci        "liteos_adapting.patch",
683b921b29Sopenharmony_ci        "libjpeg-turbo-optimize.patch",
693b921b29Sopenharmony_ci    ]
703b921b29Sopenharmony_ci
713b921b29Sopenharmony_ci    for patch in patch_file:
723b921b29Sopenharmony_ci        apply_patch(patch, target_dir)
733b921b29Sopenharmony_ci
743b921b29Sopenharmony_ci
753b921b29Sopenharmony_cidef main():
763b921b29Sopenharmony_ci    freetype_path = argparse.ArgumentParser()
773b921b29Sopenharmony_ci    freetype_path.add_argument('--gen-dir', help='generate path of log', required=True)
783b921b29Sopenharmony_ci    freetype_path.add_argument('--source-dir', help='generate path of log', required=True)
793b921b29Sopenharmony_ci    args = freetype_path.parse_args()
803b921b29Sopenharmony_ci    tar_file_path = os.path.join(args.source_dir, "libjpeg-turbo-2.1.1.tar.gz")
813b921b29Sopenharmony_ci    target_dir = os.path.join(args.gen_dir, "libjpeg-turbo-2.1.1")
823b921b29Sopenharmony_ci
833b921b29Sopenharmony_ci    untar_file(tar_file_path, args.gen_dir)
843b921b29Sopenharmony_ci    move_file(args.source_dir, target_dir)
853b921b29Sopenharmony_ci    do_patch(target_dir)
863b921b29Sopenharmony_ci    return 0
873b921b29Sopenharmony_ci
883b921b29Sopenharmony_ciif __name__ == '__main__':
893b921b29Sopenharmony_ci    sys.exit(main())
90