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