#CC := g++

RANLIB		:= ranlib
AR		:= ar rcu
#SED		:= sed

WARN     :=-Wall
#-Wstrict-prototypes -Winline
OPTIMIZE :=-Os -g
INCLUDE  :=-I. -Ibase -ISkipDB

ifeq ($(CC),gcc)
	DFLAGS := -MM
else
	DFLAGS := -M
endif

CFLAGS		:=$(OPTIMIZE) $(WARN) $(INCLUDE)
LFLAGS		:=-L. -L_libs -Lbase/_libs
#LFLAGS		+=-lMallocDebug
LIBS		:=-lIoBase -lIoVM 

### PLATFORM #################################################################
#
# Platform specifics compile time options:
#   NEEDS_DL - Needs -ldl for dlopen and friends.
#   SANE_POPEN - popen() can return a bidirectional FILE*
#


PLATFORM := $(shell uname)
BINARY := ../$(shell uname)-Binaries/IoVM

NEEDS_DL	:= Linux syllable SunOS
SANE_POPEN	:= FreeBSD Darwin NetBSD
OS		:= $(shell uname -s)

ifneq (,$(findstring Linux,$(OS)))
WARN +=-Winline
endif

ifneq (,$(findstring MINGW,$(OS)))
LIBS +=-lwsock32
endif

ifneq (,$(findstring $(OS),$(NEEDS_DL)))
LIBS +=-ldl
endif

ifneq (,$(findstring $(OS),$(SANE_POPEN)))
CFLAGS +=-DSANE_POPEN
endif

ifeq ($(OS),IRIX)
RANLIB := touch
endif 

ifeq ($(OS),Darwin)
CFLAGS +=-falign-loops=16
# -arch ppc64
endif

ifneq ($(OS),Darwin)
LIBS +=-lm
endif


### FILES ####################################################################

EXE		:= io
LIB		:= _libs/libIoVM.a 
OBJDIR		:= _objs

sources := $(wildcard *.c)
objects := $(addprefix _objs/,$(sources:.c=.o))

DIR := $(shell pwd)

NAME := VM
IOCODE_NAME := ${NAME}Code
IOOBJS := $(patsubst _ioCode/%.io,%, $(wildcard _ioCode/*.io))

### RULES ####################################################################

all: 
	mkdir -p _libs
	mkdir -p _objs
	$(CC) _tools/io2c.c -o io2c
	$(MAKE) ioCode
	if test -f base/libIoBase.a; then (rm base/libIoBase.a); fi 
	$(MAKE) -C base
	cd SkipDB; $(MAKE)
	$(MAKE) $(LIB) 
	$(MAKE) $(EXE)

$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

$(LIB): $(objects) 
	$(AR) $@ $(objects) base/_objs/*.o SkipDB/_objs/*.o
	$(RANLIB) $@

	mkdir -p _include
	cp *.h _include

	mkdir -p _include/base
	cp base/*.h _include/base

	mkdir -p _include/SkipDB
	cp SkipDB/*.h _include/SkipDB

$(EXE): main.c $(LIB)
	$(CC) $(CFLAGS) $(LFLAGS) -o $(EXE) main.c $(LIBS)

ioCode: 
	-rm -f _ioCode/*.c
	-rm -f Io${IOCODE_NAME}.io
	-rm -f Io${IOCODE_NAME}.c
	-for i in $(IOOBJS); do \
		cat _ioCode/$${i}.io >> Io${IOCODE_NAME}.io; \
		echo ";" >> Io${IOCODE_NAME}.io; \
		done
	./io2c ${IOCODE_NAME} Io${IOCODE_NAME}.io Io${IOCODE_NAME}.c

clean:
	-rm io2c
	$(MAKE) -C base clean
	-rm -rf _include
	-rm -rf _objs
	-rm -rf _libs
	-rm -f *.o 
	-rm -f $(LIB) $(EXE)
	cd SkipDB; $(MAKE) clean
	if test -f _docs/Object.html; then (rm _docs/*.html); fi

install:
	cp ./io /usr/local/bin/

configure:
	echo "nothing to configure"

test:
	#./io _tests/speed.io
	#./io _tests/primitiveTests.io
	#./io _tests/shootoutTests.io
	./io _unitTests/AllTests.io

bin:
	mkdir $(BINARY)
	cp io $(BINARY)
	cp -R _docs $(BINARY)
	cp -R _tests $(BINARY)
	cp -R _libs $(BINARY)

docs:
	./io ../_docgen/DocsExtractor.io .

speedtest:
	./io _tests/_speed/speed.io > _tests/_speed/SpeedResults
	python _tests/_speed/speed.py >> _tests/_speed/SpeedResults
	ruby _tests/_speed/speed.ruby >> _tests/_speed/SpeedResults
	#lua _tests/_speed/speed.lua >> _tests/_speed/SpeedResults
	./io _tests/_speed/speedResults.io > _tests/_speed/SpeedResults.html

