#
# Makefile for DaRuMa
#

# GNU make is required.
ifeq (,)
endif

TOPDIR=../..
DOCDIR=$(TOPDIR)/doc/misp

ANT   = ant -lib lib -emacs
JAVA  = java
RM    = rm
CHDIR = cd

CLASSPATH = lib/xerces-j-2.7.1/xml-apis.jar:lib/xerces-j-2.7.1/xercesImpl.jar:lib/xerces-j-2.7.1/resolver.jar:lib/xalan-j-2.7.0/xalan.jar:lib/xalan-j-2.7.0/serializer.jar:lib/mysql-connector-java-5.0.5-bin.jar:lib/jts-1.7.2.jar:lib/servletapi-gump-27012008.jar:lib/log4j-1.2.13.jar:lib/junit-4.1.jar

GCJ_JAR       = $(subst :, ,$(CLASSPATH))
GCJ_JAR_SO    = $(GCJ_JAR:.jar=.jar.so)
GCJ_CLASSPATH = $(patsubst %,-I%,$(GCJ_JAR))
GCJFLAGS      = -pipe -Wall -O2 -ffast-math -fomit-frame-pointer -findirect-dispatch # "-findirect-dispatch" is required.

FINDBUGS       = findbugs -maxHeap 256
FINDBUGS_FLAGS = -textui -emacs -auxclasspath "$(CLASSPATH)" -low


#all: check findbugs
all: daruma.jar

.PHONY: all check javadoc findbugs run clean clean-log clean-gcj doc \
	diff status log release dummy gcj-bin jar war

daruma.jar: dummy
	$(ANT) compile

daruma.war: dummy
	$(ANT) compile

daruma-test.jar: dummy
	$(ANT) check

jar: daruma.jar
war: daruma.war


#
# GCJ
#
%.jar.so: %.jar
	gcj $(GCJ_CLASSPATH) $(GCJFLAGS) -shared -fPIC -o $@ $<

classmap.gcjdb: daruma.jar.so $(GCJ_JAR_SO)
	gcj-dbtool -n classmap.gcjdb
	gcj-dbtool -m classmap.gcjdb $(shell gcj-dbtool -p)
	gcj-dbtool -a classmap.gcjdb daruma.jar daruma.jar.so
#
	for i in $(GCJ_JAR) ; do \
		echo $${i} ; \
		gcj-dbtool -a classmap.gcjdb $${i} $${i}.so ; \
	done

gcj-bin: daruma.jar.so $(GCJ_JAR_SO) classmap.gcjdb


# In this version, unit test only.
# doesn't run tests in ./test/ automatically
#
# To test I/O of server, make sure server is running,
# change to ./test/ ditectory and run 'make check'.
check:
	$(ANT) check

javadoc:
	$(ANT) javadoc

findbugs: daruma.jar daruma-test.jar
	$(FINDBUGS) $(FINDBUGS_FLAGS) -sourcepath src daruma.jar
	$(FINDBUGS) $(FINDBUGS_FLAGS) -auxclasspath daruma.jar \
				      -sourcepath src daruma-test.jar

run: daruma.jar
	./run

clean: clean-log clean-gcj
	$(ANT) clean
	($(CHDIR) test && $(MAKE) clean)

clean-log:
	$(RM) -f log-*
	$(RM) -rf log

clean-gcj:
	$(RM) -f daruma.jar.so
	for i in $(GCJ_JAR_SO) ; do \
		echo $${i} ; \
		rm -f $${i} ; \
	done
	$(RM) -f classmap.gcjdb


doc: $(DOCDIR)/main.pdf
$(DOCDIR)/main.pdf: $(DOCDIR)/changes.tex $(DOCDIR)/protocol.tex
	cd $(DOCDIR) && make main.pdf

# dummy virtual target to make files every time
dummy:

#
# Revision Control
#
SVN = svn

diff:
	@$(SVN) diff

status:
	@$(SVN) status

log:
	@$(SVN) log

# Makefile ends here
