
.PHONY: all
all: upcn tools

.PHONY: upcn
upcn:

.PHONY: test
test:

.PHONY: tools
tools:

.PHONY: clean
clean::

.PHONY: uclean
uclean::

.PHONY: tclean
tclean::

.PHONY: toolclean
toolclean::

.PHONY: burn
burn: upcn
	$(ST_FLASH) --reset write components/upcn/build/upcn.bin 0x08000000

.PHONY: burntest
burntest: test
	$(ST_FLASH) --reset write components/test/build/testupcn.bin 0x08000000

.PHONY: flash
flash: upcn
flash: | /usr/bin/expect
flash: | /usr/bin/telnet
	tools/ocdflash.exp components/upcn/build/upcn.bin

.PHONY: flashtest
flashtest: test
flashtest: | /usr/bin/expect
flashtest: | /usr/bin/telnet
	tools/ocdflash.exp components/test/build/testupcn.bin

.PHONY: gdb
gdb: components/upcn/build/upcn
	$(TOOLCHAIN)gdb --eval-command="target remote :3333" \
		--eval-command "cont" components/upcn/build/upcn

.PHONY: gdbtest
gdbtest: components/test/build/testupcn
	$(TOOLCHAIN)gdb --eval-command="target remote :3333" \
		--eval-command "cont" components/test/build/testupcn

.PHONY: stutil
stutil:
	$$(dirname $(ST_FLASH))/st-util -p 3333

