CC=gcc

VER=3.6
prefix=/usr/local/cpython-${VER}
PYTHON_VERSION=python${VER}

CFLAGS=-ansi -Wall -O -g -fPIC -I/usr/include/${PYTHON_VERSION}m
LDFLAGS=-g -fPIC -L${prefix}/lib -l${PYTHON_VERSION}m

# The 3.0.8 that came with Mint 18.3 was too old to support SWIG_PYTHON_STRICT_BYTE_CHAR.
# I suspect 3.0.9 would work, but the latest at "this" time is 3.0.12.
# SWIG=/usr/local/swig-3.0.12/bin/swig

# Mint 19.1 has a recent enough swig...
SWIG=swig

test: perform-c-test perform-cxx-test perform-python-low-level-test perform-python-comparison-test perform-python-bufsock-test good-news
	/bin/true

perform-python-bufsock-test: python-bufsock-test odirect.py _odirectcmodule.so
	find /boot -type f -readable -print0 | ./python-bufsock-test
	touch perform-python-bufsock-test

perform-python-comparison-test: python-comparison-test odirect.py _odirectcmodule.so
	find /boot -type f -readable -print0 | ./python-comparison-test
	touch perform-python-comparison-test

perform-python-low-level-test: python-low-level-test odirect.py _odirectcmodule.so
	./this-pylint --which-2 None --to-pylint python-low-level-test odirect.py
	./python-low-level-test `find /boot -type f -readable -print`
	touch perform-python-low-level-test

perform-c-test: Makefile c-test
	./c-test `find /boot -type f -readable -print`
	touch perform-c-test

# this cxx support is far from complete!  Sat Aug 19 12:36:52 PDT 2006
perform-cxx-test: Makefile cxx-test
	./cxx-test `find /boot -type f -readable -print`
	touch perform-cxx-test

cxx-test.o: cxx-test.cxx
	$(CXX) -c cxx-test.cxx

cxx-test: odirect-cxx.o cxx-test.o
	$(CXX) -o cxx-test cxx-test.o odirect-cxx.o odirect.o

odirect-cxx.o: odirect-cxx.cxx odirect-cxx.H
	$(CXX) -c odirect-cxx.cxx

odirectcmodule.c: odirectcmodule.swg odirect.c odirect.h Makefile
	${SWIG} -python -py3 -o odirectcmodule.c odirectcmodule.swg

odirectcmodule.o: odirectcmodule.c
	$(CC) $(CFLAGS) -c odirectcmodule.c

_odirectcmodule.so: odirectcmodule.o odirect.o
	$(CC) `./do-not-toast-functions` -shared odirectcmodule.o odirect.o -o _odirectcmodule.so $(LDFLAGS)

c-test: libodirect.a c-test.o
	$(CC) $(LDFLAGS) -o c-test c-test.o -L. -lodirect

libodirect.a: odirect.o
	rm -f libodirect.a
	ar cqv libodirect.a odirect.o

odirect.o: odirect.c odirect.h sizes
	$(CC) -c $(CFLAGS) `./sizes` odirect.c

sizes: sizes.c
	$(CC) $(CFLAGS) -o sizes sizes.c

clean:
	rm -f c-test sizes *.o *.a odirectcmodule.c odirectcmodule.py *.so perform-python-*-test perform-cxx-test cxx-test

good-news:
	@echo All tests passed ':)'

install:
	./install-file --directory ${prefix}/lib/${PYTHON_VERSION}/site-packages/. --file _odirectcmodule.so
	./install-file --directory ${prefix}/lib/${PYTHON_VERSION}/site-packages/. --file odirectcmodule.py
	./install-file --directory ${prefix}/lib/${PYTHON_VERSION}/site-packages/. --file odirect.py
	./install-file --directory ${prefix}/lib/${PYTHON_VERSION}/site-packages/. --file bufsock.py