blob: ef6ae8fdeabf3db7324c64efcd4af7a81f519359 [file] [log] [blame]
Michael Bestas3952f6c2016-08-26 01:12:08 +03001# Copyright (C) 2012 The CyanogenMod Project
Bruno Martins3bd34be2022-04-08 19:37:17 +01002# (C) 2017-2022 The LineageOS Project
Michael Bestas3952f6c2016-08-26 01:12:08 +03003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17# Android makefile to build kernel as a part of Android Build
18#
19# Configuration
20# =============
21#
22# These config vars are usually set in BoardConfig.mk:
23#
Sebastiano Barezzi94b96802022-03-16 17:42:13 +010024# TARGET_KERNEL_CONFIG = List of kernel defconfigs, first one being the base one,
25# while all the others are fragments that will be merged.
26# to main one in .config.
Michael Bestas3952f6c2016-08-26 01:12:08 +030027# TARGET_KERNEL_VARIANT_CONFIG = Variant defconfig, optional
28# TARGET_KERNEL_SELINUX_CONFIG = SELinux defconfig, optional
Michael Bestas3952f6c2016-08-26 01:12:08 +030029#
Alexander Koskoviche8e46e62022-03-07 11:29:34 -070030# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to true
Joe Maplesda328952017-11-27 10:09:38 -050031#
32# TARGET_KERNEL_CLANG_VERSION = Clang prebuilts version, optional, defaults to clang-stable
33#
34# TARGET_KERNEL_CLANG_PATH = Clang prebuilts path, optional
35#
Dan Pasanenedc1ceb2017-01-24 15:46:13 -060036# BOARD_KERNEL_IMAGE_NAME = Built image name
37# for ARM use: zImage
38# for ARM64 use: Image.gz
39# for uncompressed use: Image
40# If using an appended DT, append '-dtb'
41# to the end of the image name.
42# For example, for ARM devices,
43# use zImage-dtb instead of zImage.
Michael Bestas3952f6c2016-08-26 01:12:08 +030044#
Luca Stefani05ce3dc2020-08-03 11:58:00 +020045# BOARD_DTBO_CFG = Path to a mkdtboimg.py config file
46#
Aayush Gupta1de3b302020-08-25 10:03:10 +000047# BOARD_CUSTOM_DTBOIMG_MK = Path to a custom dtboimage makefile
48#
Joe Maplesda328952017-11-27 10:09:38 -050049# KERNEL_CC = The C Compiler used. This is automatically set based
50# on whether the clang version is set, optional.
Luca Stefani83dee702020-04-03 18:58:16 +020051# KERNEL_LD = The Linker used. This is automatically set based
52# on whether the clang/gcc version is set, optional.
Joe Maplesda328952017-11-27 10:09:38 -050053#
54# KERNEL_CLANG_TRIPLE = Target triple for clang (e.g. aarch64-linux-gnu-)
55# defaults to arm-linux-gnu- for arm
56# aarch64-linux-gnu- for arm64
57# x86_64-linux-gnu- for x86
58#
Chirayu Desai3eed09a2021-12-03 22:39:46 +053059# KERNEL_LTO = Optional, force LTO to none / thin / full
60#
Michael Bestas3952f6c2016-08-26 01:12:08 +030061# NEED_KERNEL_MODULE_ROOT = Optional, if true, install kernel
Luca Stefania1ade7f2018-07-11 00:35:33 +020062# modules in root instead of vendor
63# NEED_KERNEL_MODULE_SYSTEM = Optional, if true, install kernel
64# modules in system instead of vendor
Luca Stefaniedc2e0c2020-08-01 17:24:29 +020065# NEED_KERNEL_MODULE_VENDOR_OVERLAY = Optional, if true, install kernel
66# modules in vendor_overlay instead of vendor
Sebastiano Barezzic251f1b2021-02-09 00:31:49 +010067#
68# TARGET_FORCE_PREBUILT_KERNEL = Optional, use TARGET_PREBUILT_KERNEL even if
69# kernel sources are present
Michael Bestas3952f6c2016-08-26 01:12:08 +030070
Dan Pasanen8ee5d892017-12-08 13:46:30 -060071ifneq ($(TARGET_NO_KERNEL),true)
LuK1337d034c3f2020-09-15 11:06:18 +020072ifneq ($(TARGET_NO_KERNEL_OVERRIDE),true)
Michael Bestas3952f6c2016-08-26 01:12:08 +030073
Michael Bestas3952f6c2016-08-26 01:12:08 +030074## Externally influenced variables
Michael Bestas3952f6c2016-08-26 01:12:08 +030075KERNEL_SRC := $(TARGET_KERNEL_SOURCE)
76# kernel configuration - mandatory
77KERNEL_DEFCONFIG := $(TARGET_KERNEL_CONFIG)
marcost29b4cfe02021-07-06 12:55:59 -030078RECOVERY_DEFCONFIG := $(TARGET_KERNEL_RECOVERY_CONFIG)
Michael Bestas3952f6c2016-08-26 01:12:08 +030079VARIANT_DEFCONFIG := $(TARGET_KERNEL_VARIANT_CONFIG)
80SELINUX_DEFCONFIG := $(TARGET_KERNEL_SELINUX_CONFIG)
81
82## Internal variables
Rashed Abdel-Tawabe5238842019-10-15 00:56:52 -040083DTC := $(HOST_OUT_EXECUTABLES)/dtc
Michael Bestas3952f6c2016-08-26 01:12:08 +030084KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
marcost29b4cfe02021-07-06 12:55:59 -030085RECOVERY_KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/RECOVERY_KERNEL_OBJ
Luca Stefanife00ea92020-08-02 19:08:31 +020086DTBO_OUT := $(TARGET_OUT_INTERMEDIATES)/DTBO_OBJ
87DTB_OUT := $(TARGET_OUT_INTERMEDIATES)/DTB_OBJ
Michael Bestas3952f6c2016-08-26 01:12:08 +030088KERNEL_CONFIG := $(KERNEL_OUT)/.config
LuK13376047f5a2019-07-12 12:55:35 +020089KERNEL_RELEASE := $(KERNEL_OUT)/include/config/kernel.release
marcost29b4cfe02021-07-06 12:55:59 -030090RECOVERY_KERNEL_CONFIG := $(RECOVERY_KERNEL_OUT)/.config
91RECOVERY_KERNEL_RELEASE := $(RECOVERY_KERNEL_OUT)/include/config/kernel.release
Michael Bestas3952f6c2016-08-26 01:12:08 +030092
Michael Bestas3952f6c2016-08-26 01:12:08 +030093ifeq ($(KERNEL_ARCH),x86_64)
94KERNEL_DEFCONFIG_ARCH := x86
95else
96KERNEL_DEFCONFIG_ARCH := $(KERNEL_ARCH)
97endif
dianlujitaof30ad9e2019-09-12 17:37:45 +080098KERNEL_DEFCONFIG_DIR := $(KERNEL_SRC)/arch/$(KERNEL_DEFCONFIG_ARCH)/configs
Sebastiano Barezzi94b96802022-03-16 17:42:13 +010099ALL_KERNEL_DEFCONFIG_SRCS := $(foreach config,$(KERNEL_DEFCONFIG),$(KERNEL_DEFCONFIG_DIR)/$(config))
100ALL_RECOVERY_KERNEL_DEFCONFIG_SRCS := $(foreach config,$(RECOVERY_DEFCONFIG),$(KERNEL_DEFCONFIG_DIR)/$(config))
Michael Bestas3952f6c2016-08-26 01:12:08 +0300101
Sebastiano Barezzi94b96802022-03-16 17:42:13 +0100102BASE_KERNEL_DEFCONFIG := $(word 1, $(KERNEL_DEFCONFIG))
103BASE_KERNEL_DEFCONFIG_SRC := $(word 1, $(ALL_KERNEL_DEFCONFIG_SRCS))
104BASE_RECOVERY_KERNEL_DEFCONFIG := $(word 1, $(RECOVERY_DEFCONFIG))
105BASE_RECOVERY_KERNEL_DEFCONFIG_SRC := $(word 1, $(ALL_RECOVERY_KERNEL_DEFCONFIG_SRCS))
Michael Bestas3952f6c2016-08-26 01:12:08 +0300106
Aayush Gupta3ebfa5e2020-10-27 17:59:21 +0530107ifeq ($(TARGET_PREBUILT_KERNEL),)
108 ifeq ($(BOARD_KERNEL_IMAGE_NAME),)
109 $(error BOARD_KERNEL_IMAGE_NAME not defined.)
110 endif
dianlujitaof30ad9e2019-09-12 17:37:45 +0800111endif
112TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/$(BOARD_KERNEL_IMAGE_NAME)
113
marcost29b4cfe02021-07-06 12:55:59 -0300114TARGET_PREBUILT_INT_RECOVERY_KERNEL := $(RECOVERY_KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/$(BOARD_KERNEL_IMAGE_NAME)
115
Michael Bestas3952f6c2016-08-26 01:12:08 +0300116ifeq "$(wildcard $(KERNEL_SRC) )" ""
117 ifneq ($(TARGET_PREBUILT_KERNEL),)
118 HAS_PREBUILT_KERNEL := true
119 NEEDS_KERNEL_COPY := true
120 else
121 $(foreach cf,$(PRODUCT_COPY_FILES), \
122 $(eval _src := $(call word-colon,1,$(cf))) \
123 $(eval _dest := $(call word-colon,2,$(cf))) \
124 $(ifeq kernel,$(_dest), \
125 $(eval HAS_PREBUILT_KERNEL := true)))
126 endif
127
128 ifneq ($(HAS_PREBUILT_KERNEL),)
129 $(warning ***************************************************************)
130 $(warning * Using prebuilt kernel binary instead of source *)
Nolen Johnsonc0b837d2020-04-17 13:13:20 -0400131 $(warning * THIS IS DEPRECATED, AND IS NOT ADVISED. *)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300132 $(warning * Please configure your device to download the kernel *)
133 $(warning * source repository to $(KERNEL_SRC))
Michael Bestas3952f6c2016-08-26 01:12:08 +0300134 $(warning * for more information *)
135 $(warning ***************************************************************)
136 FULL_KERNEL_BUILD := false
137 KERNEL_BIN := $(TARGET_PREBUILT_KERNEL)
138 else
139 $(warning ***************************************************************)
140 $(warning * *)
141 $(warning * No kernel source found, and no fallback prebuilt defined. *)
142 $(warning * Please make sure your device is properly configured to *)
143 $(warning * download the kernel repository to $(KERNEL_SRC))
144 $(warning * and add the TARGET_KERNEL_CONFIG variable to BoardConfig.mk *)
145 $(warning * *)
146 $(warning * As an alternative, define the TARGET_PREBUILT_KERNEL *)
147 $(warning * variable with the path to the prebuilt binary kernel image *)
148 $(warning * in your BoardConfig.mk file *)
149 $(warning * *)
150 $(warning ***************************************************************)
151 $(error "NO KERNEL")
152 endif
153else
154 NEEDS_KERNEL_COPY := true
155 ifeq ($(TARGET_KERNEL_CONFIG),)
156 $(warning **********************************************************)
157 $(warning * Kernel source found, but no configuration was defined *)
158 $(warning * Please add the TARGET_KERNEL_CONFIG variable to your *)
159 $(warning * BoardConfig.mk file *)
160 $(warning **********************************************************)
Luca Stefanifb58f782020-08-03 11:32:19 +0200161 $(error "NO KERNEL CONFIG")
Michael Bestas3952f6c2016-08-26 01:12:08 +0300162 else
Tim Schumacher6303cd72021-06-27 11:27:27 +0200163 ifneq ($(TARGET_FORCE_PREBUILT_KERNEL),)
164 ifneq ($(filter RELEASE NIGHTLY SNAPSHOT EXPERIMENTAL,$(LINEAGE_BUILDTYPE)),)
165 $(error "PREBUILT KERNEL IS NOT ALLOWED ON OFFICIAL BUILDS!")
166 else
Sebastiano Barezzic251f1b2021-02-09 00:31:49 +0100167 $(warning **********************************************************)
168 $(warning * Kernel source found and configuration was defined, *)
169 $(warning * but prebuilt kernel is being forced. *)
170 $(warning * While this is likely intentional, *)
171 $(warning * it is NOT SUPPORTED WHATSOEVER. *)
172 $(warning * Generated kernel headers may not align with *)
173 $(warning * the ABI of kernel you're including. *)
174 $(warning * Please unset TARGET_FORCE_PREBUILT_KERNEL *)
175 $(warning * to build the kernel from source. *)
176 $(warning **********************************************************)
177 FULL_KERNEL_BUILD := false
178 KERNEL_BIN := $(TARGET_PREBUILT_KERNEL)
Sebastiano Barezzic251f1b2021-02-09 00:31:49 +0100179 endif
Tim Schumacher6303cd72021-06-27 11:27:27 +0200180 else
181 FULL_KERNEL_BUILD := true
182 KERNEL_BIN := $(TARGET_PREBUILT_INT_KERNEL)
Sebastiano Barezzic251f1b2021-02-09 00:31:49 +0100183 endif
Michael Bestas3952f6c2016-08-26 01:12:08 +0300184 endif
185endif
186
marcost29b4cfe02021-07-06 12:55:59 -0300187ifneq ($(TARGET_KERNEL_RECOVERY_CONFIG),)
188 ifeq "$(wildcard $(KERNEL_SRC) )" ""
189 ifeq ($(TARGET_PREBUILT_RECOVERY_KERNEL),)
190 $(warning ***************************************************************)
191 $(warning * *)
192 $(warning * No recovery kernel source found, and no fallback prebuilt *)
193 $(warning * defined. Please make sure your device is properly *)
194 $(warning * configured to download the kernel repository to $(KERNEL_SRC))
195 $(warning * or remove TARGET_KERNEL_RECOVERY_CONFIG from BoardConfig.mk *)
196 $(warning * *)
197 $(warning * Or, define the TARGET_PREBUILT_RECOVERY_KERNEL *)
198 $(warning * variable with the path to the prebuilt recovery kernel image*)
199 $(warning * in your BoardConfig.mk file *)
200 $(warning * *)
201 $(warning ***************************************************************)
202 $(error "NO RECOVERY KERNEL SOURCE")
203 endif
204 endif
205 ifneq ($(BOARD_USES_RECOVERY_AS_BOOT),)
206 $(warning ********************************************************)
207 $(warning * TARGET_KERNEL_RECOVERY_CONFIG set but device uses *)
208 $(warning * RECOVERY_AS_BOOT, which uses boot kernel as recovery *)
209 $(warning * kernel, as such it's not possible to use different *)
210 $(warning * configs *)
211 $(warning ********************************************************)
212 $(error "INVALID CONFIGURATION")
213 else
214 FULL_RECOVERY_KERNEL_BUILD := true
215 RECOVERY_KERNEL_COPY := true
216 RECOVERY_BIN := $(TARGET_PREBUILT_INT_RECOVERY_KERNEL)
217 endif
Michael Bestas3952f6c2016-08-26 01:12:08 +0300218else
marcost29b4cfe02021-07-06 12:55:59 -0300219 ifneq ($(TARGET_PREBUILT_RECOVERY_KERNEL),)
220 RECOVERY_BIN := $(TARGET_PREBUILT_RECOVERY_KERNEL)
221 RECOVERY_KERNEL_COPY := true
222 endif
Michael Bestas3952f6c2016-08-26 01:12:08 +0300223endif
224
marcost29b4cfe02021-07-06 12:55:59 -0300225ifeq ($(or $(FULL_RECOVERY_KERNEL_BUILD), $(FULL_KERNEL_BUILD)),true)
Rashed Abdel-Tawabe5238842019-10-15 00:56:52 -0400226# Add host bin out dir to path
227PATH_OVERRIDE := PATH=$(KERNEL_BUILD_OUT_PREFIX)$(HOST_OUT_EXECUTABLES):$$PATH
Alexander Koskoviche8e46e62022-03-07 11:29:34 -0700228ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
Joe Maplesda328952017-11-27 10:09:38 -0500229 ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
Luca Stefani736f1be2019-09-14 16:26:26 +0200230 KERNEL_CLANG_VERSION := clang-$(TARGET_KERNEL_CLANG_VERSION)
Joe Maplesda328952017-11-27 10:09:38 -0500231 else
Nathan Chancellorca403312018-04-11 16:23:19 -0700232 # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
233 KERNEL_CLANG_VERSION := $(LLVM_PREBUILTS_VERSION)
Joe Maplesda328952017-11-27 10:09:38 -0500234 endif
Bruno Martins63d13c32020-09-24 11:51:33 +0100235 TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/$(KERNEL_CLANG_VERSION)
Joe Maplesda328952017-11-27 10:09:38 -0500236 ifeq ($(KERNEL_ARCH),arm64)
237 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=aarch64-linux-gnu-
238 else ifeq ($(KERNEL_ARCH),arm)
239 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=arm-linux-gnu-
240 else ifeq ($(KERNEL_ARCH),x86)
241 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=x86_64-linux-gnu-
242 endif
dianlujitaof30ad9e2019-09-12 17:37:45 +0800243 PATH_OVERRIDE += PATH=$(TARGET_KERNEL_CLANG_PATH)/bin:$$PATH LD_LIBRARY_PATH=$(TARGET_KERNEL_CLANG_PATH)/lib64:$$LD_LIBRARY_PATH
Dan Pasanenf65a6df2018-02-18 22:13:05 -0600244 ifeq ($(KERNEL_CC),)
Nathan Chancellor4d0d4382018-10-21 19:07:47 -0700245 KERNEL_CC := CC="$(CCACHE_BIN) clang"
Dan Pasanenf65a6df2018-02-18 22:13:05 -0600246 endif
Luca Stefani83dee702020-04-03 18:58:16 +0200247 ifeq ($(KERNEL_LD),)
248 KERNEL_LD :=
249 endif
Michael Bestas3952f6c2016-08-26 01:12:08 +0300250endif
251
dianlujitaof30ad9e2019-09-12 17:37:45 +0800252ifneq ($(TARGET_KERNEL_MODULES),)
253 $(error TARGET_KERNEL_MODULES is no longer supported!)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300254endif
255
Alessandro Astone8fb7cda2021-12-11 01:49:53 +0100256PATH_OVERRIDE += PATH=$(KERNEL_TOOLCHAIN_PATH_gcc):$$PATH
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -0700257
Rashed Abdel-Tawabdfc3d182019-09-06 10:44:50 -0700258# System tools are no longer allowed on 10+
259PATH_OVERRIDE += $(TOOLS_PATH_OVERRIDE)
260
Ethan Chena3254462019-02-09 16:26:51 -0800261# Internal implementation of make-kernel-target
262# $(1): output path (The value passed to O=)
263# $(2): target to build (eg. defconfig, modules, dtbo.img)
264define internal-make-kernel-target
Luca Stefani83dee702020-04-03 18:58:16 +0200265$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_BUILD_OUT_PREFIX)$(1) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_LD) $(2)
Ethan Chena3254462019-02-09 16:26:51 -0800266endef
267
marcost29b4cfe02021-07-06 12:55:59 -0300268# Generate kernel .config from a given defconfig
269# $(1): Output path (The value passed to O=)
270# $(2): The defconfig to process (just the filename, no need for full path to file)
271define make-kernel-config
272 $(call internal-make-kernel-target,$(1),VARIANT_DEFCONFIG=$(VARIANT_DEFCONFIG) SELINUX_DEFCONFIG=$(SELINUX_DEFCONFIG) $(2))
Chirayu Desai3eed09a2021-12-03 22:39:46 +0530273 $(hide) if [ "$(KERNEL_LTO)" = "none" ]; then \
274 $(KERNEL_SRC)/scripts/config --file $(1)/.config \
275 -d LTO_CLANG \
276 -e LTO_NONE \
277 -d LTO_CLANG_THIN \
278 -d LTO_CLANG_FULL \
279 -d THINLTO; \
280 $(call make-kernel-target,olddefconfig); \
281 elif [ "$(KERNEL_LTO)" = "thin" ]; then \
282 $(KERNEL_SRC)/scripts/config --file $(1)/.config \
283 -e LTO_CLANG \
284 -d LTO_NONE \
285 -e LTO_CLANG_THIN \
286 -d LTO_CLANG_FULL \
287 -e THINLTO; \
288 $(call make-kernel-target,olddefconfig); \
289 elif [ "$(KERNEL_LTO)" = "full" ]; then \
290 $(KERNEL_SRC)/scripts/config --file $(1)/.config \
291 -e LTO_CLANG \
292 -d LTO_NONE \
293 -d LTO_CLANG_THIN \
294 -e LTO_CLANG_FULL \
295 -d THINLTO; \
296 $(call make-kernel-target,olddefconfig); \
297 fi
marcost29b4cfe02021-07-06 12:55:59 -0300298 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
299 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
300 echo $(KERNEL_CONFIG_OVERRIDE) >> $(1)/.config; \
301 $(call make-kernel-target,oldconfig); \
302 fi
303 # Create defconfig build artifact
304 $(call internal-make-kernel-target,$(1),savedefconfig)
marcost29b4cfe02021-07-06 12:55:59 -0300305endef
306
Ethan Chena3254462019-02-09 16:26:51 -0800307# Make a kernel target
308# $(1): The kernel target to build (eg. defconfig, modules, modules_install)
309define make-kernel-target
310$(call internal-make-kernel-target,$(KERNEL_OUT),$(1))
311endef
312
marcost29b4cfe02021-07-06 12:55:59 -0300313# Make a recovery kernel target
314# $(1): The kernel target to build (eg. defconfig, modules, modules_install)
315define make-recovery-kernel-target
316$(call internal-make-kernel-target,$(RECOVERY_KERNEL_OUT),$(1))
317endef
318
Ethan Chena3254462019-02-09 16:26:51 -0800319# Make a DTBO target
320# $(1): The DTBO target to build (eg. dtbo.img, defconfig)
321define make-dtbo-target
Luca Stefanife00ea92020-08-02 19:08:31 +0200322$(call internal-make-kernel-target,$(DTBO_OUT),$(1))
Ethan Chena3254462019-02-09 16:26:51 -0800323endef
324
Rashed Abdel-Tawab2c247d82019-09-27 23:37:14 -0400325# Make a DTB targets
326# $(1): The DTB target to build (eg. dtbs, defconfig)
327define make-dtb-target
Luca Stefanife00ea92020-08-02 19:08:31 +0200328$(call internal-make-kernel-target,$(DTB_OUT),$(1))
Rashed Abdel-Tawab2c247d82019-09-27 23:37:14 -0400329endef
330
Alessandro Astone1ab63132020-09-14 21:14:47 +0200331# $(1): modules list
332# $(2): output dir
333# $(3): mount point
334# $(4): staging dir
Alessandro Astone8d6e3d62021-01-08 17:43:27 +0100335# $(5): module load list
Alessandro Astone1ab63132020-09-14 21:14:47 +0200336# Depmod requires a well-formed kernel version so 0.0 is used as a placeholder.
337define build-image-kernel-modules-lineage
Alessandro Astone1ab63132020-09-14 21:14:47 +0200338 mkdir -p $(2)/lib/modules
339 cp $(1) $(2)/lib/modules/
340 rm -rf $(4)
341 mkdir -p $(4)/lib/modules/0.0/$(3)lib/modules
342 cp $(1) $(4)/lib/modules/0.0/$(3)lib/modules
343 $(DEPMOD) -b $(4) 0.0
344 sed -e 's/\(.*modules.*\):/\/\1:/g' -e 's/ \([^ ]*modules[^ ]*\)/ \/\1/g' $(4)/lib/modules/0.0/modules.dep > $(2)/lib/modules/modules.dep
Alessandro Astone8d6e3d62021-01-08 17:43:27 +0100345 cp $(4)/lib/modules/0.0/modules.softdep $(2)/lib/modules
Alessandro Astone1ab63132020-09-14 21:14:47 +0200346 cp $(4)/lib/modules/0.0/modules.alias $(2)/lib/modules
Alessandro Astone8d6e3d62021-01-08 17:43:27 +0100347 rm -f $(2)/lib/modules/modules.load
348 for MODULE in $(5); do \
349 basename $$MODULE >> $(2)/lib/modules/modules.load; \
350 done
Alessandro Astone1ab63132020-09-14 21:14:47 +0200351endef
352
marcost29b4cfe02021-07-06 12:55:59 -0300353endif # FULL_RECOVERY_KERNEL_BUILD or FULL_KERNEL_BUILD
354
355ifeq ($(FULL_KERNEL_BUILD),true)
356
357ifeq ($(NEED_KERNEL_MODULE_ROOT),true)
358KERNEL_MODULES_OUT := $(TARGET_ROOT_OUT)
359KERNEL_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_recovery)
360KERNEL_MODULE_MOUNTPOINT :=
361else ifeq ($(NEED_KERNEL_MODULE_SYSTEM),true)
362KERNEL_MODULES_OUT := $(TARGET_OUT)
363KERNEL_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_system)
364KERNEL_MODULE_MOUNTPOINT := system
365$(INSTALLED_SYSTEMIMAGE_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
366else ifeq ($(NEED_KERNEL_MODULE_VENDOR_OVERLAY),true)
367KERNEL_MODULES_OUT := $(TARGET_OUT_PRODUCT)/vendor_overlay/$(PRODUCT_TARGET_VNDK_VERSION)
368KERNEL_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_product)
369KERNEL_MODULE_MOUNTPOINT := vendor
370$(INSTALLED_PRODUCTIMAGE_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
Alexander Koskovich20ca83c2022-01-22 22:26:50 -0700371else ifeq ($(BOARD_USES_VENDOR_DLKMIMAGE),true)
372KERNEL_MODULES_OUT := $(TARGET_OUT_VENDOR_DLKM)
373KERNEL_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor)
374KERNEL_MODULE_MOUNTPOINT := vendor_dlkm
375$(INSTALLED_VENDOR_DLKMIMAGE_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
marcost29b4cfe02021-07-06 12:55:59 -0300376else
377KERNEL_MODULES_OUT := $(TARGET_OUT_VENDOR)
378KERNEL_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor)
379KERNEL_MODULE_MOUNTPOINT := vendor
380$(INSTALLED_VENDORIMAGE_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
381endif
382MODULES_INTERMEDIATES := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,kernel_modules)
383
384KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor_ramdisk)
385$(INTERNAL_VENDOR_RAMDISK_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
386
Luca Stefanie30626d2020-09-03 20:56:29 +0200387$(KERNEL_OUT):
dianlujitaof30ad9e2019-09-12 17:37:45 +0800388 mkdir -p $(KERNEL_OUT)
Luca Stefanie30626d2020-09-03 20:56:29 +0200389
Bruno Martins3bd34be2022-04-08 19:37:17 +0100390$(KERNEL_CONFIG): $(KERNEL_OUT) $(ALL_KERNEL_DEFCONFIG_SRCS)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300391 @echo "Building Kernel Config"
marcost29b4cfe02021-07-06 12:55:59 -0300392 $(call make-kernel-config,$(KERNEL_OUT),$(KERNEL_DEFCONFIG))
Michael Bestas3952f6c2016-08-26 01:12:08 +0300393
Rashed Abdel-Tawabe5238842019-10-15 00:56:52 -0400394$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_CONFIG) $(DEPMOD) $(DTC)
395 @echo "Building Kernel Image ($(BOARD_KERNEL_IMAGE_NAME))"
Ethan Chena3254462019-02-09 16:26:51 -0800396 $(call make-kernel-target,$(BOARD_KERNEL_IMAGE_NAME))
Tom Marshallf15c3712017-11-10 01:00:18 +0100397 $(hide) if grep -q '^CONFIG_OF=y' $(KERNEL_CONFIG); then \
398 echo "Building DTBs"; \
Ethan Chena3254462019-02-09 16:26:51 -0800399 $(call make-kernel-target,dtbs); \
Tom Marshallf15c3712017-11-10 01:00:18 +0100400 fi
Ethan Chen0724f782019-02-09 21:05:01 -0800401 $(hide) if grep -q '=m' $(KERNEL_CONFIG); then \
Tom Marshallf15c3712017-11-10 01:00:18 +0100402 echo "Building Kernel Modules"; \
dianlujitaof30ad9e2019-09-12 17:37:45 +0800403 $(call make-kernel-target,modules) || exit "$$?"; \
Tom Marshallf15c3712017-11-10 01:00:18 +0100404 echo "Installing Kernel Modules"; \
dianlujitaof30ad9e2019-09-12 17:37:45 +0800405 $(call make-kernel-target,INSTALL_MOD_PATH=$(MODULES_INTERMEDIATES) INSTALL_MOD_STRIP=1 modules_install); \
LuK13376047f5a2019-07-12 12:55:35 +0200406 kernel_release=$$(cat $(KERNEL_RELEASE)) \
Luca Stefani4b7a0a02020-08-01 21:20:23 +0200407 kernel_modules_dir=$(MODULES_INTERMEDIATES)/lib/modules/$$kernel_release \
408 $(foreach s, $(TARGET_MODULE_ALIASES),\
409 $(eval p := $(subst :,$(space),$(s))) \
410 ; mv $$(find $$kernel_modules_dir -name $(word 1,$(p))) $$kernel_modules_dir/$(word 2,$(p))); \
411 modules=$$(find $$kernel_modules_dir -type f -name '*.ko'); \
Alessandro Astone8d6e3d62021-01-08 17:43:27 +0100412 ($(call build-image-kernel-modules-lineage,$$modules,$(KERNEL_MODULES_OUT),$(KERNEL_MODULE_MOUNTPOINT)/,$(KERNEL_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_KERNEL_MODULES_LOAD))); \
413 $(if $(BOOT_KERNEL_MODULES),\
414 vendor_boot_modules=$$(for m in $(BOOT_KERNEL_MODULES); do \
415 p=$$(find $$kernel_modules_dir -type f -name $$m); \
416 if [ -n "$$p" ]; then echo $$p; else echo "ERROR: $$m from BOOT_KERNEL_MODULES was not found" 1>&2 && exit 1; fi; \
417 done); \
418 [ $$? -ne 0 ] && exit 1; \
419 ($(call build-image-kernel-modules-lineage,$$vendor_boot_modules,$(TARGET_VENDOR_RAMDISK_OUT),/,$(KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD))); \
420 ) \
Tom Marshallf15c3712017-11-10 01:00:18 +0100421 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300422
Tom Marshall6b83d832017-11-09 21:02:33 +0100423.PHONY: kerneltags
424kerneltags: $(KERNEL_CONFIG)
Ethan Chena3254462019-02-09 16:26:51 -0800425 $(call make-kernel-target,tags)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300426
Michael Bestasb4c4aba2018-10-06 22:12:10 +0300427.PHONY: kernelsavedefconfig alldefconfig
Michael Bestas3952f6c2016-08-26 01:12:08 +0300428
Luca Stefanie30626d2020-09-03 20:56:29 +0200429kernelsavedefconfig: $(KERNEL_OUT)
Sebastiano Barezzi94b96802022-03-16 17:42:13 +0100430 $(call make-kernel-config,$(KERNEL_OUT),$(BASE_KERNEL_DEFCONFIG))
431 cp $(KERNEL_OUT)/defconfig $(BASE_KERNEL_DEFCONFIG_SRC)
Michael Bestas296008c2017-07-12 20:56:35 +0300432
Luca Stefanie30626d2020-09-03 20:56:29 +0200433alldefconfig: $(KERNEL_OUT)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300434 env KCONFIG_NOTIMESTAMP=true \
Ethan Chena3254462019-02-09 16:26:51 -0800435 $(call make-kernel-target,alldefconfig)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300436
Luca Stefanife00ea92020-08-02 19:08:31 +0200437ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
Aayush Gupta1de3b302020-08-25 10:03:10 +0000438ifneq ($(BOARD_CUSTOM_DTBOIMG_MK),)
439include $(BOARD_CUSTOM_DTBOIMG_MK)
440else
Rashed Abdel-Tawabe5238842019-10-15 00:56:52 -0400441MKDTIMG := $(HOST_OUT_EXECUTABLES)/mkdtimg$(HOST_EXECUTABLE_SUFFIX)
442MKDTBOIMG := $(HOST_OUT_EXECUTABLES)/mkdtboimg.py$(HOST_EXECUTABLE_SUFFIX)
443$(BOARD_PREBUILT_DTBOIMAGE): $(DTC) $(MKDTIMG) $(MKDTBOIMG)
Luca Stefanife00ea92020-08-02 19:08:31 +0200444ifeq ($(BOARD_KERNEL_SEPARATED_DTBO),true)
445$(BOARD_PREBUILT_DTBOIMAGE):
446 @echo "Building dtbo.img"
Alexander Koskovich7f280992022-05-11 16:02:12 -0700447 rm -rf $(DTBO_OUT)
Luca Stefanife00ea92020-08-02 19:08:31 +0200448 $(call make-dtbo-target,$(KERNEL_DEFCONFIG))
449 $(call make-dtbo-target,dtbs)
Luca Stefani05ce3dc2020-08-03 11:58:00 +0200450ifdef BOARD_DTBO_CFG
451 $(MKDTBOIMG) cfg_create $@ $(BOARD_DTBO_CFG) -d $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts
452else
Luca Stefani16ea8142020-08-05 11:37:46 +0200453 $(MKDTBOIMG) create $@ --page_size=$(BOARD_KERNEL_PAGESIZE) $(shell find $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtbo" | sort)
Luca Stefani05ce3dc2020-08-03 11:58:00 +0200454endif
Luca Stefanife00ea92020-08-02 19:08:31 +0200455else
456$(BOARD_PREBUILT_DTBOIMAGE):
457 @echo "Building dtbo.img"
Ethan Chena3254462019-02-09 16:26:51 -0800458 $(call make-dtbo-target,$(KERNEL_DEFCONFIG))
459 $(call make-dtbo-target,dtbo.img)
Luca Stefanife00ea92020-08-02 19:08:31 +0200460endif # BOARD_KERNEL_SEPARATED_DTBO
Aayush Gupta1de3b302020-08-25 10:03:10 +0000461endif # BOARD_CUSTOM_DTBOIMG_MK
Luca Stefanife00ea92020-08-02 19:08:31 +0200462endif # TARGET_NEEDS_DTBOIMAGE/BOARD_KERNEL_SEPARATED_DTBO
463
464ifeq ($(BOARD_INCLUDE_DTB_IN_BOOTIMG),true)
465ifeq ($(BOARD_PREBUILT_DTBIMAGE_DIR),)
466$(INSTALLED_DTBIMAGE_TARGET): $(DTC)
467 @echo "Building dtb.img"
Alexander Koskovich7f280992022-05-11 16:02:12 -0700468 rm -rf $(DTB_OUT)
Luca Stefanife00ea92020-08-02 19:08:31 +0200469 $(call make-dtb-target,$(KERNEL_DEFCONFIG))
470 $(call make-dtb-target,dtbs)
471 cat $(shell find $(DTB_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtb" | sort) > $@
472endif # !BOARD_PREBUILT_DTBIMAGE_DIR
473endif # BOARD_INCLUDE_DTB_IN_BOOTIMG
Christian Oder8d5db3c2017-11-25 16:52:17 +0100474
Ethan Chen117a0fd2019-02-10 12:12:50 -0800475endif # FULL_KERNEL_BUILD
476
marcost29b4cfe02021-07-06 12:55:59 -0300477ifeq ($(FULL_RECOVERY_KERNEL_BUILD),true)
478
479$(RECOVERY_KERNEL_OUT):
480 mkdir -p $(RECOVERY_KERNEL_OUT)
481
Bruno Martins164dfec2022-04-08 20:49:25 +0100482$(RECOVERY_KERNEL_CONFIG): $(ALL_RECOVERY_KERNEL_DEFCONFIG_SRCS)
marcost29b4cfe02021-07-06 12:55:59 -0300483 @echo "Building Recovery Kernel Config"
484 $(call make-kernel-config,$(RECOVERY_KERNEL_OUT),$(RECOVERY_DEFCONFIG))
485
486$(TARGET_PREBUILT_INT_RECOVERY_KERNEL): $(RECOVERY_KERNEL_CONFIG) $(DEPMOD) $(DTC)
487 @echo "Building Recovery Kernel Image ($(BOARD_KERNEL_IMAGE_NAME))"
488 $(call make-recovery-kernel-target,$(BOARD_KERNEL_IMAGE_NAME))
489
490
491endif
492
Michael Bestas3952f6c2016-08-26 01:12:08 +0300493## Install it
494
495ifeq ($(NEEDS_KERNEL_COPY),true)
496file := $(INSTALLED_KERNEL_TARGET)
497ALL_PREBUILT += $(file)
498$(file) : $(KERNEL_BIN) | $(ACP)
499 $(transform-prebuilt-to-target)
500
501ALL_PREBUILT += $(INSTALLED_KERNEL_TARGET)
502endif
503
marcost29b4cfe02021-07-06 12:55:59 -0300504ifeq ($(RECOVERY_KERNEL_COPY),true)
Jarl-Penguin91ed1292022-02-20 13:26:31 +0100505file := $(INSTALLED_RECOVERY_KERNEL_TARGET)
marcost29b4cfe02021-07-06 12:55:59 -0300506ALL_PREBUILT += $(file)
507$(file) : $(RECOVERY_BIN) | $(ACP)
508 $(transform-prebuilt-to-target)
509
Jarl-Penguin91ed1292022-02-20 13:26:31 +0100510ALL_PREBUILT += $(INSTALLED_RECOVERY_KERNEL_TARGET)
marcost29b4cfe02021-07-06 12:55:59 -0300511endif
512
513.PHONY: recovery-kernel
Jarl-Penguin91ed1292022-02-20 13:26:31 +0100514recovery-kernel: $(INSTALLED_RECOVERY_KERNEL_TARGET)
marcost29b4cfe02021-07-06 12:55:59 -0300515
Michael Bestas3952f6c2016-08-26 01:12:08 +0300516.PHONY: kernel
dianlujitaof30ad9e2019-09-12 17:37:45 +0800517kernel: $(INSTALLED_KERNEL_TARGET)
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600518
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700519.PHONY: dtboimage
520dtboimage: $(INSTALLED_DTBOIMAGE_TARGET)
Christian Oder8d5db3c2017-11-25 16:52:17 +0100521
Rashed Abdel-Tawab2c247d82019-09-27 23:37:14 -0400522.PHONY: dtbimage
523dtbimage: $(INSTALLED_DTBIMAGE_TARGET)
Rashed Abdel-Tawab2c247d82019-09-27 23:37:14 -0400524
LuK1337d034c3f2020-09-15 11:06:18 +0200525endif # TARGET_NO_KERNEL_OVERRIDE
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600526endif # TARGET_NO_KERNEL