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