Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 1 | #Android makefile to build kernel as a part of Android Build |
| 2 | PERL = perl |
| 3 | |
| 4 | KERNEL_TARGET := $(strip $(INSTALLED_KERNEL_TARGET)) |
| 5 | ifeq ($(KERNEL_TARGET),) |
| 6 | INSTALLED_KERNEL_TARGET := $(PRODUCT_OUT)/kernel |
| 7 | endif |
| 8 | |
Puja Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 9 | TARGET_KERNEL_MAKE_ENV := $(strip $(TARGET_KERNEL_MAKE_ENV)) |
| 10 | ifeq ($(TARGET_KERNEL_MAKE_ENV),) |
| 11 | KERNEL_MAKE_ENV := |
| 12 | else |
| 13 | KERNEL_MAKE_ENV := $(TARGET_KERNEL_MAKE_ENV) |
| 14 | endif |
| 15 | |
Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 16 | TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH)) |
| 17 | ifeq ($(TARGET_KERNEL_ARCH),) |
| 18 | KERNEL_ARCH := arm |
| 19 | else |
| 20 | KERNEL_ARCH := $(TARGET_KERNEL_ARCH) |
| 21 | endif |
| 22 | |
| 23 | TARGET_KERNEL_HEADER_ARCH := $(strip $(TARGET_KERNEL_HEADER_ARCH)) |
| 24 | ifeq ($(TARGET_KERNEL_HEADER_ARCH),) |
| 25 | KERNEL_HEADER_ARCH := $(KERNEL_ARCH) |
| 26 | else |
| 27 | $(warning Forcing kernel header generation only for '$(TARGET_KERNEL_HEADER_ARCH)') |
| 28 | KERNEL_HEADER_ARCH := $(TARGET_KERNEL_HEADER_ARCH) |
| 29 | endif |
| 30 | |
| 31 | KERNEL_HEADER_DEFCONFIG := $(strip $(KERNEL_HEADER_DEFCONFIG)) |
| 32 | ifeq ($(KERNEL_HEADER_DEFCONFIG),) |
| 33 | KERNEL_HEADER_DEFCONFIG := $(KERNEL_DEFCONFIG) |
| 34 | endif |
| 35 | |
| 36 | # Force 32-bit binder IPC for 64bit kernel with 32bit userspace |
| 37 | ifeq ($(KERNEL_ARCH),arm64) |
| 38 | ifeq ($(TARGET_ARCH),arm) |
| 39 | KERNEL_CONFIG_OVERRIDE := CONFIG_ANDROID_BINDER_IPC_32BIT=y |
| 40 | endif |
| 41 | endif |
| 42 | |
| 43 | TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)) |
| 44 | ifeq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),) |
| 45 | KERNEL_CROSS_COMPILE := arm-eabi- |
| 46 | else |
| 47 | KERNEL_CROSS_COMPILE := $(TARGET_KERNEL_CROSS_COMPILE_PREFIX) |
| 48 | endif |
| 49 | |
| 50 | ifeq ($(TARGET_PREBUILT_KERNEL),) |
| 51 | |
| 52 | KERNEL_GCC_NOANDROID_CHK := $(shell (echo "int main() {return 0;}" | $(KERNEL_CROSS_COMPILE)gcc -E -mno-android - > /dev/null 2>&1 ; echo $$?)) |
| 53 | ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),0) |
| 54 | KERNEL_CFLAGS := KCFLAGS=-mno-android |
| 55 | endif |
| 56 | |
| 57 | mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) |
| 58 | current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) |
| 59 | TARGET_KERNEL := msm-$(TARGET_KERNEL_VERSION) |
| 60 | ifeq ($(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 |
| 67 | else |
| 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 |
| 73 | endif |
| 74 | |
| 75 | KERNEL_CONFIG := $(KERNEL_OUT)/.config |
| 76 | |
| 77 | ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),) |
| 78 | $(error Kernel configuration not defined, cannot build kernel) |
| 79 | else |
| 80 | |
| 81 | ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true) |
| 82 | $(info Using uncompressed kernel) |
| 83 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image |
| 84 | else |
| 85 | ifeq ($(KERNEL_ARCH),arm64) |
| 86 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz |
| 87 | else |
| 88 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage |
| 89 | endif |
| 90 | endif |
| 91 | |
| 92 | ifeq ($(TARGET_KERNEL_APPEND_DTB), true) |
| 93 | $(info Using appended DTB) |
| 94 | TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb |
| 95 | endif |
| 96 | |
| 97 | KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr |
Puja Gupta | 4544540 | 2017-03-24 12:56:52 -0700 | [diff] [blame] | 98 | KERNEL_MODULES_INSTALL ?= system |
| 99 | KERNEL_MODULES_OUT ?= $(PRODUCT_OUT)/$(KERNEL_MODULES_INSTALL)/lib/modules |
Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 100 | |
| 101 | TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL) |
| 102 | |
| 103 | define mv-modules |
| 104 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 105 | if [ "$$mdpath" != "" ];then\ |
| 106 | mpath=`dirname $$mdpath`;\ |
| 107 | ko=`find $$mpath/kernel -type f -name *.ko`;\ |
| 108 | for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\ |
| 109 | fi |
| 110 | endef |
| 111 | |
| 112 | define clean-module-folder |
| 113 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 114 | if [ "$$mdpath" != "" ];then\ |
| 115 | mpath=`dirname $$mdpath`; rm -rf $$mpath;\ |
| 116 | fi |
| 117 | endef |
| 118 | |
| 119 | ifneq ($(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) |
| 125 | endif |
| 126 | |
| 127 | $(KERNEL_OUT): |
| 128 | mkdir -p $(KERNEL_OUT) |
| 129 | |
| 130 | $(KERNEL_CONFIG): $(KERNEL_OUT) |
Puja Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 131 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 132 | $(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 Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 135 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 136 | |
| 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 Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 140 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 143 | $(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 Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 149 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 151 | 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 Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 158 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 159 | $(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 Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 162 | $(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 Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 163 | |
| 164 | kerneltags: $(KERNEL_OUT) $(KERNEL_CONFIG) |
Puja Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 165 | $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) tags |
Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 166 | |
| 167 | kernelconfig: $(KERNEL_OUT) $(KERNEL_CONFIG) |
| 168 | env KCONFIG_NOTIMESTAMP=true \ |
Puja Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 169 | $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) menuconfig |
Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 170 | env KCONFIG_NOTIMESTAMP=true \ |
Puja Gupta | e5df4f3 | 2017-02-24 14:21:40 -0800 | [diff] [blame] | 171 | $(MAKE) -C $(TARGET_KERNEL_SOURCE) O=$(BUILD_ROOT_LOC)$(KERNEL_OUT) $(KERNEL_MAKE_ENV) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) savedefconfig |
Ameya Thakur | 300fae9 | 2016-08-10 11:33:55 -0700 | [diff] [blame] | 172 | cp $(KERNEL_OUT)/defconfig $(TARGET_KERNEL_SOURCE)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_DEFCONFIG) |
| 173 | |
| 174 | endif |
| 175 | endif |