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