# Commands
LN_S   = @lns@
PERL   = @perl@
RM_F   = @rm_f@
CP     = @cp@
PARROT = ../../parrot@exe@
BUILD  = $(PERL) @build_dir@/tools/build/dynpmc.pl

# Directories
PGE_DIR  = ../../compilers/pge
TGE_DIR  = ../../compilers/tge
PMC_DIR   = src/pmc
GROUP_DIR = @build_dir@/runtime/parrot/dynext

# Files
APL_GROUP = $(GROUP_DIR)/apl_group$(LOAD_EXT)
PMC_NAMES = aplvector
PMC_FILES = $(PMC_DIR)/aplvector.pmc

# Extensions
O        = @o@
LOAD_EXT = @load_ext@


all: APL.pbc

$(PARROT):
	@echo "You must first configure and build parrot. See ../../README"
	@$(PERL) -e "exit 1"

SOURCES = APL.pir \
  src/APLGrammar.pir \
  src/parse2past.pir \
  src/APLFunctions.pir

APL.pbc: $(PARROT) $(APL_GROUP) $(SOURCES) 
	$(PARROT) --output=APL.pbc APL.pir

interactive: APL.pbc
	$(PARROT) APL.pbc

.SUFFIXES : .pir .pbc .tg .pg

.pir.pbc :
	$(PARROT) -o $@ $<

.tg.pir :
	$(PARROT) $(TGE_DIR)/tgc.pir --output=$@ $<

.pg.pir :
	$(PARROT) $(PGE_DIR)/pgc.pir --encoding=utf8 --output=$@ $<

src/APLFunctions.pir: tools/gen_operator_defs.pl
	$(PERL) tools/gen_operator_defs.pl > src/APLFunctions.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:               APL.pbc"
	@echo "                     This is the default."
	@echo "Running:"
	@echo "  interactive:       Run the interative APL shell"
	@echo ""
	@echo "Testing:"
	@echo "  test:              Run the test suite."
	@echo "  devtest:           Run the test suite, using TODO not SKIP"
	@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: all
	$(PERL) -Ilib t/harness

devtest: all
	APLDEV=1 $(PERL) -Ilib t/harness

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

clean: testclean
	$(RM_F) \
  APL.pbc \
  src/parse2past.pir \
  lib/pge2past.pir \
  lib/past2pir.pir \
  lib/pasteval.pir \
  src/APLGrammar.pir \
  src/APLFunctions.pir \
  $(PMC_DIR)/*.h \
  $(PMC_DIR)/*.c \
  $(PMC_DIR)/*.dump \
  $(PMC_DIR)/*$(LOAD_EXT) \
  $(PMC_DIR)/*$(O) \
  $(APL_GROUP)

realclean: clean
	$(RM_F) Makefile "EVAL_*"

distclean: realclean

# XXX Really dislike this setup - the build script should be giving us the make
# targets so we can let make do its job, not forcing us to call it in 
# this manner.
$(APL_GROUP): $(PMC_FILES)
	cd $(PMC_DIR) && $(BUILD) generate $(PMC_NAMES)
	cd $(PMC_DIR) && $(BUILD) compile  $(PMC_NAMES)
	cd $(PMC_DIR) && $(BUILD) linklibs $(PMC_NAMES)
	cd $(PMC_DIR) && $(BUILD) copy "--destination=$(GROUP_DIR)"  $(PMC_NAMES)
