blob: a3b58fb9aa752fde1a7021a0c4b126c0d483ba44 [file] [log] [blame]
Bruno Martins63d13c32020-09-24 11:51:33 +01001# Copyright (C) 2018-2020 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)
Rashed Abdel-Tawab3254caf2018-06-15 22:45:53 -070023# TARGET_KERNEL_ADDITIONAL_FLAGS = Additional make flags, optional
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070024# TARGET_KERNEL_ARCH = Kernel Arch
25# TARGET_KERNEL_CROSS_COMPILE_PREFIX = Compiler prefix (e.g. arm-eabi-)
26# defaults to arm-linux-androidkernel- for arm
dianlujitaobd16a8e2019-09-08 12:44:49 +080027# aarch64-linux-android- for arm64
28# x86_64-linux-android- for x86
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070029#
Alexander Koskoviche8e46e62022-03-07 11:29:34 -070030# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to true
Park Ju Hyung4e345c12020-01-06 10:53:00 +090031# TARGET_KERNEL_NEW_GCC_COMPILE = Compile kernel with newer version GCC, defaults to false
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070032#
33# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
34# Set this var in shell to override
35# toolchain specified in BoardConfig.mk
36# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
37# TARGET_KERNEL_CROSS_COMPILE_PREFIX
38# is in PATH
39# USE_CCACHE = Enable ccache (global Android flag)
40
41BUILD_TOP := $(shell pwd)
42
43TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
44TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070045
46TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
47ifeq ($(TARGET_KERNEL_ARCH),)
48KERNEL_ARCH := $(TARGET_ARCH)
49else
50KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
51endif
52
Arianab400452022-08-17 18:14:15 +020053CLANG_PREBUILTS := $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/clang-r450784d
Bruno Martins63d13c32020-09-24 11:51:33 +010054GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_PREBUILT_TAG)
Park Ju Hyung4e345c12020-01-06 10:53:00 +090055ifeq ($(TARGET_KERNEL_NEW_GCC_COMPILE),true)
56 ifeq ($(TARGET_KERNEL_CLANG_COMPILE),true)
57 $(error TARGET_KERNEL_NEW_GCC_COMPILE cannot be used with TARGET_KERNEL_CLANG_COMPILE!)
58 endif
59 # arm64 toolchain
60 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-elf/bin
61 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-elf-
62 # arm toolchain
63 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-eabi/bin
64 KERNEL_TOOLCHAIN_PREFIX_arm := arm-eabi-
65else
66 # arm64 toolchain
67 KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
68 KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
69 # arm toolchain
70 KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
71 KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
72 # x86 toolchain
73 KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
74 KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
75endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070076TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
77ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
78KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -070079else
80KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
81KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070082endif
83
84ifeq ($(KERNEL_TOOLCHAIN),)
85KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -070086else
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070087KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
88endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070089
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -070090# We need to add GCC toolchain to the path no matter what
91# for tools like `as`
Alessandro Astone8fb7cda2021-12-11 01:49:53 +010092KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -070093
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070094ifneq ($(USE_CCACHE),)
Rashed Abdel-Tawab3ad0e0d2019-09-05 21:26:19 -070095 ifneq ($(CCACHE_EXEC),)
96 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
97 CCACHE_BIN := $(CCACHE_EXEC)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070098 endif
99endif
100
Alexander Koskoviche8e46e62022-03-07 11:29:34 -0700101ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700102 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
103else
104 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
105endif
106
107# Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
108ifeq ($(KERNEL_ARCH),arm64)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -0700109 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
LibXZR7e0bb362021-08-04 11:31:47 +0800110 KERNEL_CROSS_COMPILE += CROSS_COMPILE_COMPAT="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700111endif
112
dianlujitao4fae7882020-09-16 10:40:57 +0800113prebuilt_build_tools_lineage := prebuilts/tools-lineage
114prebuilt_build_tools_lineage_bin := $(prebuilt_build_tools_lineage)/$(HOST_PREBUILT_TAG)/bin
115
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700116# Clear this first to prevent accidental poisoning from env
117KERNEL_MAKE_FLAGS :=
118
Ethan Chen995aff22019-02-09 14:59:56 -0800119# Add back threads, ninja cuts this to $(nproc)/2
dianlujitaoef7cee22020-09-16 10:40:57 +0800120KERNEL_MAKE_FLAGS += -j$(shell prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin/nproc --all)
Ethan Chen995aff22019-02-09 14:59:56 -0800121
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700122ifeq ($(KERNEL_ARCH),arm)
123 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
124 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
125endif
126
127ifeq ($(KERNEL_ARCH),arm64)
128 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
129 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
130endif
131
132ifeq ($(HOST_OS),darwin)
LuK133750118c42020-09-22 23:11:21 +0200133 KERNEL_MAKE_FLAGS += HOSTCFLAGS="-I$(BUILD_TOP)/external/elfutils/libelf -I/usr/local/opt/openssl/include -fuse-ld=lld" HOSTLDFLAGS="-L/usr/local/opt/openssl/lib -fuse-ld=lld"
Rashed Abdel-Tawabd7e24e32019-09-05 21:29:27 -0700134else
LuK133750118c42020-09-22 23:11:21 +0200135 KERNEL_MAKE_FLAGS += CPATH="/usr/include:/usr/include/x86_64-linux-gnu" HOSTCFLAGS="-fuse-ld=lld" HOSTLDFLAGS="-L/usr/lib/x86_64-linux-gnu -L/usr/lib64 -fuse-ld=lld"
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700136endif
Rashed Abdel-Tawab06539d42019-09-27 06:46:05 -0700137
Rashed Abdel-Tawab3254caf2018-06-15 22:45:53 -0700138ifneq ($(TARGET_KERNEL_ADDITIONAL_FLAGS),)
139 KERNEL_MAKE_FLAGS += $(TARGET_KERNEL_ADDITIONAL_FLAGS)
140endif
141
LuK13379d8578b2019-09-30 17:55:58 +0200142TOOLS_PATH_OVERRIDE := \
Bruno Martins63d13c32020-09-24 11:51:33 +0100143 LD_LIBRARY_PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/lib:$$LD_LIBRARY_PATH \
144 PERL5LIB=$(BUILD_TOP)/prebuilts/tools-lineage/common/perl-base
Rashed Abdel-Tawabdfc3d182019-09-06 10:44:50 -0700145
LuK1337321a8952021-10-17 15:21:24 +0200146ifeq ($(KERNEL_ARCH),arm64)
147 # Add 32-bit GCC to PATH so that arm-linux-androidkernel-as is available for CONFIG_COMPAT_VDSO
148 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$(KERNEL_TOOLCHAIN_arm):$$PATH
149else
150 TOOLS_PATH_OVERRIDE += PATH=$(BUILD_TOP)/prebuilts/tools-lineage/$(HOST_PREBUILT_TAG)/bin:$$PATH
151endif
152
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700153# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200154ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
155BOARD_PREBUILT_DTBOIMAGE ?= $(TARGET_OUT_INTERMEDIATES)/DTBO_OBJ/arch/$(KERNEL_ARCH)/boot/dtbo.img
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700156endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700157
158# Set use the full path to the make command
Bruno Martins63d13c32020-09-24 11:51:33 +0100159KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700160
LuK133750118c42020-09-22 23:11:21 +0200161# Set the full path to the clang command
162KERNEL_MAKE_FLAGS += HOSTCC=$(CLANG_PREBUILTS)/bin/clang
163KERNEL_MAKE_FLAGS += HOSTCXX=$(CLANG_PREBUILTS)/bin/clang++
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700164
dianlujitaoa24144f2020-03-11 19:05:29 +0800165# Since Linux 4.16, flex and bison are required
Bruno Martins63d13c32020-09-24 11:51:33 +0100166KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/flex
167KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_PREBUILT_TAG)/bin/bison
Alessandro Astoneceb70862020-09-13 19:43:00 +0200168KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_OS)-x86/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800169TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
170
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700171# Set the out dir for the kernel's O= arg
172# This needs to be an absolute path, so only set this if the standard out dir isn't used
173OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
174KERNEL_BUILD_OUT_PREFIX :=
175ifeq ($(OUT_DIR_PREFIX),out)
176KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
177endif