# ---------------------------------------------------------------------------
# (C) Copyright 2005-2009 Roesch & Walter Industrie-Elektronik GmbH, Germany
#     support@rw-gmbh.de
# ---------------------------------------------------------------------------
#
# Simple Makefile for generation of:
# kapisample01.[o, ko] loadable kernel module for testing MCUG3 KAPI interface
#
# Changelog:
#	2009-10-29 extra_symbols target added
#	2005-10-04 created 
# 
##################################################################
# !!!!! please set up KERNELDIR !!!!!
##################################################################

SRCS = kapisample01.c 

# CONFIG_G3_KAPI should always be "y"
CONFIG_G3_KAPI=y

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

KERNELDIR = /lib/modules/$(shell uname -r)/build
# KERNELDIR=/usr/src/linux

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

# Get the "x" in kernel 2.x.y (major version e.g. [4,6])
KERNEL_MAJOR_VERSION:=$(shell echo $(KERNELVERSION) | cut -d. -f2)

ifeq ("$(KERNEL_MAJOR_VERSION)","4")
##################################################################
#                    Makesupport for kernel 2.4                  #
##################################################################

# Filename of kernel module
MODULE = kapisample.o

# setup prefix for crosscompiler
CROSS_COMPILE=mipsel-linux-
# uncomment when not cross compiling
# CROSS_COMPILE=

CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
DEPENDFILE = .depend

CFLAGS += -D__KERNEL__ -DMODULE -I. -I$(KERNELDIR)/include
CFLAGS += -Wall -Wstrict-prototypes -DEXPORT_SYMTAB

LINKFLAGS = -r

ifeq (${CROSS_COMPILE}, mipsel-linux-)
	# Additional compiler flags for mipsel linux kernel module
	CFLAGS += -Wno-trigraphs -O2 -fomit-frame-pointer -fno-strict-aliasing -fno-common
	CFLAGS += -G 0 -mno-abicalls -fno-pic -Wa,--trap -pipe -mlong-calls
	CFLAGS += -Wall -Wstrict-prototypes -pipe 
	CFLAGS += -march=r4600 -Wa,-32 -Wa,-march=r4600 -Wa,-mips3    
	# CFLAGS += -mcpu=r4600 -mips2
	# CFLAGS += -finline-limit=100000 -mabi=32 
	SRCS += fpu.S
else
	CFLAGS += -O2 -fomit-frame-pointer -finline-functions
endif

CFLAGS += $(EXTRA_CFLAGS)

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

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

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

fpu.o: fpu.S
	$(CC) $(CFLAGS) -D__ASSEMBLY__ -c $< -o $@

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

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

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

else
##################################################################
#                    Makesupport for kernel 2.6                  #
##################################################################

MODULE:=kapisample01.ko

OBJS :=  $(SRCS:%.c=%.o)  

obj-m := kapisample01.o

mcug3-objs := $(OBJS)

KDIR        := /lib/modules/$(shell uname -r)/build
LDIR	    := /lib/modules/$(shell uname -r)
PWD         := $(shell pwd)

all default: extra_symbols
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

extra_symbols:
	# Copy symbol versioning informations from mcug3lkm directory.
	# Module.symvers contains all exported k_x functions.
	# This action prevents the modpost undefined symbol warnings.
	cp ../mcug3lkm/Module.symvers .

clean:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean

install modules_install:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install

.PHONY: clean install modules_install extra_symbols

endif

# End of Makefile

