quilt.mk 4.6 KB

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