## @configure_input@
##
## @file    Makefile.in
## @brief   Makefile template for libSBML Java bindings
## @author  Mike Hucka
## @author  Ben Kovitz
## @author  Akiya Jouraku
##
## <!--------------------------------------------------------------------------
## This file is part of libSBML.  Please visit http://sbml.org for more
## information about SBML, and the latest version of libSBML.
##
## Copyright (C) 2013-2016 jointly by the following organizations:
##     1. California Institute of Technology, Pasadena, CA, USA
##     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
##     3. University of Heidelberg, Heidelberg, Germany
##
## Copyright (C) 2009-2013 jointly by the following organizations: 
##     1. California Institute of Technology, Pasadena, CA, USA
##     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
##  
## Copyright (C) 2006-2008 by the California Institute of Technology,
##     Pasadena, CA, USA 
##  
## Copyright (C) 2002-2005 jointly by the following organizations: 
##     1. California Institute of Technology, Pasadena, CA, USA
##     2. Japan Science and Technology Agency, Japan
## 
## 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.  A copy of the license agreement is provided
## in the file named "LICENSE.txt" included with this software distribution
## and also available online as http://sbml.org/software/libsbml/license.html
## ------------------------------------------------------------------------ -->


# -----------------------------------------------------------------------------
# Configuration variables
# -----------------------------------------------------------------------------
# Some of the following are substituted automatically by `configure'.  If
# you are looking at "Makefile", do not edit these values; instead, run the
# configure script at the top level of the src tree.  It will recreate
# "Makefile".

include @top_srcdir@/config/makefile-common-vars.mk

# `srcdir' points to the current directory, but should be set by configure.
# `subdir' must be set manually to the relative dir under srcdir.  Don't 
# set `subdir' to an absolute path, or some make actions will fail.

srcdir            = @srcdir@
thisdir           = src/bindings/java

# In the normal core C++/C libSBML code, `sources' are used by the default
# rules to determine what needs to be compiled.  In these language
# bindings, the variable is not used by default rules, and everything is
# handled explicitly below.  However, to maintain some resemblance to the
# other libSBML build organization, we still use the name `sources'.

sources           = libsbml_wrap.cpp libsbml_wrap.h javadoc.i

extra_sources     = libsbml.i                          \
                    local.cpp                          \
                    local.i                            \
                    local-doc-extras.i                 \
                    local-downcast-extension-layout.i  \
                    local-downcast-extension.i.in      \
					local-downcast-converters.i.in     \
					local-downcast-converters-layout.i \
                    local-downcast-namespaces-layout.i \
                    local-downcast-namespaces.i.in     \
                    local-packages-layout.i            \
                    local-packages.i.in

# The SWIG interface files should not need to be regenerated for recipients
# of the libSBML distributions; only developers should need to regenerate
# them.  The conditions for this are handled later below.

swig_sources      = $(extra_sources) ../swig/libsbml.h ../swig/libsbml.i


# Variables `subdirs', `headers', `sources', `libraries', `extra_CPPFLAGS',
# `extra_CXXFLAGS', `extra_LDFLAGS' and `distfiles' are used by the default
# rules in `makefile-common-actions.mk' included at the end.

extra_CPPFLAGS    = $(JAVA_CPPFLAGS) -I../swig
SWIGFLAGS        += $(JAVA_CPPFLAGS) -I../swig

extra_LDFLAGS     = $(JAVA_LDFLAGS)

extra_LIBS        = $(JAVA_LIBS)

ifdef USE_EXPAT
  extra_CPPFLAGS += @EXPAT_CPPFLAGS@ 
  extra_LDFLAGS  += @EXPAT_LDFLAGS@
  extra_LIBS     += @EXPAT_LIBS@
endif

ifdef USE_XERCES
  extra_CPPFLAGS += @XERCES_CPPFLAGS@
  extra_LDFLAGS  += @XERCES_LDFLAGS@
  extra_LIBS     += @XERCES_LIBS@
endif

ifdef USE_LIBXML
  extra_CPPFLAGS += @LIBXML_CPPFLAGS@
  extra_LDFLAGS  += @LIBXML_LDFLAGS@
  extra_LIBS     += @LIBXML_LIBS@
