.SUFFIXES:
.PHONY: all clean
.DELETE_ON_ERROR:

PYVERSION=3.12
PYPREFIX=/usr
CYTHON=cython3
CC=gcc
CFLAGS=-ggdb

all: verify-results

Makefile.mk: generate-recipes.py $(wildcard test_*/test_*)
	python3 generate-recipes.py Makefile.mk it0 it1 it2

include Makefile.mk

clean:
	find build -name '*.c' -delete
	find build -name '*.it0' -delete
	find build -name '*.it0.result' -delete
	find build -name '*.it0.stderr' -delete
	find build -name '*.it0.stdout' -delete
	find build -name '*.it1' -delete
	find build -name '*.it1.result' -delete
	find build -name '*.it1.stderr' -delete
	find build -name '*.it1.stdout' -delete
	find build -name '*.it2' -delete
	find build -name '*.it2.result' -delete
	find build -name '*.it2.stderr' -delete
	find build -name '*.it2.stdout' -delete
	find build -name '*.py' -delete
	find build -name '*.tmp' -delete

###
# it0

../0-lang0py/lang0py.exe: ../0-lang0py/lang0py.py
	$(MAKE) -C ../0-lang0py

build/%.it0.py: %.lang0 ../0-lang0py/lang0py.exe
	-cat $< | ../0-lang0py/lang0py.exe > $@.tmp 2> build/$*.it0.cmperr
	mv $@.tmp $@

build/%.it0.c: build/%.it0.py
	$(CYTHON) -3 --embed -o $@ $^

build/%.it0.o: build/%.it0.c
	$(CC) $(CFLAGS) -o $@ -c $^ -I$(PYPREFIX)/include/python$(PYVERSION)

build/%.it0: build/%.it0.o
	$(CC) $(CFLAGS) -o $@ $^ -lpython$(PYVERSION)

###
# it1

../1-lang0py/lang0py.exe: ../0-lang0py/lang0py.exe ../1-lang0py/lang0py.lang0
	$(MAKE) -C ../1-lang0py

build/%.it1.py: %.lang0 ../1-lang0py/lang0py.exe
	-cat $< | ../1-lang0py/lang0py.exe > $@.tmp 2> build/$*.it1.cmperr
	mv $@.tmp $@

build/%.it1.c: build/%.it1.py
	$(CYTHON) -3 --embed -o $@ $^

build/%.it1.o: build/%.it1.c
	$(CC) $(CFLAGS) -o $@ -c $^ -I$(PYPREFIX)/include/python$(PYVERSION)

build/%.it1: build/%.it1.o
	$(CC) $(CFLAGS) -o $@ $^ -lpython$(PYVERSION)

###
# it2

../2-lang0c/lang0c.exe: ../0-lang0py/lang0py.exe ../1-lang0py/lang0py.exe ../2-lang0c/lang0c.lang0
	$(MAKE) -C ../2-lang0c

build/%.it2.c: %.lang0 ../2-lang0c/lang0c.exe
	-cat $< | ../2-lang0c/lang0c.exe > $@.tmp 2> build/$*.it2.cmperr
	mv $@.tmp $@

build/%.it2.o: build/%.it2.c
	$(CC) $(CFLAGS) -o $@ -c $^ -I$(PYPREFIX)/include/python$(PYVERSION)

build/%.it2: build/%.it2.o
	$(CC) $(CFLAGS) -o $@ $^ -lpython$(PYVERSION)
