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