4dce2a6d8ca2ef59312ab255d8c3100b1a2ce941
[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 Quilt/Patch
21         @for patch in $$$$( (cd $(1) && if [ -f series ]; then grep -v '^#' series; else ls; fi; ) 2>/dev/null ); do ( \
22                 cp "$(1)/$$$$patch" $(PKG_BUILD_DIR); \
23                 cd $(PKG_BUILD_DIR); \
24                 quilt import -P$(2)$$$$patch -p 1 "$$$$patch"; \
25                 quilt push -f >/dev/null 2>/dev/null; \
26                 rm -f "$$$$patch"; \
27         ); done
28 endef
29
30 QUILT?=$(strip $(shell test -f $(PKG_BUILD_DIR)/.quilt_used && echo y))
31 ifneq ($(QUILT),)
32   STAMP_PATCHED:=$(PKG_BUILD_DIR)/.quilt_patched
33   override CONFIG_AUTOREBUILD=
34   define Build/Patch/Default
35         rm -rf $(PKG_BUILD_DIR)/patches
36         mkdir -p $(PKG_BUILD_DIR)/patches
37         $(call Quilt/Patch,$(PATCH_DIR),)
38         @echo
39         touch $(PKG_BUILD_DIR)/.quilt_used
40   endef
41   $(STAMP_CONFIGURED): $(STAMP_PATCHED) FORCE
42   prepare: $(STAMP_PATCHED)
43   quilt-check: $(STAMP_PATCHED)
44 else
45   define Build/Patch/Default
46         @if [ -d $(PATCH_DIR) -a "$$$$(ls $(PATCH_DIR) | wc -l)" -gt 0 ]; then \
47                 $(PATCH) $(PKG_BUILD_DIR) $(PATCH_DIR); \
48         fi
49   endef
50 endif
51
52 define Kernel/Patch/Default
53         if [ -d $(GENERIC_PLATFORM_DIR)/files ]; then $(CP) $(GENERIC_PLATFORM_DIR)/files/* $(LINUX_DIR)/; fi
54         if [ -d ./files ]; then $(CP) ./files/* $(LINUX_DIR)/; fi
55         $(if $(strip $(QUILT)),$(call Quilt/Patch,$(GENERIC_PATCH_DIR),generic/), \
56                 if [ -d $(GENERIC_PATCH_DIR) ]; then $(PATCH) $(LINUX_DIR) $(GENERIC_PATCH_DIR); fi \
57         )
58         $(if $(strip $(QUILT)),$(call Quilt/Patch,$(PATCH_DIR),platform/), \
59                 if [ -d $(PATCH_DIR) ]; then $(PATCH) $(LINUX_DIR) $(PATCH_DIR); fi \
60         )
61         $(if $(strip $(QUILT)),touch $(PKG_BUILD_DIR)/.quilt_used)
62 endef
63
64 ifeq ($(TARGET_BUILD),1)
65 $(STAMP_PATCHED): $(STAMP_PREPARED)
66         @cd $(PKG_BUILD_DIR); quilt pop -a -f >/dev/null 2>/dev/null || true
67         (\
68                 cd $(PKG_BUILD_DIR)/patches; \
69                 rm -f series; \
70                 for file in *; do \
71                         if [ -f $$file/series ]; then \
72                                 echo "Converting $$file/series"; \
73                                 awk -v file="$$file/" '$$0 !~ /^#/ { print file $$0 }' $$file/series >> series; \
74                         else \
75                                 echo "Sorting patches in $$file"; \
76                                 find $$file/* -type f \! -name series | sort >> series; \
77                         fi; \
78                 done; \
79         )
80         if [ -s "$(PKG_BUILD_DIR)/patches/series" ]; then (cd $(PKG_BUILD_DIR); quilt push -a); fi
81         touch $@
82 else
83 $(STAMP_PATCHED): $(STAMP_PREPARED)
84         @cd $(PKG_BUILD_DIR); quilt pop -a -f >/dev/null 2>/dev/null || true
85         (\
86                 cd $(PKG_BUILD_DIR)/patches; \
87                 find * -type f \! -name series | sort > series; \
88         )
89         if [ -s "$(PKG_BUILD_DIR)/patches/series" ]; then (cd $(PKG_BUILD_DIR); quilt push -a); fi
90         touch $@
91 endif
92
93 define Quilt/RefreshDir
94         mkdir -p $(1)
95         -rm -f $(1)/* 2>/dev/null >/dev/null
96         @( \
97                 for patch in $$($(if $(2),grep "^$(2)",cat) $(PKG_BUILD_DIR)/patches/series | awk '{print $$1}'); do \
98                         $(CP) -v "$(PKG_BUILD_DIR)/patches/$$patch" $(1); \
99                 done; \
100         )
101 endef
102
103 define Quilt/Refresh/Package
104         $(call Quilt/RefreshDir,$(PATCH_DIR))
105 endef
106
107 define Quilt/Refresh/Kernel
108         @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
109                 echo "All kernel patches must start with either generic/ or platform/"; \
110                 false; \
111         }
112         $(call Quilt/RefreshDir,$(GENERIC_PATCH_DIR),generic/)
113         $(call Quilt/RefreshDir,$(PATCH_DIR),platform/)
114 endef
115
116 quilt-check: $(STAMP_PREPARED) FORCE
117         @[ -f "$(PKG_BUILD_DIR)/.quilt_used" ] || { \
118                 echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
119                 false; \
120         }
121         @[ -f "$(PKG_BUILD_DIR)/patches/series" ] || { \
122                 echo "The source directory contains no quilt patches."; \
123                 false; \
124         }
125         @[ "$$(cat $(PKG_BUILD_DIR)/patches/series | md5sum)" = "$$(sort $(PKG_BUILD_DIR)/patches/series | md5sum)" ] || { \
126                 echo "The patches are not sorted in the right order. Please fix."; \
127                 false; \
128         }
129
130 refresh: quilt-check
131         @cd $(PKG_BUILD_DIR); quilt pop -a -f >/dev/null 2>/dev/null
132         @cd $(PKG_BUILD_DIR); while quilt next 2>/dev/null >/dev/null && quilt push; do \
133                 quilt refresh; \
134         done; ! quilt next 2>/dev/null >/dev/null
135         $(if $(KERNEL_BUILD),$(Quilt/Refresh/Kernel),$(Quilt/Refresh/Package))
136         
137 update: quilt-check
138         $(if $(KERNEL_BUILD),$(Quilt/Refresh/Kernel),$(Quilt/Refresh/Package))
139
140 endif