# This software is covered by the GNU GENERAL PUBLIC LICENSE (GPL).
# $Revision: 6 $
# $Date: 7/30/07 8:24a $
# Makefile for the HPDI32 driver.

default: all

# Most of this is ignored by the 2.6 module builder.
CC		= gcc
CC_FLAGS	+= -c -O6 -pipe -fomit-frame-pointer -Wall
CC_FLAGS	+= -D__KERNEL__ -DLINUX
CC_FLAGS	+= -I. -I${KERNELDIR}
DEP_FILE	= makefile.dep
KERNELDIR	=
KERNELVER	= $(shell uname -r | cut -d . -f1-2 )
RELEASE_RM	= ${OBJ_FILES} .tmp* .*.o.cmd .*.ko.cmd *.mod.* Modules*
TARGET		= "hpdi32"

OBJ_FILES	=		\
		close.o		\
		dma.o		\
		fasync.o	\
		io.o		\
		io_dma.o	\
		io_pio.o	\
		ioctl.o		\
		irq.o		\
		kernel_2_2.o	\
		kernel_2_4.o	\
		kernel_2_6.o	\
		main.o		\
		mmap.o		\
		open.o		\
		proc.o		\
		reg.o



# kernel 2.2 ==================================================================
ifeq ("${KERNELVER}","2.2")
KERNELDIR	= /usr/src/linux
MODULE_NAME	= ${TARGET}".o"

${MODULE_NAME}: ${OBJ_FILES}
	@echo ==== Linking: $@
	@ld -r -o $@ $^
endif

# kernel 2.4 ==================================================================
ifeq ("${KERNELVER}","2.4")
# Some distributions use /usr/include/linux
KERNELDIR	= /usr/src/linux-2.4/include
MODULE_NAME	= ${TARGET}".o"

${MODULE_NAME}: ${OBJ_FILES}
	@echo ==== Linking: $@
	@ld -r -o $@ $^
endif

# kernel 2.6 ==================================================================
ifeq ("${KERNELVER}","2.6")
hpdi32-objs	= ${OBJ_FILES}
KERNELDIR	= /lib/modules/$(shell uname -r)/build
MODULE_NAME	= ${TARGET}".ko"
obj-m		= hpdi32.o
PWD		= $(shell pwd)
RELEASE_RM	+= hpdi32.o Module.symvers

${MODULE_NAME}:
	@-echo -n > ${DEP_FILE}
	@make -C ${KERNELDIR} SUBDIRS=${PWD} modules
	@strip -d --strip-unneeded $@
endif

# kernel OTHER ================================================================
ifeq ("${KERNELDIR}","")
KERNELDIR	= /UNKNOWN_AT_THIS_TIME
MODULE_NAME	= /UNKNOWN_AT_THIS_TIME

${MODULE_NAME}:
	@echo ERROR: KERNEL ${KERNELVER} IS NOT SUPPORTED BY THIS MAKEFILE.
	@_ERROR_ERROR_ERROR_ERROR_ERROR_ERROR_ERROR_ERROR_ERROR
endif



# COMMON ======================================================================

# This is ignored by the 2.6 module builder.
.c.o:
	@echo == Compiling: $<
	@-chmod +rw ${DEP_FILE}
	@# Get the dependency list for this module.
	@-${CC} -MM ${CC_FLAGS} $< >  .tmp1
	@# Remove trailing white space and backslash, if present.
	@-sed -e "s/[ ]*[\\\\]//g" < .tmp1 > .tmp2
	@# Put everything on seperate lines.
	@-tr [:space:] \\n < .tmp2 > .tmp3
	@# Remove all of the system include files.
	@-grep -v "^[ ]*/" < .tmp3 > .tmp4
	@# Remove all empty lines.
	@-grep [[:alnum:]] < .tmp4 > .tmp5
	@# Put everything on the same line.
	@-tr '\n' '\040' < .tmp5 > .tmp6
	@-echo -e '\012' >> .tmp6
	@# Add all the other dependencies to the end of this file.
	@-echo >> ${DEP_FILE}
	@-grep -v "^[ ]*$@" < ${DEP_FILE} >> .tmp6
	@# Remove blank lines from the list.
	@-grep "[[:alnum:]]" < .tmp6 > .tmp7
	@# Sort the list and put it in the dependency file.
	@-sort < .tmp7 > ${DEP_FILE}
	@# Cleanup.
	@rm -f .tmp*
	@# Compile the module.
	@${CC} ${CC_FLAGS} $< -o $@



all: ${MODULE_NAME}
	@echo ==== All Done

release: ${MODULE_NAME}
	@rm -rf  ${RELEASE_RM}
	@echo ==== Release Done

clean:
	@echo ==== Cleaning ...
	@rm -rf ${RELEASE_RM} *.o *.ko
	@echo > ${DEP_FILE}

${DEP_FILE}:
	@echo ==== Creating: $@
	@echo > ${DEP_FILE}

include ${DEP_FILE}