endif

# SBML package plug-ins
ifdef USE_COMP
  SWIGFLAGS      += -DUSE_COMP
  SWIGDOCDEFINES += --define USE_COMP
endif # USE_COMP  

ifdef USE_FBC
  SWIGFLAGS      += -DUSE_FBC
  SWIGDOCDEFINES += --define USE_FBC
endif # USE_FBC  

ifdef USE_GROUPS
  SWIGFLAGS      += -DUSE_GROUPS
  SWIGDOCDEFINES += --define USE_GROUPS
endif # USE_GROUPS  

ifdef USE_LAYOUT
  SWIGFLAGS      += -DUSE_LAYOUT
  SWIGDOCDEFINES += --define USE_LAYOUT
endif # USE_LAYOUT  

ifdef USE_QUAL
  SWIGFLAGS      += -DUSE_QUAL
  SWIGDOCDEFINES += --define USE_QUAL
endif # USE_QUAL  

# -fno-strict-aliasing option is added to CXXFLAGS when using GCC.  As
# written in the SWIG Manual (section 20.2.4), SWIG generated code could
# cause crash problems when compiled by GCC with -fstrict-aliasing option
# (automatically enabled when using -O2 or higher optimization in GCC 3 or
# later) and the option should be disabled by adding -fno-strict-aliasing
# option.
#
# Currently, we know that Java binding library causes crash when it is
# compiled by GCC 4 with -O2 or higher optimization, and the problem can be
# avoided by adding -fno-strict-aliasing option.

ifeq (GCC,$(findstring GCC,$(shell $(CXX) -dumpversion | sed -e 's/^[34]\./GCC/' )))
      CXXFLAGS += -fno-strict-aliasing
endif

# By default, the compliance level of eclipse JDT compiler (ecj) is Java
# 1.4 which doesn't support covariant return types.  To avoid the
# incompatible issue, "-5" option which sets the compliance level to Java
# 1.5 is added if ecj is detected.

ifeq (1.5 compliance, $(findstring 1.5 compliance, $(shell ${JAVAC} -help 2>&1 | grep "1.5 compliance" )))
  JAVAC_OPTION += -5
endif

# We're building two libraries: the shared JNI library, and the Jar file.

java_package      = org.sbml.$(PACKAGE)
jarfile           = libsbmlj.jar
jnifile           = $(JNIBASENAME).$(JNIEXT)

libraries         = $(jarfile) $(jnifile)

# We generate the $(jnifile) (C++ library for System.loadLibrary) with an
# object file in this directory (libsbml_wrap.o) and other object files in
# sub directories in 'src' directory (src/sbml, src/common, ... ) instead
# of linking a shared library of libSBML (libsbml.$(SHAREDLIBEXT)).

# All directories that contain *.{c,cpp} (implementation) files of libSBML
# need to be listed in the following 'otherdirs' variable.

otherdirs = ../../sbml/common ../../sbml/util ../../sbml/xml \
            ../../sbml/math ../../sbml/validator ../../sbml/validator/constraints \
            ../../sbml/units ../../sbml/annotation ../../sbml/compress \
            ../../sbml/ ../../sbml/extension ../../sbml/conversion

ifdef USE_COMP
  otherdirs += ../../sbml/packages/comp/common \
               ../../sbml/packages/comp/extension \
               ../../sbml/packages/comp/sbml \
               ../../sbml/packages/comp/util \
               ../../sbml/packages/comp/validator \
               ../../sbml/packages/comp/validator/constraints 
endif
ifdef USE_FBC
  otherdirs += ../../sbml/packages/fbc/common \
               ../../sbml/packages/fbc/extension \
               ../../sbml/packages/fbc/sbml \
               ../../sbml/packages/fbc/util \
               ../../sbml/packages/fbc/validator \
               ../../sbml/packages/fbc/validator/constraints 
endif
ifdef USE_GROUPS
  otherdirs += ../../sbml/packages/groups/common \
               ../../sbml/packages/groups/extension \
               ../../sbml/packages/groups/sbml \
               ../../sbml/packages/groups/validator \
               ../../sbml/packages/groups/validator/constraints 
