quilt.mk 5.9 KB

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