move a stampfile to make it easier to override the patch template
[openwrt.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 endef
63
64 define Kernel/Patch/Default
65         $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches)
66         if [ -d $(GENERIC_PLATFORM_DIR)/files ]; then $(CP) $(GENERIC_PLATFORM_DIR)/files/* $(LINUX_DIR)/; fi
67         if [ -d ./files ]; then $(CP) ./files/* $(LINUX_DIR)/; fi
68         $(call PatchDir,$(GENERIC_PATCH_DIR),generic/)
69         $(call PatchDir,$(PATCH_DIR),platform/)
70 endef
71
72 $(STAMP_PATCHED): $(STAMP_PREPARED)
73         @( \
74                 cd $(PKG_BUILD_DIR)/patches; \
75                 quilt pop -a -f >/dev/null 2>/dev/null; \
76                 if [ -s ".subdirs" ]; then \
77                         rm -f series; \
78                         for file in $$(cat .subdirs); do \
79                                 if [ -f $$file/series ]; then \
80                                         echo "Converting $$file/series"; \
81                                         awk -v file="$$file/" '$$0 !~ /^#/ { print file $$0 }' $$file/series >> series; \
82                                 else \
83                                         echo "Sorting patches in $$file"; \
84                                         find $$file/* -type f \! -name series | sort >> series; \
85                                 fi; \
86                         done; \
87                 else \
88                         find * -type f \! -name series | sort > series; \
89                 fi; \
90         )
91         if [ -s "$(PKG_BUILD_DIR)/patches/series" ]; then (cd $(PKG_BUILD_DIR); quilt push -a); fi
92         touch $@
93
94 define Quilt/RefreshDir
95         mkdir -p $(1)
96         -rm -f $(1)/* 2>/dev/null >/dev/null
97         @( \
98                 for patch in $$($(if $(2),grep "^$(2)",cat) $(PKG_BUILD_DIR)/patches/series | awk '{print $$1}'); do \
99                         $(CP) -v "$(PKG_BUILD_DIR)/patches/$$patch" $(1); \
100                 done; \
101         )
102 endef
103
104 define Quilt/Refresh/Package
105         $(call Quilt/RefreshDir,$(PATCH_DIR))
106 endef
107
108 define Quilt/Refresh/Kernel
109         @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
110                 echo "All kernel patches must start with either generic/ or platform/"; \
111                 false; \
112         }
113         $(call Quilt/RefreshDir,$(GENERIC_PATCH_DIR),generic/)
114         $(call Quilt/RefreshDir,$(PATCH_DIR),platform/)
115 endef
116
117 quilt-check: $(STAMP_PREPARED) FORCE
118         @[ -f "$(PKG_BUILD_DIR)/.quilt_used" ] || { \
119                 echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
120                 false; \
121         }
122         @[ -f "$(PKG_BUILD_DIR)/patches/series" ] || { \
123                 echo "The source directory contains no quilt patches."; \
124                 false; \
125         }
126         @[ "$$(cat $(PKG_BUILD_DIR)/patches/series | md5sum)" = "$$(sort $(PKG_BUILD_DIR)/patches/series | md5sum)" ] || { \
127                 echo "The patches are not sorted in the right order. Please fix."; \
128                 false; \
129         }
130
131 refresh: quilt-check
132         @cd $(PKG_BUILD_DIR); quilt pop -a -f >/dev/null 2>/dev/null
133         @cd $(PKG_BUILD_DIR); while quilt next 2>/dev/null >/dev/null && quilt push; do \
134                 quilt refresh; \
135         done; ! quilt next 2>/dev/null >/dev/null
136         $(if $(KERNEL_BUILD),$(Quilt/Refresh/Kernel),$(Quilt/Refresh/Package))
137         
138 update: quilt-check
139         $(if $(KERNEL_BUILD),$(Quilt/Refresh/Kernel),$(Quilt/Refresh/Package))
140
141 endif