blob: 2a8d506597846389e38795c6ee1d2982d4bac0e0 [file] [log] [blame]
Ameya Thakur300fae92016-08-10 11:33:55 -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
Puja Guptae5df4f32017-02-24 14:21:40 -08009TARGET_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
Ameya Thakur300fae92016-08-10 11:33:55 -070016TARGET_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 $$?))
53ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),0)
54KERNEL_CFLAGS := KCFLAGS=-mno-android
55endif
56
57mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
58current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
59TARGET_KERNEL := msm-$(TARGET_KERNEL_VERSION)
60ifeq ($(TARGET_KERNEL),$(current_dir))
61 # New style, kernel/msm-version
62 BUILD_ROOT_LOC := ../../
63 TARGET_KERNEL_SOURCE := kernel/$(TARGET_KERNEL)
64 KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/kernel/$(TARGET_KERNEL)
65 KERNEL_SYMLINK := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
66 KERNEL_USR := $(KERNEL_SYMLINK)/usr
67else
68 # Legacy style, kernel source directly under kernel
69 KERNEL_LEGACY_DIR := true
70 BUILD_ROOT_LOC := ../
71 TARGET_KERNEL_SOURCE := kernel
72 KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
73endif
74
75KERNEL_CONFIG := $(KERNEL_OUT)/.config
76
77ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),)
78$(error Kernel configuration not defined, cannot build kernel)
79else
80
81ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true)
82$(info Using uncompressed kernel)
83TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image
84else
85ifeq ($(KERNEL_ARCH),arm64)
86TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz
87else
88TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage
89endif
90endif
91
92ifeq ($(TARGET_KERNEL_APPEND_DTB), true)
93$(info Using appended DTB)
94TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb
95endif
96
97KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr
Puja Gupta45445402017-03-24 12:56:52 -070098KERNEL_MODULES_INSTALL ?= system
99KERNEL_MODULES_OUT ?= $(PRODUCT_OUT)/$(KERNEL_MODULES_INSTALL)/lib/modules
Ameya Thakur300fae92016-08-10 11:33:55 -0700100
101TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)
102
103define mv-modules
104mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
105if [ "$$mdpath" != "" ];then\
106mpath=`dirname $$mdpath`;\
107ko=`find $$mpath/kernel -type f -name *.ko`;\
108for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\
109fi
110endef
111
112define clean-module-folder
113mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
114if [ "$$mdpath" != "" ];then\
115mpath=`dirname $$mdpath`; rm -rf $$mpath;\
116fi
117endef
118
119ifneq ($(KERNEL_LEGACY_DIR),true)
120$(KERNEL_USR): $(KERNEL_HEADERS_INSTALL)
121 rm -rf $(KERNEL_SYMLINK)
122 ln -s kernel/$(TARGET_KERNEL) $(KERNEL_SYMLINK)
123
124$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_USR)
125endif
126
127$(KERNEL_OUT):
128 mkdir -p $(KERNEL_OUT)
129
130$(KERNEL_CONFIG): $(KERNEL_OUT)
Puja Guptae5df4f32017-02-24 14:21:40 -0800131 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_DEFCONFIG)
Ameya Thakur300fae92016-08-10 11:33:55 -0700132 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
133 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
134 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
Puja Guptae5df4f32017-02-24 14:21:40 -0800135 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) oldconfig; fi
Ameya Thakur300fae92016-08-10 11:33:55 -0700136
137$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_OUT) $(KERNEL_HEADERS_INSTALL)
138 $(hide) echo "Building kernel..."
139 $(hide) rm -rf $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/dts
Puja Guptae5df4f32017-02-24 14:21:40 -0800140 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_CFLAGS)
141 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_CFLAGS) modules
142 $(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) modules_install
Ameya Thakur300fae92016-08-10 11:33:55 -0700143 $(mv-modules)
144 $(clean-module-folder)
145
146$(KERNEL_HEADERS_INSTALL): $(KERNEL_OUT)
147 $(hide) if [ ! -z "$(KERNEL_HEADER_DEFCONFIG)" ]; then \
148 rm -f $(BUILD_ROOT_LOC)$(KERNEL_CONFIG); \
Puja Guptae5df4f32017-02-24 14:21:40 -0800149 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_HEADER_DEFCONFIG); \
150 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) headers_install;\
Ameya Thakur300fae92016-08-10 11:33:55 -0700151 if [ -d "$(KERNEL_HEADERS_INSTALL)/include/bringup_headers" ]; then \
152 cp -Rf $(KERNEL_HEADERS_INSTALL)/include/bringup_headers/* $(KERNEL_HEADERS_INSTALL)/include/ ;\
153 fi ;\
154 fi
155 $(hide) if [ "$(KERNEL_HEADER_DEFCONFIG)" != "$(KERNEL_DEFCONFIG)" ]; then \
156 echo "Used a different defconfig for header generation"; \
157 rm -f $(BUILD_ROOT_LOC)$(KERNEL_CONFIG); \
Puja Guptae5df4f32017-02-24 14:21:40 -0800158 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_DEFCONFIG); fi
Ameya Thakur300fae92016-08-10 11:33:55 -0700159 $(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
160 echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
161 echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
Puja Guptae5df4f32017-02-24 14:21:40 -0800162 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) oldconfig; fi
Ameya Thakur300fae92016-08-10 11:33:55 -0700163
164kerneltags: $(KERNEL_OUT) $(KERNEL_CONFIG)
Puja Guptae5df4f32017-02-24 14:21:40 -0800165 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) tags
Ameya Thakur300fae92016-08-10 11:33:55 -0700166
167kernelconfig: $(KERNEL_OUT) $(KERNEL_CONFIG)
168 env KCONFIG_NOTIMESTAMP=true \
Puja Guptae5df4f32017-02-24 14:21:40 -0800169 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) menuconfig
Ameya Thakur300fae92016-08-10 11:33:55 -0700170 env KCONFIG_NOTIMESTAMP=true \
Puja Guptae5df4f32017-02-24 14:21:40 -0800171 $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) savedefconfig
Ameya Thakur300fae92016-08-10 11:33:55 -0700172 cp $(KERNEL_OUT)/defconfig $(TARGET_KERNEL_SOURCE)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_DEFCONFIG)
173
174endif
175endif