define target_assignments
 ifneq "$$(strip $$($(1)_$(2)_TARGET))" ""
  $(1)_$(2)_OBJECT_FILES := $$(addprefix $$($(1)_DIR),$$($(1)_$(2)_OBJECTS))
 endif
endef

define indexed_bin_target
$(call target_assignments,$(1),BIN$(2))
endef

define template

$(1)_TARGET_DIR ?= $$($(1)_DIR)
$(1)_TARGET_NUMBERS ?= 1 2 3
$(foreach index,$$($(1)_TARGET_NUMBERS),$(call indexed_bin_target,$(1),$(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 $*)
