kernel: bump 5.10 to 5.10.164
[openwrt/openwrt.git] / include / kernel.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 ifneq ($(filter check,$(MAKECMDGOALS)),)
6 CHECK:=1
7 DUMP:=1
8 endif
9
10 ifneq ($(SOURCE_DATE_EPOCH),)
11 ifndef DUMP
12 KBUILD_BUILD_TIMESTAMP:=$(shell perl -e 'print scalar gmtime($(SOURCE_DATE_EPOCH))')
13 endif
14 endif
15
16 ifeq ($(__target_inc),)
17 ifndef CHECK
18 include $(INCLUDE_DIR)/target.mk
19 endif
20 endif
21
22 ifeq ($(DUMP),1)
23 KERNEL?=<KERNEL>
24 BOARD?=<BOARD>
25 LINUX_VERSION?=<LINUX_VERSION>
26 LINUX_VERMAGIC?=<LINUX_VERMAGIC>
27 else
28 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
29 export GCC_HONOUR_COPTS=s
30 endif
31
32 LINUX_KMOD_SUFFIX=ko
33
34 ifneq (,$(findstring uml,$(BOARD)))
35 KERNEL_CC?=$(HOSTCC)
36 KERNEL_CROSS?=
37 else
38 KERNEL_CC?=$(TARGET_CC)
39 KERNEL_CROSS?=$(TARGET_CROSS)
40 endif
41
42 ifeq ($(TARGET_BUILD),1)
43 PATCH_DIR ?= $(CURDIR)/patches$(if $(wildcard ./patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
44 FILES_DIR ?= $(foreach dir,$(wildcard $(CURDIR)/files $(CURDIR)/files-$(KERNEL_PATCHVER)),"$(dir)")
45 endif
46 KERNEL_BUILD_DIR ?= $(BUILD_DIR)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))
47 LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
48 LINUX_UAPI_DIR=uapi/
49 LINUX_VERMAGIC:=$(strip $(shell cat $(LINUX_DIR)/.vermagic 2>/dev/null))
50 LINUX_VERMAGIC:=$(if $(LINUX_VERMAGIC),$(LINUX_VERMAGIC),unknown)
51
52 LINUX_UNAME_VERSION:=$(KERNEL_BASE)
53 ifneq ($(findstring -rc,$(LINUX_VERSION)),)
54 LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
55 endif
56
57 LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
58
59 ifneq (,$(findstring -rc,$(LINUX_VERSION)))
60 LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.gz
61 else
62 LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
63 endif
64
65 ifneq (,$(findstring -rc,$(LINUX_VERSION)))
66 LINUX_SITE:=https://git.kernel.org/torvalds/t
67 else ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
68 LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x
69 else
70 LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release 2>/dev/null))
71 endif
72
73 MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
74 TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
75
76 ifneq ($(TARGET_BUILD),1)
77 PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
78 endif
79 endif
80
81 ifneq (,$(findstring uml,$(BOARD)))
82 LINUX_KARCH=um
83 else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
84 LINUX_KARCH := arm64
85 else ifneq (,$(findstring $(ARCH) , arceb ))
86 LINUX_KARCH := arc
87 else ifneq (,$(findstring $(ARCH) , armeb ))
88 LINUX_KARCH := arm
89 else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
90 LINUX_KARCH := mips
91 else ifneq (,$(findstring $(ARCH) , powerpc64 ))
92 LINUX_KARCH := powerpc
93 else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
94 LINUX_KARCH := sh
95 else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
96 LINUX_KARCH := x86
97 else
98 LINUX_KARCH := $(ARCH)
99 endif
100
101 KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
102
103 KERNEL_MAKE_FLAGS = \
104 KCFLAGS="$(call iremap,$(BUILD_DIR),$(notdir $(BUILD_DIR))) $(filter-out -fno-plt,$(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))) $(call qstrip,$(CONFIG_KERNEL_CFLAGS))" \
105 HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
106 CROSS_COMPILE="$(KERNEL_CROSS)" \
107 ARCH="$(LINUX_KARCH)" \
108 KBUILD_HAVE_NLS=no \
109 KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
110 KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
111 KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
112 KBUILD_BUILD_VERSION="0" \
113 HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
114 KBUILD_HOSTLDLIBS="-L$(STAGING_DIR_HOST)/lib" \
115 CONFIG_SHELL="$(BASH)" \
116 $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
117 $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
118 cmd_syscalls= \
119 $(if $(__package_mk),KBUILD_EXTRA_SYMBOLS="$(wildcard $(PKG_SYMVERS_DIR)/*.symvers)")
120
121 KERNEL_NOSTDINC_FLAGS = \
122 -nostdinc $(if $(DUMP),, -isystem $(shell $(TARGET_CC) -print-file-name=include))
123
124 ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
125 KERNEL_MAKE_FLAGS += \
126 KERNELRELEASE=$(LINUX_VERSION)
127 endif
128
129 KERNEL_MAKEOPTS = -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
130
131 ifdef CONFIG_USE_SPARSE
132 KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
133 endif
134
135 ifneq ($(HOST_OS),Linux)
136 KERNEL_MAKEOPTS += CONFIG_STACK_VALIDATION=
137 export SKIP_STACK_VALIDATION:=1
138 endif
139
140 PKG_EXTMOD_SUBDIRS ?= .
141
142 PKG_SYMVERS_DIR = $(KERNEL_BUILD_DIR)/symvers
143
144 define collect_module_symvers
145 for subdir in $(PKG_EXTMOD_SUBDIRS); do \
146 realdir=$$$$(readlink -f $(PKG_BUILD_DIR)); \
147 grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
148 [ "$(PKG_BUILD_DIR)" = "$$$$realdir" ] || \
149 grep -F $$$$realdir $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
150 done; \
151 sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
152 mkdir -p $(PKG_SYMVERS_DIR); \
153 mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_SYMVERS_DIR)/$(PKG_NAME).symvers
154 endef
155
156 define KernelPackage/hooks
157 ifneq ($(PKG_NAME),kernel)
158 Hooks/Compile/Post += collect_module_symvers
159 endif
160 define KernelPackage/hooks
161 endef
162 endef
163
164 define KernelPackage/Defaults
165 FILES:=
166 AUTOLOAD:=
167 MODPARAMS:=
168 PKGFLAGS+=nonshared
169 endef
170
171 # 1: name
172 # 2: install prefix
173 # 3: module priority prefix
174 # 4: required for boot
175 # 5: module list
176 define ModuleAutoLoad
177 $(if $(5), \
178 mkdir -p $(2)/etc/modules.d; \
179 ($(foreach mod,$(5), \
180 echo "$(mod)$(if $(MODPARAMS.$(mod)), $(MODPARAMS.$(mod)),$(if $(MODPARAMS), $(MODPARAMS)))"; )) > $(2)/etc/modules.d/$(3)$(1); \
181 $(if $(4), \
182 mkdir -p $(2)/etc/modules-boot.d; \
183 ln -sf ../modules.d/$(3)$(1) $(2)/etc/modules-boot.d/;))
184 endef
185
186 ifeq ($(DUMP)$(TARGET_BUILD),)
187 -include $(LINUX_DIR)/.config
188 endif
189
190 define KernelPackage/depends
191 $(STAMP_BUILT): $(LINUX_DIR)/.config
192 define KernelPackage/depends
193 endef
194 endef
195
196 define KernelPackage
197 NAME:=$(1)
198 $(eval $(call Package/Default))
199 $(eval $(call KernelPackage/Defaults))
200 $(eval $(call KernelPackage/$(1)))
201 $(eval $(call KernelPackage/$(1)/$(BOARD)))
202 $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
203
204 define Package/kmod-$(1)
205 TITLE:=$(TITLE)
206 SECTION:=kernel
207 CATEGORY:=Kernel modules
208 DESCRIPTION:=$(DESCRIPTION)
209 EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
210 VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
211 PKGFLAGS:=$(PKGFLAGS)
212 $(call KernelPackage/$(1))
213 $(call KernelPackage/$(1)/$(BOARD))
214 $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
215 endef
216
217 ifdef KernelPackage/$(1)/conffiles
218 define Package/kmod-$(1)/conffiles
219 $(call KernelPackage/$(1)/conffiles)
220 endef
221 endif
222
223 ifdef KernelPackage/$(1)/description
224 define Package/kmod-$(1)/description
225 $(call KernelPackage/$(1)/description)
226 endef
227 endif
228
229 ifdef KernelPackage/$(1)/config
230 define Package/kmod-$(1)/config
231 $(call KernelPackage/$(1)/config)
232 endef
233 endif
234
235 $(call KernelPackage/depends)
236 $(call KernelPackage/hooks)
237
238 ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
239 define Package/kmod-$(1)/install
240 @for mod in $$(call version_filter,$$(FILES)); do \
241 if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
242 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
243 elif [ -e $$$$$$$$mod ]; then \
244 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
245 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
246 else \
247 echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
248 exit 1; \
249 fi; \
250 done;
251 $(call ModuleAutoLoad,$(1),$$(1),$(filter-out 0-,$(word 1,$(AUTOLOAD))-),$(filter-out 0,$(word 2,$(AUTOLOAD))),$(sort $(wordlist 3,99,$(AUTOLOAD))))
252 $(call KernelPackage/$(1)/install,$$(1))
253 endef
254 $(if $(CONFIG_PACKAGE_kmod-$(1)),
255 else
256 compile: $(1)-disabled
257 $(1)-disabled:
258 @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
259
260 define Package/kmod-$(1)/install
261 true
262 endef
263 )
264 endif
265 $$(eval $$(call BuildPackage,kmod-$(1)))
266
267 $$(IPKG_kmod-$(1)): $$(wildcard $$(call version_filter,$$(FILES)))
268
269 endef
270
271 version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
272
273 # 1: priority (optional)
274 # 2: module list
275 # 3: boot flag
276 define AutoLoad
277 $(if $(1),$(1),0) $(if $(3),1,0) $(call version_filter,$(2))
278 endef
279
280 # 1: module list
281 # 2: boot flag
282 define AutoProbe
283 $(call AutoLoad,,$(1),$(2))
284 endef
285
286 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
287 kernel_version_merge=$$(( ($(call version_field,1,$(1)) << 24) + ($(call version_field,2,$(1)) << 16) + ($(call version_field,3,$(1)) << 8) + $(call version_field,4,$(1)) ))
288
289 ifdef DUMP
290 kernel_version_cmp=
291 else
292 kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
293 endif
294
295 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
296
297 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
298 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
299 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
300 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
301 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))
302