From 2cfc7dd57a062c35c3e0dfbf8e26f2a6dfb55004 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 31 Aug 2021 17:57:16 +0200
Subject: [PATCH] Build system: fix detection of SLICOT for MATLAB on RHEL
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The MATLAB_CFLAGS variable was not injected when testing for the presence of
libslicot64_pic. In particular, on GNU/Linux system, this means that the -fPIC
flag was not injected when doing the test. This was not a problem on Debian,
where GCC now enables -fPIC by default. But this would fail on RHEL, where
-fPIC is not enabled by default.

By the way:
— rename ac_save_* variables to my_save_*, to avoid conflicts with internal
  autoconf variables;
— do not inject “$(mkoctfile -p LFLAGS)” into LDFLAGS when testing for SLICOT
  under Octave; this is no longer necessary, since those flags are already
  injected at the beginning of mex/build/octave/configure.ac.

(cherry picked from commit 3d6f86c234dd3accd281a4a9aa8a4de4e0013144)
---
 m4/ax_slicot.m4               | 10 +++++++---
 mex/build/matlab/configure.ac |  4 ++--
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/m4/ax_slicot.m4 b/m4/ax_slicot.m4
index b53e64480c..ae9793693d 100644
--- a/m4/ax_slicot.m4
+++ b/m4/ax_slicot.m4
@@ -37,7 +37,7 @@ AC_DEFUN([AX_SLICOT],
   else
     LDFLAGS_SLICOT=""
   fi
-  ac_save_LDFLAGS=$LDFLAGS
+  my_save_LDFLAGS=$LDFLAGS
 
   # At this point we should add MATLAB_FCFLAGS to FCFLAGS for Windows (which has -fno-underscoring),
   # but that does not work. The actual underscore test seems to happen at the very beginning of the
@@ -47,9 +47,13 @@ AC_DEFUN([AX_SLICOT],
   if test "$1" = matlab; then
     LDFLAGS="$LDFLAGS $MATLAB_LDFLAGS_NOMAP $LDFLAGS_SLICOT"
 
+    # Add MATLAB_CFLAGS to get the -fPIC on Linux/x86_64 (otherwise linking fails)
+    my_save_CFLAGS=$CFLAGS
+    CFLAGS="$CFLAGS $MATLAB_CFLAGS"
     AC_CHECK_LIB([slicot64_pic], [$sb02od], [LIBADD_SLICOT="-lslicot64_pic"], [has_slicot=no], [$MATLAB_LIBS])
+    CFLAGS=$my_save_CFLAGS
   else
-    LDFLAGS="$LDFLAGS $($MKOCTFILE -p LFLAGS) $LDFLAGS_SLICOT"
+    LDFLAGS="$LDFLAGS $LDFLAGS_SLICOT"
     # Fallback on libslicot_pic if dynamic libslicot not found
     AC_CHECK_LIB([slicot], [$sb02od], [LIBADD_SLICOT="-lslicot"],
              [
@@ -58,7 +62,7 @@ AC_DEFUN([AX_SLICOT],
              [$($MKOCTFILE -p BLAS_LIBS) $($MKOCTFILE -p LAPACK_LIBS)])
   fi
 
-  LDFLAGS=$ac_save_LDFLAGS
+  LDFLAGS=$my_save_LDFLAGS
   AC_SUBST(LDFLAGS_SLICOT)
   AC_SUBST(LIBADD_SLICOT)
 ])
diff --git a/mex/build/matlab/configure.ac b/mex/build/matlab/configure.ac
index bab4ac5ccf..97b0eabd49 100644
--- a/mex/build/matlab/configure.ac
+++ b/mex/build/matlab/configure.ac
@@ -88,10 +88,10 @@ if test "$enable_mex_kalman_steady_state" = yes; then
   # FCFLAGS must be temporarily modified, because otherwise -fno-underscoring is not
   # taken into account by AC_FC_FUNC in the AX_SLICOT macro.
   # For some obscure reason, it is necessary to do it at this level and not within the macro.
-  ac_save_FCFLAGS=$FCFLAGS
+  my_save_FCFLAGS=$FCFLAGS
   FCFLAGS="$FCFLAGS $MATLAB_FCFLAGS"
   AX_SLICOT([matlab])
-  FCFLAGS=$ac_save_FCFLAGS
+  FCFLAGS=$my_save_FCFLAGS
   test "$has_slicot" != yes && AC_MSG_ERROR([slicot cannot be found. If you want to skip the compilation of the kalman_steady_state MEX, pass the --disable-mex-kalman-steady-state flag.])
 fi
 
-- 
GitLab