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

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

ARCH := $(shell uname -m)

# make -f _makefile 2.4 or 2.6
all: 2.6

2.4_nodma: checkconfig .depend xpci3xxx_no_DMA.o samples ksamples_24 doc
2.4: checkconfig .depend xpci3xxx.o samples ksamples_24 doc

2.6_nodma: checkconfig xpci3xxx_no_DMA.ko samples ksamples_26 doc
2.6: checkconfig xpci3xxx.ko samples ksamples_26 doc

.PHONY: checkconfig .depend samples dist doc

# make -f _makefile clean
clean: localclean

localclean:
	-cd tests && make clean
	-cd samples && make clean
	-cd ksamples && make -f _makefile clean
	- rm -rf ./doc
	- 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.o' -exec rm {} \;
	- find . -name '.*.cmd' -exec rm {} \;
	- find . -name '*.o.cmd' -exec rm {} \;
	- find . -name '*.ko.cmd' -exec rm {} \;
	- find . -name '*.mod.c' -exec rm {} \;
	- find . -name '*.o.d' -exec rm {} \;
	- find . -name '*.symvers' -exec rm {} \;
	- find . -name '*.symvers.cmd' -exec rm {} \;
	- find . -name '*.order.cmd' -exec rm {} \;
	- find . -name '*.mod.cmd' -exec rm {} \;
	- find . -name '*.mod' -exec rm {} \;
	- find . -name '*-options.h' -exec rm {} \;

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
# USE_ADDIEEPROM => 3 state :
#					-> target x86_64 & USE_ADDIEEPROM defined 	: use s5933 AMCC
#					-> target x86_64 & USE_ADDIEEPROM undefined : use 93c76 EEPROM
#					-> other target as x86_64  					: Don't use eeprom (functions will be disabled)
xpci3xxx.o: option_config
	echo "//#define CONFIG_XPCI3XXX_NO_DMA	1" >> xpci3xxx-options.h;
	$(MAKE) $(KFLAG)=$(shell pwd) CONFIG_XPCI3XXX_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

xpci3xxx_no_DMA.o: option_config
	echo "!!! DMA not used"; \
	echo "#define CONFIG_XPCI3XXX_NO_DMA 	1" >> xpci3xxx-options.h;
	$(MAKE) $(KFLAG)=$(shell pwd) CONFIG_XPCI3XXX_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

xpci3xxx.ko: option_config
	echo "//#define CONFIG_XPCI3XXX_NO_DMA	1" >> xpci3xxx-options.h;
	$(MAKE) $(REDHAT_FLAG) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd) CONFIG_XPCI3XXX_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules
	$(sign_cmd) $@

xpci3xxx_no_DMA.ko: option_config
	echo "!!! DMA not used"; \
	echo "#define CONFIG_XPCI3XXX_NO_DMA 	1" >> xpci3xxx-options.h;
	$(MAKE) $(REDHAT_FLAG) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd) CONFIG_XPCI3XXX_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

.depend:
	$(MAKE) $(KFLAG)=$(shell pwd) CONFIG_XPCI3XXX_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) dep

install: install_26

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

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

ksamples_24: ksamples/ksample01.o ksamples/ksample02.o ksamples/ksample03.o ksamples/ksample04.o ksamples/ksample05.o ksamples/ksample06.o
ksamples_26: ksamples/ksample01.ko ksamples/ksample02.ko ksamples/ksample03.ko ksamples/ksample04.ko ksamples/ksample05.ko ksamples/ksample06.ko

ksamples/$(SYMVERFILE): $(SYMVERFILE)
	-cp $(SYMVERFILE) ksamples/$(SYMVERFILE)

#.PHONY: Module.symvers

# call the kernel's build process to actually build our stuff
ksamples/ksample01.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE01=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample02.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE02=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample03.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE03=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample04.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE04=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample05.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE05=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample06.o:
	$(MAKE) $(KFLAG)=$(shell pwd)/ksamples CONFIG_SAMPLE06=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

ksamples/ksample01.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE01=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

ksamples/ksample02.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE02=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

ksamples/ksample03.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE03=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

ksamples/ksample04.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE04=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

ksamples/ksample05.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE05=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

ksamples/ksample06.ko: ksamples/$(SYMVERFILE)
	$(MAKE) $(SPARSE_FLAGS) $(KFLAG)=$(shell pwd)/ksamples KBUILD_EXTRA_SYMBOLS=$(shell pwd)/Module.symvers CONFIG_SAMPLE06=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) M=$(shell pwd)/ksamples modules

# Example of how to compile the test apps
tests:
	cd tests && ADDIDATAINC=$(ADDIDATAINC) make  all

samples:
	cd samples && ADDIDATAINC=$(ADDIDATAINC) make all

# generates driver's package

ifeq ($(ARCHIVE_DIR),)
ARCHIVE_DIR:=/tmp
endif

dist: localclean doc
	../makedistrib xpci3xxx ./ ./dist/srcfiles $(ADDIDATAINC)/ ./dist/headers $(ARCHIVE_DIR) "" ./dist/extra_files

doc: Doxyfile
	doxygen Doxyfile

# Set USE_ADDIEEPROM to allow using EEPROM lib if the ARCH is valid
option_config: 
	@if [ "$(ARCH)" = "x86_64" ]; then \
        echo "Current architecture is x86_64"; \
    else \
        echo "Current architecture is not x86_64"; \
		echo "#define USE_ADDIEEPROM 1" >> xpci3xxx-options.h; \
    fi
