#!/usr/bin/make -f
# ---
#	Project: my_runner
#	Author: yann.magnin@epitech.eu
# ---
include global.mk


# function
# $1 module name
# $2 file
define rule-module
build/$1_$(patsubst %.c,%,$2).o: src/$1/$2	
	$(cc) $(cflags) -c $$< -o $$@ $(header)
endef

config		= gcc.cfg
name		= my_runner
header		= -Iinclude/
module		= core draw game memory menu text kinematic
build-dir	= build
$(foreach mod,$(module), $(eval \
	mod-$(mod)-src	= $(notdir $(wildcard src/$(mod)/*.c))		$n\
	mod-$(mod)-obj	= $$(patsubst %.c,build/$(mod)_%.o,$$(mod-$(mod)-src)) \
))
target-obj	= $(foreach mod, $(module), $(mod-$(mod)-obj))




# Chek configuration file
ifeq ($(wildcard $(config)),)
$(warning "Configuration file missing. skipping some compilation flags.")
cflags	+= -g0
else
cflags	+= @$(config)
endif




all: check_lib $(name)
$(target-obj): | $(build-dir)

$(name): $(target-obj)
	@ printf 'link binary $@\n'
	$(cc) $(cflags) -o $@ $(target-obj) $(header) $(lib-link) -lgcc

$(build-dir):
	@ printf 'create build folder:\033[1;35m $@\033[0m\n'
	mkdir -p $@

check_lib:
	make -C lib/

$(foreach mod, $(module), \
	$(foreach source,$(mod-$(mod)-src), $(eval \
		$(call rule-module,$(mod),$(source)))) \
)





##
## Clean rule
##
clean:
	rm -rf $(name)
	make clean -C lib/
fclean: clean
	rm -rf build
	make fclean -C lib/
re: fclean all


.PHONY: clean re fclean
