From 3af0cc8969eb030fa73a2470dba13da5a54d5a92 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 15 Feb 2026 16:00:03 -0500
Subject: [PATCH v6 4/7] Teach meson build infrastructure not to build static
 libs on AIX.

On AIX, shared and static libraries are really the same thing.
We must avoid having two rules that both build 'libpq.a',
for instance.  Add dependencies to make that work.

Note: in principle this could be extended to allow disabling
shared library build, but no platform needs that so not all
of the code to support it has been filled in.

Note: the original submission suppressed installation of the
libpgport, libpgcommon, and libpgfeutils static libraries,
which is wrong.

TODO: who to credit for this work?
---
 meson.build                                | 28 ++++++++++++++++++----
 src/interfaces/ecpg/compatlib/meson.build  |  8 ++++---
 src/interfaces/ecpg/ecpglib/meson.build    |  8 ++++---
 src/interfaces/ecpg/pgtypeslib/meson.build |  8 ++++---
 src/interfaces/libpq-oauth/meson.build     |  1 +
 src/interfaces/libpq/meson.build           |  4 ++--
 6 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/meson.build b/meson.build
index 7e382aebab6..32d82b244e1 100644
--- a/meson.build
+++ b/meson.build
@@ -185,6 +185,14 @@ memset_loop_limit = 1024
 shmem_kind = 'sysv'
 sema_kind = 'sysv'
 
+# Most platforms want to build both shared and static libraries,
+# but on some we only want one kind.  We use these variables to
+# control building/installation of the two kinds.
+# Note: in principle you can disable building of shared libraries,
+# but that case is not used and therefore not built out completely...
+dep_shared_lib = declare_dependency()
+dep_static_lib = declare_dependency()
+
 # We implement support for some operating systems by pretending they're
 # another. Map here, before determining system properties below
 if host_system == 'dragonfly'
@@ -216,6 +224,13 @@ if host_system == 'aix'
   # -brtllib indicates binaries should use runtime-loaded shared libraries.
   ldflags_be += '-Wl,-brtllib'
 
+  # On AIX, shared libraries are wrapped in static libraries and can have the
+  # same extension '.a'.  Therefore we must refrain from trying to build
+  # static libraries, or meson will complain about duplicate targets.
+  # Note however that we leave dlsuffix with its default value of '.so';
+  # this results in using '.so' for loadable modules, which is fine.
+  dep_static_lib = disabler()
+
 elif host_system == 'cygwin'
   sema_kind = 'unnamed_posix'
   cppflags += '-D_GNU_SOURCE'
@@ -3541,18 +3556,21 @@ endif
 installed_targets = [
   backend_targets,
   bin_targets,
-  libpq_st,
   pl_targets,
   contrib_targets,
   nls_mo_targets,
   ecpg_targets,
 ]
 
+if dep_static_lib.found()
+  installed_targets += libpq_st
+endif
+
 if oauth_flow_supported
-  installed_targets += [
-    libpq_oauth_so,
-    libpq_oauth_st,
-  ]
+  installed_targets += libpq_oauth_so
+  if dep_static_lib.found()
+    installed_targets += libpq_oauth_st
+  endif
 endif
 
 # all targets that require building code
diff --git a/src/interfaces/ecpg/compatlib/meson.build b/src/interfaces/ecpg/compatlib/meson.build
index 6cb1be73407..94dcc67c03c 100644
--- a/src/interfaces/ecpg/compatlib/meson.build
+++ b/src/interfaces/ecpg/compatlib/meson.build
@@ -20,17 +20,19 @@ ecpg_compat_st = static_library('libecpg_compat',
   ecpg_compat_sources,
   include_directories: ecpg_compat_inc,
   c_args: ecpg_compat_c_args,
-  dependencies: [frontend_stlib_code, thread_dep],
+  dependencies: [dep_static_lib, frontend_stlib_code, thread_dep],
   link_with: [ecpglib_st, ecpg_pgtypes_st],
   kwargs: default_lib_args,
 )
