blob: 0cb175c8c308abba9269f1cb8942b58e1215e33a [file] [log] [blame]
Bruno Martins44b85532023-05-11 21:53:03 +01001# Copyright (C) 2018-2023 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),)
78 ifeq ($(shell expr $(KERNEL_VERSION) \>= 5), 1)
79 ifeq ($(shell expr $(KERNEL_PATCHLEVEL) \>= 10), 1)
80 TARGET_KERNEL_NO_GCC ?= true
81 endif
Bruno Martins44b85532023-05-11 21:53:03 +010082 endif
Michael Bestas38ba7c02022-11-14 13:09:00 +020083endif
84
85ifeq ($(TARGET_KERNEL_NO_GCC), true)
86 KERNEL_NO_GCC := true
87endif
88
Michael Bestasb9c73042022-11-01 14:41:42 +020089ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
90 KERNEL_CLANG_VERSION := clang-$(TARGET_KERNEL_CLANG_VERSION)
91else
92 # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
basamaryan97ca19d2023-10-04 16:59:19 +020093 KERNEL_CLANG_VERSION := clang-r487747c
Michael Bestasb9c73042022-11-01 14:41:42 +020094endif
95TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/$(KERNEL_CLANG_VERSION)
Michael Bestasae531bc2022-10-01 00:38:14 +030096
97ifneq ($(USE_CCACHE),)
98 ifneq ($(CCACHE_EXEC),)
99 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
100 CCACHE_BIN := $(CCACHE_EXEC)
101 endif
102endif
103
104# Clear this first to prevent accidental poisoning from env
105KERNEL_MAKE_FLAGS :=
106
Alexander Koskovich76ce4f82022-08-04 13:00:07 -0700107# Add back threads, ninja cuts this to $(getconf _NPROCESSORS_ONLN)/2
108KERNEL_MAKE_FLAGS += -j$(shell getconf _NPROCESSORS_ONLN)
Michael Bestasae531bc2022-10-01 00:38:14 +0300109
110TOOLS_PATH_OVERRIDE := \
Michael Bestasae531bc2022-10-01 00:38:14 +0300111 PERL5LIB=$(BUILD_TOP)/prebuilts/tools-lineage/common/perl-base
112
Michael Bestas38ba7c02022-11-14 13:09:00 +0200113ifneq ($(KERNEL_NO_GCC), true)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300114 GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_PREBUILT_TAG)
115 # arm64 toolchain
116 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
117 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
118 # arm toolchain
119 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
120 KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
121 # x86 toolchain
122 KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
123 KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700124
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300125 TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
126 ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
127 KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
128 else
129 KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
130 KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
131 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700132
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300133 ifeq ($(KERNEL_TOOLCHAIN),)
134 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
135 else
136 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
137 endif
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -0700138
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300139 # We need to add GCC toolchain to the path no matter what
140 # for tools like `as`
141 KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700142
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300143 ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
144 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
145 else
146 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
147 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700148
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300149 # Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
150 ifeq ($(KERNEL_ARCH),arm64)
151 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
152 KERNEL_CROSS_COMPILE += CROSS_COMPILE_COMPAT="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
153 endif
154
155 ifeq ($(TARGET_KERNEL_CLANG_COMPILE),false)
156 ifeq ($(KERNEL_ARCH),arm)
157 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
158 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
159 endif
160
161 ifeq ($(KERNEL_ARCH),arm64)
162 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
163 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
164 endif
165 endif
166
Michael Bestas63354f72022-11-01 14:54:44 +0200167 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 -0700168
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300169 ifeq ($(KERNEL_ARCH),arm64)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300170 # Add 32-bit GCC to PATH so that arm-linux-androidkernel-as is available for CONFIG_COMPAT_VDSO
171 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(KERNEL_TOOLCHAIN_arm):$$PATH
172 else
173 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$$PATH
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300174 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700175
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300176 # Set the full path to the clang command and LLVM binutils
Michael Bestasb9c73042022-11-01 14:41:42 +0200177 KERNEL_MAKE_FLAGS += HOSTCC=$(TARGET_KERNEL_CLANG_PATH)/bin/clang
178 KERNEL_MAKE_FLAGS += HOSTCXX=$(TARGET_KERNEL_CLANG_PATH)/bin/clang++
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300179 ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
180 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
Michael Bestasb9c73042022-11-01 14:41:42 +0200181 KERNEL_MAKE_FLAGS += LD=$(TARGET_KERNEL_CLANG_PATH)/bin/ld.lld
182 KERNEL_MAKE_FLAGS += AR=$(TARGET_KERNEL_CLANG_PATH)/bin/llvm-ar
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300183 endif
Michael Bestasae531bc2022-10-01 00:38:14 +0300184 endif
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300185else
186 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"
187 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"
188
Michael Bestasb9c73042022-11-01 14:41:42 +0200189 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 +0300190endif
191
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700192# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200193ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300194 TARGET_KERNEL_DTBO_PREFIX ?=
195 TARGET_KERNEL_DTBO ?= dtbo.img
196 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 -0700197endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700198
Michael Bestasa7999b02022-08-10 05:29:55 +0300199# Set the default dtb target
200TARGET_KERNEL_DTB ?= dtbs
201
Neel Chauhane546ac82022-07-21 09:30:48 -0700202# Set no external modules by default
203TARGET_KERNEL_EXT_MODULE_ROOT ?=
204TARGET_KERNEL_EXT_MODULES ?=
205
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700206# Set use the full path to the make command
Bruno Martins63d13c32020-09-24 11:51:33 +0100207KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700208
Michael Bestas4d700e72022-09-08 04:19:32 +0300209# Use LLVM's substitutes for GNU binutils
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700210ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300211 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
212 KERNEL_MAKE_FLAGS += LLVM=1 LLVM_IAS=1
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300213 endif
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700214endif
215
dianlujitaoa24144f2020-03-11 19:05:29 +0800216# Since Linux 4.16, flex and bison are required
Bruno Martins63d13c32020-09-24 11:51:33 +0100217KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/flex
218KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/bison
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300219KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800220TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
221
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700222# Set the out dir for the kernel's O= arg
223# This needs to be an absolute path, so only set this if the standard out dir isn't used
224OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
225KERNEL_BUILD_OUT_PREFIX :=
226ifeq ($(OUT_DIR_PREFIX),out)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300227 KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700228endif