# Top-level Makefile

# Configuration and build smarts
include Makefile.common

# Now for the image definition
IMAGE = $(MCU)-main.bin

all: .toolchain version $(IMAGE)

.PHONY: clean distclean version dep

# Include library target
include libs/$(MCU).mk
include src/build.mk

# Bookeeping
VERSION_H = inc/version.h
VERSION = `./git-version-gen /dev/null`
DEPFILE = .depend

# Set up the libraries we need
LIBRARIES = $(MCU_LIBS)
LIBS_OBJS = $(MCU_LIBS_OBJS)
PROGRAM_LIBS = src/core.a 

# Generic compilation rules
$(MCU).ld: libs/$(MCU).ld.in inc/$(MCU)_hw.h
	@$(E) "  GENERATE " $@
	$(Q)$(CPP) $(TARGET_LDCPPFLAGS) -include inc/$(MCU)_hw.h libs/$(MCU).ld.in -P -o $@

%.bin: %.elf
	@$(E) "  OBJCOPY  " $@
	$(Q)$(OBJCOPY) -O binary $(<:.bin=.elf) --remove-section .note.gnu.build-id $@ 

%.o: %.c
	@$(E) "  CC " $<
	$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -c $(<:.o=.c) -o $@

%.elf: $(MCU).ld $(LIBRARIES) $(PROGRAM_LIBS)
	@$(E) "  LINK     " $@
	$(Q)$(CC) -o $@ $(LDFLAGS) \
		-Wl,--gc-sections,-T$(MCU).ld \
		-Wl,--whole-archive $(PROGRAM_LIBS) \
		-Wl,--no-whole-archive $(LIBRARIES) \
		-Wl,--build-id=none \
		-flto=$(JLEVEL)
# -Wl,-allow-multiple-definition

# Cleaning up..
clean:
	@$(E) "  CLEAN    "
	$(Q)${RM} -f $(SRC_OBJS) libs/*.o src/*.a $(MCU).ld

distclean: clean
	@$(E) "  DISTCLEAN"
	$(Q)${RM} -f $(DEPFILE) $(VERSION_H)
	$(Q)${RM} -f $(LIBS_OBJS) $(LIBRARIES)
	$(Q)${RM} -Rf $(TOOLCHAIN_SRC_DIR)/.build
	$(Q)if [ -r ${TOOLCHAIN_SRC_DIR}/Makefile ] ; then \
		cd ${TOOLCHAIN_SRC_DIR} ; \
		MAKELEVEL='' make clean ; \
		MAKELEVEL='' make distclean ; \
	fi
	@${RM} -f .toolchain-$(MCU_CORE)
	$(Q)${RM} -f tags TAGS

# Toolchain
.toolchain: .toolchain-$(MCU_CORE)

.toolchain-$(MCU_CORE):
	$(Q)if [ ! -x ${CROSS_COMPILE}gcc ] ; then  \
		( unset AS LD LDD CC CPP AR NM STRIP STRIPTOOL OBJCOPY OBJDUMP RANLIB LD_LIBRARY_PATH MAKELEVEL ; \
		if [ -r ${TOOLCHAIN_CONFIG} ] ; then \
			cp ${TOOLCHAIN_CONFIG} ${TOOLCHAIN_SRC_DIR}/.config ; \
		else \
			${ECHO} "${TOOLCHAIN_CONFIG} missing!  Exiting." ; \
			exit 1 ;\
		fi ; \
		${RM} -Rf ${TOOLCHAIN_SRC_DIR}/.build ; \
		cd ${TOOLCHAIN_SRC_DIR} ; \
		./configure --enable-local && \
		make && \
		./ct-ng build ; \
		cd - ) ; \
	fi 
	@if [ ! -x ${CROSS_COMPILE}gcc ] ; then \
		${ECHO} "Toolchain Build failed!" ; \
		exit 1 ;\
	fi
	@touch .toolchain-$(MCU_CORE)

toolclean:
	$(Q)if [ -d ${CROSS_DIR} ] ; then \
		chmod -R +w ${CROSS_DIR} ; \
		${RM} -fr ${CROSS_DIR} ; \
	fi
	@${RM} -f .toolchain-$(MCU_CORE)

tags:
	find . 	-path '*crosstool*' -prune \
		-o -name '*.[CHch]' -type f -print \
		| etags -


dep: .toolchain-$(MCU_CORE)
	$(Q)rm -f $(DEPFILE)
	$(Q)make $(DEPFILE)

version:
#	@$(E) " VERSION " $(VERSION_H)
	$(Q)./git-version-gen $(VERSION_H) $(APP) > /dev/null

dep: $(DEPFILE)

$(DEPFILE): #$(SRC_OBJS:.o=.c) $(LIBS_OBJS:.o=.c)
	@$(E) "  DEP "
	@echo > $(DEPFILE)
	@for i in $(SRC_OBJS) $(LIBS_OBJS) ; do \
	  c="$${i%.o}.c"; l="$${i%.o}.lo"; \
	  $(CC) $(CFLAGS) $(CPPFLAGS) -MM $$c | \
	  sed "s!^.*\.o:!$$i $$l:!" >> $(DEPFILE) ; \
	  done

sinclude $(DEPFILE)
