From 9ab35894a91f3a020c9712a31404e8e38e4cfb7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 30 May 2024 14:48:03 +0200
Subject: [PATCH] macOS package: fix unresolved symbols in qmc_sequence MEX
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fixes https://forum.dynare.org/t/invalid-mex-file-whe-running-sensitivity-analysis-m1-mac-6-1/25829

It seems that meson does not link libquadmath (which is needed by libgfortran).
When doing dynamic linking, this is not an issue since the dynamic linker loads
libquadmath recursively after loading libgfortran. But when doing static linking,
libquadmath needs to be explicitly linked.

Not yet clear at this point: why isn’t this needed under Linux and Windows?

(cherry picked from commit f1e10960d7c6f544d55ab3f548735e93a7aaff1a)
---
 meson.build | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/meson.build b/meson.build
index 6a28f218e6..a318621f00 100644
--- a/meson.build
+++ b/meson.build
@@ -386,6 +386,16 @@ qmc_sequence_mex_kwargs = mex_kwargs
 if get_option('prefer_static') and host_machine.system() == 'windows'
   qmc_sequence_mex_kwargs += { 'link_args' : qmc_sequence_mex_kwargs.get('link_args') + [ '-Wl,-Bstatic', '-lgfortran', '-Wl,-Bdynamic' ] }
 endif
+# Hack for statically linking libquadmath
+# Fixes https://forum.dynare.org/t/invalid-mex-file-whe-running-sensitivity-analysis-m1-mac-6-1/25829
+# It seems that meson does not link libquadmath (which is needed by libgfortran).
+# When doing dynamic linking, this is not an issue since the dynamic linker loads
+# libquadmath recursively after loading libgfortran. But when doing static linking,
+# libquadmath needs to be explicitly linked.
+# FIXME: Why isn’t this needed under Linux and Windows?
+if get_option('prefer_static') and host_machine.system() == 'darwin'
+  qmc_sequence_mex_kwargs += { 'link_args' : qmc_sequence_mex_kwargs.get('link_args') + [ '-lquadmath' ] }
+endif
 shared_module('qmc_sequence', qmc_sequence_src, kwargs : qmc_sequence_mex_kwargs, dependencies : [ blas_dep, openmp_dep ])
 
 shared_module('kalman_steady_state', 'mex/sources/kalman_steady_state/kalman_steady_state.cc', kwargs : mex_kwargs, dependencies : slicot_dep)
-- 
GitLab