#
# This is a Makefile to cross-compile this component into a C++ library
#

# You may have to adjust these settings according to your platform

JAVA_HOME=/usr/lib/jvm/java-6-sun
GCJ=gcj
GCJH=gcjh
GCJFLAGS=-O2 -w
GCJHFLAGS=-cni

# Input and output

JAVASRCDIR=src

BUILDDIR=gcj-build
TEMPDIR=gcj-temp
DISTDIR=gcj-dist
OUTPUT=org.eclipse.higgins.idas.client.o

# These packages and classes will not be compiled

EXCLUDES=""

# These are exceptions from the EXCLUDES list

INCLUDES=""

# These are the dependencies that will be compiled with GCJ

DEPENDENCIES="../org.eclipse.higgins.dependencies.redistributable/commons-codec-1.3/commons-codec-1.3.jar ../org.eclipse.higgins.dependencies.redistributable/servlet-api/servlet-api.jar ../org.eclipse.higgins.dependencies.redistributable/non-redistributable/commons-logging-api-1.0.4/commons-logging-api-1.0.4.jar ../org.eclipse.higgins.dependencies.redistributable/non-redistributable/json/json.jar"

# This is the classpath with ALL dependencies

CLASSPATH="../org.eclipse.higgins.configuration.api/src:../org.eclipse.higgins.idas.api/src:../org.eclipse.higgins.idas.common/src:../org.eclipse.higgins.idas.spi/src:../org.eclipse.higgins.idas.cp.xdi/src:../org.eclipse.higgins.idas.udi/src:../org.eclipse.higgins.xdi4j/src:.../org.eclipse.higgins.dependencies.redistributable/commons-collections-3.2.jar:../org.eclipse.higgins.dependencies.redistributable/commons-codec-1.3/commons-codec-1.3.jar:../org.eclipse.higgins.dependencies.redistributable/dom4j-1.6.1/dom4j-1.6.1.jar:../org.eclipse.higgins.dependencies.redistributable/servlet-api/servlet-api.jar:../org.eclipse.higgins.dependencies.redistributable/icu4j_3_4/icu4j_3_4.jar:../org.eclipse.higgins.dependencies.redistributable/xmlsec-1.4.0/xmlsec-1.4.0.jar:../org.eclipse.higgins.dependencies.redistributable/ehcache-1.2.3/ehcache-1.2.3.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributable/json/json.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributabe/je-3.2.68/je-3.2.68.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributabe/openxri-client-1.2.0/openxri-client-1.2.0.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributabe/openxri-syntax-1.2.0/openxri-syntax-1.2.0.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributabe/hibernate-3.2.5.ga/hibernate-4.2.5.ga.jar:../org.eclipse.higgins.dependencies.redistributable/non-redistributable/commons-logging-api-1.0.4/commons-logging-api-1.0.4.jar"

# 
# Build Targets
#

all: dependencies headers $(OUTPUT)

clean:
	rm -fr $(BUILDDIR) $(DISTDIR) $(TEMPDIR)

init:
	echo Initializing directories...
	if test ! -d $(BUILDDIR); then mkdir $(BUILDDIR); fi
	if test ! -d $(TEMPDIR); then mkdir $(TEMPDIR); fi
	if test ! -d $(DISTDIR); then mkdir $(DISTDIR); fi
	if test ! -d $(DISTDIR)/bin; then mkdir $(DISTDIR)/bin; fi
	if test ! -d $(DISTDIR)/include; then mkdir $(DISTDIR)/include; fi

dependencies: init
	echo Compiling .jar dependencies...
	if [ `echo $(DEPENDENCIES) | wc -w` -gt 0 ]; then for DEPENDENCY in "$(DEPENDENCIES)"; do echo Compiling $$DEPENDENCY to $(DISTDIR)/bin/; $(GCJ) $(GCJFLAGS) -c -o $(DISTDIR)/bin/`echo "$$DEPENDENCY"|sed -e 's/^\(.\+\)\/\(.\+\).jar/\2.o/'` $$DEPENDENCY; done; fi

classes: init java-list
	echo Compiling .java files...
	cat $(TEMPDIR)/java-list | xargs $(GCJ) $(GCJFLAGS) --classpath=$(CLASSPATH):$(JAVASRCDIR) -C -d $(BUILDDIR)

headers: init class-list
	echo Generating C++ headers from .class files...
	cat $(TEMPDIR)/class-list | xargs $(GCJH) $(GCJHFLAGS) -cp $(CLASSPATH):$(JAVA_HOME)/jre/lib/rt.jar:$(BUILDDIR) -d $(DISTDIR)/include

$(OUTPUT): init class-list
	echo Building $(OUTPUT)...
	cat $(TEMPDIR)/class-list | xargs $(GCJ) $(GCJFLAGS) --classpath=$(CLASSPATH):$(BUILDDIR) -c -o $(DISTDIR)/bin/$(OUTPUT)

java-list: init
	echo Looking for .java files...
	find $(JAVASRCDIR) -name "*.java" > $(TEMPDIR)/java-list-all
	cp $(TEMPDIR)/java-list-all $(TEMPDIR)/java-list-excl
	touch $(TEMPDIR)/java-list-incl
	if [ `echo $(EXCLUDES) | wc -w` -gt 0 ]; then for EXCLUDE in "$(EXCLUDES)"; do cat $(TEMPDIR)/java-list-excl | grep -v $$EXCLUDE > $(TEMPDIR)/java-list-temp; mv $(TEMPDIR)/java-list-temp $(TEMPDIR)/java-list-excl; done; fi 
	if [ `echo $(INCLUDES) | wc -w` -gt 0 ]; then for INCLUDE in "$(INCLUDES)"; do cat $(TEMPDIR)/java-list-all | grep $$INCLUDE >> $(TEMPDIR)/java-list-incl; done; fi
	cat $(TEMPDIR)/java-list-excl $(TEMPDIR)/java-list-incl | uniq > $(TEMPDIR)/java-list
	echo Marked `wc -l < $(TEMPDIR)/java-list` .java files.

class-list: init classes
	echo Looking for .class files...
	find $(BUILDDIR) -name "*.class" > $(TEMPDIR)/class-list
	echo Marked `wc -l < $(TEMPDIR)/class-list` .class files.

