From 91e3348806cbaafd14cace04ddcd5127717e1569 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 21 Sep 2021 17:58:05 +0200
Subject: [PATCH] Windows package: fix bug introduced in
 c5120e12780220b14e27aeb71f9e4426b1a94895

---
 mex/build/octave/configure.ac | 11 +++++++++++
 mex/build/octave/mex.am       | 14 +++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac
index 6a38305d05..b1d7f23ce1 100644
--- a/mex/build/octave/configure.ac
+++ b/mex/build/octave/configure.ac
@@ -98,6 +98,17 @@ case ${host_os} in
     ;;
 esac
 
+# Define an automake conditional for the Windows platform
+case ${host_os} in
+  *mingw32*)
+        windows_api=yes
+        ;;
+  *)
+        windows_api=no
+        ;;
+esac
+AM_CONDITIONAL([WINDOWS_API], [test ${windows_api} = yes])
+
 # Construct final output message
 if test "$enable_mex_dynareplusplus" = yes; then
    BUILD_GENSYLV_KORDER_DYNSIMUL_MEX_OCTAVE="yes"
diff --git a/mex/build/octave/mex.am b/mex/build/octave/mex.am
index f9d03cd4bf..ac0fb4721f 100644
--- a/mex/build/octave/mex.am
+++ b/mex/build/octave/mex.am
@@ -8,7 +8,19 @@ DEFS += -DMEXEXT=\".mex\"
 AM_CFLAGS = $(shell $(MKOCTFILE) -p CPICFLAG) -Wall -Wno-parentheses
 AM_FCFLAGS = $(shell $(MKOCTFILE) -p FPICFLAG) -Wall -Wimplicit-interface
 AM_CXXFLAGS = $(shell $(MKOCTFILE) -p CXXPICFLAG) -Wall -Wno-parentheses -Wold-style-cast
-AM_LDFLAGS = $(shell $(MKOCTFILE) -p DL_LDFLAGS) -L"$(shell $(MKOCTFILE) -p OCTLIBDIR)"
+AM_LDFLAGS = $(shell $(MKOCTFILE) -p DL_LDFLAGS)
+
+# Mimick the behaviour of mkoctfile: link against Octave libraries on Windows only
+# (it is required there, otherwise we get a link failure)
+if WINDOWS_API
+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
 
-- 
GitLab