test: untangle interactive-x11 from the test headers
[platform/upstream/libxkbcommon.git] / meson.build
1 project(
2     'libxkbcommon',
3     'c',
4     version: '0.10.0',
5     default_options: [
6         'c_std=c99',
7         'warning_level=2',
8         'b_lundef=true',
9     ],
10     meson_version : '>= 0.41.0',
11 )
12 pkgconfig = import('pkgconfig')
13 cc = meson.get_compiler('c')
14
15
16 # Compiler flags.
17 foreach cflag: [
18     '-fvisibility=hidden',
19     '-fno-strict-aliasing',
20     '-fsanitize-undefined-trap-on-error',
21     '-Wextra',
22     '-Wno-unused-parameter',
23     '-Wno-missing-field-initializers',
24     '-Wpointer-arith',
25     '-Wmissing-declarations',
26     '-Wformat=2',
27     '-Wstrict-prototypes',
28     '-Wmissing-prototypes',
29     '-Wnested-externs',
30     '-Wbad-function-cast',
31     '-Wshadow',
32     '-Wlogical-op',
33     '-Wdate-time',
34     '-Wwrite-strings',
35     '-Wno-documentation-deprecated-sync',
36 ]
37     if cc.has_argument(cflag)
38         add_project_arguments(cflag, language: 'c')
39     endif
40 endforeach
41
42
43 # The XKB config root.
44 XKBCONFIGROOT = get_option('xkb-config-root')
45 if XKBCONFIGROOT == ''
46     xkeyboard_config_dep = dependency('xkeyboard-config', required: false)
47     if xkeyboard_config_dep.found()
48         XKBCONFIGROOT = xkeyboard_config_dep.get_pkgconfig_variable('xkb_base')
49     else
50         XKBCONFIGROOT = join_paths(get_option('prefix'), get_option('datadir'), 'X11', 'xkb')
51   endif
52 endif
53
54
55 # The X locale directory for compose.
56 XLOCALEDIR = get_option('x-locale-root')
57 if XLOCALEDIR == ''
58     XLOCALEDIR = join_paths(get_option('prefix'), get_option('datadir'), 'X11', 'locale')
59 endif
60
61
62 # config.h.
63 configh_data = configuration_data()
64 # Like AC_USE_SYSTEM_EXTENSIONS, what #define to use to get extensions
65 # beyond the base POSIX function set.
66 if host_machine.system() == 'sunos'
67   system_extensions = '__EXTENSIONS__'
68 else
69   system_extensions = '_GNU_SOURCE'
70 endif
71 configh_data.set(system_extensions, 1)
72 system_ext_define = '#define ' + system_extensions
73 configh_data.set_quoted('DFLT_XKB_CONFIG_ROOT', XKBCONFIGROOT)
74 configh_data.set_quoted('XLOCALEDIR', XLOCALEDIR)
75 configh_data.set_quoted('DEFAULT_XKB_RULES', get_option('default-rules'))
76 configh_data.set_quoted('DEFAULT_XKB_MODEL', get_option('default-model'))
77 configh_data.set_quoted('DEFAULT_XKB_LAYOUT', get_option('default-layout'))
78 if get_option('default-variant') != ''
79     configh_data.set_quoted('DEFAULT_XKB_VARIANT', get_option('default-variant'))
80 endif
81 if get_option('default-options') != ''
82     configh_data.set_quoted('DEFAULT_XKB_OPTIONS', get_option('default-options'))
83 endif
84 if cc.links('int main(){if(__builtin_expect(1<0,0)){}}', name: '__builtin_expect')
85     configh_data.set('HAVE___BUILTIN_EXPECT', 1)
86 endif
87 if cc.has_header_symbol('unistd.h', 'eaccess', prefix: system_ext_define)
88     configh_data.set('HAVE_EACCESS', 1)
89 endif
90 if cc.has_header_symbol('unistd.h', 'euidaccess', prefix: system_ext_define)
91     configh_data.set('HAVE_EUIDACCESS', 1)
92 endif
93 if cc.has_header_symbol('sys/mman.h', 'mmap')
94     configh_data.set('HAVE_MMAP', 1)
95 endif
96 if cc.has_header_symbol('stdlib.h', 'mkostemp', prefix: system_ext_define)
97     configh_data.set('HAVE_MKOSTEMP', 1)
98 endif
99 if cc.has_header_symbol('fcntl.h', 'posix_fallocate', prefix: system_ext_define)
100     configh_data.set('HAVE_POSIX_FALLOCATE', 1)
101 endif
102 if cc.has_header_symbol('string.h', 'strndup', prefix: system_ext_define)
103     configh_data.set('HAVE_STRNDUP', 1)
104 endif
105 if cc.has_header_symbol('stdio.h', 'asprintf', prefix: system_ext_define)
106     configh_data.set('HAVE_ASPRINTF', 1)
107 elif cc.has_header_symbol('stdio.h', 'vasprintf', prefix: system_ext_define)
108     configh_data.set('HAVE_VASPRINTF', 1)
109 endif
110 if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix: system_ext_define)
111     configh_data.set('HAVE_SECURE_GETENV', 1)
112 elif cc.has_header_symbol('stdlib.h', '__secure_getenv', prefix: system_ext_define)
113     configh_data.set('HAVE___SECURE_GETENV', 1)
114 else
115     message('C library does not support secure_getenv, using getenv instead')
116 endif
117 # Silence some security & deprecation warnings on MSVC
118 # for some unix/C functions we use.
119 # https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=vs-2019
120 configh_data.set('_CRT_SECURE_NO_WARNINGS', 1)
121 configh_data.set('_CRT_NONSTDC_NO_WARNINGS', 1)
122 configh_data.set('_CRT_NONSTDC_NO_DEPRECATE', 1)
123 # Reduce unnecessary includes on MSVC.
124 configh_data.set('WIN32_LEAN_AND_MEAN', 1)
125 configure_file(output: 'config.h', configuration: configh_data)
126
127
128 # Supports -Wl,--version-script?
129 have_version_script = cc.links(
130     'int main(){}',
131     args: '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon.map'),
132     name: '-Wl,--version-script',
133 )
134
135
136 # libxkbcommon.
137 # Note: we use some yacc extensions, which work with either GNU bison
138 # (preferred) or byacc (with backtracking enabled).
139 bison = find_program('bison', 'win_bison', required: false)
140 if bison.found()
141     yacc_gen = generator(
142         bison,
143         output: ['@BASENAME@.c', '@BASENAME@.h'],
144         arguments: ['@INPUT@', '--defines=@OUTPUT1@', '--output=@OUTPUT0@', '-p _xkbcommon_'],
145     )
146 else
147     byacc = find_program('byacc', required: false)
148     if byacc.found()
149         yacc_gen = generator(
150             byacc,
151             output: ['@BASENAME@.c', '@BASENAME@.h'],
152             arguments: ['@INPUT@', '-H', '@OUTPUT1@', '-o', '@OUTPUT0@', '-p _xkbcommon_'],
153         )
154     else
155         error('Could not find a compatible YACC program (bison or byacc)')
156     endif
157 endif
158 libxkbcommon_sources = [
159     'src/compose/parser.c',
160     'src/compose/parser.h',
161     'src/compose/paths.c',
162     'src/compose/paths.h',
163     'src/compose/state.c',
164     'src/compose/table.c',
165     'src/compose/table.h',
166     'src/xkbcomp/action.c',
167     'src/xkbcomp/action.h',
168     'src/xkbcomp/ast.h',
169     'src/xkbcomp/ast-build.c',
170     'src/xkbcomp/ast-build.h',
171     'src/xkbcomp/compat.c',
172     'src/xkbcomp/expr.c',
173     'src/xkbcomp/expr.h',
174     'src/xkbcomp/include.c',
175     'src/xkbcomp/include.h',
176     'src/xkbcomp/keycodes.c',
177     'src/xkbcomp/keymap.c',
178     'src/xkbcomp/keymap-dump.c',
179     'src/xkbcomp/keywords.c',
180     yacc_gen.process('src/xkbcomp/parser.y'),
181     'src/xkbcomp/parser-priv.h',
182     'src/xkbcomp/rules.c',
183     'src/xkbcomp/rules.h',
184     'src/xkbcomp/scanner.c',
185     'src/xkbcomp/symbols.c',
186     'src/xkbcomp/types.c',
187     'src/xkbcomp/vmod.c',
188     'src/xkbcomp/vmod.h',
189     'src/xkbcomp/xkbcomp.c',
190     'src/xkbcomp/xkbcomp-priv.h',
191     'src/atom.c',
192     'src/atom.h',
193     'src/context.c',
194     'src/context.h',
195     'src/context-priv.c',
196     'src/darray.h',
197     'src/keysym.c',
198     'src/keysym.h',
199     'src/keysym-utf.c',
200     'src/ks_tables.h',
201     'src/keymap.c',
202     'src/keymap.h',
203     'src/keymap-priv.c',
204     'src/scanner-utils.h',
205     'src/state.c',
206     'src/text.c',
207     'src/text.h',
208     'src/utf8.c',
209     'src/utf8.h',
210     'src/utils.c',
211     'src/utils.h',
212 ]
213 libxkbcommon_link_args = []
214 if have_version_script
215     libxkbcommon_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon.map')
216 endif
217 libxkbcommon = library(
218     'xkbcommon',
219     'xkbcommon/xkbcommon.h',
220     libxkbcommon_sources,
221     link_args: libxkbcommon_link_args,
222     link_depends: 'xkbcommon.map',
223     version: '0.0.0',
224     install: true,
225     include_directories: include_directories('src'),
226 )
227 install_headers(
228     'xkbcommon/xkbcommon.h',
229     'xkbcommon/xkbcommon-compat.h',
230     'xkbcommon/xkbcommon-compose.h',
231     'xkbcommon/xkbcommon-keysyms.h',
232     'xkbcommon/xkbcommon-names.h',
233     subdir: 'xkbcommon',
234 )
235 pkgconfig.generate(
236     name: 'xkbcommon',
237     filebase: 'xkbcommon',
238     libraries: libxkbcommon,
239     version: meson.project_version(),
240     description: 'XKB API common to servers and clients',
241 )
242
243
244 # libxkbcommon-x11.
245 if get_option('enable-x11')
246     xcb_dep = dependency('xcb', version: '>=1.10', required: false)
247     xcb_xkb_dep = dependency('xcb-xkb', version: '>=1.10', required: false)
248     if not xcb_dep.found() or not xcb_xkb_dep.found()
249         error('''X11 support requires xcb-xkb >= 1.10 which was not found.
250 You can disable X11 support with -Denable-x11=false.''')
251     endif
252
253     libxkbcommon_x11_sources = [
254         'src/x11/keymap.c',
255         'src/x11/state.c',
256         'src/x11/util.c',
257         'src/x11/x11-priv.h',
258         'src/context.h',
259         'src/context-priv.c',
260         'src/keymap.h',
261         'src/keymap-priv.c',
262         'src/atom.h',
263         'src/atom.c',
264     ]
265     libxkbcommon_x11_link_args = []
266     if have_version_script
267         libxkbcommon_x11_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon-x11.map')
268     endif
269     libxkbcommon_x11 = library(
270         'xkbcommon-x11',
271         'xkbcommon/xkbcommon-x11.h',
272         libxkbcommon_x11_sources,
273         link_args: libxkbcommon_x11_link_args,
274         link_depends: 'xkbcommon-x11.map',
275         version: '0.0.0',
276         install: true,
277         include_directories: include_directories('src'),
278         link_with: libxkbcommon,
279         dependencies: [
280             xcb_dep,
281             xcb_xkb_dep,
282         ],
283     )
284     install_headers(
285         'xkbcommon/xkbcommon-x11.h',
286         subdir: 'xkbcommon',
287     )
288     pkgconfig.generate(
289         name: 'xkbcommon-x11',
290         filebase: 'xkbcommon-x11',
291         libraries: libxkbcommon_x11,
292         version: meson.project_version(),
293         description: 'XKB API common to servers and clients - X11 support',
294         requires: ['xkbcommon'],
295         requires_private: ['xcb>=1.10', 'xcb-xkb>=1.10'],
296     )
297 endif
298
299
300 # Tests
301 test_env = environment()
302 test_env.set('XKB_LOG_LEVEL', 'debug')
303 test_env.set('XKB_LOG_VERBOSITY', '10')
304 test_env.set('top_srcdir', meson.source_root())
305 test_env.set('top_builddir', meson.build_root())
306
307 test_configh_data = configuration_data()
308 test_configh_data.set_quoted('TEST_XKB_CONFIG_ROOT', join_paths(meson.source_root(), 'test', 'data'))
309 configure_file(output: 'test-config.h', configuration: test_configh_data)
310
311 # Some tests need to use unexported symbols, so we link them against
312 # an internal copy of libxkbcommon with all symbols exposed.
313 libxkbcommon_test_internal = static_library(
314     'xkbcommon-test-internal',
315     'test/common.c',
316     'test/test.h',
317     'test/evdev-scancodes.h',
318     libxkbcommon_sources,
319     include_directories: include_directories('src'),
320 )
321 test_dep = declare_dependency(
322     include_directories: include_directories('src'),
323     link_with: libxkbcommon_test_internal,
324 )
325 if get_option('enable-x11')
326     libxkbcommon_x11_internal = static_library(
327         'xkbcommon-x11-internal',
328         libxkbcommon_x11_sources,
329         include_directories: include_directories('src'),
330         link_with: libxkbcommon_test_internal,
331         dependencies: [
332             xcb_dep,
333             xcb_xkb_dep,
334         ],
335     )
336     x11_test_dep = declare_dependency(
337         link_with: libxkbcommon_x11_internal,
338         dependencies: [
339             test_dep,
340             xcb_dep,
341             xcb_xkb_dep,
342         ],
343     )
344 endif
345 test(
346     'keysym',
347     executable('test-keysym', 'test/keysym.c', dependencies: test_dep),
348     env: test_env,
349 )
350 test(
351     'keymap',
352     executable('test-keymap', 'test/keymap.c', dependencies: test_dep),
353     env: test_env,
354 )
355 test(
356     'filecomp',
357     executable('test-filecomp', 'test/filecomp.c', dependencies: test_dep),
358     env: test_env,
359 )
360 # TODO: This test currently uses some functions that don't exist on Windows.
361 if cc.get_id() != 'msvc'
362   test(
363       'context',
364       executable('test-context', 'test/context.c', dependencies: test_dep),
365       env: test_env,
366   )
367 endif
368 test(
369     'rules-file',
370     executable('test-rules-file', 'test/rules-file.c', dependencies: test_dep),
371     env: test_env,
372 )
373 test(
374     'rules-file-includes',
375     executable('test-rules-file-includes', 'test/rules-file-includes.c', dependencies: test_dep),
376     env: test_env,
377 )
378 test(
379     'stringcomp',
380     executable('test-stringcomp', 'test/stringcomp.c', dependencies: test_dep),
381     env: test_env,
382 )
383 test(
384     'buffercomp',
385     executable('test-buffercomp', 'test/buffercomp.c', dependencies: test_dep),
386     env: test_env,
387 )
388 test(
389     'log',
390     executable('test-log', 'test/log.c', dependencies: test_dep),
391     env: test_env,
392 )
393 test(
394     'atom',
395     executable('test-atom', 'test/atom.c', dependencies: test_dep),
396     env: test_env,
397 )
398 test(
399     'utf8',
400     executable('test-utf8', 'test/utf8.c', dependencies: test_dep),
401     env: test_env,
402 )
403 test(
404     'state',
405     executable('test-state', 'test/state.c', dependencies: test_dep),
406     env: test_env,
407 )
408 test(
409     'keyseq',
410     executable('test-keyseq', 'test/keyseq.c', dependencies: test_dep),
411     env: test_env,
412 )
413 test(
414     'rulescomp',
415     executable('test-rulescomp', 'test/rulescomp.c', dependencies: test_dep),
416     env: test_env,
417 )
418 test(
419     'compose',
420     executable('test-compose', 'test/compose.c', dependencies: test_dep),
421     env: test_env,
422 )
423 test(
424     'symbols-leak-test',
425     find_program('test/symbols-leak-test.bash'),
426     env: test_env,
427 )
428 if get_option('enable-x11')
429     test(
430         'x11',
431         executable('test-x11', 'test/x11.c', dependencies: x11_test_dep),
432         env: test_env,
433     )
434     # test/x11comp is meant to be run, but it is (temporarily?) disabled.
435     # See: https://github.com/xkbcommon/libxkbcommon/issues/30
436     executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep)
437 endif
438
439
440 # Fuzzing target programs.
441 executable('fuzz-keymap', 'fuzz/keymap/target.c', dependencies: test_dep)
442 executable('fuzz-compose', 'fuzz/compose/target.c', dependencies: test_dep)
443
444
445 # Demo programs.
446 libxkbcommon_tools_internal = static_library(
447     'tools-internal',
448     'tools/tools-common.h',
449     'tools/tools-common.c',
450     libxkbcommon_sources,
451     include_directories: include_directories('src'),
452 )
453 tools_dep = declare_dependency(
454     include_directories: [include_directories('src'), include_directories('tools')],
455     link_with: libxkbcommon_tools_internal,
456 )
457
458 if cc.has_header_symbol('getopt.h', 'getopt_long', prefix: '#define _GNU_SOURCE')
459     executable('rmlvo-to-kccgst', 'tools/rmlvo-to-kccgst.c', dependencies: tools_dep)
460     executable('rmlvo-to-keymap', 'tools/rmlvo-to-keymap.c', dependencies: tools_dep)
461     executable('print-compiled-keymap', 'tools/print-compiled-keymap.c', dependencies: tools_dep)
462     executable('how-to-type', 'tools/how-to-type.c', dependencies: tools_dep)
463 endif
464 if cc.has_header('linux/input.h')
465     executable('interactive-evdev', 'test/interactive-evdev.c', dependencies: tools_dep)
466 endif
467 if get_option('enable-x11')
468     x11_tools_dep = declare_dependency(
469         link_with: libxkbcommon_x11_internal,
470         dependencies: [
471             tools_dep,
472             xcb_dep,
473             xcb_xkb_dep,
474         ],
475     )
476     executable('interactive-x11', 'test/interactive-x11.c', dependencies: x11_tools_dep)
477 endif
478 if get_option('enable-wayland')
479     wayland_client_dep = dependency('wayland-client', version: '>=1.2.0', required: false)
480     wayland_protocols_dep = dependency('wayland-protocols', version: '>=1.12', required: false)
481     wayland_scanner_dep = dependency('wayland-scanner', required: false, native: true)
482     if not wayland_client_dep.found() or not wayland_protocols_dep.found() or not wayland_scanner_dep.found()
483         error('''The Wayland demo programs require wayland-client >= 1.2.0, wayland-protocols >= 1.7 which were not found.
484 You can disable the Wayland demo programs with -Denable-wayland=false.''')
485     endif
486
487     wayland_scanner = find_program(wayland_scanner_dep.get_pkgconfig_variable('wayland_scanner'))
488     wayland_scanner_code_gen = generator(
489         wayland_scanner,
490         output: '@BASENAME@-protocol.c',
491         arguments: ['code', '@INPUT@', '@OUTPUT@'],
492     )
493     wayland_scanner_client_header_gen = generator(
494         wayland_scanner,
495         output: '@BASENAME@-client-protocol.h',
496         arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
497     )
498     wayland_protocols_datadir = wayland_protocols_dep.get_pkgconfig_variable('pkgdatadir')
499     xdg_shell_xml = join_paths(wayland_protocols_datadir, 'stable/xdg-shell/xdg-shell.xml')
500     xdg_shell_sources = [
501         wayland_scanner_code_gen.process(xdg_shell_xml),
502         wayland_scanner_client_header_gen.process(xdg_shell_xml),
503     ]
504     executable('interactive-wayland', 'test/interactive-wayland.c', xdg_shell_sources, dependencies: [test_dep, wayland_client_dep])
505 endif
506
507 # xkeyboard-config "verifier"
508 xkct_config = configuration_data()
509 xkct_config.set('MESON_BUILD_ROOT', meson.build_root())
510 xkct_config.set('XKB_CONFIG_ROOT', XKBCONFIGROOT)
511 configure_file(input: 'test/xkeyboard-config-test.py.in',
512                output: 'xkeyboard-config-test',
513                configuration: xkct_config,
514                install: false)
515
516
517 # Benchmarks.
518 libxkbcommon_bench_internal = static_library(
519     'xkbcommon-bench-internal',
520     'bench/bench.c',
521     'bench/bench.h',
522     link_with: libxkbcommon_test_internal,
523 )
524 bench_dep = declare_dependency(
525     include_directories: include_directories('src'),
526     link_with: libxkbcommon_bench_internal,
527 )
528 bench_env = environment()
529 bench_env.set('top_srcdir', meson.source_root())
530 benchmark(
531     'key-proc',
532     executable('bench-key-proc', 'bench/key-proc.c', dependencies: bench_dep),
533     env: bench_env,
534 )
535 benchmark(
536     'rules',
537     executable('bench-rules', 'bench/rules.c', dependencies: bench_dep),
538     env: bench_env,
539 )
540 benchmark(
541     'rulescomp',
542     executable('bench-rulescomp', 'bench/rulescomp.c', dependencies: bench_dep),
543     env: bench_env,
544 )
545 benchmark(
546     'compose',
547     executable('bench-compose', 'bench/compose.c', dependencies: bench_dep),
548     env: bench_env,
549 )
550
551
552 # Documentation.
553 if get_option('enable-docs')
554     doxygen = find_program('doxygen', required: false)
555     if not doxygen.found()
556         error('''Documentation requires doxygen which was not found.
557 You can disable the documentation with -Denable-docs=false.''')
558     endif
559     doxygen_wrapper = find_program('scripts/doxygen-wrapper')
560
561     doxygen_input = [
562         'README.md',
563         'doc/doxygen-extra.css',
564         'doc/quick-guide.md',
565         'doc/compat.md',
566         'doc/user-configuration.md',
567         'doc/rules-format.md',
568         'xkbcommon/xkbcommon.h',
569         'xkbcommon/xkbcommon-names.h',
570         'xkbcommon/xkbcommon-x11.h',
571         'xkbcommon/xkbcommon-compose.h',
572     ]
573     doxygen_data = configuration_data()
574     doxygen_data.set('PACKAGE_NAME', meson.project_name())
575     doxygen_data.set('PACKAGE_VERSION', meson.project_version())
576     doxygen_data.set('INPUT', ' '.join(doxygen_input))
577     doxygen_data.set('OUTPUT_DIRECTORY', meson.build_root())
578     doxyfile = configure_file(
579         input: 'doc/Doxyfile.in',
580         output: 'Doxyfile',
581         configuration: doxygen_data,
582     )
583     # TODO: Meson should provide this.
584     docdir = join_paths(get_option('datadir'), 'doc', meson.project_name())
585     custom_target(
586         'doc',
587         input: [doxyfile] + doxygen_input,
588         output: 'html',
589         command: [doxygen_wrapper, doxygen.path(), join_paths(meson.build_root(), 'Doxyfile'), meson.source_root()],
590         install: true,
591         install_dir: docdir,
592         build_by_default: true,
593     )
594 endif