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