# ---------------------------------------------------------------------------
# (C) Copyright 2012 Roesch & Walter Industrie-Elektronik GmbH, Germany
#     support@rw-gmbh.de
# ---------------------------------------------------------------------------
#
# Simple Makefile for generation of:
# mcug3.[o, ko] loadable kernel module driver, linux 2.4.x
#
# Changelog:
#	2005-10-21 shrinked for linux-2.4 only support 
#	2005-07-19 KAPI module support added
#		   install target modified
#	2005-06-06 2.4 kernel added CFLAGS options:
#		-O2 -fomit-frame-pointer -finline-functions
#	2005-01-25 added support for 2.6 kernel version
#	2004-01-23 added dependency stuff
#	2004-01-13 updated for build on i386
#	2003-10-28 imported and updated for cygwin cross compiling
# 
##################################################################
# !!!!! please set up KERNELDIR and INSTALLDIR !!!!!
##################################################################

drive_u=/pub
#drive_u=u:

INSTALLDIR ?= /lib/modules/$(shell uname -r)/kernel/drivers/char

SRCS = mcug3c.c mcug3cfuncs.c mcug3i.c mcug3mmap.c

CONFIG_G3_KAPI=n
CONFIG_MCE=n

ifeq ("$(CONFIG_G3_KAPI)", "y")
SRCS		+= mcug3kapi.c 
EXTRA_CFLAGS	+= -DCONFIG_G3_KAPI -mhard-float
endif

ifeq ("$(CONFIG_MCE)", "y")
SRCS		+= mce.c 
EXTRA_CFLAGS	+= -DCONFIG_MCE -DOFFSETHEAPTOPPMON=128
endif


# Filename of kernel module
MODULE = mcug3.o

# uncomment when not cross compiling
CROSS_COMPILE=mipsel-linux-

CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
#KERNELDIR = $(drive_u)/build/linux-mips/work-2.4.35.2-rthal5g
KERNELDIR ?= $(drive_u)/build/linux-mips/addi-data/linux-2.4.22-released-rev56-work
DEPENDFILE = .depend

#RNWNIC = CONFIG_G3_NETWORK

# autofind kernel version
KERNELVERSION := $(shell grep '[\t ]UTS_RELEASE[\t ][\t ]*"' \
	$(KERNELDIR)/include/linux/version.h  | \
	sed -e 's;[^"]*"\(.*\)";\1;g' )

LINKFLAGS = -r

# Compiler Flags for mipsel linux kernel module (gcc-3.3)
KMOD_APP_CFLAGS_GCC_3_3 = -D__KERNEL__ -I$(KERNELDIR)/include -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common -fomit-frame-pointer -G 0 -mno-abicalls -fno-pic -pipe -finline-limit=100000 -g -mabi=32 -march=r4600 -Wa,-32 -Wa,-march=r4600 -Wa,-mips3 -Wa,--trap -DMODULE -mlong-calls -DEXPORT_SYMTAB -Wall

# Compiler Flags for mipsel linux kernel module (gcc-2.95.4)
KMOD_APP_CFLAGS_GCC_2_95_4 = -D__KERNEL__ -DMODULE -I. -I$(KERNELDIR)/include -Wall -Wstrict-prototypes -DEXPORT_SYMTAB -Wno-trigraphs -O2 -fomit-frame-pointer -fno-strict-aliasing -fno-common -G 0 -mno-abicalls -fno-pic -Wa,--trap -pipe -mlong-calls -Wall -Wstrict-prototypes -pipe  -march=r4600 -Wa,-32 -Wa,-march=r4600 -Wa,-mips3 


CFLAGS = $(KMOD_APP_CFLAGS_GCC_2_95_4)

# where to install module
INSTALLDIR ?= /opt/cantasticnfs/lib/modules/${KERNELVERSION}/mcug3

ifdef RNWNIC
SRCS    += rnwnic.c
CFLAGS += -D$(RNWNIC)
endif

CFLAGS += $(EXTRA_CFLAGS)

OBJ     = $(filter %.o,$(patsubst %.c,%.o,$(patsubst %.sx,%.o,$(SRCS)))) 

export-objs = mcug3cfuncs.o 

# the kernel module is named mcug3.o
all: $(DEPENDFILE) $(MODULE) 

$(MODULE): $(OBJ)
	$(LD) $(LINKFLAGS) $^ -o $@
	chmod go+r $@

clean:
	rm -f *.o *.ko .*.cmd *~ core $(DEPENDFILE)

$(DEPENDFILE): $(SRCS)
	$(CC) -MM $(SRCS) $(CFLAGS) > $(DEPENDFILE)

ifeq ($(DEPENDFILE),$(wildcard .depend))
include $(DEPENDFILE)
endif                        

install: all
	mkdir -p $(INSTALLDIR)/lib/modules/${KERNELVERSION}/mcug3 >/dev/null 2>&1
	cp  $(MODULE) $(INSTALLDIR)/lib/modules/${KERNELVERSION}/mcug3 --archive

# End of Makefile

