xref: /aosp_15_r20/external/libxkbcommon/meson.build (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1project(
2    'libxkbcommon',
3    'c',
4    version: '1.4.0',
5    default_options: [
6        'c_std=c11',
7        'warning_level=2',
8        'b_lundef=true',
9    ],
10    meson_version : '>= 0.49.0',
11)
12pkgconfig = import('pkgconfig')
13cc = meson.get_compiler('c')
14
15dir_libexec = get_option('prefix')/get_option('libexecdir')/'xkbcommon'
16
17# Compiler flags.
18cflags = [
19    '-fno-strict-aliasing',
20    '-fsanitize-undefined-trap-on-error',
21    '-Wno-unused-parameter',
22    '-Wno-missing-field-initializers',
23    '-Wpointer-arith',
24    '-Wmissing-declarations',
25    '-Wformat=2',
26    '-Wstrict-prototypes',
27    '-Wmissing-prototypes',
28    '-Wnested-externs',
29    '-Wbad-function-cast',
30    '-Wshadow',
31    '-Wlogical-op',
32    '-Wdate-time',
33    '-Wwrite-strings',
34    '-Wno-documentation-deprecated-sync',
35]
36add_project_arguments(cc.get_supported_arguments(cflags), language: 'c')
37
38
39# The XKB config root.
40XKBCONFIGROOT = get_option('xkb-config-root')
41if XKBCONFIGROOT == ''
42    xkeyboard_config_dep = dependency('xkeyboard-config', required: false)
43    if xkeyboard_config_dep.found()
44        XKBCONFIGROOT = xkeyboard_config_dep.get_pkgconfig_variable('xkb_base')
45    else
46        XKBCONFIGROOT = get_option('prefix')/get_option('datadir')/'X11'/'xkb'
47  endif
48endif
49
50XKBCONFIGEXTRAPATH = get_option('xkb-config-extra-path')
51if XKBCONFIGEXTRAPATH == ''
52    XKBCONFIGEXTRAPATH = get_option('prefix')/get_option('sysconfdir')/'xkb'
53endif
54
55# The X locale directory for compose.
56XLOCALEDIR = get_option('x-locale-root')
57if XLOCALEDIR == ''
58    XLOCALEDIR = get_option('prefix')/get_option('datadir')/'X11'/'locale'
59endif
60
61
62# config.h.
63configh_data = configuration_data()
64configh_data.set('EXIT_INVALID_USAGE', '2')
65configh_data.set_quoted('LIBXKBCOMMON_VERSION', meson.project_version())
66configh_data.set_quoted('LIBXKBCOMMON_TOOL_PATH', dir_libexec)
67# Like AC_USE_SYSTEM_EXTENSIONS, what #define to use to get extensions
68# beyond the base POSIX function set.
69if host_machine.system() == 'sunos'
70  system_extensions = '__EXTENSIONS__'
71else
72  system_extensions = '_GNU_SOURCE'
73endif
74configh_data.set(system_extensions, 1)
75system_ext_define = '#define ' + system_extensions
76configh_data.set_quoted('DFLT_XKB_CONFIG_ROOT', XKBCONFIGROOT)
77configh_data.set_quoted('DFLT_XKB_CONFIG_EXTRA_PATH', XKBCONFIGEXTRAPATH)
78configh_data.set_quoted('XLOCALEDIR', XLOCALEDIR)
79configh_data.set_quoted('DEFAULT_XKB_RULES', get_option('default-rules'))
80configh_data.set_quoted('DEFAULT_XKB_MODEL', get_option('default-model'))
81configh_data.set_quoted('DEFAULT_XKB_LAYOUT', get_option('default-layout'))
82if get_option('default-variant') != ''
83    configh_data.set_quoted('DEFAULT_XKB_VARIANT', get_option('default-variant'))
84else
85    configh_data.set('DEFAULT_XKB_VARIANT', 'NULL')
86endif
87if get_option('default-options') != ''
88    configh_data.set_quoted('DEFAULT_XKB_OPTIONS', get_option('default-options'))
89else
90    configh_data.set('DEFAULT_XKB_OPTIONS', 'NULL')
91endif
92if cc.has_header('unistd.h')
93    configh_data.set('HAVE_UNISTD_H', 1)
94endif
95if cc.links('int main(){if(__builtin_expect(1<0,0)){}}', name: '__builtin_expect')
96    configh_data.set('HAVE___BUILTIN_EXPECT', 1)
97endif
98if cc.has_header_symbol('unistd.h', 'eaccess', prefix: system_ext_define)
99    configh_data.set('HAVE_EACCESS', 1)
100endif
101if cc.has_header_symbol('unistd.h', 'euidaccess', prefix: system_ext_define)
102    configh_data.set('HAVE_EUIDACCESS', 1)
103endif
104if cc.has_header_symbol('sys/mman.h', 'mmap')
105    configh_data.set('HAVE_MMAP', 1)
106endif
107if cc.has_header_symbol('stdlib.h', 'mkostemp', prefix: system_ext_define)
108    configh_data.set('HAVE_MKOSTEMP', 1)
109endif
110if cc.has_header_symbol('fcntl.h', 'posix_fallocate', prefix: system_ext_define)
111    configh_data.set('HAVE_POSIX_FALLOCATE', 1)
112endif
113if cc.has_header_symbol('string.h', 'strndup', prefix: system_ext_define)
114    configh_data.set('HAVE_STRNDUP', 1)
115endif
116if cc.has_header_symbol('stdio.h', 'asprintf', prefix: system_ext_define)
117    configh_data.set('HAVE_ASPRINTF', 1)
118elif cc.has_header_symbol('stdio.h', 'vasprintf', prefix: system_ext_define)
119    configh_data.set('HAVE_VASPRINTF', 1)
120endif
121if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix: system_ext_define)
122    configh_data.set('HAVE_SECURE_GETENV', 1)
123elif cc.has_header_symbol('stdlib.h', '__secure_getenv', prefix: system_ext_define)
124    configh_data.set('HAVE___SECURE_GETENV', 1)
125else
126    message('C library does not support secure_getenv, using getenv instead')
127endif
128if not cc.has_header_symbol('limits.h', 'PATH_MAX', prefix: system_ext_define)
129    if host_machine.system() == 'windows'
130        # see https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
131        configh_data.set('PATH_MAX', 260)
132    else
133        configh_data.set('PATH_MAX', 4096)
134    endif
135endif
136
137# Silence some security & deprecation warnings on MSVC
138# for some unix/C functions we use.
139# https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=vs-2019
140configh_data.set('_CRT_SECURE_NO_WARNINGS', 1)
141configh_data.set('_CRT_NONSTDC_NO_WARNINGS', 1)
142configh_data.set('_CRT_NONSTDC_NO_DEPRECATE', 1)
143# Reduce unnecessary includes on MSVC.
144configh_data.set('WIN32_LEAN_AND_MEAN', 1)
145
146# Supports -Wl,--version-script?
147have_version_script = cc.links(
148    'int main(){}',
149    args: '-Wl,--version-script=' + meson.source_root()/'xkbcommon.map',
150    name: '-Wl,--version-script',
151)
152
153map_to_def = find_program('scripts/map-to-def')
154
155# libxkbcommon.
156# Note: we use some yacc extensions, which work with either GNU bison
157# (preferred) or byacc (with backtracking enabled).
158bison = find_program('bison', 'win_bison', required: false)
159if bison.found()
160    yacc_gen = generator(
161        bison,
162        output: ['@[email protected]', '@[email protected]'],
163        arguments: ['--defines=@OUTPUT1@', '-o', '@OUTPUT0@', '-p', '_xkbcommon_', '@INPUT@'],
164    )
165else
166    byacc = find_program('byacc', required: false)
167    if byacc.found()
168        yacc_gen = generator(
169            byacc,
170            output: ['@[email protected]', '@[email protected]'],
171            arguments: ['-H', '@OUTPUT1@', '-o', '@OUTPUT0@', '-p', '_xkbcommon_', '@INPUT@'],
172        )
173    else
174        error('Could not find a compatible YACC program (bison or byacc)')
175    endif
176endif
177libxkbcommon_sources = [
178    'src/compose/parser.c',
179    'src/compose/parser.h',
180    'src/compose/paths.c',
181    'src/compose/paths.h',
182    'src/compose/state.c',
183    'src/compose/table.c',
184    'src/compose/table.h',
185    'src/xkbcomp/action.c',
186    'src/xkbcomp/action.h',
187    'src/xkbcomp/ast.h',
188    'src/xkbcomp/ast-build.c',
189    'src/xkbcomp/ast-build.h',
190    'src/xkbcomp/compat.c',
191    'src/xkbcomp/expr.c',
192    'src/xkbcomp/expr.h',
193    'src/xkbcomp/include.c',
194    'src/xkbcomp/include.h',
195    'src/xkbcomp/keycodes.c',
196    'src/xkbcomp/keymap.c',
197    'src/xkbcomp/keymap-dump.c',
198    'src/xkbcomp/keywords.c',
199    yacc_gen.process('src/xkbcomp/parser.y'),
200    'src/xkbcomp/parser-priv.h',
201    'src/xkbcomp/rules.c',
202    'src/xkbcomp/rules.h',
203    'src/xkbcomp/scanner.c',
204    'src/xkbcomp/symbols.c',
205    'src/xkbcomp/types.c',
206    'src/xkbcomp/vmod.c',
207    'src/xkbcomp/vmod.h',
208    'src/xkbcomp/xkbcomp.c',
209    'src/xkbcomp/xkbcomp-priv.h',
210    'src/atom.c',
211    'src/atom.h',
212    'src/context.c',
213    'src/context.h',
214    'src/context-priv.c',
215    'src/darray.h',
216    'src/keysym.c',
217    'src/keysym.h',
218    'src/keysym-utf.c',
219    'src/ks_tables.h',
220    'src/keymap.c',
221    'src/keymap.h',
222    'src/keymap-priv.c',
223    'src/scanner-utils.h',
224    'src/state.c',
225    'src/text.c',
226    'src/text.h',
227    'src/utf8.c',
228    'src/utf8.h',
229    'src/utils.c',
230    'src/utils.h',
231]
232libxkbcommon_link_args = []
233libxkbcommon_link_deps = []
234if have_version_script
235    libxkbcommon_link_args += '-Wl,--version-script=' + meson.source_root()/'xkbcommon.map'
236    libxkbcommon_link_deps += 'xkbcommon.map'
237elif cc.get_argument_syntax() == 'msvc'
238    libxkbcommon_def = custom_target('xkbcommon.def',
239        command: [map_to_def, '@INPUT@', '@OUTPUT@'],
240        input: 'xkbcommon.map',
241        output: 'kxbcommon.def',
242    )
243    libxkbcommon_link_deps += libxkbcommon_def
244    libxkbcommon_link_args += '/DEF:' + libxkbcommon_def.full_path()
245endif
246libxkbcommon = library(
247    'xkbcommon',
248    'include/xkbcommon/xkbcommon.h',
249    libxkbcommon_sources,
250    link_args: libxkbcommon_link_args,
251    link_depends: libxkbcommon_link_deps,
252    gnu_symbol_visibility: 'hidden',
253    version: '0.0.0',
254    install: true,
255    include_directories: include_directories('src', 'include'),
256)
257install_headers(
258    'include/xkbcommon/xkbcommon.h',
259    'include/xkbcommon/xkbcommon-compat.h',
260    'include/xkbcommon/xkbcommon-compose.h',
261    'include/xkbcommon/xkbcommon-keysyms.h',
262    'include/xkbcommon/xkbcommon-names.h',
263    subdir: 'xkbcommon',
264)
265
266dep_libxkbcommon = declare_dependency(
267    link_with: libxkbcommon,
268    include_directories: include_directories('include'),
269)
270pkgconfig.generate(
271    libxkbcommon,
272    name: 'xkbcommon',
273    filebase: 'xkbcommon',
274    version: meson.project_version(),
275    description: 'XKB API common to servers and clients',
276)
277
278
279# libxkbcommon-x11.
280if get_option('enable-x11')
281    xcb_dep = dependency('xcb', version: '>=1.10', required: false)
282    xcb_xkb_dep = dependency('xcb-xkb', version: '>=1.10', required: false)
283    if not xcb_dep.found() or not xcb_xkb_dep.found()
284        error('''X11 support requires xcb-xkb >= 1.10 which was not found.
285You can disable X11 support with -Denable-x11=false.''')
286    endif
287
288    libxkbcommon_x11_sources = [
289        'src/x11/keymap.c',
290        'src/x11/state.c',
291        'src/x11/util.c',
292        'src/x11/x11-priv.h',
293        'src/context.h',
294        'src/context-priv.c',
295        'src/keymap.h',
296        'src/keymap-priv.c',
297        'src/atom.h',
298        'src/atom.c',
299    ]
300    libxkbcommon_x11_link_args = []
301    libxkbcommon_x11_link_deps = []
302    if have_version_script
303        libxkbcommon_x11_link_args += '-Wl,--version-script=' + meson.source_root()/'xkbcommon-x11.map'
304        libxkbcommon_x11_link_deps += 'xkbcommon-x11.map'
305    elif cc.get_argument_syntax() == 'msvc'
306        libxkbcommon_x11_def = custom_target('xkbcommon-x11.def',
307            command: [map_to_def, '@INPUT@', '@OUTPUT@'],
308            input: 'xkbcommon-x11.map',
309            output: 'xkbcommon-x11.def',
310        )
311        libxkbcommon_x11_link_deps += libxkbcommon_x11_def
312        libxkbcommon_x11_link_args += '/DEF:' + libxkbcommon_x11_def.full_path()
313    endif
314    libxkbcommon_x11 = library(
315        'xkbcommon-x11',
316        'include/xkbcommon/xkbcommon-x11.h',
317        libxkbcommon_x11_sources,
318        link_args: libxkbcommon_x11_link_args,
319        link_depends: libxkbcommon_x11_link_deps,
320        gnu_symbol_visibility: 'hidden',
321        version: '0.0.0',
322        install: true,
323        include_directories: include_directories('src', 'include'),
324        link_with: libxkbcommon,
325        dependencies: [
326            xcb_dep,
327            xcb_xkb_dep,
328        ],
329    )
330    install_headers(
331        'include/xkbcommon/xkbcommon-x11.h',
332        subdir: 'xkbcommon',
333    )
334    dep_libxkbcommon_x11 = declare_dependency(
335        link_with: libxkbcommon_x11,
336        include_directories: include_directories('include'),
337    )
338    pkgconfig.generate(
339        libxkbcommon_x11,
340        name: 'xkbcommon-x11',
341        filebase: 'xkbcommon-x11',
342        version: meson.project_version(),
343        description: 'XKB API common to servers and clients - X11 support',
344        requires: ['xkbcommon'],
345        requires_private: ['xcb>=1.10', 'xcb-xkb>=1.10'],
346    )
347endif
348
349# libxkbregistry
350if get_option('enable-xkbregistry')
351    dep_libxml = dependency('libxml-2.0')
352    deps_libxkbregistry = [dep_libxml]
353    libxkbregistry_sources = [
354        'src/registry.c',
355        'src/utils.h',
356        'src/utils.c',
357        'src/util-list.h',
358        'src/util-list.c',
359    ]
360    libxkbregistry_link_args = []
361    libxkbregistry_link_deps = []
362    if have_version_script
363        libxkbregistry_link_args += '-Wl,--version-script=' + meson.source_root()/'xkbregistry.map'
364        libxkbregistry_link_deps += 'xkbregistry.map'
365    elif cc.get_argument_syntax() == 'msvc'
366        libxkbregistry_def = custom_target('xkbregistry.def',
367            command: [map_to_def, '@INPUT@', '@OUTPUT@'],
368            input: 'xkbregistry.map',
369            output: 'xkbregistry.def',
370        )
371        libxkbregistry_link_deps += libxkbregistry_def
372        libxkbregistry_link_args += '/DEF:' + libxkbregistry_def.full_path()
373    endif
374    libxkbregistry = library(
375        'xkbregistry',
376        'include/xkbcommon/xkbregistry.h',
377        libxkbregistry_sources,
378        link_args: libxkbregistry_link_args,
379        link_depends: libxkbregistry_link_deps,
380        gnu_symbol_visibility: 'hidden',
381        dependencies: deps_libxkbregistry,
382        version: '0.0.0',
383        install: true,
384        include_directories: include_directories('src', 'include'),
385    )
386    install_headers(
387        'include/xkbcommon/xkbregistry.h',
388        subdir: 'xkbcommon',
389    )
390    pkgconfig.generate(
391        libxkbregistry,
392        name: 'xkbregistry',
393        filebase: 'xkbregistry',
394        version: meson.project_version(),
395        description: 'XKB API to query available rules, models, layouts, variants and options',
396    )
397
398    dep_libxkbregistry = declare_dependency(
399        link_with: libxkbregistry,
400        include_directories: include_directories('include'),
401    )
402endif
403
404man_pages = []
405
406# Tools
407build_tools = get_option('enable-tools') and cc.has_header_symbol('getopt.h', 'getopt_long', prefix: '#define _GNU_SOURCE')
408if build_tools
409    libxkbcommon_tools_internal = static_library(
410        'tools-internal',
411        'tools/tools-common.h',
412        'tools/tools-common.c',
413        dependencies: dep_libxkbcommon,
414    )
415    tools_dep = declare_dependency(
416        include_directories: [include_directories('tools', 'include')],
417        link_with: libxkbcommon_tools_internal,
418    )
419
420    executable('xkbcli', 'tools/xkbcli.c',
421               dependencies: tools_dep, install: true)
422    install_man('tools/xkbcli.1')
423
424    xkbcli_compile_keymap = executable('xkbcli-compile-keymap',
425                                       'tools/compile-keymap.c',
426                                       dependencies: tools_dep,
427                                       install: true,
428                                       install_dir: dir_libexec)
429    install_man('tools/xkbcli-compile-keymap.1')
430    # The same tool again, but with access to some private APIs.
431    executable('compile-keymap',
432               'tools/compile-keymap.c',
433               libxkbcommon_sources,
434               dependencies: [tools_dep],
435               c_args: ['-DENABLE_PRIVATE_APIS'],
436               include_directories: [include_directories('src', 'include')],
437               install: false)
438    executable('compose',
439               'tools/compose.c',
440               dependencies: tools_dep,
441               include_directories: [include_directories('src', 'include')],
442               install: false)
443    configh_data.set10('HAVE_XKBCLI_COMPILE_KEYMAP', true)
444    executable('xkbcli-how-to-type',
445               'tools/how-to-type.c',
446               dependencies: tools_dep,
447               install: true,
448               install_dir: dir_libexec)
449    install_man('tools/xkbcli-how-to-type.1')
450    configh_data.set10('HAVE_XKBCLI_HOW_TO_TYPE', true)
451    if cc.has_header('linux/input.h')
452        executable('xkbcli-interactive-evdev',
453                   'tools/interactive-evdev.c',
454                   dependencies: tools_dep,
455                   install: true,
456                   install_dir: dir_libexec)
457        configh_data.set10('HAVE_XKBCLI_INTERACTIVE_EVDEV', true)
458        install_man('tools/xkbcli-interactive-evdev.1')
459    endif
460    if get_option('enable-x11')
461        x11_tools_dep = declare_dependency(
462            link_with: libxkbcommon_x11,
463            dependencies: [
464                tools_dep,
465                xcb_dep,
466                xcb_xkb_dep,
467            ],
468        )
469        executable('xkbcli-interactive-x11',
470                   'tools/interactive-x11.c',
471                   dependencies: x11_tools_dep,
472                   install: true,
473                   install_dir: dir_libexec)
474        install_man('tools/xkbcli-interactive-x11.1')
475        configh_data.set10('HAVE_XKBCLI_INTERACTIVE_X11', true)
476    endif
477    if get_option('enable-wayland')
478        wayland_client_dep = dependency('wayland-client', version: '>=1.2.0', required: false)
479        wayland_protocols_dep = dependency('wayland-protocols', version: '>=1.12', required: false)
480        wayland_scanner_dep = dependency('wayland-scanner', required: false, native: true)
481        if not wayland_client_dep.found() or not wayland_protocols_dep.found() or not wayland_scanner_dep.found()
482            error('''The Wayland xkbcli programs require wayland-client >= 1.2.0, wayland-protocols >= 1.7 which were not found.
483You can disable the Wayland xkbcli programs with -Denable-wayland=false.''')
484        endif
485
486        wayland_scanner = find_program(wayland_scanner_dep.get_pkgconfig_variable('wayland_scanner'))
487        wayland_scanner_code_gen = generator(
488            wayland_scanner,
489            output: '@[email protected]',
490            arguments: ['code', '@INPUT@', '@OUTPUT@'],
491        )
492        wayland_scanner_client_header_gen = generator(
493            wayland_scanner,
494            output: '@[email protected]',
495            arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
496        )
497        wayland_protocols_datadir = wayland_protocols_dep.get_pkgconfig_variable('pkgdatadir')
498        xdg_shell_xml = wayland_protocols_datadir/'stable/xdg-shell/xdg-shell.xml'
499        xdg_shell_sources = [
500            wayland_scanner_code_gen.process(xdg_shell_xml),
501            wayland_scanner_client_header_gen.process(xdg_shell_xml),
502        ]
503        executable('xkbcli-interactive-wayland',
504                   'tools/interactive-wayland.c',
505                   xdg_shell_sources,
506                   dependencies: [tools_dep, wayland_client_dep],
507                   install: true,
508                   install_dir: dir_libexec)
509        install_man('tools/xkbcli-interactive-wayland.1')
510        configh_data.set10('HAVE_XKBCLI_INTERACTIVE_WAYLAND', true)
511    endif
512
513    if get_option('enable-xkbregistry')
514        configh_data.set10('HAVE_XKBCLI_LIST', true)
515        executable('xkbcli-list',
516                   'tools/registry-list.c',
517                   dependencies: dep_libxkbregistry,
518                   install: true,
519                   install_dir: dir_libexec)
520        install_man('tools/xkbcli-list.1')
521    endif
522endif
523
524
525# xkeyboard-config "verifier"
526xkct_config = configuration_data()
527xkct_config.set('MESON_BUILD_ROOT', meson.build_root())
528xkct_config.set('XKB_CONFIG_ROOT', XKBCONFIGROOT)
529configure_file(input: 'test/xkeyboard-config-test.py.in',
530               output: 'xkeyboard-config-test',
531               configuration: xkct_config)
532
533# Tests
534test_env = environment()
535test_env.set('XKB_LOG_LEVEL', 'debug')
536test_env.set('XKB_LOG_VERBOSITY', '10')
537test_env.set('top_srcdir', meson.source_root())
538test_env.set('top_builddir', meson.build_root())
539test_env.set('HAVE_XKBCLI_INTERACTIVE_EVDEV', configh_data.get('HAVE_XKBCLI_INTERACTIVE_EVDEV', 0).to_string())
540test_env.set('HAVE_XKBCLI_INTERACTIVE_WAYLAND', configh_data.get('HAVE_XKBCLI_INTERACTIVE_WAYLAND', 0).to_string())
541test_env.set('HAVE_XKBCLI_INTERACTIVE_X11', configh_data.get('HAVE_XKBCLI_INTERACTIVE_X11', 0).to_string())
542test_env.set('HAVE_XKBCLI_LIST', configh_data.get('HAVE_XKBCLI_LIST', 0).to_string())
543
544test_configh_data = configuration_data()
545test_configh_data.set_quoted('TEST_XKB_CONFIG_ROOT', meson.source_root()/'test'/'data')
546configure_file(output: 'test-config.h', configuration: test_configh_data)
547
548# Some tests need to use unexported symbols, so we link them against
549# an internal copy of libxkbcommon with all symbols exposed.
550libxkbcommon_test_internal = static_library(
551    'xkbcommon-test-internal',
552    'test/common.c',
553    'test/test.h',
554    'test/evdev-scancodes.h',
555    'bench/bench.c',
556    'bench/bench.h',
557    libxkbcommon_sources,
558    include_directories: include_directories('src', 'include'),
559)
560test_dep = declare_dependency(
561    include_directories: include_directories('src', 'include'),
562    link_with: libxkbcommon_test_internal,
563)
564if get_option('enable-x11')
565    libxkbcommon_x11_internal = static_library(
566        'xkbcommon-x11-internal',
567        libxkbcommon_x11_sources,
568        include_directories: include_directories('src', 'include'),
569        link_with: libxkbcommon_test_internal,
570        dependencies: [
571            xcb_dep,
572            xcb_xkb_dep,
573        ],
574    )
575    x11_test_dep = declare_dependency(
576        link_with: libxkbcommon_x11_internal,
577        dependencies: [
578            test_dep,
579            xcb_dep,
580            xcb_xkb_dep,
581        ],
582    )
583endif
584test(
585    'keysym',
586    executable('test-keysym', 'test/keysym.c', dependencies: test_dep),
587    env: test_env,
588)
589test(
590    'keymap',
591    executable('test-keymap', 'test/keymap.c', dependencies: test_dep),
592    env: test_env,
593)
594test(
595    'filecomp',
596    executable('test-filecomp', 'test/filecomp.c', dependencies: test_dep),
597    env: test_env,
598)
599# TODO: This test currently uses some functions that don't exist on Windows.
600if cc.get_id() != 'msvc'
601  test(
602      'context',
603      executable('test-context', 'test/context.c', dependencies: test_dep),
604      env: test_env,
605  )
606endif
607test(
608    'rules-file',
609    executable('test-rules-file', 'test/rules-file.c', dependencies: test_dep),
610    env: test_env,
611)
612test(
613    'rules-file-includes',
614    executable('test-rules-file-includes', 'test/rules-file-includes.c', dependencies: test_dep),
615    env: test_env,
616)
617test(
618    'stringcomp',
619    executable('test-stringcomp', 'test/stringcomp.c', dependencies: test_dep),
620    env: test_env,
621)
622test(
623    'buffercomp',
624    executable('test-buffercomp', 'test/buffercomp.c', dependencies: test_dep),
625    env: test_env,
626)
627test(
628    'log',
629    executable('test-log', 'test/log.c', dependencies: test_dep),
630    env: test_env,
631)
632test(
633    'atom',
634    executable('test-atom', 'test/atom.c', dependencies: test_dep),
635    env: test_env,
636)
637test(
638    'utf8',
639    executable('test-utf8', 'test/utf8.c', dependencies: test_dep),
640    env: test_env,
641)
642test(
643    'state',
644    executable('test-state', 'test/state.c', dependencies: test_dep),
645    env: test_env,
646)
647test(
648    'keyseq',
649    executable('test-keyseq', 'test/keyseq.c', dependencies: test_dep),
650    env: test_env,
651)
652test(
653    'rulescomp',
654    executable('test-rulescomp', 'test/rulescomp.c', dependencies: test_dep),
655    env: test_env,
656)
657test(
658    'compose',
659    executable('test-compose', 'test/compose.c', dependencies: test_dep),
660    env: test_env,
661)
662test(
663    'utils',
664    executable('test-utils', 'test/utils.c', dependencies: test_dep),
665    env: test_env,
666)
667test(
668    'symbols-leak-test',
669    find_program('test/symbols-leak-test.py'),
670    env: test_env,
671    suite: ['python-tests'],
672)
673if get_option('enable-x11')
674    test(
675        'x11',
676        executable('test-x11', 'test/x11.c', dependencies: x11_test_dep),
677        env: test_env,
678    )
679    # test/x11comp is meant to be run, but it is (temporarily?) disabled.
680    # See: https://github.com/xkbcommon/libxkbcommon/issues/30
681    executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep)
682endif
683if get_option('enable-xkbregistry')
684    test(
685        'registry',
686        executable('test-registry', 'test/registry.c',
687                   include_directories: include_directories('src'),
688                   dependencies: dep_libxkbregistry),
689        env: test_env,
690    )
691endif
692if build_tools
693    test('tool-option-parsing',
694         find_program('test/tool-option-parsing.py'),
695         env: test_env,
696         suite: ['python-tests'])
697
698    # A set of keysyms to test for. Add one or two symbols to this array
699    # whenever the xorgproto gets updated to make sure we resolve them.
700    keysyms_to_test = [
701        'XF86Macro23',
702    ]
703
704    env = environment()
705    env.set('XKB_CONFIG_ROOT', meson.source_root()/'test'/'data')
706    foreach keysym: keysyms_to_test
707        test('keysym-test-@0@'.format(keysym),
708             find_program('test/test-keysym.py'),
709             env: env,
710             args: [keysym, '--tool', xkbcli_compile_keymap],
711             suite: ['python-tests'])
712    endforeach
713endif
714
715valgrind = find_program('valgrind', required: false)
716if valgrind.found()
717    add_test_setup('valgrind',
718        exe_wrapper: [valgrind,
719                       '--leak-check=full',
720                       '--track-origins=yes',
721                       '--gen-suppressions=all',
722                       '--error-exitcode=99'],
723        timeout_multiplier : 10)
724else
725    message('valgrind not found, disabling valgrind test setup')
726endif
727
728
729# Fuzzing target programs.
730executable('fuzz-keymap', 'fuzz/keymap/target.c', dependencies: test_dep)
731executable('fuzz-compose', 'fuzz/compose/target.c', dependencies: test_dep)
732
733
734# Benchmarks.
735bench_env = environment()
736bench_env.set('top_srcdir', meson.source_root())
737benchmark(
738    'key-proc',
739    executable('bench-key-proc', 'bench/key-proc.c', dependencies: test_dep),
740    env: bench_env,
741)
742benchmark(
743    'rules',
744    executable('bench-rules', 'bench/rules.c', dependencies: test_dep),
745    env: bench_env,
746)
747benchmark(
748    'rulescomp',
749    executable('bench-rulescomp', 'bench/rulescomp.c', dependencies: test_dep),
750    env: bench_env,
751)
752benchmark(
753    'compose',
754    executable('bench-compose', 'bench/compose.c', dependencies: test_dep),
755    env: bench_env,
756)
757benchmark(
758    'atom',
759    executable('bench-atom', 'bench/atom.c', dependencies: test_dep),
760    env: bench_env,
761)
762if get_option('enable-x11')
763  benchmark(
764      'x11',
765      executable('bench-x11', 'bench/x11.c', dependencies: x11_test_dep),
766      env: bench_env,
767  )
768endif
769
770
771# Documentation.
772if get_option('enable-docs')
773    doxygen = find_program('doxygen', required: false)
774    if not doxygen.found()
775        error('''Documentation requires doxygen which was not found.
776You can disable the documentation with -Denable-docs=false.''')
777    endif
778    doxygen_wrapper = find_program('scripts/doxygen-wrapper')
779
780    doxygen_input = [
781        'README.md',
782        'doc/doxygen-extra.css',
783        'doc/quick-guide.md',
784        'doc/compat.md',
785        'doc/user-configuration.md',
786        'doc/rules-format.md',
787        'doc/keymap-format-text-v1.md',
788        'include/xkbcommon/xkbcommon.h',
789        'include/xkbcommon/xkbcommon-names.h',
790        'include/xkbcommon/xkbcommon-x11.h',
791        'include/xkbcommon/xkbcommon-compose.h',
792        'include/xkbcommon/xkbregistry.h',
793    ]
794    doxygen_data = configuration_data()
795    doxygen_data.set('PACKAGE_NAME', meson.project_name())
796    doxygen_data.set('PACKAGE_VERSION', meson.project_version())
797    doxygen_data.set('INPUT', ' '.join(doxygen_input))
798    doxygen_data.set('OUTPUT_DIRECTORY', meson.build_root())
799    doxyfile = configure_file(
800        input: 'doc/Doxyfile.in',
801        output: 'Doxyfile',
802        configuration: doxygen_data,
803    )
804    # TODO: Meson should provide this.
805    docdir = get_option('datadir')/'doc'/meson.project_name()
806    custom_target(
807        'doc',
808        input: [doxyfile] + doxygen_input,
809        output: 'html',
810        command: [doxygen_wrapper, doxygen.path(), meson.build_root()/'Doxyfile', meson.source_root()],
811        install: true,
812        install_dir: docdir,
813        build_by_default: true,
814    )
815endif
816
817configure_file(output: 'config.h', configuration: configh_data)
818
819
820# Stable variables for projects using xkbcommon as a subproject.
821# These variables should not be renamed.
822libxkbcommon_dep = dep_libxkbcommon
823if get_option('enable-x11')
824  libxkbcommon_x11_dep = dep_libxkbcommon_x11
825endif
826if get_option('enable-xkbregistry')
827  libxkbregistry_dep = dep_libxkbregistry
828endif
829