## @configure_input@
##
## @file    Makefile.in
## @brief   Makefile template for libSBML Ruby bindings 
## @author  Alex Gutteridge
## @author  Mike Hucka
## @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-2018 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/ruby

# `sources' are used by the default rules to determine what needs to be
# compiled.

sources       =  libsbml_wrap.cpp libsbml_wrap.h
extra_sources =  libsbml.i \
                 local-packages.i.in \
				 local-layout.i \
				 local-l3v2extendedmath.i \
                 local.cpp \
				 local.i \
         local-downcast-astplugins-l3v2extendedmath.cpp     \
         local-downcast-astplugins.cpp.in     \
				 local-downcast-extension-layout.cpp \
				 local-downcast-extension-l3v2extendedmath.cpp \
				 local-downcast-extension.cpp.in \
				 local-downcast-namespaces.cpp.in \
				 local-downcast-namespaces-layout.cpp \
				 local-downcast-namespaces-l3v2extendedmath.cpp \
				 local-downcast-converters.cpp.in \
				 local-downcast-converters-layout.cpp \
				 local-downcast-plugins-layout.cpp \
				 local-downcast-plugins-l3v2extendedmath.cpp \
				 local-downcast-plugins.cpp.in \
				 local-downcast-packages-layout.cpp \
				 local-downcast-packages-l3v2extendedmath.cpp \
				 local-downcast.cpp.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

# What we're building is a module for use with ruby.  

# The libSBML Ruby API library on MacOS X has to be built as a "bundle"
# rather than a .dylib library.

ifeq "$(HOST_TYPE)" "darwin"
  platform_link_flags = -bundle
  libraries           = libSBML.bundle

  # Environment variable MACOSX_DEPLOYMENT_TARGET must be set to 10.3 or later
  # to use -undefined dynamic_lookup.

  macosx_version = $(shell sw_vers -productVersion | cut -d"." -f1,2)
  export MACOSX_DEPLOYMENT_TARGET=${macosx_version}
else
  libraries = libSBML.$(SHAREDLIBEXT)
endif

# If we're building 64-bit binaries, but are not running under 10.6, the 
# version of ruby will probably be 32 bit instead of 64 bit.

ifdef USE_UNIVBINARY
  ifneq "$(MACOS_VERSION)" "10.6"
  ifneq "$(MACOS_VERSION)" "10.7"
  ifneq "$(MACOS_VERSION)" "10.8"
  ifneq "$(MACOS_VERSION)" "10.9"

    filtered_cflags   = $(shell echo '$(CFLAGS)'   | sed -e "s/-arch x86_64//g")
    filtered_cppflags = $(shell echo '$(CPPFLAGS)' | sed -e "s/-arch x86_64//g")
    filtered_cxxflags = $(shell echo '$(CXXFLAGS)' | sed -e "s/-arch x86_64//g")
    filtered_ldflags  = $(shell echo '$(LDFLAGS)'  | sed -e "s/-arch x86_64//g")

    CFLAGS   := $(filtered_cflags)
    CPPFLAGS := $(filtered_cppflags)
    CXXFLAGS := $(filtered_cxxflags)
    LDFLAGS  := $(filtered_ldflags)
  endif    
  endif    
  endif    
  endif
endif

# 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 += $(RUBY_CPPFLAGS) -I../swig -I../..
SWIGFLAGS      += $(RUBY_CPPFLAGS) -I../swig

# 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  

ifdef USE_MULTI
  SWIGFLAGS      += -DUSE_MULTI
  SWIGDOCDEFINES += --define USE_MULTI
endif # USE_MULTI  

ifdef USE_RENDER
  SWIGFLAGS      += -DUSE_RENDER
  SWIGDOCDEFINES += --define USE_RENDER
endif # USE_RENDER

ifdef USE_L3V2EXTENDEDMATH
  SWIGFLAGS      += -DUSE_L3V2EXTENDEDMATH
  SWIGDOCDEFINES += --define USE_L3V2EXTENDEDMATH
endif # USE_L3V2EXTENDEDMATH  


# On MacOS X, compilation generates a warning about `long double'.

ifeq "$(HOST_TYPE)" "darwin"
  ifdef HAS_GCC_WNO_LONG_DOUBLE
    extra_CPPFLAGS += -Wno-long-double
  endif
endif

