# -----------------------------------------------------------------------------
# makefile
#
# Make file for SCADAPack 350 / SCADASense 4203 C Tools application for 
# TelePACE firmware
# Copyright 2007 Control Microsystems Inc.
#
# usage:
#    make                     - makes application for all controllers
#    make TARGET=SCADAPack350 - makes application for SCADAPack 350 controller
#    make TARGET=4203         - makes application for SCADASense 4203 controller
#    make clean               - deletes all output files
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# set the name of the output file here
# -----------------------------------------------------------------------------

APPLICATION_NAME = sp350TelePACE

# -----------------------------------------------------------------------------
# list all object files here
# -----------------------------------------------------------------------------

objects = appstart.o main.o

# -----------------------------------------------------------------------------
# list the default target controllers here
# -----------------------------------------------------------------------------

TARGET = SCADAPack350 4203

# -----------------------------------------------------------------------------
# set location of C Tools files
# -----------------------------------------------------------------------------

# take the C Tools path from the environment, or set default if it's not there (default may not be correct for all installations)
ifeq ($(strip $(CTOOLS_PATH)),)
CTOOLS_PATH = C:\Program Files\Control Microsystems\CTools
endif

# -----------------------------------------------------------------------------
# set location of TelePACE specific files
# -----------------------------------------------------------------------------

INCLUDE_PATH = $(CTOOLS_PATH)\Controller\TelePACE

# -----------------------------------------------------------------------------
# compiler flags
# -----------------------------------------------------------------------------

CFLAGS = -O3 -mapcs-32 -mlittle-endian -march=armv4 -ansi -fno-builtin -DARMEL -I"$(INCLUDE_PATH)" -DCPU=ARMARCH4 -DTOOL_FAMILY=gnu -DTOOL=gnu -std=c99

# -----------------------------------------------------------------------------
# list of file suffixes used in this makefile
# -----------------------------------------------------------------------------

.SUFFIXES:
.SUFFIXES: .cpp .c .o .out

# -----------------------------------------------------------------------------
# determine intermediate link target(s) used to check
# if all symbols can be resolved in firmware
# -----------------------------------------------------------------------------

stripTarget = $(strip $(TARGET))
ifeq ($(stripTarget),SCADAPack350 4203)
intermediate_objects = imLink_SCADAPack350.o imLink_4203.o
endif

ifeq ($(stripTarget),SCADAPack350)
intermediate_objects = imLink_SCADAPack350.o
endif

ifeq ($(stripTarget),4203)
intermediate_objects = imLink_4203.o
endif

# -----------------------------------------------------------------------------
# rules for making .out file
# -----------------------------------------------------------------------------

$(APPLICATION_NAME).out : imImage.o $(intermediate_objects)
# Process CPP constructors and destructors
	@echo
	@echo --------------------
	@echo Building output file
	@echo --------------------
	nmarm imImage.o | "$(CTOOLS_PATH)\Arm7\tcl\bin\tclsh84.exe" "$(CTOOLS_PATH)\Arm7\host\x86-win32\bin\munch.tcl" -c arm > ctdt.c
	ccarm $(CFLAGS) -c -fdollars-in-identifiers ctdt.c -o ctdt.o
	
# Link downloadable application.
	ccarm -I. -r -nostdlib -Wl,-X -Wl,-EL -T "$(CTOOLS_PATH)\Arm7\target\h\tool\gnu\ldscripts\link.OUT" imImage.o ctdt.o -o $(APPLICATION_NAME).out

# Clean up temporary files
	del ctdt.c ctdt.o

# -----------------------------------------------------------------------------
# rules for making intermediate objects
# -----------------------------------------------------------------------------

imImage.o: $(objects) 
# Merge all object files into one
	ccarm -I. -r -nostdlib -Wl,-X -Wl,-EL -Wl $(objects) -o imImage.o
	
# -----------------------------------------------------------------------------
# link with controller specific CTools library to check for unresolved externals
# -----------------------------------------------------------------------------

imLink_SCADAPack350.o: imImage.o
	@echo
	@echo -------------------------------------------------------------------
	@echo Checking for unresolved externals with SCADAPack 350 CTools library
	@echo -------------------------------------------------------------------
	ldarm -e0 imImage.o "$(INCLUDE_PATH)\SCADAPack_350_TelePACE_Firmware_Image" -o imLink_SCADAPack350.o

imLink_4203.o: imImage.o
	@echo
	@echo ---------------------------------------------------------------------
	@echo Checking for unresolved externals with SCADASense 4203 CTools library
	@echo ---------------------------------------------------------------------
	ldarm -e0 imImage.o "$(INCLUDE_PATH)\SCADASense_4203_TelePACE_Firmware_Image" -o imLink_4203.o

# -----------------------------------------------------------------------------
# list all source file dependencies here
# -----------------------------------------------------------------------------

appstart.o: appstart.cpp nvMemory.h
main.o:     main.cpp nvMemory.h

# -----------------------------------------------------------------------------
# rules for making files
# -----------------------------------------------------------------------------

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

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

# -----------------------------------------------------------------------------
# clean up all output files
# -----------------------------------------------------------------------------
.PHONY: clean
clean:
	del *.o
	del *.out


