RM_F    = @rm_f@
PERL    = @perl@
PARROT  = ../../parrot@exe@
PBC_MERGE = ../../pbc_merge@exe@
PGE      = $(PARROT) ../../compilers/pge/pgc.pir
TGE      = $(PARROT) ../../compilers/tge/tgc.pir

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

PMCS = \
 lua \
 luaany \
 luaboolean \
 luaclosure \
 luafunction \
 luanil \
 luanumber \
 luastring \
 luatable \
 luathread \
 luauserdata

PMC_FILES = \
 $(PMCDIR)/lua.pmc \
 $(PMCDIR)/luaany.pmc \
 $(PMCDIR)/luaboolean.pmc \
 $(PMCDIR)/luaclosure.pmc \
 $(PMCDIR)/luafunction.pmc \
 $(PMCDIR)/luanil.pmc \
 $(PMCDIR)/luanumber.pmc \
 $(PMCDIR)/luastring.pmc \
 $(PMCDIR)/luatable.pmc \
 $(PMCDIR)/luathread.pmc \
 $(PMCDIR)/luauserdata.pmc

LIB_PBCS = \
 $(LIBPATH)/luaperl.pbc \
 $(LIBPATH)/luaaux.pbc \
 $(LIBPATH)/luabasic.pbc \
 $(LIBPATH)/luacoroutine.pbc \
 $(LIBPATH)/luapackage.pbc \
 $(LIBPATH)/luaregex.pbc \
 $(LIBPATH)/luastring.pbc \
 $(LIBPATH)/luatable.pbc \
 $(LIBPATH)/luamath.pbc \
 $(LIBPATH)/luaio.pbc \
 $(LIBPATH)/luaos.pbc \
 $(LIBPATH)/luadebug.pbc

GEN_PIR = \
 src/lua51_testlex_gen.pir \
 src/dumplex_gen.pir \
 src/lua_grammar_gen.pir \
 src/ASTGrammar.pir

GEN_PBC = \
 src/Lua51TestLex.pbc \
 src/LuaDumpLex.pbc \
 src/lua.pbc \
 luac.pbc

all: \
 pmc/lua_group$(LOAD_EXT) \
 $(LIB_PBCS) \
 $(GEN_PBC) \
 Lua/parser.pm


src/lua51_testlex_gen.pir: src/lua51_testlex.pg
	$(PGE) --output=src/lua51_testlex_gen.pir src/lua51_testlex.pg

src/Lua51TestLex.pbc: src/lua51_testlex_gen.pir
	$(PARROT) -o src/Lua51TestLex.pbc --output-pbc src/lua51_testlex_gen.pir

src/dumplex_gen.pir: src/dumplex.tg
	$(TGE) --output=src/dumplex_gen.pir src/dumplex.tg

src/LuaDumpLex.pbc: src/dumplex_gen.pir
	$(PARROT) -o src/LuaDumpLex.pbc --output-pbc src/dumplex_gen.pir

src/lua_grammar_gen.pir: src/lua.pg
	$(PGE) --output=src/lua_grammar_gen.pir src/lua.pg

src/ASTGrammar.pir: src/ASTGrammar.tg
	$(TGE) --output=src/ASTGrammar.pir src/ASTGrammar.tg

src/lua.pbc: src/lua.pir src/lua_grammar_gen.pir src/ASTGrammar.pir
	$(PARROT) -o src/lua.pbc --output-pbc src/lua.pir

luac.pbc: luac.pir
	$(PARROT) -o luac.pbc --output-pbc luac.pir


pmc/lua_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)

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

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

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

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

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

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

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

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

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

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

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

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

Lua/parser.pm: Lua/lua51.yp
	yapp -s -v -m Lua::parser -o Lua/parser.pm Lua/lua51.yp

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=lua

test2:
	cd .. && cd lua2 && $(PERL) -I../../lib t/harness


TEST_CLEANERS = \
 "t/pmc/*.pir" \
 "t/*.pir" \
 "t/*.lua" \
 "t/*.orig_out" \
 "t/*.parrot_out"

testclean:
	$(RM_F) $(TEST_CLEANERS)

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

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

realclean: clean
	$(RM_F) Makefile

distclean: realclean