extra_LDFLAGS  += $(RUBY_LDFLAGS)
extra_LIBS     += $(RUBY_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

# -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.

ifeq (GCC,$(findstring GCC,$(shell $(CXX) --version)))
  CXXFLAGS += -fno-strict-aliasing
endif

# We generates a C++ library for Ruby bindings 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
ifdef USE_MULTI
  otherdirs += ../../sbml/packages/multi/common \
               ../../sbml/packages/multi/extension \
               ../../sbml/packages/multi/sbml \
               ../../sbml/packages/multi/validator \
               ../../sbml/packages/multi/validator/constraints 
endif
ifdef USE_RENDER
  otherdirs += ../../sbml/packages/render/common \
               ../../sbml/packages/render/extension \
               ../../sbml/packages/render/sbml \
               ../../sbml/packages/render/validator \
               ../../sbml/packages/render/validator/constraints 
endif
ifdef USE_L3V2EXTENDEDMATH
  otherdirs += ../../sbml/packages/l3v2extendedmath/common \
               ../../sbml/packages/l3v2extendedmath/extension \
               ../../sbml/packages/l3v2extendedmath/validator \
               ../../sbml/packages/l3v2extendedmath/validator/constraints 
endif

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

# Files for test system.

test_dir      = test
check_sources = test.rb $(wildcard $(test_dir)/**/Test*.rb) $(wildcard ruby/**/**/**/**/*.rb) $(wildcard ruby/**/**/**/*.rb)  $(wildcard ruby/**/**/*.rb)  $(wildcard ruby/**/*.rb)

# `distfiles' determines the files and directories included in a
# distribution.  `distfiles_exclude' is used to filter out specific items,
# so that even if they appear in `distfiles', they will not be copied in
# the distribution.

distfiles = Makefile.in      \
            CMakeLists.txt   \
            $(check_sources) \
            $(extra_sources) \
            README.txt       \
            $(sources)

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

extra_clean = build $(wildcard test.xml.*)
extra_distclean = \
	local-downcast-extension.i local-downcast-extension.cpp \
	local-downcast-namespaces.i local-downcast-namespaces.cpp \
	local-packages.i local-downcast-plugins.cpp local-downcast.cpp

# 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 generated by this Makfile.  (For BSD/MacOS X replace 
# LD_LIBRARY_PATH with DYLD_LIBRARY_PATH.)

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

.SUFFIXES:
.SUFFIXES: .i .cpp .o .obj .lo .la

# The default action is to remake everything.

ifdef USE_RUBY
  all: Makefile libsbml_wrap.cpp $(libraries)
else
  all:
	@echo
	@echo "Reconfigure libSBML using --with-ruby to enable Ruby 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

ifdef USE_MULTI
  define check_multi
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep ListOfSpeciesFeatures libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_multi
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep ListOfSpeciesFeatures libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif
ifdef USE_RENDER
  define check_render
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -z "`grep ListOfGlobalRenderInformation libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
else
  define check_render
    if test ! -f libsbml_wrap.cpp; then echo "do_reswig"; exit; fi; \
    if test -n "`grep ListOfGlobalRenderInformation libsbml_wrap.cpp`"; then echo "do_reswig"; fi;
  endef
endif
ifdef USE_L3V2EXTENDEDMATH
  define check_l3v2extendedmath
    if test ! -f LibSBML_wrap.cxx; then echo "do_reswig"; exit; fi; 
  endef
else
  define check_l3v2extendedmath
    if test ! -f LibSBML_wrap.cxx; then echo "do_reswig"; exit; fi; 
  endef
endif

libsbml_wrap.cpp: $(swig_sources) $(main_sources) \
  $(shell $(call check_comp)) $(shell $(call check_fbc)) \
  $(shell $(call check_layout)) $(shell $(call check_qual)) \
  $(shell $(call check_groups)) $(shell $(call check_multi)) \
  $(shell $(call check_render)) $(shell $(call check_l3v2extendedmath)) 
	@top_srcdir@/config/chk_swig_version.sh || exit
	$(SWIG) $(SWIGFLAGS) $(sort $(default_includes) $(INCLUDES)) \
		-c++ -ruby -o libsbml_wrap.cpp libsbml.i

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

check: $(libraries) $(shell $(call check_layout)) \
  $(shell $(call check_comp)) $(shell $(call check_fbc)) \
  $(shell $(call check_qual)) $(shell $(call check_groups)) \
  $(shell $(call check_multi)) $(shell $(call check_render)) \
  $(shell $(call check_l3v2extendedmath))
	@echo
	@echo
	@echo Running Tests in $(thisdir)
	@echo -----------------------------------------------------------------
	$(call libsbmlrun, $(RUBY) -I./ -I./ruby test.rb)
	@echo -----------------------------------------------------------------
	@echo

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

test-files:
	$(call test_translator,r,xml math annotation sbml,test)

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

install:
	archdir=$(RUBY_INSTALL_DIR); \
	for i in $(libraries); do \
	  $(MKINSTALLDIRS) $(DESTDIR)$$archdir; \
	  $(INSTALL_SH) $$i $(DESTDIR)$$archdir/$$i; \
	done ; \

installcheck:

uninstall:
	-archdir=$(RUBY_INSTALL_DIR); \
	for i in $(libraries); do \
	  rm $(DESTDIR)$$archdir/$$i; \
	done ; \

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

dist: libsbml_wrap.cpp dist-normal

distcheck: distcheck-normal


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

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


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