From 6a9ede885ca32bf78ab49c84ce4cd273aea79889 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= <clement.chigot@atos.net>
Date: Thu, 9 Sep 2021 14:59:38 +0200
Subject: [PATCH] aix: Add FAT library support for libffi
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Even if GCC64 is able to boostrap without libffi being a
FAT library on AIX, the tests for "-maix32" are not working
without it.

libffi/ChangeLog:
2021-09-10  Clément Chigot  <clement.chigot@atos.net>

	* Makefile.am (tmake_file): Build and install AIX-style FAT
	  libraries.
	* Makefile.in: Regenerate.
	* include/Makefile.in: Regenerate.
	* man/Makefile.in: Regenerate.
	* testsuite/Makefile.in: Regenerate.
	* configure (tmake_file): Substitute.
	* configure.ac: Regenerate.
 	* configure.host (powerpc-*-aix*): Define tmake_file.
	* src/powerpc/t-aix: New file.
---
 libffi/Makefile.am           |  3 +++
 libffi/Makefile.in           |  4 ++++
 libffi/configure             | 17 +++++++++++++++--
 libffi/configure.ac          | 12 ++++++++++++
 libffi/configure.host        |  2 ++
 libffi/include/Makefile.in   |  1 +
 libffi/man/Makefile.in       |  1 +
 libffi/src/powerpc/t-aix     | 12 ++++++++++++
 libffi/testsuite/Makefile.in |  1 +
 9 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 libffi/src/powerpc/t-aix

diff --git a/libffi/Makefile.am b/libffi/Makefile.am
index 203b7d10a42..19758b8b232 100644
--- a/libffi/Makefile.am
+++ b/libffi/Makefile.am
@@ -247,4 +247,7 @@ clean-recursive: clean-multi
 distclean-recursive: distclean-multi
 maintainer-clean-recursive: maintainer-clean-multi
 
+# target overrides
+-include $(tmake_file)
+
 include $(top_srcdir)/../multilib.am
diff --git a/libffi/Makefile.in b/libffi/Makefile.in
index 745bdd80777..43336d05ca6 100644
--- a/libffi/Makefile.in
+++ b/libffi/Makefile.in
@@ -436,6 +436,7 @@ target_alias = @target_alias@
 target_cpu = @target_cpu@
 target_os = @target_os@
 target_vendor = @target_vendor@
+tmake_file = @tmake_file@
 toolexecdir = @toolexecdir@
 toolexeclibdir = @toolexeclibdir@
 top_build_prefix = @top_build_prefix@
@@ -1912,6 +1913,9 @@ clean-recursive: clean-multi
 distclean-recursive: distclean-multi
 maintainer-clean-recursive: maintainer-clean-multi
 
+# target overrides
+-include $(tmake_file)
+
 # GNU Make needs to see an explicit $(MAKE) variable in the command it
 # runs to enable its job server during parallel builds.  Hence the
 # comments below.
--- ./libffi/configure_orig     2025-02-15 22:08:38.264496748 -0600
+++ ./libffi/configure  2025-02-15 22:13:39.726532346 -0600
@@ -644,6 +644,7 @@
 LIBFFI_BUILD_VERSIONED_SHLIB_TRUE
 OPT_LDFLAGS
 SECTION_LDFLAGS
+tmake_file
 toolexeclibdir
 toolexecdir
 FFI_DEBUG_FALSE
@@ -11596,7 +11597,7 @@
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11599 "configure"
+#line 11600 "configure"
 #include "confdefs.h"

 #if HAVE_DLFCN_H
@@ -11702,7 +11703,7 @@
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11705 "configure"
+#line 11706 "configure"
 #include "confdefs.h"

 #if HAVE_DLFCN_H
@@ -16518,6 +16519,17 @@
 esac


+# Conditionalize the makefile for this target machine.
+tmake_file_=
+for f in ${tmake_file}
+do
+       if test -f ${srcdir}/src/$TARGETDIR/$f
+       then
+               tmake_file_="${tmake_file_} \$(srcdir)/src/$TARGETDIR/$f"
+       fi
+done
+tmake_file="${tmake_file_}"
+

 if test "${multilib}" = "yes"; then
   multilib_arg="--enable-multilib"
@@ -18922,10 +18934,10 @@
 macro_revision=$macro_revision

 # Whether or not to build shared libraries.
-build_libtool_libs=$enable_shared
+build_libtool_libs=yes

 # Whether or not to build static libraries.
-build_old_libs=$enable_static
+build_old_libs=no

 # What type of objects to build.
 pic_mode=$pic_mode
diff --git a/libffi/configure.ac b/libffi/configure.ac
index 0bf4af410d3..b4214459c6c 100644
--- a/libffi/configure.ac
+++ b/libffi/configure.ac
@@ -362,6 +362,18 @@ esac
 AC_SUBST(toolexecdir)
 AC_SUBST(toolexeclibdir)
 
