# rename these sources so that they do not get included
#      ./src/blfwk/src/hid-mac.c.orig
#      ./src/blfwk/src/UsbHidPeripheral.cpp.orig
#      ./src/blfwk/src/hid-windows.c.orig
#      ./src/blfwk/src/SimulatorMemory.cpp.orig
#      ./src/blfwk/src/SimPacketizer.cpp.orig
#      ./src/blfwk/src/UsbHidPacketizer.cpp.orig
#      ./src/blfwk/src/Simulator.cpp.orig
#      ./src/blfwk/src/hid-linux.c.orig

# set to nothing starting out
dependencies :=

#CFLAGS_ARC := -mfpu=neon -mfloat-abi=softfp

BUILD_DIR := build-$(CPU_PREFIX)

#PATH := $(SYSROOT_DIR)/bin:$(PATH)

CXX  := $(CROSS_COMPILE)g++
CC   := $(CROSS_COMPILE)gcc
AR   := $(CROSS_COMPILE)ar
LD   := $(CROSS_COMPILE)ld
NM   := $(CROSS_COMPILE)nm
AS   := $(CROSS_COMPILE)as
RANLIB := $(CROSS_COMPILE)ranlib
STRIP  := $(CROSS_COMPILE)strip

CFLAGS_COMMON := \
	-Wall -Wextra -Wpointer-arith -Wconversion \
	-fno-strict-aliasing -pthread \
	-fPIC -Os

#	-g \
#	-DDEBUG \

CFLAGS := \
	$(CFLAGS_COMMON) \
	$(CFLAGS_ARC)

CXXFLAGS := \
	$(CFLAGS) \
	-std=c++11

INCLUDES_COMMON := -I $(SYSROOT_DIR)/include
LIBRARIES_COMMON := -L $(SYSROOT_DIR)/lib

################################################################################
# The main targets
################################################################################
.PHONY: help
help:
	@echo "Targets:"
	@echo "    all"
	@echo "    clean"

.PHONY : all
all: blhost

.PHONY: clean
clean:
	-@rm $(BUILD_DIR)/*

################################################################################
# FUNCTION: create-dir
#
# PURPOSE: if directory doesn't exist then create it
#
# INPUTS:
#  - directory: path/to/directory
#
# SEE:
#  Mecklenburg's "Managing Projects with GNU Make" for more info
#
# USAGE:
#    $(call create-dir,directory)
define create-dir
  dummy-var := $(shell mkdir -p $1)
endef

################################################################################
# genRules
#
# PURPOSE: This function dynamically creates makefile targets for subfolders.
#          It saves typing and reduces the chance of copy/paste errors.
#
# INPUTS:
#  - dir: path/to/src-directory
#  - dir: path/to/build-directory
#  - flags: space-delimited list of compiler flags (e.g. -D... -I....)
#  - objs: space-delimited list of object files
#
# DOES THE FOLLOWING:
#  - accumulates dependency *.d files
#  - creates pattern rule to build objects, using the provided compiler flags
#  - creates the output directory if necessary
#
# SEE:
#  - GNU makefile manual for 'eval' and 'call' functions
#
# USAGE:
#    $(call genRules,srcDir,destDir,flags,objects)
define genRules

  # build list of all dependencies
  dependencies += $(subst .o,.d,$4)

  # build output directory.  Yes we're constantly overwriting this variable!
  $(call create-dir,$2)

  # create pattern rule for building object files and depend makefiles
  $2/%.o: $1/%.cpp
	$$(CXX) $(CXXFLAGS) $3 -c $$< -o $$@
	$$(CXX) -M  \
		-MF $$(subst .o,.d,$$@) \
		-MP \
		-MG \
		-MT $$@ \
		$(CXXFLAGS) $3 $$<

  $2/%.e: $1/%.cpp
	$$(CXX) $(CXXFLAGS) $3 -E $$< -o $$@

  $2/%.s: $1/%.cpp
	$$(CXX) $(CXXFLAGS) $3 -Wa,-adlh -c $$< > $$@

  $2/%.o: $1/%.c
	$$(CC) $(CFLAGS) $3 -c $$< -o $$@
	$$(CXX) -M  \
		-MF $$(subst .o,.d,$$@) \
		-MP \
		-MG \
		-MT $$@ \
		$(CFLAGS) $3 $$<

  $2/%.e: $1/%.c
	$$(CC) $(CFLAGS) $3 -E $$< -o $$@

  $2/%.s: $1/%.c
	$$(CC) $(CFLAGS) $3 -Wa,-adlh -c $$< > $$@
endef

################################################################################
# objsFromCpps
#
# PURPOSE: Given a path to some *.cpp files, creates a list of object files.
#
# INPUTS:
#  - dir: path/to/src-directory
#  - dir: path/to/build-directory
#
# USAGE:
#    $(call objsFromCpps,path/to/srcFiles,path/to/objFiles)
define objsFromCpps
  $(patsubst $1/%.cpp,$2/%.o,$(wildcard $1/*.cpp)) $(patsubst $1/%.c,$2/%.o,$(wildcard $1/*.c))
endef

###############################################################################
BLHOST_OBJS := \
	$(call objsFromCpps,validation/blhost/src,$(BUILD_DIR)) \
	$(call objsFromCpps,src/blfwk/src,$(BUILD_DIR)) \
	$(call objsFromCpps,src/crc/src,$(BUILD_DIR)) \

BLHOST_INCLUDE := \
	$(INCLUDES_COMMON) \
	-I src \
	-I src/include \
	-I src/blfwk \
	-I src/sbloader \
	-I src/bootloader \
	-I src/crc \
	-I src/packet \
	-I src/property \
	-I src/drivers/common \
	-I src/bm_usb \

# CFLAGS and CXXFLAGS added via genRules
BLHOST_FLAGS := \
	$(BLHOST_INCLUDE) \
	-D LINUX -D BOOTLOADER_HOST \

$(eval $(call genRules,validation/blhost/src,$(BUILD_DIR),$(BLHOST_FLAGS),$(BLHOST_OBJS)))
$(eval $(call genRules,src/blfwk/src,$(BUILD_DIR),$(BLHOST_FLAGS),$(BLHOST_OBJS)))
$(eval $(call genRules,src/crc/src,$(BUILD_DIR),$(BLHOST_FLAGS),$(BLHOST_OBJS)))

BLHOST_LIBS := \

.PHONY: blhost
blhost: $(BUILD_DIR)/blhost

$(BUILD_DIR)/blhost : $(BLHOST_OBJS) $(BLHOST_LIBS)
	$(CXX) 	$(BLHOST_FLAGS) -o $@ $^
	$(STRIP) $@

.PHONY: blhost-clean
blhost-clean:
	-@rm -rf $(BUILD_DIR)

################################################################################
# automatically generated dependencies
# Note, we don't want to run the dependencies for the "clean" or "help" targets.
ifneq "$(MAKECMDGOALS)" "clean"
 ifneq "$(MAKECMDGOALS)" "help"
  # Default target ("") is "help",
  ifneq "$(MAKECMDGOALS)" ""
  -include $(dependencies)
  endif
 endif
endif

