# This file is part of Java-GNOME.
#
# Java-GNOME is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Java-GNOME 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Java-GNOME; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

CLASSPATH = @CLASSPATH@:java:

OBJECTS:= \
	$(shell ls jni/*.c  | sort | sed -e 's=\.c=\.o=g')

SOURCE_PATH:=java/org/gnu/gtk/*.java \
			java/org/gnu/gtk/event/*.java \
			java/org/gnu/glib/*.java \
			java/org/gnu/gdk/*.java \
			java/org/gnu/pango/*.java \
			java/org/gnu/atk/*.java

JAVA_SOURCE_PATH:=java


gtkversion = @gtkversion@
gtkapiversion = @gtkapiversion@

# Set Java related stuff
JAVAC = @JAVAC@
JAVA = @JAVA@
JAR = @JAR@

# If you need to change this line please let us know
JAVA_INCLUDES =

# Set C related stuff
CC = @CC@
CFLAGS= @CFLAGS@ -fPIC -Wall
GTK_CFLAGS=@GTK_CFLAGS@
GTK_LIBS=@GTK_LIBS@
GCJ=@GCJ@ -fPIC -fjni
GCJFLAGS=
GCJ_CLASSPATH=@GCJ_CLASSPATH@.
INSTALL=@INSTALL@

.SUFFIXES: .java .class

.c.o:
	$(CC) -c $(CFLAGS) $< $(JAVA_INCLUDES) $(GTK_CFLAGS) \
                -o $@

.java.o:
	$(GCJ) $(GCJFLAGS) -g -O --classpath=${GCJ_CLASSPATH} -c $< \
        -o $@


CLASSES:= \
	$(shell /bin/ls $(SOURCE_PATH) \
				| sort | sed -e 's=\.java=\.class=g' \
				| sed -e 's=\.\./==g' \
				| sed -e 's=\s*\(.*\)\s*= build-java/\1 =g' \
				| sed -e 's=build-java/.*/org/\(.*\)\s*= build-java/org/\1 =g' \
			) 

dirs: ../lib build-java

../lib/:
	$(INSTALL) -d ../lib
build-java:
	$(INSTALL) -d build-java

build-java/%.class: java/%.java
	CLASSPATH=$(JAVA_SOURCE_PATH) $(JAVAC)  -d build-java   \
		$<
	
build-java/%.o: java/%.java
	$(GCJ) $(GCJFLAGS) -g -O --classpath=$(JAVA_SOURCE_PATH):${GCJ_CLASSPATH} -o $@ -c \
		$<
		
COMPILED_CLASSES:=$(shell echo $(CLASSES) | sed -e 's=\.class=\.o=g' )

../lib/gtk${gtkapiversion}.jar: $(CLASSES)
	(cd build-java; \
	$(JAR) cf ../../lib/gtk${gtkapiversion}-${gtkversion}.jar \
		`find -name "*.class"` 	)
	(cd ../lib; test ! -L gtk${gtkapiversion}.jar && ln -s gtk${gtkapiversion}-${gtkversion}.jar gtk${gtkapiversion}.jar) || exit 0

jar: dirs ../lib/gtk${gtkapiversion}.jar


../lib/libgtkjava${gtkapiversion}.so.${gtkversion} : $(OBJECTS)
	@(test ! -d ../lib && mkdir ../lib) || exit 0
	$(CC) $(CFLAGS) -shared  \
		$(CFLAGS) $(GTK_LIBS) $(JAVA_INCLUDES) -Xlinker --no-undefined \
		$(OBJECTS) \
		-o ../lib/libgtkjava${gtkapiversion}.so.${gtkversion} 
	(cd ../lib; test ! -L libgtkjava${gtkapiversion}.so && ln -s libgtkjava${gtkapiversion}.so.${gtkversion} libgtkjava${gtkapiversion}.so) || exit 0

nativelib: ../lib/libgtkjava${gtkapiversion}.so.${gtkversion}

../lib/libgtkjar${gtkapiversion}.so.${gtkversion}: $(COMPILED_CLASSES)
	$(GCJ) $(GCJFLAGS) -g -shared $(COMPILED_CLASSES) -o ../lib/libgtkjar${gtkapiversion}.so.${gtkversion}
	@(cd ../lib; test ! -L libgtkjar${gtkapiversion}.so && ln -s libgtkjar${gtkapiversion}.so.${gtkversion} libgtkjar${gtkapiversion}.so) || exit 0

native-gcj: ../lib/libgtkjar${gtkapiversion}.so.${gtkversion}

all: dirs jar nativelib @GCJ_BUILD_TARGETS@

# Targets to clean up after us
.PHONY: mostlyclean clean distclean maintainer-clean dirs all native-gcj nativelib jar

mostlyclean:

clean: mostlyclean
	rm -f jni/*.o 
	rm -rf build-java/
	rm -rf ../lib/

distclean: clean
	rm -f Makefile

maintainer-clean: distclean

