From c55cd308dc9896e1487844e4c2edf27b68a08dc6 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 26 Sep 2022 10:30:39 -0700
Subject: [PATCH v17 05/23] meson: ecpg: Split definition of static and shared
 libraries

Required for correct resource file generation, as the resource files should
only be added to the shared library.

This also fixes the copy-pasto of naming the target for the export files for
the ecpg libraries libpq.exports.

Only really makes sense with the subsequent commit, but is sufficiently
complicated to be worth keeping separate for easier review.
---
 src/interfaces/libpq/meson.build           |  4 +--
 src/interfaces/ecpg/compatlib/meson.build  | 37 +++++++++++++++++-----
 src/interfaces/ecpg/ecpglib/meson.build    | 32 +++++++++++++++----
 src/interfaces/ecpg/pgtypeslib/meson.build | 28 ++++++++++++----
 src/interfaces/ecpg/test/meson.build       |  2 +-
 5 files changed, 79 insertions(+), 24 deletions(-)

diff --git a/src/interfaces/libpq/meson.build b/src/interfaces/libpq/meson.build
index bc047e00d62..c203372a35b 100644
--- a/src/interfaces/libpq/meson.build
+++ b/src/interfaces/libpq/meson.build
@@ -49,8 +49,8 @@ libpq_st = static_library('libpq',
   kwargs: default_lib_args,
 )
 
