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