Skip to content

Commit

Permalink
styx: Checkout kernel, dtbo rules to lineage-20
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilSonar committed Jan 1, 2023
1 parent 938a13e commit 073c7ac
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 121 deletions.
2 changes: 1 addition & 1 deletion build/tasks/dt_image.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ endif
$(INSTALLED_DTIMAGE_TARGET): $(DTBTOOL) $(INSTALLED_KERNEL_TARGET)
$(build-dtimage-target)
ifeq ($(strip $(BOARD_KERNEL_LZ4C_DT)),true)
lz4 -9 < $@ > $(LZ4_DT_IMAGE) || lz4c -c1 -y $@ $(LZ4_DT_IMAGE)
prebuilts/tools-lineage/${HOST_OS}-x86/bin/lz4 -9 < $@ > $(LZ4_DT_IMAGE)
$(hide) $(ACP) $(LZ4_DT_IMAGE) $@
endif
@echo "Made DT image: $@"
Expand Down
146 changes: 98 additions & 48 deletions build/tasks/kernel.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#
# These config vars are usually set in BoardConfig.mk:
#
# TARGET_KERNEL_ADDITIONAL_FLAGS = Additional make flags, optional
# TARGET_KERNEL_CONFIG = List of kernel defconfigs, first one being the base one,
# while all the others are fragments that will be merged
# to main one in .config.
Expand All @@ -30,10 +31,6 @@
#
# TARGET_KERNEL_CLANG_COMPILE = Compile kernel with clang, defaults to true
#
# TARGET_KERNEL_CLANG_VERSION = Clang prebuilts version, optional, defaults to clang-stable
#
# TARGET_KERNEL_CLANG_PATH = Clang prebuilts path, optional
#
# BOARD_KERNEL_IMAGE_NAME = Built image name
# for ARM use: zImage
# for ARM64 use: Image.gz
Expand All @@ -43,14 +40,16 @@
# For example, for ARM devices,
# use zImage-dtb instead of zImage.
#
# BOARD_DTB_CFG = Path to a mkdtboimg.py config file for dtb.img
#
# BOARD_DTBO_CFG = Path to a mkdtboimg.py config file
#
# BOARD_CUSTOM_DTBIMG_MK = Path to a custom dtbimage makefile
#
# BOARD_CUSTOM_DTBOIMG_MK = Path to a custom dtboimage makefile
#
# KERNEL_CC = The C Compiler used. This is automatically set based
# on whether the clang version is set, optional.
# KERNEL_LD = The Linker used. This is automatically set based
# on whether the clang/gcc version is set, optional.
#
# KERNEL_CLANG_TRIPLE = Target triple for clang (e.g. aarch64-linux-gnu-)
# defaults to arm-linux-gnu- for arm
Expand All @@ -69,8 +68,8 @@
# TARGET_FORCE_PREBUILT_KERNEL = Optional, use TARGET_PREBUILT_KERNEL even if
# kernel sources are present

ifneq ($(TARGET_PROVIDES_KERNEL_MAKEFILE),true)
ifneq ($(TARGET_NO_KERNEL),true)
ifneq ($(TARGET_NO_KERNEL_OVERRIDE),true)

## Externally influenced variables
KERNEL_SRC := $(TARGET_KERNEL_SOURCE)
Expand All @@ -82,6 +81,7 @@ SELINUX_DEFCONFIG := $(TARGET_KERNEL_SELINUX_CONFIG)

