13298bea7Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
23298bea7Sopenharmony_ci# This program is free software; you can redistribute it and/or modify
33298bea7Sopenharmony_ci# it under the terms of the GNU General Public License as published by
43298bea7Sopenharmony_ci# the Free Software Foundation; either version 2 of the License, or
53298bea7Sopenharmony_ci# (at your option) any later version.
63298bea7Sopenharmony_ci#
73298bea7Sopenharmony_ci# This program is distributed in the hope that it will be useful,
83298bea7Sopenharmony_ci# but WITHOUT ANY WARRANTY; without even the implied warranty of
93298bea7Sopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103298bea7Sopenharmony_ci# GNU General Public License for more details.
113298bea7Sopenharmony_ci#
123298bea7Sopenharmony_ci# You should have received a copy of the GNU General Public License
133298bea7Sopenharmony_ci# along with this program. If not, see <http://www.gnu.org/licenses/>.
143298bea7Sopenharmony_ci#
153298bea7Sopenharmony_ci# ohos makefile to build kernel
163298bea7Sopenharmony_ci
173298bea7Sopenharmony_ciPRODUCT_NAME=$(TARGET_PRODUCT)
183298bea7Sopenharmony_ciOHOS_BUILD_HOME := $(realpath $(shell pwd)/../../../)
193298bea7Sopenharmony_ciKERNEL_SRC_TMP_PATH := $(OUT_DIR)/kernel/${KERNEL_VERSION}
203298bea7Sopenharmony_ciKERNEL_OBJ_TMP_PATH := $(OUT_DIR)/kernel/OBJ/${KERNEL_VERSION}
213298bea7Sopenharmony_ciifeq ($(BUILD_TYPE), standard)
223298bea7Sopenharmony_ci    BOOT_IMAGE_PATH = $(OHOS_BUILD_HOME)/device/board/hisilicon/hispark_taurus/uboot/prebuilts
233298bea7Sopenharmony_ci    KERNEL_SRC_TMP_PATH := $(OUT_DIR)/kernel/src_tmp/${KERNEL_VERSION}
243298bea7Sopenharmony_ci    export KERNEL_SRC_DIR=out/KERNEL_OBJ/kernel/src_tmp/${KERNEL_VERSION}
253298bea7Sopenharmony_ciendif
263298bea7Sopenharmony_ci
273298bea7Sopenharmony_ciKERNEL_SRC_PATH := $(OHOS_BUILD_HOME)/kernel/linux/${KERNEL_VERSION}
283298bea7Sopenharmony_ciKERNEL_PATCH_PATH := $(OHOS_BUILD_HOME)/kernel/linux/patches/${KERNEL_VERSION}
293298bea7Sopenharmony_ciKERNEL_CONFIG_PATH := $(OHOS_BUILD_HOME)/kernel/linux/config/${KERNEL_VERSION}
303298bea7Sopenharmony_ciPREBUILTS_GCC_DIR := $(OHOS_BUILD_HOME)/prebuilts/gcc
313298bea7Sopenharmony_ciCLANG_HOST_TOOLCHAIN := $(OHOS_BUILD_HOME)/prebuilts/clang/ohos/linux-x86_64/llvm/bin
323298bea7Sopenharmony_ciKERNEL_HOSTCC := $(CLANG_HOST_TOOLCHAIN)/clang
333298bea7Sopenharmony_ciKERNEL_PREBUILT_MAKE := make
343298bea7Sopenharmony_ciCLANG_CC := $(CLANG_HOST_TOOLCHAIN)/clang
353298bea7Sopenharmony_ci
363298bea7Sopenharmony_ciKERNEL_CROSS_COMPILE :=
373298bea7Sopenharmony_ciifeq ($(KERNEL_ARCH), arm)
383298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN := $(PREBUILTS_GCC_DIR)/linux-x86/arm/gcc-linaro-7.5.0-arm-linux-gnueabi/bin
393298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN_PREFIX := $(KERNEL_TARGET_TOOLCHAIN)/arm-linux-gnueabi-
403298bea7Sopenharmony_cielse ifeq ($(KERNEL_ARCH), arm64)
413298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN := $(PREBUILTS_GCC_DIR)/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin
423298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN_PREFIX := $(KERNEL_TARGET_TOOLCHAIN)/aarch64-linux-gnu-
433298bea7Sopenharmony_cielse ifeq ($(KERNEL_ARCH), riscv64)
443298bea7Sopenharmony_ci    PATH := $(CLANG_HOST_TOOLCHAIN):$(PATH)
453298bea7Sopenharmony_ci    KERNEL_ARCH := riscv
463298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN_PREFIX :=
473298bea7Sopenharmony_ci    KERNEL_CROSS_COMPILE += LLVM=1
483298bea7Sopenharmony_ci    KERNEL_CROSS_COMPILE += LLVM_IAS=1
493298bea7Sopenharmony_cielse ifeq ($(KERNEL_ARCH), x86_64)
503298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN := gcc
513298bea7Sopenharmony_ci    KERNEL_TARGET_TOOLCHAIN_PREFIX :=
523298bea7Sopenharmony_ciendif
533298bea7Sopenharmony_ci
543298bea7Sopenharmony_ciifeq ($(DEVICE_NAME), hispark_phoenix)
553298bea7Sopenharmony_ciKERNEL_CROSS_COMPILE += CONFIG_MSP="y"
563298bea7Sopenharmony_ciendif
573298bea7Sopenharmony_ci
583298bea7Sopenharmony_ciKERNEL_CROSS_COMPILE += CC="$(CLANG_CC)"
593298bea7Sopenharmony_ci
603298bea7Sopenharmony_ciifneq ($(KERNEL_ARCH), x86_64)
613298bea7Sopenharmony_ciKERNEL_CROSS_COMPILE += CROSS_COMPILE="$(KERNEL_TARGET_TOOLCHAIN_PREFIX)"
623298bea7Sopenharmony_ciendif
633298bea7Sopenharmony_ci
643298bea7Sopenharmony_ciKERNEL_MAKE := \
653298bea7Sopenharmony_ci    PATH="$(BOOT_IMAGE_PATH):$$PATH" \
663298bea7Sopenharmony_ci    $(KERNEL_PREBUILT_MAKE)
673298bea7Sopenharmony_ci
683298bea7Sopenharmony_ci
693298bea7Sopenharmony_ciifneq ($(findstring $(BUILD_TYPE), small standard),)
703298bea7Sopenharmony_ciDEVICE_PATCH_DIR := $(OHOS_BUILD_HOME)/kernel/linux/patches/${KERNEL_VERSION}/$(DEVICE_NAME)_patch
713298bea7Sopenharmony_ciDEVICE_PATCH_FILE := $(DEVICE_PATCH_DIR)/$(DEVICE_NAME).patch
723298bea7Sopenharmony_ciPRODUCT_PATCH_FILE := $(OHOS_BUILD_HOME)/vendor/hisilicon/watchos/patches/$(DEVICE_NAME).patch
733298bea7Sopenharmony_ciSMALL_PATCH_FILE := $(DEVICE_PATCH_DIR)/$(DEVICE_NAME)_$(BUILD_TYPE).patch
743298bea7Sopenharmony_ciKERNEL_IMAGE_FILE := $(KERNEL_SRC_TMP_PATH)/arch/$(KERNEL_ARCH)/boot/$(KERNEL_IMAGE)
753298bea7Sopenharmony_ciDEFCONFIG_FILE := $(DEVICE_NAME)_$(BUILD_TYPE)_defconfig
763298bea7Sopenharmony_ciUNIFIED_COLLECTION_PATCH_FILE := ${OHOS_BUILD_HOME}/kernel/linux/common_modules/ucollection/apply_ucollection.sh
773298bea7Sopenharmony_ci
783298bea7Sopenharmony_ciexport KBUILD_OUTPUT=$(KERNEL_OBJ_TMP_PATH)
793298bea7Sopenharmony_ci
803298bea7Sopenharmony_ci$(KERNEL_IMAGE_FILE):
813298bea7Sopenharmony_ci	$(hide) echo "build kernel..."
823298bea7Sopenharmony_ciifeq ($(DEVICE_NAME), hispark_phoenix)
833298bea7Sopenharmony_ci	$(hide) rm -rf $(KERNEL_SRC_TMP_PATH);mkdir -p $(KERNEL_SRC_TMP_PATH);cp -arfP $(KERNEL_SRC_PATH)/* $(KERNEL_SRC_TMP_PATH)/
843298bea7Sopenharmony_ci	$(hide) cd $(KERNEL_SRC_TMP_PATH)/drivers && rm -rf common && ln -s $(SDK_SOURCE_DIR)/common/drv ./common && cd -
853298bea7Sopenharmony_ci	$(hide) cd $(KERNEL_SRC_TMP_PATH)/drivers && rm -rf msp && ln -s $(SDK_SOURCE_DIR)/msp/drv ./msp && cd -
863298bea7Sopenharmony_cielse
873298bea7Sopenharmony_ci	$(hide) rm -rf $(KERNEL_SRC_TMP_PATH);mkdir -p $(KERNEL_SRC_TMP_PATH);cp -arfL $(KERNEL_SRC_PATH)/* $(KERNEL_SRC_TMP_PATH)/
883298bea7Sopenharmony_ciendif
893298bea7Sopenharmony_ci	$(hide) $(OHOS_BUILD_HOME)/drivers/hdf_core/adapter/khdf/linux/patch_hdf.sh $(OHOS_BUILD_HOME) $(KERNEL_SRC_TMP_PATH) $(KERNEL_PATCH_PATH) $(DEVICE_NAME)
903298bea7Sopenharmony_ci
913298bea7Sopenharmony_ciifeq ($(PRODUCT_PATH), vendor/hisilicon/watchos)
923298bea7Sopenharmony_ci	$(hide) cd $(KERNEL_SRC_TMP_PATH) && patch -p1 < $(PRODUCT_PATCH_FILE)
933298bea7Sopenharmony_cielse
943298bea7Sopenharmony_ci	$(hide) cd $(KERNEL_SRC_TMP_PATH) && test -f $(DEVICE_PATCH_FILE) && patch -p1 < $(DEVICE_PATCH_FILE) || true
953298bea7Sopenharmony_ciendif
963298bea7Sopenharmony_ci
973298bea7Sopenharmony_ciifneq ($(findstring $(BUILD_TYPE), small),)
983298bea7Sopenharmony_ci	$(hide) cd $(KERNEL_SRC_TMP_PATH) && patch -p1 < $(SMALL_PATCH_FILE)
993298bea7Sopenharmony_ciendif
1003298bea7Sopenharmony_ciifeq ($(UNIFIED_COLLECTION_PATCH_FILE), $(wildcard $(UNIFIED_COLLECTION_PATCH_FILE)))
1013298bea7Sopenharmony_ci	$(hide) $(UNIFIED_COLLECTION_PATCH_FILE) $(OHOS_BUILD_HOME) $(KERNEL_SRC_TMP_PATH) $(DEVICE_NAME) $(KERNEL_VERSION)
1023298bea7Sopenharmony_ciendif
1033298bea7Sopenharmony_ci	$(hide) cp -rf $(KERNEL_CONFIG_PATH)/. $(KERNEL_SRC_TMP_PATH)/
1043298bea7Sopenharmony_ci	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) distclean
1053298bea7Sopenharmony_ci	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(DEFCONFIG_FILE)
1063298bea7Sopenharmony_ciifeq ($(KERNEL_VERSION), linux-5.10)
1073298bea7Sopenharmony_ci	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) modules_prepare
1083298bea7Sopenharmony_ciendif
1093298bea7Sopenharmony_ci	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 $(KERNEL_IMAGE)
1103298bea7Sopenharmony_ciendif
1113298bea7Sopenharmony_ciifeq ($(DEVICE_NAME), hispark_phoenix)
1123298bea7Sopenharmony_ci	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) dtbs
1133298bea7Sopenharmony_ciendif
1143298bea7Sopenharmony_ci.PHONY: build-kernel
1153298bea7Sopenharmony_cibuild-kernel: $(KERNEL_IMAGE_FILE)
116