blob: 83c7114e55e6fdfe060cbf9777be8f90e5d526fa [file] [log] [blame]
Rashed Abdel-Tawab06539d42019-09-27 06:46:05 -07001# Copyright (C) 2018-2019 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#
30# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to false
31#
32# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
33# Set this var in shell to override
34# toolchain specified in BoardConfig.mk
35# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
36# TARGET_KERNEL_CROSS_COMPILE_PREFIX
37# is in PATH
38# USE_CCACHE = Enable ccache (global Android flag)
39
40BUILD_TOP := $(shell pwd)
41
42TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
43TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
44ifneq ($(TARGET_PREBUILT_KERNEL),)
45TARGET_KERNEL_SOURCE :=
46endif
47
48TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
49ifeq ($(TARGET_KERNEL_ARCH),)
50KERNEL_ARCH := $(TARGET_ARCH)
51else
52KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
53endif
54
LuK13375b5e7352020-09-22 23:11:21 +020055CLANG_PREBUILTS := $(BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/clang-r383902b
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -070056GCC_PREBUILTS := $(BUILD_TOP)/prebuilts/gcc/$(HOST_OS)-x86
57# arm64 toolchain
58KERNEL_TOOLCHAIN_arm64 := $(GCC_PREBUILTS)/aarch64/aarch64-linux-android-4.9/bin
59KERNEL_TOOLCHAIN_PREFIX_arm64 := aarch64-linux-android-
60# arm toolchain
61KERNEL_TOOLCHAIN_arm := $(GCC_PREBUILTS)/arm/arm-linux-androideabi-4.9/bin
62KERNEL_TOOLCHAIN_PREFIX_arm := arm-linux-androidkernel-
63# x86 toolchain
64KERNEL_TOOLCHAIN_x86 := $(GCC_PREBUILTS)/x86/x86_64-linux-android-4.9/bin
65KERNEL_TOOLCHAIN_PREFIX_x86 := x86_64-linux-android-
66
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070067TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
68ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
69KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -070070else
71KERNEL_TOOLCHAIN ?= $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))
72KERNEL_TOOLCHAIN_PREFIX ?= $(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070073endif
74
75ifeq ($(KERNEL_TOOLCHAIN),)
76KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -070077else
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070078KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
79endif
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070080
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -070081# We need to add GCC toolchain to the path no matter what
82# for tools like `as`
Rashed Abdel-Tawab168fd232019-09-24 17:22:39 +020083KERNEL_TOOLCHAIN_PATH_gcc := $(KERNEL_TOOLCHAIN_$(KERNEL_ARCH))/$(KERNEL_TOOLCHAIN_PREFIX_$(KERNEL_ARCH))
Rashed Abdel-Tawabc6485192019-09-10 08:38:34 -070084
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070085ifneq ($(USE_CCACHE),)
Rashed Abdel-Tawab3ad0e0d2019-09-05 21:26:19 -070086 ifneq ($(CCACHE_EXEC),)
87 # Android 10+ deprecates use of a build ccache. Only system installed ones are now allowed
88 CCACHE_BIN := $(CCACHE_EXEC)
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -070089 endif
90endif
91
92ifeq ($(TARGET_KERNEL_CLANG_COMPILE),true)
93 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
94else
95 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(CCACHE_BIN) $(KERNEL_TOOLCHAIN_PATH)"
96endif
97
98# Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
99ifeq ($(KERNEL_ARCH),arm64)
Rashed Abdel-Tawab493ad4b2019-09-06 14:26:34 -0700100 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="$(KERNEL_TOOLCHAIN_arm)/$(KERNEL_TOOLCHAIN_PREFIX_arm)"
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700101endif
102
dianlujitao4fae7882020-09-16 10:40:57 +0800103prebuilt_build_tools_lineage := prebuilts/tools-lineage
104prebuilt_build_tools_lineage_bin := $(prebuilt_build_tools_lineage)/$(HOST_PREBUILT_TAG)/bin
105
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700106# Clear this first to prevent accidental poisoning from env
107KERNEL_MAKE_FLAGS :=
108
Ethan Chen995aff22019-02-09 14:59:56 -0800109# Add back threads, ninja cuts this to $(nproc)/2
dianlujitao4fae7882020-09-16 10:40:57 +0800110KERNEL_MAKE_FLAGS += -j$(shell $(prebuilt_build_tools_lineage_bin)/nproc --all)
Ethan Chen995aff22019-02-09 14:59:56 -0800111
Rashed Abdel-Tawaba836d792018-09-20 15:19:57 -0700112ifeq ($(KERNEL_ARCH),arm)
113 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
114 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
115endif
116
117ifeq ($(KERNEL_ARCH),arm64)
118 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
119 KERNEL_MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
120endif
121
122ifeq ($(HOST_OS),darwin)
LuK13375b5e7352020-09-22 23:11:21 +0200123 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 -0700124else
LuK13375b5e7352020-09-22 23:11:21 +0200125 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 -0700126endif
Rashed Abdel-Tawab06539d42019-09-27 06:46:05 -0700127
Rashed Abdel-Tawab3254caf2018-06-15 22:45:53 -0700128ifneq ($(TARGET_KERNEL_ADDITIONAL_FLAGS),)
129 KERNEL_MAKE_FLAGS += $(TARGET_KERNEL_ADDITIONAL_FLAGS)
130endif
131
LuK13379d8578b2019-09-30 17:55:58 +0200132TOOLS_PATH_OVERRIDE := \
dianlujitao4fae7882020-09-16 10:40:57 +0800133 PATH=$(BUILD_TOP)/$(prebuilt_build_tools_lineage_bin):$$PATH \
134 LD_LIBRARY_PATH=$(BUILD_TOP)/$(prebuilt_build_tools_lineage)/$(HOST_PREBUILT_TAG)/lib:$$LD_LIBRARY_PATH \
135 PERL5LIB=$(BUILD_TOP)/$(prebuilt_build_tools_lineage)/common/perl-base
Rashed Abdel-Tawabdfc3d182019-09-06 10:44:50 -0700136
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700137# Set DTBO image locations so the build system knows to build them
Luca Stefanife00ea92020-08-02 19:08:31 +0200138ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
139BOARD_PREBUILT_DTBOIMAGE ?= $(TARGET_OUT_INTERMEDIATES)/DTBO_OBJ/arch/$(KERNEL_ARCH)/boot/dtbo.img
Rashed Abdel-Tawab60fe9352019-05-16 14:01:19 -0700140endif
Rashed Abdel-Tawabfb35b9d2019-09-05 21:21:13 -0700141
142# Set use the full path to the make command
143KERNEL_MAKE_CMD := $(BUILD_TOP)/prebuilts/build-tools/$(HOST_OS)-x86/bin/make
Rashed Abdel-Tawab21ee5982019-09-05 21:24:07 -0700144
LuK13375b5e7352020-09-22 23:11:21 +0200145# Set the full path to the clang command
146KERNEL_MAKE_FLAGS += HOSTCC=$(CLANG_PREBUILTS)/bin/clang
147KERNEL_MAKE_FLAGS += HOSTCXX=$(CLANG_PREBUILTS)/bin/clang++
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700148
dianlujitaoa24144f2020-03-11 19:05:29 +0800149# Since Linux 4.16, flex and bison are required
150KERNEL_MAKE_FLAGS += LEX=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_OS)-x86/bin/flex
151KERNEL_MAKE_FLAGS += YACC=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_OS)-x86/bin/bison
Alessandro Astoneceb70862020-09-13 19:43:00 +0200152KERNEL_MAKE_FLAGS += M4=$(BUILD_TOP)/prebuilts/build-tools/$(HOST_OS)-x86/bin/m4
dianlujitaoa24144f2020-03-11 19:05:29 +0800153TOOLS_PATH_OVERRIDE += BISON_PKGDATADIR=$(BUILD_TOP)/prebuilts/build-tools/common/bison
154
Rashed Abdel-Tawabba5ea352019-09-07 17:01:13 -0700155# Set the out dir for the kernel's O= arg
156# This needs to be an absolute path, so only set this if the standard out dir isn't used
157OUT_DIR_PREFIX := $(shell echo $(OUT_DIR) | sed -e 's|/target/.*$$||g')
158KERNEL_BUILD_OUT_PREFIX :=
159ifeq ($(OUT_DIR_PREFIX),out)
160KERNEL_BUILD_OUT_PREFIX := $(BUILD_TOP)/
161endif