quilt.mk 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 \
  62. $(CP) ./files/* $(LINUX_DIR)/; \
  63. find $(LINUX_DIR)/ -name \*.rej | xargs rm -f; \
  64. fi
  65. $(call PatchDir,$(GENERIC_PATCH_DIR),generic/)
  66. $(call PatchDir,$(PATCH_DIR),platform/)
  67. endef
  68. define Quilt/RefreshDir
  69. mkdir -p $(1)
  70. -rm -f $(1)/* 2>/dev/null >/dev/null
  71. @( \
  72. for patch in $$$$($(if $(2),grep "^$(2)",cat) $(PKG_BUILD_DIR)/patches/series | awk '{print $$$$1}'); do \
  73. $(CP) -v "$(PKG_BUILD_DIR)/patches/$$$$patch" $(1); \
  74. done; \
  75. )
  76. endef
  77. define Quilt/Refresh/Package
  78. $(call Quilt/RefreshDir,$(PATCH_DIR))
  79. endef
  80. define Quilt/Refresh/Kernel
  81. @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
  82. echo "All kernel patches must start with either generic/ or platform/"; \
  83. false; \
  84. }
  85. $(call Quilt/RefreshDir,$(GENERIC_PATCH_DIR),generic/)
  86. $(call Quilt/RefreshDir,$(PATCH_DIR),platform/)
  87. endef
  88. define Quilt/Refresh
  89. $(if $(TARGET_BUILD),$(Quilt/Refresh/Kernel),$(Quilt/Refresh/Package))
  90. endef
  91. define Build/Quilt
  92. $(STAMP_PATCHED): $(STAMP_PREPARED)
  93. @( \
  94. cd $(PKG_BUILD_DIR)/patches; \
  95. quilt pop -a -f >/dev/null 2>/dev/null; \
  96. if [ -s ".subdirs" ]; then \
  97. rm -f series; \
  98. for file in $$$$(cat .subdirs); do \
  99. if [ -f $$$$file/series ]; then \
  100. echo "Converting $$file/series"; \
  101. $$(call filter_series,$$$$file/series) | awk -v file="$$$$file/" '$$$$0 !~ /^#/ { print file $$$$0 }' | sed -e s,//,/,g >> series; \
  102. else \
  103. echo "Sorting patches in $$$$file"; \
  104. find $$$$file/* -type f \! -name series | sed -e s,//,/,g | sort >> series; \
  105. fi; \
  106. done; \
  107. else \
  108. find * -type f \! -name series | sort > series; \
  109. fi; \
  110. )
  111. touch $$@
  112. $(STAMP_CONFIGURED): $(STAMP_CHECKED) FORCE
  113. $(STAMP_CHECKED): $(STAMP_PATCHED)
  114. 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
  115. touch $$@
  116. quilt-check: $(STAMP_PREPARED) FORCE
  117. @[ -f "$(PKG_BUILD_DIR)/.quilt_used" ] || { \
  118. echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
  119. false; \
  120. }
  121. @[ -f "$(PKG_BUILD_DIR)/patches/series" ] || { \
  122. echo "The source directory contains no quilt patches."; \
  123. false; \
  124. }
  125. @[ -n "$$$$(ls $(PKG_BUILD_DIR)/patches/series)" -o "$$$$(cat $(PKG_BUILD_DIR)/patches/series | md5sum)" = "$$(sort $(PKG_BUILD_DIR)/patches/series | md5sum)" ] || { \
  126. echo "The patches are not sorted in the right order. Please fix."; \
  127. false; \
  128. }
  129. refresh: quilt-check
  130. @cd $(PKG_BUILD_DIR); quilt pop -a -f >/dev/null 2>/dev/null
  131. @cd $(PKG_BUILD_DIR); while quilt next 2>/dev/null >/dev/null && quilt push; do \
  132. quilt refresh; \
  133. done; ! quilt next 2>/dev/null >/dev/null
  134. $(Quilt/Refresh)
  135. update: quilt-check
  136. $(Quilt/Refresh)
  137. endef
  138. endif