#
# Makefile for the standalone serial driver
#


#########################################################
# Begin user configuration section here
#########################################################

# Set this to the root of your kernel source tree.  This must match
# the version of the kernel for which you are building the serial driver
# as the modules version information and the header files must match.
# The serial.ver file is also installed into this kernel source tree on a
# "make install"
#
LINUX_SRC=	/usr/src/linux-$(shell uname -r)

# Change this to reflect the appropriate architecure for your system
# (i.e., I386_FLAGS, PPC_FLAGS, etc.)
#
ARCH_FLAGS=    $(I386_FLAGS)

# Define the serial driver options as appropriate .
# See srlxrspci.c for more information.
#
CONFIG_FLAGS=	-DCONFIG_SERIAL_MANY_PORTS -DCONFIG_SERIAL_MULTIPORT

#########################################################
# End user configuration section here
#########################################################

SERIALVERSION=	5.05

INCL_FLAGS=	-I. -I$(LINUX_SRC)/include
DEF_FLAGS=	-D__KERNEL__ -DLINUX -DLOCAL_HEADERS -DEXPORT_SYMTAB 

#
# Architecture specific flags go here....
#
I386_FLAGS=	-m486 -O2 -Wall 

PPC_FLAGS=	-Wall -Wstrict-prototypes -O2 -fomit-frame-poin\ter \
			-D__powerpc__ \
			-fsigned-char -msoft-float -pipe -fno-builtin \
			-ffixed-r2 -Wno-uninitialized -mmultiple -mstring

BASE_FLAGS=	$(ARCH_FLAGS) $(INCL_FLAGS) $(DEF_FLAGS) $(CONFIG_FLAGS) \
			`cat .smpflag` -DMODULE

CFLAGS=		$(BASE_FLAGS) `cat .modflag`
GENKSYM_FLAGS=	$(BASE_FLAGS) -D__GENKSYMS__

LDFLAGS= 	-s -N 
CC=		gcc
GENKSYMS=	genksyms

VERFILE=	$(LINUX_SRC)/include/linux/modules/serial.ver
MODVERFILE=	$(LINUX_SRC)/include/linux/modversions.h

DEP_HEADERS=	serial.h serialP.h serial_compat.h \
			$(LINUX_SRC)/include/linux/version.h

all: test_version maybe_modules srlxrspci.o 

test_version:
	@LINUX_SRC=$(LINUX_SRC) NAME="serial" CHK_OBJS=srlxrspci.o \
	sh ./test_version.sh
	@if [ ! -f $(LINUX_SRC)/include/linux/circ_buf.h ]; then \
		echo "circ_buf.h doesn't exist, using local copy." ; \
		mkdir -p linux ; cp circ_buf.h linux ; \
	fi

maybe_modules:
	@if test -s .modflag ; then \
		make modversions.h serial.ver ; \
	fi

install:
	@LINUX_SRC=$(LINUX_SRC) sh ./setup.sh

clean:
	rm -f srlxrspci.o *~ .kver serial.ver modversions.h serial.s core
	rm -rf linux

really-clean: clean
	rm -f *.o .smpflag .modflag

srlxrspci.o: srlxrspci.c $(DEP_HEADERS)
	@echo $(CC) $(CFLAGS) -c srlxrspci.c -o srlxrspci.o ; \
	$(CC) $(CFLAGS) -c srlxrspci.c -o srlxrspci.o
	@sync

serial.s: srlxrspci.c $(DEP_HEADERS)
	@echo $(CC) $(CFLAGS) -S srlxrspci.c ; \
	$(CC) $(CFLAGS) -S srlxrspci.c

serial.ver: srlxrspci.c $(DEP_HEADERS)
	@echo "Generating serial.ver..." ; \
	if test -s .smpflag ; then smp_prefix="-p smp_"; \
	else smp_prefix=; fi; \
	kver=`sed -e 's/-SMP//' -e 's/-MOD//' .kver` ; \
	$(CC) $(GENKSYM_FLAGS) -E srlxrspci.c > serial.tmp ; \
	echo $(CC) $(GENKSYM_FLAGS) -E srlxrspci.c \> serial.tmp; \
	case $$kver in \
	2.0.*)	echo $(GENKSYMS) . < serial.tmp ; \
		$(GENKSYMS) . < serial.tmp ;; \
	2.[1234].*) echo $(GENKSYMS) $$smp_prefix -k $$kver \< serial.tmp ; \
		$(GENKSYMS) $$smp_prefix -k $$kver < serial.tmp \
			> serial.ver ;; \
	*)      echo "Unsupported kernel version $$kver" ;; \
	esac ; rm -f serial.tmp ; echo rm -f serial.tmp ; \
	mkdir -p linux/modules ; cp serial.ver linux/modules ; \

modversions.h: $(MODVERFILE)
	@if ! grep -q /serial.ver $(MODVERFILE) ; then \
		sed -f modversions.sed < $(MODVERFILE) > modversions.tmp ; \
	else \
		cp $(MODVERFILE) modversions.tmp ; \
	fi ; \
	if ! test -f modversions.h || \
	   ! cmp -s modverisions.h modversions.tmp ; then \
		echo "Generating modversions.h" ; \
		mv modversions.tmp modversions.h ; \
		mkdir -p linux ; cp modversions.h linux ; \
	else rm -f modversions.tmp ; fi

SRCROOT = `echo serial-$(SERIALVERSION) | sed -e 's/-WIP//' \
			-e 's/pre-//' -e 's/-PLUS//'`
TAR=tar

.exclude-file: Makefile
	a=$(SRCROOT); \
	(cd .. ; find serial \( -name \*~ -o -name \*.orig -o -name \*.o \
		-o -name CVS -o -name \*.rej -o -name \*.ver \
		-o -name TODO -o -name changed-files -o -name .#\* \) \
		-print) | sed -e "s/serial/$$a/" > .exclude-file
	echo "$(SRCROOT)/Attic" >> .exclude-file
	echo "$(SRCROOT)/linux" >> .exclude-file
	echo "$(SRCROOT)/.kver" >> .exclude-file
	echo "$(SRCROOT)/.smpflag" >> .exclude-file
	echo "$(SRCROOT)/.modflag" >> .exclude-file
	echo "$(SRCROOT)/.exclude-file" >> .exclude-file
	echo "$(SRCROOT)/modversions.h" >> .exclude-file

source_tar_file: .exclude-file
	(cd ..; a=$(SRCROOT); rm -f $$a ; ln -sf serial $$a ; \
		$(TAR) -c -h -v -f - \
			-X $$a/.exclude-file $$a | \
		gzip -9 > serial-$(SERIALVERSION).tar.gz)
	rm -f .exclude-file
