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