#*
#* 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
#*


X86 = $(shell if [ "`uname -m | grep  ppc`" == "" ] ; then echo 1 ; fi)

CROSS_COMPILE = $(shell if [ "`cat /proc/cpuinfo | grep Broadband`" == "" ] ; \
		  then echo 1 ; fi)

ifeq "$(X86)"  "1"
    CROSS	?= /opt/cell/toolchain-3.3/bin/ppu-
    EXTRA_CFLAGS = -m32  -mabi=altivec 
    # RPM defined default flags may not work with this cross-compiler
    override OPTFLAGS = 
endif
ifeq "$(CROSS_COMPILE)"  "1"
    SYSROOT	?= ../../../sysroot
    prefix	?= /usr
else
prefix		?= /usr/local
endif

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

DESTDIR		:=
ROOT		:= ${DESTDIR}${SYSROOT}

INSTALL		:= /usr/bin/install -c
INSTALL_PROGRAM := ${INSTALL} -m 755
INSTALL_DATA	:= ${INSTALL} -m 644
INSTALL_LINK	:= ln -sf
INSTALL_DIR	:= ${INSTALL} -d -m 755

MAJOR_VERSION	:= 1
MINOR_VERSION	:= 2.2

libspe_SO	:= libspe.so.${MAJOR_VERSION}.${MINOR_VERSION}
libspe_SONAME	:= libspe.so.${MAJOR_VERSION}

CC		:= ${CROSS}gcc
CTAGS		:= ctags

CFLAGS := -O2 -Wall -I. -fPIC -Iinclude -std=gnu99 \
			-Wformat -Wdisabled-optimization \
			-Wundef -Wshadow -Wcast-align -Wwrite-strings \
			-Wmissing-prototypes -Wmissing-declarations \
			-Wmissing-noreturn -Wredundant-decls \
			-Wnested-externs -Winline -Wformat \
			${EXTRA_CFLAGS} ${OPTFLAGS}
ifdef DEBUG
CFLAGS += -DDEBUG -g
endif

libspe_OBJS := elf_loader.o spe.o spethreads.o dma.o ps.o handler.o default_syscall_handler.o default_c99_handler.o default_posix1_handler.o

#all: $(libspe_SO) libspe.a elfspe elfspe-register
all: $(libspe_SO) libspe.a 


$(libspe_SO): $(libspe_OBJS)
	$(CC) $(CFLAGS) -shared -o $@ $^ -lpthread -lrt -Wl,--soname=${libspe_SONAME}

libspe.a: $(libspe_OBJS)
	 $(CROSS)ar -r libspe.a *.o

elfspe: elfspe.o $(libspe_SO)
	$(CC) $(CFLAGS) -o $@ $< $(libspe_SO)
	touch tests/Makefile

#
# when building in the cross environment this script should reference the
# boot-time relative location not the sysroot relative location.
#
elfspe-register: tools/elfspe-register
	sed -e "s%@BINDIR@%${bindir}%g" <$< >$@
	chmod +x $@

#install: spe.h $(libspe_SO) elfspe elfspe-register libspe.a
install: spe.h $(libspe_SO) libspe.a
	$(INSTALL_DIR)			   $(ROOT)$(libdir)
	$(INSTALL_DATA)	   libspe.a	   $(ROOT)$(libdir)/libspe.a
	$(INSTALL_PROGRAM) $(libspe_SO)	   $(ROOT)$(libdir)/$(libspe_SO)
	$(INSTALL_LINK)	   $(libspe_SO)	   $(ROOT)$(libdir)/$(libspe_SONAME)
	$(INSTALL_LINK)	  $(libspe_SONAME) $(ROOT)$(libdir)/libspe.so
	$(INSTALL_DIR)			   $(ROOT)$(includedir)
	$(INSTALL_DATA)	   libspe.h	   $(ROOT)$(includedir)/libspe.h
	$(INSTALL_DATA)	   cbea_map.h	   $(ROOT)$(includedir)/cbea_map.h
	$(INSTALL_DIR)			   $(ROOT)$(speinclude)
	$(INSTALL_DATA)	   cbea_map.h	   $(ROOT)$(speinclude)/cbea_map.h
	$(INSTALL_DIR)			   $(ROOT)$(bindir)
#	$(INSTALL_PROGRAM) elfspe	   $(ROOT)$(bindir)/elfspe
#	$(INSTALL_PROGRAM) elfspe-register $(ROOT)$(bindir)/elfspe-register
#	$(INSTALL_PROGRAM) elfspe-unregister $(ROOT)$(bindir)/elfspe-unregister

tests: tests/Makefile
	make -C tests

tags:
	$(CTAGS) -R .

PACKAGE		= libspe
FULLNAME	= $(PACKAGE)-$(MAJOR_VERSION).$(MINOR_VERSION)
TARBALL		= $(SOURCES)$(FULLNAME).tar.gz
SOURCEFILES	= $(TARBALL)
#RPMBUILD	= ppc32 rpmbuild --target=ppc 
RPMBUILD	= rpmbuild 
# ../make.rules contains the local rpm build infrastructure in CVS
-include ../make.rules
rpms: rpm rpm32

rpm32: 
	HOME=$(PWD) $(RPMBUILD) --target=ppc -ba $(SPEC) $(RPMFLAGS)
rpm64: 
	HOME=$(PWD) $(RPMBUILD) --target=ppc64 -ba $(SPEC) $(RPMFLAGS)



tarball: $(TARBALL)

$(TARBALL): $(SOURCES)
	rm -f $(FULLNAME)
	ln -s . $(FULLNAME)
	tar czf $@ --exclude=$(FULLNAME).tar.gz \
		--exclude=$(FULLNAME)/$(FULLNAME) \
		--exclude=CVS $(FULLNAME)/*

doc: text pdf
 
text: xml
	cd doc; xsltproc extractfunctions.xslt ../xml/all.xml > functions.txt

xml: clean
	doxygen doc/DoxyfileRef
	cd xml; xsltproc combine.xslt index.xml >all.xml

pdf: apiref detail

detail: clean
	doxygen doc/Doxyfile
	cd latex; make; cp refman.pdf ../doc/detail.pdf

apiref: clean
	doxygen doc/DoxyfileRef
	cd latex; make; cp refman.pdf ../doc/apiref.pdf

clean: 
	rm -f $(libspe_SO) libspe.a $(libspe_OBJS) *~ tags elfspe elfspe-register
	rm -f elfspe.o $(TARBALL) $(FULLNAME)
	rm -rf xml
	rm -rf latex
	rm -rf html
	make -C tests clean

.PHONY: all clean tests tags
