# Current prox2 version (commit) used for bootstrap:
# e5de16dfa111ebe0b6ece48e9923fda1fa1d6e91 (2019-07-27)
PROX=../prox

PROX2RT=$(PROX)/compile.px2 $(PROX)/num.px2 $(PROX)/rwx.px2 $(PROX)/str.px2 $(PROX)/table.px2

ASTERIXRT=asterix-main.ax compile.ax errline.ax error.ax num.ax rwx.ax table.ax
RT=compile.ax errline.ax error.ax num.ax rwx.ax str.ax table.ax

all: asterix asterix-asm elfwrap

prereq:
	$(MAKE) -C $(PROX) prereq prox2 prox2-asm elfwrap

asterix-s1: asterix-s1.px2 $(PROX2RT)
	cat $< $(PROX2RT) | $(PROX)/prox2 | $(PROX)/prox2-asm | $(PROX)/elfwrap > $@
	chmod +x $@

asterix-s1-asm: asterix-s1-asm.px2 $(PROX2RT)
	cat $< $(PROX2RT) | $(PROX)/prox2 | $(PROX)/prox2-asm | $(PROX)/elfwrap > $@
	chmod +x $@

elfwrap: elfwrap.ax compile.ax error.ax rwx.ax asterix-s1 asterix-s1-asm
	cat elfwrap.ax compile.ax error.ax rwx.ax | ./asterix-s1 | ./asterix-s1-asm | $(PROX)/elfwrap > $@
	chmod +x $@

asterix: asterix.ax $(ASTERIXRT) asterix-s1 asterix-s1-asm elfwrap
	cat $< $(ASTERIXRT) | ./asterix-s1 | ./asterix-s1-asm | ./elfwrap > $@
	chmod +x $@

asterix-asm: asterix-asm.ax $(ASTERIXRT) asterix asterix-s1-asm elfwrap
	cat $< $(ASTERIXRT) | ./asterix-s1 | ./asterix-s1-asm | ./elfwrap > $@
	chmod +x $@

test-asterix: asterix asterix-asm elfwrap
	cat asterix.ax $(ASTERIXRT) | ./asterix | ./asterix-asm | ./elfwrap | diff -s - asterix
	cat asterix-asm.ax $(ASTERIXRT) | ./asterix | ./asterix-asm | ./elfwrap | diff -s - asterix-asm

%: %.ax $(RT) asterix asterix-asm elfwrap
	cat $< $(RT) | ./asterix | ./asterix-asm | ./elfwrap > $@
	chmod +x $@

test: test-asterix

clean:
	$(RM) elfwrap asterix asterix-asm asterix-s1 asterix-s1-asm test-alloca test-expr

clean-prereq:
	$(MAKE) -C $(PROX) clean clean-prereq

clean-all: clean clean-prereq

.PHONY: all prereq test-asterix test clean clean-prereq clean-all
