blob: 4b60438774885b90b5c1c936877efa711be88e36 [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 Bestas4d700e72022-09-08 04:19:32 +030030# TARGET_KERNEL_LLVM_BINUTILS = Use LLVM binutils, defaults to true
Alexander Koskovich8655c4a2022-06-15 08:46:57 -070031# TARGET_KERNEL_VERSION = Reported kernel version in top level kernel
32# makefile. Can be overriden in device trees
33# in the event of prebuilt kernel.
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070034#
Michael Bestas581a3862022-08-10 06:44:40 +030035# TARGET_KERNEL_DTBO_PREFIX = Override path prefix of TARGET_KERNEL_DTBO.
36# Defaults to empty
Michael Bestas15a631e2022-08-10 05:18:59 +030037# TARGET_KERNEL_DTBO = Name of the kernel Makefile target that
38# generates dtbo.img. Defaults to dtbo.img
Michael Bestasa7999b02022-08-10 05:29:55 +030039# TARGET_KERNEL_DTB = Name of the kernel Makefile target that
40# generates the *.dtb targets. Defaults to dtbs
Michael Bestas15a631e2022-08-10 05:18:59 +030041#
Neel Chauhane546ac82022-07-21 09:30:48 -070042# TARGET_KERNEL_EXT_MODULE_ROOT = Optional, the external modules root directory
43# Defaults to empty
44# TARGET_KERNEL_EXT_MODULES = Optional, the external modules we are
45# building. Defaults to empty
46#
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070047# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
48# Set this var in shell to override
49# toolchain specified in BoardConfig.mk
50# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
51# TARGET_KERNEL_CROSS_COMPILE_PREFIX
52# is in PATH
53# USE_CCACHE = Enable ccache (global Android flag)
54
Jason Wojcik4916cab2021-03-23 10:46:09 -070055BUILD_TOP := $(abspath .)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070056
57TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
58TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070059
60TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
61ifeq ($(TARGET_KERNEL_ARCH),)
Michael Bestasf0b1dd62022-10-01 00:33:17 +030062 KERNEL_ARCH := $(TARGET_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070063else
Michael Bestasf0b1dd62022-10-01 00:33:17 +030064 KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070065endif
66
Chirayu Desai63dbd4e2022-07-08 03:33:22 +053067KERNEL_VERSION := $(shell grep -s "^VERSION = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
68KERNEL_PATCHLEVEL := $(shell grep -s "^PATCHLEVEL = " $(TARGET_KERNEL_SOURCE)/Makefile | awk '{ print $$3 }')
Alexander Koskovich8655c4a2022-06-15 08:46:57 -070069TARGET_KERNEL_VERSION ?= $(shell echo $(KERNEL_VERSION)"."$(KERNEL_PATCHLEVEL))
70
Arianab400452022-08-17 18:14:15 +020071CLANG_PREBUILTS := $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/clang-r450784d
Michael Bestasae531bc2022-10-01 00:38:14 +030072
73ifneq ($(USE_CCACHE),)
74 ifneq ($(CCACHE_EXEC),)
75 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
76 CCACHE_BIN := $(CCACHE_EXEC)
77 endif
78endif
79
80# Clear this first to prevent accidental poisoning from env
81KERNEL_MAKE_FLAGS :=
82
83# Add back threads, ninja cuts this to $(nproc)/2
84KERNEL_MAKE_FLAGS += -j$(shell prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin/nproc --all)
85
86TOOLS_PATH_OVERRIDE := \
87 LD_LIBRARY_PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/lib:$$LD_LIBRARY_PATH \
88 PERL5LIB=$(BUILD_TOP)/prebuilts/tools-lineage/common/perl-base
89
Michael Bestas3b11b2a2022-09-10 23:19:55 +030090# 5.10+ can fully compile without gcc
91ifeq (,$(filter 5.10, $(TARGET_KERNEL_VERSION)))
92 GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_PREBUILT_TAG)
93 # arm64 toolchain
94 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
95 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
96 # arm toolchain
97 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
98 KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
99 # x86 toolchain
100 KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
101 KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700102
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300103 TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
104 ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
105 KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
106 else
107 KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
108 KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
109 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700110
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300111 ifeq ($(KERNEL_TOOLCHAIN),)
112 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
113 else
114 KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
115 endif
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -0700116
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300117 # We need to add GCC toolchain to the path no matter what
118 # for tools like `as`
119 KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700120
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300121 ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
122 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
123 else
124 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
125 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700126
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300127 # Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
128 ifeq ($(KERNEL_ARCH),arm64)
129 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
130 KERNEL_CROSS_COMPILE += CROSS_COMPILE_COMPAT="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
131 endif
132
133 ifeq ($(TARGET_KERNEL_CLANG_COMPILE),false)
134 ifeq ($(KERNEL_ARCH),arm)
135 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
136 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
137 endif
138
139 ifeq ($(KERNEL_ARCH),arm64)
140 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
141 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
142 endif
143 endif
144
145 ifeq ($(HOST_OS),darwin)
146 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"
147 else
148 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 +0300149 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700150
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300151 ifeq ($(KERNEL_ARCH),arm64)
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300152 # Add 32-bit GCC to PATH so that arm-linux-androidkernel-as is available for CONFIG_COMPAT_VDSO
153 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(KERNEL_TOOLCHAIN_arm):$$PATH
154 else
155 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$$PATH
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300156 endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700157
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300158 # Set the full path to the clang command and LLVM binutils
159 KERNEL_MAKE_FLAGS += HOSTCC=$(CLANG_PREBUILTS)/bin/clang
160 KERNEL_MAKE_FLAGS += HOSTCXX=$(CLANG_PREBUILTS)/bin/clang++
161 ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
162 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
163 KERNEL_MAKE_FLAGS += LD=$(CLANG_PREBUILTS)/bin/ld.lld
164 KERNEL_MAKE_FLAGS += AR=$(CLANG_PREBUILTS)/bin/llvm-ar
165 endif
Michael Bestasae531bc2022-10-01 00:38:14 +0300166 endif
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300167else
168 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"
169 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"
170
171 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(CLANG_PREBUILTS)/bin:$$PATH
Michael Bestasae531bc2022-10-01 00:38:14 +0300172endif
173
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700174# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200175ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300176 TARGET_KERNEL_DTBO_PREFIX ?=
177 TARGET_KERNEL_DTBO ?= dtbo.img
178 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 -0700179endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700180
Michael Bestasa7999b02022-08-10 05:29:55 +0300181# Set the default dtb target
182TARGET_KERNEL_DTB ?= dtbs
183
Neel Chauhane546ac82022-07-21 09:30:48 -0700184# Set no external modules by default
185TARGET_KERNEL_EXT_MODULE_ROOT ?=
186TARGET_KERNEL_EXT_MODULES ?=
187
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700188# Set use the full path to the make command
Bruno Martins63d13c32020-09-24 11:51:33 +0100189KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700190
Michael Bestas4d700e72022-09-08 04:19:32 +0300191# Use LLVM's substitutes for GNU binutils
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700192ifneq ($(TARGET_KERNEL_CLANG_COMPILE), false)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300193 ifneq ($(TARGET_KERNEL_LLVM_BINUTILS), false)
194 KERNEL_MAKE_FLAGS += LLVM=1 LLVM_IAS=1
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300195 endif
Alexander Koskovich8655c4a2022-06-15 08:46:57 -0700196endif
197
dianlujitaoa24144f2020-03-11 19:05:29 +0800198# Since Linux 4.16, flex and bison are required
Bruno Martins63d13c32020-09-24 11:51:33 +0100199KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/flex
200KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/bison
Michael Bestas3b11b2a2022-09-10 23:19:55 +0300201KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800202TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
203
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700204# Set the out dir for the kernel's O= arg
205# This needs to be an absolute path, so only set this if the standard out dir isn't used
206OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
207KERNEL_BUILD_OUT_PREFIX :=
208ifeq ($(OUT_DIR_PREFIX),out)
Michael Bestasf0b1dd62022-10-01 00:33:17 +0300209 KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700210endif