]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
meson: Use -fvisibility=hidden on Unix if supported
authorSimon McVittie <smcv@collabora.com>
Fri, 6 Jan 2023 13:49:17 +0000 (13:49 +0000)
committerSimon McVittie <smcv@collabora.com>
Mon, 9 Jan 2023 14:46:02 +0000 (14:46 +0000)
This avoids accidentally exporting all of our private symbols as part
of the LIBDBUS_PRIVATE_x.y.z verdef, including ones that don't need to
be visible outside the shared library even when testing internals
(_dbus_watch_set_handler is a good example).

Helps: https://gitlab.freedesktop.org/dbus/dbus/-/issues/437
Signed-off-by: Simon McVittie <smcv@collabora.com>
meson.build

index fdb54e77dea50e3185359ef19f4b171998976e31..31423007873c69da82a5bd54c4ead71e9cde8553 100644 (file)
@@ -200,6 +200,27 @@ if using_cpp
     add_project_arguments(compile_args_cpp, language: 'cpp')
 endif
 
+# Try to use hidden visibility on Unix. We don't do this on Windows to avoid
+# interfering with use of __declspec(dllexport) and __declspec(dllimport),
+# which have a similar effect.
+if cc.has_argument('-fvisibility=hidden') and not platform_windows
+    default_visibility = '__attribute__ ((__visibility__ ("default")))'
+    test_visibility = '''
+@0@ void public_func (void);
+@0@ int public_var;
+int main (void) { return 0; }
+'''.format(default_visibility)
+
+    if cc.compiles(test_visibility, args: compile_args_c + ['-fvisibility=hidden'])
+        add_project_arguments(['-fvisibility=hidden'], language: 'c')
+        config.set('DBUS_PRIVATE_EXPORT', default_visibility)
+        config.set('DBUS_EXPORT', default_visibility)
+        # If we were using C++ then we'd need to add -fvisibility=hidden
+        # to the C++ arguments too, but that's not currently applicable here.
+        assert(not using_cpp, 'C++ only used on Windows')
+    endif
+endif
+
 if host_machine.endian() == 'big'
   config.set('WORDS_BIGENDIAN', 1)
 endif