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