ifndef KPVER
KPVER := $(shell uname -r)
endif
TKPVER26 := $(findstring 2.6,$(KPVER))
#TKPVER26 := $(strip $(shell echo $KPVER|cut -c1-3|grep "2\.[6789]"))
PWD=`pwd`
ifndef MST_CPU
MST_CPU   := $(shell uname -m)
endif

#mst_devasys.o
ifneq (${MST_CPU},ppc_8xx)
all: mst_pci.o mst_pciconf.o
endif

all: libmtcr.a \
     minit mstop mread mwrite mtserver mremote mdevices

ifeq (${TKPVER26},)
all: mst_calbr.o mod_i2c.o
.IGNORE: mst_calbr.o mod_i2c.o
endif

ifneq (${MST_NO_USB},1)
all: buildusb
#all: ezusb
endif

ifeq (${MST_CPU},ppc_8xx)
VPATH += ppc_utils/
all: mst_ppc.o mppctest i2c_8xx
 ifeq (${KPVER},2.4.18)
 all: i2c-8xx.o         # New kernels have buildin CPM i2c driver 
 endif    
MST_DISTFILES += ppc_utils/mppctest.c
else 
all: dimax_init mtusb-1_test
endif


#all: c-mif-gen.o


#all: usbioctl
SMP = $(shell if uname -a | grep SMP > /dev/null; then echo -D__SMP__; fi)
export SMP

#Old pci way is to check size by writing to BARs.
# This is best avoided if possible.
ifeq (${MST_CPU},ppc_8xx)
OLD_PCI_DEF = $(shell if [ "`uname -m`" = "ppc" ]; then echo -DOLD_PCI_METHOD; else echo ""; fi)
export OLD_PCI_DEF
endif

MODFL += $(SMP) $(OLD_PCI_DEF)
mst_calbr.o: MODFL += -I.
mod_i2c.o:   MODFL +=-I.  -DEXPORT_SYMTAB

prefix = ${ROOTDIR}/usr/mst

CC    = $(CROSS_COMPILE)gcc
LD    = $(CROSS_COMPILE)ld
AR    = $(CROSS_COMPILE)ar

#changes for kernel 2.6+
MODFL += -fno-strict-aliasing -I/lib/modules/$(KPVER)/build/include/asm/mach-default/

ifdef USE_IB_MGT
#
### Build with IB_MGT library
CFLAGS += -DMST_CPU_$(MST_CPU) -DUSE_IB_MGT -DMIB_DEBUG -MD -g -O2 -Wall -Werror -I. -I/usr/mellanox/include
LDFLAGS += -lmtl_common -lmosal -lpthread -lib_mgt -L/usr/mellanox/lib
#
else
#
### Regular build
CFLAGS += -DMST_CPU_$(MST_CPU) -MD -g -O2 -Wall -Werror -I.
#
endif

ifdef PLATINC
CFLAGS += -I${PLATINC}
endif

CFLAGS+=-idirafter compat
MODFL+=-idirafter compat

ifeq ($(MST_CPU),x86_64)
CFLAGS += -fPIC
MODFL += -mcmodel=kernel
MODFL += -mno-red-zone
endif

ifeq (${MST_CPU},ppc_8xx)
CFLAGS += -DENABLE_MST_DEV_I2C
endif

MODFL += -MD -g -D__KERNEL__ -DMODULE -D__LINUX__ -DMST_CPU_${MST_CPU} \
	-Wall -Wstrict-prototypes                                  \
	-I$(KINCPATH)                                                \
	-fomit-frame-pointer -fno-strength-reduce   \
	-pipe  -O2 -Wno-unused

#special case removes -Werror due to unused vars in system includes in ia64, RH AS 3.0 2.4.21-4.EL
ifneq (ia64,${MST_CPU})
  MODFL += -Werror
else
  ifneq "$(shell cat /etc/redhat-release 2> /dev/null | grep Taroon | sed -e 's/.*Taroon/Taroon/' | sed -e 's/Taroon.*/Taroon/')" "Taroon"
    MODFL += -Werror
  endif 
endif

