From e5017149184a50c3131aeaec9c4b0cd2b7279c27 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 29 Sep 2022 21:39:14 -0700
Subject: [PATCH v18 03/22] 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 a bunch of issues in the .pc files.

Previously I tried to avoid building sources twice, once for the static and
once for the shared libraries. We could still do so, but it's not clear that
it's worth the complication.

Discussion: https://postgr.es/m/20220927011951.j3h4o7n6bhf7dwau@awork3.anarazel.de
---
 src/interfaces/ecpg/compatlib/meson.build  | 39 +++++++++++++++++-----
 src/interfaces/ecpg/ecpglib/meson.build    | 33 +++++++++++++-----
 src/interfaces/ecpg/pgtypeslib/meson.build | 29 ++++++++++++----
 src/interfaces/ecpg/test/meson.build       |  2 +-
 4 files changed, 78 insertions(+), 25 deletions(-)

diff --git a/src/interfaces/ecpg/compatlib/meson.build b/src/interfaces/ecpg/compatlib/meson.build
index 5887cb92b52..b803000c708 100644
--- a/src/interfaces/ecpg/compatlib/meson.build
+++ b/src/interfaces/ecpg/compatlib/meson.build
@@ -1,11 +1,29 @@
-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'],
-  dependencies: [frontend_code, thread_dep],
-  link_with: [ecpglib, ecpg_pgtypes],
+)
+ecpg_compat_so_sources = [] # for shared lib, in addition to the above
+
+ecpg_compat_inc = [include_directories('.'), ecpg_inc, libpq_inc]
+ecpg_compat_c_args = ['-DSO_MAJOR_VERSION=3']
+export_file = custom_target('libecpg_compat.exports', kwargs: gen_export_kwargs)
+
+# see src/interfaces/libpq/meson.build
+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],
+  link_with: [ecpglib_st, ecpg_pgtypes_st],
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpg_compat_st
+
+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],
+  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 +31,13 @@ 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(),
+  name: 'libecpg_compat',
   description: 'PostgreSQL libecpg_compat library',
   url: pg_url,
+  libraries: ecpg_compat_so,
+  libraries_private: [frontend_stlib_code, thread_dep],
+  requires_private: ['libecpg', 'libpgtypes'],
 )
diff --git a/src/interfaces/ecpg/ecpglib/meson.build b/src/interfaces/ecpg/ecpglib/meson.build
index 2da029ec8ea..6fdf019149c 100644
--- a/src/interfaces/ecpg/ecpglib/meson.build
+++ b/src/interfaces/ecpg/ecpglib/meson.build
@@ -10,15 +10,29 @@ ecpglib_sources = files(
   'sqlda.c',
   'typename.c',
 )
+ecpglib_so_sources = [] # for shared lib, in addition to the above
 
-export_file = custom_target('libpq.exports', kwargs: gen_export_kwargs)
+ecpglib_inc = [include_directories('.'), ecpg_inc]
+ecpglib_c_args = ['-DSO_MAJOR_VERSION=6']
+export_file = custom_target('libecpg.exports', kwargs: gen_export_kwargs)
 
-ecpglib = both_libraries('libecpg',
+# see src/interfaces/libpq/meson.build
+ecpglib_st = static_library('libecpg',
   ecpglib_sources,
-  include_directories: ['.', ecpg_inc, postgres_inc],
-  c_args: ['-DSO_MAJOR_VERSION=6'],
-  dependencies: [frontend_code, libpq, thread_dep],
-  link_with: [ecpg_pgtypes],
+  include_directories: ecpglib_inc,
+  c_args: ecpglib_c_args,
+  dependencies: [frontend_stlib_code, thread_dep, libpq],
+  link_with: [ecpg_pgtypes_st],
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpglib_st
+
+ecpglib_so = shared_library('libecpg',
+  ecpglib_sources + ecpglib_so_sources,
+  include_directories: ecpglib_inc,
+  c_args: ecpglib_c_args,
+  dependencies: [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()],
   version: '6.' + pg_version_major.to_string(),
@@ -26,12 +40,15 @@ 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(),
+  name: 'libecpg',
   description: 'PostgreSQL libecpg library',
   url: pg_url,
+  libraries: ecpglib_so,
+  libraries_private: [frontend_shlib_code, thread_dep],
+  requires_private: ['libpgtypes', 'libpq'],
 )
 
 subdir('po', if_found: libintl)
diff --git a/src/interfaces/ecpg/pgtypeslib/meson.build b/src/interfaces/ecpg/pgtypeslib/meson.build
index 96489d9f1d7..8e5d235810a 100644
--- a/src/interfaces/ecpg/pgtypeslib/meson.build
+++ b/src/interfaces/ecpg/pgtypeslib/meson.build
@@ -6,14 +6,27 @@ ecpg_pgtypes_sources = files(
   'numeric.c',
   'timestamp.c',
 )
+ecpg_pgtypes_so_sources = [] # for shared lib, in addition to the above
 
-export_file = custom_target('libpq.exports', kwargs: gen_export_kwargs)
+export_file = custom_target('libpgtypes.exports', kwargs: gen_export_kwargs)
+ecpg_pgtypes_inc = [include_directories('.'), ecpg_inc]
+ecpg_pgtypes_c_args = ['-DSO_MAJOR_VERSION=3']
 
-ecpg_pgtypes = both_libraries('libpgtypes',
+# see src/interfaces/libpq/meson.build
+ecpg_pgtypes_st = static_library('libpgtypes',
   ecpg_pgtypes_sources,
-  include_directories: ['.', ecpg_inc, postgres_inc],
-  c_args: ['-DSO_MAJOR_VERSION=3'],
-  dependencies: [frontend_code],
+  include_directories: ecpg_pgtypes_inc,
+  c_args: ecpg_pgtypes_c_args,
+  dependencies: frontend_stlib_code,
+  kwargs: default_lib_args,
+)
+ecpg_targets += ecpg_pgtypes_st
+
+ecpg_pgtypes_so = shared_library('libpgtypes',
+  ecpg_pgtypes_sources + ecpg_pgtypes_so_sources,
+  include_directories: ecpg_pgtypes_inc,
+  c_args: ecpg_pgtypes_c_args,
+  dependencies: frontend_shlib_code,
   version: '3.' + pg_version_major.to_string(),
   soversion: host_system != 'windows' ? '3' : '',
   darwin_versions: ['3', '3.' + pg_version_major.to_string()],
@@ -21,10 +34,12 @@ 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(),
+  name: 'libpgtypes',
   description: 'PostgreSQL libpgtypes library',
   url: pg_url,
+  libraries: ecpg_pgtypes_so,
+  libraries_private: [frontend_stlib_code],
 )
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

