# Default target.
all:

ifndef TILERA_ROOT
$(error The 'TILERA_ROOT' environment variable is not set.)
endif

CC = $(TILERA_ROOT)/bin/tile-cc
OPT = -Os
CFLAGS = -std=gnu99 -Wall -Werror -g $(OPT)

TILE_MONITOR = $(TILERA_ROOT)/bin/tile-monitor

all: test memory

memory: memory.c mem_pool.o
	$(CC) $(CFLAGS) -pthread -ltmc memory.c mem_pool.o -o memory  

test: test.o mem_pool.o
	$(CC) $(CFLAGS) -ltmc mem_pool.o test.o -o test 

test.o: test.c mem_pool.o
	$(CC) $(LDFLAGS) test.c -pthread -ltmc -c -o test.o 

mem_pool.o: mem_pool.c
	$(CC) $(LDFLAGS) -pthread mem_pool.c -ltmc -c -o mem_pool.o 

clean:
	rm -f *.o test test_pci.out

.PHONY: all clean

SIZE = 1x1

TILE_MONITOR_ARGST = \
  --resume --net localhost --shepherd-port 10000 --tile $(SIZE) \--debug-on-crash --batch-mode \
  --mkdir /opt/test --cd /opt/test \
  --upload test test -- test \
  --debug-on-crash --batch-mode

TILE_MONITOR_ARGSM = \
  --resume --net localhost --shepherd-port 10000 --tile 8x8 \
  --debug-on-crash --batch-mode \
  --mkdir /opt/memory --cd /opt/memory \
  --upload memory memory -- memory \
  --debug-on-crash --batch-mode

FILTER = sed -e 's/= [0-9]*/= NUMBER/g'

run_pcit: $(EXE)
	$(TILE_MONITOR) \
	  $(TILE_MONITOR_ARGST)

run_pcim: $(EXE)
	$(TILE_MONITOR) \
	  $(TILE_MONITOR_ARGSM)

test_pci: $(EXE)
	set -e -o pipefail; \
	$(TILE_MONITOR) --pci \
	  $(TILE_MONITOR_ARGS) \
	  | $(FILTER) > $@.out; \
	 cat output.txt | $(FILTER) | diff - $@.out

run_shep: test
	$(TILE_MONITOR) --

test_sim: $(EXE)
	echo "WARNING: Simulator testing not implemented yet."

.PHONY: run_pci test_pci test_sim
