quilt.mk 6.0 KB

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