blob: 5025218ee4f009e6713aa20fb94fc1467c384b88 [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
Michael Bestas3952f6c2016-08-26 01:12:08 +0300272ccache =
273
Michael Bestas3952f6c2016-08-26 01:12:08 +0300274ifeq ($(HOST_OS),darwin)
Rashed Abdel-Tawab3d293152018-08-06 22:01:29 -0700275 MAKE_FLAGS += C_INCLUDE_PATH=$(BUILD_TOP)/external/elfutils/libelf:/usr/local/opt/openssl/include
Rashed Abdel-Tawab7fb92512017-03-16 14:37:07 -0400276 MAKE_FLAGS += LIBRARY_PATH=/usr/local/opt/openssl/lib
Michael Bestas3952f6c2016-08-26 01:12:08 +0300277endif
278
279ifeq ($(TARGET_KERNEL_MODULES),)
Tom Marshallf15c3712017-11-10 01:00:18 +0100280 TARGET_KERNEL_MODULES := INSTALLED_KERNEL_MODULES
Michael Bestas3952f6c2016-08-26 01:12:08 +0300281endif
282
Michael Bestas3952f6c2016-08-26 01:12:08 +0300283KERNEL_ADDITIONAL_CONFIG_OUT := $(KERNEL_OUT)/.additional_config
284
285.PHONY: force_additional_config
286$(KERNEL_ADDITIONAL_CONFIG_OUT): force_additional_config
287 $(hide) cmp -s $(KERNEL_ADDITIONAL_CONFIG_SRC) $@ || cp $(KERNEL_ADDITIONAL_CONFIG_SRC) $@;
288
Tom Marshall6b83d832017-11-09 21:02:33 +0100289$(KERNEL_CONFIG): $(KERNEL_DEFCONFIG_SRC) $(KERNEL_ADDITIONAL_CONFIG_OUT)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300290 @echo "Building Kernel Config"
Tom Marshall6b83d832017-11-09 21:02:33 +0100291 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500292 $(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 +0300293 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
294 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
295 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
Joe Maplesda328952017-11-27 10:09:38 -0500296 $(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 -0700297 # Create defconfig build artifact
Joe Maplesda328952017-11-27 10:09:38 -0500298 $(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 +0300299 $(hide) if [ ! -z "$(KERNEL_ADDITIONAL_CONFIG)" ]; then \
300 echo "Using additional config '$(KERNEL_ADDITIONAL_CONFIG)'"; \
301 $(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 -0500302 $(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 +0300303
Tom Marshall6b83d832017-11-09 21:02:33 +0100304.PHONY: TARGET_KERNEL_BINARIES
305TARGET_KERNEL_BINARIES: $(KERNEL_CONFIG)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300306 @echo "Building Kernel"
Tom Marshall6b83d832017-11-09 21:02:33 +0100307 $(hide) rm -rf $(KERNEL_MODULES_OUT)
308 $(hide) mkdir -p $(KERNEL_MODULES_OUT)
309 $(hide) rm -rf $(KERNEL_DEPMOD_STAGING_DIR)
Joe Maplesda328952017-11-27 10:09:38 -0500310 $(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 +0100311 $(hide) if grep -q '^CONFIG_OF=y' $(KERNEL_CONFIG); then \
312 echo "Building DTBs"; \
Joe Maplesda328952017-11-27 10:09:38 -0500313 $(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 +0100314 fi
315 $(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
316 echo "Building Kernel Modules"; \
Joe Maplesda328952017-11-27 10:09:38 -0500317 $(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 +0100318 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300319
Tom Marshallf15c3712017-11-10 01:00:18 +0100320.PHONY: INSTALLED_KERNEL_MODULES
Aaron Klinga078a222018-01-12 18:19:27 -0600321INSTALLED_KERNEL_MODULES: depmod-host
Tom Marshallf15c3712017-11-10 01:00:18 +0100322 $(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
323 echo "Installing Kernel Modules"; \
Joe Maplesda328952017-11-27 10:09:38 -0500324 $(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 +0100325 mofile=$$(find $(KERNEL_MODULES_OUT) -type f -name modules.order) && \
326 mpath=$$(dirname $$mofile) && \
327 for f in $$(find $$mpath/kernel -type f -name '*.ko'); do \
328 $(KERNEL_TOOLCHAIN_PATH)strip --strip-unneeded $$f; \
329 mv $$f $(KERNEL_MODULES_OUT); \
330 done && \
Ethan Chenbf38db22017-12-03 23:59:48 -0800331 rm -rf $$mpath && \
332 mkdir -p $(KERNEL_DEPMOD_STAGING_DIR)/lib/modules/0.0/$(KERNEL_MODULE_MOUNTPOINT)/lib/modules && \
333 find $(KERNEL_MODULES_OUT) -name *.ko -exec cp {} $(KERNEL_DEPMOD_STAGING_DIR)/lib/modules/0.0/$(KERNEL_MODULE_MOUNTPOINT)/lib/modules \; && \
334 $(DEPMOD) -b $(KERNEL_DEPMOD_STAGING_DIR) 0.0 && \
335 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 +0100336 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300337
338$(TARGET_KERNEL_MODULES): TARGET_KERNEL_BINARIES
339
340$(TARGET_PREBUILT_INT_KERNEL): $(TARGET_KERNEL_MODULES)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300341
Tom Marshall6b83d832017-11-09 21:02:33 +0100342# Install kernel (uapi) headers.
343#
344# The dependency file serves two purposes:
345# - It is a stamp indicating when the headers were last installed.
346# - It contains a rule to regenerate itself when any kernel header
347# files change. This rule is identical to the rule emitted by
348# GCC using the M/MM flags.
349#
350# Note that the location of installed kernel headers changed when the
351# kernel uapi system was introduced in 3.7. Unfortunately, it is not
352# sufficient to test whether the uapi directories exist because some
353# kernels backport patches that contain uapi headers. So we look for
354# the string "version_h" in the kernel makefile which was introduced
355# as a part of the uapi system (commit d183e6f570f3).
356-include $(KERNEL_HEADERS_INSTALL_DEPS)
357$(KERNEL_HEADERS_INSTALL_DEPS):
Michael Bestas3952f6c2016-08-26 01:12:08 +0300358 @echo "Building Kernel Headers"
Tom Marshall6b83d832017-11-09 21:02:33 +0100359 $(hide) mkdir -p $(KERNEL_OUT)
360 $(hide) rm -f $@
361 $(hide) $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) headers_install
362 $(hide) echo "$@: \\" > $@
363 $(hide) ( cd $(KERNEL_SRC); \
364 if grep -q '^version_h' 'Makefile'; then \
365 depdirs="arch/$(KERNEL_ARCH)/include/uapi include/uapi"; \
366 else \
367 depdirs="arch/$(KERNEL_ARCH)/include/asm include"; \
368 fi; \
369 deps="Makefile $$(find $$depdirs -type f -name '*.h')"; \
370 for f in $$deps; do \
371 echo " $(KERNEL_SRC)/$$f \\" >> $@; \
372 done ; \
373 echo "" >> $@ ; \
374 for f in $$deps; do \
375 echo "$(KERNEL_SRC)/$$f:" >> $@; \
376 echo "" >> $@; \
377 done \
378 )
Michael Bestas3952f6c2016-08-26 01:12:08 +0300379
Tom Marshall6b83d832017-11-09 21:02:33 +0100380.PHONY: INSTALLED_KERNEL_HEADERS
381INSTALLED_KERNEL_HEADERS: $(KERNEL_HEADERS_INSTALL_DEPS)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300382
Tom Marshall6b83d832017-11-09 21:02:33 +0100383# Dependencies on $(KERNEL_OUT)/usr are deprecated
384$(KERNEL_HEADERS_INSTALL_DIR): $(KERNEL_HEADERS_INSTALL_DEPS)
385 @echo "Depending on $(KERNEL_HEADERS_INSTALL_DIR) is deprecated." 1>&2
386 @echo "Use INSTALLED_KERNEL_HEADERS instead." 1>&2
387 @exit 1
388
389.PHONY: kerneltags
390kerneltags: $(KERNEL_CONFIG)
391 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500392 $(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 +0300393
Tom Marshall6b83d832017-11-09 21:02:33 +0100394.PHONY: kernelconfig kernelxconfig kernelsavedefconfig alldefconfig
395
Michael Bestas3952f6c2016-08-26 01:12:08 +0300396kernelconfig: KERNELCONFIG_MODE := menuconfig
397kernelxconfig: KERNELCONFIG_MODE := xconfig
Tom Marshall6b83d832017-11-09 21:02:33 +0100398kernelxconfig kernelconfig:
399 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500400 $(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 +0300401 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500402 $(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 +0300403 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500404 $(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 +0300405 cp $(KERNEL_OUT)/defconfig $(KERNEL_DEFCONFIG_SRC)
406
Tom Marshall6b83d832017-11-09 21:02:33 +0100407kernelsavedefconfig:
408 $(hide) mkdir -p $(KERNEL_OUT)
Joe Maplesda328952017-11-27 10:09:38 -0500409 $(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 +0300410 env KCONFIG_NOTIMESTAMP=true \
Joe Maplesda328952017-11-27 10:09:38 -0500411 $(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 +0300412 cp $(KERNEL_OUT)/defconfig $(KERNEL_DEFCONFIG_SRC)
413
Tom Marshall6b83d832017-11-09 21:02:33 +0100414alldefconfig:
415 $(hide) mkdir -p $(KERNEL_OUT)
Michael Bestas3952f6c2016-08-26 01:12:08 +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) alldefconfig
Michael Bestas3952f6c2016-08-26 01:12:08 +0300418
419endif # FULL_KERNEL_BUILD
420
Christian Oder8d5db3c2017-11-25 16:52:17 +0100421TARGET_PREBUILT_DTBO = $(PRODUCT_OUT)/dtbo/arch/$(KERNEL_ARCH)/boot/dtbo.img
422$(TARGET_PREBUILT_DTBO): $(AVBTOOL)
423 echo -e ${CL_GRN}"Building DTBO.img"${CL_RST}
424 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
425 $(MAKE) $(MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbo.img
426 $(AVBTOOL) add_hash_footer \
427 --image $@ \
428 --partition_size $(BOARD_DTBOIMG_PARTITION_SIZE) \
429 --partition_name dtbo $(INTERNAL_AVB_DTBO_SIGNING_ARGS) \
430 $(BOARD_AVB_DTBO_ADD_HASH_FOOTER_ARGS)
431
Michael Bestas3952f6c2016-08-26 01:12:08 +0300432## Install it
433
434ifeq ($(NEEDS_KERNEL_COPY),true)
435file := $(INSTALLED_KERNEL_TARGET)
436ALL_PREBUILT += $(file)
437$(file) : $(KERNEL_BIN) | $(ACP)
438 $(transform-prebuilt-to-target)
439
440ALL_PREBUILT += $(INSTALLED_KERNEL_TARGET)
441endif
442
Christian Oder8d5db3c2017-11-25 16:52:17 +0100443ifeq ($(TARGET_NEEDS_DTBOIMAGE),true)
444file := $(INSTALLED_DTBOIMAGE_TARGET)
445ALL_PREBUILT += $(file)
446$(file) : $(TARGET_PREBUILT_DTBO) | $(ACP)
447 $(transform-prebuilt-to-target)
448
449ALL_PREBUILT += $(INSTALLED_DTBOIMAGE_TARGET)
450endif
451
Michael Bestas3952f6c2016-08-26 01:12:08 +0300452.PHONY: kernel
453kernel: $(INSTALLED_KERNEL_TARGET)
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600454
Christian Oder8d5db3c2017-11-25 16:52:17 +0100455.PHONY: dtbo
456dtbo: $(INSTALLED_DTBOIMAGE_TARGET)
457
Dan Pasanen8ee5d892017-12-08 13:46:30 -0600458endif # TARGET_NO_KERNEL