build system refactoring in preparation for allowing packages to do host-build steps
[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 endef
83
84 define Build/Patch/Default
85         $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches)
86         $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),)
87 endef
88
89 kernel_files=$(foreach fdir,$(GENERIC_FILES_DIR) $(FILES_DIR),$(fdir)/.)
90 define Kernel/Patch/Default
91         rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches
92         $(if $(kernel_files),$(CP) $(kernel_files) $(LINUX_DIR)/)
93         find $(LINUX_DIR)/ -name \*.rej -or -name \*.orig | $(XARGS) rm -f
94         $(call PatchDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
95         $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
96 endef
97
98 define Quilt/RefreshDir
99         mkdir -p $(2)
100         -rm -f $(2)/* 2>/dev/null >/dev/null
101         @( \
102                 for patch in $$$$($(if $(3),grep "^$(3)",cat) $(PKG_BUILD_DIR)/patches/series | awk '{print $$$$1}'); do \
103                         $(CP) -v "$(PKG_BUILD_DIR)/patches/$$$$patch" $(2); \
104                 done; \
105         )
106 endef
107
108 define Quilt/Refresh/Host
109         $(call Quilt/RefreshDir,$(HOST_BUILD_DIR),$(PATCH_DIR))
110 endef
111
112 define Quilt/Refresh/Package
113         $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR))
114 endef
115
116 define Quilt/Refresh/Kernel
117         @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
118                 echo "All kernel patches must start with either generic/ or platform/"; \
119                 false; \
120         }
121         $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
122         $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
123 endef
124
125 define Build/Quilt
126   $($(2)STAMP_PATCHED): $($(2)STAMP_PREPARED)
127         @( \
128                 cd $(1)/patches; \
129                 $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null; \
130                 if [ -s ".subdirs" ]; then \
131                         rm -f series; \
132                         for file in $$$$(cat .subdirs); do \
133                                 if [ -f $$$$file/series ]; then \
134                                         echo "Converting $$file/series"; \
135                                         $$(call filter_series,$$$$file/series) | awk -v file="$$$$file/" '$$$$0 !~ /^#/ { print file $$$$0 }' | sed -e s,//,/,g >> series; \
136                                 else \
137                                         echo "Sorting patches in $$$$file"; \
138                                         find $$$$file/* -type f \! -name series | sed -e s,//,/,g | sort >> series; \
139                                 fi; \
140                         done; \
141                 else \
142                         find * -type f \! -name series | sort > series; \
143                 fi; \
144         )
145         touch "$$@"
146
147   $($(2)STAMP_CONFIGURED): $($(2)STAMP_CHECKED) FORCE
148   $($(2)STAMP_CHECKED): $($(2)STAMP_PATCHED)
149         if [ -s "$(1)/patches/series" ]; then \
150                 (cd "$(1)"; \
151                         if $(QUILT_CMD) next >/dev/null 2>&1; then \
152                                 $(QUILT_CMD) push -a; \
153                         else \
154                                 $(QUILT_CMD) top >/dev/null 2>&1; \
155                         fi \
156                 ); \
157         fi
158         touch "$$@"
159
160   $(3)quilt-check: $($(2)STAMP_PREPARED) FORCE
161         @[ -f "$(1)/.quilt_used" ] || { \
162                 echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
163                 false; \
164         }
165         @[ -f "$(1)/patches/series" ] || { \
166                 echo "The source directory contains no quilt patches."; \
167                 false; \
168         }
169         @[ -n "$$$$(ls $(1)/patches/series)" -o "$$$$(cat $(1)/patches/series | md5sum)" = "$$(sort $(1)/patches/series | md5sum)" ] || { \
170                 echo "The patches are not sorted in the right order. Please fix."; \
171                 false; \
172         }
173
174   $(3)refresh: $(3)quilt-check
175         @cd "$(1)"; $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null
176         @cd "$(1)"; while $(QUILT_CMD) next 2>/dev/null >/dev/null && $(QUILT_CMD) push; do \
177                 QUILT_DIFF_OPTS="-p" $(QUILT_CMD) refresh -p ab --no-index --no-timestamps; \
178         done; ! $(QUILT_CMD) next 2>/dev/null >/dev/null
179         $(Quilt/Refresh/$(4))
180         
181   $(3)update: $(3)quilt-check
182         $(Quilt/Refresh/$(4))
183 endef
184
185 Build/Quilt=$(call Quilt/Template,$(PKG_BUILD_DIR),,,$(if $(TARGET_BUILD),Kernel,Package))
186 Host/Quilt=$(call Quilt/Template,$(HOST_BUILD_DIR),HOST_,host-,Host)
187
188 endif