blob: 627d5c46bd51f02ceebc67fa6bdde28256488a93 [file] [log] [blame]
Aaron Kling51e93ce2024-01-02 21:13:18 -06001# Copyright (C) 2018-2024 The LineageOS Project
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15#
16# Kernel build configuration variables
17# ====================================
18#
19# These config vars are usually set in BoardConfig.mk:
20#
21# TARGET_KERNEL_SOURCE = Kernel source dir, optional, defaults
22# to kernel/$(TARGET_DEVICE_DIR)
23# TARGET_KERNEL_ARCH = Kernel Arch
24# TARGET_KERNEL_CROSS_COMPILE_PREFIX = Compiler prefix (e.g. arm-eabi-)
25# defaults to arm-linux-androidkernel- for arm
dianlujitaobd16a8e2019-09-08 12:44:49 +080026# aarch64-linux-android- for arm64
27# x86_64-linux-android- for x86
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070028#
Alexander Koskoviche8e46e62022-03-07 11:29:34 -070029# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to true
Michael Bestasb9c73042022-11-01 14:41:42 +020030# TARGET_KERNEL_CLANG_VERSION = Clang prebuilts version, optional, defaults to clang-stable
31# TARGET_KERNEL_CLANG_PATH = Clang prebuilts path, optional
32#
Michael Bestas4d700e72022-09-08 04:19:32 +030033# TARGET_KERNEL_LLVM_BINUTILS = Use LLVM binutils, defaults to true
Michael Bestas38ba7c02022-11-14 13:09:00 +020034# TARGET_KERNEL_NO_GCC = Fully compile the kernel without GCC.
35# Defaults to false
Michael Bestas826cbec2023-05-26 21:33:48 +030036# TARGET_KERNEL_VERSION = Reported kernel version in top level kernel
37# makefile. Can be overriden in device trees
38# in the event of prebuilt kernel.
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070039#
Michael Bestas581a3862022-08-10 06:44:40 +030040# TARGET_KERNEL_DTBO_PREFIX = Override path prefix of TARGET_KERNEL_DTBO.
41# Defaults to empty
Michael Bestas15a631e2022-08-10 05:18:59 +030042# TARGET_KERNEL_DTBO = Name of the kernel Makefile target that
43# generates dtbo.img. Defaults to dtbo.img
Michael Bestasa7999b02022-08-10 05:29:55 +030044# TARGET_KERNEL_DTB = Name of the kernel Makefile target that
45# generates the *.dtb targets. Defaults to dtbs
Michael Bestas15a631e2022-08-10 05:18:59 +030046#
Neel Chauhane546ac82022-07-21 09:30:48 -070047# TARGET_KERNEL_EXT_MODULE_ROOT = Optional, the external modules root directory
48# Defaults to empty
49# TARGET_KERNEL_EXT_MODULES = Optional, the external modules we are
50# building. Defaults to empty
51#
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070052# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
53# Set this var in shell to override
54# toolchain specified in BoardConfig.mk
55# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
56# TARGET_KERNEL_CROSS_COMPILE_PREFIX
57# is in PATH
58# USE_CCACHE = Enable ccache (global Android flag)
59
Jason Wojcik4916cab2021-03-23 10:46:09 -070060BUILD_TOP := $(abspath .)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070061
62TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
63TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070064
65TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
66ifeq ($(TARGET_KERNEL_ARCH),)
Michael Bestasf0b1dd62022-10-01 00:33:17 +030067 KERNEL_ARCH := $(TARGET_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070068else
Michael Bestasf0b1dd62022-10-01 00:33:17 +030069 KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070070endif
71
Chirayu Desai63dbd4e2022-07-08 03:33:22 +053072KERNEL_VERSION := $(shell grep -s "^VERSION = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
73KERNEL_PATCHLEVEL := $(shell grep -s "^PATCHLEVEL = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
Michael Bestas826cbec2023-05-26 21:33:48 +030074TARGET_KERNEL_VERSION ?= $(shell echo $(KERNEL_VERSION)"."$(KERNEL_PATCHLEVEL))
Alexander Koskovich8655c4a2022-06-15 08:46:57 -070075
Michael Bestas38ba7c02022-11-14 13:09:00 +020076# 5.10+ can fully compile without GCC by default
LuK133797890e32023-08-01 19:59:14 +020077ifneq ($(KERNEL_VERSION),)
Aaron Kling51e93ce2024-01-02 21:13:18 -060078 ifeq ($(shell expr $(KERNEL_VERSION) \>= 6), 1)
79 TARGET_KERNEL_NO_GCC ?= true
80 else ifeq ($(shell expr $(KERNEL_VERSION) \>= 5), 1)
LuK133797890e32023-08-01 19:59:14 +020081 ifeq ($(shell expr $(KERNEL_PATCHLEVEL) \>= 10), 1)
82 TARGET_KERNEL_NO_GCC ?= true
83 endif
Bruno Martins44b85532023-05-11 21:53:03 +010084 endif
Michael Bestas38ba7c02022-11-14 13:09:00 +020085endif
86
87ifeq ($(TARGET_KERNEL_NO_GCC), true)
88 KERNEL_NO_GCC := true
89endif
90
Michael Bestasb9c73042022-11-01 14:41:42 +020091ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
92 KERNEL_CLANG_VERSION := clang-$(TARGET_KERNEL_CLANG_VERSION)
93else
94 # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
micky38753722ce2024-10-12 10:40:41 -040095 KERNEL_CLANG_VERSION := $(LLVM_AOSP_PREBUILTS_VERSION)
Michael Bestasb9c73042022-11-01 14:41:42 +020096endif
97TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/$(KERNEL_CLANG_VERSION)
Michael Bestasae531bc2022-10-01 00:38:14 +030098
99ifneq ($(USE_CCACHE),)
100 ifneq ($(CCACHE_EXEC),)
101 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
102 CCACHE_BIN := $(CCACHE_EXEC)
103 endif
104endif
105
106# Clear this first to prevent accidental poisoning from env
107KERNEL_MAKE_FLAGS :=
108
Alexander Koskovich76ce4f82022-08-04 13:00:07 -0700109# Add back threads, ninja cuts this to $(getconf _NPROCESSORS_ONLN)/2
110KERNEL_MAKE_FLAGS += -j$(shell getconf _NPROCESSORS_ONLN)
Michael Bestasae531bc2022-10-01 00:38:14 +0300111
112TOOLS_PATH_OVERRIDE := \
voidanixebf8bca2024-11-20 00:59:00 +0100113 HIP_PATH=none PERL5LIB=$(BUILD_TOP)/prebuilts/tools-lineage/common/perl-base
Michael Bestasae531bc2022-10-01 00:38:14 +0300114
Michael Bestas38ba7c02022-11-14 13:09:00 +0200115ifneq ($(KERNEL_NO_GCC), true)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300116 GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_PREBUILT_TAG)
117 # arm64 toolchain
118 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
119 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
120 # arm toolchain
121 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
122 KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
123 # x86 toolchain
124 KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
125 KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700126
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300127 TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
128 ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
129 KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
130 else
131 KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
132 KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
133 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700134
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300135 ifeq ($(KERNEL_TOOLCHAIN),)
136 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
137 else
138 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
139 endif
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -0700140
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300141 # We need to add GCC toolchain to the path no matter what
142 # for tools like `as`
143 KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700144
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300145 ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
146 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
147 else
148 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
149 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700150
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300151 # Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
152 ifeq ($(KERNEL_ARCH),arm64)
153 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
154 KERNEL_CROSS_COMPILE += CROSS_COMPILE_COMPAT="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
155 endif
156
157 ifeq ($(TARGET_KERNEL_CLANG_COMPILE),false)
158 ifeq ($(KERNEL_ARCH),arm)
159 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
160 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
161 endif
162
163 ifeq ($(KERNEL_ARCH),arm64)
164 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
165 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
166 endif
167 endif
168
Michael Bestas63354f72022-11-01 14:54:44 +0200169 KERNEL_MAKE_FLAGS += CPATH="/usr/include:/usr/include/x86_64-linux-gnu" HOSTLDFLAGS="-L/usr/lib/x86_64-linux-gnu -L/usr/lib64 -fuse-ld=lld"
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700170
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300171 ifeq ($(KERNEL_ARCH),arm64)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300172 # Add 32-bit GCC to PATH so that arm-linux-androidkernel-as is available for CONFIG_COMPAT_VDSO
173 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(KERNEL_TOOLCHAIN_arm):$$PATH
174 else
175 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$$PATH
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300176 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700177
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300178 # Set the full path to the clang command and LLVM binutils
Michael Bestasb9c73042022-11-01 14:41:42 +0200179 KERNEL_MAKE_FLAGS += HOSTCC=$(TARGET_KERNEL_CLANG_PATH)/bin/clang
180 KERNEL_MAKE_FLAGS += HOSTCXX=$(TARGET_KERNEL_CLANG_PATH)/bin/clang++
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300181 ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
182 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
Michael Bestasb9c73042022-11-01 14:41:42 +0200183 KERNEL_MAKE_FLAGS += LD=$(TARGET_KERNEL_CLANG_PATH)/bin/ld.lld
184 KERNEL_MAKE_FLAGS += AR=$(TARGET_KERNEL_CLANG_PATH)/bin/llvm-ar
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300185 endif
Michael Bestasae531bc2022-10-01 00:38:14 +0300186 endif
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300187else
188 KERNEL_MAKE_FLAGS += HOSTCFLAGS="--sysroot=$(BUILD_TOP)/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/sysroot -I$(BUILD_TOP)/prebuilts/kernel-build-tools/linux-x86/include"
189 KERNEL_MAKE_FLAGS += HOSTLDFLAGS="--sysroot=$(BUILD_TOP)/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/sysroot -Wl,-rpath,$(BUILD_TOP)/prebuilts/kernel-build-tools/linux-x86/lib64 -L $(BUILD_TOP)/prebuilts/kernel-build-tools/linux-x86/lib64 -fuse-ld=lld --rtlib=compiler-rt"
190
Michael Bestasb9c73042022-11-01 14:41:42 +0200191 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(TARGET_KERNEL_CLANG_PATH)/bin:$$PATH
Michael Bestasae531bc2022-10-01 00:38:14 +0300192endif
193
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700194# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200195ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300196 TARGET_KERNEL_DTBO_PREFIX ?=
197 TARGET_KERNEL_DTBO ?= dtbo.img
198 BOARD_PREBUILT_DTBOIMAGE ?= $(TARGET_OUT_INTERMEDIATES)/DTBO_OBJ/arch/$(KERNEL_ARCH)/boot/$(TARGET_KERNEL_DTBO_PREFIX)$(TARGET_KERNEL_DTBO)
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700199endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700200
Michael Bestasa7999b02022-08-10 05:29:55 +0300201# Set the default dtb target
202TARGET_KERNEL_DTB ?= dtbs
203
Neel Chauhane546ac82022-07-21 09:30:48 -0700204# Set no external modules by default
205TARGET_KERNEL_EXT_MODULE_ROOT ?=
206TARGET_KERNEL_EXT_MODULES ?=
207
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700208# Set use the full path to the make command
Bruno Martins63d13c32020-09-24 11:51:33 +0100209KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700210
Michael Bestas4d700e72022-09-08 04:19:32 +0300211# Use LLVM's substitutes for GNU binutils
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700212ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300213 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
214 KERNEL_MAKE_FLAGS += LLVM=1 LLVM_IAS=1
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300215 endif
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700216endif
217
LuK13374ca87812023-10-06 09:42:53 +0200218# Pass prebuilt LZ4 path
219KERNEL_MAKE_FLAGS += LZ4=$(BUILD_TOP)/prebuilts/kernel-build-tools/linux-x86/bin/lz4
220
dianlujitaoa24144f2020-03-11 19:05:29 +0800221# Since Linux 4.16, flex and bison are required
Bruno Martins63d13c32020-09-24 11:51:33 +0100222KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/flex
223KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/bison
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300224KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800225TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
226
Bruno Martinsca8fd562023-09-19 15:59:00 +0100227# Since Linux 5.10, pahole is required
228KERNEL_MAKE_FLAGS += PAHOLE=$(BUILD_TOP)/prebuilts/kernel-build-tools/linux-x86/bin/pahole
229
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700230# Set the out dir for the kernel's O= arg
231# This needs to be an absolute path, so only set this if the standard out dir isn't used
232OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
233KERNEL_BUILD_OUT_PREFIX :=
234ifeq ($(OUT_DIR_PREFIX),out)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300235 KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700236endif