AndroidKernel.mk 7.2 KB

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