-ecpg_targets += ecpg_compat_st
+if dep_static_lib.found()
+  ecpg_targets += ecpg_compat_st
+endif
 
 ecpg_compat_so = shared_library('libecpg_compat',
   ecpg_compat_sources + ecpg_compat_so_sources,
   include_directories: ecpg_compat_inc,
   c_args: ecpg_compat_c_args,
-  dependencies: [frontend_shlib_code, thread_dep],
+  dependencies: [dep_shared_lib, frontend_shlib_code, thread_dep],
   link_with: [ecpglib_so, ecpg_pgtypes_so],
   soversion: host_system != 'windows' ? '3' : '',
   darwin_versions: ['3', '3.' + pg_version_major.to_string()],
diff --git a/src/interfaces/ecpg/ecpglib/meson.build b/src/interfaces/ecpg/ecpglib/meson.build
index 889bd9efd65..a81d772aa69 100644
--- a/src/interfaces/ecpg/ecpglib/meson.build
+++ b/src/interfaces/ecpg/ecpglib/meson.build
@@ -30,18 +30,20 @@ ecpglib_st = static_library('libecpg',
   include_directories: ecpglib_inc,
   c_args: ecpglib_c_args,
   c_pch: pch_postgres_fe_h,
-  dependencies: [frontend_stlib_code, thread_dep, libpq],
+  dependencies: [dep_static_lib, frontend_stlib_code, thread_dep, libpq],
   link_with: [ecpg_pgtypes_st],
   kwargs: default_lib_args,
 )
-ecpg_targets += ecpglib_st
+if dep_static_lib.found()
+  ecpg_targets += ecpglib_st
+endif
 
 ecpglib_so = shared_library('libecpg',
   ecpglib_sources + ecpglib_so_sources,
   include_directories: ecpglib_inc,
   c_args: ecpglib_c_args,
   c_pch: pch_postgres_fe_h,
-  dependencies: [frontend_shlib_code, libpq, thread_dep],
+  dependencies: [dep_shared_lib, frontend_shlib_code, libpq, thread_dep],
   link_with: ecpg_pgtypes_so,
   soversion: host_system != 'windows' ? '6' : '',
   darwin_versions: ['6', '6.' + pg_version_major.to_string()],
diff --git a/src/interfaces/ecpg/pgtypeslib/meson.build b/src/interfaces/ecpg/pgtypeslib/meson.build
index 6b78f529e53..897a8febc76 100644
--- a/src/interfaces/ecpg/pgtypeslib/meson.build
+++ b/src/interfaces/ecpg/pgtypeslib/meson.build
@@ -26,17 +26,19 @@ ecpg_pgtypes_st = static_library('libpgtypes',
   include_directories: ecpg_pgtypes_inc,
   c_args: ecpg_pgtypes_c_args,
   c_pch: pch_postgres_fe_h,
-  dependencies: frontend_stlib_code,
+  dependencies: [dep_static_lib, frontend_stlib_code],
   kwargs: default_lib_args,
 )
-ecpg_targets += ecpg_pgtypes_st
+if dep_static_lib.found()
+  ecpg_targets += ecpg_pgtypes_st
+endif
 
 ecpg_pgtypes_so = shared_library('libpgtypes',
   ecpg_pgtypes_sources + ecpg_pgtypes_so_sources,
   include_directories: ecpg_pgtypes_inc,
   c_args: ecpg_pgtypes_c_args,
   c_pch: pch_postgres_fe_h,
-  dependencies: frontend_shlib_code,
+  dependencies: [dep_shared_lib, frontend_shlib_code],
   version: '3.' + pg_version_major.to_string(),
   soversion: host_system != 'windows' ? '3' : '',
   darwin_versions: ['3', '3.' + pg_version_major.to_string()],
diff --git a/src/interfaces/libpq-oauth/meson.build b/src/interfaces/libpq-oauth/meson.build
index d8a0c04095a..b0083f279aa 100644
--- a/src/interfaces/libpq-oauth/meson.build
+++ b/src/interfaces/libpq-oauth/meson.build
@@ -26,6 +26,7 @@ libpq_oauth_st = static_library('libpq-oauth',
   include_directories: [libpq_oauth_inc, postgres_inc],
   c_pch: pch_postgres_fe_h,
   dependencies: [
+    dep_static_lib,
     frontend_stlib_code,
     libpq_oauth_deps,
     ssl, # libpq-int.h includes OpenSSL headers
diff --git a/src/interfaces/libpq/meson.build b/src/interfaces/libpq/meson.build
index 3333f6d00c8..ea98596b936 100644
--- a/src/interfaces/libpq/meson.build
+++ b/src/interfaces/libpq/meson.build
@@ -63,7 +63,7 @@ libpq_st = static_library('libpq',
   include_directories: [libpq_inc],
   c_args: libpq_c_args,
   c_pch: pch_postgres_fe_h,
-  dependencies: [frontend_stlib_code, libpq_deps],
+  dependencies: [dep_static_lib, frontend_stlib_code, libpq_deps],
   kwargs: default_lib_args,
 )
 
@@ -75,7 +75,7 @@ libpq_so = shared_library('libpq',
   version: '5.' + pg_version_major.to_string(),
   soversion: host_system != 'windows' ? '5' : '',
   darwin_versions: ['5', '5.' + pg_version_major.to_string()],
-  dependencies: [frontend_shlib_code, libpq_deps],
+  dependencies: [dep_shared_lib, frontend_shlib_code, libpq_deps],
   link_depends: export_file,
   link_args: export_fmt.format(export_file.full_path()),
   kwargs: default_lib_args,
-- 
2.43.7

