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