@@ -161,6 +161,7 @@ cdata.set_quoted('CONFIGURE_ARGS', '')
161161
162162exesuffix = '' # overridden below where necessary
163163dlsuffix = ' .so' # overridden below where necessary
164+ rpath_origin = ' $ORIGIN'
164165library_path_var = ' LD_LIBRARY_PATH'
165166
166167# Format of file to control exports from libraries, and how to pass them to
@@ -207,6 +208,7 @@ if host_system == 'cygwin'
207208elif host_system == ' darwin'
208209 dlsuffix = ' .dylib'
209210 library_path_var = ' DYLD_LIBRARY_PATH'
211+ rpath_origin = ' @loader_path'
210212
211213 export_file_format = ' darwin'
212214 export_fmt = ' -Wl,-exported_symbols_list,@0@'
@@ -255,8 +257,16 @@ elif host_system == 'netbsd'
255257 # LDFLAGS.
256258 ldflags += [' -Wl,-z,now' , ' -Wl,-z,relro' ]
257259
260+ # netbsd patched their meson in a broken way:
261+ # https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=56959
262+ # until there's a way out of that, disable rpath_origin
263+ rpath_origin = ''
264+
258265elif host_system == ' openbsd'
259- # you're ok
266+ # openbsd's $ORIGIN doesn't use an absolute path to the binary, but argv[0]
267+ # (i.e. absolute when invoked with an absolute name, but e.g. not absolute
268+ # when invoked via PATH search).
269+ rpath_origin = ''
260270
261271elif host_system == ' sunos'
262272 portname = ' solaris'
@@ -268,6 +278,7 @@ elif host_system == 'windows'
268278 exesuffix = ' .exe'
269279 dlsuffix = ' .dll'
270280 library_path_var = ''
281+ rpath_origin = ''
271282 if cc.get_id() != ' msvc'
272283 # define before including <time.h> for getting localtime_r() etc. on MinGW
273284 cppflags += ' -D_POSIX_C_SOURCE'
@@ -2813,25 +2824,41 @@ bin_install_rpaths = []
28132824lib_install_rpaths = []
28142825mod_install_rpaths = []
28152826
2816-
2817- # Don't add rpaths on darwin for now - as long as only absolute references to
2818- # libraries are needed, absolute LC_ID_DYLIB ensures libraries can be found in
2819- # their final destination.
2827+ # Add extra_lib_dirs to rpath. This ensures we find libraries we depend on.
2828+ #
2829+ # Not needed on darwin, even if we use relative rpaths for our own libraries,
2830+ # as the install_name of libraries in extra_lib_dirs will point to their
2831+ # location anyway.
28202832if host_system != ' darwin'
2833+ bin_install_rpaths += postgres_lib_d
2834+ lib_install_rpaths += postgres_lib_d
2835+ mod_install_rpaths += postgres_lib_d
2836+ endif
2837+
2838+ # If the host can form relative rpaths, use that to make the installation
2839+ # properly relocatable
2840+ if rpath_origin != ''
2841+ # PG binaries might need to link to libpq, use relative path to reference
2842+ bin_to_lib = run_command (python, files (' src/tools/relpath.py' ),
2843+ dir_bin, dir_lib, check : true ).stdout().strip()
2844+ bin_install_rpaths += rpath_origin / bin_to_lib
2845+
2846+ # PG extensions might need to link to libpq, use relative path to reference
2847+ # (often just .)
2848+ mod_to_lib = run_command (python, files (' src/tools/relpath.py' ),
2849+ dir_lib_pkg, dir_lib, check : true ).stdout().strip()
2850+ mod_install_rpaths += rpath_origin / mod_to_lib
2851+
2852+ test_use_library_path_var = false
2853+ else
2854+
28212855 # Add absolute path to libdir to rpath. This ensures installed binaries /
28222856 # libraries find our libraries (mainly libpq).
28232857 bin_install_rpaths += dir_prefix / dir_lib
28242858 lib_install_rpaths += dir_prefix / dir_lib
28252859 mod_install_rpaths += dir_prefix / dir_lib
28262860
2827- # Add extra_lib_dirs to rpath. This ensures we find libraries we depend on.
2828- #
2829- # Not needed on darwin even if we use relative rpaths for our own libraries,
2830- # as the install_name of libraries in extra_lib_dirs will point to their
2831- # location anyway.
2832- bin_install_rpaths += postgres_lib_d
2833- lib_install_rpaths += postgres_lib_d
2834- mod_install_rpaths += postgres_lib_d
2861+ test_use_library_path_var = true
28352862endif
28362863
28372864
@@ -3180,6 +3207,14 @@ above, or by running configure and then make maintainer-clean.
31803207endif
31813208
31823209
3210+ # To make MacOS installation work without a prior make install, even with SIP
3211+ # enabled, make rpaths relative after installation. This also makes the
3212+ # installation relocatable.
3213+ if host_system == ' darwin'
3214+ meson .add_install_script(' src/tools/relativize_shared_library_references' )
3215+ endif
3216+
3217+
31833218
31843219###############################################################
31853220# Install targets
@@ -3310,7 +3345,9 @@ test_env.set('PG_TEST_EXTRA', get_option('PG_TEST_EXTRA'))
33103345# Add the temporary installation to the library search path on platforms where
33113346# that works (everything but windows, basically). On windows everything
33123347# library-like gets installed into bindir, solving that issue.
3313- if library_path_var != ''
3348+ # On platforms without $ORIGIN support we need to add the temporary
3349+ # installation to the library search path.
3350+ if library_path_var != '' and library_path_var != ''
33143351 test_env.prepend(library_path_var, temp_install_libdir)
33153352endif
33163353
0 commit comments