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