-# not using both_libraries here, causes problems with precompiled headers and
-# resource files with msbuild
+# not using both_libraries() here as the resource file should only be in the
+# shared library
 libpq_so = shared_library('libpq',
   dependencies: libpq_deps,
   include_directories: [libpq_inc, postgres_inc],
diff --git a/src/interfaces/ecpg/compatlib/meson.build b/src/interfaces/ecpg/compatlib/meson.build
index 5887cb92b52..7aeb627ee86 100644
--- a/src/interfaces/ecpg/compatlib/meson.build
+++ b/src/interfaces/ecpg/compatlib/meson.build
@@ -1,11 +1,31 @@
-export_file = custom_target('libpq.exports', kwargs: gen_export_kwargs)
-
-ecpg_compat = both_libraries('libecpg_compat',
+ecpg_compat_sources = files(
   'informix.c',
-  include_directories: ['.', ecpg_inc, postgres_inc, libpq_inc],
-  c_args: ['-DSO_MAJOR_VERSION=3'],
+)
+ecpg_compat_so_sources = [] # only for shared lib, in addition to above
+
+ecpg_compat_inc = ['.', ecpg_inc, postgres_inc, libpq_inc]
+ecpg_compat_c_args = ['-DSO_MAJOR_VERSION=3']
+export_file = custom_target('libecpg_compat.exports', kwargs: gen_export_kwargs)
+
+# not using both_libraries(), as the resource file should only be in the
+# shared library
+ecpg_compat_st = static_library('libecpg_compat',
+  ecpg_compat_sources,
+  include_directories: ecpg_compat_inc,
+  c_args: ecpg_compat_c_args,
+  pic: true,
   dependencies: [frontend_code, thread_dep],
-  link_with: [ecpglib, ecpg_pgtypes],
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpg_compat_st
+
+ecpg_compat_so = shared_library('libecpg_compat',
+  ecpg_compat_so_sources,
+  include_directories: ecpg_compat_inc,
+  c_args: ecpg_compat_c_args,
+  dependencies: [frontend_code, thread_dep],
+  link_whole: ecpg_compat_st,
+  link_with: [ecpglib_so, ecpg_pgtypes_so],
   soversion: host_system != 'windows' ? '3' : '',
   darwin_versions: ['3', '3.' + pg_version_major.to_string()],
   version: '3.' + pg_version_major.to_string(),
@@ -13,10 +33,11 @@ ecpg_compat = both_libraries('libecpg_compat',
   link_depends: export_file,
   kwargs: default_lib_args,
 )
-ecpg_targets += [ecpg_compat.get_shared_lib(), ecpg_compat.get_static_lib()]
+ecpg_targets += ecpg_compat_so
 
 pkgconfig.generate(
-  ecpg_compat.get_shared_lib(),
+  ecpg_compat_so,
+  requires_private: 'libpgtypes',
   description: 'PostgreSQL libecpg_compat library',
   url: pg_url,
 )
diff --git a/src/interfaces/ecpg/ecpglib/meson.build b/src/interfaces/ecpg/ecpglib/meson.build
index 2da029ec8ea..eb8dd616571 100644
--- a/src/interfaces/ecpg/ecpglib/meson.build
+++ b/src/interfaces/ecpg/ecpglib/meson.build
@@ -10,15 +10,32 @@ ecpglib_sources = files(
   'sqlda.c',
   'typename.c',
 )
+ecpglib_so_sources = [] # only for shared lib, in addition to above
 
-export_file = custom_target('libpq.exports', kwargs: gen_export_kwargs)
+ecpglib_inc = ['.', ecpg_inc, postgres_inc]
+ecpglib_c_args = ['-DSO_MAJOR_VERSION=6']
+export_file = custom_target('libecpg.exports', kwargs: gen_export_kwargs)
 
-ecpglib = both_libraries('libecpg',
+# not using both_libraries(), as the resource file should only be in the
+# shared library
+ecpglib_st = static_library('libecpg',
   ecpglib_sources,
-  include_directories: ['.', ecpg_inc, postgres_inc],
-  c_args: ['-DSO_MAJOR_VERSION=6'],
+  include_directories: ecpglib_inc,
+  c_args: ecpglib_c_args,
+  pic: true,
   dependencies: [frontend_code, libpq, thread_dep],
-  link_with: [ecpg_pgtypes],
+  link_with: [ecpg_pgtypes_so],
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpglib_st
+
+ecpglib_so = shared_library('libecpg',
+  ecpglib_so_sources,
+  include_directories: ecpglib_inc,
+  c_args: ecpglib_c_args,
+  dependencies: [frontend_code, libpq, thread_dep],
+  link_whole: ecpglib_st,
+  link_with: [ecpg_pgtypes_so],
   soversion: host_system != 'windows' ? '6' : '',
   darwin_versions: ['6', '6.' + pg_version_major.to_string()],
   version: '6.' + pg_version_major.to_string(),
@@ -26,10 +43,11 @@ ecpglib = both_libraries('libecpg',
   link_depends: export_file,
   kwargs: default_lib_args,
 )
-ecpg_targets += [ecpglib.get_shared_lib(), ecpglib.get_static_lib()]
+ecpg_targets += ecpglib_so
 
 pkgconfig.generate(
-  ecpglib.get_shared_lib(),
+  ecpglib_so,
+  requires_private: 'libpgtypes',
   description: 'PostgreSQL libecpg library',
   url: pg_url,
 )
diff --git a/src/interfaces/ecpg/pgtypeslib/meson.build b/src/interfaces/ecpg/pgtypeslib/meson.build
index 96489d9f1d7..5254843f84d 100644
--- a/src/interfaces/ecpg/pgtypeslib/meson.build
+++ b/src/interfaces/ecpg/pgtypeslib/meson.build
@@ -6,13 +6,29 @@ ecpg_pgtypes_sources = files(
   'numeric.c',
   'timestamp.c',
 )
+ecpg_pgtypes_so_sources = []
 
-export_file = custom_target('libpq.exports', kwargs: gen_export_kwargs)
+export_file = custom_target('libpgtypes.exports', kwargs: gen_export_kwargs)
+ecpg_pgtypes_inc = ['.', ecpg_inc, postgres_inc]
+ecpg_pgtypes_c_args = ['-DSO_MAJOR_VERSION=3']
 
-ecpg_pgtypes = both_libraries('libpgtypes',
+# not using both_libraries(), as the resource file should only be in the
+# shared library
+ecpg_pgtypes_st = static_library('libpgtypes',
   ecpg_pgtypes_sources,
-  include_directories: ['.', ecpg_inc, postgres_inc],
-  c_args: ['-DSO_MAJOR_VERSION=3'],
+  include_directories: ecpg_pgtypes_inc,
+  c_args: ecpg_pgtypes_c_args,
+  pic: true,
+  dependencies: [frontend_code],
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpg_pgtypes_st
+
+ecpg_pgtypes_so = shared_library('libpgtypes',
+  ecpg_pgtypes_so_sources,
+  include_directories: ecpg_pgtypes_inc,
+  c_args: ecpg_pgtypes_c_args,
+  link_whole: ecpg_pgtypes_st,
   dependencies: [frontend_code],
   version: '3.' + pg_version_major.to_string(),
   soversion: host_system != 'windows' ? '3' : '',
@@ -21,10 +37,10 @@ ecpg_pgtypes = both_libraries('libpgtypes',
   link_depends: export_file,
   kwargs: default_lib_args,
 )
-ecpg_targets += [ecpg_pgtypes.get_shared_lib(), ecpg_pgtypes.get_static_lib()]
+ecpg_targets += ecpg_pgtypes_so
 
 pkgconfig.generate(
-  ecpg_pgtypes.get_shared_lib(),
+  ecpg_pgtypes_so,
   description: 'PostgreSQL libpgtypes library',
   url: pg_url,
 )
diff --git a/src/interfaces/ecpg/test/meson.build b/src/interfaces/ecpg/test/meson.build
index f0ace641f0c..8904aa7fd90 100644
--- a/src/interfaces/ecpg/test/meson.build
+++ b/src/interfaces/ecpg/test/meson.build
@@ -22,7 +22,7 @@ testprep_targets += pg_regress_ecpg
 ecpg_test_exec_kw = {
   'dependencies': [frontend_code, libpq],
   'include_directories': [ecpg_inc],
-  'link_with': [ecpglib, ecpg_compat, ecpg_pgtypes],
+  'link_with': [ecpglib_so, ecpg_compat_so, ecpg_pgtypes_so],
   'build_by_default': false,
   'install': false,
 }
-- 
2.37.3.542.gdd3f6c4cae

