blob: ec26deda407206c2c90e0b0a69a4969adbfc0d1d [file] [log] [blame]
Michael Bestas3952f6c2016-08-26 01:12:08 +03001# Copyright (C) 2012 The CyanogenMod Project
Dan Pasanen03447712016-12-19 11:22:55 -06002# (C) 2017 The LineageOS Project
Michael Bestas3952f6c2016-08-26 01:12:08 +03003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17# Android makefile to build kernel as a part of Android Build
18#
19# Configuration
20# =============
21#
22# These config vars are usually set in BoardConfig.mk:
23#
24# TARGET_KERNEL_SOURCE = Kernel source dir, optional, defaults
25# to kernel/$(TARGET_DEVICE_DIR)
26# TARGET_KERNEL_CONFIG = Kernel defconfig
27# TARGET_KERNEL_VARIANT_CONFIG = Variant defconfig, optional
28# TARGET_KERNEL_SELINUX_CONFIG = SELinux defconfig, optional
29# TARGET_KERNEL_ADDITIONAL_CONFIG = Additional defconfig, optional
30# TARGET_KERNEL_ARCH = Kernel Arch
Michael Bestas3952f6c2016-08-26 01:12:08 +030031# TARGET_KERNEL_CROSS_COMPILE_PREFIX = Compiler prefix (e.g. arm-eabi-)
Michael Bestas1d55ece2016-09-28 20:16:25 +030032# defaults to arm-linux-androidkernel- for arm
33# aarch64-linux-androidkernel- for arm64
34# x86_64-linux-androidkernel- for x86
Michael Bestas3952f6c2016-08-26 01:12:08 +030035#
Joe Maplesda328952017-11-27 10:09:38 -050036# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to false
37#
38# TARGET_KERNEL_CLANG_VERSION = Clang prebuilts version, optional, defaults to clang-stable
39#
40# TARGET_KERNEL_CLANG_PATH = Clang prebuilts path, optional
41#
Dan Pasanenedc1ceb2017-01-24 15:46:13 -060042# BOARD_KERNEL_IMAGE_NAME = Built image name
43# for ARM use: zImage
44# for ARM64 use: Image.gz
45# for uncompressed use: Image
46# If using an appended DT, append '-dtb'
47# to the end of the image name.
48# For example, for ARM devices,
49# use zImage-dtb instead of zImage.
Michael Bestas3952f6c2016-08-26 01:12:08 +030050#
51# KERNEL_TOOLCHAIN_PREFIX = Overrides TARGET_KERNEL_CROSS_COMPILE_PREFIX,
52# Set this var in shell to override
53# toolchain specified in BoardConfig.mk
54# KERNEL_TOOLCHAIN = Path to toolchain, if unset, assumes
55# TARGET_KERNEL_CROSS_COMPILE_PREFIX
56# is in PATH
Joe Maplesda328952017-11-27 10:09:38 -050057#
58# KERNEL_CC = The C Compiler used. This is automatically set based
59# on whether the clang version is set, optional.
60#
61# KERNEL_CLANG_TRIPLE = Target triple for clang (e.g. aarch64-linux-gnu-)
62# defaults to arm-linux-gnu- for arm
63# aarch64-linux-gnu- for arm64
64# x86_64-linux-gnu- for x86
65#
Michael Bestas3952f6c2016-08-26 01:12:08 +030066# USE_CCACHE = Enable ccache (global Android flag)
67#
68# NEED_KERNEL_MODULE_ROOT = Optional, if true, install kernel
Luca Stefania1ade7f2018-07-11 00:35:33 +020069# modules in root instead of vendor
70# NEED_KERNEL_MODULE_SYSTEM = Optional, if true, install kernel
71# modules in system instead of vendor
Michael Bestas3952f6c2016-08-26 01:12:08 +030072
Dan Pasanen8ee5d892017-12-08 13:46:30 -060073ifneq ($(TARGET_NO_KERNEL),true)
Michael Bestas3952f6c2016-08-26 01:12:08 +030074
75TARGET_AUTO_KDIR := $(shell echo $(TARGET_DEVICE_DIR) | sed -e 's/^device/kernel/g')
76
77## Externally influenced variables
78# kernel location - optional, defaults to kernel/<vendor>/<device>
79TARGET_KERNEL_SOURCE ?= $(TARGET_AUTO_KDIR)
80KERNEL_SRC := $(TARGET_KERNEL_SOURCE)
81# kernel configuration - mandatory
82KERNEL_DEFCONFIG := $(TARGET_KERNEL_CONFIG)
83VARIANT_DEFCONFIG := $(TARGET_KERNEL_VARIANT_CONFIG)
84SELINUX_DEFCONFIG := $(TARGET_KERNEL_SELINUX_CONFIG)
85
86## Internal variables
87KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
88KERNEL_CONFIG := $(KERNEL_OUT)/.config
Michael Bestas3952f6c2016-08-26 01:12:08 +030089
90TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
91ifeq ($(TARGET_KERNEL_ARCH),)
92KERNEL_ARCH := $(TARGET_ARCH)
93else
94KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
95endif
96
97ifeq ($(KERNEL_ARCH),x86_64)
98KERNEL_DEFCONFIG_ARCH := x86
99else
100KERNEL_DEFCONFIG_ARCH := $(KERNEL_ARCH)
101endif
102KERNEL_DEFCONFIG_SRC := $(KERNEL_SRC)/arch/$(KERNEL_DEFCONFIG_ARCH)/configs/$(KERNEL_DEFCONFIG)
103
Dan Pasanenedc1ceb2017-01-24 15:46:13 -0600104ifeq ($(BOARD_KERNEL_IMAGE_NAME),)
105$(error BOARD_KERNEL_IMAGE_NAME not defined.)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300106endif
Dan Pasanenedc1ceb2017-01-24 15:46:13 -0600107ifneq ($(TARGET_USES_UNCOMPRESSED_KERNEL),)
108$(error TARGET_USES_UNCOMPRESSED_KERNEL is deprecated.)
109endif
110ifneq ($(TARGET_KERNEL_APPEND_DTB),)
111$(error TARGET_KERNEL_APPEND_DTB is deprecated.)
112endif
113TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/$(BOARD_KERNEL_IMAGE_NAME)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300114
115# Clear this first to prevent accidental poisoning from env
116MAKE_FLAGS :=
117
Ziyan54648c22017-01-12 18:47:54 +0100118ifeq ($(KERNEL_ARCH),arm)
119 # Avoid "Unknown symbol _GLOBAL_OFFSET_TABLE_" errors
120 MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
121endif
122
Michael Bestas3952f6c2016-08-26 01:12:08 +0300123ifeq ($(KERNEL_ARCH),arm64)
124 # Avoid "unsupported RELA relocation: 311" errors (R_AARCH64_ADR_GOT_PAGE)
125 MAKE_FLAGS += CFLAGS_MODULE="-fno-pic"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300126endif
127
128ifneq ($(TARGET_KERNEL_ADDITIONAL_CONFIG),)
129KERNEL_ADDITIONAL_CONFIG := $(TARGET_KERNEL_ADDITIONAL_CONFIG)
130KERNEL_ADDITIONAL_CONFIG_SRC := $(KERNEL_SRC)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_ADDITIONAL_CONFIG)
131 ifeq ("$(wildcard $(KERNEL_ADDITIONAL_CONFIG_SRC))","")
132 $(warning TARGET_KERNEL_ADDITIONAL_CONFIG '$(TARGET_KERNEL_ADDITIONAL_CONFIG)' doesn't exist)
133 KERNEL_ADDITIONAL_CONFIG_SRC := /dev/null
134 endif
135else
136 KERNEL_ADDITIONAL_CONFIG_SRC := /dev/null
137endif
138
Michael Bestas3952f6c2016-08-26 01:12:08 +0300139ifeq "$(wildcard $(KERNEL_SRC) )" ""
140 ifneq ($(TARGET_PREBUILT_KERNEL),)
141 HAS_PREBUILT_KERNEL := true
142 NEEDS_KERNEL_COPY := true
143 else
144 $(foreach cf,$(PRODUCT_COPY_FILES), \
145 $(eval _src := $(call word-colon,1,$(cf))) \
146 $(eval _dest := $(call word-colon,2,$(cf))) \
147 $(ifeq kernel,$(_dest), \
148 $(eval HAS_PREBUILT_KERNEL := true)))
149 endif
150
151 ifneq ($(HAS_PREBUILT_KERNEL),)
152 $(warning ***************************************************************)
153 $(warning * Using prebuilt kernel binary instead of source *)
154 $(warning * THIS IS DEPRECATED, AND WILL BE DISCONTINUED *)
155 $(warning * Please configure your device to download the kernel *)
156 $(warning * source repository to $(KERNEL_SRC))
Michael Bestas3952f6c2016-08-26 01:12:08 +0300157 $(warning * for more information *)
158 $(warning ***************************************************************)
159 FULL_KERNEL_BUILD := false
160 KERNEL_BIN := $(TARGET_PREBUILT_KERNEL)
161 else
162 $(warning ***************************************************************)
163 $(warning * *)
164 $(warning * No kernel source found, and no fallback prebuilt defined. *)
165 $(warning * Please make sure your device is properly configured to *)
166 $(warning * download the kernel repository to $(KERNEL_SRC))
167 $(warning * and add the TARGET_KERNEL_CONFIG variable to BoardConfig.mk *)
168 $(warning * *)
169 $(warning * As an alternative, define the TARGET_PREBUILT_KERNEL *)
170 $(warning * variable with the path to the prebuilt binary kernel image *)
171 $(warning * in your BoardConfig.mk file *)
172 $(warning * *)
173 $(warning ***************************************************************)
174 $(error "NO KERNEL")
175 endif
176else
177 NEEDS_KERNEL_COPY := true
178 ifeq ($(TARGET_KERNEL_CONFIG),)
179 $(warning **********************************************************)
180 $(warning * Kernel source found, but no configuration was defined *)
181 $(warning * Please add the TARGET_KERNEL_CONFIG variable to your *)
182 $(warning * BoardConfig.mk file *)
183 $(warning **********************************************************)
184 # $(error "NO KERNEL CONFIG")
185 else
186 #$(info Kernel source found, building it)
187 FULL_KERNEL_BUILD := true
188 KERNEL_BIN := $(TARGET_PREBUILT_INT_KERNEL)
189 endif
190endif
191
192ifeq ($(FULL_KERNEL_BUILD),true)
193
Tom Marshall6b83d832017-11-09 21:02:33 +0100194KERNEL_HEADERS_INSTALL_DIR := $(KERNEL_OUT)/usr
195KERNEL_HEADERS_INSTALL_DEPS := $(KERNEL_OUT)/.headers_install_deps
Michael Bestas3952f6c2016-08-26 01:12:08 +0300196
197ifeq ($(NEED_KERNEL_MODULE_ROOT),true)
198KERNEL_MODULES_INSTALL := root
199KERNEL_MODULES_OUT := $(TARGET_ROOT_OUT)/lib/modules
Ethan Chenbf38db22017-12-03 23:59:48 -0800200KERNEL_DEPMOD_STAGING_DIR := $(call intermediates-dir-for,PACKAGING,depmod_recovery)
201KERNEL_MODULE_MOUNTPOINT :=
Luca Stefania1ade7f2018-07-11 00:35:33 +0200202else ifeq ($(NEED_KERNEL_MODULE_SYSTEM),true)
203KERNEL_MODULES_INSTALL := $(TARGET_COPY_OUT_SYSTEM)
204KERNEL_MODULES_OUT := $(TARGET_OUT)/lib/modules
205KERNEL_DEPMOD_STAGING_DIR := $(call intermediates-dir-for,PACKAGING,depmod_system)
206KERNEL_MODULE_MOUNTPOINT := system
Michael Bestas3952f6c2016-08-26 01:12:08 +0300207else
Ethan Chenbf38db22017-12-03 23:59:48 -0800208KERNEL_MODULES_INSTALL := $(TARGET_COPY_OUT_VENDOR)
209KERNEL_MODULES_OUT := $(TARGET_OUT_VENDOR)/lib/modules
210KERNEL_DEPMOD_STAGING_DIR := $(call intermediates-dir-for,PACKAGING,depmod_vendor)
211KERNEL_MODULE_MOUNTPOINT := vendor
Michael Bestas3952f6c2016-08-26 01:12:08 +0300212endif
213
214TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
215ifneq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
216KERNEL_TOOLCHAIN_PREFIX ?= $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
Zhao Wei Liew9689bb92016-11-10 20:02:58 +0800217else ifeq ($(KERNEL_ARCH),arm64)
Michael Bestas1d55ece2016-09-28 20:16:25 +0300218KERNEL_TOOLCHAIN_PREFIX ?= aarch64-linux-androidkernel-
Zhao Wei Liew9689bb92016-11-10 20:02:58 +0800219else ifeq ($(KERNEL_ARCH),arm)
Michael Bestas1d55ece2016-09-28 20:16:25 +0300220KERNEL_TOOLCHAIN_PREFIX ?= arm-linux-androidkernel-
Zhao Wei Liew9689bb92016-11-10 20:02:58 +0800221else ifeq ($(KERNEL_ARCH),x86)
Michael Bestas1d55ece2016-09-28 20:16:25 +0300222KERNEL_TOOLCHAIN_PREFIX ?= x86_64-linux-androidkernel-
Michael Bestas3952f6c2016-08-26 01:12:08 +0300223endif
224
225ifeq ($(KERNEL_TOOLCHAIN),)
226KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN_PREFIX)
227else
228ifneq ($(KERNEL_TOOLCHAIN_PREFIX),)
229KERNEL_TOOLCHAIN_PATH := $(KERNEL_TOOLCHAIN)/$(KERNEL_TOOLCHAIN_PREFIX)
230endif
231endif
232
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700233BUILD_TOP := $(shell pwd)
234
Joe Maplesda328952017-11-27 10:09:38 -0500235ifeq ($(TARGET_KERNEL_CLANG_COMPILE),true)
236 ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
237 # Find the clang-* directory containing the specified version
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700238 KERNEL_CLANG_VERSION := $(shell find $(BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/ -name AndroidVersion.txt -exec grep -l $(TARGET_KERNEL_CLANG_VERSION) "{}" \; | sed -e 's|/AndroidVersion.txt$$||g;s|^.*/||g')
Joe Maplesda328952017-11-27 10:09:38 -0500239 else
Nathan Chancellorca403312018-04-11 16:23:19 -0700240 # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
241 KERNEL_CLANG_VERSION := $(LLVM_PREBUILTS_VERSION)
Joe Maplesda328952017-11-27 10:09:38 -0500242 endif
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700243 TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/$(KERNEL_CLANG_VERSION)/bin
Joe Maplesda328952017-11-27 10:09:38 -0500244 ifeq ($(KERNEL_ARCH),arm64)
245 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=aarch64-linux-gnu-
246 else ifeq ($(KERNEL_ARCH),arm)
247 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=arm-linux-gnu-
248 else ifeq ($(KERNEL_ARCH),x86)
249 KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=x86_64-linux-gnu-
250 endif
251endif
252
Michael Bestas3952f6c2016-08-26 01:12:08 +0300253ifneq ($(USE_CCACHE),)
Steve Kondik6a378282016-09-25 22:37:01 -0700254 # Detect if the system already has ccache installed to use instead of the prebuilt
255 ccache := $(shell which ccache)
256
257 ifeq ($(ccache),)
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700258 ccache := $(BUILD_TOP)/prebuilts/misc/$(HOST_PREBUILT_TAG)/ccache/ccache
Steve Kondik6a378282016-09-25 22:37:01 -0700259 # Check that the executable is here.
260 ccache := $(strip $(wildcard $(ccache)))
261 endif
Michael Bestas3952f6c2016-08-26 01:12:08 +0300262endif
263
Joe Maplesda328952017-11-27 10:09:38 -0500264ifeq ($(TARGET_KERNEL_CLANG_COMPILE),true)
265 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(KERNEL_TOOLCHAIN_PATH)"
Dan Pasanenf65a6df2018-02-18 22:13:05 -0600266 ifeq ($(KERNEL_CC),)
267 KERNEL_CC := CC="$(ccache) $(TARGET_KERNEL_CLANG_PATH)/clang"
268 endif
Joe Maplesda328952017-11-27 10:09:38 -0500269else
270 KERNEL_CROSS_COMPILE := CROSS_COMPILE="$(ccache) $(KERNEL_TOOLCHAIN_PATH)"
271endif
Rashed Abdel-Tawab8960e3d2018-08-07 17:44:14 -0700272
273# Needed for CONFIG_COMPAT_VDSO, safe to set for all arm64 builds
274ifeq ($(KERNEL_ARCH),arm64)
275 KERNEL_CROSS_COMPILE += CROSS_COMPILE_ARM32="arm-linux-androideabi-"
276endif
277
Michael Bestas3952f6c2016-08-26 01:12:08 +0300278ccache =
279
Michael Bestas3952f6c2016-08-26 01:12:08 +0300280ifeq ($(HOST_OS),darwin)
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700281 MAKE_FLAGS += C_INCLUDE_PATH=$(BUILD_TOP)/external/elfutils/libelf:/usr/local/opt/openssl/include
Rashed Abdel-Tawab7fb92512017-03-16 14:37:07 -0400282 MAKE_FLAGS += LIBRARY_PATH=/usr/local/opt/openssl/lib
Michael Bestas3952f6c2016-08-26 01:12:08 +0300283endif
284
285ifeq ($(TARGET_KERNEL_MODULES),)
Tom Marshallf15c3712017-11-10 01:00:18 +0100286 TARGET_KERNEL_MODULES := INSTALLED_KERNEL_MODULES
Michael Bestas3952f6c2016-08-26 01:12:08 +0300287endif
288
Michael Bestas3952f6c2016-08-26 01:12:08 +0300289KERNEL_ADDITIONAL_CONFIG_OUT := $(KERNEL_OUT)/.additional_config
290
291.PHONY: force_additional_config
292$(KERNEL_ADDITIONAL_CONFIG_OUT): force_additional_config
293 $(hide) cmp -s $(KERNEL_ADDITIONAL_CONFIG_SRC) $@ || cp $(KERNEL_ADDITIONAL_CONFIG_SRC) $@;
294
Tom Marshall6b83d832017-11-09 21:02:33 +0100295$(KERNEL_CONFIG): $(KERNEL_DEFCONFIG_SRC) $(KERNEL_ADDITIONAL_CONFIG_OUT)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300296 @echo "Building Kernel Config"
Tom Marshall6b83d832017-11-09 21:02:33 +0100297 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500298 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) VARIANT_DEFCONFIG=$(VARIANT_DEFCONFIG) SELINUX_DEFCONFIG=$(SELINUX_DEFCONFIG) $(KERNEL_DEFCONFIG)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300299 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
300 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
301 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
Joe Maplesda328952017-11-27 10:09:38 -0500302 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) oldconfig; fi
Sam Mortimerb0cecb72016-10-01 09:56:52 -0700303 # Create defconfig build artifact
Joe Maplesda328952017-11-27 10:09:38 -0500304 $(hide) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
Michael Bestas3952f6c2016-08-26 01:12:08 +0300305 $(hide) if [ ! -z "$(KERNEL_ADDITIONAL_CONFIG)" ]; then \
306 echo "Using additional config '$(KERNEL_ADDITIONAL_CONFIG)'"; \
307 $(KERNEL_SRC)/scripts/kconfig/merge_config.sh -m -O $(KERNEL_OUT) $(KERNEL_OUT)/.config $(KERNEL_SRC)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_ADDITIONAL_CONFIG); \
Joe Maplesda328952017-11-27 10:09:38 -0500308 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) KCONFIG_ALLCONFIG=$(KERNEL_OUT)/.config alldefconfig; fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300309
Tom Marshall6b83d832017-11-09 21:02:33 +0100310.PHONY: TARGET_KERNEL_BINARIES
311TARGET_KERNEL_BINARIES: $(KERNEL_CONFIG)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300312 @echo "Building Kernel"
Tom Marshall6b83d832017-11-09 21:02:33 +0100313 $(hide) rm -rf $(KERNEL_MODULES_OUT)
314 $(hide) mkdir -p $(KERNEL_MODULES_OUT)
315 $(hide) rm -rf $(KERNEL_DEPMOD_STAGING_DIR)
Joe Maplesda328952017-11-27 10:09:38 -0500316 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(BOARD_KERNEL_IMAGE_NAME)
Tom Marshallf15c3712017-11-10 01:00:18 +0100317 $(hide) if grep -q '^CONFIG_OF=y' $(KERNEL_CONFIG); then \
318 echo "Building DTBs"; \
Joe Maplesda328952017-11-27 10:09:38 -0500319 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbs; \
Tom Marshallf15c3712017-11-10 01:00:18 +0100320 fi
321 $(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
322 echo "Building Kernel Modules"; \
Joe Maplesda328952017-11-27 10:09:38 -0500323 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) modules; \
Tom Marshallf15c3712017-11-10 01:00:18 +0100324 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300325
Tom Marshallf15c3712017-11-10 01:00:18 +0100326.PHONY: INSTALLED_KERNEL_MODULES
Aaron Klinga078a222018-01-12 18:19:27 -0600327INSTALLED_KERNEL_MODULES: depmod-host
Tom Marshallf15c3712017-11-10 01:00:18 +0100328 $(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
329 echo "Installing Kernel Modules"; \
Joe Maplesda328952017-11-27 10:09:38 -0500330 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) INSTALL_MOD_PATH=../../$(KERNEL_MODULES_INSTALL) modules_install && \
Tom Marshallf15c3712017-11-10 01:00:18 +0100331 mofile=$$(find $(KERNEL_MODULES_OUT) -type f -name modules.order) && \
332 mpath=$$(dirname $$mofile) && \
333 for f in $$(find $$mpath/kernel -type f -name '*.ko'); do \
334 $(KERNEL_TOOLCHAIN_PATH)strip --strip-unneeded $$f; \
335 mv $$f $(KERNEL_MODULES_OUT); \
336 done && \
Ethan Chenbf38db22017-12-03 23:59:48 -0800337 rm -rf $$mpath && \
338 mkdir -p $(KERNEL_DEPMOD_STAGING_DIR)/lib/modules/0.0/$(KERNEL_MODULE_MOUNTPOINT)/lib/modules && \
339 find $(KERNEL_MODULES_OUT) -name *.ko -exec cp {} $(KERNEL_DEPMOD_STAGING_DIR)/lib/modules/0.0/$(KERNEL_MODULE_MOUNTPOINT)/lib/modules \; && \
340 $(DEPMOD) -b $(KERNEL_DEPMOD_STAGING_DIR) 0.0 && \
341 sed -e 's/\(.*modules.*\):/\/\1:/g' -e 's/ \([^ ]*modules[^ ]*\)/ \/\1/g' $(KERNEL_DEPMOD_STAGING_DIR)/lib/modules/0.0/modules.dep > $(KERNEL_MODULES_OUT)/modules.dep; \
Tom Marshallf15c3712017-11-10 01:00:18 +0100342 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300343
344$(TARGET_KERNEL_MODULES): TARGET_KERNEL_BINARIES
345
346$(TARGET_PREBUILT_INT_KERNEL): $(TARGET_KERNEL_MODULES)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300347
Tom Marshall6b83d832017-11-09 21:02:33 +0100348# Install kernel (uapi) headers.
349#
350# The dependency file serves two purposes:
351# - It is a stamp indicating when the headers were last installed.
352# - It contains a rule to regenerate itself when any kernel header
353# files change. This rule is identical to the rule emitted by
354# GCC using the M/MM flags.
355#
356# Note that the location of installed kernel headers changed when the
357# kernel uapi system was introduced in 3.7. Unfortunately, it is not
358# sufficient to test whether the uapi directories exist because some
359# kernels backport patches that contain uapi headers. So we look for
360# the string "version_h" in the kernel makefile which was introduced
361# as a part of the uapi system (commit d183e6f570f3).
362-include $(KERNEL_HEADERS_INSTALL_DEPS)
363$(KERNEL_HEADERS_INSTALL_DEPS):
Michael Bestas3952f6c2016-08-26 01:12:08 +0300364 @echo "Building Kernel Headers"
Tom Marshall6b83d832017-11-09 21:02:33 +0100365 $(hide) mkdir -p $(KERNEL_OUT)
366 $(hide) rm -f $@
367 $(hide) $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) headers_install
368 $(hide) echo "$@: \\" > $@
369 $(hide) ( cd $(KERNEL_SRC); \
370 if grep -q '^version_h' 'Makefile'; then \
371 depdirs="arch/$(KERNEL_ARCH)/include/uapi include/uapi"; \
372 else \
373 depdirs="arch/$(KERNEL_ARCH)/include/asm include"; \
374 fi; \
375 deps="Makefile $$(find $$depdirs -type f -name '*.h')"; \
376 for f in $$deps; do \
377 echo " $(KERNEL_SRC)/$$f \\" >> $@; \
378 done ; \
379 echo "" >> $@ ; \
380 for f in $$deps; do \
381 echo "$(KERNEL_SRC)/$$f:" >> $@; \
382 echo "" >> $@; \
383 done \
384 )
Michael Bestas3952f6c2016-08-26 01:12:08 +0300385
Tom Marshall6b83d832017-11-09 21:02:33 +0100386.PHONY: INSTALLED_KERNEL_HEADERS
387INSTALLED_KERNEL_HEADERS: $(KERNEL_HEADERS_INSTALL_DEPS)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300388
Tom Marshall6b83d832017-11-09 21:02:33 +0100389# Dependencies on $(KERNEL_OUT)/usr are deprecated
390$(KERNEL_HEADERS_INSTALL_DIR): $(KERNEL_HEADERS_INSTALL_DEPS)
391 @echo "Depending on $(KERNEL_HEADERS_INSTALL_DIR) is deprecated." 1>&2
392 @echo "Use INSTALLED_KERNEL_HEADERS instead." 1>&2
393 @exit 1
394
395.PHONY: kerneltags
396kerneltags: $(KERNEL_CONFIG)
397 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500398 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) tags
Michael Bestas3952f6c2016-08-26 01:12:08 +0300399
Tom Marshall6b83d832017-11-09 21:02:33 +0100400.PHONY: kernelconfig kernelxconfig kernelsavedefconfig alldefconfig
401
Michael Bestas3952f6c2016-08-26 01:12:08 +0300402kernelconfig: KERNELCONFIG_MODE := menuconfig
403kernelxconfig: KERNELCONFIG_MODE := xconfig
Tom Marshall6b83d832017-11-09 21:02:33 +0100404kernelxconfig kernelconfig:
405 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500406 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300407 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500408 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNELCONFIG_MODE)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300409 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500410 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
Michael Bestas3952f6c2016-08-26 01:12:08 +0300411 cp $(KERNEL_OUT)/defconfig $(KERNEL_DEFCONFIG_SRC)
412
Tom Marshall6b83d832017-11-09 21:02:33 +0100413kernelsavedefconfig:
414 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500415 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
Michael Bestas296008c2017-07-12 20:56:35 +0300416 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500417 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
Michael Bestas296008c2017-07-12 20:56:35 +0300418 cp $(KERNEL_OUT)/defconfig $(KERNEL_DEFCONFIG_SRC)
419
Tom Marshall6b83d832017-11-09 21:02:33 +0100420alldefconfig:
421 $(hide) mkdir -p $(KERNEL_OUT)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300422 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500423 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) alldefconfig
Michael Bestas3952f6c2016-08-26 01:12:08 +0300424
425endif # FULL_KERNEL_BUILD
426
Christian Oder8d5db3c2017-11-25 16:52:17 +0100427TARGET_PREBUILT_DTBO = $(PRODUCT_OUT)/dtbo/arch/$(KERNEL_ARCH)/boot/dtbo.img
428$(TARGET_PREBUILT_DTBO): $(AVBTOOL)
429 echo -e ${CL_GRN}"Building DTBO.img"${CL_RST}
430 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
431 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbo.img
432 $(AVBTOOL) add_hash_footer \
433 --image $@ \
434 --partition_size $(BOARD_DTBOIMG_PARTITION_SIZE) \
435 --partition_name dtbo $(INTERNAL_AVB_DTBO_SIGNING_ARGS) \
436 $(BOARD_AVB_DTBO_ADD_HASH_FOOTER_ARGS)
437
Michael Bestas3952f6c2016-08-26 01:12:08 +0300438## Install it
439
440ifeq ($(NEEDS_KERNEL_COPY),true)
441file := $(INSTALLED_KERNEL_TARGET)
442ALL_PREBUILT += $(file)
443$(file) : $(KERNEL_BIN) | $(ACP)
444 $(transform-prebuilt-to-target)
445
446ALL_PREBUILT += $(INSTALLED_KERNEL_TARGET)
447endif
448
Christian Oder8d5db3c2017-11-25 16:52:17 +0100449ifeq ($(TARGET_NEEDS_DTBOIMAGE),true)
450file := $(INSTALLED_DTBOIMAGE_TARGET)
451ALL_PREBUILT += $(file)
452$(file) : $(TARGET_PREBUILT_DTBO) | $(ACP)
453 $(transform-prebuilt-to-target)
454
455ALL_PREBUILT += $(INSTALLED_DTBOIMAGE_TARGET)
456endif
457
Michael Bestas3952f6c2016-08-26 01:12:08 +0300458.PHONY: kernel
459kernel: $(INSTALLED_KERNEL_TARGET)
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600460
Christian Oder8d5db3c2017-11-25 16:52:17 +0100461.PHONY: dtbo
462dtbo: $(INSTALLED_DTBOIMAGE_TARGET)
463
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600464endif # TARGET_NO_KERNEL