#-------------------------------------------------------------------------------
#
ALL += build/s2c.c-cmm/main.binary
ALL += build/s2c.c-lmm/main.binary
ALL += build/s2c.cpp-cmm/main.binary
ALL += build/s2c.cpp-lmm/main.binary
ALL += build/s2c.pasm-cog/main.binary
ALL += build/s2c.pasm-hub/main.binary
ALL += build/openspin/main.binary
#
# files every compilation depends on:
#
DEPS = Makefile FullDuplexSerial.spin
#
# needed to make c(++) binaries stop in spinsim
CSTOP = -Dcogstop0
#
#-------------------------------------------------------------------------------
#
# additional PATH entries.
# the order is important!
# e.g.: because /opt/parallax contains openspin, spin2cpp and spinsim too.
#
APATH += /opt/openspin/bin
APATH += /opt/spin2cpp/bin
APATH += /opt/spinsim/bin
APATH += /opt/parallax/bin

export PATH := $(shell printf '%s:' $(APATH))$(PATH)
#
#-------------------------------------------------------------------------------
#
all: $(ALL:.binary=.log)
	@echo 'Size'
	@echo '----'
	@stat -c'%14s bytes    %n' $(ALL) | sort -n
	@echo
	@echo 'Speed'
	@echo '-----'
	@awk '/^[0-9]+ ticks$$/ { \
		printf "%20s    %-32s\n",$$0,FILENAME | "sort -n" \
	}' $(ALL:.binary=.log)
	@echo
#
#-------------------------------------------------------------------------------
#
build/s2c.c-cmm/%.binary: %.spin $(DEPS) build/s2c.c-cmm
	spin2cpp $(CSTOP) --ccode --binary -mcmm -Os -o $@ $<

build/s2c.c-lmm/%.binary: %.spin $(DEPS) build/s2c.c-lmm
	spin2cpp $(CSTOP) --ccode --binary -mlmm -Os -o $@ $<

build/s2c.cpp-cmm/%.binary: %.spin $(DEPS) build/s2c.cpp-cmm
	spin2cpp $(CSTOP) --binary -mcmm -Os -o $@ $<

build/s2c.cpp-lmm/%.binary: %.spin $(DEPS) build/s2c.cpp-lmm
	spin2cpp $(CSTOP) --binary -mlmm -Os -o $@ $<

build/s2c.pasm-hub/%.binary: %.spin $(DEPS) build/s2c.pasm-hub
	spin2cpp --asm --code=hub --binary -o $@ $<

build/s2c.pasm-cog/%.binary: %.spin $(DEPS) build/s2c.pasm-cog
	spin2cpp --asm --code=cog --binary -o $@ $<

build/openspin/%.binary: %.spin $(DEPS) build/openspin
	openspin -u -o $@ $<
#
#-------------------------------------------------------------------------------
#
build/s2c.c-cmm \
build/s2c.c-lmm \
build/s2c.cpp-cmm \
build/s2c.cpp-lmm \
build/s2c.pasm-cog \
build/s2c.pasm-hub \
build/openspin: build
	@test -d $@ || mkdir $@

build:
	@test -d $@ || mkdir $@
#
#-------------------------------------------------------------------------------
#
build/%/main.log: build/%/main.binary
	spinsim $< -b | tee $@

run: $(ALL:.binary=.log)

#
#-------------------------------------------------------------------------------
#
clean:
	rm -rf build
#
#-------------------------------------------------------------------------------