+# Conditionalize the makefile for this target machine.
+tmake_file_=
+for f in ${tmake_file}
+do
+	if test -f ${srcdir}/src/$TARGETDIR/$f
+	then
+		tmake_file_="${tmake_file_} \$(srcdir)/src/$TARGETDIR/$f"
+	fi
+done
+tmake_file="${tmake_file_}"
+AC_SUBST(tmake_file)
+
 if test "${multilib}" = "yes"; then
   multilib_arg="--enable-multilib"
 else
diff --git a/libffi/configure.host b/libffi/configure.host
index 7248acb7458..581212bf213 100644
--- a/libffi/configure.host
+++ b/libffi/configure.host
@@ -207,6 +207,8 @@
 	;;
   powerpc-*-aix* | rs6000-*-aix*)
 	TARGET=POWERPC_AIX; TARGETDIR=powerpc
+	# Create AIX-style "FAT" libraries.
+	tmake_file="t-aix"
 	;;
   powerpc-*-freebsd* | powerpc-*-openbsd* | powerpc-*-netbsd*)
 	TARGET=POWERPC_FREEBSD; TARGETDIR=powerpc
diff --git a/libffi/include/Makefile.in b/libffi/include/Makefile.in
index 99da8410d8b..57690655e8d 100644
--- a/libffi/include/Makefile.in
+++ b/libffi/include/Makefile.in
@@ -310,6 +310,7 @@ target_alias = @target_alias@
 target_cpu = @target_cpu@
 target_os = @target_os@
 target_vendor = @target_vendor@
+tmake_file = @tmake_file@
 toolexecdir = @toolexecdir@
 toolexeclibdir = @toolexeclibdir@
 top_build_prefix = @top_build_prefix@
diff --git a/libffi/man/Makefile.in b/libffi/man/Makefile.in
index 754691095f7..a5f55de0f88 100644
--- a/libffi/man/Makefile.in
+++ b/libffi/man/Makefile.in
@@ -292,6 +292,7 @@ target_alias = @target_alias@
 target_cpu = @target_cpu@
 target_os = @target_os@
 target_vendor = @target_vendor@
+tmake_file = @tmake_file@
 toolexecdir = @toolexecdir@
 toolexeclibdir = @toolexeclibdir@
 top_build_prefix = @top_build_prefix@
diff --git a/libffi/src/powerpc/t-aix b/libffi/src/powerpc/t-aix
new file mode 100644
index 00000000000..d3536e9dc8a
--- /dev/null
+++ b/libffi/src/powerpc/t-aix
@@ -0,0 +1,12 @@
+ifeq ($(MULTIBUILDTOP),)
+BITS=$(shell if test -z "`$(CC) -x c -E /dev/null -g3 -o - | grep 64BIT`" ; then \
+    echo '64'; else echo '32'; fi)
+ARX=$(shell echo $(AR) | sed -e 's/-X[^ ]*//g')
+MAJOR=$(firstword $(subst :, ,$(shell grep -v '^\#' $(srcdir)/libtool-version)))
+all-local: $(toolexeclib_LTLIBRARIES)
+	-rm -f .libs/$(PACKAGE).a ../pthread/$(PACKAGE)/.libs/$(PACKAGE).a
+	$(AR) rc .libs/$(PACKAGE).a .libs/$(PACKAGE).so.$(MAJOR)
+	$(AR) rc ../pthread/$(PACKAGE)/.libs/$(PACKAGE).a ../pthread/$(PACKAGE)/.libs/$(PACKAGE).so.$(MAJOR)
+	$(ARX) -X$(BITS) rc .libs/$(PACKAGE).a ../ppc$(BITS)/$(PACKAGE)/.libs/$(PACKAGE).so.$(MAJOR)
+	$(ARX) -X$(BITS) rc ../pthread/$(PACKAGE)/.libs/$(PACKAGE).a ../pthread/ppc$(BITS)/$(PACKAGE)/.libs/$(PACKAGE).so.$(MAJOR)
+endif
\ No newline at end of file
diff --git a/libffi/testsuite/Makefile.in b/libffi/testsuite/Makefile.in
index 8b574dad5d7..5d83c3caf05 100644
--- a/libffi/testsuite/Makefile.in
+++ b/libffi/testsuite/Makefile.in
@@ -263,6 +263,7 @@ target_alias = @target_alias@
 target_cpu = @target_cpu@
 target_os = @target_os@
 target_vendor = @target_vendor@
+tmake_file = @tmake_file@
 toolexecdir = @toolexecdir@
 toolexeclibdir = @toolexeclibdir@
 top_build_prefix = @top_build_prefix@
-- 
2.31.1

