AM_CPPFLAGS = $(shell $(MKOCTFILE) -p CPPFLAGS)
AM_CPPFLAGS += $(shell $(MKOCTFILE) -p INCFLAGS)
AM_CPPFLAGS += -I$(top_srcdir)/../../sources

DEFS += -DOCTAVE_MEX_FILE
DEFS += -DMEXEXT=\".mex\"

AM_CFLAGS = $(shell $(MKOCTFILE) -p CPICFLAG) -Wall -Wno-parentheses # TODO: use same warnings as C++
AM_FCFLAGS = $(shell $(MKOCTFILE) -p FPICFLAG) -Wall -Wimplicit-interface
AM_CXXFLAGS = -std=gnu++20 $(shell $(MKOCTFILE) -p CXXPICFLAG) -Wall -Wno-dangling-else -Wextra -Wold-style-cast
AM_LDFLAGS = $(shell $(MKOCTFILE) -p DL_LDFLAGS)

# See the comments in configure.ac
if LINK_OCTAVE_LIBS
AM_LDFLAGS += -L"$(shell $(MKOCTFILE) -p OCTLIBDIR)"
LIBS += $(shell $(MKOCTFILE) -p OCTAVE_LIBS)
endif

# Unconditionally link against BLAS/LAPACK, since we tend to use them in
# many MEX files
LIBS += $(shell $(MKOCTFILE) -p BLAS_LIBS)
LIBS += $(shell $(MKOCTFILE) -p LAPACK_LIBS)

mexdir = $(libdir)/dynare/mex/octave

all-local: $(PROGRAMS)
## Create symlinks in mex/octave/, but only if the build is not
## out-of-tree (because of the Windows installer)
	if [ "$(abs_srcdir)" = "$(abs_builddir)" ]; then \
		$(MKDIR_P) $(top_srcdir)/../../octave && \
		cd $(top_srcdir)/../../octave && \
		for p in $(PROGRAMS); do \
			$(LN_S) -f $(abs_builddir)/$$p $$p; \
		done; \
	fi

clean-local:
	rm -f *.mod # Fortran modules
	if test -d $(top_srcdir)/../../octave; then \
		cd $(top_srcdir)/../../octave && rm -f $(PROGRAMS); \
	fi

# Rules for the Fortran 2008 interface to MEX, BLAS/LAPACK and pthread functions
matlab_mat.mod: matlab_mex.o
matlab_mex.mod: matlab_mex.o

matlab_mex.F08: $(top_srcdir)/../../sources/matlab_mex.F08
	$(LN_S) -f $< $@

blas.mod: blas_lapack.o
lapack.mod: blas_lapack.o

blas_lapack.F08: $(top_srcdir)/../../sources/blas_lapack.F08
	$(LN_S) -f $< $@

pthread.mod: pthread.o

pthread.F08: $(top_srcdir)/../../sources/pthread.F08
	$(LN_S) -f $< $@
