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