quilt.mk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ifneq ($(PKG_BUILD_DIR),)
  50. QUILT?=$(if $(wildcard $(PKG_BUILD_DIR)/.quilt_used),y)
  51. ifneq ($(QUILT),)
  52. STAMP_PATCHED:=$(PKG_BUILD_DIR)/.quilt_patched
  53. STAMP_CHECKED:=$(PKG_BUILD_DIR)/.quilt_checked
  54. override CONFIG_AUTOREBUILD=
  55. prepare: $(STAMP_PATCHED)
  56. quilt-check: $(STAMP_CHECKED)
  57. endif
  58. endif
  59. ifneq ($(HOST_BUILD_DIR),)
  60. HOST_QUILT?=$(if $(wildcard $(if $(PKG_BUILD_DIR),$(PKG_BUILD_DIR),$(HOST_BUILD_DIR))/.quilt_used),y)
  61. ifneq ($(HOST_QUILT),)
  62. HOST_STAMP_PATCHED:=$(HOST_BUILD_DIR)/.quilt_patched
  63. HOST_STAMP_CHECKED:=$(HOST_BUILD_DIR)/.quilt_checked
  64. override CONFIG_AUTOREBUILD=
  65. host-prepare: $(HOST_STAMP_PATCHED)
  66. host-quilt-check: $(HOST_STAMP_CHECKED)
  67. endif
  68. endif
  69. define Host/Patch/Default
  70. $(if $(QUILT),rm -rf $(HOST_BUILD_DIR)/patches; mkdir -p $(HOST_BUILD_DIR)/patches)
  71. $(call PatchDir,$(HOST_BUILD_DIR),$(HOST_PATCH_DIR),)
  72. $(if $(QUILT),touch $(HOST_BUILD_DIR)/.quilt_used)
  73. endef
  74. define Build/Patch/Default
  75. $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches)
  76. $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),)
  77. $(if $(QUILT),touch $(PKG_BUILD_DIR)/.quilt_used)
  78. endef
  79. kernel_files=$(foreach fdir,$(GENERIC_FILES_DIR) $(FILES_DIR),$(fdir)/.)
  80. define Kernel/Patch/Default
  81. rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches
  82. $(if $(kernel_files),$(CP) $(kernel_files) $(LINUX_DIR)/)
  83. find $(LINUX_DIR)/ -name \*.rej -or -name \*.orig | $(XARGS) rm -f
  84. $(call PatchDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
  85. $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
  86. endef
  87. define Quilt/RefreshDir
  88. mkdir -p $(2)
  89. -rm -f $(2)/* 2>/dev/null >/dev/null
  90. @( \
  91. for patch in $$$$($(if $(3),grep "^$(3)",cat) $(PKG_BUILD_DIR)/patches/series | awk '{print $$$$1}'); do \
  92. $(CP) -v "$(PKG_BUILD_DIR)/patches/$$$$patch" $(2); \
  93. done; \
  94. )
  95. endef
  96. define Quilt/Refresh/Host
  97. $(call Quilt/RefreshDir,$(HOST_BUILD_DIR),$(PATCH_DIR))
  98. endef
  99. define Quilt/Refresh/Package
  100. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR))
  101. endef
  102. define Quilt/Refresh/Kernel
  103. @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
  104. echo "All kernel patches must start with either generic/ or platform/"; \
  105. false; \
  106. }
  107. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
  108. $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
  109. endef
  110. define Quilt/Template
  111. $($(2)STAMP_PATCHED): $($(2)STAMP_PREPARED)
  112. @( \
  113. cd $(1)/patches; \
  114. $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null; \
  115. if [ -s ".subdirs" ]; then \
  116. rm -f series; \
  117. for file in $$$$(cat .subdirs); do \
  118. if [ -f $$$$file/series ]; then \
  119. echo "Converting $$file/series"; \
  120. $$(call filter_series,$$$$file/series) | awk -v file="$$$$file/" '$$$$0 !~ /^#/ { print file $$$$0 }' | sed -e s,//,/,g >> series; \
  121. else \
  122. echo "Sorting patches in $$$$file"; \
  123. find $$$$file/* -type f \! -name series | sed -e s,//,/,g | sort >> series; \
  124. fi; \
  125. done; \
  126. else \
  127. find * -type f \! -name series | sort > series; \
  128. fi; \
  129. )
  130. touch "$$@"
  131. $($(2)STAMP_CONFIGURED): $($(2)STAMP_CHECKED) FORCE
  132. $($(2)STAMP_CHECKED): $($(2)STAMP_PATCHED)
  133. if [ -s "$(1)/patches/series" ]; then \
  134. (cd "$(1)"; \
  135. if $(QUILT_CMD) next >/dev/null 2>&1; then \
  136. $(QUILT_CMD) push -a; \
  137. else \
  138. $(QUILT_CMD) top >/dev/null 2>&1; \
  139. fi \
  140. ); \
  141. fi
  142. touch "$$@"
  143. $(3)quilt-check: $($(2)STAMP_PREPARED) FORCE
  144. @[ -f "$(1)/.quilt_used" ] || { \
  145. echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
  146. false; \
  147. }
  148. @[ -f "$(1)/patches/series" ] || { \
  149. echo "The source directory contains no quilt patches."; \
  150. false; \
  151. }
  152. @[ -n "$$$$(ls $(1)/patches/series)" -o "$$$$(cat $(1)/patches/series | md5sum)" = "$$(sort $(1)/patches/series | md5sum)" ] || { \
  153. echo "The patches are not sorted in the right order. Please fix."; \
  154. false; \
  155. }
  156. $(3)refresh: $(3)quilt-check
  157. @cd "$(1)"; $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null
  158. @cd "$(1)"; while $(QUILT_CMD) next 2>/dev/null >/dev/null && $(QUILT_CMD) push; do \
  159. QUILT_DIFF_OPTS="-p" $(QUILT_CMD) refresh -p ab --no-index --no-timestamps; \
  160. done; ! $(QUILT_CMD) next 2>/dev/null >/dev/null
  161. $(Quilt/Refresh/$(4))
  162. $(3)update: $(3)quilt-check
  163. $(Quilt/Refresh/$(4))
  164. endef
  165. Build/Quilt=$(call Quilt/Template,$(PKG_BUILD_DIR),,,$(if $(TARGET_BUILD),Kernel,Package))
  166. Host/Quilt=$(call Quilt/Template,$(HOST_BUILD_DIR),HOST_,host-,Host)
  167. endif