
KSRCDIR = /lib/modules/$(shell uname -r)
ARCH = $(shell uname -m)
CUR_DIR = $(shell pwd)
CLEANUPDIRS = $(CUR_DIR) $(CUR_DIR)/arch/i386 $(CUR_DIR)/arch/ia64

ifeq ($(shell grep synchronize_rcu /proc/kallsyms 2> /dev/null),)
 USE_SYNC_RCU = 
else
 USE_SYNC_RCU = -DUSE_SYNC_RCU
endif

ifeq ($(ARCH),ia64)
 PROGS = kstrax_buf.ko arch/ia64/ctr_mod.ko arch/ia64/kstrax_stub.ko
else
 PROGS = kstrax_buf.ko arch/i386/ctr_mod.ko 
endif

#if KERNELRELEASE is defined, we've been invoked from the
#kernel build system and can use its language.

ifneq ($(KERNELRELEASE),)

ifeq ($(ARCH),ia64)
 CTR= arch/ia64
 EXTRA_CFLAGS = -D_IA64_KSTRAX_ \
		-I$(M)/$(CTR)/ \
		-I$(M)/ \
		$(USE_SYNC_RCU)

	obj-m := kstrax_buf.o
	obj-m += $(CTR)/
	kstrax_buf-y := mcontrol.o mbuffer.o
	clean-files := *.o *.ko *.mod.[co]

else
 CTR = arch/i386
 EXTRA_CFLAGS = -I$(M)/$(CTR) \
		-I$(M)/ \
		$(USE_SYNC_RCU)

	obj-m := kstrax_buf.o
	obj-m += $(CTR)/
	kstrax_buf-y := mcontrol.o mbuffer.o
	clean-files := *.o *.ko *.mod.[co]
endif

#Otherwise we were called directly from the command
#line; invoke the kernel build system.

else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

install: all
	mkdir $(KSRCDIR)/kernel/drivers/kstrax/; \
	cp $(PROGS) $(KSRCDIR)/kernel/drivers/kstrax; \
	/sbin/depmod -a;

uninstall:
	rm -r -f $(KSRCDIR)/kernel/drivers/kstrax/; \
	/sbin/depmod -a;

distclean: clean
	@(for dir in $(CLEANUPDIRS) ; do \
		(cd $$dir ; \
		rm -f *~ ; \
		cd $(CUR_DIR) ); \
	done )

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

endif

