#!/usr/bin/make
CC=gcc
CFLAGS=-O2 -Wall -Werror
# If you're using a recent version of GCC, enable this to fix its braindamage:
PRELOADCFLAGS=-Wno-unused-value
SRCS=bytefunc.c cons.c integer.c main.c nil.c preload.c quote.c rawfunc.c stream.c string.c throw.c util.c vm.c word.c
LIBS=
LISPS=compiler.lisp functions.lisp macroexpand.lisp optimizer.lisp
BUILDDIR=obj/

### Uncomment these three lines to enable float.c
CFLAGS+=-DUSE_FLOAT
SRCS+=float.c
LIBS+=-lm

### Uncomment these three lines to enable bigint.c
CFLAGS+=-DUSE_BIGINT
SRCS+=bigint.c
LIBS+=-lgmp

### Uncomment this to enable the vm-debug function
#CFLAGS+=-DDEBUG
###

# Has targets related to building tarballs, etc.
-include .local.Makefile

$(BUILDDIR)mikelisp : $(SRCS:%.c=$(BUILDDIR)%.o) opcodes.c lisp.h
	$(CC) $(CFLAGS) -o $@ $(SRCS:%.c=$(BUILDDIR)%.o) $(LIBS)

$(BUILDDIR)preload.o : preload.c lisp.h $(LISPS:%.lisp=$(BUILDDIR)%.c)
	$(CC) $(CFLAGS) $(PRELOADCFLAGS) -I$(BUILDDIR) -c -o $@ $<

$(BUILDDIR)vm.o : vm.c lisp.h vm.h opcodes.c
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILDDIR)%.o : %.c lisp.h
	$(CC) $(CFLAGS) -c -o $@ $<

opcodes.c : vm.h
	perl -ne 'print "\tADD_OP(\"".lc($$2)."\",$$1);\n" if(/#define (OP_([A-Z]+)) /)' vm.h | tac > opcodes.c2
	mv opcodes.c2 opcodes.c

$(BUILDDIR)%.c : %.lisp
	echo '(compile-file "$*.lisp" "$(BUILDDIR)$*.c2")' | $(BUILDDIR)mikelisp
	mv $(BUILDDIR)$*.c2 $(BUILDDIR)$*.c

clean :
	rm -f $(BUILDDIR)mikelisp $(BUILDDIR)*.o

