quilt.mk 4.7 KB

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