#------------------------ CosateQ -----------------------------------------------
#  $Id: Makefile 1243 2009-03-26 14:38:59Z muellera $
#
#  CosateQ                                     Tel.  : (07522) 9749-0
#  Siemensstrasse 12                           Fax   : (07522) 9749-49
#  88239 Wangen i. A.                          eMail : info@cosateq.com
#
#  Copyright 2009, CosateQ
#  Unpublished Work. All Rights Reserved Under The German Copyright Laws.
#
#  Date     : 25.03.2009
#  Project  : SCALE-RT Prototyping System
#  File     : Makefile
#  Contents : Basic Makefile for compiling C style models for SCALE-RT 5.0.
#             Creates an kernel module and packs it together with an xml
#             file into a <MODEL>.tgz which can be uploaded to a SCALE-RT target.
#             Must be called within the SCALE-RT cygwin environment.
#
#  Makefile Parameters
#
#     MODEL        The model name (WITHOUT ANY WHITESPACES)
#                  The c file of the model must be $(MODEL).c
#
#     MODELSRCDIR  The source directory of the model (e.g. $PWD)
#
#  HINT:
#     The directory where this Makefile is called MUST NOT CONTAIN WHITESPACES
#     due to the model.
#     Therefore it is recommended to use the compilemd.sh script file which calls
#     the Makefile itself (allowing whitepaces in the path).
# 
#  History  :
#  Date        Ver   Editor                  Change
#  25.03.2009  1.0  muellera                Makefile for dshplus1 example
#-------------------------------------------------------------------------------

HOST            = PC
BUILD           = yes
MODULES         =

ifneq ($(KERNELRELEASE),)
  # invoked from the kernel
else
  # invoked from the command line
  SUBDIRS=$(shell pwd)
endif

#-------------------------------- GNU Tools ------------------------------------
#
# You may need to modify the following variables if you have installed the GNU
# Tools in a different location.
#
SCALERTVERSION         = 1.0
CFRAMEVERSION          = 1.9
KERNELVERSION          = linux-2.6.20.14-ipipe
KPATCHVERSION          = xenomai-2.3.1
KOMPILVERSION          = 4.1.1
GNUROOT                = /scale-rt-cross
TARGET                 = i686-pc-linux-gnu
GCCINCLUDPATH          = $(GNUROOT)/$(TARGET)
CROSS_COMPILE          = $(GNUROOT)/bin/$(TARGET)-
GCC_EXEC_PREFIX        = $(GNUROOT)/bin/

CC = $(GNUROOT)/bin/$(TARGET)-gcc.exe
LD = $(GNUROOT)/bin/$(TARGET)-ld.exe
AR = $(GNUROOT)/bin/$(TARGET)-ar.exe

#------------------------------ Include Path -----------------------------------
# includes for model & s-functions source build

RTLINUX_INCLUDES = \
	-I$(GNUROOT)/usr/src/linux/include \
	-I$(GNUROOT)/usr/src/linux/include/asm \
	-I$(GNUROOT)/usr/src/linux/include/asm/mach-default \
	-I$(GNUROOT)/usr/src/linux/include/xenomai \
	-I$(GCCINCLUDPATH)/include

USER_INCLUDES = -I"$(MODELSRCDIR)/."

# includes for build
MODEL_IF_INCLUDE := "$(MODELSRCDIR)/../../include"
INCLUDES = -I"$(MODEL_IF_INCLUDE)" $(RTLINUX_INCLUDES) $(USER_INCLUDES)

#-------------------------------- C Flags --------------------------------------
# General User Options
OPTS      =

# Required Options
GCC_FLAGS = -Wall -nostdinc -mhard-float -fno-builtin -fno-common -fno-strict-aliasing -B$(GCC_EXEC_PREFIX) -B$(CROSS_COMPILE)
REQ_OPTS  = -D__KERNEL__ -DMODULE $(GCC_FLAGS) -DSCALE_RT -DPROSYS_RT -D__PROSYS_GCC_2_6 -DCONFIG_MODULE_UNLOAD

CC_OPTS   = $(REQ_OPTS) $(OPTS) $(EXT_CC_OPTS) $(LOG_OPTS)

CPP_REQ_DEFINES = -DMODEL=$(MODEL) -DRT -DNUMST=$(NUMST) \
                  -DTID01EQ=$(TID01EQ) -DNCSTATES=$(NCSTATES) -DSHM_USE_LXRT

EXTRA_CFLAGS    = -c $(CC_OPTS) $(CPP_REQ_DEFINES) $(INCLUDES) $(OPT_OPTS) -D_bool_T -D_FORTIFY_SOURCE -DRSIM_WITH_SOLVER_MULTITASKING=0
export EXTRA_CFLAGS

#----------------------------- Source Files ------------------------------------
#
EXT_SRC = 

REQ_SRCS  = $(MODEL).c $(MODULES) $(EXT_SRC) $(LOG_SRC)
USER_SRCS = scalert_simul.c dgltools_scalert.c pneutools_scalert.c meldung_scalert.c

