From 97e9609c28fb4bda15b699da033f02fe02ca6a0e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 24 May 2021 18:21:23 +0200
Subject: [PATCH] Streamline use_dll compilation under MATLAB
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

— drop Windows 32-bit support
— drop -fno-omit-frame-pointer compilation flag under GNU/Linux
— drop -static-libstdc++ flag under Windows
— no longer link to -lstdc++ under GNU/Linux and macOS
— gracefully fail when 'mexext' value is unknown
---
 src/ModelTree.cc | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index b540bcb5..e6fc89d7 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1927,24 +1927,19 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
       flags << " -L " << bin_dir;
       flags << " -fexceptions -DNDEBUG";
       libs = "-lmex -lmx";
-      if (mexext == "mexglx" || mexext == "mexa64")
+      if (mexext == "mexa64")
         {
           // GNU/Linux
           flags << " -D_GNU_SOURCE -fPIC -pthread"
                 << " -shared -Wl,--no-undefined -Wl,-rpath-link," << bin_dir;
-          libs += " -lm -lstdc++";
-
-          if (mexext == "mexglx")
-            flags << " -D_FILE_OFFSET_BITS=64 -m32";
-          else
-            flags << " -fno-omit-frame-pointer";
+          libs += " -lm";
         }
-      else if (mexext == "mexw32" || mexext == "mexw64")
+      else if (mexext == "mexw64")
         {
           // Windows
-          flags << " -static-libgcc -static-libstdc++ -shared";
+          flags << " -static-libgcc -shared";
           // Put the MinGW environment shipped with Dynare in the path
-          auto mingwpath = dynareroot / (string{"mingw"} + (mexext == "mexw32" ? "32" : "64")) / "bin";
+          auto mingwpath = dynareroot / "mingw64" / "bin";
           string newpath = "PATH=" + mingwpath.string() + ';' + string{getenv("PATH")};
           if (putenv(const_cast<char *>(newpath.c_str())) != 0)
             {
@@ -1952,13 +1947,13 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
               exit(EXIT_FAILURE);
             }
         }
-      else
+#ifdef __APPLE__
+      else if (mexext == "mexmaci64")
         {
           // macOS
-#ifdef __APPLE__
           char dynare_m_path[PATH_MAX];
           uint32_t size = PATH_MAX;
-          string gcc_relative_path = "";
+          string gcc_relative_path;
           if (_NSGetExecutablePath(dynare_m_path, &size) == 0)
             {
               string str = dynare_m_path;
@@ -1975,9 +1970,14 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
                    << "You can do this via the Dynare installation package." << endl;
               exit(EXIT_FAILURE);
             }
-#endif
           flags << " -fno-common -Wl,-twolevel_namespace -undefined error -bundle";
-          libs += " -lm -lstdc++";
+          libs += " -lm";
+        }
+#endif
+      else
+        {
+          cerr << "ERROR: unsupported value '" << mexext << "' for 'mexext' option" << endl;
+          exit(EXIT_FAILURE);
         }
     }
 
-- 
GitLab