################################################################################
#
################################################################################

#SHELL = /bin/sh


# clear implicit rules
.SUFFIXES :

#.SECONDARYEXPANSION :
.LOW_RESOLUTION_TIME :
.DELETE_ON_ERROR :
#.SECONDARY :
.NOTPARALLEL :


RM := rm -rf
MD := mkdir -p
CP := cp -f


#######################################################
# All of the sources & libraries participating in the build are defined here
-include objects.mk


############################################
# define Link Libraries based on build target  

ifeq ($(findstring .so,$(TARGETAPP)),.so)
LIBSRCHBASE = ..
else
ifeq ($(findstring .so,$(TARGETAPP)),.a)
LIBSRCHBASE = ..
else
LIBSRCHBASE = ../lib
endif
endif

ifeq ($(MAKECMDGOALS),debug)

#everything should be debug
CDEBUG 	= -g3 -DDEBUG
TARGETDIR := Debug
LINKLIBDIRS = $(patsubst %, -L$(LIBSRCHBASE)/%/$(TARGETDIR), $(LIBDIRS))
LIBS2BUILD := $(patsubst %, $(LIBSRCHBASE)/%, $(LIBDIRS))

else
ifeq ($(MAKECMDGOALS),mixdebug)
#only the current target is debug, all libs are whatever was last built
CDEBUG 	=
LINKLIBDIRS = $(patsubst %, -L$(LIBSRCHBASE)/%, $(LIBDIRS))
LIBS2BUILD := $(patsubst %, $(LIBSRCHBASE)/%, $(LIBDIRS))
TARGETDIR := Debug

else
ifeq ($(MAKECMDGOALS),mixrel)
#only the current target is debug, all libs are whatever was last built
CDEBUG 	=
LINKLIBDIRS = $(patsubst %, -L$(LIBSRCHBASE)/%, $(LIBDIRS))
LIBS2BUILD := $(patsubst %, $(LIBSRCHBASE)/%, $(LIBDIRS))
TARGETDIR := Release

else
#Everything should be built release
CDEBUG 	=
TARGETDIR := Release
LINKLIBDIRS = $(patsubst %, -L$(LIBSRCHBASE)/%/$(TARGETDIR), $(LIBDIRS))
LIBS2BUILD := $(patsubst %, $(LIBSRCHBASE)/%, $(LIBDIRS))
endif
endif
endif



TARGET=$(TARGETDIR)/$(TARGETAPP)

############################################################
## 1. if the target is lib, then tell linker to create shared lib 
# 2.  library include files that for compile params
# path is slightly different if building a library
#  product/src/appdir  vs.  product/src/lib/libdir
# build occurs from the appdir or libdir

ifeq ($(findstring .so,$(TARGETAPP)),.a)
CINCLUDE = $(patsubst %, -I../%/inc, $(LIBDIRS))
LINK := ar
LDFLAGS := -r
else
LINK = ~/datm3/toolchain/bin/armv5l-timesys-linux-gnueabi-g++
ifeq ($(findstring .so,$(TARGETAPP)),.so)
CINCLUDE = $(patsubst %, -I../%/inc, $(LIBDIRS))
LDFLAGS := -shared -o
else
CINCLUDE = $(patsubst %, -I../lib/%/inc, $(LIBDIRS))
LDFLAGS := -o
endif
endif

#not needed
# create list of source files with path.
#C_SRCS += $(patsubst %, src/%, $(SRCS_C))

#create list of object files with path.
OBJS += $(patsubst %.c, $(TARGETDIR)/%.o, $(SRCS_C))

#create list of dependency files with path.
C_DEPS += $(patsubst %.c, $(TARGETDIR)/%.d, $(SRCS_C))


CC = ~/datm3/toolchain/bin/armv5l-timesys-linux-gnueabi-gcc
CFLAGS	+= $(CINCLUDE) -I./inc $(CDEBUG) -O0 -Wall -Wstrict-prototypes -fmessage-length=0 -MMD -MF"$(@:%.o=%.d)" -MP -MT"$(@:%.o=%.d)" -o"$(@:%.d=%.o)" 