SRCS     =  $(REQ_SRCS) $(SOLVER) $(USER_SRCS)
OBJS      = $(SRCS:.c=.o) 

PROGRAM   = rtm-$(MODEL).o

#----------------------------- Defines -----------------------------------------

RETVALSYM  = 0
EXT_OBJS_ROOT := "$(MODELSRCDIR)/../../obj/rtw/c/scale_rt/"
EXT_OBJS := rtm-scale_precomp.o 

#--------------------------------- Rules ---------------------------------------

obj-m:= rtm-$(MODEL).o

rtm-$(MODEL)-objs := $(OBJS) $(EXT_OBJS)

KDIR:= $(GNUROOT)/usr/src/linux
SHELL:=/bin/bash
PWD:= $(shell pwd)

#------------------- Rule to make MODEL ----------------------------------------
default: clean preconditions
	$(MAKE) V=1 CROSS_COMPILE=$(GNUROOT)/bin/$(TARGET)- -C$(KDIR) SUBDIRS="$(PWD)" MODELSRCDIR="$(MODELSRCDIR)" modules || ( echo "ERROR while executing sub-makefile return code = $$?" ; exit 1)
	@echo ""
	@echo "*******************************************************"
	@echo "* Model Build Information:   Scale-RT:  $(SCALERTVERSION)"
	@echo "*                            CodeFrame: $(CFRAMEVERSION)"
	@echo "*                            Kernel:    $(KERNELVERSION)"
	@echo "*                            Compiler:  $(KOMPILVERSION)$(KPATCHVERSION)"
	@echo "*******************************************************"
	@echo "### Created executable: $(PROGRAM)"
	@cp rtm-$(MODEL).ko rtm-$(MODEL).o
	/bin/tar --ignore-failed-read -cz -f $(MODEL).tgz rtm-$(MODEL).o *.xml
	@echo "### Created compressed archiv: $(MODEL).tgz"
	$(MAKE) clean

# check support files	
preconditions:
	@echo
	@echo "[Checking preconditions]"
	@[ ! -z "$(MODEL)" ] || ( echo "ERROR: MODEL parameter is missing in Makefile" ; exit 1 )
	@[ "$${MODEL// /}" == "$${MODEL}" ] || ( echo "ERROR: Whitespaces in MODEL not allowed" ; exit 1 ) 
	@[ "$${PWD// /}" == "$${PWD}" ] || ( echo "ERROR: Whitespaces in build path not allowed!!" ; exit 1 )
	@echo " * no whitespaces in build path"	
	@[ ! -z "$(MODELSRCDIR)" ] || ( echo "ERROR: MODELSRCDIR parameter is missing in Makefile" ; exit 1 )
	@echo " * Makefile parameters set"
	@[ -f "$(CC)" ] || ( echo "ERROR: cannot find gcc $(CC). Check your cygwin environment" ; exit 1 )
	@echo " * GCC found in $(CC)"
	@[ -f "$(LD)" ] || ( echo "ERROR: cannot find ld $(LD). Check your cygwin environment" ; exit 1 )
	@echo " * LD found in $(LD)"
	@[ -f "$(MODEL_IF_INCLUDE)/model_if.h" ] || ( echo "ERROR: cannot find model_if.h in MODEL_IF_INCLUDE directory $(MODEL_IF_INCLUDE). Check your cygwin environment" ; exit 1 )
	@echo " * model_if.h found in $(MODEL_IF_INCLUDE)"
	@[ -f "$(MODELSRCDIR)/$(MODEL).c" ] || ( echo "ERROR: model c file \"$(MODEL).c\" not found in MODELSRCDIR $(MODELSRCDIR)"; exit 1 )
	@echo " * Model \"$(MODEL).c\" found in $(MODELSRCDIR)"
	@for f in $(EXT_OBJS) ; do [ -f "$(EXT_OBJS_ROOT)/$$f" ] || ( echo "ERROR: cannot find external object file \"$$f\" in $(EXT_OBJS_ROOT)" ; exit 1 ) ; done
	@echo " * External objects found"
	@echo
	@echo " * Copying extra files into compile directory"
	@for f in $(EXT_OBJS) ; do cp "$(EXT_OBJS_ROOT)/$$f" "$(MODELSRCDIR)/" ; done
	@echo "[preconditions OK]"
	@echo

clean:
	rm -rf .tmp_versions
	rm -f *~ *.o
	rm -f *.mod.*
	rm -f *.ko
	rm -f .*.cmd
	rm -f Module.symvers
	@[ ! -z "$(MODEL)" ] || ( echo "ERROR: MODEL parameter is missing in Makefile" ; exit 1 )
	@[ "$${MODEL// /}" == "$${MODEL}" ] || ( echo "ERROR: Whitespaces in MODEL not allowed" ; exit 1 ) 	
	rm -f ".rtm-$(MODEL)"*
	rm -f ".$(MODEL)"*

.PHONY: default preconditions clean
