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