patching: introduce add-only mode for DT Makefile AutoPatcher

- Autopatcher was commiting atrocities to the upstream DTs that are so
  carefully hand-crafted, by simply rewriting everything, or trying to be
  smart and failing at it (`incremental: true`)
- Instead, add `add-only: true` mode, where we simply only ever add to the
  end of the Makefile, adding each new .dts and optionally the `subdir-y`
  for overlays
This commit is contained in:
Ricardo Pardini 2026-01-08 19:52:01 +01:00
parent a61ff239af
commit a85a5aef67
2 changed files with 36 additions and 13 deletions

View File

@ -65,7 +65,8 @@ class AutomaticPatchDescription:
return f"Armbian Autopatcher: {self.description}"
def auto_patch_dt_makefile(git_work_dir: str, dt_rel_dir: str, config_var: str, dt_files_to_add: list[str], incremental: bool) -> dict[str, str]:
def auto_patch_dt_makefile(git_work_dir: str, dt_rel_dir: str, config_var: str, dt_files_to_add: list[str], incremental: bool, add_only: bool) -> \
dict[str, str]:
ret: dict[str, str] = {}
dt_path = os.path.join(git_work_dir, dt_rel_dir)
# Bomb if it does not exist or is not a directory
@ -83,6 +84,23 @@ def auto_patch_dt_makefile(git_work_dir: str, dt_rel_dir: str, config_var: str,
makefile_contents = f.read()
log.info(f"Read {len(makefile_contents)} bytes from {makefile_path}")
log.debug(f"Contents:\n{makefile_contents}")
if add_only:
# much simpler. just add individual -y lines for each dt file to add directly to the end of the Makefile and hope for the best
log.info(f"Add-only mode: will just add {len(dt_files_to_add)} dt files to the end of the Makefile")
with open(makefile_path, "a") as f:
f.write("\n\n# Added by Armbian autopatcher in add-only:true mode\n")
for one_dt_file in dt_files_to_add:
f.write(f"\ndtb-$({config_var}) += {one_dt_file[:-4]}.dtb\n") # replace .dts suffix with .dtb
overlay_lines = []
add_overlay_lines(dt_path, incremental, overlay_lines, ret)
if len(overlay_lines) > 0:
f.write("\n")
f.write("\n".join(overlay_lines))
log.info(f"Wrote add-only entries to {makefile_path}")
ret["extra_desc"] = "add-only mode"
return ret
# Parse it into a list of lines
makefile_lines = makefile_contents.splitlines()
log.info(f"Read {len(makefile_lines)} lines from {makefile_path}")
@ -166,17 +184,7 @@ def auto_patch_dt_makefile(git_work_dir: str, dt_rel_dir: str, config_var: str,
# Late to the game: if DT_DIR/overlay/Makefile exists, add it.
overlay_lines = []
DT_OVERLAY_PATH = os.path.join(dt_path, "overlay")
DT_OVERLAY_MAKEFILE_PATH = os.path.join(DT_OVERLAY_PATH, "Makefile")
overlay_lines.append("")
if os.path.isfile(DT_OVERLAY_MAKEFILE_PATH):
if incremental:
overlay_lines.append("# Armbian: Incremental: assuming overlay targets are already in the Makefile")
else:
ret["DT_OVERLAY_MAKEFILE_PATH"] = DT_OVERLAY_MAKEFILE_PATH
ret["DT_OVERLAY_PATH"] = DT_OVERLAY_PATH
overlay_lines.append("# Added by Armbian autopatcher for DT overlay")
overlay_lines.append("subdir-y := $(dts-dirs) overlay")
add_overlay_lines(dt_path, incremental, overlay_lines, ret)
# Now join the preambles, midamble, postamble and overlay stuff into a single list
new_makefile_lines = preamble_lines + midamble_lines + postamble_lines + overlay_lines
@ -191,6 +199,20 @@ def auto_patch_dt_makefile(git_work_dir: str, dt_rel_dir: str, config_var: str,
return ret
def add_overlay_lines(dt_path: str, incremental: bool, overlay_lines: list[str], ret: dict[str, str]):
DT_OVERLAY_PATH = os.path.join(dt_path, "overlay")
DT_OVERLAY_MAKEFILE_PATH = os.path.join(DT_OVERLAY_PATH, "Makefile")
overlay_lines.append("")
if os.path.isfile(DT_OVERLAY_MAKEFILE_PATH):
if incremental:
overlay_lines.append("# Armbian: Incremental: assuming overlay targets are already in the Makefile")
else:
ret["DT_OVERLAY_MAKEFILE_PATH"] = DT_OVERLAY_MAKEFILE_PATH
ret["DT_OVERLAY_PATH"] = DT_OVERLAY_PATH
overlay_lines.append("# Added by Armbian autopatcher for DT overlay")
overlay_lines.append("subdir-y := $(dts-dirs) overlay")
def copy_bare_files(autopatcher_params: AutoPatcherParams, type: str) -> list[AutomaticPatchDescription]:
ret_desc_list: list[AutomaticPatchDescription] = []
@ -284,7 +306,7 @@ def auto_patch_all_dt_makefiles(autopatcher_params: AutoPatcherParams) -> list[A
log.warning(f"Autopatching DT Makefile in {one_autopatch_config.directory} with config '{one_autopatch_config.config_var}'...")
autopatch_makefile_info = auto_patch_dt_makefile(
autopatcher_params.git_work_dir, one_autopatch_config.directory, one_autopatch_config.config_var,
autopatcher_params.all_dt_files_copied, one_autopatch_config.incremental
autopatcher_params.all_dt_files_copied, one_autopatch_config.incremental, one_autopatch_config.add_only
)
desc = AutomaticPatchDescription()

View File

@ -17,6 +17,7 @@ class PatchingAutoPatchMakefileDTConfig:
self.config_var: str = data.get("config-var", None)
self.directory: str = data.get("directory", None)
self.incremental: bool = not not data.get("incremental", False)
self.add_only: bool = not not data.get("add-only", True)
def __str__(self):
return f"PatchingAutoPatchMakefileDTConfig(config-var={self.config_var}, directory={self.directory}, incremental={self.incremental})"