RM_F    = @rm_f@
PERL    = @perl@
PARROT  = ../../parrot@exe@

PMCBUILD = $(PERL) @build_dir@/tools/build/dynpmc.pl
OPSBUILD = $(PERL) @build_dir@/tools/build/dynoplibs.pl
LIBPATH  = runtime
PMCDIR   = pmc
OPSDIR   = ops
O        = @o@
LOAD_EXT = @load_ext@
PARROT_DYNEXT = @build_dir@/runtime/parrot/dynext

SRM=Stack

PMCS = \
 wmlsinteger \
 wmlsfloat \
 wmlsstring \
 wmlsboolean \
 wmlsinvalid \
 wmlsbytecode

PMC_FILES = \
 $(PMCDIR)/wmlsinteger.pmc \
 $(PMCDIR)/wmlsfloat.pmc \
 $(PMCDIR)/wmlsstring.pmc \
 $(PMCDIR)/wmlsboolean.pmc \
 $(PMCDIR)/wmlsinvalid.pmc \
 $(PMCDIR)/wmlsbytecode.pmc

OPS_FILES = $(OPSDIR)/wmls.ops

OPSLIB    = wmls

PBCS = \
 $(LIBPATH)/wmlslang.pbc \
 $(LIBPATH)/wmlsfloat.pbc \
 $(LIBPATH)/wmlsstring.pbc \
 $(LIBPATH)/wmlsconsole.pbc

GEN_PIR = \
 src/opcode.pir \
 src/stdlibs.pir

all: \
 pmc/wmls_group$(LOAD_EXT) \
 ops/wmls_ops$(LOAD_EXT) \
 src/WMLScript.pbc \
 $(PBCS)

pmc/wmls_group$(LOAD_EXT) : $(PMC_FILES)
	@cd $(PMCDIR) && $(PMCBUILD) generate $(PMCS)
	@cd $(PMCDIR) && $(PMCBUILD) compile $(PMCS)
	@cd $(PMCDIR) && $(PMCBUILD) linklibs $(PMCS)
	@cd $(PMCDIR) && $(PMCBUILD) copy "--destination=$(PARROT_DYNEXT)" $(PMCS)

ops/wmls_ops$(LOAD_EXT) : $(OPS_FILES)
	@cd $(OPSDIR) && $(OPSBUILD) generate $(OPSLIB)
	@cd $(OPSDIR) && $(OPSBUILD) compile $(OPSLIB)
	@cd $(OPSDIR) && $(OPSBUILD) linklibs $(OPSLIB)
	@cd $(OPSDIR) && $(OPSBUILD) copy "--destination=$(PARROT_DYNEXT)" $(OPSLIB)

src/WMLScript.pbc: src/WMLScript.pir \
 src/script.pir src/wmlsstdlibs.pir $(GEN_PIR)
	$(PARROT) -o src/WMLScript.pbc src/WMLScript.pir

src/opcode.pir: src/translation.rules build/translator.pl \
 build/SRM/Stack.pm build/SRM/Register.pm
	$(PERL) build/translator.pl --output src/opcode.pir --srm $(SRM) \
	  src/translation.rules

src/stdlibs.pir: src/wmlslibs.cfg build/stdlibs.pl
	$(PERL) build/stdlibs.pl --output src/stdlibs.pir src/wmlslibs.cfg

$(LIBPATH)/wmlslang.pbc: $(LIBPATH)/wmlslang.pir
	$(PARROT) --output=$(LIBPATH)/wmlslang.pbc $(LIBPATH)/wmlslang.pir

$(LIBPATH)/wmlsfloat.pbc: $(LIBPATH)/wmlsfloat.pir
	$(PARROT) --output=$(LIBPATH)/wmlsfloat.pbc $(LIBPATH)/wmlsfloat.pir

$(LIBPATH)/wmlsstring.pbc: $(LIBPATH)/wmlsstring.pir
	$(PARROT) --output=$(LIBPATH)/wmlsstring.pbc $(LIBPATH)/wmlsstring.pir

$(LIBPATH)/wmlsconsole.pbc: $(LIBPATH)/wmlsconsole.pir
	$(PARROT) --output=$(LIBPATH)/wmlsconsole.pbc $(LIBPATH)/wmlsconsole.pir

help:
	@echo ""
	@echo "Following targets are available for the user:"
	@echo ""
	@echo "  all:"
	@echo "                     This is the default."
	@echo "Testing:"
	@echo "  test:              Run the test suite."
	@echo "  testclean:         Clean up test results."
	@echo ""
	@echo "Cleaning:"
	@echo "  clean:             Basic cleaning up."
	@echo "  realclean:         Removes also files generated by 'Configure.pl'"
	@echo "  distclean:         Removes also anything built, in theory"
	@echo ""
	@echo "Misc:"
	@echo "  help:              Print this help message."
	@echo ""

test:
	cd .. && $(PERL) t/harness --languages=WMLScript

TEST_CLEANERS = \
 "t/pmc/*.pir" \
 "t/*.pir" \
 "t/*.pbc" \
 "t/*.wmls" \
 "t/*.wmlsc" \
 "t/*.out"

testclean:
	$(RM_F) $(TEST_CLEANERS)

BUILD_CLEANERS = \
 $(GEN_PIR) \
 "src/*.pbc" \
 "$(PMCDIR)/*.dump" \
 "$(PMCDIR)/*.c" \
 "$(PMCDIR)/*.h" \
 "$(PMCDIR)/*$(LOAD_EXT)" \
 "$(PMCDIR)/*$(O)" \
 "$(OPSDIR)/*.c" \
 "$(OPSDIR)/*.h" \
 "$(OPSDIR)/*$(LOAD_EXT)" \
 "$(OPSDIR)/*$(O)" \
 "$(LIBPATH)/*.pbc"

clean:
	$(RM_F) $(BUILD_CLEANERS) $(TEST_CLEANERS)

realclean: clean
	$(RM_F) Makefile

distclean: realclean

