python_version=2.7

# $@ is the target list
# $< is the input
# $* is the basename of something

%.so: %.m4
	#
	# Create the pure python file
	m4 --define=purepy=1 < $*.m4 | sed 's/^import \([a-z]*sort\)$$/import \1_py as \1/' > $*_py.py
	# create something without @profile's for pylint
	sed 's/@profile//' < $*_py.py > $*_for_linting.py
	# pylint the file for correctness!
	./this-pylint $*_for_linting.py 
	# clean up
	rm -f $*_for_linting.py
	# Now we start on the fast version - .pyx
	# create the pyx file
	m4 --define=m4pyx=1 < $*.m4 > $*.pyx
	# create the .c file from the .pyx
	/usr/local/cpython-2.7/bin/cython $*.pyx
	# clean up
	#rm -f $*.pyx
	# create the .o file from the .c
	gcc -O3 -fPIC -I /usr/local/cpython-$(python_version)/include/python$(python_version) -c -o $*.o $*.c
	# clean up
	rm -f $*.c
	# create the .so file from the .o
	$(CC) -shared $*.o -o $*.so

go: insertionsort.so binarysort.so funnelsort.so quicksort.so bubblesort.so shellsort.so mergesort.so timsort_reimp.so
	rm -f *.dat
	# this does a line-resolution profiling, if you decorate relevant functions with @profile
	#kernprof.py -l -v ./test-funnelsort
	./performance-tests

go2: test-smallsorts funnelsort_py.py funnelsort.so
	kernprof.py -l -v ./test-smallsorts

go3: timsort_reimp.so
	./test-timsort_reimp

dats=slow_mergesort_cython.dat slow_shellsort_cython.dat slow_bubblesort_cython.dat slow_binarysort_cython.dat slow_insertionsort_cython.dat slow_funnelsort_cython.dat slow_timsort_cython.dat slow_quicksort_cython.dat slow_timsort_cext.dat small_mergesort_cython.dat small_shellsort_cython.dat small_bubblesort_cython.dat small_binarysort_cython.dat small_insertionsort_cython.dat small_funnelsort_cython.dat small_timsort_cython.dat small_quicksort_cython.dat small_timsort_cext.dat
graph.ps: graph-ps.gp $(dats)
	gnuplot graph-ps.gp

clean:
	rm -f *.dat *.ps *.pyx *.c *.so *.o *_for_linting.py *_py.py *.lprof *.pyc