1#
2# Copyright (c) 2019, Google, Inc. All rights reserved
3#
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# Inputs:
18# LINUX_ARCH contains the architecture to build for (Global)
19# Outputs:
20# LINUX_BUILD_DIR contains the path to the built linux kernel sources
21# LINUX_IMAGE path of the final linux image target
22
23# This Makefile will build the Linux kernel with our configuration.
24
25LINUX_BUILD_DIR := $(abspath $(BUILDDIR)/linux-build)
26ifndef LINUX_ARCH
27	$(error LINUX_ARCH must be specified)
28endif
29
30ifeq ($(LINUX_ARCH),arm)
31LINUX_CLANG_TRIPLE := $(LINUX_ARCH)-linux-gnueabi-
32else
33LINUX_CLANG_TRIPLE := $(LINUX_ARCH)-linux-gnu-
34endif
35
36LINUX_SRC := $(call FIND_EXTERNAL,linux)
37LINUX_CONFIG_DIR = $(LINUX_SRC)/arch/$(LINUX_ARCH)/configs
38
39# Preserve compatibility with architectures without GKI
40ifeq (,$(wildcard $(LINUX_CONFIG_DIR)/gki_defconfig))
41LINUX_DEFCONFIG_FRAGMENTS := \
42	$(LINUX_CONFIG_DIR)/trusty_qemu_defconfig \
43
44else
45LINUX_DEFCONFIG_FRAGMENTS := \
46	$(LINUX_CONFIG_DIR)/gki_defconfig \
47	$(LINUX_CONFIG_DIR)/trusty_qemu_defconfig.fragment \
48
49endif
50
51LINUX_IMAGE := $(LINUX_BUILD_DIR)/arch/$(LINUX_ARCH)/boot/Image
52
53$(LINUX_IMAGE): LINUX_TMP_DEFCONFIG := $(LINUX_CONFIG_DIR)/tmp_defconfig
54$(LINUX_IMAGE): LINUX_SRC := $(LINUX_SRC)
55$(LINUX_IMAGE): LINUX_DEFCONFIG_FRAGMENTS := $(LINUX_DEFCONFIG_FRAGMENTS)
56$(LINUX_IMAGE): LINUX_MAKE_ARGS := -C $(LINUX_SRC)
57$(LINUX_IMAGE): LINUX_MAKE_ARGS += O=$(LINUX_BUILD_DIR)
58$(LINUX_IMAGE): LINUX_MAKE_ARGS += ARCH=$(LINUX_ARCH)
59
60# Preserve compatibility with older linux kernel
61ifeq (,$(wildcard $(LINUX_SRC)/Documentation/kbuild/llvm.rst))
62$(LINUX_IMAGE): CLANG_BINDIR := $(CLANG_BINDIR)
63$(LINUX_IMAGE): LINUX_MAKE_ARGS += CROSS_COMPILE=$(ARCH_$(LINUX_ARCH)_TOOLCHAIN_PREFIX)
64$(LINUX_IMAGE): LINUX_MAKE_ARGS += CC=$(CLANG_BINDIR)/clang
65$(LINUX_IMAGE): LINUX_MAKE_ARGS += LD=$(CLANG_BINDIR)/ld.lld
66$(LINUX_IMAGE): LINUX_MAKE_ARGS += CLANG_TRIPLE=$(LINUX_CLANG_TRIPLE)
67else
68# Newer linux kernel versions need a newer toolchain (optionally specified in
69# LINUX_CLANG_BINDIR) than the older linux kernel needs or supports.
70LINUX_CLANG_BINDIR ?= $(CLANG_BINDIR)
71$(LINUX_IMAGE): CLANG_BINDIR := $(LINUX_CLANG_BINDIR)
72$(LINUX_IMAGE): LINUX_MAKE_ARGS += CROSS_COMPILE=$(LINUX_CLANG_TRIPLE)
73$(LINUX_IMAGE): LINUX_MAKE_ARGS += LLVM=1
74$(LINUX_IMAGE): LINUX_MAKE_ARGS += LLVM_IAS=1
75endif
76
77$(LINUX_IMAGE): LINUX_MAKE_ARGS += LEX=$(BUILDTOOLS_BINDIR)/flex
78$(LINUX_IMAGE): LINUX_MAKE_ARGS += YACC=$(BUILDTOOLS_BINDIR)/bison
79$(LINUX_IMAGE): LINUX_MAKE_ARGS += BISON_PKGDATADIR=$(BUILDTOOLS_COMMON)/bison
80$(LINUX_IMAGE): .PHONY
81	KCONFIG_CONFIG=$(LINUX_TMP_DEFCONFIG) $(LINUX_SRC)/scripts/kconfig/merge_config.sh -m -r $(LINUX_DEFCONFIG_FRAGMENTS)
82	PATH=$(CLANG_BINDIR):$(PATH) $(MAKE) $(LINUX_MAKE_ARGS) $(notdir $(LINUX_TMP_DEFCONFIG))
83	rm $(LINUX_TMP_DEFCONFIG)
84	PATH=$(CLANG_BINDIR):$(PATH) $(MAKE) $(LINUX_MAKE_ARGS)
85
86# Add LINUX_IMAGE to the list of project dependencies
87EXTRA_BUILDDEPS += $(LINUX_IMAGE)
88
89LINUX_DEFCONFIG_FRAGMENTS :=
90LINUX_CONFIG_DIR :=
91LINUX_SRC :=
92LINUX_CLANG_TRIPLE :=
93