CFLAGS=-g -Wall -O2
CC=gcc

ABI_FLAG=$(shell ./platform-flags.sh)

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

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}" HOSTCC=${CC} gctest

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

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

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 runtime/platform-flags.sh

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 "*~"
