define target_assignments
 ifneq "$$(strip $$($(1)_$(2)_TARGET))" ""
  $(1)_$(2)_OBJECT_FILES := $$(addprefix $$($(2)_DIR),$$($(1)_$(2)_OBJECTS))
 endif
endef

define template

$(1)_TARGET_DIR ?= $($(1)_DIR)
$(1)_TARGET_NUMBERS ?= 3
$(call target_assignments,$(1),BIN1)
$(foreach index,2,$(call target_assignments,$(1),BIN$(index)))
$(foreach index,$($(1)_TARGET_NUMBERS),$(call target_assignments,$(1),BIN$(index)))

.PHONY: expansion
expansion:
	@ cat Makefile > .tmp.mak
	@ echo '$$$$(info $$$$(call template,$(1)))' >> .tmp.mak
	@ echo 'nada:' >> .tmp.mak
	@ $$(MAKE) -f .tmp.mak nada
	@ rm -f .tmp.mak

endef

SAMPLE_DIR = sampledir/

SAMPLE_BIN1_TARGET = sample1
SAMPLE_BIN1_OBJECTS = sample1.o

SAMPLE_BIN2_TARGET = sample2
SAMPLE_BIN2_OBJECTS = sample2.o

SAMPLE_BIN3_TARGET = sample3
SAMPLE_BIN3_OBJECTS = sample3.o

$(eval $(call template,SAMPLE))

.PHONY: print-%
print-%:
	@echo $* = $($*)
	@echo $* origin is $(origin $*)
