From d9b26d69ee6cf695ca67bb8d23f31d1eb8d39796 Mon Sep 17 00:00:00 2001
From: Rob Browning <rlb@defaultvalue.org>
Date: Tue, 20 Feb 2018 23:58:09 -0600
Subject: Fix 32-bit big endian builds via -Oresolve-primitives -Ocps

Specify -Oresolve-primitives -Ocps during bootstrapping builds to fix
build failures on 32-bit big endian architectures.

Thanks to John David Anglin for reporting the problem and investigating
the problem, and to John Paul Adrian Glaubitz for helping devise the
solution.

Bug: https://debbugs.gnu.org/45214
Bug-Debian: https://bugs.debian.org/977223
---
 bootstrap/Makefile.am |  9 ++++++++-
 configure.ac          |  3 +++
 stage0/Makefile.am    | 10 +++++++++-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/bootstrap/Makefile.am b/bootstrap/Makefile.am
index a4634c447..0aa548c26 100644
--- a/bootstrap/Makefile.am
+++ b/bootstrap/Makefile.am
@@ -22,7 +22,14 @@
 
 
 GUILE_WARNINGS = -W0
-GUILE_OPTIMIZATIONS = -O1
+
+if !DEB_GUILE_32_BIT_BIG_ENDIAN
+  $(info Note: not adjusting GUILE_OPTIMIZATIONS for 32-bit big-endian architecture)
+  GUILE_OPTIMIZATIONS = -O1
+else
+  $(info Note: adjusting GUILE_OPTIMIZATIONS for 32-bit big-endian architecture)
+  GUILE_OPTIMIZATIONS = -O1 -Oresolve-primitives -Ocps
+endif
 
 include $(top_srcdir)/am/bootstrap.am
 
diff --git a/configure.ac b/configure.ac
index 0dcb71cce..dc915ebaf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -374,6 +374,9 @@ esac
 AC_MSG_RESULT($SCM_PREBUILT_BINARIES)
 AC_SUBST([SCM_PREBUILT_BINARIES])
 
+AM_CONDITIONAL([DEB_GUILE_32_BIT_BIG_ENDIAN],
+  [test "$SCM_PREBUILT_BINARIES" = 32-bit-big-endian])
+
 AC_HEADER_SYS_WAIT
 AC_HEADER_DIRENT
 
diff --git a/stage0/Makefile.am b/stage0/Makefile.am
index 12029fb45..4228f607d 100644
--- a/stage0/Makefile.am
+++ b/stage0/Makefile.am
@@ -22,7 +22,15 @@
 
 
 GUILE_WARNINGS = -W0
-GUILE_OPTIMIZATIONS = -O1
+
+if !DEB_GUILE_32_BIT_BIG_ENDIAN
+  $(info Note: not adjusting GUILE_OPTIMIZATIONS for 32-bit big-endian architecture)
+  GUILE_OPTIMIZATIONS = -O1
+else
+  $(info Note: adjusting GUILE_OPTIMIZATIONS for 32-bit big-endian architecture)
+  GUILE_OPTIMIZATIONS = -O1 -Oresolve-primitives -Ocps
+endif
+
 GUILE_BOOTSTRAP_STAGE = stage0
 
 include $(top_srcdir)/am/bootstrap.am