quilt.mk 5.8 KB

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