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 | |
| 9 | TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH)) |
| 10 | ifeq ($(TARGET_KERNEL_ARCH),) |
| 11 | KERNEL_ARCH := arm |
| 12 | else |
| 13 | KERNEL_ARCH := $(TARGET_KERNEL_ARCH) |
| 14 | endif |
| 15 | |
| 16 | TARGET_KERNEL_HEADER_ARCH := $(strip $(TARGET_KERNEL_HEADER_ARCH)) |
| 17 | ifeq ($(TARGET_KERNEL_HEADER_ARCH),) |
| 18 | KERNEL_HEADER_ARCH := $(KERNEL_ARCH) |
| 19 | else |
| 20 | $(warning Forcing kernel header generation only for '$(TARGET_KERNEL_HEADER_ARCH)') |
| 21 | KERNEL_HEADER_ARCH := $(TARGET_KERNEL_HEADER_ARCH) |
| 22 | endif |
| 23 | |
| 24 | KERNEL_HEADER_DEFCONFIG := $(strip $(KERNEL_HEADER_DEFCONFIG)) |
| 25 | ifeq ($(KERNEL_HEADER_DEFCONFIG),) |
| 26 | KERNEL_HEADER_DEFCONFIG := $(KERNEL_DEFCONFIG) |
| 27 | endif |
| 28 | |
| 29 | # Force 32-bit binder IPC for 64bit kernel with 32bit userspace |
| 30 | ifeq ($(KERNEL_ARCH),arm64) |
| 31 | ifeq ($(TARGET_ARCH),arm) |
| 32 | KERNEL_CONFIG_OVERRIDE := CONFIG_ANDROID_BINDER_IPC_32BIT=y |
| 33 | endif |
| 34 | endif |
| 35 | |
| 36 | TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)) |
| 37 | ifeq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),) |
| 38 | KERNEL_CROSS_COMPILE := arm-eabi- |
| 39 | else |
| 40 | KERNEL_CROSS_COMPILE := $(TARGET_KERNEL_CROSS_COMPILE_PREFIX) |
| 41 | endif |
| 42 | |
| 43 | ifeq ($(TARGET_PREBUILT_KERNEL),) |
| 44 | |
| 45 | KERNEL_GCC_NOANDROID_CHK := $(shell (echo "int main() {return 0;}" | $(KERNEL_CROSS_COMPILE)gcc -E -mno-android - > /dev/null 2>&1 ; echo $$?)) |
| 46 | ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),0) |
| 47 | KERNEL_CFLAGS := KCFLAGS=-mno-android |
| 48 | endif |
| 49 | |
| 50 | mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) |
| 51 | current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) |
| 52 | TARGET_KERNEL := msm-$(TARGET_KERNEL_VERSION) |
| 53 | ifeq ($(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 |
| 60 | else |
| 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 |
| 66 | endif |
| 67 | |
| 68 | KERNEL_CONFIG := $(KERNEL_OUT)/.config |
| 69 | |
| 70 | ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),) |
| 71 | $(error Kernel configuration not defined, cannot build kernel) |
| 72 | else |
| 73 | |
| 74 | ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true) |
| 75 | $(info Using uncompressed kernel) |
| 76 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image |
| 77 | else |
| 78 | ifeq ($(KERNEL_ARCH),arm64) |
| 79 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz |
| 80 | else |
| 81 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage |
| 82 | endif |
| 83 | endif |
| 84 | |
| 85 | ifeq ($(TARGET_KERNEL_APPEND_DTB), true) |
| 86 | $(info Using appended DTB) |
| 87 | TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb |
| 88 | endif |
| 89 | |
| 90 | KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr |
| 91 | KERNEL_MODULES_INSTALL := system |
| 92 | KERNEL_MODULES_OUT := $(TARGET_OUT)/lib/modules |
| 93 | |
| 94 | TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL) |
| 95 | |
| 96 | define mv-modules |
| 97 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 98 | if [ "$$mdpath" != "" ];then\ |
| 99 | mpath=`dirname $$mdpath`;\ |
| 100 | ko=`find $$mpath/kernel -type f -name *.ko`;\ |
| 101 | for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\ |
| 102 | fi |
| 103 | endef |
| 104 | |
| 105 | define clean-module-folder |
| 106 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 107 | if [ "$$mdpath" != "" ];then\ |
| 108 | mpath=`dirname $$mdpath`; rm -rf $$mpath;\ |
| 109 | fi |
| 110 | endef |
| 111 | |
| 112 | ifneq ($(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) |
| 118 | endif |
| 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 | |
| 157 | kerneltags: $(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 | |
| 160 | kernelconfig: $(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 | |
| 167 | endif |
| 168 | endif |