From b8352b251d55c94aaca66f2b6ae97f675f26b7c5 Mon Sep 17 00:00:00 2001
From: Willi Mutschler <willi@mutschler.eu>
Date: Tue, 25 Apr 2023 23:51:42 +0200
Subject: [PATCH] macOS: provisions to detect Apple Silicon architecture
 (maca64 and mexmaca64)

Works with MATLAB R2022b beta (ARM)
---
 README.md                            | 4 ++--
 doc/manual/source/running-dynare.rst | 2 +-
 m4/ax_matlab_arch.m4                 | 5 ++++-
 m4/ax_mexopts.m4                     | 4 ++--
 matlab/add_path_to_mex_files.m       | 9 +++++++++
 5 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 4c59c93c00..d77a436f7f 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`,
+`MACI64` or `MACA64` 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 abef1d1a1b..7a0987cca3 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|mexmaci64|mexmaca64|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..3def1809a8 100644
--- a/m4/ax_matlab_arch.m4
+++ b/m4/ax_matlab_arch.m4
@@ -1,7 +1,7 @@
 dnl matlabarch.m4 --- check for MATLAB machine architecture.
 dnl
 dnl Copyright © 2002, 2003 Ralph Schleicher
-dnl Copyright © 2009 Dynare Team
+dnl Copyright © 2009-2023 Dynare Team
 dnl
 dnl This program is free software; you can redistribute it and/or
 dnl modify it under the terms of the GNU General Public License as
@@ -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 644ffa0141..1a5ef95125 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -68,7 +68,7 @@ case ${MATLAB_ARCH} in
     FORTRAN_LIBS_FOR_CXXLINK="-Wl,-Bstatic -lgfortran -Wl,-Bdynamic" # libquadmath is already in MATLAB_LDFLAGS_NOMAP
     ax_mexopts_ok="yes"
     ;;
-  maci64)
+  mac[[ai]]64)
     MATLAB_DEFS="-DNDEBUG"
     MATLAB_CFLAGS="-fno-common -fexceptions"
     MATLAB_CXXFLAGS="-fno-common -fexceptions"
@@ -78,7 +78,7 @@ case ${MATLAB_ARCH} in
     # 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/maci64 -lmx -lmex -lmat -lmwlapack -lmwblas"
+    MATLAB_LIBS="-L$MATLAB/bin/${MATLAB_ARCH} -lmx -lmex -lmat -lmwlapack -lmwblas"
     FORTRAN_LIBS_FOR_CXXLINK="-lgfortran -lquadmath"
     ax_mexopts_ok="yes"
     ;;
diff --git a/matlab/add_path_to_mex_files.m b/matlab/add_path_to_mex_files.m
index 2a352d82fc..97548b1ea9 100644
--- a/matlab/add_path_to_mex_files.m
+++ b/matlab/add_path_to_mex_files.m
@@ -89,6 +89,15 @@ else
             end
         end
     end
+    if strcmp(computer, 'MACA64')
+        tmp = [dynareroot '../mex/matlab/maca64-9.14/'];
+        if exist(tmp, 'dir')
+            mexpath = tmp;
+            if modifypath
+                addpath(mexpath);
+            end
+        end
+    end
     % Add generic MATLAB path (with higher priority than the previous ones)
     if exist('mexpath')
         mexpath = { mexpath; [dynareroot '../mex/matlab/'] };
-- 
GitLab