# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1601132241 -7200
#      Sat Sep 26 16:57:21 2020 +0200
# Branch stable
# Node ID e360d3179de08833d45f9fddba2dcfd60fc8eab1
# Parent  7ad31288141915fbd8c9467a91b353354b7ed655
mkoctfile: Replace exec_prefix in compiler flags (bug #59173).

* build-aux/subst-cross-config-vals.in.sh, build-aux/subst-config-vals.in.sh:
  Replace prefix in some compiler flags with replaceable strings.
* src/mkoctfile.in.cc (replace_prefix): New function that fills in the actual
  prefix in strings.
  (initialize): Call new function on those compiler flags.

diff -r 7ad312881419 -r e360d3179de0 build-aux/subst-config-vals.in.sh
--- a/build-aux/subst-config-vals.in.sh	Sat Sep 26 10:48:07 2020 +0200
+++ b/build-aux/subst-config-vals.in.sh	Sat Sep 26 16:57:21 2020 +0200
@@ -77,6 +77,15 @@
 startupfiledir=`echo "@startupfiledir@" | sed "s|^${prefix}/\\+||"`
 texi_macros_file=`echo "@texi_macros_file@" | sed "s|^${prefix}/\\+||"`
 
+## Replace portions of compiler flags that depend on prefix on target
+cppflags=`echo "@CPPFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+ldflags=`echo "@LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+oct_link_opts=`echo "@OCT_LINK_OPTS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+
+fftw3f_ldflags=`echo "@FFTW3F_LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+fftw3_ldflags=`echo "@FFTW3_LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+flibs=`echo "@FLIBS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+
 srcdir="@srcdir@"
 top_srcdir="@top_srcdir@"
 abs_srcdir="@abs_srcdir@"
@@ -108,7 +117,6 @@
 COLAMD_LDFLAGS="@COLAMD_LDFLAGS@"
 COLAMD_LIBS="@COLAMD_LIBS@"
 CPICFLAG="@CPICFLAG@"
-CPPFLAGS="@CPPFLAGS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}\"|" \
-  -e "s|%OCTAVE_CONF_FFTW3F_LDFLAGS%|\"${FFTW3F_LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_FFTW3F_LDFLAGS%|\"${fftw3f_ldflags}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3F_LIBS%|\"${FFTW3F_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3_CPPFLAGS%|\"${FFTW3_CPPFLAGS}\"|" \
-  -e "s|%OCTAVE_CONF_FFTW3_LDFLAGS%|\"${FFTW3_LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_FFTW3_LDFLAGS%|\"${fftw3_ldflags}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3_LIBS%|\"${FFTW3_LIBS}\"|" \
-  -e "s|%OCTAVE_CONF_FLIBS%|\"${FLIBS}\"|" \
+  -e "s|%OCTAVE_CONF_FLIBS%|\"${flibs}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_CPPFLAGS%|\"${FLTK_CPPFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_LDFLAGS%|\"${FLTK_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_LIBS%|\"${FLTK_LIBS}\"|" \
@@ -319,7 +322,7 @@
   -e "s|%OCTAVE_CONF_KLU_LDFLAGS%|\"${KLU_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_KLU_LIBS%|\"${KLU_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_LAPACK_LIBS%|\"${LAPACK_LIBS}\"|" \
-  -e "s|%OCTAVE_CONF_LDFLAGS%|\"${LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_LDFLAGS%|\"${ldflags}\"|" \
   -e "s|%OCTAVE_CONF_LD_STATIC_FLAG%|\"${LD_STATIC_FLAG}\"|" \
   -e "s|%OCTAVE_CONF_LEX%|\"${LEX}\"|" \
   -e "s|%OCTAVE_CONF_LEXLIB%|\"${LEXLIB}\"|" \
@@ -348,7 +351,7 @@
   -e "s|%OCTAVE_CONF_OCTINCLUDEDIR%|\"${octincludedir}\"|" \
   -e "s|%OCTAVE_CONF_OCTLIBDIR%|\"${octlibdir}\"|" \
   -e "s|%OCTAVE_CONF_OCT_LINK_DEPS%|\"${OCT_LINK_DEPS}\"|" \
-  -e "s|%OCTAVE_CONF_OCT_LINK_OPTS%|\"${OCT_LINK_OPTS}\"|" \
+  -e "s|%OCTAVE_CONF_OCT_LINK_OPTS%|\"${oct_link_opts}\"|" \
   -e "s|%OCTAVE_CONF_OPENGL_LIBS%|\"${OPENGL_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_PCRE_CPPFLAGS%|\"${PCRE_CPPFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_PCRE_LDFLAGS%|\"${PCRE_LDFLAGS}\"|" \
diff -r 7ad312881419 -r e360d3179de0 build-aux/subst-cross-config-vals.in.sh
--- a/build-aux/subst-cross-config-vals.in.sh	Sat Sep 26 10:48:07 2020 +0200
+++ b/build-aux/subst-cross-config-vals.in.sh	Sat Sep 26 16:57:21 2020 +0200
@@ -87,6 +87,15 @@
 startupfiledir=`echo "@startupfiledir@" | sed "s|^${prefix}/\\+||"`
 texi_macros_file=`echo "@texi_macros_file@" | sed "s|^${prefix}/\\+||"`
 
+## Replace portions of compiler flags that depend on prefix on target
+cppflags=`echo "@CPPFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+ldflags=`echo "@LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+oct_link_opts=`echo "@OCT_LINK_OPTS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+
+fftw3f_ldflags=`echo "@FFTW3F_LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+fftw3_ldflags=`echo "@FFTW3_LDFLAGS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+flibs=`echo "@FLIBS@" | sed "s|@prefix@|\$\{prefix\}|g"`
+
 srcdir="@srcdir@"
 top_srcdir="@top_srcdir@"
 abs_sr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LAGS%|\"${COLAMD_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_COLAMD_LIBS%|\"${COLAMD_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_CPICFLAG%|\"${CPICFLAG}\"|" \
-  -e "s|%OCTAVE_CONF_CPPFLAGS%|\"${CPPFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_CPPFLAGS%|\"${cppflags}\"|" \
   -e "s|%OCTAVE_CONF_CURL_CPPFLAGS%|\"${CURL_CPPFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_CURL_LDFLAGS%|\"${CURL_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_CURL_LIBS%|\"${CURL_LIBS}\"|" \
@@ -294,12 +297,12 @@
   -e "s|%OCTAVE_CONF_F77_INTEGER_8_FLAG%|\"${F77_INTEGER_8_FLAG}\"|" \
   -e "s|%OCTAVE_CONF_FFLAGS%|\"${FFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3F_CPPFLAGS%|\"${FFTW3F_CPPFLAGS}\"|" \
-  -e "s|%OCTAVE_CONF_FFTW3F_LDFLAGS%|\"${FFTW3F_LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_FFTW3F_LDFLAGS%|\"${fftw3f_ldflags}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3F_LIBS%|\"${FFTW3F_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3_CPPFLAGS%|\"${FFTW3_CPPFLAGS}\"|" \
-  -e "s|%OCTAVE_CONF_FFTW3_LDFLAGS%|\"${FFTW3_LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_FFTW3_LDFLAGS%|\"${fftw3_ldflags}\"|" \
   -e "s|%OCTAVE_CONF_FFTW3_LIBS%|\"${FFTW3_LIBS}\"|" \
-  -e "s|%OCTAVE_CONF_FLIBS%|\"${FLIBS}\"|" \
+  -e "s|%OCTAVE_CONF_FLIBS%|\"${flibs}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_CPPFLAGS%|\"${FLTK_CPPFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_LDFLAGS%|\"${FLTK_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_FLTK_LIBS%|\"${FLTK_LIBS}\"|" \
@@ -323,7 +326,7 @@
   -e "s|%OCTAVE_CONF_KLU_LDFLAGS%|\"${KLU_LDFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_KLU_LIBS%|\"${KLU_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_LAPACK_LIBS%|\"${LAPACK_LIBS}\"|" \
-  -e "s|%OCTAVE_CONF_LDFLAGS%|\"${LDFLAGS}\"|" \
+  -e "s|%OCTAVE_CONF_LDFLAGS%|\"${ldflags}\"|" \
   -e "s|%OCTAVE_CONF_LD_STATIC_FLAG%|\"${LD_STATIC_FLAG}\"|" \
   -e "s|%OCTAVE_CONF_LEX%|\"${LEX}\"|" \
   -e "s|%OCTAVE_CONF_LEXLIB%|\"${LEXLIB}\"|" \
@@ -352,7 +355,7 @@
   -e "s|%OCTAVE_CONF_OCTINCLUDEDIR%|\"${octincludedir}\"|" \
   -e "s|%OCTAVE_CONF_OCTLIBDIR%|\"${octlibdir}\"|" \
   -e "s|%OCTAVE_CONF_OCT_LINK_DEPS%|\"${OCT_LINK_DEPS}\"|" \
-  -e "s|%OCTAVE_CONF_OCT_LINK_OPTS%|\"${OCT_LINK_OPTS}\"|" \
+  -e "s|%OCTAVE_CONF_OCT_LINK_OPTS%|\"${oct_link_opts}\"|" \
   -e "s|%OCTAVE_CONF_OPENGL_LIBS%|\"${OPENGL_LIBS}\"|" \
   -e "s|%OCTAVE_CONF_PCRE_CPPFLAGS%|\"${PCRE_CPPFLAGS}\"|" \
   -e "s|%OCTAVE_CONF_PCRE_LDFLAGS%|\"${PCRE_LDFLAGS}\"|" \
diff -r 7ad312881419 -r e360d3179de0 src/mkoctfile.in.cc
--- a/src/mkoctfile.in.cc	Sat Sep 26 10:48:07 2020 +0200
+++ b/src/mkoctfile.in.cc	Sat Sep 26 16:57:21 2020 +0200
@@ -147,6 +147,20 @@
     return s;
 }
 