## Internal variables
DTC := $(HOST_OUT_EXECUTABLES)/dtc
PAHOLE := $(HOST_OUT_EXECUTABLES)/pahole
KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
RECOVERY_KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/RECOVERY_KERNEL_OBJ
DTBO_OUT := $(TARGET_OUT_INTERMEDIATES)/DTBO_OBJ
Expand Down Expand Up @@ -162,16 +162,22 @@ else
$(error "NO KERNEL CONFIG")
else
ifneq ($(TARGET_FORCE_PREBUILT_KERNEL),)
$(warning **********************************************************)
$(warning * Kernel source found and configuration was defined *)
$(warning * but prebuilt kernel is being enforced. *)
$(warning * While there may be a good reason for this, *)
$(warning * THIS IS NOT ADVISED. *)
$(warning * Please configure your device to build the kernel *)
$(warning * from source by unsetting TARGET_FORCE_PREBUILT_KERNEL *)
$(warning **********************************************************)
FULL_KERNEL_BUILD := false
KERNEL_BIN := $(TARGET_PREBUILT_KERNEL)
ifneq ($(filter RELEASE NIGHTLY SNAPSHOT EXPERIMENTAL,$(LINEAGE_BUILDTYPE)),)
$(error "PREBUILT KERNEL IS NOT ALLOWED ON OFFICIAL BUILDS!")
else
$(warning **********************************************************)
$(warning * Kernel source found and configuration was defined, *)
$(warning * but prebuilt kernel is being forced. *)
$(warning * While this is likely intentional, *)
$(warning * it is NOT SUPPORTED WHATSOEVER. *)
$(warning * Generated kernel headers may not align with *)
$(warning * the ABI of kernel you're including. *)
$(warning * Please unset TARGET_FORCE_PREBUILT_KERNEL *)
$(warning * to build the kernel from source. *)
$(warning **********************************************************)
FULL_KERNEL_BUILD := false
KERNEL_BIN := $(TARGET_PREBUILT_KERNEL)
endif
else
FULL_KERNEL_BUILD := true
KERNEL_BIN := $(TARGET_PREBUILT_INT_KERNEL)
Expand Down Expand Up @@ -221,43 +227,50 @@ ifeq ($(or $(FULL_RECOVERY_KERNEL_BUILD), $(FULL_KERNEL_BUILD)),true)
# Add host bin out dir to path
PATH_OVERRIDE := PATH=$(KERNEL_BUILD_OUT_PREFIX)$(HOST_OUT_EXECUTABLES):$$PATH
ifneq ($(TARGET_KERNEL_CLANG_COMPILE),false)
ifneq ($(TARGET_KERNEL_CLANG_VERSION),)
KERNEL_CLANG_VERSION := clang-$(TARGET_KERNEL_CLANG_VERSION)
else
# Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
KERNEL_CLANG_VERSION := $(LLVM_PREBUILTS_VERSION)
endif
TARGET_KERNEL_CLANG_PATH ?= $(BUILD_TOP)/prebuilts/clang/host/$(HOST_PREBUILT_TAG)/$(KERNEL_CLANG_VERSION)
ifeq ($(KERNEL_ARCH),arm64)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=aarch64-linux-gnu-
else ifeq ($(KERNEL_ARCH),arm)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=arm-linux-gnu-
else ifeq ($(KERNEL_ARCH),x86)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=x86_64-linux-gnu-
ifneq ($(KERNEL_NO_GCC), true)
ifeq ($(KERNEL_ARCH),arm64)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=aarch64-linux-gnu-
else ifeq ($(KERNEL_ARCH),arm)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=arm-linux-gnu-
else ifeq ($(KERNEL_ARCH),x86)
KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=x86_64-linux-gnu-
endif
PATH_OVERRIDE += LD_LIBRARY_PATH=$(TARGET_KERNEL_CLANG_PATH)/lib64:$$LD_LIBRARY_PATH
endif
PATH_OVERRIDE += PATH=$(TARGET_KERNEL_CLANG_PATH)/bin:$$PATH LD_LIBRARY_PATH=$(TARGET_KERNEL_CLANG_PATH)/lib64:$$LD_LIBRARY_PATH
PATH_OVERRIDE += PATH=$(TARGET_KERNEL_CLANG_PATH)/bin:$$PATH
ifeq ($(KERNEL_CC),)
KERNEL_CC := CC="$(CCACHE_BIN) clang"
endif
ifeq ($(KERNEL_LD),)
KERNEL_LD :=
endif
endif

ifneq ($(TARGET_KERNEL_MODULES),)
$(error TARGET_KERNEL_MODULES is no longer supported!)
ifneq ($(KERNEL_NO_GCC), true)
PATH_OVERRIDE += PATH=$(KERNEL_TOOLCHAIN_PATH_gcc):$$PATH
endif

PATH_OVERRIDE += PATH=$(KERNEL_TOOLCHAIN_PATH_gcc):$$PATH

# System tools are no longer allowed on 10+
PATH_OVERRIDE += $(TOOLS_PATH_OVERRIDE)

ifeq (true,$(filter true, $(TARGET_NEEDS_DTBOIMAGE) $(BOARD_KERNEL_SEPARATED_DTBO)))
KERNEL_MAKE_FLAGS += DTC_EXT=$(KERNEL_BUILD_OUT_PREFIX)$(DTC)
endif

ifneq ($(TARGET_KERNEL_ADDITIONAL_FLAGS),)
KERNEL_MAKE_FLAGS += $(TARGET_KERNEL_ADDITIONAL_FLAGS)
endif

# Internal implementation of make-kernel-target
# $(1): output path (The value passed to O=)
# $(2): target to build (eg. defconfig, modules, dtbo.img)
define internal-make-kernel-target
$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_BUILD_OUT_PREFIX)$(1) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_LD) $(2)
$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_BUILD_OUT_PREFIX)$(1) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(2)
endef

