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