From 87d515bc207b9a4429f1b964cbcb7877a842baae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 15 Mar 2023 17:35:25 +0100
Subject: [PATCH] Windows package: improve hack for statically linking libgomp
 into MEX files

Link only the needed parts, instead of embedding the whole library.
---
 m4/ax_mexopts.m4                          | 8 +++++---
 mex/build/kronecker.am                    | 2 +-
 mex/build/local_state_space_iterations.am | 2 +-
 mex/build/perfect_foresight_problem.am    | 3 +--
 mex/build/qmc_sequence.am                 | 2 +-
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index 8c1150287d..52c7505811 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -60,8 +60,10 @@ case ${MATLAB_ARCH} in
     # 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"
+    # Hack for static linking of libgomp, needed for OpenMP.
+    # Should come by the end of the link command
+    # (otherwise it will have to be enclosed within --whole-archive).
+    OPENMP_LIBS="-Wl,-Bstatic -lgomp -Wl,-Bdynamic"
     ax_mexopts_ok="yes"
     ;;
   maci64)
@@ -129,5 +131,5 @@ AC_SUBST([MATLAB_CXXFLAGS])
 AC_SUBST([MATLAB_FCFLAGS])
 AC_SUBST([MATLAB_LDFLAGS])
 AC_SUBST([MATLAB_LIBS])
-AC_SUBST([OPENMP_LDFLAGS])
+AC_SUBST([OPENMP_LIBS])
 ])
diff --git a/mex/build/kronecker.am b/mex/build/kronecker.am
index ac07bb6020..9308edf08d 100644
--- a/mex/build/kronecker.am
+++ b/mex/build/kronecker.am
@@ -4,7 +4,7 @@ nodist_sparse_hessian_times_B_kronecker_C_SOURCES = sparse_hessian_times_B_krone
 nodist_A_times_B_kronecker_C_SOURCES = A_times_B_kronecker_C.f08 matlab_mex.F08 blas_lapack.F08
 
 sparse_hessian_times_B_kronecker_C_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-sparse_hessian_times_B_kronecker_C_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+sparse_hessian_times_B_kronecker_C_LDADD = $(OPENMP_LIBS)
 
 BUILT_SOURCES = $(nodist_sparse_hessian_times_B_kronecker_C_SOURCES) $(nodist_A_times_B_kronecker_C_SOURCES)
 CLEANFILES = $(nodist_sparse_hessian_times_B_kronecker_C_SOURCES) $(nodist_A_times_B_kronecker_C_SOURCES)
diff --git a/mex/build/local_state_space_iterations.am b/mex/build/local_state_space_iterations.am
index e9281345f3..ddff330da4 100644
--- a/mex/build/local_state_space_iterations.am
+++ b/mex/build/local_state_space_iterations.am
@@ -5,7 +5,7 @@ nodist_local_state_space_iteration_3_SOURCES = local_state_space_iteration_3.f08
 nodist_local_state_space_iteration_k_SOURCES = local_state_space_iteration_k.f08
 
 local_state_space_iteration_2_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-local_state_space_iteration_2_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+local_state_space_iteration_2_LDADD = $(OPENMP_LIBS)
 
 local_state_space_iteration_3_FCFLAGS = $(AM_FCFLAGS) -I../libkordersim -pthread
 local_state_space_iteration_3_LDADD = ../libkordersim/libkordersim.a 
diff --git a/mex/build/perfect_foresight_problem.am b/mex/build/perfect_foresight_problem.am
index 2c186d815c..6046a09c22 100644
--- a/mex/build/perfect_foresight_problem.am
+++ b/mex/build/perfect_foresight_problem.am
@@ -6,8 +6,7 @@ nodist_perfect_foresight_problem_SOURCES = perfect_foresight_problem.cc DynamicM
 
 perfect_foresight_problem_CPPFLAGS = $(AM_CPPFLAGS) -I$(TOPDIR)
 perfect_foresight_problem_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-perfect_foresight_problem_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
-perfect_foresight_problem_LDADD = $(LIBADD_DLOPEN)
+perfect_foresight_problem_LDADD = $(LIBADD_DLOPEN) $(OPENMP_LIBS)
 
 BUILT_SOURCES = $(nodist_perfect_foresight_problem_SOURCES)
 CLEANFILES = $(nodist_perfect_foresight_problem_SOURCES)
diff --git a/mex/build/qmc_sequence.am b/mex/build/qmc_sequence.am
index 5ee862daea..2cb09b2bfe 100644
--- a/mex/build/qmc_sequence.am
+++ b/mex/build/qmc_sequence.am
@@ -4,7 +4,7 @@ TOPDIR = $(top_srcdir)/../../sources/sobol
 
 qmc_sequence_CPPFLAGS = $(AM_CPPFLAGS) -I$(TOPDIR)
 qmc_sequence_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-qmc_sequence_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+qmc_sequence_LDADD = $(OPENMP_LIBS)
 
 nodist_qmc_sequence_SOURCES = qmc_sequence.cc
 
-- 
GitLab