CFLAGS=-g -Wall -O2
CC=gcc
CPU:=$(shell uname -m)

# If you're having problems, set it to -m32 manually.
# You will likely have to do the same in linki9.sh
ABI_FLAG=

ifeq (${CPU},x86_64)
ABI_FLAG=-m32
endif

all: i9filt libi9.a examples/arr examples/fact

i9filt: demangle/demangle.o common/io.o
	gcc $(ABI_FLAG) -o $@ $^

libi9.a: gc6.8/gc.a libi9/libi9.o common/io.o
	-rm $@
	cp gc6.8/gc.a $@
	ar rcs $@  libi9/libi9.o common/io.o

%.o: %.c
	gcc $(CFLAGS) $(ABI_FLAG) -c -o $@ $<

gc6.8/gc.a: gc6.8/Makefile
	cd gc6.8 && make ABI_FLAG=${ABI_FLAG} test

gc6.8/Makefile:
	# Downloads and extracts Boehm GC 6.8 --- the same
	# version as "gc.tar.gz", which is what's recommended.
	-rm gc6.8.tar.gz
	-rm -r gc6.8
	wget http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/gc6.8.tar.gz
	tar xvzf gc6.8.tar.gz

examples/fact:
	./linki9.sh examples/fact.s -o examples/fact

examples/arr:
	./linki9.sh examples/arr.s -o examples/arr

clean:
	-rm gc6.8.tar.gz
	-cd gc6.8 && make clean
	-rm libi9.a i9filt
	-rm libi9/libi9.o
	-rm demangle/demangle.o common/io.o
	-rm examples/fact examples/arr:
	-rm *~

veryclean:
	make clean
	-rm -r gc6.8

FILES = runtime/demangle runtime/libi9 runtime/common/io.h runtime/common/io.c\
        runtime/linki9.sh runtime/Makefile runtime/README.txt runtime/gc6.8\
        runtime/examples/*.c runtime/examples/*.s runtime/include \
        runtime/ChangeLog.txt

tarballs: gc6.8/Makefile
	make clean
	cd .. && tar cvz --exclude=CVS --exclude="*~" ${FILES} > runtime.tar.gz
	cd .. && zip runtime.zip -r ${FILES} -x '*CVS*' -x "*~"
