Use bitwise test instead of popcount to check if one bit is set
[platform/upstream/libxkbcommon.git] / meson.build
1 project(
2     'libxkbcommon',
3     'c',
4     version: '0.8.4',
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 configh_data.set('_GNU_SOURCE', 1)
65 configh_data.set_quoted('DFLT_XKB_CONFIG_ROOT', XKBCONFIGROOT)
66 configh_data.set_quoted('XLOCALEDIR', XLOCALEDIR)
67 configh_data.set_quoted('DEFAULT_XKB_RULES', get_option('default-rules'))
68 configh_data.set_quoted('DEFAULT_XKB_MODEL', get_option('default-model'))
69 configh_data.set_quoted('DEFAULT_XKB_LAYOUT', get_option('default-layout'))
70 if get_option('default-variant') != ''
71     configh_data.set_quoted('DEFAULT_XKB_VARIANT', get_option('default-variant'))
72 endif
73 if get_option('default-options') != ''
74     configh_data.set_quoted('DEFAULT_XKB_OPTIONS', get_option('default-options'))
75 endif
76 if cc.links('int main(){if(__builtin_expect(1<0,0)){}}', name: '__builtin_expect')
77     configh_data.set('HAVE___BUILTIN_EXPECT', 1)
78 endif
79 if cc.has_header_symbol('unistd.h', 'eaccess', prefix: '#define _GNU_SOURCE')
80     configh_data.set('HAVE_EACCESS', 1)
81 endif
82 if cc.has_header_symbol('unistd.h', 'euidaccess', prefix: '#define _GNU_SOURCE')
83     configh_data.set('HAVE_EUIDACCESS', 1)
84 endif
85 if cc.has_header_symbol('sys/mman.h', 'mmap')
86     configh_data.set('HAVE_MMAP', 1)
87 endif
88 if cc.has_header_symbol('stdlib.h', 'mkostemp', prefix: '#define _GNU_SOURCE')
89     configh_data.set('HAVE_MKOSTEMP', 1)
90 endif
91 if cc.has_header_symbol('fcntl.h', 'posix_fallocate', prefix: '#define _GNU_SOURCE')
92     configh_data.set('HAVE_POSIX_FALLOCATE', 1)
93 endif
94 if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix: '#define _GNU_SOURCE')
95     configh_data.set('HAVE_SECURE_GETENV', 1)
96 elif cc.has_header_symbol('stdlib.h', '__secure_getenv', prefix: '#define _GNU_SOURCE')
97     configh_data.set('HAVE___SECURE_GETENV', 1)
98 else
99     message('C library does not support secure_getenv, using getenv instead')
100 endif
101 configure_file(output: 'config.h', configuration: configh_data)
102 add_project_arguments('-include', 'config.h', language: 'c')
103
104
105 # Supports -Wl,--version-script?
106 have_version_script = cc.links(
107     'int main(){}',
108     args: '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon.map'),
109     name: '-Wl,--version-script',
110 )
111
112
113 # libxkbcommon.
114 # Note: we use some yacc extensions, which work with either GNU bison
115 # (preferred) or byacc. Other yacc's may or may not work.
116 yacc = find_program('bison', 'byacc')
117 yacc_gen = generator(
118     yacc,
119     output: ['@BASENAME@.c', '@BASENAME@.h'],
120     arguments: ['@INPUT@', '--defines=@OUTPUT1@', '--output=@OUTPUT0@', '-p _xkbcommon_'],
121 )
122 libxkbcommon_sources = [
123     'src/compose/parser.c',
124     'src/compose/parser.h',
125     'src/compose/paths.c',
126     'src/compose/paths.h',
127     'src/compose/state.c',
128     'src/compose/table.c',
129     'src/compose/table.h',
130     'src/xkbcomp/action.c',
131     'src/xkbcomp/action.h',
132     'src/xkbcomp/ast.h',
133     'src/xkbcomp/ast-build.c',
134     'src/xkbcomp/ast-build.h',
135     'src/xkbcomp/compat.c',
136     'src/xkbcomp/expr.c',
137     'src/xkbcomp/expr.h',
138     'src/xkbcomp/include.c',
139     'src/xkbcomp/include.h',
140     'src/xkbcomp/keycodes.c',
141     'src/xkbcomp/keymap.c',
142     'src/xkbcomp/keymap-dump.c',
143     'src/xkbcomp/keywords.c',
144     yacc_gen.process('src/xkbcomp/parser.y'),
145     'src/xkbcomp/parser-priv.h',
146     'src/xkbcomp/rules.c',
147     'src/xkbcomp/rules.h',
148     'src/xkbcomp/scanner.c',
149     'src/xkbcomp/symbols.c',
150     'src/xkbcomp/types.c',
151     'src/xkbcomp/vmod.c',
152     'src/xkbcomp/vmod.h',
153     'src/xkbcomp/xkbcomp.c',
154     'src/xkbcomp/xkbcomp-priv.h',
155     'src/atom.c',
156     'src/atom.h',
157     'src/context.c',
158     'src/context.h',
159     'src/context-priv.c',
160     'src/darray.h',
161     'src/keysym.c',
162     'src/keysym.h',
163     'src/keysym-utf.c',
164     'src/ks_tables.h',
165     'src/keymap.c',
166     'src/keymap.h',
167     'src/keymap-priv.c',
168     'src/scanner-utils.h',
169     'src/state.c',
170     'src/text.c',
171     'src/text.h',
172     'src/utf8.c',
173     'src/utf8.h',
174     'src/utils.c',
175     'src/utils.h',
176 ]
177 libxkbcommon_link_args = []
178 if have_version_script
179     libxkbcommon_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon.map')
180 endif
181 libxkbcommon = library(
182     'xkbcommon',
183     'xkbcommon/xkbcommon.h',
184     libxkbcommon_sources,
185     link_args: libxkbcommon_link_args,
186     link_depends: 'xkbcommon.map',
187     version: '0.0.0',
188     install: true,
189     include_directories: include_directories('src'),
190 )
191 install_headers(
192     'xkbcommon/xkbcommon.h',
193     'xkbcommon/xkbcommon-compat.h',
194     'xkbcommon/xkbcommon-compose.h',
195     'xkbcommon/xkbcommon-keysyms.h',
196     'xkbcommon/xkbcommon-names.h',
197     subdir: 'xkbcommon',
198 )
199 pkgconfig.generate(
200     name: 'xkbcommon',
201     filebase: 'xkbcommon',
202     libraries: libxkbcommon,
203     version: meson.project_version(),
204     description: 'XKB API common to servers and clients',
205 )
206
207
208 # libxkbcommon-x11.
209 if get_option('enable-x11')
210     xcb_dep = dependency('xcb', version: '>=1.10', required: false)
211     xcb_xkb_dep = dependency('xcb-xkb', version: '>=1.10', required: false)
212     if not xcb_dep.found() or not xcb_xkb_dep.found()
213         error('''X11 support requires xcb-xkb >= 1.10 which was not found.
214 You can disable X11 support with -Denable-x11=false.''')
215     endif
216
217     libxkbcommon_x11_sources = [
218         'src/x11/keymap.c',
219         'src/x11/state.c',
220         'src/x11/util.c',
221         'src/x11/x11-priv.h',
222         'src/context.h',
223         'src/context-priv.c',
224         'src/keymap.h',
225         'src/keymap-priv.c',
226         'src/atom.h',
227         'src/atom.c',
228     ]
229     libxkbcommon_x11_internal = static_library(
230         'xkbcommon-x11-internal',
231         libxkbcommon_x11_sources,
232         include_directories: include_directories('src'),
233         link_with: libxkbcommon,
234         dependencies: [
235             xcb_dep,
236             xcb_xkb_dep,
237         ],
238     )
239     libxkbcommon_x11_link_args = []
240     if have_version_script
241         libxkbcommon_x11_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon-x11.map')
242     endif
243     libxkbcommon_x11 = library(
244         'xkbcommon-x11',
245         'xkbcommon/xkbcommon-x11.h',
246         libxkbcommon_x11_sources,
247         link_args: libxkbcommon_x11_link_args,
248         link_depends: 'xkbcommon-x11.map',
249         version: '0.0.0',
250         install: true,
251         include_directories: include_directories('src'),
252         link_with: libxkbcommon,
253         dependencies: [
254             xcb_dep,
255             xcb_xkb_dep,
256         ],
257     )
258     install_headers(
259         'xkbcommon/xkbcommon-x11.h',
260         subdir: 'xkbcommon',
261     )
262     pkgconfig.generate(
263         name: 'xkbcommon-x11',
264         filebase: 'xkbcommon-x11',
265         libraries: libxkbcommon_x11,
266         version: meson.project_version(),
267         description: 'XKB API common to servers and clients - X11 support',
268         requires: ['xkbcommon'],
269         requires_private: ['xcb>=1.10', 'xcb-xkb>=1.10'],
270     )
271 endif
272
273
274 # Tests
275 test_env = environment()
276 test_env.set('XKB_LOG_LEVEL', 'debug')
277 test_env.set('XKB_LOG_VERBOSITY', '10')
278 test_env.set('top_srcdir', meson.source_root())
279 test_env.set('MALLOC_PERTURB_', '15')
280 test_env.set('MallocPreScribble', '1')
281 test_env.set('MallocScribble', '1')
282 # Some tests need to use unexported symbols, so we link them against
283 # an internal copy of libxkbcommon with all symbols exposed.
284 libxkbcommon_test_internal = static_library(
285     'xkbcommon-test-internal',
286     'test/common.c',
287     'test/test.h',
288     'test/evdev-scancodes.h',
289     libxkbcommon_sources,
290     include_directories: include_directories('src'),
291 )
292 test_dep = declare_dependency(
293     include_directories: include_directories('src'),
294     link_with: libxkbcommon_test_internal,
295 )
296 if get_option('enable-x11')
297     x11_test_dep = declare_dependency(
298         link_with: libxkbcommon_x11_internal,
299         dependencies: [
300             test_dep,
301             xcb_dep,
302             xcb_xkb_dep,
303         ],
304     )
305 endif
306 test(
307     'keysym',
308     executable('test-keysym', 'test/keysym.c', dependencies: test_dep),
309     env: test_env,
310 )
311 test(
312     'keymap',
313     executable('test-keymap', 'test/keymap.c', dependencies: test_dep),
314     env: test_env,
315 )
316 test(
317     'filecomp',
318     executable('test-filecomp', 'test/filecomp.c', dependencies: test_dep),
319     env: test_env,
320 )
321 test(
322     'context',
323     executable('test-context', 'test/context.c', dependencies: test_dep),
324     env: test_env,
325 )
326 test(
327     'rules-file',
328     executable('test-rules-file', 'test/rules-file.c', dependencies: test_dep),
329     env: test_env,
330 )
331 test(
332     'stringcomp',
333     executable('test-stringcomp', 'test/stringcomp.c', dependencies: test_dep),
334     env: test_env,
335 )
336 test(
337     'buffercomp',
338     executable('test-buffercomp', 'test/buffercomp.c', dependencies: test_dep),
339     env: test_env,
340 )
341 test(
342     'log',
343     executable('test-log', 'test/log.c', dependencies: test_dep),
344     env: test_env,
345 )
346 test(
347     'atom',
348     executable('test-atom', 'test/atom.c', dependencies: test_dep),
349     env: test_env,
350 )
351 test(
352     'utf8',
353     executable('test-utf8', 'test/utf8.c', dependencies: test_dep),
354     env: test_env,
355 )
356 test(
357     'state',
358     executable('test-state', 'test/state.c', dependencies: test_dep),
359     env: test_env,
360 )
361 test(
362     'keyseq',
363     executable('test-keyseq', 'test/keyseq.c', dependencies: test_dep),
364     env: test_env,
365 )
366 test(
367     'rulescomp',
368     executable('test-rulescomp', 'test/rulescomp.c', dependencies: test_dep),
369     env: test_env,
370 )
371 test(
372     'compose',
373     executable('test-compose', 'test/compose.c', dependencies: test_dep),
374     env: test_env,
375 )
376 test(
377     'symbols-leak-test',
378     find_program('test/symbols-leak-test.bash'),
379     env: test_env,
380 )
381 if get_option('enable-x11')
382     test(
383         'x11',
384         executable('test-x11', 'test/x11.c', dependencies: x11_test_dep),
385         env: test_env,
386     )
387     # test/x11comp is meant to be run, but it is (temporarily?) disabled.
388     # See: https://github.com/xkbcommon/libxkbcommon/issues/30
389     executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep)
390 endif
391
392
393 # Fuzzing target programs.
394 executable('fuzz-keymap', 'fuzz/keymap/target.c', dependencies: test_dep)
395 executable('fuzz-compose', 'fuzz/compose/target.c', dependencies: test_dep)
396
397
398 # Demo programs.
399 executable('rmlvo-to-kccgst', 'test/rmlvo-to-kccgst.c', dependencies: test_dep)
400 executable('rmlvo-to-keymap', 'test/rmlvo-to-keymap.c', dependencies: test_dep)
401 executable('print-compiled-keymap', 'test/print-compiled-keymap.c', dependencies: test_dep)
402 if cc.has_header('linux/input.h')
403     executable('interactive-evdev', 'test/interactive-evdev.c', dependencies: test_dep)
404 endif
405 if get_option('enable-x11')
406     executable('interactive-x11', 'test/interactive-x11.c', dependencies: x11_test_dep)
407 endif
408 if get_option('enable-wayland')
409     wayland_client_dep = dependency('wayland-client', version: '>=1.2.0', required: false)
410     wayland_protocols_dep = dependency('wayland-protocols', version: '>=1.7', required: false)
411     wayland_scanner_dep = dependency('wayland-scanner', required: false, native: true)
412     if not wayland_client_dep.found() or not wayland_protocols_dep.found() or not wayland_scanner_dep.found()
413         error('''The Wayland demo programs require wayland-client >= 1.2.0, wayland-protocols >= 1.7 which were not found.
414 You can disable the Wayland demo programs with -Denable-wayland=false.''')
415     endif
416
417     wayland_scanner = find_program(wayland_scanner_dep.get_pkgconfig_variable('wayland_scanner'))
418     wayland_scanner_code_gen = generator(
419         wayland_scanner,
420         output: '@BASENAME@-protocol.c',
421         arguments: ['code', '@INPUT@', '@OUTPUT@'],
422     )
423     wayland_scanner_client_header_gen = generator(
424         wayland_scanner,
425         output: '@BASENAME@-client-protocol.h',
426         arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
427     )
428     wayland_protocols_datadir = wayland_protocols_dep.get_pkgconfig_variable('pkgdatadir')
429     xdg_shell_xml = join_paths(wayland_protocols_datadir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml')
430     xdg_shell_sources = [
431         wayland_scanner_code_gen.process(xdg_shell_xml),
432         wayland_scanner_client_header_gen.process(xdg_shell_xml),
433     ]
434     executable('interactive-wayland', 'test/interactive-wayland.c', xdg_shell_sources, dependencies: [test_dep, wayland_client_dep])
435 endif
436
437 # xkeyboard-config "verifier"
438 xkct_config = configuration_data()
439 xkct_config.set('MESON_BUILD_ROOT', meson.build_root())
440 xkct_config.set('XKB_CONFIG_ROOT', XKBCONFIGROOT)
441 configure_file(input: 'test/xkeyboard-config-test.py.in',
442                output: 'xkeyboard-config-test',
443                configuration: xkct_config,
444                install: false)
445
446
447 # Benchmarks.
448 libxkbcommon_bench_internal = static_library(
449     'xkbcommon-bench-internal',
450     'bench/bench.c',
451     'bench/bench.h',
452     link_with: libxkbcommon_test_internal,
453 )
454 bench_dep = declare_dependency(
455     include_directories: include_directories('src'),
456     link_with: libxkbcommon_bench_internal,
457 )
458 bench_env = environment()
459 bench_env.set('top_srcdir', meson.source_root())
460 benchmark(
461     'key-proc',
462     executable('bench-key-proc', 'bench/key-proc.c', dependencies: bench_dep),
463     env: bench_env,
464 )
465 benchmark(
466     'rules',
467     executable('bench-rules', 'bench/rules.c', dependencies: bench_dep),
468     env: bench_env,
469 )
470 benchmark(
471     'rulescomp',
472     executable('bench-rulescomp', 'bench/rulescomp.c', dependencies: bench_dep),
473     env: bench_env,
474 )
475 benchmark(
476     'compose',
477     executable('bench-compose', 'bench/compose.c', dependencies: bench_dep),
478     env: bench_env,
479 )
480
481
482 # Documentation.
483 if get_option('enable-docs')
484     doxygen = find_program('doxygen', required: false)
485     if not doxygen.found()
486         error('''Documentation requires doxygen which was not found.
487 You can disable the documentation with -Denable-docs=false.''')
488     endif
489     doxygen_wrapper = find_program('scripts/doxygen-wrapper')
490
491     doxygen_input = [
492         'README.md',
493         'doc/doxygen-extra.css',
494         'doc/quick-guide.md',
495         'doc/compat.md',
496         'xkbcommon/xkbcommon.h',
497         'xkbcommon/xkbcommon-names.h',
498         'xkbcommon/xkbcommon-x11.h',
499         'xkbcommon/xkbcommon-compose.h',
500     ]
501     doxygen_data = configuration_data()
502     doxygen_data.set('PACKAGE_NAME', meson.project_name())
503     doxygen_data.set('PACKAGE_VERSION', meson.project_version())
504     doxygen_data.set('INPUT', ' '.join(doxygen_input))
505     doxygen_data.set('OUTPUT_DIRECTORY', meson.build_root())
506     doxyfile = configure_file(
507         input: 'doc/Doxyfile.in',
508         output: 'Doxyfile',
509         configuration: doxygen_data,
510     )
511     # TODO: Meson should provide this.
512     docdir = join_paths(get_option('datadir'), 'doc', meson.project_name())
513     custom_target(
514         'doc',
515         input: [doxyfile] + doxygen_input,
516         output: 'html',
517         command: [doxygen_wrapper, doxygen.path(), join_paths(meson.build_root(), 'Doxyfile'), meson.source_root()],
518         install: true,
519         install_dir: docdir,
520         build_by_default: true,
521     )
522 endif