1
0

quilt.mk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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?=./patches
  32. FILES_DIR?=./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. mkdir -p $(2)
  94. -rm -f $(2)/* 2>/dev/null >/dev/null
  95. @( \
  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. endef
  101. define Quilt/Refresh/Host
  102. $(call Quilt/RefreshDir,$(HOST_BUILD_DIR),$(HOST_PATCH_DIR))
  103. endef
  104. define Quilt/Refresh/Package
  105. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR))
  106. endef
  107. define Quilt/Refresh/Kernel
  108. @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
  109. echo "All kernel patches must start with either generic/ or platform/"; \
  110. false; \
  111. }
  112. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_BACKPORT_DIR),generic-backport/)
  113. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
  114. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_HACK_DIR),generic-hack/)
  115. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
  116. endef
  117. define Quilt/Template
  118. $($(2)STAMP_CONFIGURED): $($(2)STAMP_CHECKED)
  119. $(if $(NO_RECONFIGURE),$($(2)STAMP_BUILT),$($(2)STAMP_CONFIGURED)): FORCE
  120. $($(2)STAMP_CHECKED): $($(2)STAMP_PREPARED)
  121. if [ -s "$(1)/patches/series" ]; then \
  122. (cd "$(1)"; \
  123. if $(QUILT_CMD) next >/dev/null 2>&1; then \
  124. $(QUILT_CMD) push -a; \
  125. else \
  126. $(QUILT_CMD) top >/dev/null 2>&1; \
  127. fi \
  128. ); \
  129. fi
  130. touch "$$@"
  131. $(3)quilt-check: $($(2)STAMP_PREPARED) FORCE
  132. @[ -f "$(1)/.quilt_used" ] || { \
  133. echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
  134. false; \
  135. }
  136. @[ -f "$(1)/patches/series" ] || { \
  137. echo "The source directory contains no quilt patches."; \
  138. false; \
  139. }
  140. @[ -n "$$$$(ls $(1)/patches/series)" -o \
  141. "$$$$(cat $(1)/patches/series | $(MKHASH) md5)" = "$$(sort $(1)/patches/series | $(MKHASH) md5)" ] || { \
  142. echo "The patches are not sorted in the right order. Please fix."; \
  143. false; \
  144. }
  145. $(3)refresh: $(3)quilt-check
  146. @cd "$(1)"; $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null
  147. @cd "$(1)"; while $(QUILT_CMD) next 2>/dev/null >/dev/null && $(QUILT_CMD) push; do \
  148. QUILT_DIFF_OPTS="-p" $(QUILT_CMD) refresh -p ab --no-index --no-timestamps; \
  149. done; ! $(QUILT_CMD) next 2>/dev/null >/dev/null
  150. $(Quilt/Refresh/$(4))
  151. $(3)update: $(3)quilt-check
  152. $(Quilt/Refresh/$(4))
  153. endef
  154. Build/Quilt=$(call Quilt/Template,$(PKG_BUILD_DIR),,,$(if $(TARGET_BUILD),Kernel,Package))
  155. Host/Quilt=$(call Quilt/Template,$(HOST_BUILD_DIR),HOST_,host-,Host)
  156. endif