include Makeconf

# Auto tracking of headers
CXX_FLAGS += -MMD
depfiles := $(wildcard *.d **/*.d)

clean-objs := $(TARGETS) $(foreach t,$(TARGETS),$(modules_$(t))) $(depfiles)
cmd = $(VERBOSE)g++ $(CXX_FLAGS) -o $@ $^
msg = $(or $(verb_$@),finalize) $@

ifneq ($V,1)
VERBOSE := @
endif

.PHONY: all test clean

all: $(or $(DEFAULT_TARGETS),$(TARGETS))

-include $(depfiles)

clean:
	rm -rf $(clean-objs)

test: vgbc.test
	@echo " ... run test $<"
	$(VERBOSE)./$< $(if $(V),-s)

%.o: %.cpp
	@echo " ... build $@"
	$(VERBOSE)g++ $(CXX_FLAGS) -c -o $@ $<

define target_rule
$(1): $$(modules_$(1))
	@echo " ..." $$(or $$(msg_$(1)),$$(msg))
	$$(VERBOSE)$$(or $$(cmd_$(1)),$$(cmd))
endef

$(foreach t,$(TARGETS),$(eval $(call target_rule,$(t))))