From d00dbc2188893a0f0f6b533aaee5733a22568527 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 20 Aug 2022 08:43:28 -0700
Subject: [PATCH v5 1/2] aix: No need to use mkldexport when we want to export
 all symbols

When building a shared library with an exports.txt there's no need to build an
intermediary static library, we can just pass -Wl,-bE:... when generating the
.so.

When building a shared library without an exports.txt, there's no need to call
mkldexport.sh to export all symbols, because all symbols are exported anyway,
and we don't need the export file on the import side (like we do for
postgres.imp).

This makes building .so's on aix a lot more similar to building on other
platforms. In particular, we don't create and remove a .a of the same name but
different contents anymore.
---
 src/makefiles/Makefile.aix |  8 ++-----
 src/Makefile.shlib         | 45 +++++++++++++++-----------------------
 2 files changed, 20 insertions(+), 33 deletions(-)

diff --git a/src/makefiles/Makefile.aix b/src/makefiles/Makefile.aix
index 9408c1e2913..56d7f22aff6 100644
--- a/src/makefiles/Makefile.aix
+++ b/src/makefiles/Makefile.aix
@@ -38,9 +38,5 @@ endif
 MKLDEXPORT_DIR=src/backend/port/aix
 MKLDEXPORT=$(top_srcdir)/$(MKLDEXPORT_DIR)/mkldexport.sh
 
-%.exp: %.o
-	$(MKLDEXPORT) $^ >$@
-
-# Rule for building a shared library from a single .o file
-%$(DLSUFFIX): %.o %.exp
-	$(CC) $(CFLAGS) $*.o $(LDFLAGS) $(LDFLAGS_SL) -o $@ -Wl,-bE:$*.exp $(BE_DLLLIBS)
+%$(DLSUFFIX): %.o
+	$(CC) $(CFLAGS) $*.o $(LDFLAGS) $(LDFLAGS_SL) -o $@ $(BE_DLLLIBS)
diff --git a/src/Makefile.shlib b/src/Makefile.shlib
index 1e09ab8ea62..db466b3b845 100644
--- a/src/Makefile.shlib
+++ b/src/Makefile.shlib
@@ -107,12 +107,17 @@ override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
 endif
 
 ifeq ($(PORTNAME), aix)
+  LINK.shared		= $(COMPILER)
   ifdef SO_MAJOR_VERSION
     shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
   endif
   haslibarule   = yes
   # $(exports_file) is also usable as an import file
   exports_file		= lib$(NAME).exp
+  BUILD.exports		= ( echo '\#! $(shlib)'; $(AWK) '/^[^\#]/ {printf "%s\n",$$1}' $< ) > $@
+  ifneq (,$(SHLIB_EXPORTS))
+    LINK.shared		+= -Wl,-bE:$(exports_file)
+  endif
 endif
 
 ifeq ($(PORTNAME), darwin)
@@ -259,9 +264,15 @@ $(stlib): $(OBJS) | $(SHLIB_PREREQS)
 	touch $@
 endif #haslibarule
 
+# AIX wraps shared libraries inside a static library, can be used both
+# for static and shared linking
+ifeq ($(PORTNAME), aix)
+$(stlib): $(shlib)
+	rm -f $(stlib)
+	$(AR) $(AROPT) $(stlib) $(shlib)
+endif # aix
 
 ifeq (,$(filter cygwin win32,$(PORTNAME)))
-ifneq ($(PORTNAME), aix)
 
 # Normal case
 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
@@ -274,9 +285,12 @@ ifneq ($(shlib), $(shlib_major))
 endif
 # Make sure we have a link to a name without any version numbers
 ifneq ($(shlib), $(shlib_bare))
+# except on AIX, where that's not a thing
+ifneq ($(PORTNAME), aix)
 	rm -f $(shlib_bare)
 	$(LN_S) $(shlib) $(shlib_bare)
-endif
+endif # aix
+endif # shlib_bare
 endif # shlib_major
 
 # Where possible, restrict the symbols exported by the library to just the
@@ -285,36 +299,13 @@ endif # shlib_major
 # libpgport along with libpq.
 ifneq (,$(SHLIB_EXPORTS))
 ifdef BUILD.exports
-$(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
+$(shlib): $(exports_file)
 
-$(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
+$(exports_file): $(SHLIB_EXPORTS)
 	$(BUILD.exports)
 endif
 endif
 
-else # PORTNAME == aix
-
-# AIX case
-
-# See notes in src/backend/parser/Makefile about the following two rules
-$(stlib): $(shlib)
-	touch $@
-
-$(shlib): $(OBJS) | $(SHLIB_PREREQS)
-	rm -f $(stlib)
-	$(LINK.static) $(stlib) $^
-	$(RANLIB) $(stlib)
-ifeq (,$(SHLIB_EXPORTS))
-	$(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
-else
-	( echo '#! $(shlib)'; $(AWK) '/^[^#]/ {printf "%s\n",$$1}' ${srcdir}/$(SHLIB_EXPORTS) ) >$(exports_file)
-endif
-	$(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
-	rm -f $(stlib)
-	$(AR) $(AROPT) $(stlib) $(shlib)
-
-endif # PORTNAME == aix
-
 else # PORTNAME == cygwin || PORTNAME == win32
 
 ifeq ($(PORTNAME), cygwin)
-- 
2.37.0.3.g30cc8d0f14

