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

# Find directory of rts-cpu-isolator headers
CPU_ISO_HEADERS_DIR ?= /usr/src/rts-cpu-isolator-*/
ifeq ("$(wildcard $(CPU_ISO_HEADERS_DIR)/rts-cpu-isolator.h)","")
$(error Could not find the CPU Isolator header. Is the advligorts-cpu-isolator-dkms package installed? You can also define CPU_ISO_HEADERS_DIR if you have a repo checkout)
endif
CPU_ISO_HEADERS:=$(shell dirname $(shell find $(CPU_ISO_HEADERS_DIR) -name rts-cpu-isolator.h) )

# 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))/../

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

# obj-m specifie we're a kernel module.
obj-m := eth-ipc-manager.o 
eth-ipc-manager-objs := src/manager.o src/ligo_roce_mcast.o


#OFA_INCLUDES := -I/usr/src/ofa_kernel/x86_64/6.1.0-26-amd64/include/
INCS:= $(OFA_INCLUDES) -I$(mkfile_dir)./include/ -I$(mkfile_dir)./submodules/kern-module-utils/ -I$(mkfile_dir)./submodules/advligorts/src/include 

#DKMS Build has slightly different directory structure
INCS+= -I$(mkfile_dir) 


ccflags-y :=  -std=gnu99 -Wno-declaration-after-statement 
ccflags-y += $(INCS)  -I$(CPU_ISO_HEADERS) -I$(RTS_LOGGER_HEADERS) -I$(GPS_TIME_HEADERS)

#This is how you insert OFA includes to skip built in linux headers
#SAVE := $(LINUXINCLUDE)
#LINUXINCLUDE=$(OFA_INCLUDES) $(SAVE)


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

#BUILD_DIR :=  $(PWD)/build/
#BUILD_DIR_MAKEFILE := $(BUILD_DIR)/Makefile

ifndef CPU_ISO_SYMVERS
CPU_ISO_SYMVERS := $(shell find /var/lib/dkms/rts-cpu-isolator/ -name Module.symvers | head -n 1)
endif

ifndef RTS_LOGGER_SYM
RTS_LOGGER_SYM := $(shell find /var/lib/dkms/rts-logger/ -name Module.symvers | head -n 1)
endif

ifndef RTS_GPSTIME_SYM
RTS_GPSTIME_SYM := $(shell find /var/lib/dkms/gpstime/ -name Module.symvers | head -n 1)
endif

#ifndef OFED_SYM
#OFED_SYM := $(shell find /usr/src/ofa_kernel/x86_64/6.1.0-26-amd64/ -name Module.symvers | head -n 1)
#endif


KBUILD_EXTRA_SYMBOLS := $(CPU_ISO_SYMVERS) $(RTS_LOGGER_SYM) $(RTS_GPSTIME_SYM) #$(OFED_SYM)

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

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

