AM_CPPFLAGS = @NEOPZINCL@ @DAKOTAINCL@ @MATLABINCL@ @PETSCINCL@ @MPIINCL@ @SPOOLESINCL@ @METISINCL@ @TRIANGLEINCL@ @CHACOINCL@ @SCOTCHINCL@ @SHAPELIBINCL@ @AMPIINCL@ @ADJOINTMPIINCL@ @MEDIPACKINCL@ @CODIPACKINCL@ @PROJINCL@
AUTOMAKE_OPTIONS = subdir-objects

EXEEXT=$(MATLABWRAPPEREXT)

#define prefix (from http://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Defining-Directories.html)
AM_CPPFLAGS += -DISSM_PREFIX='"$(prefix)"'

#matlab io{{{
if !WINDOWS
lib_LTLIBRARIES = libISSMMatlab.la
else
noinst_LTLIBRARIES = libISSMMatlab.la
lib_LTLIBRARIES =
endif

io_sources = \
	./io/CheckNumMatlabArguments.cpp \
	./io/FetchMatlabData.cpp \
	./io/WriteMatlabData.cpp

ALL_CXXFLAGS = -fPIC -D_WRAPPERS_

if MSYS2
ALL_CXXFLAGS += -D_DO_NOT_LOAD_GLOBALS_ # Cannot have undefined symbols under MSYS2
ALL_CXXFLAGS += -D_USE_MATH_DEFINES # Need to open fence to math.h definitions when --std=c++ is used (which is the default)
endif

ALL_CXXFLAGS += $(CXXOPTFLAGS) $(CXXFLAGS)

libISSMMatlab_la_SOURCES = $(io_sources)
libISSMMatlab_la_CXXFLAGS = ${ALL_CXXFLAGS}
#}}}
#api io{{{
if !WINDOWS
lib_LTLIBRARIES += libISSMApi.la
else
noinst_LTLIBRARIES += libISSMApi.la
endif

if !MSYS2
api_sources= ./io/ApiPrintf.cpp

libISSMApi_la_SOURCES = $(api_sources)
libISSMApi_la_CXXFLAGS = ${ALL_CXXFLAGS}
endif
#}}}
#Wrappers {{{
lib_LTLIBRARIES += \
	BamgConvertMesh_matlab.la \
	BamgMesher_matlab.la \
	BamgTriangulate_matlab.la \
	ContourToMesh_matlab.la \
	ContourToNodes_matlab.la \
	DistanceToMaskBoundary_matlab.la \
	ElementConnectivity_matlab.la \
	ExpSimplify_matlab.la \
	ExpToLevelSet_matlab.la \
	InterpFromGridToMesh_matlab.la \
	InterpFromMesh2d_matlab.la \
	InterpFromMeshToGrid_matlab.la \
	InterpFromMeshToMesh2d_matlab.la \
	InterpFromMeshToMesh3d_matlab.la \
	IssmConfig_matlab.la \
	M1qn3_matlab.la \
	MeshPartition_matlab.la \
	MeshProfileIntersection_matlab.la \
	NodeConnectivity_matlab.la \
	PointCloudFindNeighbors_matlab.la \
	PropagateFlagsFromConnectivity_matlab.la \
	Triangle_matlab.la \
	ProcessRifts_matlab.la \
	Scotch_matlab.la

if CHACO
lib_LTLIBRARIES += Chaco_matlab.la
endif
if KRIGING
lib_LTLIBRARIES += Kriging_matlab.la
endif
if KML
lib_LTLIBRARIES += ShpRead_matlab.la
endif
if PROJ
lib_LTLIBRARIES += CoordTransform_matlab.la
endif
#}}}

# Dependencies {{{
deps = $(MATHLIB)

#Triangle library
CXXFLAGS_FOR_TRI = -DTRILIBRARY -DANSI_DECLARATORS -DNO_TIMER

#Matlab part
CXXFLAGS_FOR_MEX = -D_HAVE_MATLAB_MODULES_ $(MEXOPTFLAGS) $(MEXCXXFLAGS)
CPPFLAGS_FOR_MEX = $(MATLABINCL)
LDFLAGS_FOR_MEX = -module -shrext $(MEXEXT) -no-undefined
LIBADD_FOR_MEX = $(MEXLINKFLAGS) $(MEXLIB)

AM_CPPFLAGS += ${CXXFLAGS_FOR_MEX}
AM_CXXFLAGS = ${CXXFLAGS_FOR_TRI} ${CPPFLAGS_FOR_MEX}
AM_LDFLAGS = ${LDFLAGS_FOR_MEX}

