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



build-dir		= ../build
header			= -I../include
target-module		= my_stdio my_stdlib my_string my_csfml

$(foreach mod, $(target-module), $(eval \
	target-$(mod)-src	= $(wildcard $(mod)/*.c)$n\
	target-$(mod)-obj	= $$(patsubst %.c,$(build-dir)/lib_%.o, $$(subst \
					$(mod)/,l$(subst _,,$(mod))_,$$(target-$(mod)-src)))	$n\
	target-$(mod)-out	= $(patsubst %,lib%.a,$(subst _,,$(mod)))\
))
target-lib	= $(foreach mod, $(target-module), $(target-$(mod)-out))


define rule-target
$(patsubst %.c,$(build-dir)/lib_%.o,\
$(subst $2/,l$(subst _,,$2)_,$1)): $1 | $(build-dir)
	$$(cc) $(cflags) -c $$< -o $$@ $(header)
endef

define rule-link
$(patsubst %,lib%.a,$(subst _,,$1)): $2
	@ printf 'link lib \033[1;35m$$@\033[0m\n'
	$(ar) crs $$@ $$^
endef

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


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

clean:
	rm -rf $(build-dir)
fclean: clean
	rm -rf $(target-lib)
re: fclean all
.PHONY: re clean fclean all 
