From f63d58d90c35b056412c9c7d2bafbd608e3af488 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 25 Oct 2021 18:02:46 +0200
Subject: [PATCH] =?UTF-8?q?macOS:=20fix=20compilation=20of=20MEX=20files?=
 =?UTF-8?q?=20against=20Homebrew=E2=80=99s=20octave?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It is necessary to link MEX files against Octave libraries, as under Windows.

(cherry picked from commit e155c5cbb2f6177359d07309ce00252d17f23bab)
---
 mex/build/octave/configure.ac | 16 ++++++++++++----
 mex/build/octave/mex.am       |  5 ++---
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac
index 54afee1e06..14ed2fa1f7 100644
--- a/mex/build/octave/configure.ac
+++ b/mex/build/octave/configure.ac
@@ -98,16 +98,24 @@ case ${host_os} in
     ;;
 esac
 
-# Define an automake conditional for the Windows platform
+# Determine whether to link MEX files against the Octave libraries.
+# mkoctfile no longer does this by default but in practice it is needed
+# for Windows and macOS.
 case ${host_os} in
   *mingw32*)
-        windows_api=yes
+        # This is hardcoded in src/mkoctfile.cc.in.
+        link_octave_libs=yes
+        ;;
+  *darwin*)
+        # Under macOS, the Homebrew formula passes --enable-link-all-dependencies
+        # to the configure script.
+        link_octave_libs=yes
         ;;
   *)
-        windows_api=no
+        link_octave_libs=no
         ;;
 esac
-AM_CONDITIONAL([WINDOWS_API], [test ${windows_api} = yes])
+AM_CONDITIONAL([LINK_OCTAVE_LIBS], [test ${link_octave_libs} = yes])
 
 # Construct final output message
 if test "$enable_mex_dynareplusplus" = yes; then
diff --git a/mex/build/octave/mex.am b/mex/build/octave/mex.am
index ac0fb4721f..fd98f21191 100644
--- a/mex/build/octave/mex.am
+++ b/mex/build/octave/mex.am
@@ -10,9 +10,8 @@ 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)
 
-# 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
+# See the comments in configure.ac
+if LINK_OCTAVE_LIBS
 AM_LDFLAGS += -L"$(shell $(MKOCTFILE) -p OCTLIBDIR)"
 LIBS += $(shell $(MKOCTFILE) -p OCTAVE_LIBS)
 endif
-- 
GitLab