#
# RTES_final makefile
#
# 	 Author: Christos Choutouridis AEM:8997
#	 email:  cchoutou@ece.auth.gr
#

#
# Edit the paths to match your environment
# Currently supported:
#    Windows (we prefer Cygwin)
#    GNU/Linux
#
LINUX_CXX   := /usr/local/bin/cross-pi0/bin/arm-linux-gnueabihf-gcc
WIN_CXX		:= D:\SysGCC\raspberry\bin\arm-linux-gnueabihf-gcc.exe

# RTES related stuff for upload
SCP      	:= scp
PIUSER		:= root
PI			:= 192.168.0.1
RTES_PATH	:= /usr/local/bin/

# === Compilation related stuff ===
CXXFLAGS 	:= -std=c11 -Wall -Wextra -Werror
CXXFEXTR 	:= -c -fmessage-length=0
LDFLAGS  	:= -lm -lpthread
BUILD    	:= ./bin
OBJ_DIR  	:= $(BUILD)/obj
APP_DIR  	:= $(BUILD)
TARGET   	:= rtes_final
SRC  		:= $(wildcard src/*.c)

# === OS selection ===
ifeq ($(OS),Windows_NT)
	# Windows, select exe path
	CXX		:= $(WIN_CXX)
else
	# Linux filter [currently only GNU/Linux]
	UNAME	:= $(shell uname)
	ifeq ($(UNAME),Linux)
		CXX := $(LINUX_CXX)
	else
		ERR := $(error Not supporting OS) 
	endif
endif

# === MakeFile body ===
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)

$(OBJ_DIR)/%.o: %.c
	@mkdir -p $(@D)
	$(CXX) $(CXXFEXTR) $(CXXFLAGS) -o $@ -c $<

$(APP_DIR)/$(TARGET): $(OBJECTS)
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)

# === Rules ===
rtes_final: build $(APP_DIR)/$(TARGET)

clean:
	-@rm -rvf $(OBJ_DIR)/*
	-@rm -rvf $(APP_DIR)/*

build:
	@mkdir -p $(APP_DIR)
	@mkdir -p $(OBJ_DIR)

debug: CXXFLAGS += -DDEBUG -g3
debug: rtes_final

release: CXXFLAGS += -O2
release: rtes_final

upload:
	$(SCP) $(BUILD)/$(TARGET) $(PIUSER)@$(PI):$(RTES_PATH)

all: clean release upload

.PHONY: rtes_final build debug release upload clean all