ifndef KINCPATH
KINCPATH1 = /lib/modules/$(KPVER)/build/include
KINCPATH2 = /usr/src/linux/include
KINCPATH3 = /usr/src/linux-$(KPVER)/include

KINCPATH = $(firstword $(foreach path, ${KINCPATH0} ${KINCPATH1} ${KINCPATH2} ${KINCPATH3},$(shell if test -e "${path}"; then echo "${path}"; fi)))


ifeq ($(strip $(KINCPATH)),)
define err_message
 Linux kernels source headers which are required for MST installation
 were not detected in any of the following directories:
 ${KINCPATH0}
 ${KINCPATH1}
 ${KINCPATH2}
 ${KINCPATH3}

 Please install the kernel source headers in one of these directories
 or point the environment variable KINCPATH0 to the directory
 where these headers can be found.

 Kernel source for RedHat 7.2 can be found in the following RPM
 on RedHat CDROM 2:
    /mnt/cdrom/RedHat/RPMS/kernel-source-2.4.7-10.i386.rpm

endef

$(error $(err_message))
endif
endif


ifndef BINDKERNEL
mod_versions_exists=$(shell if test -e $(KINCPATH)/linux/modversions.h ; then echo 1; fi)
ifneq ($(mod_versions_exists),)
MODFL += -DMODVERSIONS -include $(KINCPATH)/linux/modversions.h
export MODVERSIONS = -DMODVERSIONS
endif
endif

rhconfig_exists=$(shell if test -e $(KINCPATH)/linux/rhconfig.h ; then echo 1; fi)
ifneq ($(rhconfig_exists),)
MODFL += -include $(KINCPATH)/linux/rhconfig.h
endif

MODLD += -r

#Hack for Eli's old I2C module
mod_%.o:	mod_%.c
		$(CC) -DKBUILD_MODNAME=$(@:.o=) -c $(MODFL) -o $@ $<

#mst_info_mod.o:	mst_info_mod.c
#		$(CC) -DKBUILD_MODNAME=$(@:_mod.o=) -c $(MODFL) -o $@ $<

ifeq (${TKPVER26},)
%_mod.o:	%_mod.c
		$(CC) -DKBUILD_MODNAME=$(@:_mod.o=) -c $(MODFL) -o $@ $<
endif

%.o:		%.c
		$(CC) $(CFLAGS) -c -o $@ $<

ifeq (${TKPVER26},)
%.o:	%_mod.o driver_common.c
	$(LD) $(MODLD) -o $@ $<
else
%.o:	%_mod.c driver_common.c
	make -C $(dir $(KINCPATH)) SUBDIRS=${PWD}/build2.6 mst_module:=$@ modules
	cp -f build2.6/$(@:.o=.ko) $@
endif

m%: m%.o
	$(CC) $(LDFLAGS) -o $@ $^

lib%.a: %.o
	$(AR) cr $@ $^

mtcr.o: usbdescriptor.h
dimax_init.o: usbdescriptor.h

libmtcr.a: mtcr.o mtcr_ib.o mtcr_i2c.o usbioctl.o
libmtcr.a dimax_init: CFLAGS += -Wno-error

mremote: mremote.o tcp.o

mread: mread.o libmtcr.a

mwrite:	mwrite.o libmtcr.a

mdevices: mdevices.o libmtcr.a

mtusb-1_test: mtusb-1_test.o libmtcr.a
	$(CC) $(LDFLAGS) -o $@ $^

mtserver: mtserver.o tcp.o libmtcr.a

ifeq (${MST_CPU},ppc_8xx)
mppctest: mppctest.o libmtcr.a
endif

buildusb: ./libusb-0.1.8/tests/testlibusb ./fxload-2002_04_11/fxload

./libusb-0.1.8/tests/testlibusb:
	-(cd libusb-0.1.8; sh ./configure --prefix=${prefix}; make)

./fxload-2002_04_11/fxload:
	-(make -C fxload-2002_04_11 prefix=${prefix})

ezusb:
	-(cd Ezusb2131; ${MAKE} INCLUDEDIR=${KINCPATH})

#usbioctl: CFLAGS+=-DMAIN
#usbioctl: ioctl.c

