From 12008bc6eb56fecaefebb1be0c9cf4fbd5296767 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 15 Mar 2023 17:02:49 +0100
Subject: [PATCH] Windows package: statically link libssp inside MEX files

Since 3244340e7a20f45cc0a67d25ceb84f88f5c2ec85, the MEX files are built with
-fstack-protector. But this added a dynamic link to libssp-0.dll on some MEX
files, making them unusable.
---
 m4/ax_mexopts.m4 | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index 8847d48b2a..8c1150287d 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -51,7 +51,15 @@ case ${MATLAB_ARCH} in
     # using threads, since we are using the POSIX threads version of MinGW)
     MATLAB_LDFLAGS_NOMAP="-static-libgcc -static-libstdc++ -static-libgfortran -Wl,-Bstatic,--whole-archive -lquadmath -Wl,-Bdynamic,--no-whole-archive -shared -L$MATLAB/bin/win64 -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,-Bdynamic,--no-whole-archive"
     MATLAB_LDFLAGS="$MATLAB_LDFLAGS_NOMAP \$(abs_top_srcdir)/mex.def"
-    MATLAB_LIBS="-lmex -lmx -lmat -lmwlapack -lmwblas"
+    # Hack for libssp, which is pulled in by -fstack-protector
+    # (curiously only on some MEX files), see windows/build.sh.
+    # Note that the link against libssp should not happen with compilers
+    # from MSYS2, see:
+    # https://www.msys2.org/news/#2022-10-10-libssp-is-no-longer-required
+    # But it happens with Debian’s cross compilers (as of Debian “bookworm” 12).
+    # Also note that the -lssp must come by the end of the link command
+    # (otherwise it will have to be enclosed within --whole-archive).
+    MATLAB_LIBS="-lmex -lmx -lmat -lmwlapack -lmwblas -Wl,-Bstatic -lssp -Wl,-Bdynamic"
     # Hack for static linking of libgomp, needed for OpenMP
     OPENMP_LDFLAGS="-Wl,-Bstatic,--whole-archive -lgomp -Wl,-Bdynamic,--no-whole-archive"
     ax_mexopts_ok="yes"
-- 
GitLab