From b9c6f593fd974152018bc80a01d6840845d3f6b5 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 25 Jan 2025 03:18:01 +0200 Subject: [PATCH] meson: link tests and benches against shared library, not static library This makes the tests, and especially benches, more realistic, since xkbcommon is almost always used as a shared library. Also significantly reduces the build time with LTO enabled (for me, from 90s to 30s). Signed-off-by: Ran Benita --- meson.build | 54 ++++++++++++++++++++------------------- src/atom.h | 10 +++++--- src/compose/dump.c | 1 - src/compose/parser.h | 4 ++- src/context.h | 6 ++--- src/keysym.h | 25 +++++++++--------- src/utf8.c | 1 + src/utf8.h | 6 +++-- src/utils-paths.h | 2 +- src/utils.h | 17 ++++++++++-- src/xkbcomp/rules.h | 2 +- test/symbols-leak-test.py | 2 +- test/test.h | 1 + 13 files changed, 77 insertions(+), 54 deletions(-) diff --git a/meson.build b/meson.build index ee1833034..ce4655772 100644 --- a/meson.build +++ b/meson.build @@ -450,18 +450,12 @@ icu_dep = dependency('icu-uc', required: false) build_tools = get_option('enable-tools') and cc.has_header_symbol('getopt.h', 'getopt_long', prefix: '#define _GNU_SOURCE') if build_tools # Common resources - libxkbcommon_tools_internal_sources = [ - 'tools/tools-common.h', - 'tools/tools-common.c', - ] - libxkbcommon_tools_internal = static_library( - 'tools-internal', - libxkbcommon_tools_internal_sources, - dependencies: dep_libxkbcommon, - ) tools_dep = declare_dependency( + sources: [ + 'tools/tools-common.h', + 'tools/tools-common.c', + ], include_directories: [include_directories('tools', 'include')], - link_with: libxkbcommon_tools_internal, dependencies: dep_libxkbcommon, ) configh_data.set10('HAVE_TOOLS', true) @@ -553,8 +547,6 @@ if build_tools # The same tool again, but with access to some private APIs. executable('interactive-evdev', 'tools/interactive-evdev.c', - libxkbcommon_sources, - libxkbcommon_tools_internal_sources, dependencies: [tools_dep], c_args: ['-DENABLE_PRIVATE_APIS'], include_directories: [include_directories('src', 'include')], @@ -687,30 +679,34 @@ configure_file(output: 'test-config.h', configuration: test_configh_data) m_dep = cc.find_library('m', required : false) # Some tests need to use unexported symbols, so we link them against # an internal copy of libxkbcommon with all symbols exposed. -libxkbcommon_test_internal = static_library( +libxkbcommon_test_internal = library( 'xkbcommon-test-internal', - 'test/common.c', - 'test/test.h', - 'test/evdev-scancodes.h', - 'bench/bench.c', - 'bench/bench.h', libxkbcommon_sources, include_directories: include_directories('src', 'include'), c_args: ['-DENABLE_PRIVATE_APIS'], - dependencies: [m_dep], + gnu_symbol_visibility: 'hidden', ) test_dep = declare_dependency( + sources: [ + 'bench/bench.c', + 'bench/bench.h', + 'test/common.c', + 'test/evdev-scancodes.h', + 'test/test.h', + 'tools/tools-common.c', + 'tools/tools-common.h', + ], include_directories: include_directories('src', 'include'), link_with: libxkbcommon_test_internal, - dependencies: [tools_dep], + dependencies: [m_dep], ) if get_option('enable-x11') - libxkbcommon_x11_test_internal = static_library( + libxkbcommon_x11_test_internal = library( 'xkbcommon-x11-internal', libxkbcommon_x11_sources, - 'test/xvfb-wrapper.c', - 'test/xvfb-wrapper.h', include_directories: include_directories('src', 'include'), + c_args: ['-DENABLE_PRIVATE_APIS'], + gnu_symbol_visibility: 'hidden', link_with: libxkbcommon_test_internal, dependencies: [ xcb_dep, @@ -722,9 +718,15 @@ if get_option('enable-x11') dependencies: [ test_dep, xcb_dep, - xcb_xkb_dep, ], ) + x11_xvfb_test_dep = declare_dependency( + sources: [ + 'test/xvfb-wrapper.c', + 'test/xvfb-wrapper.h', + ], + dependencies: [x11_test_dep], + ) endif # TODO: version range? keysyms_test_dep = [test_dep] @@ -889,13 +891,13 @@ if get_option('enable-x11') endif test( 'x11', - executable('test-x11', 'test/x11.c', dependencies: x11_test_dep), + executable('test-x11', 'test/x11.c', dependencies: x11_xvfb_test_dep), env: test_env, is_parallel : false, ) test( 'x11comp', - executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep), + executable('test-x11comp', 'test/x11comp.c', dependencies: x11_xvfb_test_dep), env: test_env, is_parallel : false, ) diff --git a/src/atom.h b/src/atom.h index d953f7333..160d9cef6 100644 --- a/src/atom.h +++ b/src/atom.h @@ -8,20 +8,22 @@ #include #include +#include "utils.h" + typedef uint32_t xkb_atom_t; #define XKB_ATOM_NONE 0 struct atom_table; -struct atom_table * +XKB_EXPORT_PRIVATE struct atom_table * atom_table_new(void); -void +XKB_EXPORT_PRIVATE void atom_table_free(struct atom_table *table); -xkb_atom_t +XKB_EXPORT_PRIVATE xkb_atom_t atom_intern(struct atom_table *table, const char *string, size_t len, bool add); -const char * +XKB_EXPORT_PRIVATE const char * atom_text(struct atom_table *table, xkb_atom_t atom); diff --git a/src/compose/dump.c b/src/compose/dump.c index c4052f241..cb398675a 100644 --- a/src/compose/dump.c +++ b/src/compose/dump.c @@ -16,7 +16,6 @@ #include "escape.h" #include "dump.h" #include "src/keysym.h" -#include "src/utils.h" bool print_compose_table_entry(FILE *file, struct xkb_compose_table_entry *entry) diff --git a/src/compose/parser.h b/src/compose/parser.h index fda53cbc9..c77ebd51b 100644 --- a/src/compose/parser.h +++ b/src/compose/parser.h @@ -11,12 +11,14 @@ #include "xkbcommon/xkbcommon.h" #include "xkbcommon/xkbcommon-compose.h" +#include "src/utils.h" + #define MAX_LHS_LEN 10 #define MAX_INCLUDE_DEPTH 5 /** Maximum size of the string returned by xkb_compose_state_get_utf8() */ #define XKB_COMPOSE_MAX_STRING_SIZE 256 -char * +XKB_EXPORT_PRIVATE char * parse_string_literal(struct xkb_context *ctx, const char *string); bool diff --git a/src/context.h b/src/context.h index 53c6f1dad..3048fc5dc 100644 --- a/src/context.h +++ b/src/context.h @@ -65,7 +65,7 @@ xkb_context_include_path_get_system_path(struct xkb_context *ctx); xkb_atom_t xkb_atom_lookup(struct xkb_context *ctx, const char *string); -xkb_atom_t +XKB_EXPORT_PRIVATE xkb_atom_t xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len); #define xkb_atom_intern_literal(ctx, literal) \ @@ -80,13 +80,13 @@ xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len); xkb_atom_t xkb_atom_steal(struct xkb_context *ctx, char *string); -const char * +XKB_EXPORT_PRIVATE const char * xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom); char * xkb_context_get_buffer(struct xkb_context *ctx, size_t size); -ATTR_PRINTF(4, 5) void +XKB_EXPORT_PRIVATE ATTR_PRINTF(4, 5) void xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity, const char *fmt, ...); diff --git a/src/keysym.h b/src/keysym.h index 0ceb47fe3..3ae079aa1 100644 --- a/src/keysym.h +++ b/src/keysym.h @@ -11,6 +11,7 @@ #include #include "xkbcommon/xkbcommon.h" +#include "utils.h" /* * NOTE: this is not defined in xkbcommon.h, because if we did, it may add @@ -47,31 +48,31 @@ * 4 bytes + NULL-terminating byte */ #define XKB_KEYSYM_UTF8_MAX_SIZE 5 -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_assigned(xkb_keysym_t ks); struct xkb_keysym_iterator; -struct xkb_keysym_iterator* +XKB_EXPORT_PRIVATE struct xkb_keysym_iterator* xkb_keysym_iterator_new(bool explicit); -struct xkb_keysym_iterator* +XKB_EXPORT_PRIVATE struct xkb_keysym_iterator* xkb_keysym_iterator_unref(struct xkb_keysym_iterator *iter); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_iterator_next(struct xkb_keysym_iterator *iter); -xkb_keysym_t +XKB_EXPORT_PRIVATE xkb_keysym_t xkb_keysym_iterator_get_keysym(struct xkb_keysym_iterator *iter); -int +XKB_EXPORT_PRIVATE int xkb_keysym_iterator_get_name(struct xkb_keysym_iterator *iter, char *buffer, size_t size); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_iterator_is_explicitly_named(struct xkb_keysym_iterator *iter); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_deprecated(xkb_keysym_t keysym, const char *name, const char **reference_name); @@ -92,14 +93,14 @@ xkb_keysym_is_deprecated(xkb_keysym_t keysym, } \ } -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_lower(xkb_keysym_t keysym); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_upper_or_title(xkb_keysym_t keysym); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_keypad(xkb_keysym_t keysym); -bool +XKB_EXPORT_PRIVATE bool xkb_keysym_is_modifier(xkb_keysym_t keysym); diff --git a/src/utf8.c b/src/utf8.c index c6f4adb3b..b7ab0425f 100644 --- a/src/utf8.c +++ b/src/utf8.c @@ -8,6 +8,7 @@ #include "config.h" +#include "utils.h" #include "utf8.h" /* Conformant encoding form conversion from UTF-32 to UTF-8. diff --git a/src/utf8.h b/src/utf8.h index e490bb9f3..54da07ae6 100644 --- a/src/utf8.h +++ b/src/utf8.h @@ -11,8 +11,10 @@ #include #include -int +#include "utils.h" + +XKB_EXPORT_PRIVATE int utf32_to_utf8(uint32_t unichar, char *buffer); -bool +XKB_EXPORT_PRIVATE bool is_valid_utf8(const char *ss, size_t len); diff --git a/src/utils-paths.h b/src/utils-paths.h index 22890281e..c4d1a1290 100644 --- a/src/utils-paths.h +++ b/src/utils-paths.h @@ -16,5 +16,5 @@ #define is_path_separator(s) ((s) == PATH_SEPARATOR) #endif -bool +XKB_EXPORT_PRIVATE bool is_absolute(const char *path); diff --git a/src/utils.h b/src/utils.h index 051fd2d6e..99be496b9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -265,6 +265,19 @@ open_file(const char *path); /* Compiler Attributes */ +/* Private functions only exposed in tests. */ +#ifdef ENABLE_PRIVATE_APIS +# if defined(__GNUC__) && !defined(__CYGWIN__) +# define XKB_EXPORT_PRIVATE __attribute__((visibility("default"))) +# elif defined(_WIN32) +# define XKB_EXPORT_PRIVATE __declspec(dllexport) +# else +# define XKB_EXPORT_PRIVATE +# endif +#else +# define XKB_EXPORT_PRIVATE +#endif + #if defined(__MINGW32__) # define ATTR_PRINTF(x,y) __attribute__((__format__(__MINGW_PRINTF_FORMAT, x, y))) #elif defined(__GNUC__) @@ -298,10 +311,10 @@ open_file(const char *path); #endif #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF) -int asprintf(char **strp, const char *fmt, ...) ATTR_PRINTF(2, 3); +XKB_EXPORT_PRIVATE int asprintf(char **strp, const char *fmt, ...) ATTR_PRINTF(2, 3); # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF) # include -int vasprintf(char **strp, const char *fmt, va_list ap); +XKB_EXPORT_PRIVATE int vasprintf(char **strp, const char *fmt, va_list ap); # endif /* !HAVE_VASPRINTF */ #endif /* !HAVE_ASPRINTF */ diff --git a/src/xkbcomp/rules.h b/src/xkbcomp/rules.h index bd090d094..b34456240 100644 --- a/src/xkbcomp/rules.h +++ b/src/xkbcomp/rules.h @@ -8,7 +8,7 @@ #include "xkbcomp-priv.h" -bool +XKB_EXPORT_PRIVATE bool xkb_components_from_rules(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo, struct xkb_component_names *out, diff --git a/test/symbols-leak-test.py b/test/symbols-leak-test.py index 48da847dd..ba8e879de 100755 --- a/test/symbols-leak-test.py +++ b/test/symbols-leak-test.py @@ -19,7 +19,7 @@ def symbols_from_map(path): def symbols_from_src(path): - return re.findall(r"XKB_EXPORT.*\n(xkb_.*)\(", path.read_text("utf-8")) + return re.findall(r"\bXKB_EXPORT\b.*\n(xkb_.*)\(", path.read_text("utf-8")) def diff(map_path, src_paths): diff --git a/test/test.h b/test/test.h index fe0753b96..854ff6838 100644 --- a/test/test.h +++ b/test/test.h @@ -7,6 +7,7 @@ #pragma once #include +#include /* Don't use compat names in internal code. */ #define _XKBCOMMON_COMPAT_H