include CrossEnvironment.inc
include Target.inc


CPP = $(CROSS_COMPILE)g++
CC = $(CROSS_COMPILE)gcc

CFLAGS = -D_GNU_SOURCE -D_LINUX -mcpu=cortex-a9 -march=armv7-a -Wall -mfloat-abi=softfp -mtune=cortex-a9 -fno-strict-aliasing -fPIC -mfpu=neon -I./inc -DPLAT_LINUX -std=c99

# Define the following for a release build
#CFLAGS += -O3 -DNDEBUG
# ...or the define the following for a debug build (if you want to use a debugger as GDB)
CFLAGS += -O0 -g

LIBPATH = ./lib

LFLAGS += -L$(LIBPATH) -lbta_eth
# For using GPU OpenCL, add the following libraries for the linker
LFLAGS += -lGAL -lOpenCL

APP = app/bta_eth_example
OBJS = app/bta_eth_example.o app/gpu_opencl_example.o app/neon_example.o


.PHONY: all
all: $(APP)


$(APP): $(OBJS) Makefile
	$(CC) -o $(APP) $(OBJS) $(LFLAGS)


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


.PHONY: help
help:
	@echo
	@echo '   === Bluetechnix Pxxx Development Package Help ==='
	@echo
	@echo '   make ...................... Build example'
	@echo '   make deploy ............... Deploy application and scripts onto target'
	@echo '   make clean ................ Clean example'
	@echo '   make autostart_on ......... Copy application autostart script to target'
	@echo '   make autostart_off ........ Remove application autostart script from target'
	@echo '   make gdbstart ............. Start a remote debugging session with local GDB'
	@echo '                               and gdbserver on target'
	@echo "   make debugtools_install ... Installs Freescale's debug tools (GDB etc.) onto"
	@echo '                               target'
	@echo '   make debugtools_remove .... Removes debug tools from target'
	@echo '   make setup_login .......... (Creates and) copies your SSH key to the target,'
	@echo '                               for password-less login'
	@echo


.PHONY: deploy
deploy:
	scp $(APP) lib/libbta_eth.so run_app_gdb.sh run_app_gdbserver.sh user@$(TARGET_IP_ADDR):/mnt/user
	scp start_custom.sh user@$(TARGET_IP_ADDR):/mnt/user/run_app.sh


.PHONY: debugtools_install
debugtools_install:
	scp -r toolchain/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/arm-fsl-linux-gnueabi/debug-root user@$(TARGET_IP_ADDR):/mnt/user


.PHONY: debugtools_remove
debugtools_remove:
	ssh user@$(TARGET_IP_ADDR) rm -rf /mnt/user/debug-root


.PHONY: autostart_on
autostart_on:
	scp start_custom.sh user@$(TARGET_IP_ADDR):/mnt/user
	@echo
	@echo
	@echo "********************************************************************************"
	@echo "*  Your application WILL be run at boot.                                       *"
	@echo "********************************************************************************"
	@echo
	@echo


.PHONY: autostart_off
autostart_off:
	ssh user@$(TARGET_IP_ADDR) rm -f /mnt/user/start_custom.sh
	@echo
	@echo
	@echo "********************************************************************************"
	@echo "*  Your application will NOT be run at boot.                                   *"
	@echo "*  Use the script /mnt/user/run_app.sh to start your application manually.     *"
	@echo "********************************************************************************"
	@echo
	@echo


.PHONY: setup_login
setup_login: $(HOME)/.ssh/id_rsa.pub # Need public key for automated SSH login
	@echo
	@echo
	@echo "********************************************************************************"
	@echo "*  I will now copy your public SSH key to the camera, so that the camera       *"
	@echo "*  accepts your SSH login without a password. Your SSH key is stored in        *"
	@echo "*  $(HOME)/.ssh/id_rsa.pub                                                     *"
	@echo "*                                                                              *"
	@echo "*  You have to provide the password for user 'user' twice. (The default        *"
	@echo "*  password is 'user'.)                                                        *"
	@echo "********************************************************************************"
	@echo
	@echo
	sleep 5
	ssh user@$(TARGET_IP_ADDR) mkdir -p /mnt/user/.ssh
	scp $(HOME)/.ssh/id_rsa.pub user@$(TARGET_IP_ADDR):/mnt/user/.ssh/authorized_keys


$(HOME)/.ssh/id_rsa.pub:
	@echo
	@echo
	@echo "********************************************************************************"
	@echo "*  You do not have SSH keys in your home directory. I will now start the       *"
	@echo "*  ssh-keygen program to create an SSH key pair for you. Please answer the     *"
	@echo "*  following questions. Please don't change the file name that is suggested.   *"
	@echo "********************************************************************************"
	@echo
	@echo
	sleep 5
	ssh-keygen -t rsa -N "" -f $(HOME)/.ssh/id_rsa
	test -r $(HOME)/.ssh/id_rsa.pub # Make sure there is a key, or Make target will fail
	ssh-add $(HOME)/.ssh/id_rsa


.PHONY: gdbstart
gdbstart:
	ssh user@$(TARGET_IP_ADDR) ./run_app_gdbserver.sh &
	echo "file $(APP)" > gdbremoteinit
	echo "target remote $(TARGET_IP_ADDR):20000" >> gdbremoteinit
	toolchain/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-gdb --command=gdbremoteinit


.PHONY: clean
clean:
	rm -f $(APP) $(OBJS)
