From d509a1ba766bd47ffc325308ecb3ad7c8ca8ce2b Mon Sep 17 00:00:00 2001 From: alexander stefanov Date: Fri, 15 Nov 2024 13:12:28 +0300 Subject: [PATCH] add patch.py --- build.py | 6 ++++-- utils/kernel.py | 5 ++++- utils/patch.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ utils/uboot.py | 5 ++++- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 utils/patch.py diff --git a/build.py b/build.py index 4cff4ae..8e66dd0 100755 --- a/build.py +++ b/build.py @@ -11,6 +11,8 @@ from utils.make_disk import create_partitions, mount_partitions from utils.generate_spec import generate_spec_file from utils.kernel import clone_kernel, make_kernel_tar from utils.uboot import build_uboot +from utils.patch import apply_uboot_patches, apply_kernel_patches + BASE_DIR = os.getcwd() TMP_DIR = os.path.join(BASE_DIR, "tmp") @@ -47,14 +49,14 @@ def main(): if not skip_kernel: generate_spec_file(TMP_DIR, config, vendor, device) kernel_dir = os.path.join(TMP_DIR, vendor, device, "kernel") - clone_kernel(TMP_DIR, config, vendor, device, kernel_dir) + clone_kernel(TMP_DIR, BASE_DIR, config, vendor, device, kernel_dir) kernel_rpm_dir = os.path.join(TMP_DIR, vendor, device, "kernel-build") make_kernel_tar(kernel_dir, kernel_rpm_dir) else: print("Skipping kernel build.") if not skip_uboot: - build_uboot(TMP_DIR, config, vendor, device) + build_uboot(TMP_DIR, BASE_DIR, config, vendor, device) else: print("Skipping U-Boot build.") diff --git a/utils/kernel.py b/utils/kernel.py index 679933a..ab8fa30 100644 --- a/utils/kernel.py +++ b/utils/kernel.py @@ -4,11 +4,14 @@ import os import sys import subprocess from utils.common import clone_repo +from utils.patch import apply_kernel_patches -def clone_kernel(TMP_DIR, config, vendor, device, kernel_dir): + +def clone_kernel(TMP_DIR, BASE_DIR, config, vendor, device, kernel_dir): kernel_git = config.get("KERNEL").split("#")[0] kernel_branch = config.get("KERNEL").split("#")[1] clone_repo(kernel_git, kernel_branch, kernel_dir, "kernel") + apply_kernel_patches(BASE_DIR, vendor, device, kernel_dir) def make_kernel_tar(kernel_dir, kernel_rpm_dir): diff --git a/utils/patch.py b/utils/patch.py new file mode 100644 index 0000000..13d2aa3 --- /dev/null +++ b/utils/patch.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import subprocess + + +def apply_patches(patch_dir, target_dir): + """ + Applies patches from the specified patch directory to the target directory using `git apply`. + """ + if not os.path.exists(patch_dir): + print(f"No patches directory found at {patch_dir}. Skipping patch application.") + return + + patches = sorted(os.listdir(patch_dir)) + + if not patches: + print(f"No patches found in {patch_dir}. Skipping patch application.") + return + + print(f"Applying patches from {patch_dir} to {target_dir}...") + + for patch in patches: + patch_path = os.path.join(patch_dir, patch) + if os.path.isfile(patch_path): + print(f"Applying patch: {patch}") + try: + subprocess.run(["git", "apply", patch_path], cwd=target_dir, check=True) + except subprocess.CalledProcessError as e: + print(f"Failed to apply patch {patch}: {e}") + else: + print(f"Skipping non-file item: {patch_path}") + + +def apply_kernel_patches(base_dir, vendor, device, kernel_dir): + """ + Apply kernel patches for the specified vendor and device. + """ + patch_dir = os.path.join(base_dir, "device", vendor, device, "patches", "kernel") + apply_patches(patch_dir, kernel_dir) + + +def apply_uboot_patches(base_dir, vendor, device, uboot_dir): + """ + Apply U-Boot patches for the specified vendor and device. + """ + patch_dir = os.path.join(base_dir, "device", vendor, device, "patches", "u-boot") + apply_patches(patch_dir, uboot_dir) + diff --git a/utils/uboot.py b/utils/uboot.py index f5f20d3..512ecf8 100644 --- a/utils/uboot.py +++ b/utils/uboot.py @@ -5,8 +5,10 @@ import os import sys import subprocess from utils.common import clone_repo +from utils.patch import apply_uboot_patches -def build_uboot(TMP_DIR, config, vendor, device): + +def build_uboot(TMP_DIR, BASE_DIR, config, vendor, device): uboot_git = config.get("UBOOT") uboot_branch = config.get("UBOOT_VERSION") @@ -16,6 +18,7 @@ def build_uboot(TMP_DIR, config, vendor, device): uboot_dir = os.path.join(TMP_DIR, vendor, device, "u-boot") clone_repo(uboot_git, uboot_branch, uboot_dir, "u-boot") + apply_uboot_patches(BASE_DIR, vendor, device, uboot_dir) #os.chdir(uboot_dir)