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