Prasad Sodagudi | 0489ba2 | 2017-10-04 14:44:24 -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_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 | |
| 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 | |
| 54 | real_cc := |
| 55 | ifeq ($(KERNEL_LLVM_SUPPORT),true) |
| 56 | real_cc := REAL_CC=$(KERNEL_LLVM_BIN) CLANG_TRIPLE=aarch64-linux-gnu- |
| 57 | else |
| 58 | ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),0) |
| 59 | KERNEL_CFLAGS := KCFLAGS=-mno-android |
| 60 | endif |
| 61 | endif |
| 62 | |
| 63 | mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) |
| 64 | current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) |
| 65 | TARGET_KERNEL := msm-$(TARGET_KERNEL_VERSION) |
| 66 | ifeq ($(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 |
| 73 | else |
| 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 |
| 79 | endif |
| 80 | |
| 81 | KERNEL_CONFIG := $(KERNEL_OUT)/.config |
| 82 | |
| 83 | ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),) |
| 84 | $(error Kernel configuration not defined, cannot build kernel) |
| 85 | else |
| 86 | |
| 87 | ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true) |
| 88 | $(info Using uncompressed kernel) |
| 89 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image |
| 90 | else |
| 91 | ifeq ($(KERNEL_ARCH),arm64) |
| 92 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz |
| 93 | else |
| 94 | TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage |
| 95 | endif |
| 96 | endif |
| 97 | |
| 98 | ifeq ($(TARGET_KERNEL_APPEND_DTB), true) |
| 99 | $(info Using appended DTB) |
| 100 | TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb |
| 101 | endif |
| 102 | |
| 103 | KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr |
| 104 | KERNEL_MODULES_INSTALL ?= system |
| 105 | KERNEL_MODULES_OUT ?= $(PRODUCT_OUT)/$(KERNEL_MODULES_INSTALL)/lib/modules |
| 106 | |
| 107 | TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL) |
| 108 | |
| 109 | define mv-modules |
| 110 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 111 | if [ "$$mdpath" != "" ];then\ |
| 112 | mpath=`dirname $$mdpath`;\ |
| 113 | ko=`find $$mpath/kernel -type f -name *.ko`;\ |
| 114 | for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\ |
| 115 | fi |
| 116 | endef |
| 117 | |
| 118 | define clean-module-folder |
| 119 | mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\ |
| 120 | if [ "$$mdpath" != "" ];then\ |
| 121 | mpath=`dirname $$mdpath`; rm -rf $$mpath;\ |
| 122 | fi |
| 123 | endef |
| 124 | |
| 125 | ifneq ($(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) |
| 131 | endif |
| 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 | |
| 170 | kerneltags: $(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 | |
| 173 | kernelconfig: $(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 | |
| 180 | endif |
| 181 | endif |