# This makefile is for "local" compilation, that is compiling a driver without using the standard Linux way (make Xconfig etc...)
# Anyway to compile this driver you need to have a already configured Linux kernel source tree

# This makefile allows to build the driver outside the kernel
# you need then to specify where are located the kernel source the includes used by this driver

# Notes:
# * the source of these drivers are generally located in the kernel source directory
# in the path $(TOPDIR)/drivers/addidata/
# * the the ADDI-DATA specific includes are generally located in the kernel source directory
# in the path $(TOPDIR)/include/linux/addidata
include ../redhat.mk

####################################################################
# relative path of the source of the kernel to build against
# IF YOU ARE BUILDING THIS DRIVER OUTSIDE THE KERNEL SOURCE
# PLEASE ADAPT THIS VARIABLE TO YOUR LOCAL INSTALLATION 
ifeq ($(KERNELSRCDIR),)
KERNELSRCDIR:=/lib/modules/$(shell uname -r)/build
endif
####################################################################


####################################################################
# relative path of the addidata include directory
# IF YOU ARE BUILDING THIS DRIVER OUTSIDE THE KERNEL SOURCE
# PLEASE ADAPT THIS VARIABLE TO YOUR LOCAL INSTALLATION 
ifeq ($(ADDIDATAINC),)
ADDIDATAINC:=$(shell pwd)/../../../include/linux/addidata
endif
####################################################################

####################################################################
# Use M= instead of $(KFLAG)= is mandatory with kernel > 5.x
KFLAG := $(shell [ "$$(uname -r | cut -c1)" -ge 5 ] && echo M || echo SUBDIRS)
####################################################################

all: 2.6

# Sign the kernel modules
ifneq ("$(wildcard ./sign.sh)","")
sign_cmd=./sign.sh $(KERNELSRCDIR)
else
sign_cmd=@echo
endif

2.4: checkconfig .depend apci1016.o samples

2.6: checkconfig apci1016.ko samples

.PHONY: checkconfig .depend samples tests

# make -f _makefile clean
clean: localclean

#remove all files generated by build process
localclean:	
	-cd tests && make clean
	-cd samples && make clean
	- find . -name '*.o' -exec rm {} \;
	- find . -name '*.ko' -exec rm {} \;
	- find . -name '*.o.flags' -exec rm {} \;
	- find . -name '.depend' -exec rm {} \;
	- find . -name '*~' -exec rm {} \;
	- find . -name '*.mod' -exec rm {} \;
	- find . -name '*.mod.c' -exec rm {} \; 
	- find . -name '*.mod.o' -exec rm {} \;
	- find . -name '*.o.cmd' -exec rm {} \;
	- find . -name '*.ko.cmd' -exec rm {} \;
	- find . -name '*.o.d' -exec rm {} \;
	- find . -name '*.symvers' -exec rm {} \;
	- find . -name '.tmp_versions' -exec rm -Rf {} \;
	- find . -name 'Module.markers' -exec rm -Rf {} \;
	- find . -name 'modules.order' -exec rm -Rf {} \;	

checkconfig:
	@if [ ! -e $(KERNELSRCDIR)/.config  ]; \
	then echo ".config \
	* Is your kernel configured (make xconfig)? \
	* Did you correctly adapt _makefile ?"; \
	echo "KERNELSRCDIR="$(KERNELSRCDIR);\
	echo "ADDIDATAINC="$(ADDIDATAINC);\
	exit 1; fi

# check for the sparse static code checker
sparse_path:=$(shell which sparse)

ifneq ($(sparse_path),)
$(info info: sparse found as "$(sparse_path)": activating static checking for driver code while compiling for Linux 2.6 )
SPARSE_FLAGS:="C=1"
endif

# call the kernel's build process to actually build our stuff

# for kernel 2.4
apci1016.o:
	$(MAKE) $(KFLAG)=$(shell pwd) CONFIG_APCI1016_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

# for kernel 2.6
apci1016.ko:
	$(MAKE) $(REDHAT_FLAG) $(SPARSE_FLAGS)  $(KFLAG)=$(shell pwd) CONFIG_APCI1016_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR)  modules
	$(sign_cmd) $@
.depend:
	$(MAKE) $(KFLAG)=$(shell pwd) CONFIG_APCI1016_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR)  dep

install: install_26

install_24: apci1016.o
	$(MAKE)   $(KFLAG)=$(shell pwd) CONFIG_APCI1016_IOCTL=m  -C $(KERNELSRCDIR) modules_install

install_26: apci1016.ko
	$(MAKE)   $(KFLAG)=$(shell pwd) CONFIG_APCI1016_IOCTL=m  -C $(KERNELSRCDIR) modules_install
	# Install the udev rules for this driver 
	depmod -a
	./mkudevrules.sh

tests:
	cd tests && ADDIDATAINC=$(ADDIDATAINC) make 

samples:
	cd samples && ADDIDATAINC=$(ADDIDATAINC) make
	
ifeq ($(ARCHIVE_DIR),)
ARCHIVE_DIR:=/tmp
endif

# generates driver's package
dist: localclean
	../makedistrib apci1016 ./ ./dist/srcfiles $(ADDIDATAINC)/ ./dist/headers $(ARCHIVE_DIR)
	
