diff --git a/ohos/packages/BUILD.gn b/ohos/packages/BUILD.gn index 0797f6dc8a58594df08ba1a120e44e711c7b8df6..a4ab2508793fc9ab800fdfaf5344fa749d39ac74 100644 --- a/ohos/packages/BUILD.gn +++ b/ohos/packages/BUILD.gn @@ -322,6 +322,8 @@ foreach(_platform, target_platform_list) { rebase_path(_system_image_zipfile, root_build_dir), "--host-toolchain", _host_toolchain, + "--module-install-copy-type", + module_install_copy_type ] _additional_system_files = [] diff --git a/ohos/packages/modules_install.py b/ohos/packages/modules_install.py index dc6708863982f833f61c33f328a14f0769ec2edc..33b3e67c27861dc70009747ae2a5980fd97f6da4 100755 --- a/ohos/packages/modules_install.py +++ b/ohos/packages/modules_install.py @@ -21,7 +21,7 @@ import shutil sys.path.append( os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))) -from scripts.util.file_utils import read_json_file, write_json_file, write_file # noqa: E402 +from scripts.util.file_utils import read_json_file, write_json_file, write_file, custom_copy, custom_copytree # noqa: E402 from scripts.util import build_utils # noqa: E402 sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "rules")) @@ -68,7 +68,8 @@ def _get_post_process_modules_info(post_process_modules_info_files: list, depfil def copy_modules(system_install_info: dict, install_modules_info_file: str, modules_info_file: str, module_list_file: str, post_process_modules_info_files: list, platform_installed_path: str, - host_toolchain, additional_system_files: dict, depfiles: list, categorized_libraries: dict): + host_toolchain, additional_system_files: dict, depfiles: list, categorized_libraries: dict, + use_hardlink: bool): output_result = [] dest_list = [] symlink_dest = [] @@ -94,7 +95,7 @@ def copy_modules(system_install_info: dict, install_modules_info_file: str, output_result.append(_module_info) for source, system_path in additional_system_files: - shutil.copy(source, os.path.join(platform_installed_path, system_path)) + custom_copy(source, os.path.join(platform_installed_path, system_path), "copy", use_hardlink) # copy modules for module_info in output_result: @@ -127,12 +128,12 @@ def copy_modules(system_install_info: dict, install_modules_info_file: str, for filename in os.listdir(source): if filename.endswith('.hap') or filename.endswith('.hsp'): is_hvigor_hap = True - shutil.copy2(os.path.join(source, filename), - os.path.join(platform_installed_path, dest, filename)) + custom_copy(os.path.join(source, filename), + os.path.join(platform_installed_path, dest, filename), "copy2", use_hardlink) if not is_hvigor_hap: - shutil.copytree(source, os.path.join(platform_installed_path, dest), dirs_exist_ok=True) + custom_copytree(source, os.path.join(platform_installed_path, dest), True, use_hardlink) else: - shutil.copy2(source, os.path.join(platform_installed_path, dest)) + custom_copy(source, os.path.join(platform_installed_path, dest), "copy2", use_hardlink) # add symlink if 'symlink' in module_info: @@ -211,6 +212,7 @@ def main(): parser.add_argument('--system-image-zipfile', required=True) parser.add_argument('--host-toolchain', required=True) parser.add_argument('--categorized-libraries', required=False) + parser.add_argument('--module-install-copy-type', required=True) parser.add_argument( '--additional-system-files', action='append', @@ -222,6 +224,8 @@ def main(): default=[]) args = parser.parse_args() + use_hardlink = (args.module_install_copy_type == "hardlink") + additional_system_files = [] for tuples in args.additional_system_files or []: filepath, system_path = tuples.split(':') @@ -304,7 +308,7 @@ def main(): args.modules_info_file, args.modules_list_file, args.post_process_modules_info_files, args.platform_installed_path, args.host_toolchain, - additional_system_files, depfiles, categorized_libraries) + additional_system_files, depfiles, categorized_libraries, use_hardlink) if os.path.exists(args.system_image_zipfile): os.unlink(args.system_image_zipfile) diff --git a/ohos_var.gni b/ohos_var.gni index 1397afc71b02bc9402135b84d990bf0c1dee82b2..d1844505f5fb98e3baf61691f24dd3396f6646a1 100755 --- a/ohos_var.gni +++ b/ohos_var.gni @@ -14,6 +14,11 @@ import("//build/common.gni") import("//build/version.gni") +declare_args() { + # module install copy type: copy/hardlink + module_install_copy_type = "copy" +} + declare_args() { # build ohos version build_public_version = true diff --git a/scripts/util/file_utils.py b/scripts/util/file_utils.py index 65dcb4af9cc96e5313bbb33e8fa90ca9d8eb470a..1d03b6010604aeff7cebb2a952c40e74d1fdc138 100755 --- a/scripts/util/file_utils.py +++ b/scripts/util/file_utils.py @@ -15,6 +15,7 @@ import json import os +import shutil import subprocess import hashlib import platform @@ -115,3 +116,55 @@ def write_file(output_file, content): cmd = [gn_exe, 'format'] cmd.append(output_file) subprocess.check_output(cmd) + + +def create_hardlink(target, link_path): + # Delete the existing target (if it is a file) + if os.path.lexists(link_path): + if os.path.isdir(link_path): + print("'{}' is a directory that cannot be replaced.".format(link_path)) + return + os.unlink(link_path) + + try: + os.link(target, link_path) + except OSError as e: + shutil.copy2(target, link_path) + + +def custom_copy(src, dst, copy_type:str, use_hardlink=False): + if not use_hardlink: + if copy_type == "copy": + shutil.copy(src, dst) + else: + shutil.copy2(src, dst) + return + # Processing Directory Targets + if os.path.isdir(dst): + dst = os.path.join(dst, os.path.basename(src)) + + # Working with symbolic follow_symlinks + if os.path.islink(src): + link_src = os.readlink(src) + os.symlink(link_src, dst) + return dst + + # Obtain the actual source path (follow symbolic links) + real_src = os.path.realpath(src) + + # Create target directory structure + os.makedirs(os.path.dirname(dst), exist_ok=True) + + # Create hard link or copy + if os.path.isfile(real_src): + create_hardlink(real_src, dst) + else: + # Non-standard files + raise Exception(f"The target path is not a standard file: '{real_src}'") + + +def custom_copytree(src, dst, dirs_exist_ok=False, use_hardlink=False): + if not use_hardlink: + shutil.copytree(src, dst, dirs_exist_ok=dirs_exist_ok) + else: + shutil.copytree(src, dst, copy_function=create_hardlink, dirs_exist_ok=dirs_exist_ok) \ No newline at end of file