.SUFFIXES:

PYVERSION=3.12
PYPREFIX=/usr
INCLUDES=-I$(PYPREFIX)/include/python$(PYVERSION)
CYTHON=cython3
LANG0PY=$(CURDIR)/../0-lang0py/lang0py.exe

all: lang0py.exe

%.exe: %.o
	gcc -o $@ $< -lpython$(PYVERSION)

%.o: %.c
	gcc -c $< $(INCLUDES)

%.c: %.py
	$(CYTHON) -3 --embed $<

lang0py0.py: lang0py.lang0 $(LANG0PY)
	cat $< | $(LANG0PY) > $@.tmp
	mv $@.tmp $@

lang0py1.py: lang0py.lang0 lang0py0.exe
	cat $< | ./lang0py0.exe > $@.tmp
	mv $@.tmp $@
# Cannot diff on the first iteration - platform change

lang0py2.py: lang0py.lang0 lang0py1.exe
	cat $< | ./lang0py1.exe > $@.tmp
	mv $@.tmp $@
	-diff lang0py1.py lang0py2.py

lang0py.py: lang0py.lang0 lang0py2.exe
	cat $< | ./lang0py2.exe > $@.tmp
	mv $@.tmp $@
	-diff lang0py2.py lang0py.py

$(LANG0PY): $(CURDIR)/../0-lang0py/lang0py.py $(CURDIR)/../0-lang0py/lang0py.lang0
	cd ../0-lang0py && make lang0py.exe

clean:
	rm -f lang0py*.py lang0py*.c lang0py*.o lang0py*.exe
