# Makefile for the JNI portion of Sojourner 
#                                  (SojournerNode.java & SojournerNode.c)


# ------------------------------------------------------------------------------


# This definition makes JAVA_HOME refer to the JDK specified in your PATH.
# You can change this to refer to a specific directory, such as /usr/lpp/J1.1.1

JAVA_HOME = `dirname \`which java\``/..


# Name of library, usually in the form libXXX.so where XXX is the name passed
# to System.loadLibrary().  If this Makefile is to be used for .class files
# only, then LIBRARY can be omitted.

LIBRARY   = libnode.so


# Class files that don't contain native methods.

CLASSES   =


# Class files that contain native methods, and for which there is a
# corresponding .c file.  If this makefile is to be used for .class files only,
# then NATIVECLASSES can be omitted.

NATIVECLASSES = SojournerNode.class


# The .o files associated with any extra .c or .C modules to be included
# in the build.  Note:  These MUST be given here as .o files.

EXTRA_C_MODULES =


# Extra .o modules to include in the build.  Unlike with EXTRA_C_MODULES,
# these modules are NOT deleted by 'make clean'.

EXTRA_O_MODULES =


# Extra Java compile options

JAVA_COMPILE_OPTIONS =


# Extra C compile options, e.g. include directories

C_COMPILE_OPTIONS = -lrpcsvc


# Choice between C++ and C module
# set LINKER to either $(C_LINKER) or $(CPP_LINKER) to select which kind of
# module you are creating.  The compiler is automatically selected depending on
# the extension of the source file (.C or .c)

LINKER     = $(C_LINKER)


# Extra link options

LINK_OPTIONS = -lrpcsvc



# ------------------------------------------------------------------------------

# For simple projects, it shouldn't be necessary to modify this section.


.SUFFIXES: .java .class .h .o .c .C .u

# This bit handles automatic header file dependencies.
all:
	@touch $(NATIVECLASSES:.class=.u) $(EXTRA_C_MODULES:.o=.u)
	@(cat $(NATIVECLASSES:.class=.u) $(EXTRA_C_MODULES:.o=.u);\
             echo "include Makefile") > Makefile.tmp
	@make -f Makefile.tmp all_1
	@rm Makefile.tmp

all_1: $(LIBRARY) $(CLASSES) $(NATIVECLASSES)

C_LINKER   = ld -bnoentry -bM:SRE -blibpath:/lib:/usr/lib -lc_r \
               -blibpath:/usr/lib/threads:/usr/lib:/lib -bE:
CPP_LINKER = /usr/lpp/xlC/bin/makeC++SharedLib -lC_r -lC -p -1 -L/usr/lpp/xlC/lib \
               -blibpath:/usr/lib/threads:/usr/lib:/lib -E

$(LIBRARY): $(NATIVECLASSES:.class=.h) $(NATIVECLASSES:.class=.o) \
            $(EXTRA_C_MODULES) $(EXTRA_O_MODULES)
	grep " JNICALL " $(NATIVECLASSES:.class=.h) | sed "s/.* JNICALL //g">$@.exp
	$(LINKER)$@.exp \
           -L$(JAVA_HOME)/lib/aix/native_threads -ljava $(LINK_OPTIONS) \
           -o $@ $(NATIVECLASSES:.class=.o) $(EXTRA_C_MODULES) $(EXTRA_O_MODULES)
	rm $@.exp

.c.o:
	@mkdir -p $(@D)
	xlc_r -c -M -I. -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/aix \
              $(C_COMPILE_OPTIONS) -o $@ $<

.C.o:
	@mkdir -p $(@D)
	xlC_r -c -M -I. -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/aix \
              $(C_COMPILE_OPTIONS) -o $@ $<

# If the class is within a package, then this makefile puts the .h file into
# the directory structure, which is different from the default javah output
# filename.
# For example, we would use testpkg/Native.h instead of javah's default of
# testpkg_Native.h.
# This is done to keep the dependencies simple.
.class.h:
	$(JAVA_HOME)/bin/javah -jni -o $*.h `echo $* | sed s?/?\.?g`

.java.class:
	$(JAVA_HOME)/bin/javac $(JAVA_COMPILE_OPTIONS) $<


clean:
	rm -f $(CLASSES) $(NATIVECLASSES)
	rm -f $(NATIVECLASSES:.class=.o)
	rm -f $(NATIVECLASSES:.class=.u)
	rm -f $(NATIVECLASSES:.class=.h)
	rm -f $(EXTRA_C_MODULES)
	rm -f $(EXTRA_C_MODULES:.o=.u)
	rm -f $(LIBRARY)
