blob: c84f70958ba77a4ead7791b9baeb64aee6a72f4c [file] [log] [blame]
Prasad Sodagudi0489ba22017-10-04 14:44:24 -07001#Android makefile to build kernel as a part of Android Build
2PERL = perl
3
4KERNEL_TARGET := $(strip $(INSTALLED_KERNEL_TARGET))
5ifeq ($(KERNEL_TARGET),)
6INSTALLED_KERNEL_TARGET := $(PRODUCT_OUT)/kernel
7endif
8
9TARGET_KERNEL_MAKE_ENV := $(strip $(TARGET_KERNEL_MAKE_ENV))
10ifeq ($(TARGET_KERNEL_MAKE_ENV),)
11KERNEL_MAKE_ENV :=
12else
13KERNEL_MAKE_ENV := $(TARGET_KERNEL_MAKE_ENV)
14endif
15
16TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
17ifeq ($(TARGET_KERNEL_ARCH),)
18KERNEL_ARCH := arm
19else
20KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
21endif
22
23TARGET_KERNEL_HEADER_ARCH := $(strip $(TARGET_KERNEL_HEADER_ARCH))
24ifeq ($(TARGET_KERNEL_HEADER_ARCH),)
25KERNEL_HEADER_ARCH := $(KERNEL_ARCH)
26else
27$(warning Forcing kernel header generation only for '$(TARGET_KERNEL_HEADER_ARCH)')
28KERNEL_HEADER_ARCH := $(TARGET_KERNEL_HEADER_ARCH)
29endif
30
31KERNEL_HEADER_DEFCONFIG := $(strip $(KERNEL_HEADER_DEFCONFIG))
32ifeq ($(KERNEL_HEADER_DEFCONFIG),)
33KERNEL_HEADER_DEFCONFIG := $(KERNEL_DEFCONFIG)
34endif
35
36# Force 32-bit binder IPC for 64bit kernel with 32bit userspace
37ifeq ($(KERNEL_ARCH),arm64)
38ifeq ($(TARGET_ARCH),arm)
39KERNEL_CONFIG_OVERRIDE := CONFIG_ANDROID_BINDER_IPC_32BIT=y
40endif
41endif
42
43TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
44ifeq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
45KERNEL_CROSS_COMPILE := arm-eabi-
46else
47KERNEL_CROSS_COMPILE := $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
48endif
49
50ifeq ($(TARGET_PREBUILT_KERNEL),)
51
52KERNEL_GCC_NOANDROID_CHK := $(shell (echo "int main() {return 0;}" | $(KERNEL_CROSS_COMPILE)gcc -E -mno-android - > /dev/null 2>&1 ; echo $$?))
53
54real_cc :=
55ifeq ($(KERNEL_LLVM_SUPPORT),true)
56real_cc := REAL_CC=$(KERNEL_LLVM_BIN) CLANG_TRIPLE=aarch64-linux-gnu-
57else
58ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),0)
59KERNEL_CFLAGS := KCFLAGS=-mno-android
60endif
61endif
62
63mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
64current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
65TARGET_KERNEL := msm-$(TARGET_KERNEL_VERSION)
66ifeq ($(TARGET_KERNEL),$(current_dir))
67 # New style, kernel/msm-version
68 BUILD_ROOT_LOC := ../../
69 TARGET_KERNEL_SOURCE := kernel/$(TARGET_KERNEL)
70 KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/kernel/$(TARGET_KERNEL)
71 KERNEL_SYMLINK := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
72 KERNEL_USR := $(KERNEL_SYMLINK)/usr
73else
74 # Legacy style, kernel source directly under kernel
75 KERNEL_LEGACY_DIR := true
76 BUILD_ROOT_LOC := ../
77 TARGET_KERNEL_SOURCE := kernel
78 KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
79endif
80
81KERNEL_CONFIG := $(KERNEL_OUT)/.config
82
83ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),)
84$(error Kernel configuration not defined, cannot build kernel)
85else
86
87ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true)
88$(info Using uncompressed kernel)
89TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image
90else
91ifeq ($(KERNEL_ARCH),arm64)
92TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz
93else
94TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage
95endif
96endif
97
98ifeq ($(TARGET_KERNEL_APPEND_DTB), true)
99$(info Using appended DTB)
100TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb
101endif
102
103KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr
104KERNEL_MODULES_INSTALL ?= system
105KERNEL_MODULES_OUT ?= $(PRODUCT_OUT)/$(KERNEL_MODULES_INSTALL)/lib/modules
106
107TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)
108
109define mv-modules
110mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
111if [ "$$mdpath" != "" ];then\
112mpath=`dirname $$mdpath`;\
113ko=`find $$mpath/kernel -type f -name *.ko`;\
114for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\
115fi
116endef
117
118define clean-module-folder
119mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
120if [ "$$mdpath" != "" ];then\
121mpath=`dirname $$mdpath`; rm -rf $$mpath;\
122fi
123endef
124
125ifneq ($(KERNEL_LEGACY_DIR),true)
126$(KERNEL_USR): $(KERNEL_HEADERS_INSTALL)
127 rm -rf $(KERNEL_SYMLINK)
128 ln -s kernel/$(TARGET_KERNEL) $(KERNEL_SYMLINK)
129
130$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_USR)
131endif
132
133$(KERNEL_OUT):
134 mkdir -p $(KERNEL_OUT)
135
136$(KERNEL_CONFIG): $(KERNEL_OUT)
137 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) $(KERNEL_DEFCONFIG)
138 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
139 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
140 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
141 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) oldconfig; fi
142
143$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_OUT) $(KERNEL_HEADERS_INSTALL)
144 $(hide) echo "Building kernel..."
145 $(hide) rm -rf $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/dts
146 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) $(KERNEL_CFLAGS)
147 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) $(KERNEL_CFLAGS) modules
148 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) INSTALL_MOD_PATH=$(BUILD_ROOT_LOC)../$(KERNEL_MODULES_INSTALL) INSTALL_MOD_STRIP=1 $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) modules_install
149 $(mv-modules)
150 $(clean-module-folder)
151
152$(KERNEL_HEADERS_INSTALL): $(KERNEL_OUT)
153 $(hide) if [ ! -z "$(KERNEL_HEADER_DEFCONFIG)" ]; then \
154 rm -f $(BUILD_ROOT_LOC)$(KERNEL_CONFIG); \
155 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) $(KERNEL_HEADER_DEFCONFIG); \
156 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) headers_install;\
157 if [ -d "$(KERNEL_HEADERS_INSTALL)/include/bringup_headers" ]; then \
158 cp -Rf $(KERNEL_HEADERS_INSTALL)/include/bringup_headers/* $(KERNEL_HEADERS_INSTALL)/include/ ;\
159 fi ;\
160 fi
161 $(hide) if [ "$(KERNEL_HEADER_DEFCONFIG)" != "$(KERNEL_DEFCONFIG)" ]; then \
162 echo "Used a different defconfig for header generation"; \
163 rm -f $(BUILD_ROOT_LOC)$(KERNEL_CONFIG); \
164 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) $(KERNEL_DEFCONFIG); fi
165 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
166 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
167 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
168 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) oldconfig; fi
169
170kerneltags: $(KERNEL_OUT) $(KERNEL_CONFIG)
171 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) tags
172
173kernelconfig: $(KERNEL_OUT) $(KERNEL_CONFIG)
174 env KCONFIG_NOTIMESTAMP=true \
175 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) menuconfig
176 env KCONFIG_NOTIMESTAMP=true \
177 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(real_cc) savedefconfig
178 cp $(KERNEL_OUT)/defconfig $(TARGET_KERNEL_SOURCE)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_DEFCONFIG)
179
180endif
181endif