blob: 77dfa9a6cf17c19a411143e4818ac4b5f4449fe6 [file] [log] [blame]
Jackeagle4231fb82022-12-02 08:56:09 -05001# Copyright (C) 2018-2022 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
Alexander Koskovich8655c4a2022-06-15 08:46:57 -070034# TARGET_KERNEL_VERSION = Reported kernel version in top level kernel
35# makefile. Can be overriden in device trees
36# in the event of prebuilt kernel.
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070037#
Michael Bestas581a3862022-08-10 06:44:40 +030038# TARGET_KERNEL_DTBO_PREFIX = Override path prefix of TARGET_KERNEL_DTBO.
39# Defaults to empty
Michael Bestas15a631e2022-08-10 05:18:59 +030040# TARGET_KERNEL_DTBO = Name of the kernel Makefile target that
41# generates dtbo.img. Defaults to dtbo.img
Michael Bestasa7999b02022-08-10 05:29:55 +030042# TARGET_KERNEL_DTB = Name of the kernel Makefile target that
43# generates the *.dtb targets. Defaults to dtbs
Michael Bestas15a631e2022-08-10 05:18:59 +030044#
Neel Chauhane546ac82022-07-21 09:30:48 -070045# TARGET_KERNEL_EXT_MODULE_ROOT = Optional, the external modules root directory
46# Defaults to empty
47# TARGET_KERNEL_EXT_MODULES = Optional, the external modules we are
48# building. Defaults to empty
49#
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070050# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
51# Set this var in shell to override
52# toolchain specified in BoardConfig.mk
53# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
54# TARGET_KERNEL_CROSS_COMPILE_PREFIX
55# is in PATH
56# USE_CCACHE = Enable ccache (global Android flag)
57
Jason Wojcik4916cab2021-03-23 10:46:09 -070058BUILD_TOP := $(abspath .)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070059
60TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
61TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070062
63TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
64ifeq ($(TARGET_KERNEL_ARCH),)
Michael Bestasf0b1dd62022-10-01 00:33:17 +030065 KERNEL_ARCH := $(TARGET_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070066else
Michael Bestasf0b1dd62022-10-01 00:33:17 +030067 KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070068endif
69
Chirayu Desai63dbd4e2022-07-08 03:33:22 +053070KERNEL_VERSION := $(shell grep -s "^VERSION = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
71KERNEL_PATCHLEVEL := $(shell grep -s "^PATCHLEVEL = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
Alexander Koskovich8655c4a2022-06-15 08:46:57 -070072TARGET_KERNEL_VERSION ?= $(shell echo $(KERNEL_VERSION)"."$(KERNEL_PATCHLEVEL))
73
Michael Bestasb9c73042022-11-01 14:41:42 +020074ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
75 KERNEL_CLANG_VERSION := clang-$(TARGET_KERNEL_CLANG_VERSION)
76else
77 # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
78 KERNEL_CLANG_VERSION := clang-r450784d
79endif
80TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/$(KERNEL_CLANG_VERSION)
Michael Bestasae531bc2022-10-01 00:38:14 +030081
82ifneq ($(USE_CCACHE),)
83 ifneq ($(CCACHE_EXEC),)
84 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
85 CCACHE_BIN := $(CCACHE_EXEC)
86 endif
87endif
88
89# Clear this first to prevent accidental poisoning from env
90KERNEL_MAKE_FLAGS :=
91
Alexander Koskovich76ce4f82022-08-04 13:00:07 -070092# Add back threads, ninja cuts this to $(getconf _NPROCESSORS_ONLN)/2
93KERNEL_MAKE_FLAGS += -j$(shell getconf _NPROCESSORS_ONLN)
Michael Bestasae531bc2022-10-01 00:38:14 +030094
95TOOLS_PATH_OVERRIDE := \
96 LD_LIBRARY_PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/lib:$$LD_LIBRARY_PATH \
97 PERL5LIB=$(BUILD_TOP)/prebuilts/tools-lineage/common/perl-base
98
Michael Bestas3b11b2a2022-09-10 23:19:55 +030099# 5.10+ can fully compile without gcc
100ifeq (,$(filter 5.10, $(TARGET_KERNEL_VERSION)))
101 GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_PREBUILT_TAG)
102 # arm64 toolchain
103 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
104 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
105 # arm toolchain
106 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
107 KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
108 # x86 toolchain
109 KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
110 KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700111
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300112 TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
113 ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
114 KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
115 else
116 KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
117 KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
118 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700119
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300120 ifeq ($(KERNEL_TOOLCHAIN),)
121 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
122 else
123 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
124 endif
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -0700125
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300126 # We need to add GCC toolchain to the path no matter what
127 # for tools like `as`
128 KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700129
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300130 ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
131 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
132 else
133 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
134 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700135
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300136 # Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
137 ifeq ($(KERNEL_ARCH),arm64)
138 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
139 KERNEL_CROSS_COMPILE += CROSS_COMPILE_COMPAT="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
140 endif
141
142 ifeq ($(TARGET_KERNEL_CLANG_COMPILE),false)
143 ifeq ($(KERNEL_ARCH),arm)
144 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
145 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
146 endif
147
148 ifeq ($(KERNEL_ARCH),arm64)
149 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
150 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
151 endif
152 endif
153
154 ifeq ($(HOST_OS),darwin)
155 KERNEL_MAKE_FLAGS += HOSTCFLAGS="-I$(BUILD_TOP)/external/elfutils/libelf -I/usr/local/opt/openssl/include" HOSTLDFLAGS="-L/usr/local/opt/openssl/lib -fuse-ld=lld"
156 else
157 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"
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300158 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700159
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300160 ifeq ($(KERNEL_ARCH),arm64)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300161 # Add 32-bit GCC to PATH so that arm-linux-androidkernel-as is available for CONFIG_COMPAT_VDSO
162 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(KERNEL_TOOLCHAIN_arm):$$PATH
163 else
164 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$$PATH
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300165 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700166
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300167 # Set the full path to the clang command and LLVM binutils
Michael Bestasb9c73042022-11-01 14:41:42 +0200168 KERNEL_MAKE_FLAGS += HOSTCC=$(TARGET_KERNEL_CLANG_PATH)/bin/clang
169 KERNEL_MAKE_FLAGS += HOSTCXX=$(TARGET_KERNEL_CLANG_PATH)/bin/clang++
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300170 ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
171 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
Michael Bestasb9c73042022-11-01 14:41:42 +0200172 KERNEL_MAKE_FLAGS += LD=$(TARGET_KERNEL_CLANG_PATH)/bin/ld.lld
173 KERNEL_MAKE_FLAGS += AR=$(TARGET_KERNEL_CLANG_PATH)/bin/llvm-ar
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300174 endif
Michael Bestasae531bc2022-10-01 00:38:14 +0300175 endif
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300176else
177 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"
178 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"
179
Michael Bestasb9c73042022-11-01 14:41:42 +0200180 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 +0300181endif
182
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700183# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200184ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300185 TARGET_KERNEL_DTBO_PREFIX ?=
186 TARGET_KERNEL_DTBO ?= dtbo.img
187 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 -0700188endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700189
Michael Bestasa7999b02022-08-10 05:29:55 +0300190# Set the default dtb target
191TARGET_KERNEL_DTB ?= dtbs
192
Neel Chauhane546ac82022-07-21 09:30:48 -0700193# Set no external modules by default
194TARGET_KERNEL_EXT_MODULE_ROOT ?=
195TARGET_KERNEL_EXT_MODULES ?=
196
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700197# Set use the full path to the make command
Bruno Martins63d13c32020-09-24 11:51:33 +0100198KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700199
Michael Bestas4d700e72022-09-08 04:19:32 +0300200# Use LLVM's substitutes for GNU binutils
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700201ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300202 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
203 KERNEL_MAKE_FLAGS += LLVM=1 LLVM_IAS=1
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300204 endif
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700205endif
206
dianlujitaoa24144f2020-03-11 19:05:29 +0800207# Since Linux 4.16, flex and bison are required
Bruno Martins63d13c32020-09-24 11:51:33 +0100208KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/flex
209KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/bison
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300210KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800211TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
212
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700213# Set the out dir for the kernel's O= arg
214# This needs to be an absolute path, so only set this if the standard out dir isn't used
215OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
216KERNEL_BUILD_OUT_PREFIX :=
217ifeq ($(OUT_DIR_PREFIX),out)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300218 KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700219endif