# General build makefile.  (Drivers)

DDIR = $(shell pwd)

KDIR ?= /lib/modules/$(shell uname -r)/build
MODDIR ?= /lib/modules/$(shell uname -r)/kernel/drivers/misc
INCLUDE ?= $(DDIR)/../include

CP ?= /bin/cp
RM ?= /bin/rm
DEPMOD ?= /sbin/depmod


## ============================================================================
HEADER = $(INCLUDE)/seaio.h
DEPENS = seaio.c seaio_cards.h $(HEADER)
OBJS = seaio.o
MODULES = seaio.ko

## ============================================================================


# Extra compiler flags
# force no-pie for distro compilers that enable pie by default
# expand to nothing for older compilers that do not have the no-pie flags
NOPIE = $(shell $(CC) -Q -v 2>&1 >/dev/null | sed -n '/--enable-default-pie/c\-fno-pie -no-pie')
CFLAG = " -I$(INCLUDE) $(NOPIE)"

# -----------------------------------------------------------------------------
#if KERENLRELEASE has been initialized, we are being run from the kbuild system
# -----------------------------------------------------------------------------
ifneq ($(KERNELRELEASE),)
  obj-m := $(OBJS)


# -----------------------------------------------------------------------------
# Otherwise we are being run from somewhere else and need to start up kbuild
# -----------------------------------------------------------------------------
else

all: $(MODULES)
	@echo " *Kernel Driver Module compiled"
	@echo

objs: all

$(HEADER):
	@echo
	@echo "  ERROR!  Can't find include file!"
	@echo
	@echo "       I suggest you obtain another copy of this distribution."
	@echo
	@exit 1

$(KDIR):
	@echo
	@echo "  ERROR!  No kernel source found in $(KDIR)"
	@echo
	@echo "       If the above directory is not the correct location"
	@echo "       for your source, please update the KDIR environment"
	@echo "       variable or pass it to the Makefile directly --"
	@echo "       make KDIR=/usr/src/linux"
	@echo
	@exit 2

$(KDIR)/.config:
	@echo
	@echo "  ERROR!  No configured kernel source found in $(KDIR)"
	@echo
	@echo "       If your kernel source is located at that directory"
	@echo "       you probably need to configure it.  First change"
	@echo "       directory to your kernel source (cd /KDIR) then run"
	@echo "       make xconfig, make menuconfig, or make oldconfig"
	@echo "       to configure your kernel source.  This step is"
	@echo "       necessary because some header files are generated at"
	@echo "       this step."
	@echo
	@exit 3

$(MODULES): $(KDIR) $(KDIR)/.config $(DEPENS)
	@echo "   Building kernel module(s): $(MODULES)"
	@$(MAKE) -s -C $(KDIR) SUBDIRS=$(DDIR) EXTRA_CFLAGS=$(CFLAG) M=$(DDIR) modules

install: $(MODULES)
	@echo "   Installing modules to $(MODDIR)"
	@mkdir --parents $(MODDIR)
	@for n in $(MODULES); do $(CP) $$n $(MODDIR); done
	@$(DEPMOD) -a
	@echo " *Done Installing $(MODULES)"
	@echo

uninstall:
	@echo "   Uninstalling modules from $(MODDIR)"
	@$(shell ../scripts/seaiounload)
	@for n in $(MODULES); do $(RM) -f ${MODDIR}/$$n; done
	@$(DEPMOD) -a
	@echo " *$(MODULES) removed"
	@echo

clean:
	@echo "   Removing driver module build files"
	@$(RM) -rf *~ *.o *.ko *mod* .*.o* .*.ko* .*.mod* .tmp* *.symvers 
	@echo " *Module build files removed"
	@echo

endif