# Make an external module target
# $(1): module name
# $(2): module root path
# $(3): target to build (eg. modules_install)
define make-external-module-target
$(PATH_OVERRIDE) $(KERNEL_MAKE_CMD) $(KERNEL_MAKE_FLAGS) -C $(TARGET_KERNEL_EXT_MODULE_ROOT)/$(1) M=$(2)/$(1) KERNEL_SRC=$(BUILD_TOP)/$(KERNEL_SRC) OUT_DIR=$(KERNEL_BUILD_OUT_PREFIX)$(KERNEL_OUT) O=$(KERNEL_BUILD_OUT_PREFIX)$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(3)
endef

# Generate kernel .config from a given defconfig
Expand Down Expand Up @@ -327,7 +340,7 @@ endef
# $(4): staging dir
# $(5): module load list
# Depmod requires a well-formed kernel version so 0.0 is used as a placeholder.
define build-image-kernel-modules-styx
define build-image-kernel-modules-lineage
mkdir -p $(2)/lib/modules
cp $(1) $(2)/lib/modules/
rm -rf $(4)
Expand Down Expand Up @@ -374,8 +387,19 @@ $(INSTALLED_VENDORIMAGE_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
endif
MODULES_INTERMEDIATES := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,kernel_modules)

ifneq (,$(filter dlkm,$(BOARD_VENDOR_RAMDISK_FRAGMENTS)))
KERNEL_VENDOR_RAMDISK_MODULES_OUT := $(VENDOR_RAMDISK_FRAGMENT.dlkm.STAGING_DIR)
KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor_ramdisk_fragment-stage-dlkm)
$(INTERNAL_VENDOR_RAMDISK_FRAGMENT_TARGETS): $(TARGET_PREBUILT_INT_KERNEL)
else ifeq ($(PRODUCT_BUILD_VENDOR_KERNEL_BOOT_IMAGE),true)
KERNEL_VENDOR_RAMDISK_MODULES_OUT := $(TARGET_VENDOR_KERNEL_RAMDISK_OUT)
KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor_kernel_ramdisk)
$(INTERNAL_VENDOR_KERNEL_RAMDISK_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
else
KERNEL_VENDOR_RAMDISK_MODULES_OUT := $(TARGET_VENDOR_RAMDISK_OUT)
KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR := $(KERNEL_BUILD_OUT_PREFIX)$(call intermediates-dir-for,PACKAGING,depmod_vendor_ramdisk)
$(INTERNAL_VENDOR_RAMDISK_TARGET): $(TARGET_PREBUILT_INT_KERNEL)
endif

$(KERNEL_OUT):
mkdir -p $(KERNEL_OUT)
Expand All @@ -384,7 +408,7 @@ $(KERNEL_CONFIG): $(KERNEL_OUT) $(ALL_KERNEL_DEFCONFIG_SRCS)
@echo "Building Kernel Config"
$(call make-kernel-config,$(KERNEL_OUT),$(KERNEL_DEFCONFIG))

$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_CONFIG) $(DEPMOD) $(DTC)
$(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_CONFIG) $(DEPMOD) $(DTC) $(PAHOLE)
@echo "Building Kernel Image ($(BOARD_KERNEL_IMAGE_NAME))"
$(call make-kernel-target,$(BOARD_KERNEL_IMAGE_NAME))
$(hide) if grep -q '^CONFIG_OF=y' $(KERNEL_CONFIG); then \
Expand All @@ -396,20 +420,28 @@ $(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_CONFIG) $(DEPMOD) $(DTC)
$(call make-kernel-target,modules) || exit "$$?"; \
echo "Installing Kernel Modules"; \
$(call make-kernel-target,INSTALL_MOD_PATH=$(MODULES_INTERMEDIATES) INSTALL_MOD_STRIP=1 modules_install); \
$(if $(TARGET_KERNEL_EXT_MODULES),\
echo "Building and Installing External Kernel Modules"; \
rpath=$$(python3 -c 'import os,sys;print(os.path.relpath(*(sys.argv[1:])))' $(TARGET_KERNEL_EXT_MODULE_ROOT) $(KERNEL_SRC)); \
$(foreach p, $(TARGET_KERNEL_EXT_MODULES),\
$$pwd; \
$(call make-external-module-target,$(p),$$rpath,); \
$(call make-external-module-target,$(p),$$rpath,INSTALL_MOD_PATH=$(MODULES_INTERMEDIATES) INSTALL_MOD_STRIP=1 KERNEL_UAPI_HEADERS_DIR=$(KERNEL_OUT) modules_install)); \
) \
kernel_release=$$(cat $(KERNEL_RELEASE)) \
kernel_modules_dir=$(MODULES_INTERMEDIATES)/lib/modules/$$kernel_release \
$(foreach s, $(TARGET_MODULE_ALIASES),\
$(eval p := $(subst :,$(space),$(s))) \
; mv $$(find $$kernel_modules_dir -name $(word 1,$(p))) $$kernel_modules_dir/$(word 2,$(p))); \
modules=$$(find $$kernel_modules_dir -type f -name '*.ko'); \
($(call build-image-kernel-modules-styx,$$modules,$(KERNEL_MODULES_OUT),$(KERNEL_MODULE_MOUNTPOINT)/,$(KERNEL_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_KERNEL_MODULES_LOAD))); \
($(call build-image-kernel-modules-lineage,$$modules,$(KERNEL_MODULES_OUT),$(KERNEL_MODULE_MOUNTPOINT)/,$(KERNEL_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_KERNEL_MODULES_LOAD))); \
$(if $(BOOT_KERNEL_MODULES),\
vendor_boot_modules=$$(for m in $(BOOT_KERNEL_MODULES); do \
p=$$(find $$kernel_modules_dir -type f -name $$m); \
if [ -n "$$p" ]; then echo $$p; else echo "ERROR: $$m from BOOT_KERNEL_MODULES was not found" 1>&2 && exit 1; fi; \
done); \
[ $$? -ne 0 ] && exit 1; \
($(call build-image-kernel-modules-styx,$$vendor_boot_modules,$(TARGET_VENDOR_RAMDISK_OUT),/,$(KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD))); \
($(call build-image-kernel-modules-lineage,$$vendor_boot_modules,$(KERNEL_VENDOR_RAMDISK_MODULES_OUT),/,$(KERNEL_VENDOR_RAMDISK_DEPMOD_STAGING_DIR),$(BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD))); \
) \
fi

