prefix = /usr/local
exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
include= ${prefix}/include
libdir = ${exec_prefix}/lib

INSTALL= /usr/bin/install -c
KSRC?=/lib/modules/3.5.0-54-generic/build

#####################################################################################
# Check kernel version. 2.4 is built outside kernel, 2.6 uses kernel build system
ifeq ($(shell uname -r | cut -d . -f 1-2),2.4)
#2.4.x

vpath %.c .:../../lib
vpath %.c .:../common

KCFLAGS= -Wall -Wimplicit -Wformat -O2 -DNDEBUG -DHAVE_CONFIG_H -pipe
KERNELCONFIG=/lib/modules/3.5.0-54-generic/build/.config

DRV_GCC=gcc

#
# Optional defines:
#	-DDAGMEM_SIZE=<new_size_in_bytes>	default: 32MB
#
include $(KERNELCONFIG)

INCLUDE= -I$(KSRC)/include -I/usr/share/tcc/endace-dag/include -I/usr/share/tcc/endace-dag/drv/common/ -I/usr/share/tcc/endace-dag/drv/linux

ifdef CONFIG_SMP
	CFLAGS += -D__SMP__ -DSMP
endif

# Voyager subarch support
mflags-$(CONFIG_X86_VOYAGER)    := -I$(KSRC)/include/asm-i386/mach-voyager

# VISWS subarch support
mflags-$(CONFIG_X86_VISWS)      := -I$(KSRC)/include/asm-i386/mach-visws

# NUMAQ subarch support
mflags-$(CONFIG_X86_NUMAQ)      := -I$(KSRC)/include/asm-i386/mach-numaq

# BIGSMP subarch support
mflags-$(CONFIG_X86_BIGSMP)     := -I$(KSRC)/include/asm-i386/mach-bigsmp

#Summit subarch support
mflags-$(CONFIG_X86_SUMMIT) := -I$(KSRC)/include/asm-i386/mach-summit

# generic subarchitecture
mflags-$(CONFIG_X86_GENERICARCH) := -I$(KSRC)/include/asm-i386/mach-generic

# ES7000 subarch support
mflags-$(CONFIG_X86_ES7000)     := -I$(KSRC)/include/asm-i386/mach-es7000

# default subarch .h files
mflags-y += -I$(KSRC)/include/asm-i386/mach-default

CFLAGS += $(mflags-y)

CFLAGS += $(INCLUDE) -D__KERNEL__ -DMODULE -fomit-frame-pointer
CFLAGS += -DDIAGNOSTIC
CFLAGS += $(DEBUGINC)
CFLAGS += $(KCFLAGS)

ACFILE = $(KSRC)/include/linux/autoconf.h
# Detect modversions support
ifneq ($(strip $(shell grep 'define CONFIG_MODVERSIONS 1' $(ACFILE))),)
CFLAGS += -DMODVERSIONS -include $(KSRC)/include/linux/modversions.h
endif

CC = ${DRV_GCC}

CSRC  = $(wildcard *.c)
DAGOBJ= dag_main.o dagpci.o dagduck.o dagmon.o dagreg.o dagversion.o
MEMOBJ= dagmem_main.o dagpci.o
DRV   = dag dagmem

.PHONY: all clean distclean install depend

all: $(DRV)

install: install-drv

depend: .depend

install-drv: $(DRV)
	$(INSTALL) -d $(DESTDIR)/lib/modules/`uname -r`/endace
	$(INSTALL) $(DRV) $(DESTDIR)/lib/modules/`uname -r`/endace
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) dagload $(DESTDIR)$(bindir)
ifeq ($(DESTDIR),)
	depmod -aeu > /dev/null
endif
	@echo '====>>>' Dont forget to update /etc/modules

dag: $(DAGOBJ)
	$(LD) -r $(DAGOBJ) -o dag

dagmem: $(MEMOBJ)
	$(LD) -r $(MEMOBJ) -o dagmem

.depend: $(CSRC) Makefile
	@echo -n "rebuilding .depend ... "
	@$(CC) -M -MG $(CFLAGS) $(CSRC) | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e 's%/usr/[a-zA-Z0-9\._/-]*%%g' -e 's/  */ /g' >.depend
	@echo "done"

ifneq ($(MAKECMDGOALS),depend)
include .depend
endif

else
####################################################################################
# assume 2.6 or 3.0 kernel compile

ifneq ($(KERNELRELEASE),)
# Called by kbuild

obj-m += dagmem.o dag.o

ifneq ($(CONFIG_64BIT),y)
obj-m+= divdi3.o
divdi3-objs = \
	./../divdi3/divdi3_exp.o \
	./../divdi3/divdi3_impl.o
endif

dagmem-objs = \
	./dagmem_main.o \
	./dagpcimem.o
dag-objs = \
	./dag_main.o \
	./dagpcidag.o \
	./dagreg.o \
	./dagversion.o \
	./dagduck.o \
	./dagmon.o

EXTRA_CFLAGS += -I/usr/share/tcc/endace-dag/include/ -I/usr/share/tcc/endace-dag/include/ -I/usr/share/tcc/endace-dag/drv/common/ -I/usr/share/tcc/endace-dag/drv/linux

else

PWD  := $(shell pwd)

default: links
	@echo '    Building DAG drivers for Linux kernel.'
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules

install:
	@echo '    Installing DAG drivers for Linux kernel.'
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) INSTALL_MOD_PATH=$(DESTDIR) modules_install
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) dagload $(DESTDIR)$(bindir)
ifeq ($(DESTDIR),)
	depmod -aeu > /dev/null
endif

endif  # End kbuild check

CSRC  = $(wildcard *.c)

.PHONY: all clean distclean install

all: default

depend:

links:
	@echo '    Creating links.'
	if ! test -e dagreg.c ; then ln -s ../../lib/dagreg.c dagreg.c ; fi
	if ! test -e dagpcidag.c ; then ln -s ../../lib/dagpci.c dagpcidag.c ; fi
	if ! test -e dagpcimem.c ; then ln -s ../../lib/dagpci.c dagpcimem.c ; fi
	if ! test -e dagduck.c ; then ln -s ../common/dagduck.c dagduck.c ; fi
	if ! test -e dagduck.h ; then ln -s ../common/dagduck.h dagduck.h ; fi
	if ! test -e dagmon.c ; then ln -s ../common/dagmon.c dagmon.c ; fi
	if ! test -e dagmon.h ; then ln -s ../common/dagmon.h dagmon.h ; fi

endif # kernel version check

####################################################################################
#common targets

distdir:
	cp dagload dag_main.c dagmem_main.c dagdrv.h dagmem.h dagcompat.h dagdebug.h dagversion.c Makefile.in $(distdir)

clean:
	$(RM) *.o *.ko $(DRV)

distclean: clean
	$(RM) -rf dagreg.c dagpcimem.c  dagpcidag.c dagmon.* dagduck.* .depend *.mod.c .dag* Module.* modules.order Makefile .tmp_versions 

.PHONY: dvi pdf ps info html tags ctags
dvi pdf ps info html tags ctags:
