# This software is covered by the GNU GENERAL PUBLIC LICENSE (GPL).
# Any Device - Driver - Linux - gcc
# $Workfile: makefile $
# $Revision: 6 $
# $Date: 1/24/08 9:56a $
#
# Common makefile content for the drivers.
#
# This must be included by the product specific makefile,
# which must define the following.
#
#   DEV_BASE_NAME	The base product name in lower case letters.
#   CWD				The directory from which this makefile is included.
#

default: all

OBJ_FILES	=					\
			bar.o				\
			close.o				\
			dma.o				\
			ioctl.o				\
			irq.o				\
			kernel_2_2.o		\
			kernel_2_4.o		\
			kernel_2_6.o		\
			open.o				\
			main.o				\
			mem.o				\
			mmap.o				\
			pid.o				\
			proc.o				\
			reg.o				\
								\
			dev_os_version.o

# Most of this is ignored by the 2.6 module builder.
CC			= gcc
CC_FLAGS	+= -c -O2 -pipe -fomit-frame-pointer -Wall
CC_FLAGS	+= -D__KERNEL__ -DLINUX
CC_FLAGS	+= -I.
CC_FLAGS	+= -I..
CC_FLAGS	+= -I../..
CC_FLAGS	+= -I../../../../common/driver
CC_FLAGS	+= -I../../../../common/driver/linux
CC_FLAGS	+= -I../../../../common/api
CC_FLAGS	+= -I../../../../common/api/linux
CC_FLAGS	+= -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.*
VPATH		=
VPATH		+= ..
VPATH		+= ../..
VPATH		+= ../../../../common/driver
VPATH		+= ../../../../common/driver/linux
VPATH		+= ../../../../common/api
#VPATH		+= ../../../../common/api/linux



# kernel 2.2 ==================================================================
ifeq ("${KERNELVER}","2.2")
KERNELDIR	= /usr/src/linux
MODULE_NAME	= ${DEV_BASE_NAME}.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	= ${DEV_BASE_NAME}.o

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

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

FORCE:
	@rm -f ${MODULE_NAME}

${MODULE_NAME}: FORCE
	@echo -n ==== Linking to sources ...
	@rm -f *.c *.h
	@# The purpose of the following command line is to link to the sources.
	@# for ... - Calls scan() for each VPATH directory.
	@# scan()  - Calls link() for each file in the given directory.
	@# link()  - Soft link to the file locally if not already done.
	
	@link() {										\
		if [ ! -f ./`basename $${link_file}` ] ;	\
		then										\
			ln -s $${link_file} ;					\
		fi											\
	} ;												\
													\
	scan() {										\
		export files=`ls -a $${scan_dir}/*.[ch]` ;	\
		for file in $${files} ;						\
			do										\
				export link_file=$${file} ;			\
				link ;								\
				done								\
	} ;												\
													\
	for dir in ${VPATH} ;							\
		do											\
			export scan_dir=$${dir} ;				\
			scan ;									\
		done
	@echo ""

	@#-echo -n > ${DEP_FILE}
	@make -C ${KERNELDIR} SUBDIRS=${CWD} modules
	@strip -d --strip-unneeded $@
	@rm -f *.c *.h
endif

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

${MODULE_NAME}: ${OBJ_FILES}
	@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: $(<F)
	@-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} *.scc *.symvers ${DEP_FILE}
	@echo "" > ${DEP_FILE}
	@echo ==== Release Done

clean:
	@echo ==== Cleaning ...
	@rm -rf .tmp_versions
	@rm -f ${RELEASE_RM} *.o *.ko *.scc *.symvers
	@chmod +w ${DEP_FILE}
	@echo "" > ${DEP_FILE}

start:
	@echo ==== Starting ...
	@./${DEV_BASE_NAME}_start

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

include ${DEP_FILE}