.PHONY: connect
connect: tools
	./tools/build/upcn_connect/upcn_connect \
		/dev/serial/by-id/*STM32_Virtual* \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727

.PHONY: receive
receive: tools
	./tools/build/upcn_receive/upcn_receive tcp://127.0.0.1:8726

.PHONY: test-routing
test-routing: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		test routing

.PHONY: test-probabilistic-forwarding
test-probabilistic-forwarding: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		test prob

.PHONY: test-discovery-and-routing
test-discovery-and-routing: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		test dr

.PHONY: send-reset
send-reset: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		reset

.PHONY: send-query
send-query: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		query

.PHONY: check-connection
check-connection: tools
	./tools/build/upcn_test/upcn_test \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727 \
		check

.PHONY: nd-local
nd-local: tools
	./tools/build/rrnd_proxy/rrnd_proxy tcp://127.0.0.1:7763

.PHONY: nd-local-callgrind
nd-local-callgrind: tools
nd-local-callgrind: | /usr/bin/valgrind
	mkdir -pv ndtest && \
	/usr/bin/valgrind --tool=callgrind --dump-instr=yes \
		--simulate-cache=yes --collect-jumps=yes \
		--callgrind-out-file=./ndtest/rrnd_proxy.%p.callgrind \
		./tools/build/rrnd_proxy/rrnd_proxy tcp://127.0.0.1:7763

.PHONY: nd-remote
nd-remote: tools
	./tools/build/rrnd_proxy/rrnd_proxy tcp://127.0.0.1:7763 \
		tcp://127.0.0.1:8726 tcp://127.0.0.1:8727

.PHONY: nd-unittests
nd-unittests:
	nosetests tools/rrnd_test/test

.PHONY: nd-test-scenario/%, nd-test-scenario/
nd-test-scenario/%:
	if [ "$(parameter)" = "" ]; then p=0; else p=$(parameter); fi; \
	mkdir -pv ndtest && cd tools && \
	python -m rrnd_test -t $$(basename $@) -p $$p -f \
	../ndtest/$$(basename $@)_$$(date +%s).out
nd-test-scenario/:
	$(error You have to specify the test: make nd-test-scenario/[1-8])

.PHONY: nd-check-style
nd-check-style:
	python -m flake8 --max-complexity 10 ./tools/rrnd_test

_VENV_NAME=upcn

.PHONY: nd-create-virtualenv
nd-create-virtualenv:
	bash -c ". $$(which virtualenvwrapper.sh); \
		mkvirtualenv -ppython3 --system-site-packages $(_VENV_NAME) && \
		pip install -U setuptools pip wheel && \
		pip install -U -r ./tools/rrnd_test/requirements.txt;"; \
	echo "=> To activate the virtualenv, use: workon $(_VENV_NAME)"

.PHONY: nd-update-virtualenv
nd-update-virtualenv:
	bash -c ". $$(which virtualenvwrapper.sh); \
		workon $(_VENV_NAME) && \
		pip install -U setuptools pip wheel && \
		pip install -U -r ./tools/rrnd_test/requirements.txt;"; \
	echo "=> To activate the virtualenv, use: workon $(_VENV_NAME)"

.PHONY: nd-perfeval-sgp4
nd-perfeval-sgp4: tools
nd-perfeval-sgp4: | /usr/bin/valgrind
	mkdir -pv ndtest && \
	/usr/bin/valgrind --tool=callgrind --dump-instr=yes \
		--simulate-cache=yes --collect-jumps=yes \
		--callgrind-out-file=./ndtest/sgp4_perf.%p.callgrind \
		./tools/build/rrnd_perf_eval/sgp4_perf

.PHONY: nd-perfeval-mpfit
nd-perfeval-mpfit: tools
nd-perfeval-mpfit: | /usr/bin/valgrind
	mkdir -pv ndtest && \
	/usr/bin/valgrind --tool=callgrind --dump-instr=yes \
		--simulate-cache=yes --collect-jumps=yes \
		--callgrind-out-file=./ndtest/mpfit_perf.%p.callgrind \
		./tools/build/rrnd_perf_eval/mpfit_perf

.PHONY: nd-perfeval-ipnd
nd-perfeval-ipnd: tools
nd-perfeval-ipnd: | /usr/bin/valgrind
	mkdir -pv ndtest && \
	/usr/bin/valgrind --tool=callgrind --dump-instr=yes \
		--simulate-cache=yes --collect-jumps=yes \
		--callgrind-out-file=./ndtest/ipnd_perf.%p.callgrind \
		./tools/build/rrnd_perf_eval/ipnd_perf

.PHONY: count-lines
count-lines:
	find components/upcn -name "*.c" -or -name "*.h" | xargs wc -l

.PHONY: check-style
check-style:
	./tools/analysis/stylecheck.sh ./tools/analysis/checkpatch.pl

.PHONY: clang-check
clang-check:
	./tools/analysis/clang-check.sh

.PHONY: clang-tidy
clang-tidy:
	./tools/analysis/clang-check.sh tidy


# DEFAULT: DEBUG
CFLAGS = -Wall -g -DDEBUG -DDEBUG_FREERTOS_TRACE
CFLAGS_LOCAL = -Wall -g -DDEBUG -DUPCN_LOCAL
CFLAGS_WEXTRA = -Wextra -Wno-unused-parameter \
	-Wno-override-init -Wno-unused-but-set-parameter -Wno-error=extra

ifeq "$(type)" "release"
  CFLAGS = $(CFLAGS_ALL) -DDEBUG_FREERTOS_TRACE
  CFLAGS_LOCAL = $(CFLAGS_ALL) -DUPCN_LOCAL
endif

ifeq "$(memdebug)" "strict"
  CFLAGS += -DMEMDEBUG_STRICT
else
  ifeq "$(memdebug)" "no"
    CFLAGS += -DNO_MEMDEBUG
  endif
endif

ifeq "$(log)" "yes"
  CFLAGS += -DLOGGING
else
  ifeq "$(log)" "no"
    CFLAGS += -DNO_LOGGING
  else
    CFLAGS += -DNO_LOGGING -DLOG_PRINT
  endif
endif

ifeq "$(verbose)" "yes"
  CFLAGS += -DVERBOSE
else
  ifeq "$(verbose)" "no"
    CFLAGS += -DQUIET
  endif
endif

ifeq "$(bprint)" "yes"
  CFLAGS += -DBUNDLE_PRINT
endif

ifeq "$(optimize)" "yes"
  CFLAGS += -O3
  CFLAGS_LOCAL += -O3
endif

ifneq "$(wextra)" "no"
  CFLAGS += $(CFLAGS_WEXTRA)
  CFLAGS_LOCAL += $(CFLAGS_WEXTRA)
endif

ifeq "$(werror)" "yes"
  CFLAGS += -Werror
  CFLAGS_LOCAL += -Werror
endif

ifeq "$(target)" "somp2"
  CFLAGS += -DBOARD_SOMP2
endif

-include config.mk
include components/system/board.mk
include components/system/toolchain.mk
include components/system/build.mk

include components/drv/build.mk
include components/upcn/build.mk
include components/test/build.mk

include tools/build.mk
