#*
#* libspe - A wrapper library to adapt the JSRE SPU usage model to SPUFS 
#* Copyright (C) 2005 IBM Corp. 
#*
#* This library is free software; you can redistribute it and/or modify it
#* under the terms of the GNU Lesser General Public License as published by 
#* the Free Software Foundation; either version 2.1 of the License, 
#* or (at your option) any later version.
#*
#*  This library is distributed in the hope that it will be useful, but 
#*  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
#*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
#*  License for more details.
#*
#*   You should have received a copy of the GNU Lesser General Public License 
#*   along with this library; if not, write to the Free Software Foundation, 
#*   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#*

HOST = $(shell uname -p)
ifeq "$(HOST)" "i686"
    CROSS=/usr/local/sce/ppu/bin/ppu-
    CFLAGS=$(CFLAGS) -m32
    prefix ?= /usr
else
prefix ?= /usr/local
endif

exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
libdir = ${exec_prefix}/lib
includedir = ${prefix}/include

DESTDIR =

INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL} -m 755
INSTALL_DATA = ${INSTALL} -m 644

CC := $(CROSS)gcc
CTAGS = ctags

CFLAGS := -O2 -m32 -Wall -I. -Iinclude -std=gnu99 \
		  -Wformat -Wdisabled-optimization \
                  -Wundef -Wshadow -Wcast-align -Wwrite-strings \
                   -Wmissing-prototypes -Wmissing-declarations \
                   -Wmissing-noreturn -Wredundant-decls \
                   -Wnested-externs -Winline -Wformat

ifdef DEBUG
CFLAGS += -DDEBUG -g
endif

ifdef NODIRREMOVE
CFLAGS += -DNODIRREMOVE
endif

ifdef NOSYSCALL
CFLAGS += -DNOSYSCALL
endif

libspu_OBJS := elf_loader.o spe.o handler.o default_syscall_handler.o default_c99_handler.o
OBJS := libspe.a $(libspu_OBJS)

all: libspe.a elfspe elfspe-register

libspe.a: $(libspu_OBJS)
	ar -r $@ $(libspu_OBJS)
	ranlib $@

elfspe: elfspe.c libspe.a
	$(CC) $(CFLAGS) -o $@ $< -L. -lspe -lpthread
	touch tests/Makefile

elfspe-register: tools/elfspe-register
	sed -e "s%@BINDIR@%${bindir}%g" <$< >$@
	chmod +x $@

ifeq "$(HOST)" "i686"
install: 
else
install: spe.h libspe.a elfspe elfspe-register
	$(INSTALL_DATA) spe.h \
		$(DESTDIR)$(includedir)/spe.h
	$(INSTALL_PROGRAM) libspe.a \
		$(DESTDIR)$(libdir)/libspe.a
	$(INSTALL_PROGRAM) elfspe \
		$(DESTDIR)$(bindir)/elfspe
	$(INSTALL_PROGRAM) elfspe-register \
		$(DESTDIR)$(bindir)/elfspe-register
endif

tests: tests/Makefile
	make -C tests

tags:
	$(CTAGS) -R .

clean: 
	rm -f $(OBJS) *~ tags elfspe elfspe-register
	make -C tests clean