Expand Down Expand Up @@ -443,33 +475,51 @@ $(BOARD_PREBUILT_DTBOIMAGE):
@echo "Building dtbo.img"
$(hide) find $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtbo" | xargs rm -f
$(call make-dtbo-target,$(KERNEL_DEFCONFIG))
$(call make-dtbo-target,$(TARGET_KERNEL_DTB))
ifeq ($(BOARD_KERNEL_SEPARATED_DTBO),true)
$(call make-dtbo-target,dtbs)
ifdef BOARD_DTBO_CFG
$(MKDTBOIMG) cfg_create $@ $(BOARD_DTBO_CFG) -d $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts
else
$(MKDTBOIMG) create $@ --page_size=$(BOARD_KERNEL_PAGESIZE) $(shell find $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtbo" | sort)
endif # BOARD_DTBO_CFG
else
$(call make-dtbo-target,dtbo.img)
$(call make-dtbo-target,$(TARGET_KERNEL_DTBO))
endif # BOARD_KERNEL_SEPARATED_DTBO
$(hide) touch -c $(DTBO_OUT)
endif # BOARD_CUSTOM_DTBOIMG_MK
endif # TARGET_NEEDS_DTBOIMAGE/BOARD_KERNEL_SEPARATED_DTBO

ifeq ($(BOARD_INCLUDE_DTB_IN_BOOTIMG),true)
ifneq ($(BOARD_CUSTOM_DTBIMG_MK),)
include $(BOARD_CUSTOM_DTBIMG_MK)
else
ifeq ($(BOARD_PREBUILT_DTBIMAGE_DIR),)
$(DTB_OUT):
mkdir -p $(DTB_OUT)

ifdef BOARD_DTB_CFG
MKDTBOIMG := $(HOST_OUT_EXECUTABLES)/mkdtboimg.py$(HOST_EXECUTABLE_SUFFIX)
$(INSTALLED_DTBIMAGE_TARGET): $(MKDTBOIMG)
endif
$(INSTALLED_DTBIMAGE_TARGET): $(DTC) $(DTB_OUT)
ifeq ($(TARGET_WANTS_EMPTY_DTB),true)
@rm -f $@
echo "empty" > $@
else
@echo "Building dtb.img"
$(hide) find $(DTB_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtb" | xargs rm -f
$(call make-dtb-target,$(KERNEL_DEFCONFIG))
$(call make-dtb-target,dtbs)
$(call make-dtb-target,$(TARGET_KERNEL_DTB))
ifdef BOARD_DTB_CFG
$(MKDTBOIMG) cfg_create $@ $(BOARD_DTB_CFG) -d $(DTB_OUT)/arch/$(KERNEL_ARCH)/boot/dts
else
cat $(shell find $(DTB_OUT)/arch/$(KERNEL_ARCH)/boot/dts -type f -name "*.dtb" | sort) > $@
endif # BOARD_DTB_CFG
$(hide) touch -c $(DTB_OUT)
endif # !TARGET_WANTS_EMPTY_DTB

endif # !BOARD_PREBUILT_DTBIMAGE_DIR
endif # BOARD_CUSTOM_DTBIMG_MK
endif # BOARD_INCLUDE_DTB_IN_BOOTIMG

endif # FULL_KERNEL_BUILD
Expand All @@ -483,7 +533,7 @@ $(RECOVERY_KERNEL_CONFIG): $(ALL_RECOVERY_KERNEL_DEFCONFIG_SRCS)
@echo "Building Recovery Kernel Config"
$(call make-kernel-config,$(RECOVERY_KERNEL_OUT),$(RECOVERY_DEFCONFIG))

$(TARGET_PREBUILT_INT_RECOVERY_KERNEL): $(RECOVERY_KERNEL_CONFIG) $(DEPMOD) $(DTC)
$(TARGET_PREBUILT_INT_RECOVERY_KERNEL): $(RECOVERY_KERNEL_CONFIG) $(DEPMOD) $(DTC) $(PAHOLE)
@echo "Building Recovery Kernel Image ($(BOARD_KERNEL_IMAGE_NAME))"
$(call make-recovery-kernel-target,$(BOARD_KERNEL_IMAGE_NAME))

Expand Down Expand Up @@ -514,5 +564,5 @@ dtboimage: $(INSTALLED_DTBOIMAGE_TARGET)
.PHONY: dtbimage
dtbimage: $(INSTALLED_DTBIMAGE_TARGET)

endif # TARGET_NO_KERNEL_OVERRIDE
endif # TARGET_NO_KERNEL
endif # TARGET_PROVIDES_KERNEL_MAKEFILE
Loading

0 comments on commit 073c7ac

Please sign in to comment.