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