# Note: Compiling kernel modules requires creating symlinks, which is not possible on certain 
# filesystems (notably VirtualBox vmfs); therefore we allow using /run/ through the 'symlink' target,
# if necessary.
 
LINUX_DIR_PATH = /lib/modules/$(shell uname -r)/build
ifdef CCPREFIX
    CCPAR = CROSS_COMPILE=${CCPREFIX}
endif
ifdef ARCH
    CCPAR += ARCH=${ARCH}
endif

MODULE_MAKE_FILE = Makefile
INSTALL = install

C_SRC_FILES = src/rtc-unipi.c

OBJ_FILES  = $(C_SRC_FILES:.c=.o)

obj-m += rtc-unipi.o
rtc-unipi-objs := ${OBJ_FILES}

.PHONY: default
default: all ;

all: 
	$(MAKE) $(CCPAR) -C $(LINUX_DIR_PATH) M=${PWD} modules

modules_install: 
	$(MAKE) $(CCPAR) -C $(LINUX_DIR_PATH) M=${PWD} modules_install

dkms:
	@echo $(obj-m:.o=) > dkms.list
	$(INSTALL) -D $(MODULE_MAKE_FILE) -t $(INSTALL_MOD_PATH)
	$(INSTALL) -D $(C_SRC_FILES) -t $(INSTALL_MOD_PATH)/src

clean: 
	rm -f ${unipi-objs} src/.*.o.cmd src/*.o src/.*.o.d
	rm -f rtc-unipi.ko $(obj-m:.o=.mod.c) $(obj-m:.o=.mod) .*.cmd *.o modules.order Module.symvers
	rm -rf .tmp_versions
