# SPDX-License-Identifier: GPL-2.0-only
# Copyright (c) 2026 MetaX Integrated Circuits (Shanghai) Co., Ltd. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under the terms of the
# GNU General Public License, version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License, version 2, for more details.
#
# You should have received a copy of the GNU General Public License, version 2, along with this
# program; if not, see <https://www.gnu.org/licenses/>

MX_MAKEFILE_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
BUILD_DATE=\"$(shell date +%Y-%m-%d" "%T)\"
BUILD_USER=\"$(shell echo $${USER})\"
GIT_VALID:=$(shell \
  test -d "$(MX_MAKEFILE_DIR)/.git" && [ "$$(ls -A $(MX_MAKEFILE_DIR)/.git)" ] && \
  echo yes || echo no)
$(info GIT_VALID = $(GIT_VALID))
ifeq ($(GIT_VALID),yes)
  BUILD_COMMIT=\"$(shell git -C $(MX_MAKEFILE_DIR) rev-parse --short HEAD)\"
else
  BUILD_COMMIT=\"unknown\"
endif
subdir-ccflags-y += -DBUILD_DATE="$(BUILD_DATE)"
subdir-ccflags-y += -DBUILD_USER="$(BUILD_USER)"
subdir-ccflags-y += -DBUILD_COMMIT="$(BUILD_COMMIT)"

AC_PATH:=$(shell which autoconf)
ifeq ("$(AC_PATH)","")
  $(error Firsr Install the autoconf & automake)
endif

# The check for KERNELRELEASE is used to separate the two parts of the makefile.
# first pass is by the "make" instance run on the command line
ifeq ($(KERNELRELEASE),)
  ifeq ("$(PREBUILD_KVER)", "")
    # for using "make" directly (dkms doesn't need this step)
    ifeq ("$(KERNEL_PATH)", "")
      $(info KERNEL_PATH not set, use 5.4.0-42-generic)
      KERNEL_PATH := /lib/modules/5.4.0-42-generic/build
      # no need to do pre-build for "make clean" and "make clean_dkms"
      ifeq ($(filter clean%,$(MAKECMDGOALS)),)
        DKMS_PREBUILD := $(shell $(MX_MAKEFILE_DIR)/dkms/pre-build.sh 5.4.0-42-generic $(CC) > /dev/null)
      endif
    endif
  else
    $(info pre-build for kernel version $(PREBUILD_KVER))
    DKMS_PREBUILD := $(shell $(MX_MAKEFILE_DIR)/dkms/pre-build.sh $(PREBUILD_KVER) $(CC) > /dev/null)
  endif
# second pass is by the kbuild system
else
  # compatible with the CLANG compiler
  MX_CC_VERSION = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
  ifneq ($(findstring clang,$(MX_CC_VERSION)),)
    MX_CLANG_FLAGS := $(call cc-disable-warning, implicit-fallthrough)
    MX_CLANG_FLAGS += $(call cc-disable-warning, gnu-variable-sized-type-not-at-end)
    subdir-ccflags-y += $(MX_CLANG_FLAGS)
  endif

  # RHEL/CentOS use backporting for the DRM code
  DRM_VER=$(shell sed -n 's/^RHEL_DRM_VERSION = \(.*\)/\1/p' $(srctree)/Makefile)
  DRM_PATCH=$(shell sed -n 's/^RHEL_DRM_PATCHLEVEL = \(.*\)/\1/p' $(srctree)/Makefile)
  DRM_SUB=$(shell sed -n 's/^RHEL_DRM_SUBLEVEL = \(.*\)/\1/p' $(srctree)/Makefile)
  # for RHEL-9 and some RHEL-8 kernels, "DRM_SUB" is empty
  ifeq ($(DRM_SUB),)
    DRM_SUB = "0"
  endif
  # for non RHEL/CentOS OS (e.g. Ubuntu), DRM_VERSION is LINUX_VERSION
  ifeq ($(DRM_VER),)
    DRM_VER = $(VERSION)
    DRM_PATCH = $(PATCHLEVEL)
    DRM_SUB = $(SUBLEVEL)
  endif
endif

ifeq ("$(WHITE_BRAND)","y")
    subdir-ccflags-y += -DWHITE_BRAND -DLICENSE_CHECK_ON
endif

# Support source code compile and link the ib_register_peer_memory_client
ifeq ("$(MXCD_STATIC_PEER_MEM_SUPPORT)","y")
  ifeq ($(shell type ofed_info >/dev/null 2>&1 && echo 1),1)
    PEER_MEM_KVER=$(shell echo '${KERNEL_PATH}' | cut -d'/' -f4)
    PEER_ARCH=$(shell uname -m)
    ifeq ($(shell grep -qw ib_register_peer_memory_client /usr/src/ofa_kernel/${PEER_ARCH}/${PEER_MEM_KVER}/Module.symvers >/dev/null 2>&1  && echo 1), 1)
      $(info found ib_register_peer_memory_client from ofed)
      subdir-ccflags-y += -DHAVE_MXCD_STATIC_PEER_MEM_SUPPORT
      KBUILD_EXTRA_SYMBOLS += /usr/src/ofa_kernel/$(PEER_ARCH)/$(PEER_MEM_KVER)/Module.symvers
    endif
  endif
endif

subdir-ccflags-y += \
        -DDRM_VER=$(DRM_VER) \
        -DDRM_PATCH=$(DRM_PATCH) \
        -DDRM_SUB=$(DRM_SUB)

subdir-ccflags-y += -include $(src)/dkms/config/config.h
subdir-ccflags-y += -include $(src)/include/ksp/mxksp_drm_version.h
subdir-ccflags-y += -include $(src)/include/ksp/mxksp_helper_backport.h

LINUXINCLUDE += -I$(src)/include	\
	-I$(src)/metaxgpu	\
	-I$(src)/interface

subdir-ccflags-y += $(MX_DEBUG_CFLAGS)

ifneq ($(MX_BUILD_MODE),gcc)
subdir-ccflags-y += -DMXLOG_CHECK_TYPE
endif

ifeq ($(TARGET_NAME),gvm)
subdir-ccflags-y += -DDEVIONFO_PCI_PF_ONLY -DNO_DMA_BUF
obj-m += mxgvm/
else
obj-m += metaxgpu/
endif

all: gvm
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR)
debug:
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR) MX_DEBUG_CFLAGS="-g"
debug_og:
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR) MX_DEBUG_CFLAGS="-Og -g"
gvm:
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR) TARGET_NAME="gvm"
qemu: gpu
gpu:
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR) TARGET_NAME="gpu"

-include $(MX_MAKEFILE_DIR)/mxobjcom/mxobjcom.mk
ifndef OBJEXIST
	MX_BUILD_MODE:=gcc
	export MX_BUILD_MODE
endif
clean:
	+make -C $(KERNEL_PATH) M=$(MX_MAKEFILE_DIR) clean
clean_dkms: clean
	@rm -rf dkms/config/config.h dkms/config/config.h.in dkms/config/config.h.in~ \
		dkms/Makefile dkms/aclocal.m4 dkms/configure dkms/build-* \
		dkms/.tmp_versions dkms/config.log dkms/config.status
