# Mostly written by Jonathan Larmour, Cygnus Solutions
# This file is in the public domain and may be used for any purpose

# PKG_INSTALL_DIR might need to be edited.  Right now it is set
# assuming that a user ran pkgconf.tcl in //c/ecos-work on Windows NT,
# or used the Configuration Tool with C:\ecos-work as a build-tree.
#
# You can also override it on the make command-line, e.g.:
#   make PKG_INSTALL_DIR=/myecc/install
# or you can set it in your environment

PKG_INSTALL_DIR = //c/ecos-work/install

# You must also set XCC to the name of your cross-compiler, including any
# options it needs.

# Uncomment one of the below, or invoke make with the name of the compiler
# you want, e.g.:
#   make XCC=mn10300-elf-gcc
# You can also set XCC in your environment

#XCC = mn10300-elf-gcc
#XCC = mips-tx39-elf-gcc
#XCC = powerpc-eabi-gcc -mcpu=860 -D_SOFT_FLOAT
#XCC = arm-elf-gcc -mcpu=arm7dmi
#XCC = arm-elf-gcc -mcpu=arm7tdmi
#XCC = sparclite-elf-gcc -mcpu=sparclite
#XCC = i686-pc-linux-gnu-gcc

###### VARIABLES
# Any of these can be overriden on the command-line or in your environment

CFLAGS        = -g

CXXFLAGS      = $(CFLAGS)

EXTRACFLAGS   = -Wall -I$(PKG_INSTALL_DIR)/include -ffunction-sections -fdata-sections

EXTRACXXFLAGS = $(EXTRACFLAGS) -fno-exceptions -fno-rtti -fvtable-gc -finit-priority

LDFLAGS       = -nostartfiles -L$(PKG_INSTALL_DIR)/lib -Wl,--gc-sections
LIBS          = -Ttarget.ld -nostdlib

LD            = $(XCC)
XCXX          = $(XCC)

###### RULES

.PHONY: all clean CCCHECK

all: hello twothreads simple-alarm serial

clean:
	-rm -f hello hello.o twothreads twothreads.o
	-rm -f simple-alarm simple-alarm.o serial serial.o
	-rm -f instrument-test instrument-test.o

CCCHECK:
ifeq ($(XCC),)
	@echo You must set XCC to the name of your cross-compiler
	@false
endif


%.o: %.c
	$(XCC) -c -o $*.o $(CFLAGS) $(EXTRACFLAGS) $<

%.o: %.cxx
	$(XCXX) -c -o $*.o $(CXXFLAGS) $(EXTRACXXFLAGS) $<

%.o: %.C
	$(XCXX) -c -o $*.o $(CXXFLAGS) $(EXTRACXXFLAGS) $<

%.o: %.cc
	$(XCXX) -c -o $*.o $(CXXFLAGS) $(EXTRACXXFLAGS) $<

hello: CCCHECK hello.o
	$(LD) $(LDFLAGS) -o $@ $@.o $(LIBS)

twothreads: CCCHECK twothreads.o
	$(LD) $(LDFLAGS) -o $@ $@.o $(LIBS)

simple-alarm: CCCHECK simple-alarm.o
	$(LD) $(LDFLAGS) -o $@ $@.o $(LIBS)

serial: CCCHECK serial.o
	$(LD) $(LDFLAGS) -o $@ $@.o $(LIBS)

instrument-test: CCCHECK instrument-test.o
	$(LD) $(LDFLAGS) -o $@ $@.o $(LIBS)

