mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(dir $(mkfile_path))

# Find directory of kern-module-utils submodule
KERN_UTILS_HEADER_DIR ?= $(mkfile_dir)/../submodules/kern-module-utils
#ifeq ("$(wildcard $(KERN_UTILS_HEADER_DIR)/README.md)","")
#$(error Could not find the kern-module-utils submodule headers. Please run the command 'git submodule update --init' You can also define KERN_UTILS_HEADER_DIR if you have a repo checkout you would rather use.)
#endif

# Find directory of rts-logger headers
RTS_LOGGER_HEADERS_DIR ?= /usr/src/rts-logger-*/
ifeq ("$(wildcard $(RTS_LOGGER_HEADERS_DIR)/drv/rts-logger.h)","")
$(error Could not find the rts-logger header. Is the advligorts-logger-dkms package installed? You can also define RTS_LOGGER_HEADERS_DIR if you have a repo checkout)
endif
# We go up one dir (../) here to preserve "#include "drv/rts-logger.h"
RTS_LOGGER_HEADERS:=$(shell dirname $(shell find $(RTS_LOGGER_HEADERS_DIR) -name rts-logger.h))/../

# obj-m specifie we're a kernel module.
obj-m += ligo_hw_usr_driver.o
ligo_hw_usr_driver-objs := module.o ligo28ao32.o ligo24ai32.o ligoPTC.o ligo_converter_private.o ligo_fw_ctrl.o

INCS :=-I$(src)/../include/ -I$(RTS_LOGGER_HEADERS) -I$(KERN_UTILS_HEADER_DIR)
CFLAGS := -std=gnu99 -Wno-declaration-after-statement -ffast-math -m80387 -msse2
ccflags-y := $(CFLAGS) $(INCS)


# Set the path to the Kernel build utils.
KBUILD=/lib/modules/$(shell uname -r)/build/

CPU_ISO_SYMVERS += $(shell find /var/lib/dkms/rts-logger/ -name Module.symvers | head -n 1)


KBUILD_EXTRA_SYMBOLS := $(CPU_ISO_SYMVERS)

default:
	$(MAKE) -C $(KBUILD) M=$(PWD) modules

clean:
	$(MAKE) -C $(KBUILD) M=$(PWD) clean

menuconfig:
	$(MAKE) -C $(KBUILD) M=$(PWD) menuconfig
