#
# Copyright © 2022 Moore Threads Inc. All rights reserved.
#

KVER ?= $(shell uname -r)

KDIR ?= /lib/modules/$(KVER)/build

PWD := $(shell pwd)
GIT_COMMIT := $(shell cd $(PWD); git log -1 --format=%h 2>/dev/null)

obj-m := sgpu_km.o
sgpu_km-objs := sgpu_linux.o
sgpu_km-objs += utils.o
sgpu_km-objs += sgpu_sched.o
sgpu_km-objs += sgpu_ioctl.o
sgpu_km-objs += sgpu.o
sgpu_km-objs += sgpu_procfs.o
sgpu_km-objs += sgpu_procfs_ioctl.o
sgpu_km-objs += sgpu_fence.o

PHONY += all clean git-commit gen_mt_configure
.PHONY: $(PHONY)

DEBUG ?= 0
ifeq ($(DEBUG), 1)
$(info debug mode is on)
CFLAGS_utils.o := -DDEBUG
DEBUG_CFLAGS += -g -DDEBUG
ccflags-y += ${DEBUG_CFLAGS}
CC += ${DEBUG_CFLAGS}
endif

ifneq ($(GIT_COMMIT),)
		ifneq ($(strip $(shell cd $(PWD); git status --porcelain 2>/dev/null)),)
			GIT_COMMIT := $(GIT_COMMIT)-dirty
		endif
		ccflags-y += -D"GIT_COMMIT=\"$(GIT_COMMIT)\""
endif

all: gen_mt_configure
		$(MAKE) -C $(KDIR) M="$(PWD)" PWD="$(PWD)" modules
clean:
		$(MAKE) -C $(KDIR) M="$(PWD)" clean
		rm -f config-host.h
git-commit:
		@echo $(GIT_COMMIT)
gen_mt_configure:
		@ ./configure $(KVER)