%: %.c
	$(CC) $(CFLAGS) -o $@ $<
%: %.o
	$(LD) $(LDFLAGS) -o $@ $<

clean:
	rm -f *.o *.a *.d Ezusb2131/*.o minit mread mwrite mstop usbioctl mremote mtserver mdevices drvinstall dimax_init mtusb-1_test
	rm -rf *.ko build2.6/*.ko build2.6/*.o build2.6/*.o.cmd build2.6/.*.*o.cmd build2.6/.tmp_versions/ build2.6/*.c
ifneq (${MST_CPU},ppc_8xx)
	make -C libusb-0.1.8 clean
	make -C fxload-2002_04_11 clean
	#rm -f $(filter-out Makefile CVS,$(shell ls build2.6))
endif
ifneq ($(strip $(wildcard *.d)),)
-include *.d
endif

print:
	@echo '${${VAR}}'


install:
	mkdir -p ${prefix}/lib/${KPVER}
	mkdir -p ${prefix}/bin
	mkdir -p ${prefix}/include
ifneq (${MST_CPU},ppc_8xx)
	install -m 644 mst_pci.o mst_pciconf.o \
	           ${prefix}/lib/${KPVER}
	if test -e ./mst_calbr.o ; then \
		install -m 644 mst_calbr.o ${prefix}/lib/${KPVER}; \
	fi
	if test -e ./mod_i2c.o ; then \
		install -m 644 mod_i2c.o ${prefix}/lib/${KPVER}; \
	fi
	mkdir -p ${prefix}/etc
	install -m 644 ezusb_fw/0abf03e8_6.hex ${prefix}/etc
	mkdir -p ${prefix}/bin
	install -m 755 minit dimax_init mtusb-1_test mstop mread mwrite mremote mtserver ${prefix}/bin
	install -m 644 mtcr.h mtcr_ib_private.h ${prefix}/include
	install -m 644 libmtcr.a ${prefix}/lib
	rm -f ${ROOTDIR}/etc/init.d/mst
	install -m 755 mst ${ROOTDIR}/etc/init.d
#	install  mst_info.o ${prefix}/lib/${KPVER}
else
 ifeq (${KPVER},2.4.18)
	install -m 644 -D i2c-8xx.o ${prefix}/lib/${KPVER}/i2c-8xx.o
 endif
	install -m 644 -D mst_ppc.o ${prefix}/lib/${KPVER}/mst_ppc.o
	install -m 755 minit mstop mread mwrite mremote mtserver mdevices ${prefix}/bin
	install -m 644 mtcr.h mtcr_ib_private.h ${prefix}/include
	install -m 644 libmtcr.a ${prefix}/lib
	rm -f ${ROOTDIR}/etc/init.d/mst
	install -m 755 mst ${ROOTDIR}/etc/init.d
endif
	rm -f ${ROOTDIR}/usr/bin/mst
	ln -s ${ROOTDIR}/etc/init.d/mst ${ROOTDIR}/usr/bin/mst
	touch ${ROOTDIR}/etc/mst.conf
ifneq (${MST_CPU},ppc_8xx)
	if test -e ./libusb-0.1.8/tests/.libs/testlibusb; then \
	  make -C libusb-0.1.8 install; \
	fi
	if test -e ./libusb-0.1.8/tests/.libs/testlibusb; then \
	  mkdir -p ${prefix}/bin; \
	  install -m 755 ./libusb-0.1.8/tests/.libs/testlibusb ${prefix}/bin/listusb; \
	fi
	if test -e ./fxload-2002_04_11/fxload; then \
	  make -C fxload-2002_04_11 prefix=${prefix} install; \
	fi
endif



makedist: dist
makedist: MST_CPU_VALUE=$(MST_CPU)
makedist: MST_NO_USB_VALUE=$(MST_NO_USB)

dist:
	cat install.sh | sed -e 's/MST_CPU_VALUE/$(MST_CPU_VALUE)/' | sed -e 's/MST_NO_USB_VALUE/$(MST_NO_USB_VALUE)/' | sed -e 's/BUILD_ID_VALUE/$(BUILD_ID)/' > drvinstall
	shar -V *.c *.h mst Makefile \
		   ${MST_DISTFILES}       \
       compat/linux/usb_ch9.h \
       compat/linux/syscalls.h \
       compat/asm/ioctl32.h \
       ezusb_fw/0abf03e8_6.hex \
       libusb-0.1.8/AUTHORS\
       libusb-0.1.8/COPYING\
       libusb-0.1.8/ChangeLog\
       libusb-0.1.8/INSTALL.libusb\
       libusb-0.1.8/INSTALL.libusb.in\
       libusb-0.1.8/LICENSE\
       libusb-0.1.8/Makefile.am\
       libusb-0.1.8/Makefile.in\
       libusb-0.1.8/NEWS\
       libusb-0.1.8/README\
       libusb-0.1.8/README.in\
       libusb-0.1.8/aclocal.m4\
       libusb-0.1.8/bsd.c\
       libusb-0.1.8/config.guess\
       libusb-0.1.8/config.h.in\
       libusb-0.1.8/config.sub\
       libusb-0.1.8/configure\
       libusb-0.1.8/configure.in\
       libusb-0.1.8/darwin.c\
       libusb-0.1.8/depcomp\
       libusb-0.1.8/descriptors.c\
       libusb-0.1.8/error.c\
       libusb-0.1.8/error.h\
       libusb-0.1.8/install-sh\
       libusb-0.1.8/libusb-config.in\
       libusb-0.1.8/libusb.spec\
       libusb-0.1.8/libusb.spec.in\
       libusb-0.1.8/linux.c\
       libusb-0.1.8/linux.h\
       libusb-0.1.8/ltmain.sh\
       libusb-0.1.8/missing\
       libusb-0.1.8/mkinstalldirs\
       libusb-0.1.8/usb.c\
       libusb-0.1.8/usb.h.in\
       libusb-0.1.8/usbi.h\
       libusb-0.1.8/doc/Makefile.am\
       libusb-0.1.8/doc/Makefile.in\
       libusb-0.1.8/doc/api.sgml\
       libusb-0.1.8/doc/examples.sgml\
       libusb-0.1.8/doc/functions.sgml\
       libusb-0.1.8/doc/intro.sgml\
       libusb-0.1.8/doc/manual.sgml\
       libusb-0.1.8/doc/website.dsl\
       libusb-0.1.8/tests/Makefile.am\
       libusb-0.1.8/tests/Makefile.in\
       libusb-0.1.8/tests/testlibusb.c\
       fxload-2002_04_11/COPYING\
       fxload-2002_04_11/*.c\
       fxload-2002_04_11/*.h\
       fxload-2002_04_11/Makefile\
       fxload-2002_04_11/README.txt\
       fxload-2002_04_11/fxload.8\
       fxload-2002_04_11/a3load.hex\
                Device-mtcr/Changes \
                Device-mtcr/LICENSE \
                Device-mtcr/MANIFEST \
                Device-mtcr/Makefile.PL \
                Device-mtcr/README \
                Device-mtcr/mtcr.h \
                Device-mtcr/mtcr.pm \
                Device-mtcr/mtcr.xs \
                Device-mtcr/test.pl \
                build2.6/Makefile \
         >> drvinstall
	chmod 755 drvinstall

#dist makedist:
#	cat install.sh | sed -e 's/MST_CPU_VALUE/$(MST_CPU)/' | sed -e 's/MST_NO_USB_VALUE/$(MST_NO_USB)/' > drvinstall
#	shar -V *.c *.h mst Makefile \
#	     Ezusb2131/Makefile Ezusb2131/ezusb2131.c \
#	     Ezusb2131/Examples/*.hex \
#                Device-mtcr/Changes \
#                Device-mtcr/LICENSE \
#                Device-mtcr/MANIFEST \
#                Device-mtcr/Makefile.PL \
#                Device-mtcr/README \
#                Device-mtcr/mtcr.h \
#                Device-mtcr/mtcr.pm \
#                Device-mtcr/mtcr.xs \
#                Device-mtcr/test.pl \
#                build2.6/Makefile \
#         >> drvinstall
#	chmod 755 drvinstall
