# $Id: root.in 17096 2007-02-20 20:20:48Z paultcochrane $

# Setup some commands
LN_S     = @lns@
PERL     = @perl@
MV       = $(PERL) -MExtUtils::Command -e mv
RM_F     = @rm_f@
RM_RF    = @rm_rf@
PARROT   = ../../parrot@exe@
BUILD    = $(PERL) @build_dir@/tools/build/dynpmc.pl
TOOL_DIR = ../..
PGE_DIR  = ../../compilers/pge
TGE_DIR  = ../../compilers/tge
CAT      = $(PERL) -MExtUtils::Command -e cat
CP       = @cp@
O        = @o@
LOAD_EXT = @load_ext@
PARROT_DYNEXT = @build_dir@/runtime/parrot/dynext

PMCDIR   = src/pmc

# the default target
all: gen_tests cardinal.pbc $(PMCDIR)/ruby_group$(LOAD_EXT)

PMCS = \
  rubyclass \
  rubyobject \
  rubyinteger \
  rubyhash \
  rubybignum \
  rubycomplex \
  rubyfixnum \
  rubyfloat \
  rubystring \
  rubyrange

PMC_SOURCES = \
  $(PMCDIR)/rubyclass.pmc \
  $(PMCDIR)/rubyobject.pmc \
  $(PMCDIR)/rubyinteger.pmc \
  $(PMCDIR)/rubyhash.pmc \
  $(PMCDIR)/rubybignum.pmc \
  $(PMCDIR)/rubycomplex.pmc \
  $(PMCDIR)/rubyfixnum.pmc \
  $(PMCDIR)/rubyfloat.pmc \
  $(PMCDIR)/rubystring.pmc \
  $(PMCDIR)/rubyrange.pmc

BUILTINS_PIR = \
  src/builtins/assign.pir \
  src/builtins/cmp.pir \
  src/builtins/io.pir \
  src/builtins/ranges.pir

GEN_PIR = \
  src/cardinal_grammar_gen.pir \
  src/ASTGrammar.pir \
  src/OSTGrammar.pir \
  src/PIRGrammar.pir \
  src/builtins_gen.pir

GEN_PBC = \
  src/CardinalGrammar.pbc \
  src/PGE2AST.pbc \
  src/AST2OST.pbc \
  src/OST2PIR.pbc \
  src/PAST.pbc \
  src/POST.pbc

RUNTIME_COMPONENTS = \
  runtime/parts/rubyclass.pir

cardinal.pbc: cardinal.pir runtime/cardinallib.pbc $(GEN_PIR) $(GEN_PBC)
	$(PARROT) -o cardinal.pbc --output-pbc cardinal.pir 

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

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

src/cardinal_grammar_gen.pir: src/cardinal_rules.pg src/cardinal_optok.pg
	$(PARROT) $(PGE_DIR)/pgc.pir --output=src/cardinal_grammar_gen.pir src/cardinal_rules.pg src/cardinal_optok.pg

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

src/CardinalGrammar.pbc: src/CardinalGrammar.pir src/cardinal_grammar_gen.pir
	$(PARROT) -o src/CardinalGrammar.pbc --output-pbc src/CardinalGrammar.pir 

src/ASTGrammar.pir: src/ASTGrammar.tg
	$(PARROT) $(TGE_DIR)/tgc.pir --output=src/ASTGrammar.pir src/ASTGrammar.tg

src/PGE2AST.pbc: src/PGE2AST.pir src/ASTGrammar.pir src/preamble
	$(PARROT) -o src/PGE2AST.pbc --output-pbc src/PGE2AST.pir 

src/OSTGrammar.pir: src/OSTGrammar.tg
	$(PARROT) $(TGE_DIR)/tgc.pir --output=src/OSTGrammar.pir src/OSTGrammar.tg

src/AST2OST.pbc: src/AST2OST.pir src/OSTGrammar.pir src/preamble
	$(PARROT) -o src/AST2OST.pbc --output-pbc src/AST2OST.pir 

src/PIRGrammar.pir: src/PIRGrammar.tg
	$(PARROT) $(TGE_DIR)/tgc.pir --output=src/PIRGrammar.pir src/PIRGrammar.tg

src/OST2PIR.pbc: src/OST2PIR.pir src/PIRGrammar.pir src/preamble
	$(PARROT) -o src/OST2PIR.pbc --output-pbc src/OST2PIR.pir 

$(PMCDIR)/ruby_group$(LOAD_EXT): $(PARROT) $(PMC_SOURCES)
	cd $(PMCDIR) && $(BUILD) generate $(PMCS)
	cd $(PMCDIR) && $(BUILD) compile $(PMCS)
	cd $(PMCDIR) && $(BUILD) linklibs $(PMCS)
	cd $(PMCDIR) && $(BUILD) copy --destination=$(PARROT_DYNEXT) $(PMCS)

src/builtins_gen.pir: $(BUILTINS_PIR)
	$(CAT) $(BUILTINS_PIR) >src/builtins_gen.pir

runtime/runtime.pir: $(RUNTIME_COMPONENTS) tools/gen_runtime.pl
	$(PERL) tools/gen_runtime.pl > runtime/runtime.pir

runtime/cardinallib.pbc: $(PARROT) runtime/cardinallib.pir runtime/runtime.pir $(CLASSES)
	$(PARROT) --output=runtime/cardinallib.pbc runtime/cardinallib.pir

# This is a listing of all targets, that are meant to be called by users
help:
	@echo ""
	@echo "Following targets are available for the user:"
	@echo ""
	@echo "  all:               cardinal.pbc"
	@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 ""

gen_tests:
	@$(MV) Parrot_gen Parrot
	-@t/harness > /dev/null 2>&1
	@$(MV) Parrot Parrot_gen

test: all
	$(PERL) -Isrc -I../../src t/harness

testclean:
	$(RM_F) "t/*.rb" "t/*.out"

CLEANUPS = \
  cardinal.pbc \
  $(GEN_PIR) \
  $(GEN_PBC) \
  $(PMCDIR)/*.h \
  $(PMCDIR)/*.c \
  $(PMCDIR)/*.dump \
  $(PMCDIR)/*$(O) \
  $(PMCDIR)/*$(LOAD_EXT) \
  t/*.out \
  t/*.rb

clean: 
	$(RM_RF) $(CLEANUPS)

realclean: clean
	$(RM_RF) Makefile

distclean: realclean
