build/meson: require meson >= 0.41.0
[platform/upstream/libxkbcommon.git] / meson.build
1 project(
2     'libxkbcommon',
3     'c',
4     version: '0.7.2',
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     '-Wextra',
20     '-Wno-unused-parameter',
21     '-Wno-missing-field-initializers',
22     '-Wpointer-arith',
23     '-Wmissing-declarations',
24     '-Wformat=2',
25     '-Wstrict-prototypes',
26     '-Wmissing-prototypes',
27     '-Wnested-externs',
28     '-Wbad-function-cast',
29     '-Wshadow',
30     '-Wlogical-op',
31     '-Wdate-time',
32     '-Wwrite-strings',
33 ]
34     if cc.has_argument(cflag)
35         add_project_arguments(cflag, language: 'c')
36     endif
37 endforeach
38
39
40 # The XKB config root.
41 XKBCONFIGROOT = get_option('xkb-config-root')
42 if XKBCONFIGROOT == ''
43     xkeyboard_config_dep = dependency('xkeyboard-config', required: false)
44     if xkeyboard_config_dep.found()
45         XKBCONFIGROOT = xkeyboard_config_dep.get_pkgconfig_variable('xkb_base')
46     else
47         XKBCONFIGROOT = join_paths(get_option('prefix'), get_option('datadir'), 'X11', 'xkb')
48   endif
49 endif
50
51
52 # The X locale directory for compose.
53 XLOCALEDIR = get_option('x-locale-root')
54 if XLOCALEDIR == ''
55     XLOCALEDIR = join_paths(get_option('prefix'), get_option('datadir'), 'X11', 'locale')
56 endif
57
58
59 # config.h.
60 configh_data = configuration_data()
61 configh_data.set('_GNU_SOURCE', 1)
62 configh_data.set_quoted('DFLT_XKB_CONFIG_ROOT', XKBCONFIGROOT)
63 configh_data.set_quoted('XLOCALEDIR', XLOCALEDIR)
64 configh_data.set_quoted('DEFAULT_XKB_RULES', get_option('default-rules'))
65 configh_data.set_quoted('DEFAULT_XKB_MODEL', get_option('default-model'))
66 configh_data.set_quoted('DEFAULT_XKB_LAYOUT', get_option('default-layout'))
67 if get_option('default-variant') != ''
68     configh_data.set_quoted('DEFAULT_XKB_VARIANT', get_option('default-variant'))
69 endif
70 if get_option('default-options') != ''
71     configh_data.set_quoted('DEFAULT_XKB_OPTIONS', get_option('default-options'))
72 endif
73 if cc.links('int main(){if(__builtin_expect(1<0,0)){}}', name: '__builtin_expect')
74     configh_data.set('HAVE___BUILTIN_EXPECT', 1)
75 endif
76 if cc.links('int main(){__builtin_popcount(1);}', name: '__builtin_popcount')
77     configh_data.set('HAVE___BUILTIN_POPCOUNT', 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     link_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_internal = static_library(
123     'xkbcommon-internal',
124     'src/compose/parser.c',
125     'src/compose/parser.h',
126     'src/compose/paths.c',
127     'src/compose/paths.h',
128     'src/compose/state.c',
129     'src/compose/table.c',
130     'src/compose/table.h',
131     'src/xkbcomp/action.c',
132     'src/xkbcomp/action.h',
133     'src/xkbcomp/ast.h',
134     'src/xkbcomp/ast-build.c',
135     'src/xkbcomp/ast-build.h',
136     'src/xkbcomp/compat.c',
137     'src/xkbcomp/expr.c',
138     'src/xkbcomp/expr.h',
139     'src/xkbcomp/include.c',
140     'src/xkbcomp/include.h',
141     'src/xkbcomp/keycodes.c',
142     'src/xkbcomp/keymap.c',
143     'src/xkbcomp/keymap-dump.c',
144     'src/xkbcomp/keywords.c',
145     yacc_gen.process('src/xkbcomp/parser.y'),
146     'src/xkbcomp/parser-priv.h',
147     'src/xkbcomp/rules.c',
148     'src/xkbcomp/rules.h',
149     'src/xkbcomp/scanner.c',
150     'src/xkbcomp/symbols.c',
151     'src/xkbcomp/types.c',
152     'src/xkbcomp/vmod.c',
153     'src/xkbcomp/vmod.h',
154     'src/xkbcomp/xkbcomp.c',
155     'src/xkbcomp/xkbcomp-priv.h',
156     'src/atom.c',
157     'src/atom.h',
158     'src/context.c',
159     'src/context.h',
160     'src/context-priv.c',
161     'src/darray.h',
162     'src/keysym.c',
163     'src/keysym.h',
164     'src/keysym-utf.c',
165     'src/ks_tables.h',
166     'src/keymap.c',
167     'src/keymap.h',
168     'src/keymap-priv.c',
169     'src/scanner-utils.h',
170     'src/state.c',
171     'src/text.c',
172     'src/text.h',
173     'src/utf8.c',
174     'src/utf8.h',
175     'src/utils.c',
176     'src/utils.h',
177     include_directories: include_directories('src'),
178 )
179 libxkbcommon_link_args = []
180 if have_version_script
181     libxkbcommon_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon.map')
182 endif
183 libxkbcommon = library(
184     'xkbcommon',
185     'xkbcommon/xkbcommon.h',
186     link_whole: libxkbcommon_internal,
187     link_args: libxkbcommon_link_args,
188     link_depends: 'xkbcommon.map',
189     version: '0.0.0',
190     install: true,
191 )
192 install_headers(
193     'xkbcommon/xkbcommon.h',
194     'xkbcommon/xkbcommon-compat.h',
195     'xkbcommon/xkbcommon-compose.h',
196     'xkbcommon/xkbcommon-keysyms.h',
197     'xkbcommon/xkbcommon-names.h',
198     subdir: 'xkbcommon',
199 )
200 pkgconfig.generate(
201     name: 'xkbcommon',
202     filebase: 'xkbcommon',
203     libraries: libxkbcommon,
204     version: meson.project_version(),
205     description: 'XKB API common to servers and clients',
206 )
207
208
209 # libxkbcommon-x11.
210 if get_option('enable-x11')
211     xcb_dep = dependency('xcb', version: '>=1.10', required: false)
212     xcb_xkb_dep = dependency('xcb-xkb', version: '>=1.10', required: false)
213     if not xcb_dep.found() or not xcb_xkb_dep.found()
214         error('''X11 support requires xcb-xkb >= 1.10 which was not found.
215 You can disable X11 support with -Denable-x11=false.''')
216     endif
217
218     libxkbcommon_x11_internal = static_library(
219         'xkbcommon-x11-internal',
220         'src/x11/keymap.c',
221         'src/x11/state.c',
222         'src/x11/util.c',
223         'src/x11/x11-priv.h',
224         'src/context.h',
225         'src/context-priv.c',
226         'src/keymap.h',
227         'src/keymap-priv.c',
228         'src/atom.h',
229         'src/atom.c',
230         include_directories: include_directories('src'),
231         link_with: libxkbcommon,
232         dependencies: [
233             xcb_dep,
234             xcb_xkb_dep,
235         ],
236     )
237     libxkbcommon_x11_link_args = []
238     if have_version_script
239         libxkbcommon_x11_link_args += '-Wl,--version-script=' + join_paths(meson.source_root(), 'xkbcommon-x11.map')
240     endif
241     libxkbcommon_x11 = library(
242         'xkbcommon-x11',
243         'xkbcommon/xkbcommon-x11.h',
244         link_whole: libxkbcommon_x11_internal,
245         link_args: libxkbcommon_x11_link_args,
246         link_depends: 'xkbcommon-x11.map',
247         version: '0.0.0',
248         install: true,
249     )
250     install_headers(
251         'xkbcommon/xkbcommon-x11.h',
252         subdir: 'xkbcommon',
253     )
254     pkgconfig.generate(
255         name: 'xkbcommon-x11',
256         filebase: 'xkbcommon-x11',
257         libraries: libxkbcommon_x11,
258         version: meson.project_version(),
259         description: 'XKB API common to servers and clients - X11 support',
260     )
261 endif
262
263
264 # Tests
265 test_env = environment()
266 test_env.set('XKB_LOG_LEVEL', 'debug')
267 test_env.set('XKB_LOG_VERBOSITY', '10')
268 test_env.set('top_srcdir', meson.source_root())
269 test_env.set('MALLOC_PERTURB_', '15')
270 test_env.set('MallocPreScribble', '1')
271 test_env.set('MallocScribble', '1')
272 # Some tests need to use unexported symbols, so we link them against
273 # the internal copy of libxkbcommon with all symbols exposed.
274 libxkbcommon_test_internal = static_library(
275     'xkbcommon-test-internal',
276     'test/common.c',
277     'test/test.h',
278     'test/evdev-scancodes.h',
279     include_directories: include_directories('src'),
280     link_with: libxkbcommon_internal,
281 )
282 test_dep = declare_dependency(
283     include_directories: include_directories('src'),
284     link_with: libxkbcommon_test_internal,
285 )
286 test(
287     'keysym',
288     executable('test-keysym', 'test/keysym.c', dependencies: test_dep),
289     env: test_env,
290 )
291 test(
292     'keymap',
293     executable('test-keymap', 'test/keymap.c', dependencies: test_dep),
294     env: test_env,
295 )
296 test(
297     'filecomp',
298     executable('test-filecomp', 'test/filecomp.c', dependencies: test_dep),
299     env: test_env,
300 )
301 test(
302     'context',
303     executable('test-context', 'test/context.c', dependencies: test_dep),
304     env: test_env,
305 )
306 test(
307     'rules-file',
308     executable('test-rules-file', 'test/rules-file.c', dependencies: test_dep),
309     env: test_env,
310 )
311 test(
312     'stringcomp',
313     executable('test-stringcomp', 'test/stringcomp.c', dependencies: test_dep),
314     env: test_env,
315 )
316 test(
317     'buffercomp',
318     executable('test-buffercomp', 'test/buffercomp.c', dependencies: test_dep),
319     env: test_env,
320 )
321 test(
322     'log',
323     executable('test-log', 'test/log.c', dependencies: test_dep),
324     env: test_env,
325 )
326 test(
327     'atom',
328     executable('test-atom', 'test/atom.c', dependencies: test_dep),
329     env: test_env,
330 )
331 test(
332     'utf8',
333     executable('test-utf8', 'test/utf8.c', dependencies: test_dep),
334     env: test_env,
335 )
336 test(
337     'state',
338     executable('test-state', 'test/state.c', dependencies: test_dep),
339     env: test_env,
340 )
341 test(
342     'keyseq',
343     executable('test-keyseq', 'test/keyseq.c', dependencies: test_dep),
344     env: test_env,
345 )
346 test(
347     'rulescomp',
348     executable('test-rulescomp', 'test/rulescomp.c', dependencies: test_dep),
349     env: test_env,
350 )
351 test(
352     'compose',
353     executable('test-compose', 'test/compose.c', dependencies: test_dep),
354     env: test_env,
355 )
356 test(
357     'symbols-leak-test',
358     find_program('test/symbols-leak-test.bash'),
359     env: test_env,
360 )
361 if get_option('enable-x11')
362     test(
363         'x11',
364         executable('test-x11', 'test/x11.c', dependencies: test_dep, link_with: libxkbcommon_x11_internal),
365         env: test_env,
366     )
367     # test/x11comp is meant to be run, but it is (temporarily?) disabled.
368     # See: https://github.com/xkbcommon/libxkbcommon/issues/30
369     executable('test-x11comp', 'test/x11comp.c', dependencies: test_dep, link_with: libxkbcommon_x11_internal)
370 endif
371
372
373 # Demo programs.
374 executable('rmlvo-to-kccgst', 'test/rmlvo-to-kccgst.c', dependencies: test_dep)
375 executable('print-compiled-keymap', 'test/print-compiled-keymap.c', dependencies: test_dep)
376 if cc.has_header('linux/input.h')
377     executable('interactive-evdev', 'test/interactive-evdev.c', dependencies: test_dep)
378 endif
379 if get_option('enable-x11')
380     executable('interactive-x11', 'test/interactive-x11.c', dependencies: test_dep, link_with: libxkbcommon_x11_internal)
381 endif
382 if get_option('enable-wayland')
383     wayland_client_dep = dependency('wayland-client', version: '>=1.2.0', required: false)
384     wayland_protocols_dep = dependency('wayland-protocols', version: '>=1.7', required: false)
385     wayland_scanner_dep = dependency('wayland-scanner', required: false)
386     if not wayland_client_dep.found() or not wayland_protocols_dep.found() or not wayland_scanner_dep.found()
387         error('''The Wayland demo programs require wayland-client >= 1.2.0, wayland-protocols >= 1.7 which were not found.
388 You can disable the Wayland demo programs with -Denable-wayland=false.''')
389     endif
390
391     wayland_scanner = find_program(wayland_scanner_dep.get_pkgconfig_variable('wayland_scanner'))
392     wayland_scanner_code_gen = generator(
393         wayland_scanner,
394         output: '@BASENAME@-protocol.c',
395         arguments: ['code', '@INPUT@', '@OUTPUT@'],
396     )
397     wayland_scanner_client_header_gen = generator(
398         wayland_scanner,
399         output: '@BASENAME@-client-protocol.h',
400         arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
401     )
402     wayland_protocols_datadir = wayland_protocols_dep.get_pkgconfig_variable('pkgdatadir')
403     xdg_shell_xml = join_paths(wayland_protocols_datadir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml')
404     xdg_shell_sources = [
405         wayland_scanner_code_gen.process(xdg_shell_xml),
406         wayland_scanner_client_header_gen.process(xdg_shell_xml),
407     ]
408     executable('interactive-wayland', 'test/interactive-wayland.c', xdg_shell_sources, dependencies: [test_dep, wayland_client_dep])
409 endif
410
411
412 # Benchmarks.
413 # For clock_gettime, on some systems.
414 rt_dep = cc.find_library('rt', required: false)
415 libxkbcommon_bench_internal = static_library(
416     'xkbcommon-bench-internal',
417     'bench/bench.c',
418     'bench/bench.h',
419     dependencies: rt_dep,
420     link_with: libxkbcommon_test_internal,
421 )
422 bench_dep = declare_dependency(
423     include_directories: include_directories('src'),
424     link_with: libxkbcommon_bench_internal,
425 )
426 bench_env = environment()
427 bench_env.set('top_srcdir', meson.source_root())
428 benchmark(
429     'key-proc',
430     executable('bench-key-proc', 'bench/key-proc.c', dependencies: bench_dep),
431     env: bench_env,
432 )
433 benchmark(
434     'rules',
435     executable('bench-rules', 'bench/rules.c', dependencies: bench_dep),
436     env: bench_env,
437 )
438 benchmark(
439     'rulescomp',
440     executable('bench-rulescomp', 'bench/rulescomp.c', dependencies: bench_dep),
441     env: bench_env,
442 )
443 benchmark(
444     'compose',
445     executable('bench-compose', 'bench/compose.c', dependencies: bench_dep),
446     env: bench_env,
447 )
448
449
450 # Documentation.
451 if get_option('enable-docs')
452     doxygen = find_program('doxygen', required: false)
453     if not doxygen.found()
454         error('''Documentation requires doxygen which was not found.
455 You can disable the documentation with -Denable-docs=false.''')
456     endif
457     doxygen_wrapper = find_program('scripts/doxygen-wrapper')
458
459     doxygen_input = [
460         'README.md',
461         'doc/doxygen-extra.css',
462         'doc/quick-guide.md',
463         'doc/compat.md',
464         'xkbcommon/xkbcommon.h',
465         'xkbcommon/xkbcommon-names.h',
466         'xkbcommon/xkbcommon-x11.h',
467         'xkbcommon/xkbcommon-compose.h',
468     ]
469     doxygen_data = configuration_data()
470     doxygen_data.set('PACKAGE_NAME', meson.project_name())
471     doxygen_data.set('PACKAGE_VERSION', meson.project_version())
472     doxygen_data.set('INPUT', ' '.join(doxygen_input))
473     doxygen_data.set('OUTPUT_DIRECTORY', meson.build_root())
474     doxyfile = configure_file(
475         input: 'doc/Doxyfile.in',
476         output: 'Doxyfile',
477         configuration: doxygen_data,
478     )
479     # TODO: Meson should provide this.
480     docdir = join_paths(get_option('datadir'), 'doc', meson.project_name())
481     custom_target(
482         'doc',
483         input: [doxyfile] + doxygen_input,
484         output: 'html',
485         command: [doxygen_wrapper, doxygen.path(), join_paths(meson.build_root(), 'Doxyfile'), meson.source_root()],
486         install: true,
487         install_dir: docdir,
488         build_by_default: true,
489     )
490 endif