#
# Makefile for SeaIO Software
#
# $Id: Makefile,v 1.21 2004/08/06 18:23:21 dalew Exp $
#

# Location of the kernel source
# Required to build drivers outside of kernel source tree.
#
KERNEL_SRC_DIR = /usr/src/linux

# Destination to install SeaIO utility programs
#
UTIL_INSTALL_DIR = /usr/local/bin

# device driver base names
#
DIO_DRIVERS   = seaio_driver

# The complete kernel source version string (a.b.c.d)
#
KERNEL_SRC_VERSION := $(shell ./kernelver.sh ${KERNEL_SRC_DIR})

# First 2 numbers of kernel source version (a.b)
#
KERNEL_SRC_VERSION_PATCH := $(shell ./kernelver.sh VERSION_PATCH ${KERNEL_SRC_DIR})

# The kernel source patch level (2nd version number) (b)
#
KERNEL_SRC_PATCHLEVEL := $(shell ./kernelver.sh PATCHLEVEL ${KERNEL_SRC_DIR})

# Set flag if development kernel (patch level is odd)
#
DEVKERNEL  := $(shell expr $(KERNEL_SRC_PATCHLEVEL) % 2)

# Subdirectory of SeaIO distribution containing driver source
# for the kernel source version. Round up for development
# kernel versions (odd patch level).
#
# examples : driver/kernel-2.4 for kernel source version 2.4.x
#            driver/kernel-2.6 for kernel source version 2.5.x
#
DRIVER_SRC_DIR := driver/kernel-$(shell ./kernelver.sh VERSION ${KERNEL_SRC_DIR}).$(shell expr $(KERNEL_SRC_PATCHLEVEL) + $(DEVKERNEL))

export PRJ_PWD = $(PWD)

# Uncomment this to add debugging information to programs
#
#DEBUG=1

SUBDIRS = lib examples

# utilities for the superuser
SUTILS = seaio-load seaio-unload
export SUTILDIR = /usr/local/sbin
# standard user utilities go here
export UTILDIR = /usr/local/bin
# Libraries go here
export LIBDIR = /usr/local/lib
# these config files are needed for seaio-load and seaio-unload to work properly
CONFIGFILES = seaio.conf
export CONFIGDIR = /etc


CFLAGS := -Wall -Wstrict-prototypes -fomit-frame-pointer 
ifdef DEBUG
CFLAGS += -g -O 
else
CFLAGS += -O2
endif

all_targets all: subdirs

subdirs:
	for n in $(SUBDIRS); do $(MAKE) -C $$n all || exit 1; done

# build drivers outside of the kernel source
drivers:
	cd $(DRIVER_SRC_DIR)
	make -C $(KERNEL_SRC_DIR) SUBDIRS=$(PWD)/$(DRIVER_SRC_DIR) modules

# Choose correct destination for compiled drivers
# based on the kernel source version
#
ifeq ($(shell expr "$(KERNEL_SRC_VERSION_PATCH)" ">" "2.2"), 1)
DRIVER_INSTALL_DIR := /lib/modules/$(KERNEL_SRC_VERSION)/kernel/drivers/misc
else
DRIVER_INSTALL_DIR := /lib/modules/$(KERNEL_SRC_VERSION)/misc
endif

# module named end with .o until kernel version 2.5 which uses .ko
#
ifeq ($(shell expr "$(KERNEL_SRC_VERSION_PATCH)" ">" "2.4"), 1)
MODULE_SUFFIX := .ko
else
MODULE_SUFFIX := .o
endif

DIO_DRIVERS := $(addprefix $(DRIVER_SRC_DIR)/, $(DIO_DRIVERS))
DIO_DRIVERS := $(addsuffix $(MODULE_SUFFIX), $(DIO_DRIVERS))


############################################################
#
#
install: subdirs
	@echo Making install in subdirectories ...
	for n in $(SUBDIRS); do $(MAKE) -C $$n install || exit 1; done
	@echo Copying utilities to $(SUTILDIR)
	for n in $(SUTILS); do cp $$n $(SUTILDIR) || exit 1; done
	@echo Copying config files to $(CONFIGDIR)
	for n in $(CONFIGFILES); do test -f /etc/$$n || cp $$n $(CONFIGDIR); done
	@echo Installation of the SeaIO suite is complete!

############################################################
#
# Install drivers built outside of the kernel source tree
# to the appropriate destination directory.
#
# Run depmod to calculate module dependencies if a
# System.map file is available.
#
#
driver_install : driver

	@if [ ! -d $(DRIVER_INSTALL_DIR) ]; then \
		echo ' ** ' directory "$(DRIVER_INSTALL_DIR) does not exist"; \
		exit -1; \
	fi

	@for f in $(DIO_DRIVERS) ; do \
		if [ -f $$f ] ; then \
	    		mkdir -p   $(DRIVER_INSTALL_DIR) ; \
	    		cp -av $$f $(DRIVER_INSTALL_DIR) ; \
		fi \
	done;

	@if [ -f $(KERNEL_SRC_DIR)/System.map ] ; then \
		echo Running depmod with $(KERNEL_SRC_DIR)/System.map ; \
		/sbin/depmod -ae -F $(KERNEL_SRC_DIR)/System.map $(KERNEL_SRC_VERSION) ; \
	else \
		if [ -f /boot/System.map-$(KERNEL_SRC_VERSION) ] ; then \
			echo Running depmod with /boot/System.map-$(KERNEL_SRC_VERSION) ; \
			/sbin/depmod -ae -F /boot/System.map-$(KERNEL_SRC_VERSION) $(KERNEL_SRC_VERSION) ; \
		else \
			echo Cant find System.map, depmod not run ; \
		fi \
	fi


# Remove all generated files and backup files
#
CLEAN_TARGETS = *~ core *.o *.ko *.mod.c *.mod.o .*.o.flags .*o.cmd
clean:
	@for f in $(CLEAN_TARGETS) ; do \
		find . -name "$$f" | xargs rm -f ; \
	done;
	@find . -type l | xargs rm -f
	for n in $(SUBDIRS); do $(MAKE) -C $$n clean; done

cvsclean:
	@find . -name CVS -type d | xargs rm -Rf