endif
ifdef USE_LAYOUT
  otherdirs += ../../sbml/packages/layout/common \
               ../../sbml/packages/layout/extension \
               ../../sbml/packages/layout/sbml \
               ../../sbml/packages/layout/util \
               ../../sbml/packages/layout/validator \
               ../../sbml/packages/layout/validator/constraints 
endif
ifdef USE_QUAL
  otherdirs += ../../sbml/packages/qual/common \
               ../../sbml/packages/qual/extension \
               ../../sbml/packages/qual/sbml \
               ../../sbml/packages/qual/validator \
               ../../sbml/packages/qual/validator/constraints 
endif

objfiles = libsbml_wrap.lo $(wildcard $(addsuffix /*.lo,$(otherdirs)))

# The next set of variable definitions are special to this file and not
# hooks into `makefile-common-actions.mk'.  The values depend in part on
# Java conventions and the output of SWIG.

outdir      = java-files
pkg_path    = $(subst .,/,$(java_package))
outpath     = $(outdir)/$(pkg_path)
java_files  = $(wildcard $(outpath)/*.java)
class_files = $(java_files:.java=.class)

# The libSBML Java API library on MacOS X has to be built as a "bundle"
# rather than a .dylib library.  This is different from the default rules
# used elsewhere in libSBML and requires the following special value for
# `platform_link_flags'.  The rest of the default rules for .so's will
# behave properly once this is set.

ifeq "$(HOST_TYPE)" "darwin"
  platform_link_flags = -bundle
endif

# Files for test system.

test_dir       = test
test_src_path  = test/org/sbml/libsbml/test
check_sources  = AutoTestRunner.java $(wildcard $(test_src_path)/**/Test*.java)

# `distfiles' determines the files and directories included in a
# distribution.  The strategy here is that we include the output of SWIG
# (both libsbml_wrap.cpp and the java files) but not the compiled class
# files.

distfiles = Makefile.in                \
            Manifest.txt.in            \
            CMakeLists.txt             \
            compile-native-files.cmake \
            swig-binding.cmake.in      \
            $(check_sources)           \
            $(extra_sources)           \
            $(java_files)              \
            $(sources)

# `extra_clean' and `extra_distclean' determine the files and directories
# removed during "make clean" and "make distclean".

extra_clean = $(class_files) $(check_sources:.java=.class) test_copy1.xml \
  test_write.xml TestFilenameFilter.class $(wildcard test.xml.*) out.xml

extra_distclean = Manifest.txt local-downcast-extension.i \
    local-downcast-namespaces.i local-packages.i javadoc.i

# For Linux/Unix, LD_LIBRARY_PATH *may* need to contain one or more library
# paths to libsbml.so, libxerces-c.so and/or libexpat.so and/or the
# libsbml.so _libsbml.so generated by this Makfile.  (For BSD/MacOS X
# replace LD_LIBRARY_PATH with DYLD_LIBRARY_PATH.)


# -----------------------------------------------------------------------------
# Primary build actions
# -----------------------------------------------------------------------------

# This whole setup is more or less custom and mostly doesn't use the common
# build directives of `makefile-common-actions.mk'.

.SUFFIXES:
.SUFFIXES: .i .cpp .h .java .class .jar .o .obj .so .dylib .lo .la

# The default action is to remake everything.

ifdef USE_JAVA
  all: Makefile libsbml_wrap.cpp $(outpath)/libsbml.class $(libraries)
else
  all:
	@echo
	@echo "Reconfigure libSBML using --with-java to enable Java support.";
	@echo
	@exit 1
endif

# Rummage through ../swig/libsbml.i to figure out the core libSBML files that
# are our dependencies.

main_sources = \
  $(shell grep '%include sbml' ../swig/libsbml.i | grep -v 'libsbml-version.h' \
    | cut -f2- -d'/' | sed -e 's,^,../../sbml/,g')

# Recreate SWIG files when one of the following conditions is true:
#
#  (1) libsbml_wrap.cpp does not exist
#  (2) libsbml_wrap.cpp exists, but the file is not consistent with the
#       current USE_LAYOUT, USE_COMP, etc., values.
#
# If the last run was made with a package flag like --enable-layout, and this
# run isn't, or vice-versa, the swig wrap files will be wrong for this run.
# However, you can't tell just by looking at file mod times, so you can't use
# normal make dependencies.  So we do this grungy trick of looking inside
# libsbml_wrap.cpp to see what's inside.
#
# The check_foo macros works as follows.  They return a non-empty string
# "do_reswig" if re-swig'ing is called for, and this string is a phony make
# target declared below.  When check_layout is used as a dependency of
# libsbml_wrap.cpp via a call like this,
# 
#  libsbml_wrap.cpp: $(shell $(call check_layout)) ...other dependencies...
#   ...ACTIONS...
#
# what will happen is either check_layout will return nothing, in which
# case the dependency is satisfied and no swig'ing needs to be done, or
# it will return "do_reswig", a phony target that is never satisfied,
# in which case the ...ACTIONS... will be executed.

.PHONY: do_reswig

ifdef USE_COMP
  define check_comp
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep getListOfSubmodels libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_comp
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep getListOfSubmodels libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif

ifdef USE_FBC
  define check_fbc
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep ListOfFluxBounds libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_fbc
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep ListOfFluxBounds libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif

ifdef USE_GROUPS
  define check_groups
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep ListOfGroups libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_groups
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep ListOfGroups libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif

ifdef USE_LAYOUT
  define check_layout
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep getListOfLayouts libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_layout
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep getListOfLayouts libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif

ifdef USE_QUAL
  define check_qual
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep ListOfQualitativeSpecies libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_qual
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep ListOfQualitativeSpecies libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif

# Generate the documentation files.
#
# javadoc-extras.i contain manually-written javadoc additions that are appended
# to the javadoc.i file created by our swigdoc.py.  It contains things that are
# Java-specific (thus, don't belong in libsbml.i) and don't need to be
# processed by swigdoc.py.

javadoc.i: $(main_sources) $(swig_sources) ../swig/swigdoc.py $(wildcard ../swig/*.i)
	touch ../../../docs/src/libsbml-converters.txt && ../swig/swigdoc.py --language java --top ../../.. $(SWIGDOCDEFINES) \
		--extra ../../../docs/src/libsbml-converters.txt \
		--master ../swig/libsbml.i --output javadoc.i

# libsbml_wrap.cpp is a stand-in for the .java files.  Its creation results
# in the creation of the .java files too -- both are made by running swig.

libsbml_wrap.cpp $(outpath): javadoc.i $(swig_sources) $(main_sources) \
    $(shell $(call check_comp)) $(shell $(call check_fbc)) \
    $(shell $(call check_layout)) $(shell $(call check_qual)) \
    $(shell $(call check_groups))
	@top_srcdir@/config/chk_swig_version.sh || exit
ifneq "$(MAKEFLAGS)" ""
	$(MAKE) -$(MAKEFLAGS) clean-swigfiles
else
	$(MAKE) clean-swigfiles
endif
	test -d $(outpath) || mkdir -p $(outpath)
	"$(SWIG)" $(SWIGFLAGS) $(sort $(default_includes) $(INCLUDES)) \
		-c++ -java -package $(java_package) \
		-outdir $(outpath) -o libsbml_wrap.cpp libsbml.i

# We use a trick involving marking methods as deprecated with the Javadoc
# @deprecated comment directive, but unfortunately javac seems to pay
# attention to that and thinks we're trying to use deprecated methods.  The
# contortion below, involving the pipe and grep, filters the output from
# javac so that people don't get confused.

compile_command = "$(JAVAC)" $(JAVAC_OPTION) -d $(outdir) $(outpath)/*.java

$(outpath)/libsbml.class: libsbml_wrap.cpp $(outpath)
	@echo $(compile_command)
	$(shell $(compile_command) 2>&1 | grep -v deprecat)

$(jarfile): $(outpath)/libsbml.class 
	$(CD) $(outdir) && "$(JAR)" -cvfm ../$(jarfile) ../Manifest.txt $(pkg_path)/*.class

# Force removal of the java-files directory.

clean-swigfiles: clean-generic clean-libtool clean-extras
	-rm -rf $(outdir) libsbml_wrap.cpp 


# -----------------------------------------------------------------------------
# Checking.
# -----------------------------------------------------------------------------

check_classes = $(check_sources:.java=.class)

ifeq "$(HOST_TYPE)" "cygwin"
  classpath = $(shell cygpath -wp $(jarfile):$(TOP_BUILDDIR)/src:$(test_dir):.)
  CPPFLAGS += -DNOMINMAX
else
  classpath = $(jarfile):$(TOP_BUILDDIR)/src:$(test_dir):.
endif

# See the note above regarding the Javadoc @deprecated comment and the
# reason for the following contortion.

check_compile_command = "$(JAVAC)" $(JAVAC_OPTION) -g -classpath "$(classpath)" $(check_sources)

$(check_classes): Makefile $(libraries) $(check_sources)
	@echo $(check_compile_command)
	$(shell $(check_compile_command) 2>&1 | grep -v deprecat)

ifeq "$(HOST_TYPE)" "darwin"
  define runjavatest
    env DYLD_LIBRARY_PATH="$(RUN_LDPATH):$(DYLD_LIBRARY_PATH):." "$(JAVA)" -classpath "$(classpath)" AutoTestRunner
  endef
else
  define runjavatest
    env LD_LIBRARY_PATH="$(RUN_LDPATH):$(LD_LIBRARY_PATH):." "$(JAVA)" -Djava.library.path=. -classpath "$(classpath)" AutoTestRunner
  endef
endif

# 2010-07-05 <mhucka@caltech.edu> For some reason, I can't get runjavatest
# or the previous version of it to work in my environment anymore.  I'm
# switching to a straight invocation of java.

check: $(check_classes) $(shell $(call check_layout)) \
  $(shell $(call check_comp))  $(shell $(call check_fbc)) \
  $(shell $(call check_qual)) $(shell $(call check_groups))
	@echo
	@echo
	@echo Running Tests in $(thisdir)
	@echo -----------------------------------------------------------------
	$(runjavatest)
	@echo -----------------------------------------------------------------
	@echo

#
# 'make test-files' will reconstruct the test files.  This is not made part
# of 'make check' (yet).
#

test-files:
	$(call test_translator,j,xml math annotation sbml,test/org/sbml/libsbml/test)


# -----------------------------------------------------------------------------
# Tags.
# -----------------------------------------------------------------------------

tags: etags ctags



# -----------------------------------------------------------------------------
# Installation.
# -----------------------------------------------------------------------------

JNI_INSTPATH=$(DESTDIR)$(LIBDIR)/$(jnifile)
JAR_INSTPATH=$(DESTDIR)$(DATADIR)/java/$(jarfile)

install: $(libraries) $(shell $(call check_layout)) \
  $(shell $(call check_comp)) $(shell $(call check_fbc)) \
  $(shell $(call check_qual)) $(shell $(call check_groups))
	$(INSTALL_SH) $(jnifile) $(JNI_INSTPATH)
	$(INSTALL_SH) $(jarfile) $(JAR_INSTPATH)

uninstall:
	@list='$(JNI_INSTPATH) $(JAR_INSTPATH)'; for lib in $$list; do \
	  if test -f "$$lib"; then \
	    echo rm "$$lib"; \
	    rm $$lib; \
	  fi; \
	done;

installcheck:



# -----------------------------------------------------------------------------
# Creating distribution (for libSBML maintainers only)
# -----------------------------------------------------------------------------

dist: libsbml_wrap.cpp $(outdir) dist-normal

distcheck: distcheck-normal



# -----------------------------------------------------------------------------
# Miscellaneous
# -----------------------------------------------------------------------------

include @top_srcdir@/config/makefile-common-actions.mk



# -----------------------------------------------------------------------------
# End.
# -----------------------------------------------------------------------------