if !MSYS2
AM_LDFLAGS += --export-dynamic -rdynamic
endif

if !VERSION
AM_LDFLAGS += -avoid-version
endif

# NOTE:
# - On Linux, We probably do not need the -static flag as long as we only
#	generate static libraries for external packages. Dynamic system libraries
#	will be linked to, whether we like it or not, if no static version is
#	available.
# - On macOS, static linking of binaries is not supported.
#
if STANDALONE_MODULES
if MAC
AM_LDFLAGS += -Wl,-rpath,'@rpath'
else
AM_LDFLAGS += -Wl,-static -Wl,--disable-new-dtags -Wl,-rpath,'$$ORIGIN'
endif
endif

AM_CXXFLAGS += -fPIC -D_WRAPPERS_

deps += ./libISSMMatlab.la ../../c/libISSMCore.la ../../c/libISSMModules.la

if !MSYS2
deps += ./libISSMApi.la
endif

# if WINDOWS
# deps += $(METISLIB)
# endif

if ADOLC
deps += $(ADOLCLIB)
endif

if FORTRAN
deps += $(FLIBS) $(FORTRANLIB)
endif

deps += ${LIBADD_FOR_MEX}

#Optimization flags:
AM_CXXFLAGS += $(CXXOPTFLAGS)
#}}}
# Module sources and dependencies {{{
if !WINDOWS
libISSMMatlab_la_LIBADD = ./../../c/libISSMCore.la ./../../c/libISSMModules.la $(PETSCLIB) $(MUMPSLIB) $(SCALAPACKLIB) $(BLASLAPACKLIB) $(PARMETISLIB) $(METISLIB) $(HDF5LIB) $(MPILIB) $(NEOPZLIB) $(MKLLIB) $(GSLLIB) $(PROJLIB) $(MATHLIB) $(MEXLIB)
endif

if VERSION
libISSMMatlab_la_LDFLAGS =
libISSMApi_la_LDFLAGS =
else
libISSMMatlab_la_LDFLAGS = -avoid-version
libISSMApi_la_LDFLAGS = -avoid-version
endif

if STANDALONE_LIBRARIES
libISSMMatlab_la_LDFLAGS += -static
libISSMApi_la_LDFLAGS += -static
deps += $(DAKOTALIB) $(PETSCLIB) $(MUMPSLIB) $(SCALAPACKLIB) $(BLASLAPACKLIB) $(PARMETISLIB) $(METISLIB) $(HDF5LIB) $(TAOLIB) $(M1QN3LIB) $(SEMICLIB) $(PLAPACKLIB) $(SUPERLULIB) $(SPOOLESLIB) $(TRIANGLELIB) $(BLACSLIB) $(HYPRELIB) $(SPAILIB) $(PROMETHEUSLIB) $(PASTIXLIB) $(MLLIB) $(SCOTCHLIB) $(MKLLIB) $(MPILIB) $(NEOPZLIB) $(MATHLIB) $(GRAPHICSLIB) $(MULTITHREADINGLIB) $(GSLLIB) $(ADOLCLIB) $(AMPILIB) $(METEOIOLIB) $(SNOWPACKLIB) $(PROJLIB) $(OSLIBS) ${LIBADD_FOR_MEX}
endif

if !WINDOWS
libISSMApi_la_LIBADD = $(PETSCLIB) $(MUMPSLIB) $(SCALAPACKLIB) $(BLASLAPACKLIB) $(PARMETISLIB) $(METISLIB) $(HDF5LIB) $(MPILIB) $(NEOPZLIB) $(GSLLIB) $(PROJLIB) $(MATHLIB) $(MEXLIB)
endif

BamgConvertMesh_matlab_la_SOURCES = ../BamgConvertMesh/BamgConvertMesh.cpp
BamgConvertMesh_matlab_la_LIBADD = ${deps}

BamgMesher_matlab_la_SOURCES = ../BamgMesher/BamgMesher.cpp
BamgMesher_matlab_la_LIBADD = ${deps}

BamgTriangulate_matlab_la_SOURCES = ../BamgTriangulate/BamgTriangulate.cpp
BamgTriangulate_matlab_la_LIBADD = ${deps}

if CHACO
Chaco_matlab_la_SOURCES = ../Chaco/Chaco.cpp
Chaco_matlab_la_LIBADD = ${deps} $(CHACOLIB)
endif

