PREFIX = open_

ifeq ($(CHIPARCH),)
	@echo CHIPARCH must be set
endif

# aarch64 SoCs (e.g. hi3519dv500) build a 64-bit kernel/userspace ABI; the
# 32-bit families keep the historical default.
OT_BIT_WIDTH := 32
ifeq ($(CHIPARCH),hi3519dv500)
    OT_BIT_WIDTH := 64
endif
ccflags-y := -D$(CHIPARCH) -DRELEASE -DUSER_BIT_$(OT_BIT_WIDTH) -DKERNEL_BIT_$(OT_BIT_WIDTH) -Wno-date-time -D_GNU_SOURCE
ccflags-y += -include $(src)/compat/kernel_compat.h
# Re-add GCC's built-in include dir (stdarg.h etc.) stripped by -nostdinc in 5.15+
ccflags-y += -isystem $(shell $(CC) -print-file-name=include)

# Vendor SDK families use their own header/source layout
ifeq ($(CHIPARCH),hi3516cv500)
    include $(src)/hi3516cv500.kbuild
else ifeq ($(CHIPARCH),hi3519v101)
    include $(src)/hi3519v101.kbuild
else ifeq ($(CHIPARCH),hi3516cv300)
    include $(src)/hi3516cv300.kbuild
else ifeq ($(CHIPARCH),hi3516cv200)
    include $(src)/hi3516cv200.kbuild
else ifeq ($(CHIPARCH),hi3516cv100)
    include $(src)/hi3516cv100.kbuild
else ifeq ($(CHIPARCH),hi3516av100)
    include $(src)/hi3516av100.kbuild
else ifeq ($(CHIPARCH),hi3520dv200)
    include $(src)/hi3520dv200.kbuild
else ifeq ($(CHIPARCH),hi3536dv100)
    include $(src)/hi3536dv100.kbuild
else ifeq ($(CHIPARCH),hi3516cv6xx)
    include $(src)/hi3516cv6xx.kbuild
else ifeq ($(CHIPARCH),hi3519dv500)
    include $(src)/hi3519dv500.kbuild
else ifeq ($(CHIPARCH),gk7205v500)
    include $(src)/gk7205v500.kbuild
else ifeq ($(CHIPARCH),gk7206v100)
    include $(src)/gk7206v100.kbuild
else

ccflags-y += -I$(src)/../include
ccflags-y += -I$(src)/ext_inc

ccflags-y += -I$(src)/isp/arch/$(CHIPARCH)/include
ccflags-y += -I$(src)/isp/mkp/include
ccflags-y += -I$(src)/../libraries/isp/arch/$(CHIPARCH)/include
ccflags-y += -I$(src)/../libraries/isp/include

include $(src)/hisi-hwrng/Kbuild
include $(src)/sys_config/Kbuild
include $(src)/osal/Kbuild
include $(src)/wdt/Kbuild
include $(src)/adc/Kbuild
ifndef DISABLE_ISP
    include $(src)/isp/Kbuild
endif
include $(src)/piris/Kbuild
include $(src)/pwm/Kbuild
ifndef DISABLE_IST
    include $(src)/sample_ist/Kbuild
endif
include $(src)/sensor_i2c/Kbuild
include $(src)/sensor_spi/Kbuild
include $(src)/mipi_rx/Kbuild
include $(src)/openipc_frame_ts/Kbuild
# Guarded on CHIPARCH inside; the V4 pair (hi3516ev200, gk7205v200) builds a
# cipher module here, and hi3516cv500 builds its own from hi3516cv500.kbuild.
include $(src)/cipher/Kbuild
#+user

ifneq ($(filter $(CHIPARCH),hi3516ev200 gk7205v200),)

# V4 vendor blobs ship under per-chiparch subdirs to make their origin
# explicit. Pick the one whose userspace ABI matches the OpenIPC
# firmware shipping alongside this kernel. The kernel<->userspace MPI
# boundary (ioctl numbers, struct layouts in mod_ext.h, comm_video.h,
# etc.) is binary, not source-level: scripts/hi2gk.sh handles only
# source-level renames, so cross-vintage userspace + kernel pairings
# segfault majestic on its first MPI call.
#
#   kernel/<mod>/hi3516ev200/<mod>.o
#       HiSi Hi3516EV200_SDK_V1.0.1.2 (Oct 2019). The only variant
#       on the HiSi side because pre-built blobs were never released
#       for newer HiSi SDKs (V5.x ships source only, and the V4
#       module set isn't in V5 anyway). Matches OpenIPC's HiSi-vintage
#       libmpi.so on hi3516ev200/ev300 boards.
#
#   kernel/<mod>/gk7205v200/<mod>_log.o
#       Goke GKIPCLinuxV100R001C00SPC030 ko_log (Aug 2021). Opt-in
#       debug build for gk7205v200/v300 — verbose printk, larger.
#
#   kernel/<mod>/gk7205v200/<mod>_nolog.o
#       Goke SPC030 ko_nolog (Aug 2021). Default for gk7205v200/v300
#       — ~30% smaller, required by the 5 MB rootfs cap on the
#       gk7205v300 lite build. Matches Goke userspace (libgk_*.so
#       MPP_V1.0.1.0 B020 + libhi_mpi.so).
#
# KO_LOG selects log vs nolog within the gk7205v200 family. hi3516ev200
# silently ignores it (only one variant exists).
ifeq ($(CHIPARCH),hi3516ev200)
KO_LOG_SUFFIX :=
else ifeq ($(CHIPARCH),gk7205v200)
KO_LOG ?= nolog
ifeq ($(KO_LOG),nolog)
KO_LOG_SUFFIX := _nolog
else ifeq ($(KO_LOG),log)
KO_LOG_SUFFIX := _log
else
$(error KO_LOG must be 'log' or 'nolog', got '$(KO_LOG)')
endif
endif

    include $(src)/acodec/Kbuild
    include $(src)/adec/Kbuild
    include $(src)/aenc/Kbuild
    include $(src)/ai/Kbuild
    include $(src)/aio/Kbuild
    include $(src)/ao/Kbuild
    include $(src)/base/Kbuild
    include $(src)/chnl/Kbuild
    include $(src)/h264e/Kbuild
    include $(src)/h265e/Kbuild
    include $(src)/ive/Kbuild
    include $(src)/ive_neo/Kbuild
    include $(src)/jpege/Kbuild
ifndef DISABLE_PM
    # it doesn't compile yet
    #include $(src)/pm/Kbuild
endif
    include $(src)/rc/Kbuild
    include $(src)/rgn/Kbuild
    include $(src)/sys/Kbuild
ifndef DISABLE_TDE
    include $(src)/tde/Kbuild
endif
    include $(src)/vedu/Kbuild
    include $(src)/venc/Kbuild
    include $(src)/vgs/Kbuild
    include $(src)/vi/Kbuild
ifndef DISABLE_VO
    #include $(src)/vo/Kbuild
endif
    include $(src)/vpss/Kbuild
    # +fb
endif

endif # hi3516cv500/hi3516cv200 else
