#!/usr/bin/make -f
#******************************************************************************#
#                                        _____           _                     #
#   Makefile                            |_   _|__  _   _| |__   ___ _   _      #
#   | Project : TuQuoque                  | |/ _ \| | | | '_ \ / _ \ | | |     #
#                                         | | (_) | |_| | | | |  __/ |_| |     #
#   By: thomas <thomas@touhey.fr>         |_|\___/ \__,_|_| |_|\___|\__, |.fr  #
#   Last updated: 2015/12/23 10:02:49                               |___/      #
#                                                                              #
#******************************************************************************#
# INCLUDE CONFIGURATION AND VARS
include $(CURDIR)/Makefile.cfg
include $(CURDIR)/Makefile.vars

# DEDUCED VARS (with spaces so it doesn't appear in zsh autocompletition)
 ALLOBJ = $(SRC:%=$(OBJDIR)/main/%.o) $(IMG:%=$(OBJDIR)/img/%.o) \
	$(foreach map,$(MAP),$(OBJDIR)/map/$(map).o $(OBJDIR)/map_e/$(map).o)

# TARGETS
## General
## - Make everything means make the project (default)
all: $(NAME).g1a

## Library-related
## - Make a library
define make-lib-rule
$(LIBDIR)/$1/lib$1.a:
	cd $(LIBDIR)/$1 && [ -f ./configure ] && ./configure || return 0
	$(MAKE) $(LIBDIR)/$1
endef
$(foreach lib,$(LLIB), \
$(eval $(call make-lib-rule,$(lib))))

## - Clean a library
define make-cleanlib-rule
clean-lib$1:
	$(MAKE) $(LIBDIR)/$1 distclean
endef
$(foreach lib,$(LLIB), \
$(eval $(call make-cleanlib-rule,$(lib))))

## Binary-related
## - Make the image include file
$(INCDIR)/main/img.h: $(IMG:%=$(IMGDIR)/%.bmp)
	$(TOOLSDIR)/make-img-include-file.sh $(IMG) >$@

## - Make the maps include file
$(INCDIR)/main/maps.h: $(MAP:%=$(MAPDIR)/%.map)
	$(TOOLSDIR)/make-maps-include-file.sh $(MAP) >$@

## - Make the maps list source file
$(SRCDIR)/maplist.c: $(MAP:%=$(MAPDIR)/%.map)
	$(TOOLSDIR)/make-maps-list-file.sh $(MAP) >$@

## - Make the object directory
$(OBJDIR)/main $(OBJDIR)/img $(OBJDIR)/map $(OBJDIR)/map_e:
	$(MD) $@

## - Make an object file out of a ASM source file
$(OBJDIR)/main/%.o: $(SRCDIR)/%.s | $(OBJDIR)/main
	$(AS) -c -o $@ $<

## - Make an object file out of a C source file
$(OBJDIR)/main/%.o: $(SRCDIR)/%.c $(INC:%=$(INCDIR)/%.h) | $(OBJDIR)/main
	$(CC) -c -o $@ $< $(CFLAGS)

## - Make an object file of a raw image
$(OBJDIR)/img/%.o: $(IMGDIR)/%.bmp | $(OBJDIR)/img
	$(FXCONV) -o $@ -n img_$* $<

## - Make an object file of a map file
$(OBJDIR)/map/%.o $(OBJDIR)/map_e/%.o: $(MAPDIR)/%.map | $(OBJDIR)/map $(OBJDIR)/map_e
	$(TOOLSDIR)/make-level-object.py -o $(OBJDIR)/map/$*.o -eo $(OBJDIR)/map_e/$*.o -n map_$* $<

## - Make the ELF file
$(NAME).elf: $(foreach lib,$(LLIB),$(LIBDIR)/$(lib)/lib$(lib).a) $(ALLOBJ)
	$(LD) -o $@ $(ALLOBJ) $(LDFLAGS)

## - Make the BIN file
$(NAME).bin: $(NAME).elf
	$(OBJCOPY) -R .comment -R .bss -R '$$iop' -O binary $< $@

## - Make the project
$(NAME).g1a: $(NAME).bin
	$(WR) $< -o $@ $(WRFLAGS)

## - Remove the object files
clean:
	$(RM) $(ALLOBJ)
	$(RM) $(INCDIR)/main/img.h $(INCDIR)/main/maps.h
	$(RM) $(SRCDIR)/maplist.c
	$(RM) $(NAME).elf $(NAME).bin

## - Clean the object files and the binary
fclean: clean
	$(RM) $(NAME).g1a

## - Remake the project
re: fclean all

## - Clean with libs
clean-lib: clean $(LLIB:%=clean-lib%)

## - Fclean with libs
fclean-lib: fclean $(LLIB:%=clean-lib%)

## - Remake the project with libs
re-lib: fclean-lib all

## Transfer to calculator
send: | $(NAME).g1a
	@if ! $(SENDER) SEND $(NAME).g1a $(NAME).g1a fls0 1>/dev/null; then \
		tput bold; tput setaf 1; \
		echo "Calculator not plugged/in receive mode !" >&2; \
		tput sgr0; false; \
	fi

## Doz rulz are phunny
.PHONY: all $(LLIB:%=clean-lib%) clean fclean re
.PHONY: clean-lib fclean-lib re-lib send

# END OF FILE
