b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # |
| 3 | # Copyright (C) 2007-2020 OpenWrt.org |
| 4 | |
| 5 | ifeq ($(TARGET_BUILD),1) |
| 6 | PKG_BUILD_DIR:=$(LINUX_DIR) |
| 7 | endif |
| 8 | |
| 9 | ifneq ($(filter host-refresh refresh,$(MAKECMDGOALS)),) |
| 10 | override QUILT=1 |
| 11 | override HOST_QUILT=1 |
| 12 | endif |
| 13 | |
| 14 | ifneq ($(PKG_BUILD_DIR),) |
| 15 | QUILT?=$(if $(wildcard $(PKG_BUILD_DIR)/.quilt_used),y) |
| 16 | ifneq ($(QUILT),) |
| 17 | STAMP_CHECKED:=$(PKG_BUILD_DIR)/.quilt_checked |
| 18 | override CONFIG_AUTOREBUILD= |
| 19 | override CONFIG_AUTOREMOVE= |
| 20 | quilt-check: $(STAMP_CHECKED) |
| 21 | endif |
| 22 | endif |
| 23 | |
| 24 | ifneq ($(HOST_BUILD_DIR),) |
| 25 | HOST_QUILT?=$(if $(findstring command,$(origin QUILT)),$(QUILT),$(if $(wildcard $(HOST_BUILD_DIR)/.quilt_used),y)) |
| 26 | ifneq ($(HOST_QUILT),) |
| 27 | HOST_STAMP_CHECKED:=$(HOST_BUILD_DIR)/.quilt_checked |
| 28 | override CONFIG_AUTOREBUILD= |
| 29 | override CONFIG_AUTOREMOVE= |
| 30 | host-quilt-check: $(HOST_STAMP_CHECKED) |
| 31 | endif |
| 32 | endif |
| 33 | |
| 34 | ifneq ($(if $(DUMP),1,$(__quilt_inc)),1) |
| 35 | __quilt_inc:=1 |
| 36 | |
| 37 | PATCH_DIR?=$(CURDIR)/patches |
| 38 | FILES_DIR?=$(CURDIR)/files |
| 39 | HOST_PATCH_DIR?=$(PATCH_DIR) |
| 40 | HOST_FILES_DIR?=$(FILES_DIR) |
| 41 | |
| 42 | QUILT_CMD:=quilt --quiltrc=- |
| 43 | |
| 44 | define filter_series |
| 45 | sed -e s,\\\#.*,, $(1) | grep -E \[a-zA-Z0-9\] |
| 46 | endef |
| 47 | |
| 48 | define PatchDir/Quilt |
| 49 | @mkdir -p "$(1)/patches$(if $(3),/$(patsubst %/,%,$(3)))" |
| 50 | @if [ -s "$(2)/series" ]; then \ |
| 51 | mkdir -p "$(1)/patches/$(3)"; \ |
| 52 | cp "$(2)/series" "$(1)/patches/$(3)"; \ |
| 53 | fi |
| 54 | @for patch in $$$$( (cd "$(2)" && if [ -f series ]; then $(call filter_series,series); else ls | sort; fi; ) 2>/dev/null ); do ( \ |
| 55 | cp "$(2)/$$$$patch" "$(1)/patches/$(3)"; \ |
| 56 | echo "$(3)$$$$patch" >> "$(1)/patches/series"; \ |
| 57 | ); done |
| 58 | $(if $(3),@echo $(3) >> "$(1)/patches/.subdirs") |
| 59 | endef |
| 60 | |
| 61 | define PatchDir/Default |
| 62 | @if [ -d "$(2)" ] && [ "$$$$(ls $(2) | wc -l)" -gt 0 ]; then \ |
| 63 | export PATCH="$(PATCH)"; \ |
| 64 | if [ -s "$(2)/series" ]; then \ |
| 65 | $(call filter_series,$(2)/series) | xargs -n1 \ |
| 66 | $(KPATCH) "$(1)" "$(2)"; \ |
| 67 | else \ |
| 68 | $(KPATCH) "$(1)" "$(2)"; \ |
| 69 | fi; \ |
| 70 | fi |
| 71 | endef |
| 72 | |
| 73 | define PatchDir |
| 74 | $(call PatchDir/$(if $(strip $(QUILT)),Quilt,Default),$(strip $(1)),$(strip $(2)),$(strip $(3))) |
| 75 | endef |
| 76 | |
| 77 | define HostPatchDir |
| 78 | $(call PatchDir/$(if $(strip $(HOST_QUILT)),Quilt,Default),$(strip $(1)),$(strip $(2)),$(strip $(3))) |
| 79 | endef |
| 80 | |
| 81 | define Host/Patch/Default |
| 82 | $(if $(HOST_QUILT),rm -rf $(HOST_BUILD_DIR)/patches; mkdir -p $(HOST_BUILD_DIR)/patches) |
| 83 | $(call HostPatchDir,$(HOST_BUILD_DIR),$(HOST_PATCH_DIR),) |
| 84 | $(if $(HOST_QUILT),touch $(HOST_BUILD_DIR)/.quilt_used) |
| 85 | endef |
| 86 | |
| 87 | define Build/Patch/Default |
| 88 | $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches) |
| 89 | $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),) |
| 90 | $(if $(QUILT),touch $(PKG_BUILD_DIR)/.quilt_used) |
| 91 | endef |
| 92 | |
| 93 | kernel_files=$(foreach fdir,$(GENERIC_FILES_DIR) $(FILES_DIR),$(fdir)/.) |
| 94 | define Kernel/Patch/Default |
| 95 | $(if $(QUILT),rm -rf $(LINUX_DIR)/patches; mkdir -p $(LINUX_DIR)/patches) |
| 96 | $(if $(kernel_files),$(CP) $(kernel_files) $(LINUX_DIR)/) |
| 97 | find $(LINUX_DIR)/ -name \*.rej -or -name \*.orig | $(XARGS) rm -f |
| 98 | if [ -d $(GENERIC_PLATFORM_DIR)/patches$(if $(wildcard $(GENERIC_PLATFORM_DIR)/patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER)) ]; then \ |
| 99 | echo "generic patches directory is present. please move your patches to the pending directory" ; \ |
| 100 | exit 1; \ |
| 101 | fi |
| 102 | $(call PatchDir,$(LINUX_DIR),$(GENERIC_BACKPORT_DIR),generic-backport/) |
| 103 | $(call PatchDir,$(LINUX_DIR),$(GENERIC_PATCH_DIR),generic/) |
| 104 | $(call PatchDir,$(LINUX_DIR),$(GENERIC_HACK_DIR),generic-hack/) |
| 105 | $(call PatchDir,$(LINUX_DIR),$(PATCH_DIR),platform/) |
| 106 | endef |
| 107 | |
| 108 | define Quilt/RefreshDir |
| 109 | -rm -rf $(2) 2>/dev/null >/dev/null |
| 110 | [ -f $(1)/.quilt_no_patch ] || mkdir -p $(2) |
| 111 | @[ -f $(1)/.quilt_no_patch ] || { \ |
| 112 | for patch in $$$$($(if $(3),grep "^$(3)",cat) $(1)/patches/series | awk '{print $$$$1}'); do \ |
| 113 | $(CP) -v "$(1)/patches/$$$$patch" $(2); \ |
| 114 | done; \ |
| 115 | } |
| 116 | @-rm -f $(1)/.quilt_no_patch 2>/dev/null >/dev/null; |
| 117 | endef |
| 118 | |
| 119 | define Quilt/Refresh/Host |
| 120 | $(call Quilt/RefreshDir,$(HOST_BUILD_DIR),$(HOST_PATCH_DIR)) |
| 121 | endef |
| 122 | |
| 123 | define Quilt/Refresh/Package |
| 124 | $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR)) |
| 125 | endef |
| 126 | |
| 127 | define Quilt/Refresh/Kernel |
| 128 | @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \ |
| 129 | echo "All kernel patches must start with either generic/ or platform/"; \ |
| 130 | false; \ |
| 131 | } |
| 132 | $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_BACKPORT_DIR),generic-backport/) |
| 133 | $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/) |
| 134 | $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_HACK_DIR),generic-hack/) |
| 135 | $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/) |
| 136 | endef |
| 137 | |
| 138 | define Quilt/Template |
| 139 | $($(2)STAMP_CONFIGURED): $($(2)STAMP_CHECKED) |
| 140 | $(if $(NO_RECONFIGURE),$($(2)STAMP_BUILT),$($(2)STAMP_CONFIGURED)): FORCE |
| 141 | $($(2)STAMP_CHECKED): $($(2)STAMP_PREPARED) |
| 142 | if [ -s "$(1)/patches/series" ]; then \ |
| 143 | (cd "$(1)"; \ |
| 144 | if $(QUILT_CMD) next >/dev/null 2>&1; then \ |
| 145 | $(QUILT_CMD) push -a; \ |
| 146 | else \ |
| 147 | $(QUILT_CMD) top >/dev/null 2>&1; \ |
| 148 | fi \ |
| 149 | ); \ |
| 150 | fi |
| 151 | touch "$$@" |
| 152 | |
| 153 | $(3)quilt-check: $($(2)STAMP_PREPARED) FORCE |
| 154 | @[ -f "$(1)/.quilt_used" ] || { \ |
| 155 | echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \ |
| 156 | false; \ |
| 157 | } |
| 158 | @[ -f "$(1)/patches/series" ] || { \ |
| 159 | echo "The source directory contains no quilt patches."; \ |
| 160 | touch $(1)/patches/series $(1)/.quilt_no_patch; \ |
| 161 | } |
| 162 | @[ -n "$$$$(ls $(1)/patches/series)" -o \ |
| 163 | "$$$$(cat $(1)/patches/series | $(MKHASH) md5)" = "$$(sort $(1)/patches/series | $(MKHASH) md5)" ] || { \ |
| 164 | echo "The patches are not sorted in the right order. Please fix."; \ |
| 165 | false; \ |
| 166 | } |
| 167 | |
| 168 | $(3)refresh: $(3)quilt-check |
| 169 | @[ -f $(1)/.quilt_no_patch ] || { \ |
| 170 | cd "$(1)"; $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null; \ |
| 171 | while $(QUILT_CMD) next 2>/dev/null >/dev/null && $(QUILT_CMD) push; do \ |
| 172 | QUILT_DIFF_OPTS="-p" $(QUILT_CMD) refresh -p ab --no-index --no-timestamps; \ |
| 173 | done; ! $(QUILT_CMD) next 2>/dev/null >/dev/null; \ |
| 174 | } |
| 175 | $(Quilt/Refresh/$(4)) |
| 176 | |
| 177 | $(3)update: $(3)quilt-check |
| 178 | $(Quilt/Refresh/$(4)) |
| 179 | endef |
| 180 | |
| 181 | Build/Quilt=$(call Quilt/Template,$(PKG_BUILD_DIR),,,$(if $(TARGET_BUILD),Kernel,Package)) |
| 182 | Host/Quilt=$(call Quilt/Template,$(HOST_BUILD_DIR),HOST_,host-,Host) |
| 183 | |
| 184 | endif |