TARGET = Cidn-SH
OBJ_PATH = $(shell pwd)/debug
PREFIX_BIN =

CC = ${CXX}
STRIP = aarch64-poky-linux-strip
INCLUDES = -I../Tools/GatewayThirdParty/boost/include \
			-I../Tools/GatewayThirdParty/fftw/include \
		   -I../Tools/GatewayThirdParty/jsoncpp/include \
			-I../Tools/GatewayThirdParty/sqlite/include/ \
			-I../Tools/GatewayThirdParty/curl/include/ \
			-I../Tools/GatewayThirdParty/zlog/include/ \
			-I ./ \

LIBS = -L../Tools/renesas_thirdparty/lib \
	-L../Tools/renesas_thirdparty/lib \
	   -L../Tools/renesas_thirdparty/lib \
	   -L../Tools/renesas_thirdparty/lib \
	   -L../Tools/renesas_thirdparty/lib \
	   -L../Tools/renesas_thirdparty/lib \
       -lsqlite3 -lboost_system -lpthread -lboost_thread -lboost_date_time -lfftw3  -ljsoncpp -lmosquitto -lcurl

CFLAGS = -O0    -fpermissive #-Wall -Werror 
LINKFLAGS =

AllDirs :=$(shell ls -R | grep '^\./.*:' | awk '{if( \
						  substr($$1,3,5) != "debug") {gsub(":",""); print}}') .

Sources := $(foreach n,$(AllDirs) , $(wildcard $(n)/*.cpp))

#C_SOURCES = $(wildcard *.c)
C_SRCDIR = $(AllDirs)
C_SOURCES = $(foreach d,$(C_SRCDIR),$(wildcard $(d)/*.c) )
C_OBJS = $(patsubst %.c, $(OBJ_PATH)/%.o, $(C_SOURCES))

#CPP_SOURCES = $(wildcard *.cpp)
CPP_SRCDIR = $(AllDirs)
CPP_SOURCES = $(foreach d,$(CPP_SRCDIR),$(wildcard $(d)/*.cpp) )
CPP_OBJS = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(CPP_SOURCES))

default:init compile strip

$(C_OBJS):$(OBJ_PATH)/%.o:%.c
	$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@

$(CPP_OBJS):$(OBJ_PATH)/%.o:%.cpp
	$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@

init:
	$(foreach d,$(AllDirs), mkdir -p $(OBJ_PATH)/$(d);)

test:
	@echo "C_SOURCES: $(C_SOURCES)"
	@echo "C_OBJS: $(C_OBJS)"
	@echo "CPP_SOURCES: $(CPP_SOURCES)"
	@echo "CPP_OBJS: $(CPP_OBJS)"

compile:$(C_OBJS) $(CPP_OBJS)
	$(CC) -O2 -o $(TARGET) $^  $(LINKFLAGS) $(LIBS)

clean:
	rm -rf $(OBJ_PATH)
	rm -f $(TARGET)

install: $(TARGET)
	cp $(TARGET) $(PREFIX_BIN)

uninstall:
	rm -f $(PREFIX_BIN)/$(TARGET)
strip:
	$(STRIP) $(TARGET)
	cp $(TARGET) ~/tftpboot

rebuild: clean compile