COMPILE_C = $(CC) $(CPPFLAGS) $(CFLAGS) -c "$<"


LIB2LINKIN = $(patsubst %, -l%, $(LIBS))
LIB2LINKIN += $(patsubst %, -l%, $(SYSLIBS))

################################################
# define nonexistent (phony) targets

.PHONY : debug mixdebug mixrel sublibs clean test $(LIBS2BUILD)



#######################################
# Rules and Recipits
#######################################

ifeq ($(MAKECMDGOALS),mixrel)
mixrel : test $(TARGET)
	@echo 'Mixed Release build'
endif

ifeq ($(MAKECMDGOALS),mixdebug)
mixdebug : $(TARGET)
	@echo 'Mixed Debug build'
endif

	
# if the application is being built define the rules for building 

ifneq ($(MAKECMDGOALS),test)
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),sublibs)

######### todo - add libraries to target rule. - Libs2Build with extension
# Application link rule
$(TARGET) :  $(OBJS) 
	@echo 'Linking target: $@'
ifneq ($(findstring .so,$(TARGETAPP)),.a)	
	@echo 'Invoking: Cygwin C++ Linker'
# if its a lib copy into target dir Release or Debug as appropriate, 
# also put a copy in the lib dir.
	$(LINK) $(LDFLAGS) $(TARGETAPP) $(OBJS) $(LIB2LINKIN) $(LINKLIBDIRS)
else
	@echo 'Invoking: GCC Archiver'
	$(LINK) $(LDFLAGS) $(TARGETAPP) $(OBJS)
endif	

	 @echo 'Copy target to target directory'
	-$(CP) 	$(TARGETAPP)  $(TARGETDIR)

	@echo 'Finished building target: $@'
	@echo ' '


# define rule to utilize dependency files to make objects.
$(TARGETDIR)/%.o : $(TARGETDIR)/%.d
	@echo 'Compiling file: $<'
	$(COMPILE_C)
	@echo 'Finished Compiling: $<'
	@echo ' '


# this acts a the default way to make the dependency 
$(TARGETDIR)/%.d : src/%.c
	@echo 'Preprocessing file: $<'
	$(COMPILE_C)	
	@echo 'Finished preprocessing: $<'
	@echo ' '
	
	
#include any existing dependency files 	
-include $(C_DEPS)	

# this causes a dependency file to be recreated when a required .h is missing.
%.h :
	-@echo ' '
	-@echo 'Include file $@ can not be found.'
	-@echo ' '


endif
endif
endif


ifneq ($(MAKECMDGOALS),clean)

# define a rules for making libraries as needed.

sublibs : $(LIBS2BUILD)
#	for dir in $(LIBS2BUILD); do \
#		$(MAKE) -C $$dir/Release ; \
#	done
	
#rule for making libraries
#%.so :
$(LIBS2BUILD) :
	-@echo 'MAKING libary : $@'
	@$(MAKE) -C $@/Release

endif


ifeq ($(MAKECMDGOALS),clean)

# Action phony Targets
clean: $(LIBS2BUILD)
	-@echo 'Removing intermediate and target files'
	-$(RM) Release/*.o Release/*.d Release/$(TARGETAPP) 
	-$(RM) Debug/*.o Debug/*.d Debug/$(TARGETAPP)
#	-$(MD) Release
#	-$(MD) Debug
	-@echo ' '
	
$(LIBS2BUILD) :
	-@echo 'MAKING libary : $@'
	@$(MAKE) -C $@/Release clean

endif


test :
	-@echo 'TARGETAPP = $(TARGETAPP)'	
	-@echo 'LIBS2BUILD = $(LIBS2BUILD)'
#	-@echo 'CDEBUG = $(CDEBUG)'
	-@echo 'C_DEPS = $(C_DEPS)'
	-@echo 'OBJS = $(OBJS)'
	-@echo 'TARGETDIR = $(TARGETDIR)'	
	-@echo 'CINCLUDE = $(CINCLUDE)'
	-@echo 'LINKLIBDIRS = $(LINKLIBDIRS)'
	-@echo 'LIB2LINKIN = $(LIB2LINKIN)'
	