# 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

####################################################################
# 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:=/usr/src/linux/
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:=/usr/src/linux/include/linux/addidata/
endif
####################################################################

# make -f _makefile
all: 
	@echo "please choose either 2.4 or 2.6"

# make -f _makefile 2.4 or 2.6
2.4: checkconfig .depend samples_24

2.6: checkconfig avert samples_26

.PHONY: checkconfig .depend avert

# make -f _makefile clean
clean: localclean

localclean:	
	- 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.o' -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 '.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
samples_24: checkconfig ksample01.o
samples_26: checkconfig ksample01.ko

# 2.4
#here we tell the Makefile that we want to compile the target modules
#the target "modules" will compile all files in "obj-m" variable
#to compile only one target, we use a variable CONFIG_APCI3600_SAMPLExx=m
#that will be set for each target samplexx.o of this file (_makefile)
ksample01.o: 
	make  SUBDIRS=$(shell pwd) CONFIG_APCI1648_SAMPLE00=m ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules

#2.6
ksample01.ko: 
	cp ../Module.symvers ./Module.symvers
	make $(SPARSE_FLAGS) SUBDIRS=$(shell pwd) CONFIG_APCI1648_SAMPLE00=m ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) modules
	
.depend:
	make  SUBDIRS=$(shell pwd) CONFIG_sample00_IOCTL=m  ADDIDATAINC=$(ADDIDATAINC) -C $(KERNELSRCDIR) dep