ContourToMesh_matlab_la_SOURCES = ../ContourToMesh/ContourToMesh.cpp
ContourToMesh_matlab_la_LIBADD = ${deps}

ContourToNodes_matlab_la_SOURCES = ../ContourToNodes/ContourToNodes.cpp
ContourToNodes_matlab_la_LIBADD = ${deps}

CoordTransform_matlab_la_SOURCES = ../CoordTransform/CoordTransform.cpp
CoordTransform_matlab_la_LIBADD = ${deps}

DistanceToMaskBoundary_matlab_la_SOURCES = ../DistanceToMaskBoundary/DistanceToMaskBoundary.cpp
DistanceToMaskBoundary_matlab_la_LIBADD = ${deps}

ElementConnectivity_matlab_la_SOURCES = ../ElementConnectivity/ElementConnectivity.cpp
ElementConnectivity_matlab_la_LIBADD = ${deps}

ExpToLevelSet_matlab_la_SOURCES = ../ExpToLevelSet/ExpToLevelSet.cpp
ExpToLevelSet_matlab_la_LIBADD = ${deps}

ExpSimplify_matlab_la_SOURCES = ../ExpSimplify/ExpSimplify.cpp
ExpSimplify_matlab_la_LIBADD = ${deps}

InterpFromGridToMesh_matlab_la_SOURCES = ../InterpFromGridToMesh/InterpFromGridToMesh.cpp
InterpFromGridToMesh_matlab_la_LIBADD = ${deps}

InterpFromMesh2d_matlab_la_SOURCES = ../InterpFromMesh2d/InterpFromMesh2d.cpp
InterpFromMesh2d_matlab_la_LIBADD = ${deps}

InterpFromMeshToGrid_matlab_la_SOURCES = ../InterpFromMeshToGrid/InterpFromMeshToGrid.cpp
InterpFromMeshToGrid_matlab_la_LIBADD = ${deps}

InterpFromMeshToMesh2d_matlab_la_SOURCES = ../InterpFromMeshToMesh2d/InterpFromMeshToMesh2d.cpp
InterpFromMeshToMesh2d_matlab_la_LIBADD = ${deps}

InterpFromMeshToMesh3d_matlab_la_SOURCES = ../InterpFromMeshToMesh3d/InterpFromMeshToMesh3d.cpp
InterpFromMeshToMesh3d_matlab_la_LIBADD = ${deps}

IssmConfig_matlab_la_SOURCES = ../IssmConfig/IssmConfig.cpp
IssmConfig_matlab_la_LIBADD = ${deps}

Kriging_matlab_la_SOURCES = ../Kriging/Kriging.cpp
Kriging_matlab_la_LIBADD = ${deps}

M1qn3_matlab_la_SOURCES = ../M1qn3/M1qn3.cpp
M1qn3_matlab_la_LIBADD = ${deps}

MeshPartition_matlab_la_SOURCES = ../MeshPartition/MeshPartition.cpp
MeshPartition_matlab_la_LIBADD = ${deps}

MeshProfileIntersection_matlab_la_SOURCES = ../MeshProfileIntersection/MeshProfileIntersection.cpp
MeshProfileIntersection_matlab_la_LIBADD = ${deps}

NodeConnectivity_matlab_la_SOURCES = ../NodeConnectivity/NodeConnectivity.cpp
NodeConnectivity_matlab_la_LIBADD = ${deps}

PointCloudFindNeighbors_matlab_la_SOURCES = ../PointCloudFindNeighbors/PointCloudFindNeighbors.cpp
PointCloudFindNeighbors_matlab_la_LIBADD = ${deps}

PropagateFlagsFromConnectivity_matlab_la_SOURCES = ../PropagateFlagsFromConnectivity/PropagateFlagsFromConnectivity.cpp
PropagateFlagsFromConnectivity_matlab_la_LIBADD = ${deps}

Scotch_matlab_la_SOURCES = ../Scotch/Scotch.cpp
Scotch_matlab_la_LIBADD = ${deps}

ShpRead_matlab_la_SOURCES = ../ShpRead/ShpRead.cpp
ShpRead_matlab_la_LIBADD = ${deps} $(SHAPELIBLIB)

Triangle_matlab_la_SOURCES = ../Triangle/Triangle.cpp
Triangle_matlab_la_LIBADD = ${deps} $(TRIANGLELIB)

ProcessRifts_matlab_la_SOURCES = ../ProcessRifts/ProcessRifts.cpp
ProcessRifts_matlab_la_LIBADD = ${deps}
#}}}
