CONFIG ?= release
BUILD_DIR := ./build/${CONFIG}
JUCE_ROOT := juce/JUCE-3.1.1
JUCE_LIB := juce/build/${CONFIG}/libjuce.a
	
GPP_SRCS := gui.cpp Rbm.cpp Layer.cpp Stack.cpp MainComponent.cpp RbmComponent.cpp DrawComponent.cpp
GPP_OBJS := $(addprefix ${BUILD_DIR}/, $(subst .cpp,.o,$(GPP_SRCS)))
GCC_SRCS := noise.c
GCC_OBJS := $(addprefix ${BUILD_DIR}/, $(subst .c,.o,$(GCC_SRCS)))

TEST_SRCS := source/main.cpp source/Rbm.cpp source/Layer.cpp source/Stack.cpp source/noise.c
	
CXXFLAGS += -std=c++11 -L juce/build/${CONFIG}
CXXFLAGS_debug := ${CXXFLAGS} -O0 -g
CXXFLAGS_release := ${CXXFLAGS} -O2

LFLAGS := -L juce/build/${CONFIG}
LFLAGS_release := ${LFLAGS}
LFLAGS_debug := ${LFLAGS}
	
CFLAGS := 
CFLAGS_debug := ${CFLAGS} -O0 -g
CFLAGS_release := ${CFLAGS} -O2

INCLUDES := -I${JUCE_ROOT} -Ijuce
LIBS := -lstdc++ -lm -larmadillo -ljsoncpp -ljuce -lfreetype -lpthread -ldl -lrt -lX11 -lGL -lXinerama -lXext
DEFINES := -DARMA_OPENMP_THREADS=1
	
all: ${JUCE_LIB} ${BUILD_DIR}/rbm.elf
test: ${BUILD_DIR}/test.elf

install: ${BUILD_DIR}/rbm.elf
	@cp $< ${HOME}/bin
	@echo "Installed to" $<
	
${GPP_OBJS}: $(BUILD_DIR)/%.o : source/%.cpp
	@mkdir -p $(dir $@)
	g++ ${CXXFLAGS_${CONFIG}} ${DEFINES} ${INCLUDES} -o $@ -c $<

${GCC_OBJS}: $(BUILD_DIR)/%.o : source/%.c
	@mkdir -p $(dir $@)
	gcc ${CFLAGS_${CONFIG}} ${DEFINES} ${INCLUDES} -o $@ -c $<

${BUILD_DIR}/rbm.elf: ${GPP_OBJS} ${GCC_OBJS} juce/build/${CONFIG}/libjuce.a
	gcc ${LFLAGS_${CONFIG}} ${GPP_OBJS} ${GCC_OBJS} ${LIBS} -o $@

${BUILD_DIR}/test.elf: ${TEST_SRCS}
	@mkdir -p $(dir $@)
	g++ ${CXXFLAGS_${CONFIG}} ${DEFINES} -o $@ ${TEST_SRCS} -larmadillo -ljsoncpp

${JUCE_LIB}: 
	make -C juce
clean:
	rm -rf ${BUILD_DIR}