###############################################################################
#                                                                             #
#  Makefile for building LIB                                                  #
#                                                                             #
###############################################################################

ROOT_DIR = .

#
# Includes
#
include $(ROOT_DIR)/env/settings.mk


#
# Additional compiler Settings for c
#	
C_OPTS +=   -D=__COMPILER_TI__ \
			-D=__GENERIC_ARM_ARMV7__ \
			-D=__SERIES_TM4C__ \
			-D=__CUSTOM_DATATYPE_DEFINITIONS__
	
#
# Additional compiler Settings for c++
#
CPP_OPTS += -D=__CC_ARM



#
# directories
#
# build directory
BUILD_DIR     = $(ROOT_DIR)\build
BUILD_SDIR    = $(subst \,/,$(BUILD_DIR))
# environment directory
ENV_DIR       = $(ROOT_DIR)\env
ENV_SDIR      = $(subst \,/,$(ENV_DIR))

# Temporal linker command file
LINK_TMP_FILE = $(BUILD_SDIR)/link_tmp.cmd



#
# C Files allgemein
#
SRCS = $(wildcard $(ROOT_DIR)/src/*.c)
SRCS += $(wildcard $(ROOT_DIR)/src/class/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Controller/Common/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Controller/Services/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Controller/Driver/Serial/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Controller/Protocol/LNet/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Library/Control/Controller/src/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Library/Math/Controller/src/*.c)
SRCS += $(wildcard $(ROOT_DIR)/lib/X2C/Library/General/Controller/src/*.c)
SRCS += $(wildcard $(ROOT_DIR)/src/X2CCode/*.c)
OBJS := $(patsubst %.c, %.obj, $(SRCS))

#
# C++ Files allgemein
#
SRCCPS = $(wildcard $(ROOT_DIR)/src/class/*.cpp)
OBJS += $(patsubst %.cpp, %.objcpp, $(SRCCPS))


#
# C++ Files allgemein
#


#
# Include Files for C and C++ Applications
#
C_INCDIRS  = -I "$(ROOT_DIR)\inc"
C_INCDIRS += -I "$(ROOT_DIR)\inc\incPAL"
C_INCDIRS += -I "$(ROOT_DIR)\src\X2CCode"
C_INCDIRS += -I "$(ROOT_DIR)\inc\class"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Controller\Common"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Controller\Services"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Controller\Driver\Serial"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Controller\Protocol\LNet"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Library\Control\Controller\inc"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Library\General\Controller\inc"
C_INCDIRS += -I "$(ROOT_DIR)\lib\X2C\Library\Math\Controller\inc"
C_INCDIRS += -I "$(ROOT_DIR)\lib"


# Copy includes for c++
CPP_INCDIRS := $(C_INCDIRS)


#
# c-Compiler specific includes
#
C_INCDIRS += -I "$(C_COMP_PATH)\..\include"


#
# c++-Compiler specific includes
#
CPP_INCDIRS += -I "$(CPP_COMP_PATH)\..\include" 



# Create Folders if not exist
$(shell mkdir "$(BUILD_SDIR)")


#$(info --------------------------------------)
#$(info $(OBJS))
#$(info --------------------------------------)


all : $(BUILD_SDIR)/app.out

code : $(BUILD_SDIR)/app.out

$(BUILD_SDIR)/app.out : $(OBJS)
	$(shell rm -rf $(LINK_TMP_FILE))
	$(foreach dir,$(OBJS),$(shell echo $(dir) >> $(LINK_TMP_FILE)))
	@echo linking $@
	@echo -o $@                                                 >> $(LINK_TMP_FILE)
	@echo -l $(FPU_LIB)                                         >> $(LINK_TMP_FILE)
	@echo $(LINK_CMDFILE)                                       >> $(LINK_TMP_FILE)
	@echo -m $(BUILD_SDIR)/app.map                              >> $(LINK_TMP_FILE)
	@echo $(L_OPTS)                                             >> $(LINK_TMP_FILE)
	@"$(LINK)" --run_linker $(LINK_TMP_FILE)
	@"$(HEX)" --quiet --intel --romwidth=32 --fill=0xFFFFFFFF $(BUILD_SDIR)/app.out -o $(BUILD_SDIR)/app.hex


# build c files
%.obj: %.c
	@echo compiling c file: $<
	@"$(CC)" $< $(C_INCDIRS) $(C_OPTS) --output_file $@ --asm_directory $(BUILD_SDIR) --obj_directory $(BUILD_SDIR)


# build c files
%.objcpp: %.cpp
	@echo compiling c++ file: $<
	@echo $(CCP)
	@"$(CCP)" $< $(CPP_INCDIRS) $(CPP_OPTS) --output_file $@ --asm_directory $(BUILD_SDIR) --obj_directory $(BUILD_SDIR)



.PHONY: clean
clean :
	@echo cleaning up application module files
	@del /F /Q $(BUILD_DIR)\*.*
	$(foreach dir,$(OBJS),$(shell rm -rf $(dir)))
	-rm -rf $(BUILD_SDIR)
	@echo done.