#------------------------------------------------------------------------------
# Copyright (C) Delta Tau Data Systems Inc., 2007
# All rights reserved.
#
# Generic makefile for any c realtime C plc 0, user servo or user phase
# For a new project change the following
#
# 1.) usralgo-objs should be assigned the 'C' source code files that need to be compiled
# 2.) issue the command 'make depend' the first time a project is created and
#     (every time an additional 'C' file is added to the project the command
#     'make depend' must be issued)
# 3.) issue the command make clean
# 4.) issue the command make
#
# Notes
# --------
# Change DTDEBUG above to -O2 for release w/ optimization
# Change DTDEBUG above to -g3 for debug
# arm,i386,i385hv,ppc460-2,ppc460-1,ppc405
#------------------------------------------------------------------------------

PPMAC=SAR-CPPM-EXPMX1
#PPMAC=SAROP11-CPPM-MOT6871

PMAC_ARCH=ppc465-2

ifeq ($(PMAC_ARCH),ppc465-2)
ARCH=powerpc
#CROSS_COMPILE=powerpc-meau-linux-gnu-
CROSS_COMPILE=/opt/eldk-4.2/usr/bin/ppc_4xxFP-
KDIR=/opt/powerpc-465-rootfs/usr/src/linux-3.2.21-serengeti-smp
KSRC=/opt/powerpc-465-rootfs/usr/src/linux-3.2.21-serengeti-smp
#CC=powerpc-meau-linux-gnu-gcc
#AS=powerpc-meau-linux-gnu-as
#LD=powerpc-meau-linux-gnu-gcc
CC=/opt/eldk-4.2/usr/bin/ppc_4xxFP-gcc
AS=/opt/eldk-4.2/usr/bin/ppc_4xxFP-gcc
LD=/opt/eldk-4.2/usr/bin/ppc_4xxFP-gcc
#STRIP=i686-meau-linux-gnu-strip
INCLUDE=/opt/powerpc-465-rootfs/usr/lib/gcc/powerpc-linux-gnu/4.6/include
XENOMAI_INC_DIR=/opt/powerpc-465-rootfs/usr/local/xenomai-2.6.2.1/include
XENOMAI_LIB_DIR=/opt/powerpc-465-rootfs/usr/local/xenomai-2.6.2.1/lib
RPATH=-Wl,-rpath-link,/opt/powerpc-465-rootfs/lib/powerpc-linux-gnu
ROOTFS_DIR=/opt/powerpc-465-rootfs
endif

#RTPMACINCLUDEDIR=/usr/local/dtlibs/rtpmac
#LIBPPMACINCLUDEDIR=/usr/local/dtlibs/libppmac
#THESE PATHS MUST BE ABSOLUTE !
RTPMACINCLUDEDIR=/opt/eldk-4.2/PPMAC_rootfs-7-wheezy/opt/ppmac/rtpmac/
LIBPPMACINCLUDEDIR=/opt/eldk-4.2/PPMAC_rootfs-7-wheezy/opt/ppmac/libppmac/

export ARCH
export CROSS_COMPILE

OBJS     := ${patsubst %, %.o, $(MODULES)}
CLEANMOD := ${patsubst %, .%*, $(MODULES)}
PWD      := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)


obj-m	+= usralgo.o
usralgo-objs := usralgomain.o \
usrcode.o

LDFLAGS := -nostdlib
EXTRA_CFLAGS :=  -O2 -DCONFIG_460EX -D_GNU_SOURCE -D_REENTRANT -D__XENO__  -mhard-float -I$(RTPMACINCLUDEDIR) -I$(LIBPPMACINCLUDEDIR) -I$(XENOMAI_INC_DIR) -I$(XENOMAI_INC_DIR)/posix -I$(KSRC)/include/xenomai -I$(KSRC)/include/xenomai/posix -I$(INCLUDE) $(ADD_CFLAGS) --sysroot=$(ROOTFS_DIR)
KBUILD_EXTRA_SYMBOLS := /usr/local/dtlibs/libppmac/Module.symvers

%.o: %.S
	$(CC) -s -D__KERNEL__ -x c -E $< -o $*.i
	$(AS)  -mbooke -o $@ $*.i

#all::
#	cp -f /usr/local/usralgo/usralgomain.c $(PWD)
all:
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules
	scp usralgo.ko root@$(PPMAC):/tmp
	scp pp_proj.ini root@$(PPMAC):/var/ftp/usrflash/Project/Configuration/pp_proj.ini
	scp pp_proj.ini root@$(PPMAC):/tmp/pp_proj.ini
	ssh root@$(PPMAC) projpp

#	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules V=1
#	mv -f usralgo.ko ../../bin/Debug/


#modules:
#	@echo "$(CFLAGS)"

bclean::
	$(RM) *.o .*.o.d .*.o.cmd *.ko *.log
	$(RM) -R .tmp*
	$(RM) .runinfo
	rm -rf .runinfo .tmp* .*.o.d .*.o.cmd .*.cmd *.o *.ko *.mod.c *.i *.so Module.symvers modules.order

clean::
	$(RM) *.o .*.o.d .*.o.cmd *.ko
	$(RM) -R .tmp*
	$(RM) .runinfo
	rm -rf .runinfo .tmp* .*.o.d .*.o.cmd .*.cmd *.o *.ko *.mod.c *.i *.so Module.symvers modules.order

dbg:
	@echo PATH $(PATH)