+static std::string
+replace_prefix (std::string s)
+{
+  const std::string match = "${prefix}";
+  size_t pos = s.find (match);
+  while (pos != std::string::npos )
+    {
+      s.replace(pos, match.length (), prepend_octave_exec_home (""));
+      pos = s.find (match);
+    }
+
+  return s;
+}
+
 static void
 initialize (void)
 {
@@ -229,7 +243,8 @@
   if (vars["LIBDIR"] != "/usr/lib")
     DEFAULT_LDFLAGS += " -L" + quote_path (vars["LIBDIR"]);
 
-  vars["CPPFLAGS"] = get_variable ("CPPFLAGS", %OCTAVE_CONF_CPPFLAGS%);
+  vars["CPPFLAGS"] = get_variable ("CPPFLAGS",
+                                   replace_prefix (%OCTAVE_CONF_CPPFLAGS%));
 
   vars["INCFLAGS"] = get_variable ("INCFLAGS", DEFAULT_INCFLAGS);
 
@@ -285,19 +300,22 @@
 
   vars["BLAS_LIBS"] = get_variable ("BLAS_LIBS", %OCTAVE_CONF_BLAS_LIBS%);
 
-  vars["FFTW3_LDFLAGS"] = get_variable ("FFTW3_LDFLAGS",
-                                        %OCTAVE_CONF_FFTW3_LDFLAGS%);
+  vars["FFTW3_LDFLAGS"]
+    = get_variable ("FFTW3_LDFLAGS",
+                    replace_prefix (%OCTAVE_CONF_FFTW3_LDFLAGS%));
 
   vars["FFTW3_LIBS"] = get_variable ("FFTW3_LIBS", %OCTAVE_CONF_FFTW3_LIBS%);
 
-  vars["FFTW3F_LDFLAGS"] = get_variable ("FFTW3F_LDFLAGS",
-                                         %OCTAVE_CONF_FFTW3F_LDFLAGS%);
+  vars["FFTW3F_LDFLAGS"]
+    = get_variable ("FFTW3F_LDFLAGS",
+                    replace_prefix (%OCTAVE_CONF_FFTW3F_LDFLAGS%));
 
   vars["FFTW3F_LIBS"] = get_variable ("FFTW3F_LIBS", %OCTAVE_CONF_FFTW3F_LIBS%);
 
   vars["LIBS"] = get_variable ("LIBS", %OCTAVE_CONF_LIBS%);
 
-  vars["FLIBS"] = get_variable ("FLIBS", %OCTAVE_CONF_FLIBS%);
+  vars["FLIBS"] = get_variable ("FLIBS",
+                                replace_prefix (%OCTAVE_CONF_FLIBS%));
 
   vars["OCTAVE_LINK_DEPS"] = get_variable ("OCTAVE_LINK_DEPS",
                                            %OCTAVE_CONF_MKOCTFILE_OCTAVE_LINK_DEPS%);
@@ -308,10 +326,12 @@
   vars["OCT_LINK_DEPS"] = get_variable ("OCT_LINK_DEPS",
                                         %OCTAVE_CONF_MKOCTFILE_OCT_LINK_DEPS%);
 
-  vars["OCT_LINK_OPTS"] = get_variable ("OCT_LINK_OPTS",
-                                        %OCTAVE_CONF_OCT_LINK_OPTS%);
+  vars["OCT_LINK_OPTS"]
+    = get_variable ("OCT_LINK_OPTS",
+                    replace_prefix (%OCTAVE_CONF_OCT_LINK_OPTS%));
 
-  vars["LDFLAGS"] = get_variable ("LDFLAGS", %OCTAVE_CONF_LDFLAGS%);
+  vars["LDFLAGS"] = get_variable ("LDFLAGS",
+                                  replace_prefix (%OCTAVE_CONF_LDFLAGS%));
 
   vars["LD_STATIC_FLAG"] = get_variable ("LD_STATIC_FLAG",
                                          %OCTAVE_CONF_LD_STATIC_FLAG%);
