@@ -167,6 +167,7 @@ portname = host_system
167167
168168exesuffix = '' # overridden below where necessary
169169dlsuffix = ' .so' # overridden below where necessary
170+ rpath_origin = ' $ORIGIN'
170171library_path_var = ' LD_LIBRARY_PATH'
171172
172173# Format of file to control exports from libraries, and how to pass them to
@@ -222,6 +223,7 @@ elif host_system == 'cygwin'
222223elif host_system == ' darwin'
223224 dlsuffix = ' .dylib'
224225 library_path_var = ' DYLD_LIBRARY_PATH'
226+ rpath_origin = ' @loader_path'
225227
226228 export_file_format = ' darwin'
227229 export_fmt = ' -exported_symbols_list=@0@'
@@ -260,8 +262,16 @@ elif host_system == 'netbsd'
260262 # LDFLAGS.
261263 ldflags += [' -Wl,-z,now' , ' -Wl,-z,relro' ]
262264
265+ # netbsd patched their meson in a broken way:
266+ # https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=56959
267+ # until there's a way out of that, disable rpath_origin
268+ rpath_origin = ''
269+
263270elif host_system == ' openbsd'
264- # you're ok
271+ # openbsd's $ORIGIN doesn't use an absolute path to the binary, but argv[0]
272+ # (i.e. absolute when invoked with an absolute name, but e.g. not absolute
273+ # when invoked via PATH search).
274+ rpath_origin = ''
265275
266276elif host_system == ' sunos'
267277 portname = ' solaris'
@@ -273,6 +283,7 @@ elif host_system == 'windows'
273283 exesuffix = ' .exe'
274284 dlsuffix = ' .dll'
275285 library_path_var = ''
286+ rpath_origin = ''
276287
277288 export_file_format = ' win'
278289 export_file_suffix = ' def'
@@ -2596,25 +2607,41 @@ bin_install_rpaths = []
25962607lib_install_rpaths = []
25972608mod_install_rpaths = []
25982609
2599-
2600- # Don't add rpaths on darwin for now - as long as only absolute references to
2601- # libraries are needed, absolute LC_ID_DYLIB ensures libraries can be found in
2602- # their final destination.
2610+ # Add extra_lib_dirs to rpath. This ensures we find libraries we depend on.
2611+ #
2612+ # Not needed on darwin, even if we use relative rpaths for our own libraries,
2613+ # as the install_name of libraries in extra_lib_dirs will point to their
2614+ # location anyway.
26032615if host_system != ' darwin'
2616+ bin_install_rpaths += postgres_lib_d
2617+ lib_install_rpaths += postgres_lib_d
2618+ mod_install_rpaths += postgres_lib_d
2619+ endif
2620+
2621+ # If the host can form relative rpaths, use that to make the installation
2622+ # properly relocatable
2623+ if rpath_origin != ''
2624+ # PG binaries might need to link to libpq, use relative path to reference
2625+ bin_to_lib = run_command (python, files (' src/tools/relpath.py' ),
2626+ dir_bin, dir_lib, check : true ).stdout().strip()
2627+ bin_install_rpaths += rpath_origin / bin_to_lib
2628+
2629+ # PG extensions might need to link to libpq, use relative path to reference
2630+ # (often just .)
2631+ mod_to_lib = run_command (python, files (' src/tools/relpath.py' ),
2632+ dir_lib_pkg, dir_lib, check : true ).stdout().strip()
2633+ mod_install_rpaths += rpath_origin / mod_to_lib
2634+
2635+ test_use_library_path_var = false
2636+ else
2637+
26042638 # Add absolute path to libdir to rpath. This ensures installed binaries /
26052639 # libraries find our libraries (mainly libpq).
26062640 bin_install_rpaths += dir_prefix / dir_lib
26072641 lib_install_rpaths += dir_prefix / dir_lib
26082642 mod_install_rpaths += dir_prefix / dir_lib
26092643
2610- # Add extra_lib_dirs to rpath. This ensures we find libraries we depend on.
2611- #
2612- # Not needed on darwin even if we use relative rpaths for our own libraries,
2613- # as the install_name of libraries in extra_lib_dirs will point to their
2614- # location anyway.
2615- bin_install_rpaths += postgres_lib_d
2616- lib_install_rpaths += postgres_lib_d
2617- mod_install_rpaths += postgres_lib_d
2644+ test_use_library_path_var = true
26182645endif
26192646
26202647
@@ -2938,6 +2965,14 @@ above, or by running configure and then make maintainer-clean.
29382965endif
29392966
29402967
2968+ # To make MacOS installation work without a prior make install, even with SIP
2969+ # enabled, make rpaths relative after installation. This also makes the
2970+ # installation relocatable.
2971+ if host_system == ' darwin'
2972+ meson .add_install_script(' src/tools/relativize_shared_library_references' )
2973+ endif
2974+
2975+
29412976
29422977###############################################################
29432978# Install targets
@@ -3062,10 +3097,9 @@ test_env.set('REGRESS_SHLIB', regress_module.full_path())
30623097# Export PG_TEST_EXTRA so it can be checked in individual tap tests.
30633098test_env.set(' PG_TEST_EXTRA' , get_option (' PG_TEST_EXTRA' ))
30643099
3065- # Add the temporary installation to the library search path on platforms where
3066- # that works (everything but windows, basically). On windows everything
3067- # library-like gets installed into bindir, solving that issue.
3068- if library_path_var != ''
3100+ # On platforms without $ORIGIN support we need to add the temporary
3101+ # installation to the library search path.
3102+ if test_use_library_path_var and library_path_var != ''
30693103 test_env.prepend(library_path_var, test_install_location / get_option (' libdir' ))
30703104endif
30713105
0 commit comments