From fe8aaf44fdef66ea965f976e585e1daf48194562 Mon Sep 17 00:00:00 2001
From: Willi Mutschler <willi@mutschler.eu>
Date: Mon, 23 May 2022 10:22:57 +0200
Subject: [PATCH] :apple: provisions for Apple Silicon package

---
 src/ModelTree.cc | 17 ++++++++++++-----
 src/ModelTree.hh |  2 +-
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 80b23c0b..7e83bc78 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1783,6 +1783,8 @@ ModelTree::matlab_arch(const string &mexext)
     }
   else if (mexext == "mexmaci64")
     return "maci64";
+  else if (mexext == "mexmaca64")
+    return "maca64";
   else
     {
       cerr << "ERROR: 'mexext' option to preprocessor incorrectly set, needed with 'use_dll'" << endl;
@@ -1792,7 +1794,7 @@ ModelTree::matlab_arch(const string &mexext)
 
 #ifdef __APPLE__
 string
-ModelTree::findGccOnMacos()
+ModelTree::findGccOnMacos(const string &mexext)
 {
   const string macos_gcc_version{"11"}; // doc/manual/source/installation-and-configuration.rst
                                         // should be updated when this is changed
@@ -1805,10 +1807,15 @@ ModelTree::findGccOnMacos()
       local_gcc_path = s.substr(0, s.find_last_of("/")) + "/../.brew/bin/gcc-" + macos_gcc_version;
     }
 
+  // if user did not choose to install gcc locally via the pkg-installer then we need to find GNU gcc
+  // homebrew binaries are located in /usr/local/bin/ on x86_64 systems and in /opt/homebrew/bin/ on arm64 systems
   if (filesystem::exists(local_gcc_path))
     return local_gcc_path;
   else if (string global_gcc_path = "/usr/local/bin/gcc-" + macos_gcc_version;
-           filesystem::exists(global_gcc_path))
+           filesystem::exists(global_gcc_path) && mexext == "mexmaci64")
+    return global_gcc_path;
+  else if (string global_gcc_path = "/opt/homebrew/bin/gcc-" + macos_gcc_version;
+           filesystem::exists(global_gcc_path) && mexext == "mexmaca64")
     return global_gcc_path;
   else
     {
@@ -1843,7 +1850,7 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
 #ifdef __APPLE__
       /* On macOS, enforce GCC, otherwise Clang will be used, and it does not
          accept our custom optimization flags (see dynare#1797) */
-      string gcc_path = findGccOnMacos();
+      string gcc_path = findGccOnMacos(mexext);
       if (setenv("CC", gcc_path.c_str(), 1) != 0)
         {
           cerr << "Can't set CC environment variable" << endl;
@@ -1892,9 +1899,9 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
             }
         }
 #ifdef __APPLE__
-      else if (mexext == "mexmaci64")
+      else if (mexext == "mexmaci64" || mexext == "mexmaca64")
         {
-          compiler = findGccOnMacos();
+          compiler = findGccOnMacos(mexext);
           flags << " -fno-common -Wl,-twolevel_namespace -undefined error -bundle";
           libs += " -lm";
         }
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 5b9bd966..d3e2a5c3 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -398,7 +398,7 @@ private:
   static string matlab_arch(const string &mexext);
 #ifdef __APPLE__
   //! Finds a suitable GCC compiler on macOS
-  static string findGccOnMacos();
+  static string findGccOnMacos(const string &mexext);
 #endif
   //! Compiles a MEX file
   void compileMEX(const string &basename, const string &funcname, const string &mexext, const vector<filesystem::path> &src_files, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
-- 
GitLab