binutils: backport an upstream fix for a linker bug that triggers with LTO
[openwrt/openwrt.git] / include / quilt.mk
1 #
2 # Copyright (C) 2007-2009 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6
7 ifeq ($(TARGET_BUILD),1)
8 PKG_BUILD_DIR:=$(LINUX_DIR)
9 endif
10
11 ifneq ($(filter host-refresh refresh,$(MAKECMDGOALS)),)
12 override QUILT=1
13 override HOST_QUILT=1
14 endif
15
16 ifneq ($(PKG_BUILD_DIR),)
17 QUILT?=$(if $(wildcard $(PKG_BUILD_DIR)/.quilt_used),y)
18 ifneq ($(QUILT),)
19 STAMP_CHECKED:=$(PKG_BUILD_DIR)/.quilt_checked
20 override CONFIG_AUTOREBUILD=
21 override CONFIG_AUTOREMOVE=
22 quilt-check: $(STAMP_CHECKED)
23 endif
24 endif
25
26 ifneq ($(HOST_BUILD_DIR),)
27 HOST_QUILT?=$(if $(findstring command,$(origin QUILT)),$(QUILT),$(if $(wildcard $(HOST_BUILD_DIR)/.quilt_used),y))
28 ifneq ($(HOST_QUILT),)
29 HOST_STAMP_CHECKED:=$(HOST_BUILD_DIR)/.quilt_checked
30 override CONFIG_AUTOREBUILD=
31 override CONFIG_AUTOREMOVE=
32 host-quilt-check: $(HOST_STAMP_CHECKED)
33 endif
34 endif
35
36 ifneq ($(if $(DUMP),1,$(__quilt_inc)),1)
37 __quilt_inc:=1
38
39 PATCH_DIR?=./patches
40 FILES_DIR?=./files
41 HOST_PATCH_DIR?=$(PATCH_DIR)
42 HOST_FILES_DIR?=$(FILES_DIR)
43
44 QUILT_CMD:=quilt --quiltrc=-
45
46 define filter_series
47 sed -e s,\\\#.*,, $(1) | grep -E \[a-zA-Z0-9\]
48 endef
49
50 define PatchDir/Quilt
51 @mkdir -p "$(1)/patches$(if $(3),/$(patsubst %/,%,$(3)))"
52 @if [ -s "$(2)/series" ]; then \
53 mkdir -p "$(1)/patches/$(3)"; \
54 cp "$(2)/series" "$(1)/patches/$(3)"; \
55 fi
56 @for patch in $$$$( (cd "$(2)" && if [ -f series ]; then $(call filter_series,series); else ls | sort; fi; ) 2>/dev/null ); do ( \
57 cp "$(2)/$$$$patch" "$(1)/patches/$(3)"; \
58 echo "$(3)$$$$patch" >> "$(1)/patches/series"; \
59 ); done
60 $(if $(3),@echo $(3) >> "$(1)/patches/.subdirs")
61 endef
62
63 define PatchDir/Default
64 @if [ -d "$(2)" ] && [ "$$$$(ls $(2) | wc -l)" -gt 0 ]; then \
65 export PATCH="$(PATCH)"; \
66 if [ -s "$(2)/series" ]; then \
67 $(call filter_series,$(2)/series) | xargs -n1 \
68 $(KPATCH) "$(1)" "$(2)"; \
69 else \
70 $(KPATCH) "$(1)" "$(2)"; \
71 fi; \
72 fi
73 endef
74
75 define PatchDir
76 $(call PatchDir/$(if $(strip $(QUILT)),Quilt,Default),$(strip $(1)),$(strip $(2)),$(strip $(3)))
77 endef
78
79 define HostPatchDir
80 $(call PatchDir/$(if $(strip $(HOST_QUILT)),Quilt,Default),$(strip $(1)),$(strip $(2)),$(strip $(3)))
81 endef
82
83 define Host/Patch/Default
84 $(if $(HOST_QUILT),rm -rf $(HOST_BUILD_DIR)/patches; mkdir -p $(HOST_BUILD_DIR)/patches)
85 $(call HostPatchDir,$(HOST_BUILD_DIR),$(HOST_PATCH_DIR),)
86 $(if $(HOST_QUILT),touch $(HOST_BUILD_DIR)/.quilt_used)
87 endef
88
89 define Build/Patch/Default
90 $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches)
91 $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),)
92 $(if $(QUILT),touch $(PKG_BUILD_DIR)/.quilt_used)
93 endef
94
95 kernel_files=$(foreach fdir,$(GENERIC_FILES_DIR) $(FILES_DIR),$(fdir)/.)
96 define Kernel/Patch/Default
97 $(if $(QUILT),rm -rf $(PKG_BUILD_DIR)/patches; mkdir -p $(PKG_BUILD_DIR)/patches)
98 $(if $(kernel_files),$(CP) $(kernel_files) $(LINUX_DIR)/)
99 find $(LINUX_DIR)/ -name \*.rej -or -name \*.orig | $(XARGS) rm -f
100 if [ -d $(GENERIC_PLATFORM_DIR)/patches$(if $(wildcard $(GENERIC_PLATFORM_DIR)/patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER)) ]; then \
101 echo "generic patches directory is present. please move your patches to the pending directory" ; \
102 exit 1; \
103 fi
104 $(call PatchDir,$(PKG_BUILD_DIR),$(GENERIC_BACKPORT_DIR),generic-backport/)
105 $(call PatchDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
106 $(call PatchDir,$(PKG_BUILD_DIR),$(GENERIC_HACK_DIR),generic-hack/)
107 $(call PatchDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
108 endef
109
110 define Quilt/RefreshDir
111 mkdir -p $(2)
112 -rm -f $(2)/* 2>/dev/null >/dev/null
113 @( \
114 for patch in $$$$($(if $(3),grep "^$(3)",cat) $(1)/patches/series | awk '{print $$$$1}'); do \
115 $(CP) -v "$(1)/patches/$$$$patch" $(2); \
116 done; \
117 )
118 endef
119
120 define Quilt/Refresh/Host
121 $(call Quilt/RefreshDir,$(HOST_BUILD_DIR),$(PATCH_DIR))
122 endef
123
124 define Quilt/Refresh/Package
125 $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR))
126 endef
127
128 define Quilt/Refresh/Kernel
129 @[ -z "$$(grep -v '^generic/' $(PKG_BUILD_DIR)/patches/series | grep -v '^platform/')" ] || { \
130 echo "All kernel patches must start with either generic/ or platform/"; \
131 false; \
132 }
133 $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_BACKPORT_DIR),generic-backport/)
134 $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_PATCH_DIR),generic/)
135 $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(GENERIC_HACK_DIR),generic-hack/)
136 $(call Quilt/RefreshDir,$(PKG_BUILD_DIR),$(PATCH_DIR),platform/)
137 endef
138
139 define Quilt/Template
140 $($(2)STAMP_CONFIGURED): $($(2)STAMP_CHECKED)
141 $(if $(NO_RECONFIGURE),$($(2)STAMP_BUILT),$($(2)STAMP_CONFIGURED)): FORCE
142 $($(2)STAMP_CHECKED): $($(2)STAMP_PREPARED)
143 if [ -s "$(1)/patches/series" ]; then \
144 (cd "$(1)"; \
145 if $(QUILT_CMD) next >/dev/null 2>&1; then \
146 $(QUILT_CMD) push -a; \
147 else \
148 $(QUILT_CMD) top >/dev/null 2>&1; \
149 fi \
150 ); \
151 fi
152 touch "$$@"
153
154 $(3)quilt-check: $($(2)STAMP_PREPARED) FORCE
155 @[ -f "$(1)/.quilt_used" ] || { \
156 echo "The source directory was not unpacked using quilt. Please rebuild with QUILT=1"; \
157 false; \
158 }
159 @[ -f "$(1)/patches/series" ] || { \
160 echo "The source directory contains no quilt patches."; \
161 false; \
162 }
163 @[ -n "$$$$(ls $(1)/patches/series)" -o \
164 "$$$$(cat $(1)/patches/series | mkhash md5)" = "$$(sort $(1)/patches/series | mkhash md5)" ] || { \
165 echo "The patches are not sorted in the right order. Please fix."; \
166 false; \
167 }
168
169 $(3)refresh: $(3)quilt-check
170 @cd "$(1)"; $(QUILT_CMD) pop -a -f >/dev/null 2>/dev/null
171 @cd "$(1)"; while $(QUILT_CMD) next 2>/dev/null >/dev/null && $(QUILT_CMD) push; do \
172 QUILT_DIFF_OPTS="-p" $(QUILT_CMD) refresh -p ab --no-index --no-timestamps; \
173 done; ! $(QUILT_CMD) next 2>/dev/null >/dev/null
174 $(Quilt/Refresh/$(4))
175
176 $(3)update: $(3)quilt-check
177 $(Quilt/Refresh/$(4))
178 endef
179
180 Build/Quilt=$(call Quilt/Template,$(PKG_BUILD_DIR),,,$(if $(TARGET_BUILD),Kernel,Package))
181 Host/Quilt=$(call Quilt/Template,$(HOST_BUILD_DIR),HOST_,host-,Host)
182
183 endif