From a2faf641277e82c9d50b9a92446612f93701b9f3 Mon Sep 17 00:00:00 2001
From: Willi Mutschler <willi@mutschler.eu>
Date: Tue, 31 May 2022 11:48:00 +0200
Subject: [PATCH] Provisions to detect Apple Silicon architecture (maca64 and
 mexmaca64)

Works only with MATLAB R2022a beta (ARM)

(cherry picked from commit 1317a39b371a952da9a50b2fbe4580ec11ba0009)
---
 .gitignore                           |  1 +
 README.md                            |  4 ++--
 doc/manual/source/running-dynare.rst |  2 +-
 m4/ax_matlab_arch.m4                 |  3 +++
 m4/ax_mexopts.m4                     | 13 +++++++++++++
 matlab/add_path_to_mex_files.m       |  4 ++--
 6 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/.gitignore b/.gitignore
index 82e8a64446..a5a9becfc1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -83,6 +83,7 @@ doc/internals/ltxpng
 *.mexmaca64
 *.mexmaci
 *.mexmaci64
+*.mexmaca64
 /mex/matlab/
 /mex/octave/
 
diff --git a/README.md b/README.md
index 36ea891262..7c0dfc5dd2 100644
--- a/README.md
+++ b/README.md
@@ -45,8 +45,8 @@ determine the type of your MATLAB/Octave installation, type:
 ```matlab
 >> computer
 ```
-at the MATLAB/Octave prompt. Under MATLAB, if it returns `PCWIN64`, `GLNX64` or
-`MACI64`, then it is a 64-bit MATLAB; if it returns `PCWIN`, `MACI` or `GLNX`,
+at the MATLAB/Octave prompt. Under MATLAB, if it returns `PCWIN64`, `GLNX64`,
+`MACA64` or `MACI64`, then it is a 64-bit MATLAB; if it returns `PCWIN`, `MACI` or `GLNX`,
 then it is a 32-bit MATLAB. Under Octave, if it returns a string that begins
 with `x86_64`, it is a 64-bit Octave; if the strings begins with `i686`, it is
 a 32-bit Octave.
diff --git a/doc/manual/source/running-dynare.rst b/doc/manual/source/running-dynare.rst
index 0e0bc48390..6e2b906478 100644
--- a/doc/manual/source/running-dynare.rst
+++ b/doc/manual/source/running-dynare.rst
@@ -315,7 +315,7 @@ by the ``dynare`` command.
         Prevent Dynare from printing the output of the steps leading up to the
         preprocessor as well as the preprocessor output itself.
 
-    .. option:: mexext=mex|mexw32|mexw64|mexmaci64|mexa64
+    .. option:: mexext=mex|mexw32|mexw64|mexmaca64|mexmaci64|mexa64
 
         The mex extension associated with your platform to be used
         when compiling output associated with :opt:`use_dll`.
diff --git a/m4/ax_matlab_arch.m4 b/m4/ax_matlab_arch.m4
index e37bd52288..c9902b3ad3 100644
--- a/m4/ax_matlab_arch.m4
+++ b/m4/ax_matlab_arch.m4
@@ -57,6 +57,9 @@ else
       *mexmaci64)
 	ax_cv_matlab_arch=maci64
 	;;
+      *mexmaca64)
+	ax_cv_matlab_arch=maca64
+	;;
       *)
 	ax_cv_matlab_arch=unknown
 	;;
diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index 52c7505811..5e3e9ad846 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -79,6 +79,19 @@ case ${MATLAB_ARCH} in
     MATLAB_LIBS="-L$MATLAB/bin/maci64 -lmx -lmex -lmat -lmwlapack -lmwblas"
     ax_mexopts_ok="yes"
     ;;
+  maca64)
+    MATLAB_DEFS="-DNDEBUG"
+    MATLAB_CFLAGS="-fno-common -fexceptions"
+    MATLAB_CXXFLAGS="-fno-common -fexceptions"
+    MATLAB_FCFLAGS="-fexceptions -fbackslash"
+    MATLAB_LDFLAGS_NOMAP="-Wl,-twolevel_namespace -undefined error -bundle"
+    MATLAB_LDFLAGS="$MATLAB_LDFLAGS_NOMAP -Wl,-exported_symbols_list,\$(abs_top_srcdir)/mexFunction-MacOSX.map"
+    # This -L flag is put here, hence later on the linker command line, so as
+    # to avoid linking against the HDF5 shipped by MATLAB (which would
+    # otherwise override the HDF5 from Homebrew)
+    MATLAB_LIBS="-L$MATLAB/bin/maca64 -lmx -lmex -lmat -lmwlapack -lmwblas"
+    ax_mexopts_ok="yes"
+    ;;
   *)
     ax_mexopts_ok="no"
     ;;
diff --git a/matlab/add_path_to_mex_files.m b/matlab/add_path_to_mex_files.m
index 2a352d82fc..24ccaf15e4 100644
--- a/matlab/add_path_to_mex_files.m
+++ b/matlab/add_path_to_mex_files.m
@@ -70,9 +70,9 @@ else
         end
     end
     % Add macOS paths for Dynare Mac package
-    if strcmp(computer, 'MACI64')
+    if strcmp(computer, 'MACI64') || strcmp(computer, 'MACA64')
         if matlab_ver_less_than('9.4')
-            tmp = [dynareroot '../mex/matlab/maci64-8.3-9.3/'];
+            tmp = [dynareroot '../mex/matlab/' lower(computer) '-8.3-9.3/'];
             if exist(tmp, 'dir')
                 mexpath = tmp;
                 if modifypath
-- 
GitLab