xref: /openbmc/qemu/meson.build (revision 33ef9cdc)
1project('qemu', ['c'], meson_version: '>=1.1.0',
2        default_options: ['warning_level=1', 'c_std=gnu11', 'cpp_std=gnu++11', 'b_colorout=auto',
3                          'b_staticpic=false', 'stdsplit=false', 'optimization=2', 'b_pie=true'],
4        version: files('VERSION'))
5
6add_test_setup('quick', exclude_suites: ['slow', 'thorough'], is_default: true)
7add_test_setup('slow', exclude_suites: ['thorough'], env: ['G_TEST_SLOW=1', 'SPEED=slow'])
8add_test_setup('thorough', env: ['G_TEST_SLOW=1', 'SPEED=thorough'])
9
10meson.add_postconf_script(find_program('scripts/symlink-install-tree.py'))
11
12####################
13# Global variables #
14####################
15
16not_found = dependency('', required: false)
17keyval = import('keyval')
18ss = import('sourceset')
19fs = import('fs')
20
21host_os = host_machine.system()
22config_host = keyval.load(meson.current_build_dir() / 'config-host.mak')
23
24# Temporary directory used for files created while
25# configure runs. Since it is in the build directory
26# we can safely blow away any previous version of it
27# (and we need not jump through hoops to try to delete
28# it when configure exits.)
29tmpdir = meson.current_build_dir() / 'meson-private/temp'
30
31if get_option('qemu_suffix').startswith('/')
32  error('qemu_suffix cannot start with a /')
33endif
34
35qemu_confdir = get_option('sysconfdir') / get_option('qemu_suffix')
36qemu_datadir = get_option('datadir') / get_option('qemu_suffix')
37qemu_docdir = get_option('docdir') / get_option('qemu_suffix')
38qemu_moddir = get_option('libdir') / get_option('qemu_suffix')
39
40qemu_desktopdir = get_option('datadir') / 'applications'
41qemu_icondir = get_option('datadir') / 'icons'
42
43genh = []
44qapi_trace_events = []
45
46bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin']
47supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
48supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
49  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64']
50
51cpu = host_machine.cpu_family()
52
53target_dirs = config_host['TARGET_DIRS'].split()
54
55############
56# Programs #
57############
58
59sh = find_program('sh')
60python = import('python').find_installation()
61
62cc = meson.get_compiler('c')
63all_languages = ['c']
64if host_os == 'windows' and add_languages('cpp', required: false, native: false)
65  all_languages += ['cpp']
66  cxx = meson.get_compiler('cpp')
67endif
68if host_os == 'darwin' and \
69   add_languages('objc', required: true, native: false)
70  all_languages += ['objc']
71  objc = meson.get_compiler('objc')
72endif
73
74dtrace = not_found
75stap = not_found
76if 'dtrace' in get_option('trace_backends')
77  dtrace = find_program('dtrace', required: true)
78  stap = find_program('stap', required: false)
79  if stap.found()
80    # Workaround to avoid dtrace(1) producing a file with 'hidden' symbol
81    # visibility. Define STAP_SDT_V2 to produce 'default' symbol visibility
82    # instead. QEMU --enable-modules depends on this because the SystemTap
83    # semaphores are linked into the main binary and not the module's shared
84    # object.
85    add_global_arguments('-DSTAP_SDT_V2',
86                         native: false, language: all_languages)
87  endif
88endif
89
90if get_option('iasl') == ''
91  iasl = find_program('iasl', required: false)
92else
93  iasl = find_program(get_option('iasl'), required: true)
94endif
95
96edk2_targets = [ 'arm-softmmu', 'aarch64-softmmu', 'i386-softmmu', 'x86_64-softmmu', 'riscv64-softmmu' ]
97unpack_edk2_blobs = false
98foreach target : edk2_targets
99  if target in target_dirs
100    bzip2 = find_program('bzip2', required: get_option('install_blobs'))
101    unpack_edk2_blobs = bzip2.found()
102    break
103  endif
104endforeach
105
106#####################
107# Option validation #
108#####################
109
110# Fuzzing
111if get_option('fuzzing') and get_option('fuzzing_engine') == '' and \
112    not cc.links('''
113          #include <stdint.h>
114          #include <sys/types.h>
115          int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
116          int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { return 0; }
117        ''',
118        args: ['-Werror', '-fsanitize=fuzzer'])
119  error('Your compiler does not support -fsanitize=fuzzer')
120endif
121
122# Tracing backends
123if 'ftrace' in get_option('trace_backends') and host_os != 'linux'
124  error('ftrace is supported only on Linux')
125endif
126if 'syslog' in get_option('trace_backends') and not cc.compiles('''
127    #include <syslog.h>
128    int main(void) {
129        openlog("qemu", LOG_PID, LOG_DAEMON);
130        syslog(LOG_INFO, "configure");
131        return 0;
132    }''')
133  error('syslog is not supported on this system')
134endif
135
136# Miscellaneous Linux-only features
137get_option('mpath') \
138  .require(host_os == 'linux', error_message: 'Multipath is supported only on Linux')
139
140multiprocess_allowed = get_option('multiprocess') \
141  .require(host_os == 'linux', error_message: 'Multiprocess QEMU is supported only on Linux') \
142  .allowed()
143
144vfio_user_server_allowed = get_option('vfio_user_server') \
145  .require(host_os == 'linux', error_message: 'vfio-user server is supported only on Linux') \
146  .allowed()
147
148have_tpm = get_option('tpm') \
149  .require(host_os != 'windows', error_message: 'TPM emulation only available on POSIX systems') \
150  .allowed()
151
152# vhost
153have_vhost_user = get_option('vhost_user') \
154  .disable_auto_if(host_os != 'linux') \
155  .require(host_os != 'windows',
156           error_message: 'vhost-user is not available on Windows').allowed()
157have_vhost_vdpa = get_option('vhost_vdpa') \
158  .require(host_os == 'linux',
159           error_message: 'vhost-vdpa is only available on Linux').allowed()
160have_vhost_kernel = get_option('vhost_kernel') \
161  .require(host_os == 'linux',
162           error_message: 'vhost-kernel is only available on Linux').allowed()
163have_vhost_user_crypto = get_option('vhost_crypto') \
164  .require(have_vhost_user,
165           error_message: 'vhost-crypto requires vhost-user to be enabled').allowed()
166
167have_vhost = have_vhost_user or have_vhost_vdpa or have_vhost_kernel
168
169have_vhost_net_user = have_vhost_user and get_option('vhost_net').allowed()
170have_vhost_net_vdpa = have_vhost_vdpa and get_option('vhost_net').allowed()
171have_vhost_net_kernel = have_vhost_kernel and get_option('vhost_net').allowed()
172have_vhost_net = have_vhost_net_kernel or have_vhost_net_user or have_vhost_net_vdpa
173
174# type of binaries to build
175have_linux_user = false
176have_bsd_user = false
177have_system = false
178foreach target : target_dirs
179  have_linux_user = have_linux_user or target.endswith('linux-user')
180  have_bsd_user = have_bsd_user or target.endswith('bsd-user')
181  have_system = have_system or target.endswith('-softmmu')
182endforeach
183have_user = have_linux_user or have_bsd_user
184
185have_tools = get_option('tools') \
186  .disable_auto_if(not have_system) \
187  .allowed()
188have_ga = get_option('guest_agent') \
189  .disable_auto_if(not have_system and not have_tools) \
190  .require(host_os in ['sunos', 'linux', 'windows', 'freebsd', 'netbsd', 'openbsd'],
191           error_message: 'unsupported OS for QEMU guest agent') \
192  .allowed()
193have_block = have_system or have_tools
194
195enable_modules = get_option('modules') \
196  .require(host_os != 'windows',
197           error_message: 'Modules are not available for Windows') \
198  .require(not get_option('prefer_static'),
199           error_message: 'Modules are incompatible with static linking') \
200  .allowed()
201
202#######################################
203# Variables for host and accelerators #
204#######################################
205
206if cpu not in supported_cpus
207  host_arch = 'unknown'
208elif cpu == 'x86'
209  host_arch = 'i386'
210elif cpu == 'mips64'
211  host_arch = 'mips'
212elif cpu in ['riscv32', 'riscv64']
213  host_arch = 'riscv'
214else
215  host_arch = cpu
216endif
217
218if cpu in ['x86', 'x86_64']
219  kvm_targets = ['i386-softmmu', 'x86_64-softmmu']
220elif cpu == 'aarch64'
221  kvm_targets = ['aarch64-softmmu']
222elif cpu == 's390x'
223  kvm_targets = ['s390x-softmmu']
224elif cpu in ['ppc', 'ppc64']
225  kvm_targets = ['ppc-softmmu', 'ppc64-softmmu']
226elif cpu in ['mips', 'mips64']
227  kvm_targets = ['mips-softmmu', 'mipsel-softmmu', 'mips64-softmmu', 'mips64el-softmmu']
228elif cpu in ['riscv32']
229  kvm_targets = ['riscv32-softmmu']
230elif cpu in ['riscv64']
231  kvm_targets = ['riscv64-softmmu']
232elif cpu in ['loongarch64']
233  kvm_targets = ['loongarch64-softmmu']
234else
235  kvm_targets = []
236endif
237accelerator_targets = { 'CONFIG_KVM': kvm_targets }
238
239if cpu in ['x86', 'x86_64']
240  xen_targets = ['i386-softmmu', 'x86_64-softmmu']
241elif cpu in ['arm', 'aarch64']
242  # i386 emulator provides xenpv machine type for multiple architectures
243  xen_targets = ['i386-softmmu', 'x86_64-softmmu', 'aarch64-softmmu']
244else
245  xen_targets = []
246endif
247accelerator_targets += { 'CONFIG_XEN': xen_targets }
248
249if cpu in ['aarch64']
250  accelerator_targets += {
251    'CONFIG_HVF': ['aarch64-softmmu']
252  }
253endif
254
255if cpu in ['x86', 'x86_64']
256  accelerator_targets += {
257    'CONFIG_HVF': ['x86_64-softmmu'],
258    'CONFIG_NVMM': ['i386-softmmu', 'x86_64-softmmu'],
259    'CONFIG_WHPX': ['i386-softmmu', 'x86_64-softmmu'],
260  }
261endif
262
263modular_tcg = []
264# Darwin does not support references to thread-local variables in modules
265if host_os != 'darwin'
266  modular_tcg = ['i386-softmmu', 'x86_64-softmmu']
267endif
268
269##################
270# Compiler flags #
271##################
272
273foreach lang : all_languages
274  compiler = meson.get_compiler(lang)
275  if compiler.get_id() == 'gcc' and compiler.version().version_compare('>=7.4')
276    # ok
277  elif compiler.get_id() == 'clang' and compiler.compiles('''
278      #ifdef __apple_build_version__
279      # if __clang_major__ < 12 || (__clang_major__ == 12 && __clang_minor__ < 0)
280      #  error You need at least XCode Clang v12.0 to compile QEMU
281      # endif
282      #else
283      # if __clang_major__ < 10 || (__clang_major__ == 10 && __clang_minor__ < 0)
284      #  error You need at least Clang v10.0 to compile QEMU
285      # endif
286      #endif''')
287    # ok
288  else
289    error('You either need GCC v7.4 or Clang v10.0 (or XCode Clang v12.0) to compile QEMU')
290  endif
291endforeach
292
293# default flags for all hosts
294# We use -fwrapv to tell the compiler that we require a C dialect where
295# left shift of signed integers is well defined and has the expected
296# 2s-complement style results. (Both clang and gcc agree that it
297# provides these semantics.)
298
299qemu_common_flags = [
300  '-D_GNU_SOURCE', '-D_FILE_OFFSET_BITS=64', '-D_LARGEFILE_SOURCE',
301  '-fno-strict-aliasing', '-fno-common', '-fwrapv' ]
302qemu_cflags = []
303qemu_ldflags = []
304
305if host_os == 'darwin'
306  # Disable attempts to use ObjectiveC features in os/object.h since they
307  # won't work when we're compiling with gcc as a C compiler.
308  if compiler.get_id() == 'gcc'
309    qemu_common_flags += '-DOS_OBJECT_USE_OBJC=0'
310  endif
311elif host_os == 'sunos'
312  # needed for CMSG_ macros in sys/socket.h
313  qemu_common_flags += '-D_XOPEN_SOURCE=600'
314  # needed for TIOCWIN* defines in termios.h
315  qemu_common_flags += '-D__EXTENSIONS__'
316elif host_os == 'haiku'
317  qemu_common_flags += ['-DB_USE_POSITIVE_POSIX_ERRORS', '-D_BSD_SOURCE', '-fPIC']
318endif
319
320# __sync_fetch_and_and requires at least -march=i486. Many toolchains
321# use i686 as default anyway, but for those that don't, an explicit
322# specification is necessary
323if host_arch == 'i386' and not cc.links('''
324  static int sfaa(int *ptr)
325  {
326    return __sync_fetch_and_and(ptr, 0);
327  }
328
329  int main(void)
330  {
331    int val = 42;
332    val = __sync_val_compare_and_swap(&val, 0, 1);
333    sfaa(&val);
334    return val;
335  }''')
336  qemu_common_flags = ['-march=i486'] + qemu_common_flags
337endif
338
339# Pick x86-64 baseline version
340if host_arch in ['i386', 'x86_64']
341  if get_option('x86_version') == '0' and host_arch == 'x86_64'
342    error('x86_64-v1 required for x86-64 hosts')
343  endif
344
345  # add flags for individual instruction set extensions
346  if get_option('x86_version') >= '1'
347    if host_arch == 'i386'
348      qemu_common_flags = ['-mfpmath=sse'] + qemu_common_flags
349    else
350      # present on basically all processors but technically not part of
351      # x86-64-v1, so only include -mneeded for x86-64 version 2 and above
352      qemu_common_flags = ['-mcx16'] + qemu_common_flags
353    endif
354  endif
355  if get_option('x86_version') >= '2'
356    qemu_common_flags = ['-mpopcnt'] + qemu_common_flags
357    qemu_common_flags = cc.get_supported_arguments('-mneeded') + qemu_common_flags
358  endif
359  if get_option('x86_version') >= '3'
360    qemu_common_flags = ['-mmovbe', '-mabm', '-mbmi1', '-mbmi2', '-mfma', '-mf16c'] + qemu_common_flags
361  endif
362
363  # add required vector instruction set (each level implies those below)
364  if get_option('x86_version') == '1'
365    qemu_common_flags = ['-msse2'] + qemu_common_flags
366  elif get_option('x86_version') == '2'
367    qemu_common_flags = ['-msse4.2'] + qemu_common_flags
368  elif get_option('x86_version') == '3'
369    qemu_common_flags = ['-mavx2'] + qemu_common_flags
370  elif get_option('x86_version') == '4'
371    qemu_common_flags = ['-mavx512f', '-mavx512bw', '-mavx512cd', '-mavx512dq', '-mavx512vl'] + qemu_common_flags
372  endif
373endif
374
375if get_option('prefer_static')
376  qemu_ldflags += get_option('b_pie') ? '-static-pie' : '-static'
377endif
378
379# Meson currently only handles pie as a boolean for now, so if the user
380# has explicitly disabled PIE we need to extend our cflags.
381#
382# -no-pie is supposedly a linker flag that has no effect on the compiler
383# command line, but some distros, that didn't quite know what they were
384# doing, made local changes to gcc's specs file that turned it into
385# a compiler command-line flag.
386#
387# What about linker flags?  For a static build, no PIE is implied by -static
388# which we added above (and if it's not because of the same specs patching,
389# there's nothing we can do: compilation will fail, report a bug to your
390# distro and do not use --disable-pie in the meanwhile).  For dynamic linking,
391# instead, we can't add -no-pie because it overrides -shared: the linker then
392# tries to build an executable instead of a shared library and fails.  So
393# don't add -no-pie anywhere and cross fingers. :(
394if not get_option('b_pie')
395  qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
396endif
397
398if not get_option('stack_protector').disabled()
399  stack_protector_probe = '''
400    int main(int argc, char *argv[])
401    {
402      char arr[64], *p = arr, *c = argv[argc - 1];
403      while (*c) {
404          *p++ = *c++;
405      }
406      return 0;
407    }'''
408  have_stack_protector = false
409  foreach arg : ['-fstack-protector-strong', '-fstack-protector-all']
410    # We need to check both a compile and a link, since some compiler
411    # setups fail only on a .c->.o compile and some only at link time
412    if cc.compiles(stack_protector_probe, args: ['-Werror', arg]) and \
413       cc.links(stack_protector_probe, args: ['-Werror', arg])
414      have_stack_protector = true
415      qemu_cflags += arg
416      qemu_ldflags += arg
417      break
418    endif
419  endforeach
420  get_option('stack_protector') \
421    .require(have_stack_protector, error_message: 'Stack protector not supported')
422endif
423
424coroutine_backend = get_option('coroutine_backend')
425ucontext_probe = '''
426  #include <ucontext.h>
427  #ifdef __stub_makecontext
428  #error Ignoring glibc stub makecontext which will always fail
429  #endif
430  int main(void) { makecontext(0, 0, 0); return 0; }'''
431
432# On Windows the only valid backend is the Windows specific one.
433# For POSIX prefer ucontext, but it's not always possible. The fallback
434# is sigcontext.
435supported_backends = []
436if host_os == 'windows'
437  supported_backends += ['windows']
438else
439  if host_os != 'darwin' and cc.links(ucontext_probe)
440    supported_backends += ['ucontext']
441  endif
442  supported_backends += ['sigaltstack']
443endif
444
445if coroutine_backend == 'auto'
446  coroutine_backend = supported_backends[0]
447elif coroutine_backend not in supported_backends
448  error('"@0@" backend requested but not available.  Available backends: @1@' \
449        .format(coroutine_backend, ', '.join(supported_backends)))
450endif
451
452# Compiles if SafeStack *not* enabled
453safe_stack_probe = '''
454  int main(void)
455  {
456  #if defined(__has_feature)
457  #if __has_feature(safe_stack)
458  #error SafeStack Enabled
459  #endif
460  #endif
461      return 0;
462  }'''
463if get_option('safe_stack') != not cc.compiles(safe_stack_probe)
464  safe_stack_arg = get_option('safe_stack') ? '-fsanitize=safe-stack' : '-fno-sanitize=safe-stack'
465  if get_option('safe_stack') != not cc.compiles(safe_stack_probe, args: safe_stack_arg)
466    error(get_option('safe_stack') \
467          ? 'SafeStack not supported by your compiler' \
468          : 'Cannot disable SafeStack')
469  endif
470  qemu_cflags += safe_stack_arg
471  qemu_ldflags += safe_stack_arg
472endif
473if get_option('safe_stack') and coroutine_backend != 'ucontext'
474  error('SafeStack is only supported with the ucontext coroutine backend')
475endif
476
477if get_option('sanitizers')
478  if cc.has_argument('-fsanitize=address')
479    qemu_cflags = ['-fsanitize=address'] + qemu_cflags
480    qemu_ldflags = ['-fsanitize=address'] + qemu_ldflags
481  endif
482
483  # Detect static linking issue with ubsan - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84285
484  if cc.links('int main(int argc, char **argv) { return argc + 1; }',
485              args: [qemu_ldflags, '-fsanitize=undefined'])
486    qemu_cflags = ['-fsanitize=undefined'] + qemu_cflags
487    qemu_ldflags = ['-fsanitize=undefined'] + qemu_ldflags
488  endif
489endif
490
491# Thread sanitizer is, for now, much noisier than the other sanitizers;
492# keep it separate until that is not the case.
493if get_option('tsan')
494  if get_option('sanitizers')
495    error('TSAN is not supported with other sanitizers')
496  endif
497  if not cc.has_function('__tsan_create_fiber',
498                         args: '-fsanitize=thread',
499                         prefix: '#include <sanitizer/tsan_interface.h>')
500    error('Cannot enable TSAN due to missing fiber annotation interface')
501  endif
502  qemu_cflags = ['-fsanitize=thread'] + qemu_cflags
503  qemu_ldflags = ['-fsanitize=thread'] + qemu_ldflags
504endif
505
506# Detect support for PT_GNU_RELRO + DT_BIND_NOW.
507# The combination is known as "full relro", because .got.plt is read-only too.
508qemu_ldflags += cc.get_supported_link_arguments('-Wl,-z,relro', '-Wl,-z,now')
509
510if host_os == 'windows'
511  qemu_ldflags += cc.get_supported_link_arguments('-Wl,--no-seh', '-Wl,--nxcompat')
512  qemu_ldflags += cc.get_supported_link_arguments('-Wl,--dynamicbase', '-Wl,--high-entropy-va')
513endif
514
515if get_option('fuzzing')
516  # Specify a filter to only instrument code that is directly related to
517  # virtual-devices.
518  configure_file(output: 'instrumentation-filter',
519                 input: 'scripts/oss-fuzz/instrumentation-filter-template',
520                 copy: true)
521
522  if cc.compiles('int main () { return 0; }',
523                  name: '-fsanitize-coverage-allowlist=/dev/null',
524                 args: ['-fsanitize-coverage-allowlist=/dev/null',
525                        '-fsanitize-coverage=trace-pc'] )
526    qemu_common_flags += ['-fsanitize-coverage-allowlist=instrumentation-filter']
527  endif
528
529  if get_option('fuzzing_engine') == ''
530    # Add CFLAGS to tell clang to add fuzzer-related instrumentation to all the
531    # compiled code.  To build non-fuzzer binaries with --enable-fuzzing, link
532    # everything with fsanitize=fuzzer-no-link. Otherwise, the linker will be
533    # unable to bind the fuzzer-related callbacks added by instrumentation.
534    qemu_common_flags += ['-fsanitize=fuzzer-no-link']
535    qemu_ldflags += ['-fsanitize=fuzzer-no-link']
536    # For the actual fuzzer binaries, we need to link against the libfuzzer
537    # library. They need to be configurable, to support OSS-Fuzz
538    fuzz_exe_ldflags = ['-fsanitize=fuzzer']
539  else
540    # LIB_FUZZING_ENGINE was set; assume we are running on OSS-Fuzz, and
541    # the needed CFLAGS have already been provided
542    fuzz_exe_ldflags = get_option('fuzzing_engine').split()
543  endif
544endif
545
546if get_option('cfi')
547  cfi_flags=[]
548  # Check for dependency on LTO
549  if not get_option('b_lto')
550    error('Selected Control-Flow Integrity but LTO is disabled')
551  endif
552  if enable_modules
553    error('Selected Control-Flow Integrity is not compatible with modules')
554  endif
555  # Check for cfi flags. CFI requires LTO so we can't use
556  # get_supported_arguments, but need a more complex "compiles" which allows
557  # custom arguments
558  if cc.compiles('int main () { return 0; }', name: '-fsanitize=cfi-icall',
559                 args: ['-flto', '-fsanitize=cfi-icall'] )
560    cfi_flags += '-fsanitize=cfi-icall'
561  else
562    error('-fsanitize=cfi-icall is not supported by the compiler')
563  endif
564  if cc.compiles('int main () { return 0; }',
565                 name: '-fsanitize-cfi-icall-generalize-pointers',
566                 args: ['-flto', '-fsanitize=cfi-icall',
567                        '-fsanitize-cfi-icall-generalize-pointers'] )
568    cfi_flags += '-fsanitize-cfi-icall-generalize-pointers'
569  else
570    error('-fsanitize-cfi-icall-generalize-pointers is not supported by the compiler')
571  endif
572  if get_option('cfi_debug')
573    if cc.compiles('int main () { return 0; }',
574                   name: '-fno-sanitize-trap=cfi-icall',
575                   args: ['-flto', '-fsanitize=cfi-icall',
576                          '-fno-sanitize-trap=cfi-icall'] )
577      cfi_flags += '-fno-sanitize-trap=cfi-icall'
578    else
579      error('-fno-sanitize-trap=cfi-icall is not supported by the compiler')
580    endif
581  endif
582  add_global_arguments(cfi_flags, native: false, language: all_languages)
583  add_global_link_arguments(cfi_flags, native: false, language: all_languages)
584endif
585
586# Check further flags that make QEMU more robust against malicious parties
587
588hardening_flags = [
589    # Initialize all stack variables to zero. This makes
590    # it harder to take advantage of uninitialized stack
591    # data to drive exploits
592    '-ftrivial-auto-var-init=zero',
593]
594
595# Zero out registers used during a function call
596# upon its return. This makes it harder to assemble
597# ROP gadgets into something usable
598#
599# NB: Clang 17 is broken and SEGVs
600# https://github.com/llvm/llvm-project/issues/75168
601#
602# NB2: This clashes with the "retguard" extension of OpenBSD's Clang
603# https://gitlab.com/qemu-project/qemu/-/issues/2278
604if host_os != 'openbsd' and \
605   cc.compiles('extern struct { void (*cb)(void); } s; void f(void) { s.cb(); }',
606               name: '-fzero-call-used-regs=used-gpr',
607               args: ['-O2', '-fzero-call-used-regs=used-gpr'])
608    hardening_flags += '-fzero-call-used-regs=used-gpr'
609endif
610
611qemu_common_flags += cc.get_supported_arguments(hardening_flags)
612
613add_global_arguments(qemu_common_flags, native: false, language: all_languages)
614add_global_link_arguments(qemu_ldflags, native: false, language: all_languages)
615
616# Collect warning flags we want to set, sorted alphabetically
617warn_flags = [
618  # First enable interesting warnings
619  '-Wempty-body',
620  '-Wendif-labels',
621  '-Wexpansion-to-defined',
622  '-Wformat-security',
623  '-Wformat-y2k',
624  '-Wignored-qualifiers',
625  '-Wimplicit-fallthrough=2',
626  '-Winit-self',
627  '-Wmissing-format-attribute',
628  '-Wmissing-prototypes',
629  '-Wnested-externs',
630  '-Wold-style-declaration',
631  '-Wold-style-definition',
632  '-Wredundant-decls',
633  '-Wshadow=local',
634  '-Wstrict-prototypes',
635  '-Wtype-limits',
636  '-Wundef',
637  '-Wvla',
638  '-Wwrite-strings',
639
640  # Then disable some undesirable warnings
641  '-Wno-gnu-variable-sized-type-not-at-end',
642  '-Wno-initializer-overrides',
643  '-Wno-missing-include-dirs',
644  '-Wno-psabi',
645  '-Wno-shift-negative-value',
646  '-Wno-string-plus-int',
647  '-Wno-tautological-type-limit-compare',
648  '-Wno-typedef-redefinition',
649]
650
651if host_os != 'darwin'
652  warn_flags += ['-Wthread-safety']
653endif
654
655# Set up C++ compiler flags
656qemu_cxxflags = []
657if 'cpp' in all_languages
658  qemu_cxxflags = ['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'] + qemu_cflags
659endif
660
661add_project_arguments(qemu_cflags, native: false, language: 'c')
662add_project_arguments(cc.get_supported_arguments(warn_flags), native: false, language: 'c')
663if 'cpp' in all_languages
664  add_project_arguments(qemu_cxxflags, native: false, language: 'cpp')
665  add_project_arguments(cxx.get_supported_arguments(warn_flags), native: false, language: 'cpp')
666endif
667if 'objc' in all_languages
668  # Note sanitizer flags are not applied to Objective-C sources!
669  add_project_arguments(objc.get_supported_arguments(warn_flags), native: false, language: 'objc')
670endif
671if host_os == 'linux'
672  add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers',
673                        '-isystem', 'linux-headers',
674                        language: all_languages)
675endif
676
677add_project_arguments('-iquote', '.',
678                      '-iquote', meson.current_source_dir(),
679                      '-iquote', meson.current_source_dir() / 'include',
680                      language: all_languages)
681
682# If a host-specific include directory exists, list that first...
683host_include = meson.current_source_dir() / 'host/include/'
684if fs.is_dir(host_include / host_arch)
685  add_project_arguments('-iquote', host_include / host_arch,
686                        language: all_languages)
687endif
688# ... followed by the generic fallback.
689add_project_arguments('-iquote', host_include / 'generic',
690                      language: all_languages)
691
692sparse = find_program('cgcc', required: get_option('sparse'))
693if sparse.found()
694  run_target('sparse',
695             command: [find_program('scripts/check_sparse.py'),
696                       'compile_commands.json', sparse.full_path(), '-Wbitwise',
697                       '-Wno-transparent-union', '-Wno-old-initializer',
698                       '-Wno-non-pointer-null'])
699endif
700
701#####################################
702# Host-specific libraries and flags #
703#####################################
704
705libm = cc.find_library('m', required: false)
706threads = dependency('threads')
707util = cc.find_library('util', required: false)
708winmm = []
709socket = []
710version_res = []
711coref = []
712iokit = []
713emulator_link_args = []
714midl = not_found
715widl = not_found
716pathcch = not_found
717host_dsosuf = '.so'
718if host_os == 'windows'
719  midl = find_program('midl', required: false)
720  widl = find_program('widl', required: false)
721  pathcch = cc.find_library('pathcch')
722  socket = cc.find_library('ws2_32')
723  winmm = cc.find_library('winmm')
724
725  win = import('windows')
726  version_res = win.compile_resources('version.rc',
727                                      depend_files: files('pc-bios/qemu-nsis.ico'),
728                                      include_directories: include_directories('.'))
729  host_dsosuf = '.dll'
730elif host_os == 'darwin'
731  coref = dependency('appleframeworks', modules: 'CoreFoundation')
732  iokit = dependency('appleframeworks', modules: 'IOKit', required: false)
733  host_dsosuf = '.dylib'
734elif host_os == 'sunos'
735  socket = [cc.find_library('socket'),
736            cc.find_library('nsl'),
737            cc.find_library('resolv')]
738elif host_os == 'haiku'
739  socket = [cc.find_library('posix_error_mapper'),
740            cc.find_library('network'),
741            cc.find_library('bsd')]
742elif host_os == 'openbsd'
743  if get_option('tcg').allowed() and target_dirs.length() > 0
744    # Disable OpenBSD W^X if available
745    emulator_link_args = cc.get_supported_link_arguments('-Wl,-z,wxneeded')
746  endif
747endif
748
749###############################################
750# Host-specific configuration of accelerators #
751###############################################
752
753accelerators = []
754if get_option('kvm').allowed() and host_os == 'linux'
755  accelerators += 'CONFIG_KVM'
756endif
757if get_option('whpx').allowed() and host_os == 'windows'
758  if get_option('whpx').enabled() and host_machine.cpu() != 'x86_64'
759    error('WHPX requires 64-bit host')
760  elif cc.has_header('winhvplatform.h', required: get_option('whpx')) and \
761       cc.has_header('winhvemulation.h', required: get_option('whpx'))
762    accelerators += 'CONFIG_WHPX'
763  endif
764endif
765
766hvf = not_found
767if get_option('hvf').allowed()
768  hvf = dependency('appleframeworks', modules: 'Hypervisor',
769                   required: get_option('hvf'))
770  if hvf.found()
771    accelerators += 'CONFIG_HVF'
772  endif
773endif
774
775nvmm = not_found
776if host_os == 'netbsd'
777  nvmm = cc.find_library('nvmm', required: get_option('nvmm'))
778  if nvmm.found()
779    accelerators += 'CONFIG_NVMM'
780  endif
781endif
782
783tcg_arch = host_arch
784if get_option('tcg').allowed()
785  if host_arch == 'unknown'
786    if not get_option('tcg_interpreter')
787      error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu))
788    endif
789  elif get_option('tcg_interpreter')
790    warning('Use of the TCG interpreter is not recommended on this host')
791    warning('architecture. There is a native TCG execution backend available')
792    warning('which provides substantially better performance and reliability.')
793    warning('It is strongly recommended to remove the --enable-tcg-interpreter')
794    warning('configuration option on this architecture to use the native')
795    warning('backend.')
796  endif
797  if get_option('tcg_interpreter')
798    tcg_arch = 'tci'
799  elif host_arch == 'x86_64'
800    tcg_arch = 'i386'
801  elif host_arch == 'ppc64'
802    tcg_arch = 'ppc'
803  endif
804  add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch,
805                        language: all_languages)
806
807  accelerators += 'CONFIG_TCG'
808endif
809
810if 'CONFIG_KVM' not in accelerators and get_option('kvm').enabled()
811  error('KVM not available on this platform')
812endif
813if 'CONFIG_HVF' not in accelerators and get_option('hvf').enabled()
814  error('HVF not available on this platform')
815endif
816if 'CONFIG_NVMM' not in accelerators and get_option('nvmm').enabled()
817  error('NVMM not available on this platform')
818endif
819if 'CONFIG_WHPX' not in accelerators and get_option('whpx').enabled()
820  error('WHPX not available on this platform')
821endif
822
823xen = not_found
824if get_option('xen').enabled() or (get_option('xen').auto() and have_system)
825  xencontrol = dependency('xencontrol', required: false,
826                          method: 'pkg-config')
827  if xencontrol.found()
828    xen_pc = declare_dependency(version: xencontrol.version(),
829      dependencies: [
830        xencontrol,
831        # disabler: true makes xen_pc.found() return false if any is not found
832        dependency('xenstore', required: false,
833                   method: 'pkg-config',
834                   disabler: true),
835        dependency('xenforeignmemory', required: false,
836                   method: 'pkg-config',
837                   disabler: true),
838        dependency('xengnttab', required: false,
839                   method: 'pkg-config',
840                   disabler: true),
841        dependency('xenevtchn', required: false,
842                   method: 'pkg-config',
843                   disabler: true),
844        dependency('xendevicemodel', required: false,
845                   method: 'pkg-config',
846                   disabler: true),
847        # optional, no "disabler: true"
848        dependency('xentoolcore', required: false,
849                   method: 'pkg-config')])
850    if xen_pc.found()
851      xen = xen_pc
852    endif
853  endif
854  if not xen.found()
855    xen_tests = [ '4.11.0', '4.10.0', '4.9.0', '4.8.0', '4.7.1' ]
856    xen_libs = {
857      '4.11.0': [ 'xenstore', 'xenctrl', 'xendevicemodel', 'xenforeignmemory', 'xengnttab', 'xenevtchn', 'xentoolcore' ],
858      '4.10.0': [ 'xenstore', 'xenctrl', 'xendevicemodel', 'xenforeignmemory', 'xengnttab', 'xenevtchn', 'xentoolcore' ],
859      '4.9.0': [ 'xenstore', 'xenctrl', 'xendevicemodel', 'xenforeignmemory', 'xengnttab', 'xenevtchn' ],
860      '4.8.0': [ 'xenstore', 'xenctrl', 'xenforeignmemory', 'xengnttab', 'xenevtchn' ],
861      '4.7.1': [ 'xenstore', 'xenctrl', 'xenforeignmemory', 'xengnttab', 'xenevtchn' ],
862    }
863    xen_deps = {}
864    foreach ver: xen_tests
865      # cache the various library tests to avoid polluting the logs
866      xen_test_deps = []
867      foreach l: xen_libs[ver]
868        if l not in xen_deps
869          xen_deps += { l: cc.find_library(l, required: false) }
870        endif
871        xen_test_deps += xen_deps[l]
872      endforeach
873
874      # Use -D to pick just one of the test programs in scripts/xen-detect.c
875      xen_version = ver.split('.')
876      xen_ctrl_version = xen_version[0] + \
877        ('0' + xen_version[1]).substring(-2) + \
878        ('0' + xen_version[2]).substring(-2)
879      if cc.links(files('scripts/xen-detect.c'),
880                  args: '-DCONFIG_XEN_CTRL_INTERFACE_VERSION=' + xen_ctrl_version,
881                  dependencies: xen_test_deps)
882        xen = declare_dependency(version: ver, dependencies: xen_test_deps)
883        break
884      endif
885    endforeach
886  endif
887  if xen.found()
888    accelerators += 'CONFIG_XEN'
889  elif get_option('xen').enabled()
890    error('could not compile and link Xen test program')
891  endif
892endif
893have_xen_pci_passthrough = get_option('xen_pci_passthrough') \
894  .require(xen.found(),
895           error_message: 'Xen PCI passthrough requested but Xen not enabled') \
896  .require(host_os == 'linux',
897           error_message: 'Xen PCI passthrough not available on this platform') \
898  .require(cpu == 'x86'  or cpu == 'x86_64',
899           error_message: 'Xen PCI passthrough not available on this platform') \
900  .allowed()
901
902################
903# Dependencies #
904################
905
906# When bumping glib minimum version, please check also whether to increase
907# the _WIN32_WINNT setting in osdep.h according to the value from glib
908glib_req_ver = '>=2.66.0'
909glib_pc = dependency('glib-2.0', version: glib_req_ver, required: true,
910                    method: 'pkg-config')
911glib_cflags = []
912if enable_modules
913  gmodule = dependency('gmodule-export-2.0', version: glib_req_ver, required: true,
914                       method: 'pkg-config')
915elif get_option('plugins')
916  gmodule = dependency('gmodule-no-export-2.0', version: glib_req_ver, required: true,
917                       method: 'pkg-config')
918else
919  gmodule = not_found
920endif
921
922# This workaround is required due to a bug in pkg-config file for glib as it
923# doesn't define GLIB_STATIC_COMPILATION for pkg-config --static
924if host_os == 'windows' and get_option('prefer_static')
925  glib_cflags += ['-DGLIB_STATIC_COMPILATION']
926endif
927
928# Sanity check that the current size_t matches the
929# size that glib thinks it should be. This catches
930# problems on multi-arch where people try to build
931# 32-bit QEMU while pointing at 64-bit glib headers
932
933if not cc.compiles('''
934  #include <glib.h>
935  #include <unistd.h>
936
937  #define QEMU_BUILD_BUG_ON(x) \
938  typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
939
940  int main(void) {
941     QEMU_BUILD_BUG_ON(sizeof(size_t) != GLIB_SIZEOF_SIZE_T);
942     return 0;
943  }''', dependencies: glib_pc, args: glib_cflags)
944  error('''sizeof(size_t) doesn't match GLIB_SIZEOF_SIZE_T.
945        You probably need to set PKG_CONFIG_LIBDIR" to point
946        to the right pkg-config files for your build target.''')
947endif
948
949glib = declare_dependency(dependencies: [glib_pc, gmodule],
950                          compile_args: glib_cflags,
951                          version: glib_pc.version())
952
953# Check whether glib has gslice, which we have to avoid for correctness.
954# TODO: remove this check and the corresponding workaround (qtree) when
955# the minimum supported glib is >= 2.75.3
956glib_has_gslice = glib.version().version_compare('<2.75.3')
957
958# override glib dep to include the above refinements
959meson.override_dependency('glib-2.0', glib)
960
961# The path to glib.h is added to all compilation commands.
962add_project_dependencies(glib.partial_dependency(compile_args: true, includes: true),
963                         native: false, language: all_languages)
964
965gio = not_found
966gdbus_codegen = not_found
967gdbus_codegen_error = '@0@ requires gdbus-codegen, please install libgio'
968if not get_option('gio').auto() or have_system
969  gio = dependency('gio-2.0', required: get_option('gio'),
970                   method: 'pkg-config')
971  if gio.found() and not cc.links('''
972    #include <gio/gio.h>
973    int main(void)
974    {
975      g_dbus_proxy_new_sync(0, 0, 0, 0, 0, 0, 0, 0);
976      return 0;
977    }''', dependencies: [glib, gio])
978    if get_option('gio').enabled()
979      error('The installed libgio is broken for static linking')
980    endif
981    gio = not_found
982  endif
983  if gio.found()
984    gdbus_codegen = find_program(gio.get_variable('gdbus_codegen'),
985                                 required: get_option('gio'))
986    gio_unix = dependency('gio-unix-2.0', required: get_option('gio'),
987                          method: 'pkg-config')
988    gio = declare_dependency(dependencies: [gio, gio_unix],
989                             version: gio.version())
990  endif
991endif
992if gdbus_codegen.found() and get_option('cfi')
993  gdbus_codegen = not_found
994  gdbus_codegen_error = '@0@ uses gdbus-codegen, which does not support control flow integrity'
995endif
996
997xml_pp = find_program('scripts/xml-preprocess.py')
998
999lttng = not_found
1000if 'ust' in get_option('trace_backends')
1001  lttng = dependency('lttng-ust', required: true, version: '>= 2.1',
1002                     method: 'pkg-config')
1003endif
1004pixman = not_found
1005if not get_option('pixman').auto() or have_system or have_tools
1006  pixman = dependency('pixman-1', required: get_option('pixman'), version:'>=0.21.8',
1007                      method: 'pkg-config')
1008endif
1009
1010zlib = dependency('zlib', required: true)
1011
1012libaio = not_found
1013if not get_option('linux_aio').auto() or have_block
1014  libaio = cc.find_library('aio', has_headers: ['libaio.h'],
1015                           required: get_option('linux_aio'))
1016endif
1017
1018linux_io_uring_test = '''
1019  #include <liburing.h>
1020  #include <linux/errqueue.h>
1021
1022  int main(void) { return 0; }'''
1023
1024linux_io_uring = not_found
1025if not get_option('linux_io_uring').auto() or have_block
1026  linux_io_uring = dependency('liburing', version: '>=0.3',
1027                              required: get_option('linux_io_uring'),
1028                              method: 'pkg-config')
1029  if not cc.links(linux_io_uring_test)
1030    linux_io_uring = not_found
1031  endif
1032endif
1033
1034libnfs = not_found
1035if not get_option('libnfs').auto() or have_block
1036  libnfs = dependency('libnfs', version: '>=1.9.3',
1037                      required: get_option('libnfs'),
1038                      method: 'pkg-config')
1039endif
1040
1041libattr_test = '''
1042  #include <stddef.h>
1043  #include <sys/types.h>
1044  #ifdef CONFIG_LIBATTR
1045  #include <attr/xattr.h>
1046  #else
1047  #include <sys/xattr.h>
1048  #endif
1049  int main(void) { getxattr(NULL, NULL, NULL, 0); setxattr(NULL, NULL, NULL, 0, 0); return 0; }'''
1050
1051libattr = not_found
1052have_old_libattr = false
1053if get_option('attr').allowed()
1054  if cc.links(libattr_test)
1055    libattr = declare_dependency()
1056  else
1057    libattr = cc.find_library('attr', has_headers: ['attr/xattr.h'],
1058                              required: get_option('attr'))
1059    if libattr.found() and not \
1060      cc.links(libattr_test, dependencies: libattr, args: '-DCONFIG_LIBATTR')
1061      libattr = not_found
1062      if get_option('attr').enabled()
1063        error('could not link libattr')
1064      else
1065        warning('could not link libattr, disabling')
1066      endif
1067    else
1068      have_old_libattr = libattr.found()
1069    endif
1070  endif
1071endif
1072
1073cocoa = dependency('appleframeworks',
1074                   modules: ['Cocoa', 'CoreVideo', 'QuartzCore'],
1075                   required: get_option('cocoa'))
1076
1077vmnet = dependency('appleframeworks', modules: 'vmnet', required: get_option('vmnet'))
1078if vmnet.found() and not cc.has_header_symbol('vmnet/vmnet.h',
1079                                              'VMNET_BRIDGED_MODE',
1080                                              dependencies: vmnet)
1081  vmnet = not_found
1082  if get_option('vmnet').enabled()
1083    error('vmnet.framework API is outdated')
1084  else
1085    warning('vmnet.framework API is outdated, disabling')
1086  endif
1087endif
1088
1089seccomp = not_found
1090seccomp_has_sysrawrc = false
1091if not get_option('seccomp').auto() or have_system or have_tools
1092  seccomp = dependency('libseccomp', version: '>=2.3.0',
1093                       required: get_option('seccomp'),
1094                       method: 'pkg-config')
1095  if seccomp.found()
1096    seccomp_has_sysrawrc = cc.has_header_symbol('seccomp.h',
1097                                                'SCMP_FLTATR_API_SYSRAWRC',
1098                                                dependencies: seccomp)
1099  endif
1100endif
1101
1102libcap_ng = not_found
1103if not get_option('cap_ng').auto() or have_system or have_tools
1104  libcap_ng = cc.find_library('cap-ng', has_headers: ['cap-ng.h'],
1105                              required: get_option('cap_ng'))
1106endif
1107if libcap_ng.found() and not cc.links('''
1108   #include <cap-ng.h>
1109   int main(void)
1110   {
1111     capng_capability_to_name(CAPNG_EFFECTIVE);
1112     return 0;
1113   }''', dependencies: libcap_ng)
1114  libcap_ng = not_found
1115  if get_option('cap_ng').enabled()
1116    error('could not link libcap-ng')
1117  else
1118    warning('could not link libcap-ng, disabling')
1119  endif
1120endif
1121
1122if get_option('xkbcommon').auto() and not have_system and not have_tools
1123  xkbcommon = not_found
1124else
1125  xkbcommon = dependency('xkbcommon', required: get_option('xkbcommon'),
1126                         method: 'pkg-config')
1127endif
1128
1129slirp = not_found
1130if not get_option('slirp').auto() or have_system
1131  slirp = dependency('slirp', required: get_option('slirp'),
1132                     method: 'pkg-config')
1133  # slirp < 4.7 is incompatible with CFI support in QEMU.  This is because
1134  # it passes function pointers within libslirp as callbacks for timers.
1135  # When using a system-wide shared libslirp, the type information for the
1136  # callback is missing and the timer call produces a false positive with CFI.
1137  # Do not use the "version" keyword argument to produce a better error.
1138  # with control-flow integrity.
1139  if get_option('cfi') and slirp.found() and slirp.version().version_compare('<4.7')
1140    if get_option('slirp').enabled()
1141      error('Control-Flow Integrity requires libslirp 4.7.')
1142    else
1143      warning('Cannot use libslirp since Control-Flow Integrity requires libslirp >= 4.7.')
1144      slirp = not_found
1145    endif
1146  endif
1147endif
1148
1149vde = not_found
1150if not get_option('vde').auto() or have_system or have_tools
1151  vde = cc.find_library('vdeplug', has_headers: ['libvdeplug.h'],
1152                           required: get_option('vde'))
1153endif
1154if vde.found() and not cc.links('''
1155   #include <libvdeplug.h>
1156   int main(void)
1157   {
1158     struct vde_open_args a = {0, 0, 0};
1159     char s[] = "";
1160     vde_open(s, s, &a);
1161     return 0;
1162   }''', dependencies: vde)
1163  vde = not_found
1164  if get_option('cap_ng').enabled()
1165    error('could not link libvdeplug')
1166  else
1167    warning('could not link libvdeplug, disabling')
1168  endif
1169endif
1170
1171pulse = not_found
1172if not get_option('pa').auto() or (host_os == 'linux' and have_system)
1173  pulse = dependency('libpulse', required: get_option('pa'),
1174                     method: 'pkg-config')
1175endif
1176alsa = not_found
1177if not get_option('alsa').auto() or (host_os == 'linux' and have_system)
1178  alsa = dependency('alsa', required: get_option('alsa'),
1179                    method: 'pkg-config')
1180endif
1181jack = not_found
1182if not get_option('jack').auto() or have_system
1183  jack = dependency('jack', required: get_option('jack'),
1184                    method: 'pkg-config')
1185endif
1186pipewire = not_found
1187if not get_option('pipewire').auto() or (host_os == 'linux' and have_system)
1188  pipewire = dependency('libpipewire-0.3', version: '>=0.3.60',
1189                    required: get_option('pipewire'),
1190                    method: 'pkg-config')
1191endif
1192sndio = not_found
1193if not get_option('sndio').auto() or have_system
1194  sndio = dependency('sndio', required: get_option('sndio'),
1195                    method: 'pkg-config')
1196endif
1197
1198spice_protocol = not_found
1199if not get_option('spice_protocol').auto() or have_system
1200  spice_protocol = dependency('spice-protocol', version: '>=0.14.0',
1201                              required: get_option('spice_protocol'),
1202                              method: 'pkg-config')
1203endif
1204spice = not_found
1205if get_option('spice') \
1206             .disable_auto_if(not have_system) \
1207             .require(pixman.found(),
1208                      error_message: 'cannot enable SPICE if pixman is not available') \
1209             .allowed()
1210  spice = dependency('spice-server', version: '>=0.14.0',
1211                     required: get_option('spice'),
1212                     method: 'pkg-config')
1213endif
1214spice_headers = spice.partial_dependency(compile_args: true, includes: true)
1215
1216rt = cc.find_library('rt', required: false)
1217
1218libiscsi = not_found
1219if not get_option('libiscsi').auto() or have_block
1220  libiscsi = dependency('libiscsi', version: '>=1.9.0',
1221                         required: get_option('libiscsi'),
1222                         method: 'pkg-config')
1223endif
1224zstd = not_found
1225if not get_option('zstd').auto() or have_block
1226  zstd = dependency('libzstd', version: '>=1.4.0',
1227                    required: get_option('zstd'),
1228                    method: 'pkg-config')
1229endif
1230qpl = not_found
1231if not get_option('qpl').auto() or have_system
1232  qpl = dependency('qpl', version: '>=1.5.0',
1233                    required: get_option('qpl'),
1234                    method: 'pkg-config')
1235endif
1236uadk = not_found
1237if not get_option('uadk').auto() or have_system
1238  libwd = dependency('libwd', version: '>=2.6',
1239                      required: get_option('uadk'),
1240                      method: 'pkg-config')
1241  libwd_comp = dependency('libwd_comp', version: '>=2.6',
1242                           required: get_option('uadk'),
1243                           method: 'pkg-config')
1244  if libwd.found() and libwd_comp.found()
1245     uadk = declare_dependency(dependencies: [libwd, libwd_comp])
1246  endif
1247endif
1248virgl = not_found
1249
1250have_vhost_user_gpu = have_tools and host_os == 'linux' and pixman.found()
1251if not get_option('virglrenderer').auto() or have_system or have_vhost_user_gpu
1252  virgl = dependency('virglrenderer',
1253                     method: 'pkg-config',
1254                     required: get_option('virglrenderer'))
1255endif
1256rutabaga = not_found
1257if not get_option('rutabaga_gfx').auto() or have_system or have_vhost_user_gpu
1258  rutabaga = dependency('rutabaga_gfx_ffi',
1259                         method: 'pkg-config',
1260                         required: get_option('rutabaga_gfx'))
1261endif
1262blkio = not_found
1263if not get_option('blkio').auto() or have_block
1264  blkio = dependency('blkio',
1265                     method: 'pkg-config',
1266                     required: get_option('blkio'))
1267endif
1268curl = not_found
1269if not get_option('curl').auto() or have_block
1270  curl = dependency('libcurl', version: '>=7.29.0',
1271                    method: 'pkg-config',
1272                    required: get_option('curl'))
1273endif
1274libudev = not_found
1275if host_os == 'linux' and (have_system or have_tools)
1276  libudev = dependency('libudev',
1277                       method: 'pkg-config',
1278                       required: get_option('libudev'))
1279endif
1280
1281mpathlibs = [libudev]
1282mpathpersist = not_found
1283if host_os == 'linux' and have_tools and get_option('mpath').allowed()
1284  mpath_test_source = '''
1285    #include <libudev.h>
1286    #include <mpath_persist.h>
1287    unsigned mpath_mx_alloc_len = 1024;
1288    int logsink;
1289    static struct config *multipath_conf;
1290    extern struct udev *udev;
1291    extern struct config *get_multipath_config(void);
1292    extern void put_multipath_config(struct config *conf);
1293    struct udev *udev;
1294    struct config *get_multipath_config(void) { return multipath_conf; }
1295    void put_multipath_config(struct config *conf) { }
1296    int main(void) {
1297        udev = udev_new();
1298        multipath_conf = mpath_lib_init();
1299        return 0;
1300    }'''
1301  libmpathpersist = cc.find_library('mpathpersist',
1302                                    required: get_option('mpath'))
1303  if libmpathpersist.found()
1304    mpathlibs += libmpathpersist
1305    if get_option('prefer_static')
1306      mpathlibs += cc.find_library('devmapper',
1307                                     required: get_option('mpath'))
1308    endif
1309    mpathlibs += cc.find_library('multipath',
1310                                 required: get_option('mpath'))
1311    foreach lib: mpathlibs
1312      if not lib.found()
1313        mpathlibs = []
1314        break
1315      endif
1316    endforeach
1317    if mpathlibs.length() == 0
1318      msg = 'Dependencies missing for libmpathpersist'
1319    elif cc.links(mpath_test_source, dependencies: mpathlibs)
1320      mpathpersist = declare_dependency(dependencies: mpathlibs)
1321    else
1322      msg = 'Cannot detect libmpathpersist API'
1323    endif
1324    if not mpathpersist.found()
1325      if get_option('mpath').enabled()
1326        error(msg)
1327      else
1328        warning(msg + ', disabling')
1329      endif
1330    endif
1331  endif
1332endif
1333
1334iconv = not_found
1335curses = not_found
1336if have_system and get_option('curses').allowed()
1337  curses_test = '''
1338    #if defined(__APPLE__) || defined(__OpenBSD__)
1339    #define _XOPEN_SOURCE_EXTENDED 1
1340    #endif
1341    #include <locale.h>
1342    #include <curses.h>
1343    #include <wchar.h>
1344    int main(void) {
1345      wchar_t wch = L'w';
1346      setlocale(LC_ALL, "");
1347      resize_term(0, 0);
1348      addwstr(L"wide chars\n");
1349      addnwstr(&wch, 1);
1350      add_wch(WACS_DEGREE);
1351      return 0;
1352    }'''
1353
1354  curses_dep_list = host_os == 'windows' ? ['ncurses', 'ncursesw'] : ['ncursesw']
1355  curses = dependency(curses_dep_list,
1356                      required: false,
1357                      method: 'pkg-config')
1358  msg = get_option('curses').enabled() ? 'curses library not found' : ''
1359  curses_compile_args = ['-DNCURSES_WIDECHAR=1']
1360  if curses.found()
1361    if cc.links(curses_test, args: curses_compile_args, dependencies: [curses])
1362      curses = declare_dependency(compile_args: curses_compile_args, dependencies: [curses],
1363                                  version: curses.version())
1364    else
1365      msg = 'curses package not usable'
1366      curses = not_found
1367    endif
1368  endif
1369  if not curses.found()
1370    has_curses_h = cc.has_header('curses.h', args: curses_compile_args)
1371    if host_os != 'windows' and not has_curses_h
1372      message('Trying with /usr/include/ncursesw')
1373      curses_compile_args += ['-I/usr/include/ncursesw']
1374      has_curses_h = cc.has_header('curses.h', args: curses_compile_args)
1375    endif
1376    if has_curses_h
1377      curses_libname_list = (host_os == 'windows' ? ['pdcurses'] : ['ncursesw', 'cursesw'])
1378      foreach curses_libname : curses_libname_list
1379        libcurses = cc.find_library(curses_libname,
1380                                    required: false)
1381        if libcurses.found()
1382          if cc.links(curses_test, args: curses_compile_args, dependencies: libcurses)
1383            curses = declare_dependency(compile_args: curses_compile_args,
1384                                        dependencies: [libcurses])
1385            break
1386          else
1387            msg = 'curses library not usable'
1388          endif
1389        endif
1390      endforeach
1391    endif
1392  endif
1393  if get_option('iconv').allowed()
1394    foreach link_args : [ ['-liconv'], [] ]
1395      # Programs will be linked with glib and this will bring in libiconv on FreeBSD.
1396      # We need to use libiconv if available because mixing libiconv's headers with
1397      # the system libc does not work.
1398      # However, without adding glib to the dependencies -L/usr/local/lib will not be
1399      # included in the command line and libiconv will not be found.
1400      if cc.links('''
1401        #include <iconv.h>
1402        int main(void) {
1403          iconv_t conv = iconv_open("WCHAR_T", "UCS-2");
1404          return conv != (iconv_t) -1;
1405        }''', args: link_args, dependencies: glib)
1406        iconv = declare_dependency(link_args: link_args, dependencies: glib)
1407        break
1408      endif
1409    endforeach
1410  endif
1411  if curses.found() and not iconv.found()
1412    if get_option('iconv').enabled()
1413      error('iconv not available')
1414    endif
1415    msg = 'iconv required for curses UI but not available'
1416    curses = not_found
1417  endif
1418  if not curses.found() and msg != ''
1419    if get_option('curses').enabled()
1420      error(msg)
1421    else
1422      warning(msg + ', disabling')
1423    endif
1424  endif
1425endif
1426
1427brlapi = not_found
1428if not get_option('brlapi').auto() or have_system
1429  brlapi = cc.find_library('brlapi', has_headers: ['brlapi.h'],
1430                         required: get_option('brlapi'))
1431  if brlapi.found() and not cc.links('''
1432     #include <brlapi.h>
1433     #include <stddef.h>
1434     int main(void) { return brlapi__openConnection (NULL, NULL, NULL); }''', dependencies: brlapi)
1435    brlapi = not_found
1436    if get_option('brlapi').enabled()
1437      error('could not link brlapi')
1438    else
1439      warning('could not link brlapi, disabling')
1440    endif
1441  endif
1442endif
1443
1444sdl = not_found
1445if not get_option('sdl').auto() or have_system
1446  sdl = dependency('sdl2', required: get_option('sdl'))
1447  sdl_image = not_found
1448endif
1449if sdl.found()
1450  # Some versions of SDL have problems with -Wundef
1451  if not cc.compiles('''
1452                     #include <SDL.h>
1453                     #include <SDL_syswm.h>
1454                     int main(int argc, char *argv[]) { return 0; }
1455                     ''', dependencies: sdl, args: '-Werror=undef')
1456    sdl = declare_dependency(compile_args: '-Wno-undef',
1457                             dependencies: sdl,
1458                             version: sdl.version())
1459  endif
1460  sdl_image = dependency('SDL2_image', required: get_option('sdl_image'),
1461                         method: 'pkg-config')
1462else
1463  if get_option('sdl_image').enabled()
1464    error('sdl-image required, but SDL was @0@'.format(
1465          get_option('sdl').disabled() ? 'disabled' : 'not found'))
1466  endif
1467  sdl_image = not_found
1468endif
1469
1470rbd = not_found
1471if not get_option('rbd').auto() or have_block
1472  librados = cc.find_library('rados', required: get_option('rbd'))
1473  librbd = cc.find_library('rbd', has_headers: ['rbd/librbd.h'],
1474                           required: get_option('rbd'))
1475  if librados.found() and librbd.found()
1476    if cc.links('''
1477      #include <stdio.h>
1478      #include <rbd/librbd.h>
1479      int main(void) {
1480        rados_t cluster;
1481        rados_create(&cluster, NULL);
1482        #if LIBRBD_VERSION_CODE < LIBRBD_VERSION(1, 12, 0)
1483        #error
1484        #endif
1485        return 0;
1486      }''', dependencies: [librbd, librados])
1487      rbd = declare_dependency(dependencies: [librbd, librados])
1488    elif get_option('rbd').enabled()
1489      error('librbd >= 1.12.0 required')
1490    else
1491      warning('librbd >= 1.12.0 not found, disabling')
1492    endif
1493  endif
1494endif
1495
1496glusterfs = not_found
1497glusterfs_ftruncate_has_stat = false
1498glusterfs_iocb_has_stat = false
1499if not get_option('glusterfs').auto() or have_block
1500  glusterfs = dependency('glusterfs-api', version: '>=3',
1501                         required: get_option('glusterfs'),
1502                         method: 'pkg-config')
1503  if glusterfs.found()
1504    glusterfs_ftruncate_has_stat = cc.links('''
1505      #include <glusterfs/api/glfs.h>
1506
1507      int
1508      main(void)
1509      {
1510          /* new glfs_ftruncate() passes two additional args */
1511          return glfs_ftruncate(NULL, 0, NULL, NULL);
1512      }
1513    ''', dependencies: glusterfs)
1514    glusterfs_iocb_has_stat = cc.links('''
1515      #include <glusterfs/api/glfs.h>
1516
1517      /* new glfs_io_cbk() passes two additional glfs_stat structs */
1518      static void
1519      glusterfs_iocb(glfs_fd_t *fd, ssize_t ret, struct glfs_stat *prestat, struct glfs_stat *poststat, void *data)
1520      {}
1521
1522      int
1523      main(void)
1524      {
1525          glfs_io_cbk iocb = &glusterfs_iocb;
1526          iocb(NULL, 0 , NULL, NULL, NULL);
1527          return 0;
1528      }
1529    ''', dependencies: glusterfs)
1530  endif
1531endif
1532
1533hv_balloon = false
1534if get_option('hv_balloon').allowed() and have_system
1535  if cc.links('''
1536    #include <string.h>
1537    #include <gmodule.h>
1538    int main(void) {
1539        GTree *tree;
1540
1541        tree = g_tree_new((GCompareFunc)strcmp);
1542        (void)g_tree_node_first(tree);
1543        g_tree_destroy(tree);
1544        return 0;
1545    }
1546  ''', dependencies: glib)
1547    hv_balloon = true
1548  else
1549    if get_option('hv_balloon').enabled()
1550      error('could not enable hv-balloon, update your glib')
1551    else
1552      warning('could not find glib support for hv-balloon, disabling')
1553    endif
1554  endif
1555endif
1556
1557libssh = not_found
1558if not get_option('libssh').auto() or have_block
1559  libssh = dependency('libssh', version: '>=0.8.7',
1560                    method: 'pkg-config',
1561                    required: get_option('libssh'))
1562endif
1563
1564libbzip2 = not_found
1565if not get_option('bzip2').auto() or have_block
1566  libbzip2 = cc.find_library('bz2', has_headers: ['bzlib.h'],
1567                             required: get_option('bzip2'))
1568  if libbzip2.found() and not cc.links('''
1569     #include <bzlib.h>
1570     int main(void) { BZ2_bzlibVersion(); return 0; }''', dependencies: libbzip2)
1571    libbzip2 = not_found
1572    if get_option('bzip2').enabled()
1573      error('could not link libbzip2')
1574    else
1575      warning('could not link libbzip2, disabling')
1576    endif
1577  endif
1578endif
1579
1580liblzfse = not_found
1581if not get_option('lzfse').auto() or have_block
1582  liblzfse = cc.find_library('lzfse', has_headers: ['lzfse.h'],
1583                             required: get_option('lzfse'))
1584endif
1585if liblzfse.found() and not cc.links('''
1586   #include <lzfse.h>
1587   int main(void) { lzfse_decode_scratch_size(); return 0; }''', dependencies: liblzfse)
1588  liblzfse = not_found
1589  if get_option('lzfse').enabled()
1590    error('could not link liblzfse')
1591  else
1592    warning('could not link liblzfse, disabling')
1593  endif
1594endif
1595
1596oss = not_found
1597if get_option('oss').allowed() and have_system
1598  if not cc.has_header('sys/soundcard.h')
1599    # not found
1600  elif host_os == 'netbsd'
1601    oss = cc.find_library('ossaudio', required: get_option('oss'))
1602  else
1603    oss = declare_dependency()
1604  endif
1605
1606  if not oss.found()
1607    if get_option('oss').enabled()
1608      error('OSS not found')
1609    endif
1610  endif
1611endif
1612dsound = not_found
1613if not get_option('dsound').auto() or (host_os == 'windows' and have_system)
1614  if cc.has_header('dsound.h')
1615    dsound = declare_dependency(link_args: ['-lole32', '-ldxguid'])
1616  endif
1617
1618  if not dsound.found()
1619    if get_option('dsound').enabled()
1620      error('DirectSound not found')
1621    endif
1622  endif
1623endif
1624
1625coreaudio = not_found
1626if not get_option('coreaudio').auto() or (host_os == 'darwin' and have_system)
1627  coreaudio = dependency('appleframeworks', modules: 'CoreAudio',
1628                         required: get_option('coreaudio'))
1629endif
1630
1631opengl = not_found
1632if not get_option('opengl').auto() or have_system or have_vhost_user_gpu
1633  epoxy = dependency('epoxy', method: 'pkg-config',
1634                      required: get_option('opengl'))
1635  if cc.has_header('epoxy/egl.h', dependencies: epoxy)
1636    opengl = epoxy
1637  elif get_option('opengl').enabled()
1638    error('epoxy/egl.h not found')
1639  endif
1640endif
1641gbm = not_found
1642if (have_system or have_tools) and (virgl.found() or opengl.found())
1643  gbm = dependency('gbm', method: 'pkg-config', required: false)
1644endif
1645have_vhost_user_gpu = have_vhost_user_gpu and virgl.found() and opengl.found() and gbm.found()
1646
1647gnutls = not_found
1648gnutls_crypto = not_found
1649if get_option('gnutls').enabled() or (get_option('gnutls').auto() and have_system)
1650  # For general TLS support our min gnutls matches
1651  # that implied by our platform support matrix
1652  #
1653  # For the crypto backends, we look for a newer
1654  # gnutls:
1655  #
1656  #   Version 3.6.8  is needed to get XTS
1657  #   Version 3.6.13 is needed to get PBKDF
1658  #   Version 3.6.14 is needed to get HW accelerated XTS
1659  #
1660  # If newer enough gnutls isn't available, we can
1661  # still use a different crypto backend to satisfy
1662  # the platform support requirements
1663  gnutls_crypto = dependency('gnutls', version: '>=3.6.14',
1664                             method: 'pkg-config',
1665                             required: false)
1666  if gnutls_crypto.found()
1667    gnutls = gnutls_crypto
1668  else
1669    # Our min version if all we need is TLS
1670    gnutls = dependency('gnutls', version: '>=3.5.18',
1671                        method: 'pkg-config',
1672                        required: get_option('gnutls'))
1673  endif
1674endif
1675
1676# We prefer use of gnutls for crypto, unless the options
1677# explicitly asked for nettle or gcrypt.
1678#
1679# If gnutls isn't available for crypto, then we'll prefer
1680# gcrypt over nettle for performance reasons.
1681gcrypt = not_found
1682nettle = not_found
1683hogweed = not_found
1684crypto_sm4 = not_found
1685xts = 'none'
1686
1687if get_option('nettle').enabled() and get_option('gcrypt').enabled()
1688  error('Only one of gcrypt & nettle can be enabled')
1689endif
1690
1691# Explicit nettle/gcrypt request, so ignore gnutls for crypto
1692if get_option('nettle').enabled() or get_option('gcrypt').enabled()
1693  gnutls_crypto = not_found
1694endif
1695
1696if not gnutls_crypto.found()
1697  if (not get_option('gcrypt').auto() or have_system) and not get_option('nettle').enabled()
1698    gcrypt = dependency('libgcrypt', version: '>=1.8',
1699                        required: get_option('gcrypt'))
1700    # Debian has removed -lgpg-error from libgcrypt-config
1701    # as it "spreads unnecessary dependencies" which in
1702    # turn breaks static builds...
1703    if gcrypt.found() and get_option('prefer_static')
1704      gcrypt = declare_dependency(dependencies:
1705        [gcrypt,
1706         cc.find_library('gpg-error', required: true)],
1707        version: gcrypt.version())
1708    endif
1709    crypto_sm4 = gcrypt
1710    # SM4 ALG is available in libgcrypt >= 1.9
1711    if gcrypt.found() and not cc.links('''
1712      #include <gcrypt.h>
1713      int main(void) {
1714        gcry_cipher_hd_t handler;
1715        gcry_cipher_open(&handler, GCRY_CIPHER_SM4, GCRY_CIPHER_MODE_ECB, 0);
1716        return 0;
1717      }''', dependencies: gcrypt)
1718      crypto_sm4 = not_found
1719    endif
1720  endif
1721  if (not get_option('nettle').auto() or have_system) and not gcrypt.found()
1722    nettle = dependency('nettle', version: '>=3.4',
1723                        method: 'pkg-config',
1724                        required: get_option('nettle'))
1725    if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle)
1726      xts = 'private'
1727    endif
1728    crypto_sm4 = nettle
1729    # SM4 ALG is available in nettle >= 3.9
1730    if nettle.found() and not cc.links('''
1731      #include <nettle/sm4.h>
1732      int main(void) {
1733        struct sm4_ctx ctx;
1734        unsigned char key[16] = {0};
1735        sm4_set_encrypt_key(&ctx, key);
1736        return 0;
1737      }''', dependencies: nettle)
1738      crypto_sm4 = not_found
1739    endif
1740  endif
1741endif
1742
1743capstone = not_found
1744if not get_option('capstone').auto() or have_system or have_user
1745  capstone = dependency('capstone', version: '>=3.0.5',
1746                        method: 'pkg-config',
1747                        required: get_option('capstone'))
1748
1749  # Some versions of capstone have broken pkg-config file
1750  # that reports a wrong -I path, causing the #include to
1751  # fail later. If the system has such a broken version
1752  # do not use it.
1753  if capstone.found() and not cc.compiles('#include <capstone.h>',
1754                                          dependencies: [capstone])
1755    capstone = not_found
1756    if get_option('capstone').enabled()
1757      error('capstone requested, but it does not appear to work')
1758    endif
1759  endif
1760endif
1761
1762gmp = dependency('gmp', required: false, method: 'pkg-config')
1763if nettle.found() and gmp.found()
1764  hogweed = dependency('hogweed', version: '>=3.4',
1765                       method: 'pkg-config',
1766                       required: get_option('nettle'))
1767endif
1768
1769
1770gtk = not_found
1771gtkx11 = not_found
1772vte = not_found
1773have_gtk_clipboard = get_option('gtk_clipboard').enabled()
1774
1775if get_option('gtk') \
1776             .disable_auto_if(not have_system) \
1777             .require(pixman.found(),
1778                      error_message: 'cannot enable GTK if pixman is not available') \
1779             .allowed()
1780  gtk = dependency('gtk+-3.0', version: '>=3.22.0',
1781                   method: 'pkg-config',
1782                   required: get_option('gtk'))
1783  if gtk.found()
1784    gtkx11 = dependency('gtk+-x11-3.0', version: '>=3.22.0',
1785                        method: 'pkg-config',
1786                        required: false)
1787    gtk = declare_dependency(dependencies: [gtk, gtkx11],
1788                             version: gtk.version())
1789
1790    if not get_option('vte').auto() or have_system
1791      vte = dependency('vte-2.91',
1792                       method: 'pkg-config',
1793                       required: get_option('vte'))
1794    endif
1795  elif have_gtk_clipboard
1796    error('GTK clipboard requested, but GTK not found')
1797  endif
1798endif
1799
1800x11 = not_found
1801if gtkx11.found()
1802  x11 = dependency('x11', method: 'pkg-config', required: gtkx11.found())
1803endif
1804png = not_found
1805if get_option('png').allowed() and have_system
1806   png = dependency('libpng', version: '>=1.6.34', required: get_option('png'),
1807                    method: 'pkg-config')
1808endif
1809vnc = not_found
1810jpeg = not_found
1811sasl = not_found
1812if get_option('vnc') \
1813             .disable_auto_if(not have_system) \
1814             .require(pixman.found(),
1815                      error_message: 'cannot enable VNC if pixman is not available') \
1816             .allowed()
1817  vnc = declare_dependency() # dummy dependency
1818  jpeg = dependency('libjpeg', required: get_option('vnc_jpeg'),
1819                    method: 'pkg-config')
1820  sasl = cc.find_library('sasl2', has_headers: ['sasl/sasl.h'],
1821                         required: get_option('vnc_sasl'))
1822  if sasl.found()
1823    sasl = declare_dependency(dependencies: sasl,
1824                              compile_args: '-DSTRUCT_IOVEC_DEFINED')
1825  endif
1826endif
1827
1828pam = not_found
1829if not get_option('auth_pam').auto() or have_system
1830  pam = cc.find_library('pam', has_headers: ['security/pam_appl.h'],
1831                        required: get_option('auth_pam'))
1832endif
1833if pam.found() and not cc.links('''
1834   #include <stddef.h>
1835   #include <security/pam_appl.h>
1836   int main(void) {
1837     const char *service_name = "qemu";
1838     const char *user = "frank";
1839     const struct pam_conv pam_conv = { 0 };
1840     pam_handle_t *pamh = NULL;
1841     pam_start(service_name, user, &pam_conv, &pamh);
1842     return 0;
1843   }''', dependencies: pam)
1844  pam = not_found
1845  if get_option('auth_pam').enabled()
1846    error('could not link libpam')
1847  else
1848    warning('could not link libpam, disabling')
1849  endif
1850endif
1851
1852snappy = not_found
1853if not get_option('snappy').auto() or have_system
1854  snappy = cc.find_library('snappy', has_headers: ['snappy-c.h'],
1855                           required: get_option('snappy'))
1856endif
1857if snappy.found() and not cc.links('''
1858   #include <snappy-c.h>
1859   int main(void) { snappy_max_compressed_length(4096); return 0; }''', dependencies: snappy)
1860  snappy = not_found
1861  if get_option('snappy').enabled()
1862    error('could not link libsnappy')
1863  else
1864    warning('could not link libsnappy, disabling')
1865  endif
1866endif
1867
1868lzo = not_found
1869if not get_option('lzo').auto() or have_system
1870  lzo = cc.find_library('lzo2', has_headers: ['lzo/lzo1x.h'],
1871                        required: get_option('lzo'))
1872endif
1873if lzo.found() and not cc.links('''
1874   #include <lzo/lzo1x.h>
1875   int main(void) { lzo_version(); return 0; }''', dependencies: lzo)
1876  lzo = not_found
1877  if get_option('lzo').enabled()
1878    error('could not link liblzo2')
1879  else
1880    warning('could not link liblzo2, disabling')
1881  endif
1882endif
1883
1884numa = not_found
1885if not get_option('numa').auto() or have_system or have_tools
1886  numa = cc.find_library('numa', has_headers: ['numa.h'],
1887                              required: get_option('numa'))
1888endif
1889if numa.found() and not cc.links('''
1890   #include <numa.h>
1891   int main(void) { return numa_available(); }
1892   ''', dependencies: numa)
1893  numa = not_found
1894  if get_option('numa').enabled()
1895    error('could not link numa')
1896  else
1897    warning('could not link numa, disabling')
1898  endif
1899endif
1900
1901fdt = not_found
1902fdt_opt = get_option('fdt')
1903if fdt_opt == 'enabled' and get_option('wrap_mode') == 'nodownload'
1904  fdt_opt = 'system'
1905endif
1906if fdt_opt in ['enabled', 'system'] or (fdt_opt == 'auto' and have_system)
1907  fdt = cc.find_library('fdt', required: fdt_opt == 'system')
1908  if fdt.found() and cc.links('''
1909     #include <libfdt.h>
1910     #include <libfdt_env.h>
1911     int main(void) { fdt_find_max_phandle(NULL, NULL); return 0; }''',
1912       dependencies: fdt)
1913    fdt_opt = 'system'
1914  elif fdt_opt != 'system'
1915    fdt_opt = get_option('wrap_mode') == 'nodownload' ? 'disabled' : 'internal'
1916    fdt = not_found
1917  else
1918    error('system libfdt is too old (1.5.1 or newer required)')
1919  endif
1920endif
1921if fdt_opt == 'internal'
1922  assert(not fdt.found())
1923  libfdt_proj = subproject('dtc', required: true,
1924                           default_options: ['tools=false',  'yaml=disabled',
1925                                             'python=disabled', 'default_library=static'])
1926  fdt = libfdt_proj.get_variable('libfdt_dep')
1927endif
1928
1929rdma = not_found
1930if not get_option('rdma').auto() or have_system
1931  rdma_libs = [cc.find_library('rdmacm', has_headers: ['rdma/rdma_cma.h'],
1932                               required: get_option('rdma')),
1933               cc.find_library('ibverbs', required: get_option('rdma'))]
1934  rdma = declare_dependency(dependencies: rdma_libs)
1935  foreach lib: rdma_libs
1936    if not lib.found()
1937      rdma = not_found
1938    endif
1939  endforeach
1940endif
1941
1942cacard = not_found
1943if not get_option('smartcard').auto() or have_system
1944  cacard = dependency('libcacard', required: get_option('smartcard'),
1945                      version: '>=2.5.1', method: 'pkg-config')
1946endif
1947u2f = not_found
1948if not get_option('u2f').auto() or have_system
1949  u2f = dependency('u2f-emu', required: get_option('u2f'),
1950                   method: 'pkg-config')
1951endif
1952canokey = not_found
1953if not get_option('canokey').auto() or have_system
1954  canokey = dependency('canokey-qemu', required: get_option('canokey'),
1955                   method: 'pkg-config')
1956endif
1957usbredir = not_found
1958if not get_option('usb_redir').auto() or have_system
1959  usbredir = dependency('libusbredirparser-0.5', required: get_option('usb_redir'),
1960                        version: '>=0.6', method: 'pkg-config')
1961endif
1962libusb = not_found
1963if not get_option('libusb').auto() or have_system
1964  libusb = dependency('libusb-1.0', required: get_option('libusb'),
1965                      version: '>=1.0.13', method: 'pkg-config')
1966endif
1967
1968libpmem = not_found
1969if not get_option('libpmem').auto() or have_system
1970  libpmem = dependency('libpmem', required: get_option('libpmem'),
1971                       method: 'pkg-config')
1972endif
1973libdaxctl = not_found
1974if not get_option('libdaxctl').auto() or have_system
1975  libdaxctl = dependency('libdaxctl', required: get_option('libdaxctl'),
1976                         version: '>=57', method: 'pkg-config')
1977endif
1978tasn1 = not_found
1979if gnutls.found()
1980  tasn1 = dependency('libtasn1',
1981                     required: false,
1982                     method: 'pkg-config')
1983endif
1984keyutils = not_found
1985if not get_option('libkeyutils').auto() or have_block
1986  keyutils = dependency('libkeyutils', required: get_option('libkeyutils'),
1987                        method: 'pkg-config')
1988endif
1989
1990has_gettid = cc.has_function('gettid')
1991
1992# libselinux
1993selinux = dependency('libselinux',
1994                     required: get_option('selinux'),
1995                     method: 'pkg-config')
1996
1997# Malloc tests
1998
1999malloc = []
2000if get_option('malloc') == 'system'
2001  has_malloc_trim = \
2002    get_option('malloc_trim').allowed() and \
2003    cc.has_function('malloc_trim', prefix: '#include <malloc.h>')
2004else
2005  has_malloc_trim = false
2006  malloc = cc.find_library(get_option('malloc'), required: true)
2007endif
2008if not has_malloc_trim and get_option('malloc_trim').enabled()
2009  if get_option('malloc') == 'system'
2010    error('malloc_trim not available on this platform.')
2011  else
2012    error('malloc_trim not available with non-libc memory allocator')
2013  endif
2014endif
2015
2016gnu_source_prefix = '''
2017  #ifndef _GNU_SOURCE
2018  #define _GNU_SOURCE
2019  #endif
2020'''
2021
2022# Check whether the glibc provides STATX_BASIC_STATS
2023
2024has_statx = cc.has_header_symbol('sys/stat.h', 'STATX_BASIC_STATS', prefix: gnu_source_prefix)
2025
2026# Check whether statx() provides mount ID information
2027
2028has_statx_mnt_id = cc.has_header_symbol('sys/stat.h', 'STATX_MNT_ID', prefix: gnu_source_prefix)
2029
2030have_vhost_user_blk_server = get_option('vhost_user_blk_server') \
2031  .require(host_os == 'linux',
2032           error_message: 'vhost_user_blk_server requires linux') \
2033  .require(have_vhost_user,
2034           error_message: 'vhost_user_blk_server requires vhost-user support') \
2035  .disable_auto_if(not have_tools and not have_system) \
2036  .allowed()
2037
2038if get_option('fuse').disabled() and get_option('fuse_lseek').enabled()
2039  error('Cannot enable fuse-lseek while fuse is disabled')
2040endif
2041
2042fuse = dependency('fuse3', required: get_option('fuse'),
2043                  version: '>=3.1', method: 'pkg-config')
2044
2045fuse_lseek = not_found
2046if get_option('fuse_lseek').allowed()
2047  if fuse.version().version_compare('>=3.8')
2048    # Dummy dependency
2049    fuse_lseek = declare_dependency()
2050  elif get_option('fuse_lseek').enabled()
2051    if fuse.found()
2052      error('fuse-lseek requires libfuse >=3.8, found ' + fuse.version())
2053    else
2054      error('fuse-lseek requires libfuse, which was not found')
2055    endif
2056  endif
2057endif
2058
2059have_libvduse = (host_os == 'linux')
2060if get_option('libvduse').enabled()
2061    if host_os != 'linux'
2062        error('libvduse requires linux')
2063    endif
2064elif get_option('libvduse').disabled()
2065    have_libvduse = false
2066endif
2067
2068have_vduse_blk_export = (have_libvduse and host_os == 'linux')
2069if get_option('vduse_blk_export').enabled()
2070    if host_os != 'linux'
2071        error('vduse_blk_export requires linux')
2072    elif not have_libvduse
2073        error('vduse_blk_export requires libvduse support')
2074    endif
2075elif get_option('vduse_blk_export').disabled()
2076    have_vduse_blk_export = false
2077endif
2078
2079# libbpf
2080bpf_version = '1.1.0'
2081libbpf = dependency('libbpf', version: '>=' + bpf_version, required: get_option('bpf'), method: 'pkg-config')
2082if libbpf.found() and not cc.links('''
2083   #include <bpf/libbpf.h>
2084   #include <linux/bpf.h>
2085   int main(void)
2086   {
2087     // check flag availability
2088     int flag = BPF_F_MMAPABLE;
2089     bpf_object__destroy_skeleton(NULL);
2090     return 0;
2091   }''', dependencies: libbpf)
2092  libbpf = not_found
2093  if get_option('bpf').enabled()
2094    error('libbpf skeleton/mmaping test failed')
2095  else
2096    warning('libbpf skeleton/mmaping test failed, disabling')
2097  endif
2098endif
2099
2100# libxdp
2101libxdp = not_found
2102if not get_option('af_xdp').auto() or have_system
2103    libxdp = dependency('libxdp', required: get_option('af_xdp'),
2104                        version: '>=1.4.0', method: 'pkg-config')
2105endif
2106
2107# libdw
2108libdw = not_found
2109if not get_option('libdw').auto() or \
2110        (not get_option('prefer_static') and (have_system or have_user))
2111    libdw = dependency('libdw',
2112                       method: 'pkg-config',
2113                       required: get_option('libdw'))
2114endif
2115
2116#################
2117# config-host.h #
2118#################
2119
2120config_host_data = configuration_data()
2121
2122audio_drivers_selected = []
2123if have_system
2124  audio_drivers_available = {
2125    'alsa': alsa.found(),
2126    'coreaudio': coreaudio.found(),
2127    'dsound': dsound.found(),
2128    'jack': jack.found(),
2129    'oss': oss.found(),
2130    'pa': pulse.found(),
2131    'pipewire': pipewire.found(),
2132    'sdl': sdl.found(),
2133    'sndio': sndio.found(),
2134  }
2135  foreach k, v: audio_drivers_available
2136    config_host_data.set('CONFIG_AUDIO_' + k.to_upper(), v)
2137  endforeach
2138
2139  # Default to native drivers first, OSS second, SDL third
2140  audio_drivers_priority = \
2141    [ 'pa', 'coreaudio', 'dsound', 'sndio', 'oss' ] + \
2142    (host_os == 'linux' ? [] : [ 'sdl' ])
2143  audio_drivers_default = []
2144  foreach k: audio_drivers_priority
2145    if audio_drivers_available[k]
2146      audio_drivers_default += k
2147    endif
2148  endforeach
2149
2150  foreach k: get_option('audio_drv_list')
2151    if k == 'default'
2152      audio_drivers_selected += audio_drivers_default
2153    elif not audio_drivers_available[k]
2154      error('Audio driver "@0@" not available.'.format(k))
2155    else
2156      audio_drivers_selected += k
2157    endif
2158  endforeach
2159endif
2160config_host_data.set('CONFIG_AUDIO_DRIVERS',
2161                     '"' + '", "'.join(audio_drivers_selected) + '", ')
2162
2163have_host_block_device = (host_os != 'darwin' or
2164    cc.has_header('IOKit/storage/IOMedia.h'))
2165
2166dbus_display = get_option('dbus_display') \
2167  .require(gio.version().version_compare('>=2.64'),
2168           error_message: '-display dbus requires glib>=2.64') \
2169  .require(gdbus_codegen.found(),
2170           error_message: gdbus_codegen_error.format('-display dbus')) \
2171  .allowed()
2172
2173have_virtfs = get_option('virtfs') \
2174    .require(host_os == 'linux' or host_os == 'darwin',
2175             error_message: 'virtio-9p (virtfs) requires Linux or macOS') \
2176    .require(host_os == 'linux' or cc.has_function('pthread_fchdir_np'),
2177             error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \
2178    .require(host_os == 'darwin' or libattr.found(),
2179             error_message: 'virtio-9p (virtfs) on Linux requires libattr-devel') \
2180    .disable_auto_if(not have_tools and not have_system) \
2181    .allowed()
2182
2183have_virtfs_proxy_helper = get_option('virtfs_proxy_helper') \
2184    .require(host_os != 'darwin', error_message: 'the virtfs proxy helper is incompatible with macOS') \
2185    .require(have_virtfs, error_message: 'the virtfs proxy helper requires that virtfs is enabled') \
2186    .disable_auto_if(not have_tools) \
2187    .require(libcap_ng.found(), error_message: 'the virtfs proxy helper requires libcap-ng') \
2188    .allowed()
2189
2190qga_fsfreeze = false
2191qga_fstrim = false
2192if host_os == 'linux'
2193    if cc.has_header_symbol('linux/fs.h', 'FIFREEZE')
2194        qga_fsfreeze = true
2195    endif
2196    if cc.has_header_symbol('linux/fs.h', 'FITRIM')
2197        qga_fstrim = true
2198    endif
2199elif host_os == 'freebsd' and cc.has_header_symbol('ufs/ffs/fs.h', 'UFSSUSPEND')
2200    qga_fsfreeze = true
2201endif
2202
2203if get_option('block_drv_ro_whitelist') == ''
2204  config_host_data.set('CONFIG_BDRV_RO_WHITELIST', '')
2205else
2206  config_host_data.set('CONFIG_BDRV_RO_WHITELIST',
2207        '"' + get_option('block_drv_ro_whitelist').replace(',', '", "') + '", ')
2208endif
2209if get_option('block_drv_rw_whitelist') == ''
2210  config_host_data.set('CONFIG_BDRV_RW_WHITELIST', '')
2211else
2212  config_host_data.set('CONFIG_BDRV_RW_WHITELIST',
2213        '"' + get_option('block_drv_rw_whitelist').replace(',', '", "') + '", ')
2214endif
2215
2216foreach k : get_option('trace_backends')
2217  config_host_data.set('CONFIG_TRACE_' + k.to_upper(), true)
2218endforeach
2219config_host_data.set_quoted('CONFIG_TRACE_FILE', get_option('trace_file'))
2220config_host_data.set_quoted('CONFIG_TLS_PRIORITY', get_option('tls_priority'))
2221if iasl.found()
2222  config_host_data.set_quoted('CONFIG_IASL', iasl.full_path())
2223endif
2224config_host_data.set_quoted('CONFIG_BINDIR', get_option('prefix') / get_option('bindir'))
2225config_host_data.set_quoted('CONFIG_PREFIX', get_option('prefix'))
2226config_host_data.set_quoted('CONFIG_QEMU_CONFDIR', get_option('prefix') / qemu_confdir)
2227config_host_data.set_quoted('CONFIG_QEMU_DATADIR', get_option('prefix') / qemu_datadir)
2228config_host_data.set_quoted('CONFIG_QEMU_DESKTOPDIR', get_option('prefix') / qemu_desktopdir)
2229
2230qemu_firmwarepath = ''
2231foreach k : get_option('qemu_firmwarepath')
2232  qemu_firmwarepath += '"' + get_option('prefix') / k + '", '
2233endforeach
2234config_host_data.set('CONFIG_QEMU_FIRMWAREPATH', qemu_firmwarepath)
2235
2236config_host_data.set_quoted('CONFIG_QEMU_HELPERDIR', get_option('prefix') / get_option('libexecdir'))
2237config_host_data.set_quoted('CONFIG_QEMU_ICONDIR', get_option('prefix') / qemu_icondir)
2238config_host_data.set_quoted('CONFIG_QEMU_LOCALEDIR', get_option('prefix') / get_option('localedir'))
2239config_host_data.set_quoted('CONFIG_QEMU_LOCALSTATEDIR', get_option('prefix') / get_option('localstatedir'))
2240config_host_data.set_quoted('CONFIG_QEMU_MODDIR', get_option('prefix') / qemu_moddir)
2241config_host_data.set_quoted('CONFIG_SYSCONFDIR', get_option('prefix') / get_option('sysconfdir'))
2242
2243if enable_modules
2244  config_host_data.set('CONFIG_STAMP', run_command(
2245      meson.current_source_dir() / 'scripts/qemu-stamp.py',
2246      meson.project_version(), get_option('pkgversion'), '--',
2247      meson.current_source_dir() / 'configure',
2248      capture: true, check: true).stdout().strip())
2249endif
2250
2251have_slirp_smbd = get_option('slirp_smbd') \
2252  .require(host_os != 'windows', error_message: 'Host smbd not supported on this platform.') \
2253  .allowed()
2254if have_slirp_smbd
2255  smbd_path = get_option('smbd')
2256  if smbd_path == ''
2257    smbd_path = (host_os == 'sunos' ? '/usr/sfw/sbin/smbd' : '/usr/sbin/smbd')
2258  endif
2259  config_host_data.set_quoted('CONFIG_SMBD_COMMAND', smbd_path)
2260endif
2261
2262config_host_data.set('HOST_' + host_arch.to_upper(), 1)
2263
2264kvm_targets_c = '""'
2265if get_option('kvm').allowed() and host_os == 'linux'
2266  kvm_targets_c = '"' + '" ,"'.join(kvm_targets) + '"'
2267endif
2268config_host_data.set('CONFIG_KVM_TARGETS', kvm_targets_c)
2269
2270if get_option('module_upgrades') and not enable_modules
2271  error('Cannot enable module-upgrades as modules are not enabled')
2272endif
2273config_host_data.set('CONFIG_MODULE_UPGRADES', get_option('module_upgrades'))
2274
2275config_host_data.set('CONFIG_ATTR', libattr.found())
2276config_host_data.set('CONFIG_BDRV_WHITELIST_TOOLS', get_option('block_drv_whitelist_in_tools'))
2277config_host_data.set('CONFIG_BRLAPI', brlapi.found())
2278config_host_data.set('CONFIG_BSD', host_os in bsd_oses)
2279config_host_data.set('CONFIG_FREEBSD', host_os == 'freebsd')
2280config_host_data.set('CONFIG_CAPSTONE', capstone.found())
2281config_host_data.set('CONFIG_COCOA', cocoa.found())
2282config_host_data.set('CONFIG_DARWIN', host_os == 'darwin')
2283config_host_data.set('CONFIG_FDT', fdt.found())
2284config_host_data.set('CONFIG_FUZZ', get_option('fuzzing'))
2285config_host_data.set('CONFIG_GCOV', get_option('b_coverage'))
2286config_host_data.set('CONFIG_LIBUDEV', libudev.found())
2287config_host_data.set('CONFIG_LINUX', host_os == 'linux')
2288config_host_data.set('CONFIG_POSIX', host_os != 'windows')
2289config_host_data.set('CONFIG_WIN32', host_os == 'windows')
2290config_host_data.set('CONFIG_LZO', lzo.found())
2291config_host_data.set('CONFIG_MPATH', mpathpersist.found())
2292config_host_data.set('CONFIG_BLKIO', blkio.found())
2293if blkio.found()
2294  config_host_data.set('CONFIG_BLKIO_VHOST_VDPA_FD',
2295                       blkio.version().version_compare('>=1.3.0'))
2296endif
2297config_host_data.set('CONFIG_CURL', curl.found())
2298config_host_data.set('CONFIG_CURSES', curses.found())
2299config_host_data.set('CONFIG_GBM', gbm.found())
2300config_host_data.set('CONFIG_GIO', gio.found())
2301config_host_data.set('CONFIG_GLUSTERFS', glusterfs.found())
2302if glusterfs.found()
2303  config_host_data.set('CONFIG_GLUSTERFS_XLATOR_OPT', glusterfs.version().version_compare('>=4'))
2304  config_host_data.set('CONFIG_GLUSTERFS_DISCARD', glusterfs.version().version_compare('>=5'))
2305  config_host_data.set('CONFIG_GLUSTERFS_FALLOCATE', glusterfs.version().version_compare('>=6'))
2306  config_host_data.set('CONFIG_GLUSTERFS_ZEROFILL', glusterfs.version().version_compare('>=6'))
2307  config_host_data.set('CONFIG_GLUSTERFS_FTRUNCATE_HAS_STAT', glusterfs_ftruncate_has_stat)
2308  config_host_data.set('CONFIG_GLUSTERFS_IOCB_HAS_STAT', glusterfs_iocb_has_stat)
2309endif
2310config_host_data.set('CONFIG_GTK', gtk.found())
2311config_host_data.set('CONFIG_VTE', vte.found())
2312config_host_data.set('CONFIG_GTK_CLIPBOARD', have_gtk_clipboard)
2313config_host_data.set('CONFIG_HEXAGON_IDEF_PARSER', get_option('hexagon_idef_parser'))
2314config_host_data.set('CONFIG_LIBATTR', have_old_libattr)
2315config_host_data.set('CONFIG_LIBCAP_NG', libcap_ng.found())
2316config_host_data.set('CONFIG_EBPF', libbpf.found())
2317config_host_data.set('CONFIG_AF_XDP', libxdp.found())
2318config_host_data.set('CONFIG_LIBDAXCTL', libdaxctl.found())
2319config_host_data.set('CONFIG_LIBISCSI', libiscsi.found())
2320config_host_data.set('CONFIG_LIBNFS', libnfs.found())
2321config_host_data.set('CONFIG_LIBSSH', libssh.found())
2322config_host_data.set('CONFIG_LINUX_AIO', libaio.found())
2323config_host_data.set('CONFIG_LINUX_IO_URING', linux_io_uring.found())
2324config_host_data.set('CONFIG_LIBPMEM', libpmem.found())
2325config_host_data.set('CONFIG_MODULES', enable_modules)
2326config_host_data.set('CONFIG_NUMA', numa.found())
2327if numa.found()
2328  config_host_data.set('HAVE_NUMA_HAS_PREFERRED_MANY',
2329                       cc.has_function('numa_has_preferred_many',
2330                                       dependencies: numa))
2331endif
2332config_host_data.set('CONFIG_OPENGL', opengl.found())
2333config_host_data.set('CONFIG_PLUGIN', get_option('plugins'))
2334config_host_data.set('CONFIG_RBD', rbd.found())
2335config_host_data.set('CONFIG_RDMA', rdma.found())
2336config_host_data.set('CONFIG_RELOCATABLE', get_option('relocatable'))
2337config_host_data.set('CONFIG_SAFESTACK', get_option('safe_stack'))
2338config_host_data.set('CONFIG_SDL', sdl.found())
2339config_host_data.set('CONFIG_SDL_IMAGE', sdl_image.found())
2340config_host_data.set('CONFIG_SECCOMP', seccomp.found())
2341if seccomp.found()
2342  config_host_data.set('CONFIG_SECCOMP_SYSRAWRC', seccomp_has_sysrawrc)
2343endif
2344config_host_data.set('CONFIG_PIXMAN', pixman.found())
2345config_host_data.set('CONFIG_SLIRP', slirp.found())
2346config_host_data.set('CONFIG_SNAPPY', snappy.found())
2347config_host_data.set('CONFIG_SOLARIS', host_os == 'sunos')
2348if get_option('tcg').allowed()
2349  config_host_data.set('CONFIG_TCG', 1)
2350  config_host_data.set('CONFIG_TCG_INTERPRETER', tcg_arch == 'tci')
2351endif
2352config_host_data.set('CONFIG_TPM', have_tpm)
2353config_host_data.set('CONFIG_TSAN', get_option('tsan'))
2354config_host_data.set('CONFIG_USB_LIBUSB', libusb.found())
2355config_host_data.set('CONFIG_VDE', vde.found())
2356config_host_data.set('CONFIG_VHOST', have_vhost)
2357config_host_data.set('CONFIG_VHOST_NET', have_vhost_net)
2358config_host_data.set('CONFIG_VHOST_NET_USER', have_vhost_net_user)
2359config_host_data.set('CONFIG_VHOST_NET_VDPA', have_vhost_net_vdpa)
2360config_host_data.set('CONFIG_VHOST_KERNEL', have_vhost_kernel)
2361config_host_data.set('CONFIG_VHOST_USER', have_vhost_user)
2362config_host_data.set('CONFIG_VHOST_CRYPTO', have_vhost_user_crypto)
2363config_host_data.set('CONFIG_VHOST_VDPA', have_vhost_vdpa)
2364config_host_data.set('CONFIG_VMNET', vmnet.found())
2365config_host_data.set('CONFIG_VHOST_USER_BLK_SERVER', have_vhost_user_blk_server)
2366config_host_data.set('CONFIG_VDUSE_BLK_EXPORT', have_vduse_blk_export)
2367config_host_data.set('CONFIG_PNG', png.found())
2368config_host_data.set('CONFIG_VNC', vnc.found())
2369config_host_data.set('CONFIG_VNC_JPEG', jpeg.found())
2370config_host_data.set('CONFIG_VNC_SASL', sasl.found())
2371if virgl.found()
2372  config_host_data.set('HAVE_VIRGL_D3D_INFO_EXT',
2373                       cc.has_member('struct virgl_renderer_resource_info_ext', 'd3d_tex2d',
2374                                     prefix: '#include <virglrenderer.h>',
2375                                     dependencies: virgl))
2376endif
2377config_host_data.set('CONFIG_VIRTFS', have_virtfs)
2378config_host_data.set('CONFIG_VTE', vte.found())
2379config_host_data.set('CONFIG_XKBCOMMON', xkbcommon.found())
2380config_host_data.set('CONFIG_KEYUTILS', keyutils.found())
2381config_host_data.set('CONFIG_GETTID', has_gettid)
2382config_host_data.set('CONFIG_GNUTLS', gnutls.found())
2383config_host_data.set('CONFIG_GNUTLS_CRYPTO', gnutls_crypto.found())
2384config_host_data.set('CONFIG_TASN1', tasn1.found())
2385config_host_data.set('CONFIG_GCRYPT', gcrypt.found())
2386config_host_data.set('CONFIG_NETTLE', nettle.found())
2387config_host_data.set('CONFIG_CRYPTO_SM4', crypto_sm4.found())
2388config_host_data.set('CONFIG_HOGWEED', hogweed.found())
2389config_host_data.set('CONFIG_QEMU_PRIVATE_XTS', xts == 'private')
2390config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim)
2391config_host_data.set('CONFIG_STATX', has_statx)
2392config_host_data.set('CONFIG_STATX_MNT_ID', has_statx_mnt_id)
2393config_host_data.set('CONFIG_ZSTD', zstd.found())
2394config_host_data.set('CONFIG_QPL', qpl.found())
2395config_host_data.set('CONFIG_UADK', uadk.found())
2396config_host_data.set('CONFIG_FUSE', fuse.found())
2397config_host_data.set('CONFIG_FUSE_LSEEK', fuse_lseek.found())
2398config_host_data.set('CONFIG_SPICE_PROTOCOL', spice_protocol.found())
2399if spice_protocol.found()
2400config_host_data.set('CONFIG_SPICE_PROTOCOL_MAJOR', spice_protocol.version().split('.')[0])
2401config_host_data.set('CONFIG_SPICE_PROTOCOL_MINOR', spice_protocol.version().split('.')[1])
2402config_host_data.set('CONFIG_SPICE_PROTOCOL_MICRO', spice_protocol.version().split('.')[2])
2403endif
2404config_host_data.set('CONFIG_SPICE', spice.found())
2405config_host_data.set('CONFIG_X11', x11.found())
2406config_host_data.set('CONFIG_DBUS_DISPLAY', dbus_display)
2407config_host_data.set('CONFIG_CFI', get_option('cfi'))
2408config_host_data.set('CONFIG_SELINUX', selinux.found())
2409config_host_data.set('CONFIG_XEN_BACKEND', xen.found())
2410config_host_data.set('CONFIG_LIBDW', libdw.found())
2411if xen.found()
2412  # protect from xen.version() having less than three components
2413  xen_version = xen.version().split('.') + ['0', '0']
2414  xen_ctrl_version = xen_version[0] + \
2415    ('0' + xen_version[1]).substring(-2) + \
2416    ('0' + xen_version[2]).substring(-2)
2417  config_host_data.set('CONFIG_XEN_CTRL_INTERFACE_VERSION', xen_ctrl_version)
2418endif
2419config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version()))
2420config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0])
2421config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1])
2422config_host_data.set('QEMU_VERSION_MICRO', meson.project_version().split('.')[2])
2423
2424config_host_data.set_quoted('CONFIG_HOST_DSOSUF', host_dsosuf)
2425config_host_data.set('HAVE_HOST_BLOCK_DEVICE', have_host_block_device)
2426
2427have_coroutine_pool = get_option('coroutine_pool')
2428if get_option('debug_stack_usage') and have_coroutine_pool
2429  message('Disabling coroutine pool to measure stack usage')
2430  have_coroutine_pool = false
2431endif
2432config_host_data.set('CONFIG_COROUTINE_POOL', have_coroutine_pool)
2433config_host_data.set('CONFIG_DEBUG_GRAPH_LOCK', get_option('debug_graph_lock'))
2434config_host_data.set('CONFIG_DEBUG_MUTEX', get_option('debug_mutex'))
2435config_host_data.set('CONFIG_DEBUG_STACK_USAGE', get_option('debug_stack_usage'))
2436config_host_data.set('CONFIG_DEBUG_TCG', get_option('debug_tcg'))
2437config_host_data.set('CONFIG_DEBUG_REMAP', get_option('debug_remap'))
2438config_host_data.set('CONFIG_QOM_CAST_DEBUG', get_option('qom_cast_debug'))
2439config_host_data.set('CONFIG_REPLICATION', get_option('replication').allowed())
2440config_host_data.set('CONFIG_FSFREEZE', qga_fsfreeze)
2441config_host_data.set('CONFIG_FSTRIM', qga_fstrim)
2442
2443# has_header
2444config_host_data.set('CONFIG_EPOLL', cc.has_header('sys/epoll.h'))
2445config_host_data.set('CONFIG_LINUX_MAGIC_H', cc.has_header('linux/magic.h'))
2446config_host_data.set('CONFIG_VALGRIND_H', cc.has_header('valgrind/valgrind.h'))
2447config_host_data.set('HAVE_BTRFS_H', cc.has_header('linux/btrfs.h'))
2448config_host_data.set('HAVE_DRM_H', cc.has_header('libdrm/drm.h'))
2449config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h'))
2450config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h'))
2451config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
2452config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h'))
2453if host_os == 'windows'
2454  config_host_data.set('HAVE_AFUNIX_H', cc.has_header('afunix.h'))
2455endif
2456
2457# has_function
2458config_host_data.set('CONFIG_CLOSE_RANGE', cc.has_function('close_range'))
2459config_host_data.set('CONFIG_ACCEPT4', cc.has_function('accept4'))
2460config_host_data.set('CONFIG_CLOCK_ADJTIME', cc.has_function('clock_adjtime'))
2461config_host_data.set('CONFIG_DUP3', cc.has_function('dup3'))
2462config_host_data.set('CONFIG_FALLOCATE', cc.has_function('fallocate'))
2463config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate'))
2464config_host_data.set('CONFIG_GETCPU', cc.has_function('getcpu', prefix: gnu_source_prefix))
2465config_host_data.set('CONFIG_SCHED_GETCPU', cc.has_function('sched_getcpu', prefix: '#include <sched.h>'))
2466# Note that we need to specify prefix: here to avoid incorrectly
2467# thinking that Windows has posix_memalign()
2468config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include <stdlib.h>'))
2469config_host_data.set('CONFIG_ALIGNED_MALLOC', cc.has_function('_aligned_malloc'))
2470config_host_data.set('CONFIG_VALLOC', cc.has_function('valloc'))
2471config_host_data.set('CONFIG_MEMALIGN', cc.has_function('memalign'))
2472config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll'))
2473config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
2474config_host_data.set('CONFIG_PTHREAD_FCHDIR_NP', cc.has_function('pthread_fchdir_np'))
2475config_host_data.set('CONFIG_SENDFILE', cc.has_function('sendfile'))
2476config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_function('unshare'))
2477config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs'))
2478config_host_data.set('CONFIG_SYNC_FILE_RANGE', cc.has_function('sync_file_range'))
2479config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create'))
2480config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))
2481config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))
2482config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', glib_has_gslice)
2483config_host_data.set('HAVE_OPENPTY', cc.has_function('openpty', dependencies: util))
2484config_host_data.set('HAVE_STRCHRNUL', cc.has_function('strchrnul'))
2485config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>'))
2486if rbd.found()
2487  config_host_data.set('HAVE_RBD_NAMESPACE_EXISTS',
2488                       cc.has_function('rbd_namespace_exists',
2489                                       dependencies: rbd,
2490                                       prefix: '#include <rbd/librbd.h>'))
2491endif
2492if rdma.found()
2493  config_host_data.set('HAVE_IBV_ADVISE_MR',
2494                       cc.has_function('ibv_advise_mr',
2495                                       dependencies: rdma,
2496                                       prefix: '#include <infiniband/verbs.h>'))
2497endif
2498
2499have_asan_fiber = false
2500if get_option('sanitizers') and \
2501   not cc.has_function('__sanitizer_start_switch_fiber',
2502                         args: '-fsanitize=address',
2503                         prefix: '#include <sanitizer/asan_interface.h>')
2504  warning('Missing ASAN due to missing fiber annotation interface')
2505  warning('Without code annotation, the report may be inferior.')
2506else
2507  have_asan_fiber = true
2508endif
2509config_host_data.set('CONFIG_ASAN_IFACE_FIBER', have_asan_fiber)
2510
2511have_inotify_init = cc.has_header_symbol('sys/inotify.h', 'inotify_init')
2512have_inotify_init1 = cc.has_header_symbol('sys/inotify.h', 'inotify_init1')
2513inotify = not_found
2514if (have_inotify_init or have_inotify_init1) and host_os == 'freebsd'
2515  # libinotify-kqueue
2516  inotify = cc.find_library('inotify')
2517  if have_inotify_init
2518    have_inotify_init = inotify.found()
2519  endif
2520  if have_inotify_init1
2521    have_inotify_init1 = inotify.found()
2522  endif
2523endif
2524config_host_data.set('CONFIG_INOTIFY', have_inotify_init)
2525config_host_data.set('CONFIG_INOTIFY1', have_inotify_init1)
2526
2527# has_header_symbol
2528config_host_data.set('CONFIG_BLKZONED',
2529                     cc.has_header_symbol('linux/blkzoned.h', 'BLKOPENZONE'))
2530config_host_data.set('CONFIG_EPOLL_CREATE1',
2531                     cc.has_header_symbol('sys/epoll.h', 'epoll_create1'))
2532config_host_data.set('CONFIG_FALLOCATE_PUNCH_HOLE',
2533                     cc.has_header_symbol('linux/falloc.h', 'FALLOC_FL_PUNCH_HOLE') and
2534                     cc.has_header_symbol('linux/falloc.h', 'FALLOC_FL_KEEP_SIZE'))
2535config_host_data.set('CONFIG_FALLOCATE_ZERO_RANGE',
2536                     cc.has_header_symbol('linux/falloc.h', 'FALLOC_FL_ZERO_RANGE'))
2537config_host_data.set('CONFIG_FIEMAP',
2538                     cc.has_header('linux/fiemap.h') and
2539                     cc.has_header_symbol('linux/fs.h', 'FS_IOC_FIEMAP'))
2540config_host_data.set('CONFIG_GETRANDOM',
2541                     cc.has_function('getrandom') and
2542                     cc.has_header_symbol('sys/random.h', 'GRND_NONBLOCK'))
2543config_host_data.set('CONFIG_PRCTL_PR_SET_TIMERSLACK',
2544                     cc.has_header_symbol('sys/prctl.h', 'PR_SET_TIMERSLACK'))
2545config_host_data.set('CONFIG_RTNETLINK',
2546                     cc.has_header_symbol('linux/rtnetlink.h', 'IFLA_PROTO_DOWN'))
2547config_host_data.set('CONFIG_SYSMACROS',
2548                     cc.has_header_symbol('sys/sysmacros.h', 'makedev'))
2549config_host_data.set('HAVE_OPTRESET',
2550                     cc.has_header_symbol('getopt.h', 'optreset'))
2551config_host_data.set('HAVE_IPPROTO_MPTCP',
2552                     cc.has_header_symbol('netinet/in.h', 'IPPROTO_MPTCP'))
2553
2554# has_member
2555config_host_data.set('HAVE_SIGEV_NOTIFY_THREAD_ID',
2556                     cc.has_member('struct sigevent', 'sigev_notify_thread_id',
2557                                   prefix: '#include <signal.h>'))
2558config_host_data.set('HAVE_STRUCT_STAT_ST_ATIM',
2559                     cc.has_member('struct stat', 'st_atim',
2560                                   prefix: '#include <sys/stat.h>'))
2561config_host_data.set('HAVE_BLK_ZONE_REP_CAPACITY',
2562                     cc.has_member('struct blk_zone', 'capacity',
2563                                   prefix: '#include <linux/blkzoned.h>'))
2564
2565# has_type
2566config_host_data.set('CONFIG_IOVEC',
2567                     cc.has_type('struct iovec',
2568                                 prefix: '#include <sys/uio.h>'))
2569config_host_data.set('HAVE_UTMPX',
2570                     cc.has_type('struct utmpx',
2571                                 prefix: '#include <utmpx.h>'))
2572
2573config_host_data.set('CONFIG_EVENTFD', cc.links('''
2574  #include <sys/eventfd.h>
2575  int main(void) { return eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); }'''))
2576config_host_data.set('CONFIG_FDATASYNC', cc.links(gnu_source_prefix + '''
2577  #include <unistd.h>
2578  int main(void) {
2579  #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
2580  return fdatasync(0);
2581  #else
2582  #error Not supported
2583  #endif
2584  }'''))
2585
2586has_madvise = cc.links(gnu_source_prefix + '''
2587  #include <sys/types.h>
2588  #include <sys/mman.h>
2589  #include <stddef.h>
2590  int main(void) { return madvise(NULL, 0, MADV_DONTNEED); }''')
2591missing_madvise_proto = false
2592if has_madvise
2593  # Some platforms (illumos and Solaris before Solaris 11) provide madvise()
2594  # but forget to prototype it. In this case, has_madvise will be true (the
2595  # test program links despite a compile warning). To detect the
2596  # missing-prototype case, we try again with a definitely-bogus prototype.
2597  # This will only compile if the system headers don't provide the prototype;
2598  # otherwise the conflicting prototypes will cause a compiler error.
2599  missing_madvise_proto = cc.links(gnu_source_prefix + '''
2600    #include <sys/types.h>
2601    #include <sys/mman.h>
2602    #include <stddef.h>
2603    extern int madvise(int);
2604    int main(void) { return madvise(0); }''')
2605endif
2606config_host_data.set('CONFIG_MADVISE', has_madvise)
2607config_host_data.set('HAVE_MADVISE_WITHOUT_PROTOTYPE', missing_madvise_proto)
2608
2609config_host_data.set('CONFIG_MEMFD', cc.links(gnu_source_prefix + '''
2610  #include <sys/mman.h>
2611  int main(void) { return memfd_create("foo", MFD_ALLOW_SEALING); }'''))
2612config_host_data.set('CONFIG_OPEN_BY_HANDLE', cc.links(gnu_source_prefix + '''
2613  #include <fcntl.h>
2614  #if !defined(AT_EMPTY_PATH)
2615  # error missing definition
2616  #else
2617  int main(void) { struct file_handle fh; return open_by_handle_at(0, &fh, 0); }
2618  #endif'''))
2619
2620# On Darwin posix_madvise() has the same return semantics as plain madvise(),
2621# i.e. errno is set and -1 is returned. That's not really how POSIX defines the
2622# function. On the flip side, it has madvise() which is preferred anyways.
2623if host_os != 'darwin'
2624  config_host_data.set('CONFIG_POSIX_MADVISE', cc.links(gnu_source_prefix + '''
2625    #include <sys/mman.h>
2626    #include <stddef.h>
2627    int main(void) { return posix_madvise(NULL, 0, POSIX_MADV_DONTNEED); }'''))
2628endif
2629
2630config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_W_TID', cc.links(gnu_source_prefix + '''
2631  #include <pthread.h>
2632
2633  static void *f(void *p) { return NULL; }
2634  int main(void)
2635  {
2636    pthread_t thread;
2637    pthread_create(&thread, 0, f, 0);
2638    pthread_setname_np(thread, "QEMU");
2639    return 0;
2640  }''', dependencies: threads))
2641config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_TID', cc.links(gnu_source_prefix + '''
2642  #include <pthread.h>
2643
2644  static void *f(void *p) { pthread_setname_np("QEMU"); return NULL; }
2645  int main(void)
2646  {
2647    pthread_t thread;
2648    pthread_create(&thread, 0, f, 0);
2649    return 0;
2650  }''', dependencies: threads))
2651config_host_data.set('CONFIG_PTHREAD_SET_NAME_NP', cc.links(gnu_source_prefix + '''
2652  #include <pthread.h>
2653  #include <pthread_np.h>
2654
2655  static void *f(void *p) { return NULL; }
2656  int main(void)
2657  {
2658    pthread_t thread;
2659    pthread_create(&thread, 0, f, 0);
2660    pthread_set_name_np(thread, "QEMU");
2661    return 0;
2662  }''', dependencies: threads))
2663config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', cc.links(gnu_source_prefix + '''
2664  #include <pthread.h>
2665  #include <time.h>
2666
2667  int main(void)
2668  {
2669    pthread_condattr_t attr
2670    pthread_condattr_init(&attr);
2671    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
2672    return 0;
2673  }''', dependencies: threads))
2674config_host_data.set('CONFIG_PTHREAD_AFFINITY_NP', cc.links(gnu_source_prefix + '''
2675  #include <pthread.h>
2676
2677  static void *f(void *p) { return NULL; }
2678  int main(void)
2679  {
2680    int setsize = CPU_ALLOC_SIZE(64);
2681    pthread_t thread;
2682    cpu_set_t *cpuset;
2683    pthread_create(&thread, 0, f, 0);
2684    cpuset = CPU_ALLOC(64);
2685    CPU_ZERO_S(setsize, cpuset);
2686    pthread_setaffinity_np(thread, setsize, cpuset);
2687    pthread_getaffinity_np(thread, setsize, cpuset);
2688    CPU_FREE(cpuset);
2689    return 0;
2690  }''', dependencies: threads))
2691config_host_data.set('CONFIG_SIGNALFD', cc.links(gnu_source_prefix + '''
2692  #include <sys/signalfd.h>
2693  #include <stddef.h>
2694  int main(void) { return signalfd(-1, NULL, SFD_CLOEXEC); }'''))
2695config_host_data.set('CONFIG_SPLICE', cc.links(gnu_source_prefix + '''
2696  #include <unistd.h>
2697  #include <fcntl.h>
2698  #include <limits.h>
2699
2700  int main(void)
2701  {
2702    int len, fd = 0;
2703    len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
2704    splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
2705    return 0;
2706  }'''))
2707
2708config_host_data.set('HAVE_MLOCKALL', cc.links(gnu_source_prefix + '''
2709  #include <sys/mman.h>
2710  int main(void) {
2711    return mlockall(MCL_FUTURE);
2712  }'''))
2713
2714have_l2tpv3 = false
2715if get_option('l2tpv3').allowed() and have_system
2716  have_l2tpv3 = cc.has_type('struct mmsghdr',
2717    prefix: gnu_source_prefix + '''
2718      #include <sys/socket.h>
2719      #include <linux/ip.h>''')
2720endif
2721config_host_data.set('CONFIG_L2TPV3', have_l2tpv3)
2722
2723have_netmap = false
2724if get_option('netmap').allowed() and have_system
2725  have_netmap = cc.compiles('''
2726    #include <inttypes.h>
2727    #include <net/if.h>
2728    #include <net/netmap.h>
2729    #include <net/netmap_user.h>
2730    #if (NETMAP_API < 11) || (NETMAP_API > 15)
2731    #error
2732    #endif
2733    int main(void) { return 0; }''')
2734  if not have_netmap and get_option('netmap').enabled()
2735    error('Netmap headers not available')
2736  endif
2737endif
2738config_host_data.set('CONFIG_NETMAP', have_netmap)
2739
2740# Work around a system header bug with some kernel/XFS header
2741# versions where they both try to define 'struct fsxattr':
2742# xfs headers will not try to redefine structs from linux headers
2743# if this macro is set.
2744config_host_data.set('HAVE_FSXATTR', cc.links('''
2745  #include <linux/fs.h>
2746  struct fsxattr foo;
2747  int main(void) {
2748    return 0;
2749  }'''))
2750
2751# Some versions of Mac OS X incorrectly define SIZE_MAX
2752config_host_data.set('HAVE_BROKEN_SIZE_MAX', not cc.compiles('''
2753    #include <stdint.h>
2754    #include <stdio.h>
2755    int main(void) {
2756        return printf("%zu", SIZE_MAX);
2757    }''', args: ['-Werror']))
2758
2759# See if 64-bit atomic operations are supported.
2760# Note that without __atomic builtins, we can only
2761# assume atomic loads/stores max at pointer size.
2762config_host_data.set('CONFIG_ATOMIC64', cc.links('''
2763  #include <stdint.h>
2764  int main(void)
2765  {
2766    uint64_t x = 0, y = 0;
2767    y = __atomic_load_n(&x, __ATOMIC_RELAXED);
2768    __atomic_store_n(&x, y, __ATOMIC_RELAXED);
2769    __atomic_compare_exchange_n(&x, &y, x, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
2770    __atomic_exchange_n(&x, y, __ATOMIC_RELAXED);
2771    __atomic_fetch_add(&x, y, __ATOMIC_RELAXED);
2772    return 0;
2773  }'''))
2774
2775has_int128_type = cc.compiles('''
2776  __int128_t a;
2777  __uint128_t b;
2778  int main(void) { b = a; }''')
2779config_host_data.set('CONFIG_INT128_TYPE', has_int128_type)
2780
2781has_int128 = has_int128_type and cc.links('''
2782  __int128_t a;
2783  __uint128_t b;
2784  int main (void) {
2785    a = a + b;
2786    b = a * b;
2787    a = a * a;
2788    return 0;
2789  }''')
2790config_host_data.set('CONFIG_INT128', has_int128)
2791
2792if has_int128_type
2793  # "do we have 128-bit atomics which are handled inline and specifically not
2794  # via libatomic". The reason we can't use libatomic is documented in the
2795  # comment starting "GCC is a house divided" in include/qemu/atomic128.h.
2796  # We only care about these operations on 16-byte aligned pointers, so
2797  # force 16-byte alignment of the pointer, which may be greater than
2798  # __alignof(unsigned __int128) for the host.
2799  atomic_test_128 = '''
2800    int main(int ac, char **av) {
2801      __uint128_t *p = __builtin_assume_aligned(av[ac - 1], 16);
2802      p[1] = __atomic_load_n(&p[0], __ATOMIC_RELAXED);
2803      __atomic_store_n(&p[2], p[3], __ATOMIC_RELAXED);
2804      __atomic_compare_exchange_n(&p[4], &p[5], p[6], 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
2805      return 0;
2806    }'''
2807  has_atomic128 = cc.links(atomic_test_128)
2808
2809  config_host_data.set('CONFIG_ATOMIC128', has_atomic128)
2810
2811  if not has_atomic128
2812    # Even with __builtin_assume_aligned, the above test may have failed
2813    # without optimization enabled.  Try again with optimizations locally
2814    # enabled for the function.  See
2815    #   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107389
2816    has_atomic128_opt = cc.links('__attribute__((optimize("O1")))' + atomic_test_128)
2817    config_host_data.set('CONFIG_ATOMIC128_OPT', has_atomic128_opt)
2818
2819    if not has_atomic128_opt
2820      config_host_data.set('CONFIG_CMPXCHG128', cc.links('''
2821        int main(void)
2822        {
2823          __uint128_t x = 0, y = 0;
2824          __sync_val_compare_and_swap_16(&x, y, x);
2825          return 0;
2826        }
2827      '''))
2828    endif
2829  endif
2830endif
2831
2832config_host_data.set('CONFIG_GETAUXVAL', cc.links(gnu_source_prefix + '''
2833  #include <sys/auxv.h>
2834  int main(void) {
2835    return getauxval(AT_HWCAP) == 0;
2836  }'''))
2837
2838config_host_data.set('CONFIG_ELF_AUX_INFO', cc.links(gnu_source_prefix + '''
2839  #include <sys/auxv.h>
2840  int main(void) {
2841    unsigned long hwcap = 0;
2842    elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap));
2843    return hwcap;
2844  }'''))
2845
2846config_host_data.set('CONFIG_USBFS', have_linux_user and cc.compiles('''
2847  #include <linux/usbdevice_fs.h>
2848
2849  #ifndef USBDEVFS_GET_CAPABILITIES
2850  #error "USBDEVFS_GET_CAPABILITIES undefined"
2851  #endif
2852
2853  #ifndef USBDEVFS_DISCONNECT_CLAIM
2854  #error "USBDEVFS_DISCONNECT_CLAIM undefined"
2855  #endif
2856
2857  int main(void) { return 0; }'''))
2858
2859have_keyring = get_option('keyring') \
2860  .require(host_os == 'linux', error_message: 'keyring is only available on Linux') \
2861  .require(cc.compiles('''
2862    #include <errno.h>
2863    #include <asm/unistd.h>
2864    #include <linux/keyctl.h>
2865    #include <sys/syscall.h>
2866    #include <unistd.h>
2867    int main(void) {
2868        return syscall(__NR_keyctl, KEYCTL_READ, 0, NULL, NULL, 0);
2869    }'''), error_message: 'keyctl syscall not available on this system').allowed()
2870config_host_data.set('CONFIG_SECRET_KEYRING', have_keyring)
2871
2872have_cpuid_h = cc.links('''
2873  #include <cpuid.h>
2874  int main(void) {
2875    unsigned a, b, c, d;
2876    unsigned max = __get_cpuid_max(0, 0);
2877
2878    if (max >= 1) {
2879        __cpuid(1, a, b, c, d);
2880    }
2881
2882    if (max >= 7) {
2883        __cpuid_count(7, 0, a, b, c, d);
2884    }
2885
2886    return 0;
2887  }''')
2888config_host_data.set('CONFIG_CPUID_H', have_cpuid_h)
2889
2890# Don't bother to advertise asm/hwprobe.h for old versions that do
2891# not contain RISCV_HWPROBE_EXT_ZBA.
2892config_host_data.set('CONFIG_ASM_HWPROBE_H',
2893                     cc.has_header_symbol('asm/hwprobe.h',
2894                                          'RISCV_HWPROBE_EXT_ZBA'))
2895
2896config_host_data.set('CONFIG_AVX2_OPT', get_option('avx2') \
2897  .require(have_cpuid_h, error_message: 'cpuid.h not available, cannot enable AVX2') \
2898  .require(cc.links('''
2899    #include <cpuid.h>
2900    #include <immintrin.h>
2901    static int __attribute__((target("avx2"))) bar(void *a) {
2902      __m256i x = *(__m256i *)a;
2903      return _mm256_testz_si256(x, x);
2904    }
2905    int main(int argc, char *argv[]) { return bar(argv[argc - 1]); }
2906  '''), error_message: 'AVX2 not available').allowed())
2907
2908config_host_data.set('CONFIG_AVX512BW_OPT', get_option('avx512bw') \
2909  .require(have_cpuid_h, error_message: 'cpuid.h not available, cannot enable AVX512BW') \
2910  .require(cc.links('''
2911    #include <cpuid.h>
2912    #include <immintrin.h>
2913    static int __attribute__((target("avx512bw"))) bar(void *a) {
2914      __m512i *x = a;
2915      __m512i res= _mm512_abs_epi8(*x);
2916      return res[1];
2917    }
2918    int main(int argc, char *argv[]) { return bar(argv[0]); }
2919  '''), error_message: 'AVX512BW not available').allowed())
2920
2921# For both AArch64 and AArch32, detect if builtins are available.
2922config_host_data.set('CONFIG_ARM_AES_BUILTIN', cc.compiles('''
2923    #include <arm_neon.h>
2924    #ifndef __ARM_FEATURE_AES
2925    __attribute__((target("+crypto")))
2926    #endif
2927    void foo(uint8x16_t *p) { *p = vaesmcq_u8(*p); }
2928  '''))
2929
2930if get_option('membarrier').disabled()
2931  have_membarrier = false
2932elif host_os == 'windows'
2933  have_membarrier = true
2934elif host_os == 'linux'
2935  have_membarrier = cc.compiles('''
2936    #include <linux/membarrier.h>
2937    #include <sys/syscall.h>
2938    #include <unistd.h>
2939    #include <stdlib.h>
2940    int main(void) {
2941        syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, 0);
2942        syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0);
2943        exit(0);
2944    }''')
2945endif
2946config_host_data.set('CONFIG_MEMBARRIER', get_option('membarrier') \
2947  .require(have_membarrier, error_message: 'membarrier system call not available') \
2948  .allowed())
2949
2950have_afalg = get_option('crypto_afalg') \
2951  .require(cc.compiles(gnu_source_prefix + '''
2952    #include <errno.h>
2953    #include <sys/types.h>
2954    #include <sys/socket.h>
2955    #include <linux/if_alg.h>
2956    int main(void) {
2957      int sock;
2958      sock = socket(AF_ALG, SOCK_SEQPACKET, 0);
2959      return sock;
2960    }
2961  '''), error_message: 'AF_ALG requested but could not be detected').allowed()
2962config_host_data.set('CONFIG_AF_ALG', have_afalg)
2963
2964config_host_data.set('CONFIG_AF_VSOCK', cc.has_header_symbol(
2965  'linux/vm_sockets.h', 'AF_VSOCK',
2966  prefix: '#include <sys/socket.h>',
2967))
2968
2969have_vss = false
2970have_vss_sdk = false # old xp/2003 SDK
2971if host_os == 'windows' and 'cpp' in all_languages
2972  have_vss = cxx.compiles('''
2973    #define __MIDL_user_allocate_free_DEFINED__
2974    #include <vss.h>
2975    int main(void) { return VSS_CTX_BACKUP; }''')
2976  have_vss_sdk = cxx.has_header('vscoordint.h')
2977endif
2978config_host_data.set('HAVE_VSS_SDK', have_vss_sdk)
2979
2980# Older versions of MinGW do not import _lock_file and _unlock_file properly.
2981# This was fixed for v6.0.0 with commit b48e3ac8969d.
2982if host_os == 'windows'
2983  config_host_data.set('HAVE__LOCK_FILE', cc.links('''
2984    #include <stdio.h>
2985    int main(void) {
2986      _lock_file(NULL);
2987      _unlock_file(NULL);
2988      return 0;
2989    }''', name: '_lock_file and _unlock_file'))
2990endif
2991
2992if host_os == 'windows'
2993  mingw_has_setjmp_longjmp = cc.links('''
2994    #include <setjmp.h>
2995    int main(void) {
2996      /*
2997       * These functions are not available in setjmp header, but may be
2998       * available at link time, from libmingwex.a.
2999       */
3000      extern int __mingw_setjmp(jmp_buf);
3001      extern void __attribute__((noreturn)) __mingw_longjmp(jmp_buf, int);
3002      jmp_buf env;
3003      __mingw_setjmp(env);
3004      __mingw_longjmp(env, 0);
3005    }
3006  ''', name: 'mingw setjmp and longjmp')
3007
3008  if cpu == 'aarch64' and not mingw_has_setjmp_longjmp
3009    error('mingw must provide setjmp/longjmp for windows-arm64')
3010  endif
3011endif
3012
3013########################
3014# Target configuration #
3015########################
3016
3017minikconf = find_program('scripts/minikconf.py')
3018
3019config_all_accel = {}
3020config_all_devices = {}
3021config_devices_mak_list = []
3022config_devices_h = {}
3023config_target_h = {}
3024config_target_mak = {}
3025
3026disassemblers = {
3027  'alpha' : ['CONFIG_ALPHA_DIS'],
3028  'avr' : ['CONFIG_AVR_DIS'],
3029  'cris' : ['CONFIG_CRIS_DIS'],
3030  'hexagon' : ['CONFIG_HEXAGON_DIS'],
3031  'hppa' : ['CONFIG_HPPA_DIS'],
3032  'i386' : ['CONFIG_I386_DIS'],
3033  'x86_64' : ['CONFIG_I386_DIS'],
3034  'm68k' : ['CONFIG_M68K_DIS'],
3035  'microblaze' : ['CONFIG_MICROBLAZE_DIS'],
3036  'mips' : ['CONFIG_MIPS_DIS'],
3037  'or1k' : ['CONFIG_OPENRISC_DIS'],
3038  'ppc' : ['CONFIG_PPC_DIS'],
3039  'riscv' : ['CONFIG_RISCV_DIS'],
3040  'rx' : ['CONFIG_RX_DIS'],
3041  's390' : ['CONFIG_S390_DIS'],
3042  'sh4' : ['CONFIG_SH4_DIS'],
3043  'sparc' : ['CONFIG_SPARC_DIS'],
3044  'xtensa' : ['CONFIG_XTENSA_DIS'],
3045  'loongarch' : ['CONFIG_LOONGARCH_DIS'],
3046}
3047
3048have_ivshmem = config_host_data.get('CONFIG_EVENTFD')
3049host_kconfig = \
3050  (get_option('fuzzing') ? ['CONFIG_FUZZ=y'] : []) + \
3051  (have_tpm ? ['CONFIG_TPM=y'] : []) + \
3052  (pixman.found() ? ['CONFIG_PIXMAN=y'] : []) + \
3053  (spice.found() ? ['CONFIG_SPICE=y'] : []) + \
3054  (have_ivshmem ? ['CONFIG_IVSHMEM=y'] : []) + \
3055  (opengl.found() ? ['CONFIG_OPENGL=y'] : []) + \
3056  (x11.found() ? ['CONFIG_X11=y'] : []) + \
3057  (fdt.found() ? ['CONFIG_FDT=y'] : []) + \
3058  (have_vhost_user ? ['CONFIG_VHOST_USER=y'] : []) + \
3059  (have_vhost_vdpa ? ['CONFIG_VHOST_VDPA=y'] : []) + \
3060  (have_vhost_kernel ? ['CONFIG_VHOST_KERNEL=y'] : []) + \
3061  (have_virtfs ? ['CONFIG_VIRTFS=y'] : []) + \
3062  (host_os == 'linux' ? ['CONFIG_LINUX=y'] : []) + \
3063  (multiprocess_allowed ? ['CONFIG_MULTIPROCESS_ALLOWED=y'] : []) + \
3064  (vfio_user_server_allowed ? ['CONFIG_VFIO_USER_SERVER_ALLOWED=y'] : []) + \
3065  (hv_balloon ? ['CONFIG_HV_BALLOON_POSSIBLE=y'] : [])
3066
3067ignored = [ 'TARGET_XML_FILES', 'TARGET_ABI_DIR', 'TARGET_ARCH' ]
3068
3069default_targets = 'CONFIG_DEFAULT_TARGETS' in config_host
3070actual_target_dirs = []
3071fdt_required = []
3072foreach target : target_dirs
3073  config_target = { 'TARGET_NAME': target.split('-')[0] }
3074  if target.endswith('linux-user')
3075    if host_os != 'linux'
3076      if default_targets
3077        continue
3078      endif
3079      error('Target @0@ is only available on a Linux host'.format(target))
3080    endif
3081    config_target += { 'CONFIG_LINUX_USER': 'y' }
3082  elif target.endswith('bsd-user')
3083    if host_os not in bsd_oses
3084      if default_targets
3085        continue
3086      endif
3087      error('Target @0@ is only available on a BSD host'.format(target))
3088    endif
3089    config_target += { 'CONFIG_BSD_USER': 'y' }
3090  elif target.endswith('softmmu')
3091    config_target += { 'CONFIG_SYSTEM_ONLY': 'y' }
3092    config_target += { 'CONFIG_SOFTMMU': 'y' }
3093  endif
3094  if target.endswith('-user')
3095    config_target += {
3096      'CONFIG_USER_ONLY': 'y',
3097      'CONFIG_QEMU_INTERP_PREFIX':
3098        get_option('interp_prefix').replace('%M', config_target['TARGET_NAME'])
3099    }
3100  endif
3101
3102  target_kconfig = []
3103  foreach sym: accelerators
3104    if sym == 'CONFIG_TCG' or target in accelerator_targets.get(sym, [])
3105      config_target += { sym: 'y' }
3106      config_all_accel += { sym: 'y' }
3107      if target in modular_tcg
3108        config_target += { 'CONFIG_TCG_MODULAR': 'y' }
3109      else
3110        config_target += { 'CONFIG_TCG_BUILTIN': 'y' }
3111      endif
3112      target_kconfig += [ sym + '=y' ]
3113    endif
3114  endforeach
3115  if target_kconfig.length() == 0
3116    if default_targets
3117      continue
3118    endif
3119    error('No accelerator available for target @0@'.format(target))
3120  endif
3121
3122  config_target += keyval.load('configs/targets' / target + '.mak')
3123  config_target += { 'TARGET_' + config_target['TARGET_ARCH'].to_upper(): 'y' }
3124
3125  if 'TARGET_NEED_FDT' in config_target and not fdt.found()
3126    if default_targets
3127      warning('Disabling ' + target + ' due to missing libfdt')
3128    else
3129      fdt_required += target
3130    endif
3131    continue
3132  endif
3133
3134  actual_target_dirs += target
3135
3136  # Add default keys
3137  if 'TARGET_BASE_ARCH' not in config_target
3138    config_target += {'TARGET_BASE_ARCH': config_target['TARGET_ARCH']}
3139  endif
3140  if 'TARGET_ABI_DIR' not in config_target
3141    config_target += {'TARGET_ABI_DIR': config_target['TARGET_ARCH']}
3142  endif
3143  if 'TARGET_BIG_ENDIAN' not in config_target
3144    config_target += {'TARGET_BIG_ENDIAN': 'n'}
3145  endif
3146
3147  foreach k, v: disassemblers
3148    if host_arch.startswith(k) or config_target['TARGET_BASE_ARCH'].startswith(k)
3149      foreach sym: v
3150        config_target += { sym: 'y' }
3151      endforeach
3152    endif
3153  endforeach
3154
3155  config_target_data = configuration_data()
3156  foreach k, v: config_target
3157    if not k.startswith('TARGET_') and not k.startswith('CONFIG_')
3158      # do nothing
3159    elif ignored.contains(k)
3160      # do nothing
3161    elif k == 'TARGET_BASE_ARCH'
3162      # Note that TARGET_BASE_ARCH ends up in config-target.h but it is
3163      # not used to select files from sourcesets.
3164      config_target_data.set('TARGET_' + v.to_upper(), 1)
3165    elif k == 'TARGET_NAME' or k == 'CONFIG_QEMU_INTERP_PREFIX'
3166      config_target_data.set_quoted(k, v)
3167    elif v == 'y'
3168      config_target_data.set(k, 1)
3169    elif v == 'n'
3170      config_target_data.set(k, 0)
3171    else
3172      config_target_data.set(k, v)
3173    endif
3174  endforeach
3175  config_target_data.set('QEMU_ARCH',
3176                         'QEMU_ARCH_' + config_target['TARGET_BASE_ARCH'].to_upper())
3177  config_target_h += {target: configure_file(output: target + '-config-target.h',
3178                                               configuration: config_target_data)}
3179
3180  if target.endswith('-softmmu')
3181    target_kconfig += 'CONFIG_' + config_target['TARGET_ARCH'].to_upper() + '=y'
3182    target_kconfig += 'CONFIG_TARGET_BIG_ENDIAN=' + config_target['TARGET_BIG_ENDIAN']
3183
3184    config_input = meson.get_external_property(target, 'default')
3185    config_devices_mak = target + '-config-devices.mak'
3186    config_devices_mak = configure_file(
3187      input: ['configs/devices' / target / config_input + '.mak', 'Kconfig'],
3188      output: config_devices_mak,
3189      depfile: config_devices_mak + '.d',
3190      capture: true,
3191      command: [minikconf,
3192                get_option('default_devices') ? '--defconfig' : '--allnoconfig',
3193                config_devices_mak, '@DEPFILE@', '@INPUT@',
3194                host_kconfig, target_kconfig])
3195
3196    config_devices_data = configuration_data()
3197    config_devices = keyval.load(config_devices_mak)
3198    foreach k, v: config_devices
3199      config_devices_data.set(k, 1)
3200    endforeach
3201    config_devices_mak_list += config_devices_mak
3202    config_devices_h += {target: configure_file(output: target + '-config-devices.h',
3203                                                configuration: config_devices_data)}
3204    config_target += config_devices
3205    config_all_devices += config_devices
3206  endif
3207  config_target_mak += {target: config_target}
3208endforeach
3209target_dirs = actual_target_dirs
3210
3211target_configs_h = []
3212foreach target: target_dirs
3213  target_configs_h += config_target_h[target]
3214  target_configs_h += config_devices_h.get(target, [])
3215endforeach
3216genh += custom_target('config-poison.h',
3217                      input: [target_configs_h],
3218                      output: 'config-poison.h',
3219                      capture: true,
3220                      command: [find_program('scripts/make-config-poison.sh'),
3221                                target_configs_h])
3222
3223if fdt_required.length() > 0
3224  error('fdt disabled but required by targets ' + ', '.join(fdt_required))
3225endif
3226
3227###############
3228# Subprojects #
3229###############
3230
3231libvfio_user_dep = not_found
3232if have_system and vfio_user_server_allowed
3233  libvfio_user_proj = subproject('libvfio-user', required: true)
3234  libvfio_user_dep = libvfio_user_proj.get_variable('libvfio_user_dep')
3235endif
3236
3237vhost_user = not_found
3238if host_os == 'linux' and have_vhost_user
3239  libvhost_user = subproject('libvhost-user')
3240  vhost_user = libvhost_user.get_variable('vhost_user_dep')
3241endif
3242
3243libvduse = not_found
3244if have_libvduse
3245  libvduse_proj = subproject('libvduse')
3246  libvduse = libvduse_proj.get_variable('libvduse_dep')
3247endif
3248
3249#####################
3250# Generated sources #
3251#####################
3252
3253genh += configure_file(output: 'config-host.h', configuration: config_host_data)
3254
3255hxtool = find_program('scripts/hxtool')
3256shaderinclude = find_program('scripts/shaderinclude.py')
3257qapi_gen = find_program('scripts/qapi-gen.py')
3258qapi_gen_depends = [ meson.current_source_dir() / 'scripts/qapi/__init__.py',
3259                     meson.current_source_dir() / 'scripts/qapi/commands.py',
3260                     meson.current_source_dir() / 'scripts/qapi/common.py',
3261                     meson.current_source_dir() / 'scripts/qapi/error.py',
3262                     meson.current_source_dir() / 'scripts/qapi/events.py',
3263                     meson.current_source_dir() / 'scripts/qapi/expr.py',
3264                     meson.current_source_dir() / 'scripts/qapi/gen.py',
3265                     meson.current_source_dir() / 'scripts/qapi/introspect.py',
3266                     meson.current_source_dir() / 'scripts/qapi/main.py',
3267                     meson.current_source_dir() / 'scripts/qapi/parser.py',
3268                     meson.current_source_dir() / 'scripts/qapi/schema.py',
3269                     meson.current_source_dir() / 'scripts/qapi/source.py',
3270                     meson.current_source_dir() / 'scripts/qapi/types.py',
3271                     meson.current_source_dir() / 'scripts/qapi/visit.py',
3272                     meson.current_source_dir() / 'scripts/qapi-gen.py'
3273]
3274
3275tracetool = [
3276  python, files('scripts/tracetool.py'),
3277   '--backend=' + ','.join(get_option('trace_backends'))
3278]
3279tracetool_depends = files(
3280  'scripts/tracetool/backend/log.py',
3281  'scripts/tracetool/backend/__init__.py',
3282  'scripts/tracetool/backend/dtrace.py',
3283  'scripts/tracetool/backend/ftrace.py',
3284  'scripts/tracetool/backend/simple.py',
3285  'scripts/tracetool/backend/syslog.py',
3286  'scripts/tracetool/backend/ust.py',
3287  'scripts/tracetool/format/ust_events_c.py',
3288  'scripts/tracetool/format/ust_events_h.py',
3289  'scripts/tracetool/format/__init__.py',
3290  'scripts/tracetool/format/d.py',
3291  'scripts/tracetool/format/simpletrace_stap.py',
3292  'scripts/tracetool/format/c.py',
3293  'scripts/tracetool/format/h.py',
3294  'scripts/tracetool/format/log_stap.py',
3295  'scripts/tracetool/format/stap.py',
3296  'scripts/tracetool/__init__.py',
3297)
3298
3299qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
3300                    meson.current_source_dir(),
3301                    get_option('pkgversion'), meson.project_version()]
3302qemu_version = custom_target('qemu-version.h',
3303                             output: 'qemu-version.h',
3304                             command: qemu_version_cmd,
3305                             capture: true,
3306                             build_by_default: true,
3307                             build_always_stale: true)
3308genh += qemu_version
3309
3310hxdep = []
3311hx_headers = [
3312  ['qemu-options.hx', 'qemu-options.def'],
3313  ['qemu-img-cmds.hx', 'qemu-img-cmds.h'],
3314]
3315if have_system
3316  hx_headers += [
3317    ['hmp-commands.hx', 'hmp-commands.h'],
3318    ['hmp-commands-info.hx', 'hmp-commands-info.h'],
3319  ]
3320endif
3321foreach d : hx_headers
3322  hxdep += custom_target(d[1],
3323                input: files(d[0]),
3324                output: d[1],
3325                capture: true,
3326                command: [hxtool, '-h', '@INPUT0@'])
3327endforeach
3328genh += hxdep
3329
3330###############
3331# Trace files #
3332###############
3333
3334# TODO: add each directory to the subdirs from its own meson.build, once
3335# we have those
3336trace_events_subdirs = [
3337  'crypto',
3338  'qapi',
3339  'qom',
3340  'monitor',
3341  'util',
3342  'gdbstub',
3343]
3344if have_linux_user
3345  trace_events_subdirs += [ 'linux-user' ]
3346endif
3347if have_bsd_user
3348  trace_events_subdirs += [ 'bsd-user' ]
3349endif
3350if have_block
3351  trace_events_subdirs += [
3352    'authz',
3353    'block',
3354    'chardev',
3355    'io',
3356    'nbd',
3357    'scsi',
3358  ]
3359endif
3360if have_system
3361  trace_events_subdirs += [
3362    'accel/kvm',
3363    'audio',
3364    'backends',
3365    'backends/tpm',
3366    'ebpf',
3367    'hw/9pfs',
3368    'hw/acpi',
3369    'hw/adc',
3370    'hw/alpha',
3371    'hw/arm',
3372    'hw/audio',
3373    'hw/block',
3374    'hw/char',
3375    'hw/display',
3376    'hw/dma',
3377    'hw/fsi',
3378    'hw/hyperv',
3379    'hw/i2c',
3380    'hw/i386',
3381    'hw/i386/xen',
3382    'hw/i386/kvm',
3383    'hw/ide',
3384    'hw/input',
3385    'hw/intc',
3386    'hw/isa',
3387    'hw/mem',
3388    'hw/mips',
3389    'hw/misc',
3390    'hw/misc/macio',
3391    'hw/net',
3392    'hw/net/can',
3393    'hw/nubus',
3394    'hw/nvme',
3395    'hw/nvram',
3396    'hw/pci',
3397    'hw/pci-host',
3398    'hw/ppc',
3399    'hw/rtc',
3400    'hw/s390x',
3401    'hw/scsi',
3402    'hw/sd',
3403    'hw/sh4',
3404    'hw/sparc',
3405    'hw/sparc64',
3406    'hw/ssi',
3407    'hw/timer',
3408    'hw/tpm',
3409    'hw/ufs',
3410    'hw/usb',
3411    'hw/vfio',
3412    'hw/virtio',
3413    'hw/watchdog',
3414    'hw/xen',
3415    'hw/gpio',
3416    'migration',
3417    'net',
3418    'system',
3419    'ui',
3420    'hw/remote',
3421  ]
3422endif
3423if have_system or have_user
3424  trace_events_subdirs += [
3425    'accel/tcg',
3426    'hw/core',
3427    'target/arm',
3428    'target/arm/hvf',
3429    'target/hppa',
3430    'target/i386',
3431    'target/i386/kvm',
3432    'target/loongarch',
3433    'target/mips/tcg',
3434    'target/ppc',
3435    'target/riscv',
3436    'target/s390x',
3437    'target/s390x/kvm',
3438    'target/sparc',
3439  ]
3440endif
3441
3442###################
3443# Collect sources #
3444###################
3445
3446authz_ss = ss.source_set()
3447blockdev_ss = ss.source_set()
3448block_ss = ss.source_set()
3449chardev_ss = ss.source_set()
3450common_ss = ss.source_set()
3451crypto_ss = ss.source_set()
3452hwcore_ss = ss.source_set()
3453io_ss = ss.source_set()
3454qmp_ss = ss.source_set()
3455qom_ss = ss.source_set()
3456system_ss = ss.source_set()
3457specific_fuzz_ss = ss.source_set()
3458specific_ss = ss.source_set()
3459stub_ss = ss.source_set()
3460trace_ss = ss.source_set()
3461user_ss = ss.source_set()
3462util_ss = ss.source_set()
3463
3464# accel modules
3465qtest_module_ss = ss.source_set()
3466tcg_module_ss = ss.source_set()
3467
3468modules = {}
3469target_modules = {}
3470hw_arch = {}
3471target_arch = {}
3472target_system_arch = {}
3473target_user_arch = {}
3474
3475# NOTE: the trace/ subdirectory needs the qapi_trace_events variable
3476# that is filled in by qapi/.
3477subdir('qapi')
3478subdir('qobject')
3479subdir('stubs')
3480subdir('trace')
3481subdir('util')
3482subdir('qom')
3483subdir('authz')
3484subdir('crypto')
3485subdir('ui')
3486subdir('gdbstub')
3487if have_system
3488  subdir('hw')
3489else
3490  subdir('hw/core')
3491endif
3492
3493if enable_modules
3494  libmodulecommon = static_library('module-common', files('module-common.c') + genh, pic: true, c_args: '-DBUILD_DSO')
3495  modulecommon = declare_dependency(objects: libmodulecommon.extract_all_objects(recursive: false), compile_args: '-DBUILD_DSO')
3496endif
3497
3498qom_ss = qom_ss.apply({})
3499libqom = static_library('qom', qom_ss.sources() + genh,
3500                        dependencies: [qom_ss.dependencies()],
3501                        build_by_default: false)
3502qom = declare_dependency(objects: libqom.extract_all_objects(recursive: false),
3503                         dependencies: qom_ss.dependencies())
3504
3505event_loop_base = files('event-loop-base.c')
3506event_loop_base = static_library('event-loop-base',
3507                                 sources: event_loop_base + genh,
3508                                 build_by_default: false)
3509event_loop_base = declare_dependency(objects: event_loop_base.extract_all_objects(recursive: false),
3510                                     dependencies: [qom])
3511
3512stub_ss = stub_ss.apply({})
3513
3514util_ss.add_all(trace_ss)
3515util_ss = util_ss.apply({})
3516libqemuutil = static_library('qemuutil',
3517                             build_by_default: false,
3518                             sources: util_ss.sources() + stub_ss.sources() + genh,
3519                             dependencies: [util_ss.dependencies(), libm, threads, glib, socket, malloc])
3520qemuutil = declare_dependency(link_with: libqemuutil,
3521                              sources: genh + version_res,
3522                              dependencies: [event_loop_base])
3523
3524if have_system or have_user
3525  decodetree = generator(find_program('scripts/decodetree.py'),
3526                         output: 'decode-@BASENAME@.c.inc',
3527                         arguments: ['@INPUT@', '@EXTRA_ARGS@', '-o', '@OUTPUT@'])
3528  subdir('libdecnumber')
3529  subdir('target')
3530endif
3531
3532subdir('audio')
3533subdir('io')
3534subdir('chardev')
3535subdir('fsdev')
3536subdir('dump')
3537
3538if have_block
3539  block_ss.add(files(
3540    'block.c',
3541    'blockjob.c',
3542    'job.c',
3543    'qemu-io-cmds.c',
3544  ))
3545  if config_host_data.get('CONFIG_REPLICATION')
3546    block_ss.add(files('replication.c'))
3547  endif
3548
3549  subdir('nbd')
3550  subdir('scsi')
3551  subdir('block')
3552
3553  blockdev_ss.add(files(
3554    'blockdev.c',
3555    'blockdev-nbd.c',
3556    'iothread.c',
3557    'job-qmp.c',
3558  ))
3559
3560  # os-posix.c contains POSIX-specific functions used by qemu-storage-daemon,
3561  # os-win32.c does not
3562  if host_os == 'windows'
3563    system_ss.add(files('os-win32.c'))
3564  else
3565    blockdev_ss.add(files('os-posix.c'))
3566  endif
3567endif
3568
3569common_ss.add(files('cpu-common.c'))
3570specific_ss.add(files('cpu-target.c'))
3571
3572subdir('system')
3573
3574# Work around a gcc bug/misfeature wherein constant propagation looks
3575# through an alias:
3576#   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99696
3577# to guess that a const variable is always zero.  Without lto, this is
3578# impossible, as the alias is restricted to page-vary-common.c.  Indeed,
3579# without lto, not even the alias is required -- we simply use different
3580# declarations in different compilation units.
3581pagevary = files('page-vary-common.c')
3582if get_option('b_lto')
3583  pagevary_flags = ['-fno-lto']
3584  if get_option('cfi')
3585    pagevary_flags += '-fno-sanitize=cfi-icall'
3586  endif
3587  pagevary = static_library('page-vary-common', sources: pagevary + genh,
3588                            c_args: pagevary_flags)
3589  pagevary = declare_dependency(link_with: pagevary)
3590endif
3591common_ss.add(pagevary)
3592specific_ss.add(files('page-target.c', 'page-vary-target.c'))
3593
3594subdir('backends')
3595subdir('disas')
3596subdir('migration')
3597subdir('monitor')
3598subdir('net')
3599subdir('replay')
3600subdir('semihosting')
3601subdir('stats')
3602subdir('tcg')
3603subdir('fpu')
3604subdir('accel')
3605subdir('plugins')
3606subdir('ebpf')
3607
3608common_user_inc = []
3609
3610subdir('common-user')
3611subdir('bsd-user')
3612subdir('linux-user')
3613
3614# needed for fuzzing binaries
3615subdir('tests/qtest/libqos')
3616subdir('tests/qtest/fuzz')
3617
3618# accel modules
3619tcg_real_module_ss = ss.source_set()
3620tcg_real_module_ss.add_all(when: 'CONFIG_TCG_MODULAR', if_true: tcg_module_ss)
3621specific_ss.add_all(when: 'CONFIG_TCG_BUILTIN', if_true: tcg_module_ss)
3622target_modules += { 'accel' : { 'qtest': qtest_module_ss,
3623                                'tcg': tcg_real_module_ss }}
3624
3625##############################################
3626# Internal static_libraries and dependencies #
3627##############################################
3628
3629modinfo_collect = find_program('scripts/modinfo-collect.py')
3630modinfo_generate = find_program('scripts/modinfo-generate.py')
3631modinfo_files = []
3632
3633block_mods = []
3634system_mods = []
3635emulator_modules = []
3636foreach d, list : modules
3637  if not (d == 'block' ? have_block : have_system)
3638    continue
3639  endif
3640
3641  foreach m, module_ss : list
3642    if enable_modules
3643      module_ss.add(modulecommon)
3644      module_ss = module_ss.apply(config_all_devices, strict: false)
3645      sl = static_library(d + '-' + m, [genh, module_ss.sources()],
3646                          dependencies: module_ss.dependencies(), pic: true)
3647      if d == 'block'
3648        block_mods += sl
3649      else
3650        system_mods += sl
3651      endif
3652      emulator_modules += shared_module(sl.name(),
3653                    name_prefix: '',
3654                    objects: sl.extract_all_objects(recursive: false),
3655                    dependencies: module_ss.dependencies(),
3656                    install: true,
3657                    install_dir: qemu_moddir)
3658      if module_ss.sources() != []
3659        # FIXME: Should use sl.extract_all_objects(recursive: true) as
3660        # input. Sources can be used multiple times but objects are
3661        # unique when it comes to lookup in compile_commands.json.
3662        # Depnds on a mesion version with
3663        # https://github.com/mesonbuild/meson/pull/8900
3664        modinfo_files += custom_target(d + '-' + m + '.modinfo',
3665                                       output: d + '-' + m + '.modinfo',
3666                                       input: module_ss.sources() + genh,
3667                                       capture: true,
3668                                       command: [modinfo_collect, module_ss.sources()])
3669      endif
3670    else
3671      if d == 'block'
3672        block_ss.add_all(module_ss)
3673      else
3674        system_ss.add_all(module_ss)
3675      endif
3676    endif
3677  endforeach
3678endforeach
3679
3680foreach d, list : target_modules
3681  foreach m, module_ss : list
3682    if enable_modules
3683      module_ss.add(modulecommon)
3684      foreach target : target_dirs
3685        if target.endswith('-softmmu')
3686          config_target = config_target_mak[target]
3687          target_inc = [include_directories('target' / config_target['TARGET_BASE_ARCH'])]
3688          c_args = ['-DCOMPILING_PER_TARGET',
3689                    '-DCONFIG_TARGET="@0@-config-target.h"'.format(target),
3690                    '-DCONFIG_DEVICES="@0@-config-devices.h"'.format(target)]
3691          target_module_ss = module_ss.apply(config_target, strict: false)
3692          if target_module_ss.sources() != []
3693            module_name = d + '-' + m + '-' + config_target['TARGET_NAME']
3694            sl = static_library(module_name,
3695                                [genh, target_module_ss.sources()],
3696                                dependencies: target_module_ss.dependencies(),
3697                                include_directories: target_inc,
3698                                c_args: c_args,
3699                                pic: true)
3700            system_mods += sl
3701            emulator_modules += shared_module(sl.name(),
3702                    name_prefix: '',
3703                    objects: sl.extract_all_objects(recursive: false),
3704                    dependencies: target_module_ss.dependencies(),
3705                    install: true,
3706                    install_dir: qemu_moddir)
3707            # FIXME: Should use sl.extract_all_objects(recursive: true) too.
3708            modinfo_files += custom_target(module_name + '.modinfo',
3709                                           output: module_name + '.modinfo',
3710                                           input: target_module_ss.sources() + genh,
3711                                           capture: true,
3712                                           command: [modinfo_collect, '--target', target, target_module_ss.sources()])
3713          endif
3714        endif
3715      endforeach
3716    else
3717      specific_ss.add_all(module_ss)
3718    endif
3719  endforeach
3720endforeach
3721
3722if enable_modules
3723  foreach target : target_dirs
3724    if target.endswith('-softmmu')
3725      config_target = config_target_mak[target]
3726      config_devices_mak = target + '-config-devices.mak'
3727      modinfo_src = custom_target('modinfo-' + target + '.c',
3728                                  output: 'modinfo-' + target + '.c',
3729                                  input: modinfo_files,
3730                                  command: [modinfo_generate, '--devices', config_devices_mak, '@INPUT@'],
3731                                  capture: true)
3732
3733      modinfo_lib = static_library('modinfo-' + target + '.c', modinfo_src)
3734      modinfo_dep = declare_dependency(link_with: modinfo_lib)
3735
3736      arch = config_target['TARGET_NAME'] == 'sparc64' ? 'sparc64' : config_target['TARGET_BASE_ARCH']
3737      hw_arch[arch].add(modinfo_dep)
3738    endif
3739  endforeach
3740
3741  if emulator_modules.length() > 0
3742    alias_target('modules', emulator_modules)
3743  endif
3744endif
3745
3746nm = find_program('nm')
3747undefsym = find_program('scripts/undefsym.py')
3748block_syms = custom_target('block.syms', output: 'block.syms',
3749                             input: [libqemuutil, block_mods],
3750                             capture: true,
3751                             command: [undefsym, nm, '@INPUT@'])
3752qemu_syms = custom_target('qemu.syms', output: 'qemu.syms',
3753                             input: [libqemuutil, system_mods],
3754                             capture: true,
3755                             command: [undefsym, nm, '@INPUT@'])
3756
3757authz_ss = authz_ss.apply({})
3758libauthz = static_library('authz', authz_ss.sources() + genh,
3759                          dependencies: [authz_ss.dependencies()],
3760                          build_by_default: false)
3761
3762authz = declare_dependency(objects: libauthz.extract_all_objects(recursive: false),
3763                           dependencies: [authz_ss.dependencies(), qom])
3764
3765crypto_ss = crypto_ss.apply({})
3766libcrypto = static_library('crypto', crypto_ss.sources() + genh,
3767                           dependencies: [crypto_ss.dependencies()],
3768                           build_by_default: false)
3769
3770crypto = declare_dependency(objects: libcrypto.extract_all_objects(recursive: false),
3771                            dependencies: [crypto_ss.dependencies(), authz, qom])
3772
3773io_ss = io_ss.apply({})
3774libio = static_library('io', io_ss.sources() + genh,
3775                       dependencies: [io_ss.dependencies()],
3776                       link_with: libqemuutil,
3777                       build_by_default: false)
3778
3779io = declare_dependency(objects: libio.extract_all_objects(recursive: false),
3780                        dependencies: [io_ss.dependencies(), crypto, qom])
3781
3782libmigration = static_library('migration', sources: migration_files + genh,
3783                              build_by_default: false)
3784migration = declare_dependency(objects: libmigration.extract_all_objects(recursive: false),
3785                               dependencies: [qom, io])
3786system_ss.add(migration)
3787
3788block_ss = block_ss.apply({})
3789libblock = static_library('block', block_ss.sources() + genh,
3790                          dependencies: block_ss.dependencies(),
3791                          build_by_default: false)
3792
3793block = declare_dependency(objects: libblock.extract_all_objects(recursive: false),
3794                           dependencies: [block_ss.dependencies(), crypto, io])
3795
3796blockdev_ss = blockdev_ss.apply({})
3797libblockdev = static_library('blockdev', blockdev_ss.sources() + genh,
3798                             dependencies: blockdev_ss.dependencies(),
3799                             build_by_default: false)
3800
3801blockdev = declare_dependency(objects: libblockdev.extract_all_objects(recursive: false),
3802                              dependencies: [blockdev_ss.dependencies(), block, event_loop_base])
3803
3804qmp_ss = qmp_ss.apply({})
3805libqmp = static_library('qmp', qmp_ss.sources() + genh,
3806                        dependencies: qmp_ss.dependencies(),
3807                        build_by_default: false)
3808
3809qmp = declare_dependency(objects: libqmp.extract_all_objects(recursive: false),
3810                         dependencies: qmp_ss.dependencies())
3811
3812libchardev = static_library('chardev', chardev_ss.sources() + genh,
3813                            dependencies: chardev_ss.dependencies(),
3814                            build_by_default: false)
3815
3816chardev = declare_dependency(objects: libchardev.extract_all_objects(recursive: false),
3817                             dependencies: chardev_ss.dependencies())
3818
3819hwcore_ss = hwcore_ss.apply({})
3820libhwcore = static_library('hwcore', sources: hwcore_ss.sources() + genh,
3821                           build_by_default: false)
3822hwcore = declare_dependency(objects: libhwcore.extract_all_objects(recursive: false))
3823common_ss.add(hwcore)
3824
3825###########
3826# Targets #
3827###########
3828
3829system_ss.add(authz, blockdev, chardev, crypto, io, qmp)
3830common_ss.add(qom, qemuutil)
3831
3832common_ss.add_all(when: 'CONFIG_SYSTEM_ONLY', if_true: [system_ss])
3833common_ss.add_all(when: 'CONFIG_USER_ONLY', if_true: user_ss)
3834
3835# Note that this library is never used directly (only through extract_objects)
3836# and is not built by default; therefore, source files not used by the build
3837# configuration will be in build.ninja, but are never built by default.
3838common_all = static_library('common',
3839                            build_by_default: false,
3840                            sources: common_ss.all_sources() + genh,
3841                            include_directories: common_user_inc,
3842                            implicit_include_directories: false,
3843                            dependencies: common_ss.all_dependencies())
3844
3845feature_to_c = find_program('scripts/feature_to_c.py')
3846
3847if host_os == 'darwin'
3848  entitlement = find_program('scripts/entitlement.sh')
3849endif
3850
3851traceable = []
3852emulators = {}
3853foreach target : target_dirs
3854  config_target = config_target_mak[target]
3855  target_name = config_target['TARGET_NAME']
3856  target_base_arch = config_target['TARGET_BASE_ARCH']
3857  arch_srcs = [config_target_h[target]]
3858  arch_deps = []
3859  c_args = ['-DCOMPILING_PER_TARGET',
3860            '-DCONFIG_TARGET="@0@-config-target.h"'.format(target),
3861            '-DCONFIG_DEVICES="@0@-config-devices.h"'.format(target)]
3862  link_args = emulator_link_args
3863
3864  target_inc = [include_directories('target' / config_target['TARGET_BASE_ARCH'])]
3865  if host_os == 'linux'
3866    target_inc += include_directories('linux-headers', is_system: true)
3867  endif
3868  if target.endswith('-softmmu')
3869    target_type='system'
3870    t = target_system_arch[target_base_arch].apply(config_target, strict: false)
3871    arch_srcs += t.sources()
3872    arch_deps += t.dependencies()
3873
3874    hw_dir = target_name == 'sparc64' ? 'sparc64' : target_base_arch
3875    if hw_arch.has_key(hw_dir)
3876      hw = hw_arch[hw_dir].apply(config_target, strict: false)
3877      arch_srcs += hw.sources()
3878      arch_deps += hw.dependencies()
3879    endif
3880
3881    arch_srcs += config_devices_h[target]
3882    link_args += ['@block.syms', '@qemu.syms']
3883  else
3884    abi = config_target['TARGET_ABI_DIR']
3885    target_type='user'
3886    target_inc += common_user_inc
3887    if target_base_arch in target_user_arch
3888      t = target_user_arch[target_base_arch].apply(config_target, strict: false)
3889      arch_srcs += t.sources()
3890      arch_deps += t.dependencies()
3891    endif
3892    if 'CONFIG_LINUX_USER' in config_target
3893      base_dir = 'linux-user'
3894    endif
3895    if 'CONFIG_BSD_USER' in config_target
3896      base_dir = 'bsd-user'
3897      target_inc += include_directories('bsd-user/' / host_os)
3898      target_inc += include_directories('bsd-user/host/' / host_arch)
3899      dir = base_dir / abi
3900      arch_srcs += files(dir / 'signal.c', dir / 'target_arch_cpu.c')
3901    endif
3902    target_inc += include_directories(
3903      base_dir,
3904      base_dir / abi,
3905    )
3906    if 'CONFIG_LINUX_USER' in config_target
3907      dir = base_dir / abi
3908      arch_srcs += files(dir / 'signal.c', dir / 'cpu_loop.c')
3909      if config_target.has_key('TARGET_SYSTBL_ABI')
3910        arch_srcs += \
3911          syscall_nr_generators[abi].process(base_dir / abi / config_target['TARGET_SYSTBL'],
3912                                             extra_args : config_target['TARGET_SYSTBL_ABI'])
3913      endif
3914    endif
3915  endif
3916
3917  if 'TARGET_XML_FILES' in config_target
3918    gdbstub_xml = custom_target(target + '-gdbstub-xml.c',
3919                                output: target + '-gdbstub-xml.c',
3920                                input: files(config_target['TARGET_XML_FILES'].split()),
3921                                command: [feature_to_c, '@INPUT@'],
3922                                capture: true)
3923    arch_srcs += gdbstub_xml
3924  endif
3925
3926  t = target_arch[target_base_arch].apply(config_target, strict: false)
3927  arch_srcs += t.sources()
3928  arch_deps += t.dependencies()
3929
3930  target_common = common_ss.apply(config_target, strict: false)
3931  objects = common_all.extract_objects(target_common.sources())
3932  arch_deps += target_common.dependencies()
3933
3934  target_specific = specific_ss.apply(config_target, strict: false)
3935  arch_srcs += target_specific.sources()
3936  arch_deps += target_specific.dependencies()
3937
3938  # allow using headers from the dependencies but do not include the sources,
3939  # because this emulator only needs those in "objects".  For external
3940  # dependencies, the full dependency is included below in the executable.
3941  lib_deps = []
3942  foreach dep : arch_deps
3943    lib_deps += dep.partial_dependency(compile_args: true, includes: true)
3944  endforeach
3945
3946  lib = static_library('qemu-' + target,
3947                 sources: arch_srcs + genh,
3948                 dependencies: lib_deps,
3949                 objects: objects,
3950                 include_directories: target_inc,
3951                 c_args: c_args,
3952                 build_by_default: false)
3953
3954  if target.endswith('-softmmu')
3955    execs = [{
3956      'name': 'qemu-system-' + target_name,
3957      'win_subsystem': 'console',
3958      'sources': files('system/main.c'),
3959      'dependencies': []
3960    }]
3961    if host_os == 'windows' and (sdl.found() or gtk.found())
3962      execs += [{
3963        'name': 'qemu-system-' + target_name + 'w',
3964        'win_subsystem': 'windows',
3965        'sources': files('system/main.c'),
3966        'dependencies': []
3967      }]
3968    endif
3969    if get_option('fuzzing')
3970      specific_fuzz = specific_fuzz_ss.apply(config_target, strict: false)
3971      execs += [{
3972        'name': 'qemu-fuzz-' + target_name,
3973        'win_subsystem': 'console',
3974        'sources': specific_fuzz.sources(),
3975        'dependencies': specific_fuzz.dependencies(),
3976      }]
3977    endif
3978  else
3979    execs = [{
3980      'name': 'qemu-' + target_name,
3981      'win_subsystem': 'console',
3982      'sources': [],
3983      'dependencies': []
3984    }]
3985  endif
3986  foreach exe: execs
3987    exe_name = exe['name']
3988    if host_os == 'darwin'
3989      exe_name += '-unsigned'
3990    endif
3991
3992    emulator = executable(exe_name, exe['sources'],
3993               install: true,
3994               c_args: c_args,
3995               dependencies: arch_deps + exe['dependencies'],
3996               objects: lib.extract_all_objects(recursive: true),
3997               link_depends: [block_syms, qemu_syms],
3998               link_args: link_args,
3999               win_subsystem: exe['win_subsystem'])
4000
4001    if host_os == 'darwin'
4002      icon = 'pc-bios/qemu.rsrc'
4003      build_input = [emulator, files(icon)]
4004      install_input = [
4005        get_option('bindir') / exe_name,
4006        meson.current_source_dir() / icon
4007      ]
4008      if 'CONFIG_HVF' in config_target
4009        entitlements = 'accel/hvf/entitlements.plist'
4010        build_input += files(entitlements)
4011        install_input += meson.current_source_dir() / entitlements
4012      endif
4013
4014      emulators += {exe['name'] : custom_target(exe['name'],
4015                   input: build_input,
4016                   output: exe['name'],
4017                   command: [entitlement, '@OUTPUT@', '@INPUT@'])
4018      }
4019
4020      meson.add_install_script(entitlement, '--install',
4021                               get_option('bindir') / exe['name'],
4022                               install_input)
4023    else
4024      emulators += {exe['name']: emulator}
4025    endif
4026
4027    traceable += [{
4028      'exe': exe['name'],
4029      'probe-prefix': 'qemu.' + target_type + '.' + target_name,
4030    }]
4031
4032  endforeach
4033endforeach
4034
4035# Other build targets
4036
4037if get_option('plugins')
4038  install_headers('include/qemu/qemu-plugin.h')
4039  if host_os == 'windows'
4040    # On windows, we want to deliver the qemu_plugin_api.lib file in the qemu installer,
4041    # so that plugin authors can compile against it.
4042    install_data(win32_qemu_plugin_api_lib, install_dir: 'lib')
4043  endif
4044endif
4045
4046subdir('qga')
4047
4048# Don't build qemu-keymap if xkbcommon is not explicitly enabled
4049# when we don't build tools or system
4050if xkbcommon.found()
4051  # used for the update-keymaps target, so include rules even if !have_tools
4052  qemu_keymap = executable('qemu-keymap', files('qemu-keymap.c', 'ui/input-keymap.c') + genh,
4053                           dependencies: [qemuutil, xkbcommon], install: have_tools)
4054endif
4055
4056if have_tools
4057  qemu_img = executable('qemu-img', [files('qemu-img.c'), hxdep],
4058             link_args: '@block.syms', link_depends: block_syms,
4059             dependencies: [authz, block, crypto, io, qom, qemuutil], install: true)
4060  qemu_io = executable('qemu-io', files('qemu-io.c'),
4061             link_args: '@block.syms', link_depends: block_syms,
4062             dependencies: [block, qemuutil], install: true)
4063  qemu_nbd = executable('qemu-nbd', files('qemu-nbd.c'),
4064               link_args: '@block.syms', link_depends: block_syms,
4065               dependencies: [blockdev, qemuutil, selinux],
4066               install: true)
4067
4068  subdir('storage-daemon')
4069
4070  foreach exe: [ 'qemu-img', 'qemu-io', 'qemu-nbd', 'qemu-storage-daemon']
4071    traceable += [{
4072      'exe': exe,
4073      'probe-prefix': 'qemu.' + exe.substring(5).replace('-', '_')
4074    }]
4075  endforeach
4076
4077  subdir('contrib/elf2dmp')
4078
4079  executable('qemu-edid', files('qemu-edid.c', 'hw/display/edid-generate.c'),
4080             dependencies: qemuutil,
4081             install: true)
4082
4083  if have_vhost_user
4084    subdir('contrib/vhost-user-blk')
4085    subdir('contrib/vhost-user-gpu')
4086    subdir('contrib/vhost-user-input')
4087    subdir('contrib/vhost-user-scsi')
4088  endif
4089
4090  if host_os == 'linux'
4091    executable('qemu-bridge-helper', files('qemu-bridge-helper.c'),
4092               dependencies: [qemuutil, libcap_ng],
4093               install: true,
4094               install_dir: get_option('libexecdir'))
4095
4096    executable('qemu-pr-helper', files('scsi/qemu-pr-helper.c', 'scsi/utils.c'),
4097               dependencies: [authz, crypto, io, qom, qemuutil,
4098                              libcap_ng, mpathpersist],
4099               install: true)
4100
4101    if cpu in ['x86', 'x86_64']
4102      executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),
4103               dependencies: [authz, crypto, io, qom, qemuutil,
4104                              libcap_ng, mpathpersist],
4105               install: true)
4106    endif
4107  endif
4108
4109  if have_ivshmem
4110    subdir('contrib/ivshmem-client')
4111    subdir('contrib/ivshmem-server')
4112  endif
4113endif
4114
4115if stap.found()
4116  foreach t: traceable
4117    foreach stp: [
4118      {'ext': '.stp-build', 'fmt': 'stap', 'bin': meson.current_build_dir() / t['exe'], 'install': false},
4119      {'ext': '.stp', 'fmt': 'stap', 'bin': get_option('prefix') / get_option('bindir') / t['exe'], 'install': true},
4120      {'ext': '-simpletrace.stp', 'fmt': 'simpletrace-stap', 'bin': '', 'install': true},
4121      {'ext': '-log.stp', 'fmt': 'log-stap', 'bin': '', 'install': true},
4122    ]
4123      cmd = [
4124        tracetool, '--group=all', '--format=' + stp['fmt'],
4125        '--binary=' + stp['bin'],
4126        '--probe-prefix=' + t['probe-prefix'],
4127        '@INPUT@', '@OUTPUT@'
4128      ]
4129
4130      custom_target(t['exe'] + stp['ext'],
4131                    input: trace_events_all,
4132                    output: t['exe'] + stp['ext'],
4133                    install: stp['install'],
4134                    install_dir: get_option('datadir') / 'systemtap/tapset',
4135                    command: cmd,
4136                    depend_files: tracetool_depends)
4137    endforeach
4138  endforeach
4139endif
4140
4141subdir('scripts')
4142subdir('tools')
4143subdir('pc-bios')
4144subdir('docs')
4145subdir('tests')
4146if gtk.found()
4147  subdir('po')
4148endif
4149
4150if host_machine.system() == 'windows'
4151  nsis_cmd = [
4152    find_program('scripts/nsis.py'),
4153    '@OUTPUT@',
4154    get_option('prefix'),
4155    meson.current_source_dir(),
4156    glib_pc.get_variable('bindir'),
4157    host_machine.cpu(),
4158    '--',
4159    '-DDISPLAYVERSION=' + meson.project_version(),
4160  ]
4161  if build_docs
4162    nsis_cmd += '-DCONFIG_DOCUMENTATION=y'
4163  endif
4164  if gtk.found()
4165    nsis_cmd += '-DCONFIG_GTK=y'
4166  endif
4167
4168  nsis = custom_target('nsis',
4169                       output: 'qemu-setup-' + meson.project_version() + '.exe',
4170                       input: files('qemu.nsi'),
4171                       build_always_stale: true,
4172                       command: nsis_cmd + ['@INPUT@'])
4173  alias_target('installer', nsis)
4174endif
4175
4176#########################
4177# Configuration summary #
4178#########################
4179
4180# Build environment
4181summary_info = {}
4182summary_info += {'Build directory':   meson.current_build_dir()}
4183summary_info += {'Source path':       meson.current_source_dir()}
4184summary_info += {'Download dependencies': get_option('wrap_mode') != 'nodownload'}
4185summary(summary_info, bool_yn: true, section: 'Build environment')
4186
4187# Directories
4188summary_info += {'Install prefix':    get_option('prefix')}
4189summary_info += {'BIOS directory':    qemu_datadir}
4190pathsep = host_os == 'windows' ? ';' : ':'
4191summary_info += {'firmware path':     pathsep.join(get_option('qemu_firmwarepath'))}
4192summary_info += {'binary directory':  get_option('prefix') / get_option('bindir')}
4193summary_info += {'library directory': get_option('prefix') / get_option('libdir')}
4194summary_info += {'module directory':  qemu_moddir}
4195summary_info += {'libexec directory': get_option('prefix') / get_option('libexecdir')}
4196summary_info += {'include directory': get_option('prefix') / get_option('includedir')}
4197summary_info += {'config directory':  get_option('prefix') / get_option('sysconfdir')}
4198if host_os != 'windows'
4199  summary_info += {'local state directory': get_option('prefix') / get_option('localstatedir')}
4200  summary_info += {'Manual directory':      get_option('prefix') / get_option('mandir')}
4201else
4202  summary_info += {'local state directory': 'queried at runtime'}
4203endif
4204summary_info += {'Doc directory':     get_option('prefix') / get_option('docdir')}
4205summary(summary_info, bool_yn: true, section: 'Directories')
4206
4207# Host binaries
4208summary_info = {}
4209summary_info += {'python':            '@0@ (version: @1@)'.format(python.full_path(), python.language_version())}
4210summary_info += {'sphinx-build':      sphinx_build}
4211
4212# FIXME: the [binaries] section of machine files, which can be probed
4213# with find_program(), would be great for passing gdb and genisoimage
4214# paths from configure to Meson.  However, there seems to be no way to
4215# hide a program (for example if gdb is too old).
4216if config_host.has_key('GDB')
4217  summary_info += {'gdb':             config_host['GDB']}
4218endif
4219summary_info += {'iasl':              iasl}
4220summary_info += {'genisoimage':       config_host['GENISOIMAGE']}
4221if host_os == 'windows' and have_ga
4222  summary_info += {'wixl':            wixl}
4223endif
4224if slirp.found() and have_system
4225  summary_info += {'smbd':            have_slirp_smbd ? smbd_path : false}
4226endif
4227summary(summary_info, bool_yn: true, section: 'Host binaries')
4228
4229# Configurable features
4230summary_info = {}
4231summary_info += {'Documentation':     build_docs}
4232summary_info += {'system-mode emulation': have_system}
4233summary_info += {'user-mode emulation': have_user}
4234summary_info += {'block layer':       have_block}
4235summary_info += {'Install blobs':     get_option('install_blobs')}
4236summary_info += {'module support':    enable_modules}
4237if enable_modules
4238  summary_info += {'alternative module path': get_option('module_upgrades')}
4239endif
4240summary_info += {'fuzzing support':   get_option('fuzzing')}
4241if have_system
4242  summary_info += {'Audio drivers':     ' '.join(audio_drivers_selected)}
4243endif
4244summary_info += {'Trace backends':    ','.join(get_option('trace_backends'))}
4245if 'simple' in get_option('trace_backends')
4246  summary_info += {'Trace output file': get_option('trace_file') + '-<pid>'}
4247endif
4248summary_info += {'D-Bus display':     dbus_display}
4249summary_info += {'QOM debugging':     get_option('qom_cast_debug')}
4250summary_info += {'Relocatable install': get_option('relocatable')}
4251summary_info += {'vhost-kernel support': have_vhost_kernel}
4252summary_info += {'vhost-net support': have_vhost_net}
4253summary_info += {'vhost-user support': have_vhost_user}
4254summary_info += {'vhost-user-crypto support': have_vhost_user_crypto}
4255summary_info += {'vhost-user-blk server support': have_vhost_user_blk_server}
4256summary_info += {'vhost-vdpa support': have_vhost_vdpa}
4257summary_info += {'build guest agent': have_ga}
4258summary(summary_info, bool_yn: true, section: 'Configurable features')
4259
4260# Compilation information
4261summary_info = {}
4262summary_info += {'host CPU':          cpu}
4263summary_info += {'host endianness':   build_machine.endian()}
4264summary_info += {'C compiler':        ' '.join(meson.get_compiler('c').cmd_array())}
4265summary_info += {'Host C compiler':   ' '.join(meson.get_compiler('c', native: true).cmd_array())}
4266if 'cpp' in all_languages
4267  summary_info += {'C++ compiler':    ' '.join(meson.get_compiler('cpp').cmd_array())}
4268else
4269  summary_info += {'C++ compiler':      false}
4270endif
4271if 'objc' in all_languages
4272  summary_info += {'Objective-C compiler': ' '.join(meson.get_compiler('objc').cmd_array())}
4273else
4274  summary_info += {'Objective-C compiler': false}
4275endif
4276option_cflags = (get_option('debug') ? ['-g'] : [])
4277if get_option('optimization') != 'plain'
4278  option_cflags += ['-O' + get_option('optimization')]
4279endif
4280summary_info += {'CFLAGS':            ' '.join(get_option('c_args') + option_cflags)}
4281if 'cpp' in all_languages
4282  summary_info += {'CXXFLAGS':        ' '.join(get_option('cpp_args') + option_cflags)}
4283endif
4284if 'objc' in all_languages
4285  summary_info += {'OBJCFLAGS':       ' '.join(get_option('objc_args') + option_cflags)}
4286endif
4287link_args = get_option('c_link_args')
4288if link_args.length() > 0
4289  summary_info += {'LDFLAGS':         ' '.join(link_args)}
4290endif
4291summary_info += {'QEMU_CFLAGS':       ' '.join(qemu_common_flags + qemu_cflags)}
4292if 'cpp' in all_languages
4293  summary_info += {'QEMU_CXXFLAGS':     ' '.join(qemu_common_flags + qemu_cxxflags)}
4294endif
4295if 'objc' in all_languages
4296  summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_common_flags)}
4297endif
4298summary_info += {'QEMU_LDFLAGS':      ' '.join(qemu_ldflags)}
4299summary_info += {'link-time optimization (LTO)': get_option('b_lto')}
4300summary_info += {'PIE':               get_option('b_pie')}
4301summary_info += {'static build':      get_option('prefer_static')}
4302summary_info += {'malloc trim support': has_malloc_trim}
4303summary_info += {'membarrier':        have_membarrier}
4304summary_info += {'debug graph lock':  get_option('debug_graph_lock')}
4305summary_info += {'debug stack usage': get_option('debug_stack_usage')}
4306summary_info += {'mutex debugging':   get_option('debug_mutex')}
4307summary_info += {'memory allocator':  get_option('malloc')}
4308summary_info += {'avx2 optimization': config_host_data.get('CONFIG_AVX2_OPT')}
4309summary_info += {'avx512bw optimization': config_host_data.get('CONFIG_AVX512BW_OPT')}
4310summary_info += {'gcov':              get_option('b_coverage')}
4311summary_info += {'thread sanitizer':  get_option('tsan')}
4312summary_info += {'CFI support':       get_option('cfi')}
4313if get_option('cfi')
4314  summary_info += {'CFI debug support': get_option('cfi_debug')}
4315endif
4316summary_info += {'strip binaries':    get_option('strip')}
4317summary_info += {'sparse':            sparse}
4318summary_info += {'mingw32 support':   host_os == 'windows'}
4319summary(summary_info, bool_yn: true, section: 'Compilation')
4320
4321# snarf the cross-compilation information for tests
4322summary_info = {}
4323have_cross = false
4324foreach target: target_dirs
4325  tcg_mak = meson.current_build_dir() / 'tests/tcg' / target / 'config-target.mak'
4326  if fs.exists(tcg_mak)
4327    config_cross_tcg = keyval.load(tcg_mak)
4328    if 'CC' in config_cross_tcg
4329      summary_info += {config_cross_tcg['TARGET_NAME']: config_cross_tcg['CC']}
4330      have_cross = true
4331    endif
4332  endif
4333endforeach
4334if have_cross
4335  summary(summary_info, bool_yn: true, section: 'Cross compilers')
4336endif
4337
4338# Targets and accelerators
4339summary_info = {}
4340if have_system
4341  summary_info += {'KVM support':       config_all_accel.has_key('CONFIG_KVM')}
4342  summary_info += {'HVF support':       config_all_accel.has_key('CONFIG_HVF')}
4343  summary_info += {'WHPX support':      config_all_accel.has_key('CONFIG_WHPX')}
4344  summary_info += {'NVMM support':      config_all_accel.has_key('CONFIG_NVMM')}
4345  summary_info += {'Xen support':       xen.found()}
4346  if xen.found()
4347    summary_info += {'xen ctrl version':  xen.version()}
4348  endif
4349  summary_info += {'Xen emulation':     config_all_devices.has_key('CONFIG_XEN_EMU')}
4350endif
4351summary_info += {'TCG support':       config_all_accel.has_key('CONFIG_TCG')}
4352if config_all_accel.has_key('CONFIG_TCG')
4353  if get_option('tcg_interpreter')
4354    summary_info += {'TCG backend':   'TCI (TCG with bytecode interpreter, slow)'}
4355  else
4356    summary_info += {'TCG backend':   'native (@0@)'.format(cpu)}
4357  endif
4358  summary_info += {'TCG plugins':       get_option('plugins')}
4359  summary_info += {'TCG debug enabled': get_option('debug_tcg')}
4360  if have_linux_user or have_bsd_user
4361    summary_info += {'syscall buffer debugging support': get_option('debug_remap')}
4362  endif
4363endif
4364summary_info += {'target list':       ' '.join(target_dirs)}
4365if have_system
4366  summary_info += {'default devices':   get_option('default_devices')}
4367  summary_info += {'out of process emulation': multiprocess_allowed}
4368  summary_info += {'vfio-user server': vfio_user_server_allowed}
4369endif
4370summary(summary_info, bool_yn: true, section: 'Targets and accelerators')
4371
4372# Block layer
4373summary_info = {}
4374summary_info += {'coroutine backend': coroutine_backend}
4375summary_info += {'coroutine pool':    have_coroutine_pool}
4376if have_block
4377  summary_info += {'Block whitelist (rw)': get_option('block_drv_rw_whitelist')}
4378  summary_info += {'Block whitelist (ro)': get_option('block_drv_ro_whitelist')}
4379  summary_info += {'Use block whitelist in tools': get_option('block_drv_whitelist_in_tools')}
4380  summary_info += {'VirtFS (9P) support':    have_virtfs}
4381  summary_info += {'VirtFS (9P) Proxy Helper support (deprecated)': have_virtfs_proxy_helper}
4382  summary_info += {'replication support': config_host_data.get('CONFIG_REPLICATION')}
4383  summary_info += {'bochs support':     get_option('bochs').allowed()}
4384  summary_info += {'cloop support':     get_option('cloop').allowed()}
4385  summary_info += {'dmg support':       get_option('dmg').allowed()}
4386  summary_info += {'qcow v1 support':   get_option('qcow1').allowed()}
4387  summary_info += {'vdi support':       get_option('vdi').allowed()}
4388  summary_info += {'vhdx support':      get_option('vhdx').allowed()}
4389  summary_info += {'vmdk support':      get_option('vmdk').allowed()}
4390  summary_info += {'vpc support':       get_option('vpc').allowed()}
4391  summary_info += {'vvfat support':     get_option('vvfat').allowed()}
4392  summary_info += {'qed support':       get_option('qed').allowed()}
4393  summary_info += {'parallels support': get_option('parallels').allowed()}
4394  summary_info += {'FUSE exports':      fuse}
4395  summary_info += {'VDUSE block exports': have_vduse_blk_export}
4396endif
4397summary(summary_info, bool_yn: true, section: 'Block layer support')
4398
4399# Crypto
4400summary_info = {}
4401summary_info += {'TLS priority':      get_option('tls_priority')}
4402summary_info += {'GNUTLS support':    gnutls}
4403if gnutls.found()
4404  summary_info += {'  GNUTLS crypto':   gnutls_crypto.found()}
4405endif
4406summary_info += {'libgcrypt':         gcrypt}
4407summary_info += {'nettle':            nettle}
4408if nettle.found()
4409   summary_info += {'  XTS':             xts != 'private'}
4410endif
4411summary_info += {'SM4 ALG support':   crypto_sm4}
4412summary_info += {'AF_ALG support':    have_afalg}
4413summary_info += {'rng-none':          get_option('rng_none')}
4414summary_info += {'Linux keyring':     have_keyring}
4415summary_info += {'Linux keyutils':    keyutils}
4416summary(summary_info, bool_yn: true, section: 'Crypto')
4417
4418# UI
4419summary_info = {}
4420if host_os == 'darwin'
4421  summary_info += {'Cocoa support':           cocoa}
4422endif
4423summary_info += {'SDL support':       sdl}
4424summary_info += {'SDL image support': sdl_image}
4425summary_info += {'GTK support':       gtk}
4426summary_info += {'pixman':            pixman}
4427summary_info += {'VTE support':       vte}
4428summary_info += {'PNG support':       png}
4429summary_info += {'VNC support':       vnc}
4430if vnc.found()
4431  summary_info += {'VNC SASL support':  sasl}
4432  summary_info += {'VNC JPEG support':  jpeg}
4433endif
4434summary_info += {'spice protocol support': spice_protocol}
4435if spice_protocol.found()
4436  summary_info += {'  spice server support': spice}
4437endif
4438summary_info += {'curses support':    curses}
4439summary_info += {'brlapi support':    brlapi}
4440summary(summary_info, bool_yn: true, section: 'User interface')
4441
4442# Graphics backends
4443summary_info = {}
4444summary_info += {'VirGL support':     virgl}
4445summary_info += {'Rutabaga support':  rutabaga}
4446summary(summary_info, bool_yn: true, section: 'Graphics backends')
4447
4448# Audio backends
4449summary_info = {}
4450if host_os not in ['darwin', 'haiku', 'windows']
4451  summary_info += {'OSS support':     oss}
4452  summary_info += {'sndio support':   sndio}
4453elif host_os == 'darwin'
4454  summary_info += {'CoreAudio support': coreaudio}
4455elif host_os == 'windows'
4456  summary_info += {'DirectSound support': dsound}
4457endif
4458if host_os == 'linux'
4459  summary_info += {'ALSA support':    alsa}
4460  summary_info += {'PulseAudio support': pulse}
4461endif
4462summary_info += {'PipeWire support':  pipewire}
4463summary_info += {'JACK support':      jack}
4464summary(summary_info, bool_yn: true, section: 'Audio backends')
4465
4466# Network backends
4467summary_info = {}
4468if host_os == 'darwin'
4469  summary_info += {'vmnet.framework support': vmnet}
4470endif
4471summary_info += {'AF_XDP support':    libxdp}
4472summary_info += {'slirp support':     slirp}
4473summary_info += {'vde support':       vde}
4474summary_info += {'netmap support':    have_netmap}
4475summary_info += {'l2tpv3 support':    have_l2tpv3}
4476summary(summary_info, bool_yn: true, section: 'Network backends')
4477
4478# Libraries
4479summary_info = {}
4480summary_info += {'libtasn1':          tasn1}
4481summary_info += {'PAM':               pam}
4482summary_info += {'iconv support':     iconv}
4483summary_info += {'blkio support':     blkio}
4484summary_info += {'curl support':      curl}
4485summary_info += {'Multipath support': mpathpersist}
4486summary_info += {'Linux AIO support': libaio}
4487summary_info += {'Linux io_uring support': linux_io_uring}
4488summary_info += {'ATTR/XATTR support': libattr}
4489summary_info += {'RDMA support':      rdma}
4490summary_info += {'fdt support':       fdt_opt == 'internal' ? 'internal' : fdt}
4491summary_info += {'libcap-ng support': libcap_ng}
4492summary_info += {'bpf support':       libbpf}
4493summary_info += {'rbd support':       rbd}
4494summary_info += {'smartcard support': cacard}
4495summary_info += {'U2F support':       u2f}
4496summary_info += {'libusb':            libusb}
4497summary_info += {'usb net redir':     usbredir}
4498summary_info += {'OpenGL support (epoxy)': opengl}
4499summary_info += {'GBM':               gbm}
4500summary_info += {'libiscsi support':  libiscsi}
4501summary_info += {'libnfs support':    libnfs}
4502if host_os == 'windows'
4503  if have_ga
4504    summary_info += {'QGA VSS support':   have_qga_vss}
4505  endif
4506endif
4507summary_info += {'seccomp support':   seccomp}
4508summary_info += {'GlusterFS support': glusterfs}
4509summary_info += {'hv-balloon support': hv_balloon}
4510summary_info += {'TPM support':       have_tpm}
4511summary_info += {'libssh support':    libssh}
4512summary_info += {'lzo support':       lzo}
4513summary_info += {'snappy support':    snappy}
4514summary_info += {'bzip2 support':     libbzip2}
4515summary_info += {'lzfse support':     liblzfse}
4516summary_info += {'zstd support':      zstd}
4517summary_info += {'Query Processing Library support': qpl}
4518summary_info += {'UADK Library support': uadk}
4519summary_info += {'NUMA host support': numa}
4520summary_info += {'capstone':          capstone}
4521summary_info += {'libpmem support':   libpmem}
4522summary_info += {'libdaxctl support': libdaxctl}
4523summary_info += {'libudev':           libudev}
4524# Dummy dependency, keep .found()
4525summary_info += {'FUSE lseek':        fuse_lseek.found()}
4526summary_info += {'selinux':           selinux}
4527summary_info += {'libdw':             libdw}
4528if host_os == 'freebsd'
4529  summary_info += {'libinotify-kqueue': inotify}
4530endif
4531summary(summary_info, bool_yn: true, section: 'Dependencies')
4532
4533if host_arch == 'unknown'
4534  message()
4535  warning('UNSUPPORTED HOST CPU')
4536  message()
4537  message('Support for CPU host architecture ' + cpu + ' is not currently')
4538  message('maintained. The QEMU project does not guarantee that QEMU will')
4539  message('compile or work on this host CPU. You can help by volunteering')
4540  message('to maintain it and providing a build host for our continuous')
4541  message('integration setup.')
4542  if get_option('tcg').allowed() and target_dirs.length() > 0
4543    message()
4544    message('configure has succeeded and you can continue to build, but')
4545    message('QEMU will use a slow interpreter to emulate the target CPU.')
4546  endif
4547endif
4548
4549if not supported_oses.contains(host_os)
4550  message()
4551  warning('UNSUPPORTED HOST OS')
4552  message()
4553  message('Support for host OS ' + host_os + 'is not currently maintained.')
4554  message('configure has succeeded and you can continue to build, but')
4555  message('the QEMU project does not guarantee that QEMU will compile or')
4556  message('work on this operating system. You can help by volunteering')
4557  message('to maintain it and providing a build host for our continuous')
4558  message('integration setup. This will ensure that future versions of QEMU')
4559  message('will keep working on ' + host_os + '.')
4560endif
4561
4562if host_arch == 'unknown' or not supported_oses.contains(host_os)
4563  message()
4564  message('If you want to help supporting QEMU on this platform, please')
4565  message('contact the developers at qemu-devel@nongnu.org.')
4566endif
4567
4568actually_reloc = get_option('relocatable')
4569# check if get_relocated_path() is actually able to relocate paths
4570if get_option('relocatable') and \
4571  not (get_option('prefix') / get_option('bindir')).startswith(get_option('prefix') / '')
4572  message()
4573  warning('bindir not included within prefix, the installation will not be relocatable.')
4574  actually_reloc = false
4575endif
4576if not actually_reloc and (host_os == 'windows' or get_option('relocatable'))
4577  if host_os == 'windows'
4578    message()
4579    warning('Windows installs should usually be relocatable.')
4580  endif
4581  message()
4582  message('QEMU will have to be installed under ' + get_option('prefix') + '.')
4583  message('Use --disable-relocatable to remove this warning.')
4584endif
4585