xref: /openbmc/qemu/configure (revision c79aa350)
1#!/bin/sh
2#
3# qemu configure script (c) 2003 Fabrice Bellard
4#
5
6# Unset some variables known to interfere with behavior of common tools,
7# just as autoconf does.
8CLICOLOR_FORCE= GREP_OPTIONS=
9unset CLICOLOR_FORCE GREP_OPTIONS
10
11# Don't allow CCACHE, if present, to use cached results of compile tests!
12export CCACHE_RECACHE=yes
13
14# make source path absolute
15source_path=$(cd "$(dirname -- "$0")"; pwd)
16
17if test "$PWD" = "$source_path"
18then
19    echo "Using './build' as the directory for build output"
20
21    MARKER=build/auto-created-by-configure
22
23    if test -e build
24    then
25        if test -f $MARKER
26        then
27           rm -rf build
28        else
29            echo "ERROR: ./build dir already exists and was not previously created by configure"
30            exit 1
31        fi
32    fi
33
34    mkdir build
35    touch $MARKER
36
37    cat > GNUmakefile <<'EOF'
38# This file is auto-generated by configure to support in-source tree
39# 'make' command invocation
40
41ifeq ($(MAKECMDGOALS),)
42recurse: all
43endif
44
45.NOTPARALLEL: %
46%: force
47	@echo 'changing dir to build for $(MAKE) "$(MAKECMDGOALS)"...'
48	@$(MAKE) -C build -f Makefile $(MAKECMDGOALS)
49	@if test "$(MAKECMDGOALS)" = "distclean" && \
50	    test -e build/auto-created-by-configure ; \
51	then \
52	    rm -rf build GNUmakefile ; \
53	fi
54force: ;
55.PHONY: force
56GNUmakefile: ;
57
58EOF
59    cd build
60    exec "$source_path/configure" "$@"
61fi
62
63# Temporary directory used for files created while
64# configure runs. Since it is in the build directory
65# we can safely blow away any previous version of it
66# (and we need not jump through hoops to try to delete
67# it when configure exits.)
68TMPDIR1="config-temp"
69rm -rf "${TMPDIR1}"
70if ! mkdir -p "${TMPDIR1}"; then
71    echo "ERROR: failed to create temporary directory"
72    exit 1
73fi
74
75TMPB="qemu-conf"
76TMPC="${TMPDIR1}/${TMPB}.c"
77TMPO="${TMPDIR1}/${TMPB}.o"
78TMPM="${TMPDIR1}/${TMPB}.m"
79TMPE="${TMPDIR1}/${TMPB}.exe"
80
81rm -f config.log
82
83# Print a helpful header at the top of config.log
84echo "# QEMU configure log $(date)" >> config.log
85printf "# Configured with:" >> config.log
86# repeat the invocation to log and stdout for CI
87invoke=$(printf " '%s'" "$0" "$@")
88test -n "$GITLAB_CI" && echo "configuring with: $invoke"
89{ echo "$invoke"; echo; echo "#"; } >> config.log
90
91quote_sh() {
92    printf "%s" "$1" | sed "s,','\\\\'',g; s,.*,'&',"
93}
94
95print_error() {
96    (echo
97    echo "ERROR: $1"
98    while test -n "$2"; do
99        echo "       $2"
100        shift
101    done
102    echo) >&2
103}
104
105error_exit() {
106    print_error "$@"
107    exit 1
108}
109
110do_compiler() {
111  # Run the compiler, capturing its output to the log. First argument
112  # is compiler binary to execute.
113  compiler="$1"
114  shift
115  if test -n "$BASH_VERSION"; then eval '
116      echo >>config.log "
117funcs: ${FUNCNAME[*]}
118lines: ${BASH_LINENO[*]}"
119  '; fi
120  echo $compiler "$@" >> config.log
121  $compiler "$@" >> config.log 2>&1 || return $?
122}
123
124do_compiler_werror() {
125    # Run the compiler, capturing its output to the log. First argument
126    # is compiler binary to execute.
127    compiler="$1"
128    shift
129    if test -n "$BASH_VERSION"; then eval '
130        echo >>config.log "
131funcs: ${FUNCNAME[*]}
132lines: ${BASH_LINENO[*]}"
133    '; fi
134    echo $compiler "$@" >> config.log
135    $compiler "$@" >> config.log 2>&1 || return $?
136    # Test passed. If this is an --enable-werror build, rerun
137    # the test with -Werror and bail out if it fails. This
138    # makes warning-generating-errors in configure test code
139    # obvious to developers.
140    if test "$werror" != "yes"; then
141        return 0
142    fi
143    # Don't bother rerunning the compile if we were already using -Werror
144    case "$*" in
145        *-Werror*)
146           return 0
147        ;;
148    esac
149    echo $compiler -Werror "$@" >> config.log
150    $compiler -Werror "$@" >> config.log 2>&1 && return $?
151    error_exit "configure test passed without -Werror but failed with -Werror." \
152        "This is probably a bug in the configure script. The failing command" \
153        "will be at the bottom of config.log." \
154        "You can run configure with --disable-werror to bypass this check."
155}
156
157do_cc() {
158    do_compiler_werror "$cc" $CPU_CFLAGS "$@"
159}
160
161do_objc() {
162    do_compiler_werror "$objcc" $CPU_CFLAGS "$@"
163}
164
165# Append $2 to the variable named $1, with space separation
166add_to() {
167    eval $1=\${$1:+\"\$$1 \"}\$2
168}
169
170compile_object() {
171  local_cflags="$1"
172  do_cc $CFLAGS $EXTRA_CFLAGS $CONFIGURE_CFLAGS $QEMU_CFLAGS $local_cflags -c -o $TMPO $TMPC
173}
174
175compile_prog() {
176  local_cflags="$1"
177  local_ldflags="$2"
178  do_cc $CFLAGS $EXTRA_CFLAGS $CONFIGURE_CFLAGS $QEMU_CFLAGS $local_cflags -o $TMPE $TMPC \
179      $LDFLAGS $EXTRA_LDFLAGS $CONFIGURE_LDFLAGS $QEMU_LDFLAGS $local_ldflags
180}
181
182# symbolically link $1 to $2.  Portable version of "ln -sf".
183symlink() {
184  rm -rf "$2"
185  mkdir -p "$(dirname "$2")"
186  ln -s "$1" "$2"
187}
188
189# check whether a command is available to this shell (may be either an
190# executable or a builtin)
191has() {
192    type "$1" >/dev/null 2>&1
193}
194
195version_ge () {
196    local_ver1=$(expr "$1" : '\([0-9.]*\)' | tr . ' ')
197    local_ver2=$(echo "$2" | tr . ' ')
198    while true; do
199        set x $local_ver1
200        local_first=${2-0}
201        # 'shift 2' if $2 is set, or 'shift' if $2 is not set
202        shift ${2:+2}
203        local_ver1=$*
204        set x $local_ver2
205        # the second argument finished, the first must be greater or equal
206        test $# = 1 && return 0
207        test $local_first -lt $2 && return 1
208        test $local_first -gt $2 && return 0
209        shift ${2:+2}
210        local_ver2=$*
211    done
212}
213
214if printf %s\\n "$source_path" "$PWD" | grep -q "[[:space:]:]";
215then
216  error_exit "main directory cannot contain spaces nor colons"
217fi
218
219# default parameters
220cpu=""
221static="no"
222cross_compile="no"
223cross_prefix=""
224host_cc="cc"
225stack_protector=""
226safe_stack=""
227use_containers="yes"
228gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
229
230if test -e "$source_path/.git"
231then
232    git_submodules_action="update"
233else
234    git_submodules_action="ignore"
235fi
236
237git_submodules="ui/keycodemapdb"
238git="git"
239
240# Don't accept a target_list environment variable.
241unset target_list
242unset target_list_exclude
243
244# Default value for a variable defining feature "foo".
245#  * foo="no"  feature will only be used if --enable-foo arg is given
246#  * foo=""    feature will be searched for, and if found, will be used
247#              unless --disable-foo is given
248#  * foo="yes" this value will only be set by --enable-foo flag.
249#              feature will searched for,
250#              if not found, configure exits with error
251#
252# Always add --enable-foo and --disable-foo command line args.
253# Distributions want to ensure that several features are compiled in, and it
254# is impossible without a --enable-foo that exits if a feature is not found.
255
256default_feature=""
257# parse CC options second
258for opt do
259  optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
260  case "$opt" in
261      --without-default-features)
262          default_feature="no"
263  ;;
264  esac
265done
266
267EXTRA_CFLAGS=""
268EXTRA_CXXFLAGS=""
269EXTRA_OBJCFLAGS=""
270EXTRA_LDFLAGS=""
271
272debug_tcg="no"
273sanitizers="no"
274tsan="no"
275fortify_source="yes"
276EXESUF=""
277modules="no"
278prefix="/usr/local"
279qemu_suffix="qemu"
280softmmu="yes"
281linux_user=""
282bsd_user=""
283pie=""
284coroutine=""
285plugins="$default_feature"
286meson=""
287ninja=""
288bindir="bin"
289skip_meson=no
290vfio_user_server="disabled"
291
292# The following Meson options are handled manually (still they
293# are included in the automatically generated help message)
294
295# 1. Track which submodules are needed
296fdt="auto"
297
298# 2. Automatically enable/disable other options
299tcg="auto"
300cfi="false"
301
302# parse CC options second
303for opt do
304  optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
305  case "$opt" in
306  --cross-prefix=*) cross_prefix="$optarg"
307                    cross_compile="yes"
308  ;;
309  --cc=*) CC="$optarg"
310  ;;
311  --cxx=*) CXX="$optarg"
312  ;;
313  --cpu=*) cpu="$optarg"
314  ;;
315  --extra-cflags=*)
316    EXTRA_CFLAGS="$EXTRA_CFLAGS $optarg"
317    EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $optarg"
318    EXTRA_OBJCFLAGS="$EXTRA_OBJCFLAGS $optarg"
319    ;;
320  --extra-cxxflags=*) EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $optarg"
321  ;;
322  --extra-objcflags=*) EXTRA_OBJCFLAGS="$EXTRA_OBJCFLAGS $optarg"
323  ;;
324  --extra-ldflags=*) EXTRA_LDFLAGS="$EXTRA_LDFLAGS $optarg"
325  ;;
326  --cross-cc-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --cross-cc-FOO option"
327  ;;
328  --cross-cc-cflags-*) cc_arch=${opt#--cross-cc-cflags-}; cc_arch=${cc_arch%%=*}
329                      eval "cross_cc_cflags_${cc_arch}=\$optarg"
330  ;;
331  --cross-cc-*) cc_arch=${opt#--cross-cc-}; cc_arch=${cc_arch%%=*}
332                eval "cross_cc_${cc_arch}=\$optarg"
333  ;;
334  --cross-prefix-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --cross-prefix-FOO option"
335  ;;
336  --cross-prefix-*) cc_arch=${opt#--cross-prefix-}; cc_arch=${cc_arch%%=*}
337                    eval "cross_prefix_${cc_arch}=\$optarg"
338  ;;
339  esac
340done
341
342# Preferred compiler:
343#  ${CC} (if set)
344#  ${cross_prefix}gcc (if cross-prefix specified)
345#  system compiler
346if test -z "${CC}${cross_prefix}"; then
347  cc="$host_cc"
348else
349  cc="${CC-${cross_prefix}gcc}"
350fi
351
352if test -z "${CXX}${cross_prefix}"; then
353  cxx="c++"
354else
355  cxx="${CXX-${cross_prefix}g++}"
356fi
357
358ar="${AR-${cross_prefix}ar}"
359as="${AS-${cross_prefix}as}"
360ccas="${CCAS-$cc}"
361objcopy="${OBJCOPY-${cross_prefix}objcopy}"
362ld="${LD-${cross_prefix}ld}"
363ranlib="${RANLIB-${cross_prefix}ranlib}"
364nm="${NM-${cross_prefix}nm}"
365smbd="$SMBD"
366strip="${STRIP-${cross_prefix}strip}"
367widl="${WIDL-${cross_prefix}widl}"
368windres="${WINDRES-${cross_prefix}windres}"
369windmc="${WINDMC-${cross_prefix}windmc}"
370pkg_config_exe="${PKG_CONFIG-${cross_prefix}pkg-config}"
371query_pkg_config() {
372    "${pkg_config_exe}" ${QEMU_PKG_CONFIG_FLAGS} "$@"
373}
374pkg_config=query_pkg_config
375sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
376
377# default flags for all hosts
378# We use -fwrapv to tell the compiler that we require a C dialect where
379# left shift of signed integers is well defined and has the expected
380# 2s-complement style results. (Both clang and gcc agree that it
381# provides these semantics.)
382QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv"
383QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
384
385QEMU_LDFLAGS=
386
387# Flags that are needed during configure but later taken care of by Meson
388CONFIGURE_CFLAGS="-std=gnu11 -Wall"
389CONFIGURE_LDFLAGS=
390
391
392check_define() {
393cat > $TMPC <<EOF
394#if !defined($1)
395#error $1 not defined
396#endif
397int main(void) { return 0; }
398EOF
399  compile_object
400}
401
402check_include() {
403cat > $TMPC <<EOF
404#include <$1>
405int main(void) { return 0; }
406EOF
407  compile_object
408}
409
410write_c_skeleton() {
411    cat > $TMPC <<EOF
412int main(void) { return 0; }
413EOF
414}
415
416if check_define __linux__ ; then
417  targetos=linux
418elif check_define _WIN32 ; then
419  targetos=windows
420elif check_define __OpenBSD__ ; then
421  targetos=openbsd
422elif check_define __sun__ ; then
423  targetos=sunos
424elif check_define __HAIKU__ ; then
425  targetos=haiku
426elif check_define __FreeBSD__ ; then
427  targetos=freebsd
428elif check_define __FreeBSD_kernel__ && check_define __GLIBC__; then
429  targetos=gnu/kfreebsd
430elif check_define __DragonFly__ ; then
431  targetos=dragonfly
432elif check_define __NetBSD__; then
433  targetos=netbsd
434elif check_define __APPLE__; then
435  targetos=darwin
436else
437  # This is a fatal error, but don't report it yet, because we
438  # might be going to just print the --help text, or it might
439  # be the result of a missing compiler.
440  targetos=bogus
441fi
442
443# OS specific
444
445mingw32="no"
446bsd="no"
447linux="no"
448solaris="no"
449case $targetos in
450windows)
451  mingw32="yes"
452  plugins="no"
453  pie="no"
454;;
455gnu/kfreebsd)
456  bsd="yes"
457;;
458freebsd)
459  bsd="yes"
460  make="${MAKE-gmake}"
461  # needed for kinfo_getvmmap(3) in libutil.h
462;;
463dragonfly)
464  bsd="yes"
465  make="${MAKE-gmake}"
466;;
467netbsd)
468  bsd="yes"
469  make="${MAKE-gmake}"
470;;
471openbsd)
472  bsd="yes"
473  make="${MAKE-gmake}"
474;;
475darwin)
476  bsd="yes"
477  darwin="yes"
478  # Disable attempts to use ObjectiveC features in os/object.h since they
479  # won't work when we're compiling with gcc as a C compiler.
480  QEMU_CFLAGS="-DOS_OBJECT_USE_OBJC=0 $QEMU_CFLAGS"
481;;
482sunos)
483  solaris="yes"
484  make="${MAKE-gmake}"
485# needed for CMSG_ macros in sys/socket.h
486  QEMU_CFLAGS="-D_XOPEN_SOURCE=600 $QEMU_CFLAGS"
487# needed for TIOCWIN* defines in termios.h
488  QEMU_CFLAGS="-D__EXTENSIONS__ $QEMU_CFLAGS"
489;;
490haiku)
491  pie="no"
492  QEMU_CFLAGS="-DB_USE_POSITIVE_POSIX_ERRORS -D_BSD_SOURCE -fPIC $QEMU_CFLAGS"
493;;
494linux)
495  linux="yes"
496;;
497esac
498
499if test ! -z "$cpu" ; then
500  # command line argument
501  :
502elif check_define __i386__ ; then
503  cpu="i386"
504elif check_define __x86_64__ ; then
505  if check_define __ILP32__ ; then
506    cpu="x32"
507  else
508    cpu="x86_64"
509  fi
510elif check_define __sparc__ ; then
511  if check_define __arch64__ ; then
512    cpu="sparc64"
513  else
514    cpu="sparc"
515  fi
516elif check_define _ARCH_PPC ; then
517  if check_define _ARCH_PPC64 ; then
518    if check_define _LITTLE_ENDIAN ; then
519      cpu="ppc64le"
520    else
521      cpu="ppc64"
522    fi
523  else
524    cpu="ppc"
525  fi
526elif check_define __mips__ ; then
527  cpu="mips"
528elif check_define __s390__ ; then
529  if check_define __s390x__ ; then
530    cpu="s390x"
531  else
532    cpu="s390"
533  fi
534elif check_define __riscv ; then
535  cpu="riscv"
536elif check_define __arm__ ; then
537  cpu="arm"
538elif check_define __aarch64__ ; then
539  cpu="aarch64"
540elif check_define __loongarch64 ; then
541  cpu="loongarch64"
542else
543  # Using uname is really broken, but it is just a fallback for architectures
544  # that are going to use TCI anyway
545  cpu=$(uname -m)
546  echo "WARNING: unrecognized host CPU, proceeding with 'uname -m' output '$cpu'"
547fi
548
549# Normalise host CPU name and set multilib cflags.  The canonicalization
550# isn't really necessary, because the architectures that we check for
551# should not hit the 'uname -m' case, but better safe than sorry.
552# Note that this case should only have supported host CPUs, not guests.
553case "$cpu" in
554  armv*b|armv*l|arm)
555    cpu="arm" ;;
556
557  i386|i486|i586|i686)
558    cpu="i386"
559    CPU_CFLAGS="-m32" ;;
560  x32)
561    cpu="x86_64"
562    CPU_CFLAGS="-mx32" ;;
563  x86_64|amd64)
564    cpu="x86_64"
565    # ??? Only extremely old AMD cpus do not have cmpxchg16b.
566    # If we truly care, we should simply detect this case at
567    # runtime and generate the fallback to serial emulation.
568    CPU_CFLAGS="-m64 -mcx16" ;;
569
570  mips*)
571    cpu="mips" ;;
572
573  ppc)
574    CPU_CFLAGS="-m32" ;;
575  ppc64)
576    CPU_CFLAGS="-m64 -mbig-endian" ;;
577  ppc64le)
578    cpu="ppc64"
579    CPU_CFLAGS="-m64 -mlittle-endian" ;;
580
581  s390)
582    CPU_CFLAGS="-m31" ;;
583  s390x)
584    CPU_CFLAGS="-m64" ;;
585
586  sparc|sun4[cdmuv])
587    cpu="sparc"
588    CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc" ;;
589  sparc64)
590    CPU_CFLAGS="-m64 -mcpu=ultrasparc" ;;
591esac
592
593: ${make=${MAKE-make}}
594
595# We prefer python 3.x. A bare 'python' is traditionally
596# python 2.x, but some distros have it as python 3.x, so
597# we check that too
598python=
599explicit_python=no
600for binary in "${PYTHON-python3}" python
601do
602    if has "$binary"
603    then
604        python=$(command -v "$binary")
605        break
606    fi
607done
608
609
610# Check for ancillary tools used in testing
611genisoimage=
612for binary in genisoimage mkisofs
613do
614    if has $binary
615    then
616        genisoimage=$(command -v "$binary")
617        break
618    fi
619done
620
621# Default objcc to clang if available, otherwise use CC
622if has clang; then
623  objcc=clang
624else
625  objcc="$cc"
626fi
627
628if test "$mingw32" = "yes" ; then
629  EXESUF=".exe"
630  # MinGW needs -mthreads for TLS and macro _MT.
631  CONFIGURE_CFLAGS="-mthreads $CONFIGURE_CFLAGS"
632  prefix="/qemu"
633  bindir=""
634  qemu_suffix=""
635fi
636
637werror=""
638
639meson_option_build_array() {
640  printf '['
641  (if test "$targetos" = windows; then
642    IFS=\;
643  else
644    IFS=:
645  fi
646  for e in $1; do
647    printf '"""'
648    # backslash escape any '\' and '"' characters
649    printf "%s" "$e" | sed -e 's/\([\"]\)/\\\1/g'
650    printf '""",'
651  done)
652  printf ']\n'
653}
654
655. "$source_path/scripts/meson-buildoptions.sh"
656
657meson_options=
658meson_option_add() {
659  meson_options="$meson_options $(quote_sh "$1")"
660}
661meson_option_parse() {
662  meson_options="$meson_options $(_meson_option_parse "$@")"
663  if test $? -eq 1; then
664    echo "ERROR: unknown option $1"
665    echo "Try '$0 --help' for more information"
666    exit 1
667  fi
668}
669
670for opt do
671  optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
672  case "$opt" in
673  --help|-h) show_help=yes
674  ;;
675  --version|-V) exec cat "$source_path/VERSION"
676  ;;
677  --prefix=*) prefix="$optarg"
678  ;;
679  --cross-prefix=*)
680  ;;
681  --cc=*)
682  ;;
683  --host-cc=*) host_cc="$optarg"
684  ;;
685  --cxx=*)
686  ;;
687  --objcc=*) objcc="$optarg"
688  ;;
689  --make=*) make="$optarg"
690  ;;
691  --install=*)
692  ;;
693  --python=*) python="$optarg" ; explicit_python=yes
694  ;;
695  --skip-meson) skip_meson=yes
696  ;;
697  --meson=*) meson="$optarg"
698  ;;
699  --ninja=*) ninja="$optarg"
700  ;;
701  --smbd=*) smbd="$optarg"
702  ;;
703  --extra-cflags=*)
704  ;;
705  --extra-cxxflags=*)
706  ;;
707  --extra-objcflags=*)
708  ;;
709  --extra-ldflags=*)
710  ;;
711  --cross-cc-*)
712  ;;
713  --cross-prefix-*)
714  ;;
715  --enable-debug-info) meson_option_add -Ddebug=true
716  ;;
717  --disable-debug-info) meson_option_add -Ddebug=false
718  ;;
719  --enable-modules)
720      modules="yes"
721  ;;
722  --disable-modules)
723      modules="no"
724  ;;
725  --cpu=*)
726  ;;
727  --target-list=*) target_list="$optarg"
728                   if test "$target_list_exclude"; then
729                       error_exit "Can't mix --target-list with --target-list-exclude"
730                   fi
731  ;;
732  --target-list-exclude=*) target_list_exclude="$optarg"
733                   if test "$target_list"; then
734                       error_exit "Can't mix --target-list-exclude with --target-list"
735                   fi
736  ;;
737  --with-default-devices) meson_option_add -Ddefault_devices=true
738  ;;
739  --without-default-devices) meson_option_add -Ddefault_devices=false
740  ;;
741  --with-devices-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --with-devices-FOO option"
742  ;;
743  --with-devices-*) device_arch=${opt#--with-devices-};
744                    device_arch=${device_arch%%=*}
745                    cf=$source_path/configs/devices/$device_arch-softmmu/$optarg.mak
746                    if test -f "$cf"; then
747                        device_archs="$device_archs $device_arch"
748                        eval "devices_${device_arch}=\$optarg"
749                    else
750                        error_exit "File $cf does not exist"
751                    fi
752  ;;
753  --without-default-features) # processed above
754  ;;
755  --static)
756    static="yes"
757    QEMU_PKG_CONFIG_FLAGS="--static $QEMU_PKG_CONFIG_FLAGS"
758  ;;
759  --bindir=*) bindir="$optarg"
760  ;;
761  --with-suffix=*) qemu_suffix="$optarg"
762  ;;
763  --host=*|--build=*|\
764  --disable-dependency-tracking|\
765  --sbindir=*|--sharedstatedir=*|\
766  --oldincludedir=*|--datarootdir=*|--infodir=*|\
767  --htmldir=*|--dvidir=*|--pdfdir=*|--psdir=*)
768    # These switches are silently ignored, for compatibility with
769    # autoconf-generated configure scripts. This allows QEMU's
770    # configure to be used by RPM and similar macros that set
771    # lots of directory switches by default.
772  ;;
773  --enable-debug-tcg) debug_tcg="yes"
774  ;;
775  --disable-debug-tcg) debug_tcg="no"
776  ;;
777  --enable-debug)
778      # Enable debugging options that aren't excessively noisy
779      debug_tcg="yes"
780      meson_option_parse --enable-debug-mutex ""
781      meson_option_add -Doptimization=0
782      fortify_source="no"
783  ;;
784  --enable-sanitizers) sanitizers="yes"
785  ;;
786  --disable-sanitizers) sanitizers="no"
787  ;;
788  --enable-tsan) tsan="yes"
789  ;;
790  --disable-tsan) tsan="no"
791  ;;
792  --disable-tcg) tcg="disabled"
793                 plugins="no"
794  ;;
795  --enable-tcg) tcg="enabled"
796  ;;
797  --disable-system) softmmu="no"
798  ;;
799  --enable-system) softmmu="yes"
800  ;;
801  --disable-user)
802      linux_user="no" ;
803      bsd_user="no" ;
804  ;;
805  --enable-user) ;;
806  --disable-linux-user) linux_user="no"
807  ;;
808  --enable-linux-user) linux_user="yes"
809  ;;
810  --disable-bsd-user) bsd_user="no"
811  ;;
812  --enable-bsd-user) bsd_user="yes"
813  ;;
814  --enable-pie) pie="yes"
815  ;;
816  --disable-pie) pie="no"
817  ;;
818  --enable-werror) werror="yes"
819  ;;
820  --disable-werror) werror="no"
821  ;;
822  --enable-stack-protector) stack_protector="yes"
823  ;;
824  --disable-stack-protector) stack_protector="no"
825  ;;
826  --enable-safe-stack) safe_stack="yes"
827  ;;
828  --disable-safe-stack) safe_stack="no"
829  ;;
830  --enable-cfi)
831      cfi="true";
832      meson_option_add -Db_lto=true
833  ;;
834  --disable-cfi) cfi="false"
835  ;;
836  --disable-fdt) fdt="disabled"
837  ;;
838  --enable-fdt) fdt="enabled"
839  ;;
840  --enable-fdt=git) fdt="internal"
841  ;;
842  --enable-fdt=*) fdt="$optarg"
843  ;;
844  --with-coroutine=*) coroutine="$optarg"
845  ;;
846  --with-git=*) git="$optarg"
847  ;;
848  --with-git-submodules=*)
849      git_submodules_action="$optarg"
850  ;;
851  --enable-plugins) if test "$mingw32" = "yes"; then
852                        error_exit "TCG plugins not currently supported on Windows platforms"
853                    else
854                        plugins="yes"
855                    fi
856  ;;
857  --disable-plugins) plugins="no"
858  ;;
859  --enable-containers) use_containers="yes"
860  ;;
861  --disable-containers) use_containers="no"
862  ;;
863  --gdb=*) gdb_bin="$optarg"
864  ;;
865  --enable-vfio-user-server) vfio_user_server="enabled"
866  ;;
867  --disable-vfio-user-server) vfio_user_server="disabled"
868  ;;
869  # everything else has the same name in configure and meson
870  --*) meson_option_parse "$opt" "$optarg"
871  ;;
872  esac
873done
874
875# test for any invalid configuration combinations
876if test "$plugins" = "yes" -a "$tcg" = "disabled"; then
877    error_exit "Can't enable plugins on non-TCG builds"
878fi
879
880case $git_submodules_action in
881    update|validate)
882        if test ! -e "$source_path/.git"; then
883            echo "ERROR: cannot $git_submodules_action git submodules without .git"
884            exit 1
885        fi
886    ;;
887    ignore)
888        if ! test -f "$source_path/ui/keycodemapdb/README"
889        then
890            echo
891            echo "ERROR: missing GIT submodules"
892            echo
893            if test -e "$source_path/.git"; then
894                echo "--with-git-submodules=ignore specified but submodules were not"
895                echo "checked out.  Please initialize and update submodules."
896            else
897                echo "This is not a GIT checkout but module content appears to"
898                echo "be missing. Do not use 'git archive' or GitHub download links"
899                echo "to acquire QEMU source archives. Non-GIT builds are only"
900                echo "supported with source archives linked from:"
901                echo
902                echo "  https://www.qemu.org/download/#source"
903                echo
904                echo "Developers working with GIT can use scripts/archive-source.sh"
905                echo "if they need to create valid source archives."
906            fi
907            echo
908            exit 1
909        fi
910    ;;
911    *)
912        echo "ERROR: invalid --with-git-submodules= value '$git_submodules_action'"
913        exit 1
914    ;;
915esac
916
917default_target_list=""
918mak_wilds=""
919
920if [ "$linux_user" != no ]; then
921    if [ "$targetos" = linux ] && [ -d "$source_path/linux-user/include/host/$cpu" ]; then
922        linux_user=yes
923    elif [ "$linux_user" = yes ]; then
924        error_exit "linux-user not supported on this architecture"
925    fi
926fi
927if [ "$bsd_user" != no ]; then
928    if [ "$bsd_user" = "" ]; then
929        test $targetos = freebsd && bsd_user=yes
930    fi
931    if [ "$bsd_user" = yes ] && ! [ -d "$source_path/bsd-user/$targetos" ]; then
932        error_exit "bsd-user not supported on this host OS"
933    fi
934fi
935if [ "$softmmu" = "yes" ]; then
936    mak_wilds="${mak_wilds} $source_path/configs/targets/*-softmmu.mak"
937fi
938if [ "$linux_user" = "yes" ]; then
939    mak_wilds="${mak_wilds} $source_path/configs/targets/*-linux-user.mak"
940fi
941if [ "$bsd_user" = "yes" ]; then
942    mak_wilds="${mak_wilds} $source_path/configs/targets/*-bsd-user.mak"
943fi
944
945for config in $mak_wilds; do
946    target="$(basename "$config" .mak)"
947    if echo "$target_list_exclude" | grep -vq "$target"; then
948        default_target_list="${default_target_list} $target"
949    fi
950done
951
952if test x"$show_help" = x"yes" ; then
953cat << EOF
954
955Usage: configure [options]
956Options: [defaults in brackets after descriptions]
957
958Standard options:
959  --help                   print this message
960  --prefix=PREFIX          install in PREFIX [$prefix]
961  --target-list=LIST       set target list (default: build all)
962$(echo Available targets: $default_target_list | \
963  fold -s -w 53 | sed -e 's/^/                           /')
964  --target-list-exclude=LIST exclude a set of targets from the default target-list
965
966Advanced options (experts only):
967  --cross-prefix=PREFIX    use PREFIX for compile tools, PREFIX can be blank [$cross_prefix]
968  --cc=CC                  use C compiler CC [$cc]
969  --host-cc=CC             use C compiler CC [$host_cc] for code run at
970                           build time
971  --cxx=CXX                use C++ compiler CXX [$cxx]
972  --objcc=OBJCC            use Objective-C compiler OBJCC [$objcc]
973  --extra-cflags=CFLAGS    append extra C compiler flags CFLAGS
974  --extra-cxxflags=CXXFLAGS append extra C++ compiler flags CXXFLAGS
975  --extra-objcflags=OBJCFLAGS append extra Objective C compiler flags OBJCFLAGS
976  --extra-ldflags=LDFLAGS  append extra linker flags LDFLAGS
977  --cross-cc-ARCH=CC       use compiler when building ARCH guest test cases
978  --cross-cc-cflags-ARCH=  use compiler flags when building ARCH guest tests
979  --cross-prefix-ARCH=PREFIX cross compiler prefix when building ARCH guest test cases
980  --make=MAKE              use specified make [$make]
981  --python=PYTHON          use specified python [$python]
982  --meson=MESON            use specified meson [$meson]
983  --ninja=NINJA            use specified ninja [$ninja]
984  --smbd=SMBD              use specified smbd [$smbd]
985  --with-git=GIT           use specified git [$git]
986  --with-git-submodules=update   update git submodules (default if .git dir exists)
987  --with-git-submodules=validate fail if git submodules are not up to date
988  --with-git-submodules=ignore   do not update or check git submodules (default if no .git dir)
989  --static                 enable static build [$static]
990  --bindir=PATH            install binaries in PATH
991  --with-suffix=SUFFIX     suffix for QEMU data inside datadir/libdir/sysconfdir/docdir [$qemu_suffix]
992  --without-default-features default all --enable-* options to "disabled"
993  --without-default-devices  do not include any device that is not needed to
994                           start the emulator (only use if you are including
995                           desired devices in configs/devices/)
996  --with-devices-ARCH=NAME override default configs/devices
997  --enable-debug           enable common debug build options
998  --enable-sanitizers      enable default sanitizers
999  --enable-tsan            enable thread sanitizer
1000  --disable-werror         disable compilation abort on warning
1001  --disable-stack-protector disable compiler-provided stack protection
1002  --cpu=CPU                Build for host CPU [$cpu]
1003  --with-coroutine=BACKEND coroutine backend. Supported options:
1004                           ucontext, sigaltstack, windows
1005  --enable-plugins
1006                           enable plugins via shared library loading
1007  --disable-containers     don't use containers for cross-building
1008  --gdb=GDB-path           gdb to use for gdbstub tests [$gdb_bin]
1009EOF
1010  meson_options_help
1011cat << EOF
1012  system          all system emulation targets
1013  user            supported user emulation targets
1014  linux-user      all linux usermode emulation targets
1015  bsd-user        all BSD usermode emulation targets
1016  pie             Position Independent Executables
1017  modules         modules support (non-Windows)
1018  debug-tcg       TCG debugging (default is disabled)
1019  debug-info      debugging information
1020  safe-stack      SafeStack Stack Smash Protection. Depends on
1021                  clang/llvm >= 3.7 and requires coroutine backend ucontext.
1022
1023NOTE: The object files are built at the place where configure is launched
1024EOF
1025exit 0
1026fi
1027
1028# Remove old dependency files to make sure that they get properly regenerated
1029rm -f ./*/config-devices.mak.d
1030
1031if test -z "$python"
1032then
1033    error_exit "Python not found. Use --python=/path/to/python"
1034fi
1035if ! has "$make"
1036then
1037    error_exit "GNU make ($make) not found"
1038fi
1039
1040# Note that if the Python conditional here evaluates True we will exit
1041# with status 1 which is a shell 'false' value.
1042if ! $python -c 'import sys; sys.exit(sys.version_info < (3,6))'; then
1043  error_exit "Cannot use '$python', Python >= 3.6 is required." \
1044      "Use --python=/path/to/python to specify a supported Python."
1045fi
1046
1047# Suppress writing compiled files
1048python="$python -B"
1049
1050if test -z "$meson"; then
1051    if test "$explicit_python" = no && has meson && version_ge "$(meson --version)" 0.61.5; then
1052        meson=meson
1053    elif test "$git_submodules_action" != 'ignore' ; then
1054        meson=git
1055    elif test -e "${source_path}/meson/meson.py" ; then
1056        meson=internal
1057    else
1058        if test "$explicit_python" = yes; then
1059            error_exit "--python requires using QEMU's embedded Meson distribution, but it was not found."
1060        else
1061            error_exit "Meson not found.  Use --meson=/path/to/meson"
1062        fi
1063    fi
1064else
1065    # Meson uses its own Python interpreter to invoke other Python scripts,
1066    # but the user wants to use the one they specified with --python.
1067    #
1068    # We do not want to override the distro Python interpreter (and sometimes
1069    # cannot: for example in Homebrew /usr/bin/meson is a bash script), so
1070    # just require --meson=git|internal together with --python.
1071    if test "$explicit_python" = yes; then
1072        case "$meson" in
1073            git | internal) ;;
1074            *) error_exit "--python requires using QEMU's embedded Meson distribution." ;;
1075        esac
1076    fi
1077fi
1078
1079if test "$meson" = git; then
1080    git_submodules="${git_submodules} meson"
1081fi
1082
1083case "$meson" in
1084    git | internal)
1085        meson="$python ${source_path}/meson/meson.py"
1086        ;;
1087    *) meson=$(command -v "$meson") ;;
1088esac
1089
1090# Probe for ninja
1091
1092if test -z "$ninja"; then
1093    for c in ninja ninja-build samu; do
1094        if has $c; then
1095            ninja=$(command -v "$c")
1096            break
1097        fi
1098    done
1099    if test -z "$ninja"; then
1100      error_exit "Cannot find Ninja"
1101    fi
1102fi
1103
1104# Check that the C compiler works. Doing this here before testing
1105# the host CPU ensures that we had a valid CC to autodetect the
1106# $cpu var (and we should bail right here if that's not the case).
1107# It also allows the help message to be printed without a CC.
1108write_c_skeleton;
1109if compile_object ; then
1110  : C compiler works ok
1111else
1112    error_exit "\"$cc\" either does not exist or does not work"
1113fi
1114if ! compile_prog ; then
1115    error_exit "\"$cc\" cannot build an executable (is your linker broken?)"
1116fi
1117
1118# Consult white-list to determine whether to enable werror
1119# by default.  Only enable by default for git builds
1120if test -z "$werror" ; then
1121    if test "$git_submodules_action" != "ignore" && \
1122        { test "$linux" = "yes" || test "$mingw32" = "yes"; }; then
1123        werror="yes"
1124    else
1125        werror="no"
1126    fi
1127fi
1128
1129if test "$targetos" = "bogus"; then
1130    # Now that we know that we're not printing the help and that
1131    # the compiler works (so the results of the check_defines we used
1132    # to identify the OS are reliable), if we didn't recognize the
1133    # host OS we should stop now.
1134    error_exit "Unrecognized host OS (uname -s reports '$(uname -s)')"
1135fi
1136
1137# Check whether the compiler matches our minimum requirements:
1138cat > $TMPC << EOF
1139#if defined(__clang_major__) && defined(__clang_minor__)
1140# ifdef __apple_build_version__
1141#  if __clang_major__ < 10 || (__clang_major__ == 10 && __clang_minor__ < 0)
1142#   error You need at least XCode Clang v10.0 to compile QEMU
1143#  endif
1144# else
1145#  if __clang_major__ < 6 || (__clang_major__ == 6 && __clang_minor__ < 0)
1146#   error You need at least Clang v6.0 to compile QEMU
1147#  endif
1148# endif
1149#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
1150# if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 4)
1151#  error You need at least GCC v7.4.0 to compile QEMU
1152# endif
1153#else
1154# error You either need GCC or Clang to compiler QEMU
1155#endif
1156int main (void) { return 0; }
1157EOF
1158if ! compile_prog "" "" ; then
1159    error_exit "You need at least GCC v7.4 or Clang v6.0 (or XCode Clang v10.0)"
1160fi
1161
1162# Accumulate -Wfoo and -Wno-bar separately.
1163# We will list all of the enable flags first, and the disable flags second.
1164# Note that we do not add -Werror, because that would enable it for all
1165# configure tests. If a configure test failed due to -Werror this would
1166# just silently disable some features, so it's too error prone.
1167
1168warn_flags=
1169add_to warn_flags -Wundef
1170add_to warn_flags -Wwrite-strings
1171add_to warn_flags -Wmissing-prototypes
1172add_to warn_flags -Wstrict-prototypes
1173add_to warn_flags -Wredundant-decls
1174add_to warn_flags -Wold-style-declaration
1175add_to warn_flags -Wold-style-definition
1176add_to warn_flags -Wtype-limits
1177add_to warn_flags -Wformat-security
1178add_to warn_flags -Wformat-y2k
1179add_to warn_flags -Winit-self
1180add_to warn_flags -Wignored-qualifiers
1181add_to warn_flags -Wempty-body
1182add_to warn_flags -Wnested-externs
1183add_to warn_flags -Wendif-labels
1184add_to warn_flags -Wexpansion-to-defined
1185add_to warn_flags -Wimplicit-fallthrough=2
1186add_to warn_flags -Wmissing-format-attribute
1187
1188nowarn_flags=
1189add_to nowarn_flags -Wno-initializer-overrides
1190add_to nowarn_flags -Wno-missing-include-dirs
1191add_to nowarn_flags -Wno-shift-negative-value
1192add_to nowarn_flags -Wno-string-plus-int
1193add_to nowarn_flags -Wno-typedef-redefinition
1194add_to nowarn_flags -Wno-tautological-type-limit-compare
1195add_to nowarn_flags -Wno-psabi
1196add_to nowarn_flags -Wno-gnu-variable-sized-type-not-at-end
1197
1198gcc_flags="$warn_flags $nowarn_flags"
1199
1200cc_has_warning_flag() {
1201    write_c_skeleton;
1202
1203    # Use the positive sense of the flag when testing for -Wno-wombat
1204    # support (gcc will happily accept the -Wno- form of unknown
1205    # warning options).
1206    optflag="$(echo $1 | sed -e 's/^-Wno-/-W/')"
1207    compile_prog "-Werror $optflag" ""
1208}
1209
1210objcc_has_warning_flag() {
1211    cat > $TMPM <<EOF
1212int main(void) { return 0; }
1213EOF
1214
1215    # Use the positive sense of the flag when testing for -Wno-wombat
1216    # support (gcc will happily accept the -Wno- form of unknown
1217    # warning options).
1218    optflag="$(echo $1 | sed -e 's/^-Wno-/-W/')"
1219    do_objc -Werror $optflag \
1220      $OBJCFLAGS $EXTRA_OBJCFLAGS $CONFIGURE_OBJCFLAGS $QEMU_OBJCFLAGS \
1221      -o $TMPE $TMPM $QEMU_LDFLAGS
1222}
1223
1224for flag in $gcc_flags; do
1225    if cc_has_warning_flag $flag ; then
1226        QEMU_CFLAGS="$QEMU_CFLAGS $flag"
1227    fi
1228    if objcc_has_warning_flag $flag ; then
1229        QEMU_OBJCFLAGS="$QEMU_OBJCFLAGS $flag"
1230    fi
1231done
1232
1233if test "$stack_protector" != "no"; then
1234  cat > $TMPC << EOF
1235int main(int argc, char *argv[])
1236{
1237    char arr[64], *p = arr, *c = argv[argc - 1];
1238    while (*c) {
1239        *p++ = *c++;
1240    }
1241    return 0;
1242}
1243EOF
1244  gcc_flags="-fstack-protector-strong -fstack-protector-all"
1245  sp_on=0
1246  for flag in $gcc_flags; do
1247    # We need to check both a compile and a link, since some compiler
1248    # setups fail only on a .c->.o compile and some only at link time
1249    if compile_object "-Werror $flag" &&
1250       compile_prog "-Werror $flag" ""; then
1251      QEMU_CFLAGS="$QEMU_CFLAGS $flag"
1252      QEMU_LDFLAGS="$QEMU_LDFLAGS $flag"
1253      sp_on=1
1254      break
1255    fi
1256  done
1257  if test "$stack_protector" = yes; then
1258    if test $sp_on = 0; then
1259      error_exit "Stack protector not supported"
1260    fi
1261  fi
1262fi
1263
1264# Disable -Wmissing-braces on older compilers that warn even for
1265# the "universal" C zero initializer {0}.
1266cat > $TMPC << EOF
1267struct {
1268  int a[2];
1269} x = {0};
1270EOF
1271if compile_object "-Werror" "" ; then
1272  :
1273else
1274  QEMU_CFLAGS="$QEMU_CFLAGS -Wno-missing-braces"
1275fi
1276
1277# Our module code doesn't support Windows
1278if test "$modules" = "yes" && test "$mingw32" = "yes" ; then
1279  error_exit "Modules are not available for Windows"
1280fi
1281
1282# Static linking is not possible with plugins, modules or PIE
1283if test "$static" = "yes" ; then
1284  if test "$modules" = "yes" ; then
1285    error_exit "static and modules are mutually incompatible"
1286  fi
1287  if test "$plugins" = "yes"; then
1288    error_exit "static and plugins are mutually incompatible"
1289  else
1290    plugins="no"
1291  fi
1292fi
1293test "$plugins" = "" && plugins=yes
1294
1295cat > $TMPC << EOF
1296
1297#ifdef __linux__
1298#  define THREAD __thread
1299#else
1300#  define THREAD
1301#endif
1302static THREAD int tls_var;
1303int main(void) { return tls_var; }
1304EOF
1305
1306# Meson currently only handles pie as a boolean for now so if we have
1307# explicitly disabled PIE we need to extend our cflags because it wont.
1308if test "$static" = "yes"; then
1309  if test "$pie" != "no" && compile_prog "-Werror -fPIE -DPIE" "-static-pie"; then
1310    CONFIGURE_CFLAGS="-fPIE -DPIE $CONFIGURE_CFLAGS"
1311    pie="yes"
1312  elif test "$pie" = "yes"; then
1313    error_exit "-static-pie not available due to missing toolchain support"
1314  else
1315    pie="no"
1316    QEMU_CFLAGS="-fno-pie -no-pie $QEMU_CFLAGS"
1317  fi
1318elif test "$pie" = "no"; then
1319  if compile_prog "-Werror -fno-pie" "-no-pie"; then
1320    CONFIGURE_CFLAGS="-fno-pie $CONFIGURE_CFLAGS"
1321    CONFIGURE_LDFLAGS="-no-pie $CONFIGURE_LDFLAGS"
1322    QEMU_CFLAGS="-fno-pie -no-pie $QEMU_CFLAGS"
1323  fi
1324elif compile_prog "-Werror -fPIE -DPIE" "-pie"; then
1325  CONFIGURE_CFLAGS="-fPIE -DPIE $CONFIGURE_CFLAGS"
1326  CONFIGURE_LDFLAGS="-pie $CONFIGURE_LDFLAGS"
1327  pie="yes"
1328elif test "$pie" = "yes"; then
1329  error_exit "PIE not available due to missing toolchain support"
1330else
1331  echo "Disabling PIE due to missing toolchain support"
1332  pie="no"
1333fi
1334
1335##########################################
1336# __sync_fetch_and_and requires at least -march=i486. Many toolchains
1337# use i686 as default anyway, but for those that don't, an explicit
1338# specification is necessary
1339
1340if test "$cpu" = "i386"; then
1341  cat > $TMPC << EOF
1342static int sfaa(int *ptr)
1343{
1344  return __sync_fetch_and_and(ptr, 0);
1345}
1346
1347int main(void)
1348{
1349  int val = 42;
1350  val = __sync_val_compare_and_swap(&val, 0, 1);
1351  sfaa(&val);
1352  return val;
1353}
1354EOF
1355  if ! compile_prog "" "" ; then
1356    QEMU_CFLAGS="-march=i486 $QEMU_CFLAGS"
1357  fi
1358fi
1359
1360if test -z "${target_list+xxx}" ; then
1361    default_targets=yes
1362    for target in $default_target_list; do
1363        target_list="$target_list $target"
1364    done
1365    target_list="${target_list# }"
1366else
1367    default_targets=no
1368    target_list=$(echo "$target_list" | sed -e 's/,/ /g')
1369    for target in $target_list; do
1370        # Check that we recognised the target name; this allows a more
1371        # friendly error message than if we let it fall through.
1372        case " $default_target_list " in
1373            *" $target "*)
1374                ;;
1375            *)
1376                error_exit "Unknown target name '$target'"
1377                ;;
1378        esac
1379    done
1380fi
1381
1382# see if system emulation was really requested
1383case " $target_list " in
1384  *"-softmmu "*) softmmu=yes
1385  ;;
1386  *) softmmu=no
1387  ;;
1388esac
1389
1390if test "$tcg" = "auto"; then
1391  if test -z "$target_list"; then
1392    tcg="disabled"
1393  else
1394    tcg="enabled"
1395  fi
1396fi
1397
1398if test "$tcg" = "enabled"; then
1399    git_submodules="$git_submodules tests/fp/berkeley-testfloat-3"
1400    git_submodules="$git_submodules tests/fp/berkeley-softfloat-3"
1401fi
1402
1403##########################################
1404# big/little endian test
1405cat > $TMPC << EOF
1406#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1407# error LITTLE
1408#endif
1409int main(void) { return 0; }
1410EOF
1411
1412if ! compile_prog ; then
1413  bigendian="no"
1414else
1415  cat > $TMPC << EOF
1416#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1417# error BIG
1418#endif
1419int main(void) { return 0; }
1420EOF
1421
1422  if ! compile_prog ; then
1423    bigendian="yes"
1424  else
1425    echo big/little test failed
1426    exit 1
1427  fi
1428fi
1429
1430##########################################
1431# pkg-config probe
1432
1433if ! has "$pkg_config_exe"; then
1434  error_exit "pkg-config binary '$pkg_config_exe' not found"
1435fi
1436
1437##########################################
1438# glib support probe
1439
1440# When bumping glib_req_ver, please check also whether we should increase
1441# the _WIN32_WINNT setting in osdep.h according to the value from glib
1442glib_req_ver=2.56
1443glib_modules=gthread-2.0
1444if test "$modules" = yes; then
1445    glib_modules="$glib_modules gmodule-export-2.0"
1446elif test "$plugins" = "yes"; then
1447    glib_modules="$glib_modules gmodule-no-export-2.0"
1448fi
1449
1450for i in $glib_modules; do
1451    if $pkg_config --atleast-version=$glib_req_ver $i; then
1452        glib_cflags=$($pkg_config --cflags $i)
1453        glib_libs=$($pkg_config --libs $i)
1454    else
1455        error_exit "glib-$glib_req_ver $i is required to compile QEMU"
1456    fi
1457done
1458
1459glib_bindir="$($pkg_config --variable=bindir glib-2.0)"
1460if test -z "$glib_bindir" ; then
1461	glib_bindir="$($pkg_config --variable=prefix glib-2.0)"/bin
1462fi
1463
1464# This workaround is required due to a bug in pkg-config file for glib as it
1465# doesn't define GLIB_STATIC_COMPILATION for pkg-config --static
1466
1467if test "$static" = yes && test "$mingw32" = yes; then
1468    glib_cflags="-DGLIB_STATIC_COMPILATION $glib_cflags"
1469fi
1470
1471# Sanity check that the current size_t matches the
1472# size that glib thinks it should be. This catches
1473# problems on multi-arch where people try to build
1474# 32-bit QEMU while pointing at 64-bit glib headers
1475cat > $TMPC <<EOF
1476#include <glib.h>
1477#include <unistd.h>
1478
1479#define QEMU_BUILD_BUG_ON(x) \
1480  typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
1481
1482int main(void) {
1483   QEMU_BUILD_BUG_ON(sizeof(size_t) != GLIB_SIZEOF_SIZE_T);
1484   return 0;
1485}
1486EOF
1487
1488if ! compile_prog "$glib_cflags" "$glib_libs" ; then
1489    error_exit "sizeof(size_t) doesn't match GLIB_SIZEOF_SIZE_T."\
1490               "You probably need to set PKG_CONFIG_LIBDIR"\
1491	       "to point to the right pkg-config files for your"\
1492	       "build target"
1493fi
1494
1495# Silence clang warnings triggered by glib < 2.57.2
1496cat > $TMPC << EOF
1497#include <glib.h>
1498typedef struct Foo {
1499    int i;
1500} Foo;
1501static void foo_free(Foo *f)
1502{
1503    g_free(f);
1504}
1505G_DEFINE_AUTOPTR_CLEANUP_FUNC(Foo, foo_free)
1506int main(void) { return 0; }
1507EOF
1508if ! compile_prog "$glib_cflags -Werror" "$glib_libs" ; then
1509    if cc_has_warning_flag "-Wno-unused-function"; then
1510        glib_cflags="$glib_cflags -Wno-unused-function"
1511        CONFIGURE_CFLAGS="$CONFIGURE_CFLAGS -Wno-unused-function"
1512    fi
1513fi
1514
1515##########################################
1516# fdt probe
1517
1518case "$fdt" in
1519  auto | enabled | internal)
1520    # Simpler to always update submodule, even if not needed.
1521    git_submodules="${git_submodules} dtc"
1522    ;;
1523esac
1524
1525##########################################
1526# check and set a backend for coroutine
1527
1528# We prefer ucontext, but it's not always possible. The fallback
1529# is sigcontext. On Windows the only valid backend is the Windows
1530# specific one.
1531
1532ucontext_works=no
1533if test "$darwin" != "yes"; then
1534  cat > $TMPC << EOF
1535#include <ucontext.h>
1536#ifdef __stub_makecontext
1537#error Ignoring glibc stub makecontext which will always fail
1538#endif
1539int main(void) { makecontext(0, 0, 0); return 0; }
1540EOF
1541  if compile_prog "" "" ; then
1542    ucontext_works=yes
1543  fi
1544fi
1545
1546if test "$coroutine" = ""; then
1547  if test "$mingw32" = "yes"; then
1548    coroutine=win32
1549  elif test "$ucontext_works" = "yes"; then
1550    coroutine=ucontext
1551  else
1552    coroutine=sigaltstack
1553  fi
1554else
1555  case $coroutine in
1556  windows)
1557    if test "$mingw32" != "yes"; then
1558      error_exit "'windows' coroutine backend only valid for Windows"
1559    fi
1560    # Unfortunately the user visible backend name doesn't match the
1561    # coroutine-*.c filename for this case, so we have to adjust it here.
1562    coroutine=win32
1563    ;;
1564  ucontext)
1565    if test "$ucontext_works" != "yes"; then
1566      error_exit "'ucontext' backend requested but makecontext not available"
1567    fi
1568    ;;
1569  sigaltstack)
1570    if test "$mingw32" = "yes"; then
1571      error_exit "only the 'windows' coroutine backend is valid for Windows"
1572    fi
1573    ;;
1574  *)
1575    error_exit "unknown coroutine backend $coroutine"
1576    ;;
1577  esac
1578fi
1579
1580##################################################
1581# SafeStack
1582
1583
1584if test "$safe_stack" = "yes"; then
1585cat > $TMPC << EOF
1586int main(void)
1587{
1588#if ! __has_feature(safe_stack)
1589#error SafeStack Disabled
1590#endif
1591    return 0;
1592}
1593EOF
1594  flag="-fsanitize=safe-stack"
1595  # Check that safe-stack is supported and enabled.
1596  if compile_prog "-Werror $flag" "$flag"; then
1597    # Flag needed both at compilation and at linking
1598    QEMU_CFLAGS="$QEMU_CFLAGS $flag"
1599    QEMU_LDFLAGS="$QEMU_LDFLAGS $flag"
1600  else
1601    error_exit "SafeStack not supported by your compiler"
1602  fi
1603  if test "$coroutine" != "ucontext"; then
1604    error_exit "SafeStack is only supported by the coroutine backend ucontext"
1605  fi
1606else
1607cat > $TMPC << EOF
1608int main(void)
1609{
1610#if defined(__has_feature)
1611#if __has_feature(safe_stack)
1612#error SafeStack Enabled
1613#endif
1614#endif
1615    return 0;
1616}
1617EOF
1618if test "$safe_stack" = "no"; then
1619  # Make sure that safe-stack is disabled
1620  if ! compile_prog "-Werror" ""; then
1621    # SafeStack was already enabled, try to explicitly remove the feature
1622    flag="-fno-sanitize=safe-stack"
1623    if ! compile_prog "-Werror $flag" "$flag"; then
1624      error_exit "Configure cannot disable SafeStack"
1625    fi
1626    QEMU_CFLAGS="$QEMU_CFLAGS $flag"
1627    QEMU_LDFLAGS="$QEMU_LDFLAGS $flag"
1628  fi
1629else # "$safe_stack" = ""
1630  # Set safe_stack to yes or no based on pre-existing flags
1631  if compile_prog "-Werror" ""; then
1632    safe_stack="no"
1633  else
1634    safe_stack="yes"
1635    if test "$coroutine" != "ucontext"; then
1636      error_exit "SafeStack is only supported by the coroutine backend ucontext"
1637    fi
1638  fi
1639fi
1640fi
1641
1642########################################
1643# check if ccache is interfering with
1644# semantic analysis of macros
1645
1646unset CCACHE_CPP2
1647ccache_cpp2=no
1648cat > $TMPC << EOF
1649static const int Z = 1;
1650#define fn() ({ Z; })
1651#define TAUT(X) ((X) == Z)
1652#define PAREN(X, Y) (X == Y)
1653#define ID(X) (X)
1654int main(void)
1655{
1656    int x = 0, y = 0;
1657    x = ID(x);
1658    x = fn();
1659    fn();
1660    if (PAREN(x, y)) return 0;
1661    if (TAUT(Z)) return 0;
1662    return 0;
1663}
1664EOF
1665
1666if ! compile_object "-Werror"; then
1667    ccache_cpp2=yes
1668fi
1669
1670#################################################
1671# clang does not support glibc + FORTIFY_SOURCE.
1672
1673if test "$fortify_source" != "no"; then
1674  if echo | $cc -dM -E - | grep __clang__ > /dev/null 2>&1 ; then
1675    fortify_source="no";
1676  elif test -n "$cxx" && has $cxx &&
1677       echo | $cxx -dM -E - | grep __clang__ >/dev/null 2>&1 ; then
1678    fortify_source="no";
1679  else
1680    fortify_source="yes"
1681  fi
1682fi
1683
1684##########################################
1685# checks for sanitizers
1686
1687have_asan=no
1688have_ubsan=no
1689have_asan_iface_h=no
1690have_asan_iface_fiber=no
1691
1692if test "$sanitizers" = "yes" ; then
1693  write_c_skeleton
1694  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=address" ""; then
1695      have_asan=yes
1696  fi
1697
1698  # we could use a simple skeleton for flags checks, but this also
1699  # detect the static linking issue of ubsan, see also:
1700  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84285
1701  cat > $TMPC << EOF
1702#include <stdlib.h>
1703int main(void) {
1704    void *tmp = malloc(10);
1705    if (tmp != NULL) {
1706        return *(int *)(tmp + 2);
1707    }
1708    return 1;
1709}
1710EOF
1711  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=undefined" ""; then
1712      have_ubsan=yes
1713  fi
1714
1715  if check_include "sanitizer/asan_interface.h" ; then
1716      have_asan_iface_h=yes
1717  fi
1718
1719  cat > $TMPC << EOF
1720#include <sanitizer/asan_interface.h>
1721int main(void) {
1722  __sanitizer_start_switch_fiber(0, 0, 0);
1723  return 0;
1724}
1725EOF
1726  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=address" "" ; then
1727      have_asan_iface_fiber=yes
1728  fi
1729fi
1730
1731# Thread sanitizer is, for now, much noisier than the other sanitizers;
1732# keep it separate until that is not the case.
1733if test "$tsan" = "yes" && test "$sanitizers" = "yes"; then
1734  error_exit "TSAN is not supported with other sanitiziers."
1735fi
1736have_tsan=no
1737have_tsan_iface_fiber=no
1738if test "$tsan" = "yes" ; then
1739  write_c_skeleton
1740  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=thread" "" ; then
1741      have_tsan=yes
1742  fi
1743  cat > $TMPC << EOF
1744#include <sanitizer/tsan_interface.h>
1745int main(void) {
1746  __tsan_create_fiber(0);
1747  return 0;
1748}
1749EOF
1750  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=thread" "" ; then
1751      have_tsan_iface_fiber=yes
1752  fi
1753fi
1754
1755##########################################
1756# functions to probe cross compilers
1757
1758container="no"
1759if test $use_containers = "yes" && (has "docker" || has "podman"); then
1760    case $($python "$source_path"/tests/docker/docker.py probe) in
1761        *docker) container=docker ;;
1762        podman) container=podman ;;
1763        no) container=no ;;
1764    esac
1765    if test "$container" != "no"; then
1766        docker_py="$python $source_path/tests/docker/docker.py --engine $container"
1767    fi
1768fi
1769
1770# cross compilers defaults, can be overridden with --cross-cc-ARCH
1771: ${cross_prefix_aarch64="aarch64-linux-gnu-"}
1772: ${cross_prefix_aarch64_be="$cross_prefix_aarch64"}
1773: ${cross_prefix_alpha="alpha-linux-gnu-"}
1774: ${cross_prefix_arm="arm-linux-gnueabihf-"}
1775: ${cross_prefix_armeb="$cross_prefix_arm"}
1776: ${cross_prefix_hexagon="hexagon-unknown-linux-musl-"}
1777: ${cross_prefix_loongarch64="loongarch64-unknown-linux-gnu-"}
1778: ${cross_prefix_hppa="hppa-linux-gnu-"}
1779: ${cross_prefix_i386="i686-linux-gnu-"}
1780: ${cross_prefix_m68k="m68k-linux-gnu-"}
1781: ${cross_prefix_microblaze="microblaze-linux-musl-"}
1782: ${cross_prefix_mips64el="mips64el-linux-gnuabi64-"}
1783: ${cross_prefix_mips64="mips64-linux-gnuabi64-"}
1784: ${cross_prefix_mipsel="mipsel-linux-gnu-"}
1785: ${cross_prefix_mips="mips-linux-gnu-"}
1786: ${cross_prefix_nios2="nios2-linux-gnu-"}
1787: ${cross_prefix_ppc="powerpc-linux-gnu-"}
1788: ${cross_prefix_ppc64="powerpc64-linux-gnu-"}
1789: ${cross_prefix_ppc64le="$cross_prefix_ppc64"}
1790: ${cross_prefix_riscv64="riscv64-linux-gnu-"}
1791: ${cross_prefix_s390x="s390x-linux-gnu-"}
1792: ${cross_prefix_sh4="sh4-linux-gnu-"}
1793: ${cross_prefix_sparc64="sparc64-linux-gnu-"}
1794: ${cross_prefix_sparc="$cross_prefix_sparc64"}
1795: ${cross_prefix_x86_64="x86_64-linux-gnu-"}
1796
1797: ${cross_cc_aarch64_be="$cross_cc_aarch64"}
1798: ${cross_cc_cflags_aarch64_be="-mbig-endian"}
1799: ${cross_cc_armeb="$cross_cc_arm"}
1800: ${cross_cc_cflags_armeb="-mbig-endian"}
1801: ${cross_cc_hexagon="hexagon-unknown-linux-musl-clang"}
1802: ${cross_cc_cflags_hexagon="-mv67 -O2 -static"}
1803: ${cross_cc_cflags_i386="-m32"}
1804: ${cross_cc_cflags_ppc="-m32 -mbig-endian"}
1805: ${cross_cc_cflags_ppc64="-m64 -mbig-endian"}
1806: ${cross_cc_ppc64le="$cross_cc_ppc64"}
1807: ${cross_cc_cflags_ppc64le="-m64 -mlittle-endian"}
1808: ${cross_cc_cflags_sparc64="-m64 -mcpu=ultrasparc"}
1809: ${cross_cc_sparc="$cross_cc_sparc64"}
1810: ${cross_cc_cflags_sparc="-m32 -mcpu=supersparc"}
1811: ${cross_cc_cflags_x86_64="-m64"}
1812
1813compute_target_variable() {
1814  eval "$2="
1815  if eval test -n "\"\${cross_prefix_$1}\""; then
1816    if eval has "\"\${cross_prefix_$1}\$3\""; then
1817      eval "$2=\"\${cross_prefix_$1}\$3\""
1818    fi
1819  fi
1820}
1821
1822have_target() {
1823  for i; do
1824    case " $target_list " in
1825      *" $i "*) return 0;;
1826      *) ;;
1827    esac
1828  done
1829  return 1
1830}
1831
1832# probe_target_compiler TARGET
1833#
1834# Look for a compiler for the given target, either native or cross.
1835# Set variables target_* if a compiler is found, and container_cross_*
1836# if a Docker-based cross-compiler image is known for the target.
1837# Set got_cross_cc to yes/no depending on whether a non-container-based
1838# compiler was found.
1839#
1840# If TARGET is a user-mode emulation target, also set build_static to
1841# "y" if static linking is possible.
1842#
1843probe_target_compiler() {
1844  # reset all output variables
1845  got_cross_cc=no
1846  container_image=
1847  container_hosts=
1848  container_cross_cc=
1849  container_cross_ar=
1850  container_cross_as=
1851  container_cross_ld=
1852  container_cross_nm=
1853  container_cross_objcopy=
1854  container_cross_ranlib=
1855  container_cross_strip=
1856
1857  # We shall skip configuring the target compiler if the user didn't
1858  # bother enabling an appropriate guest. This avoids building
1859  # extraneous firmware images and tests.
1860  if test "${target_list#*$1}" = "$1"; then
1861      return 1
1862  fi
1863
1864  target_arch=${1%%-*}
1865  case $target_arch in
1866    aarch64) container_hosts="x86_64 aarch64" ;;
1867    alpha) container_hosts=x86_64 ;;
1868    arm) container_hosts="x86_64 aarch64" ;;
1869    cris) container_hosts=x86_64 ;;
1870    hexagon) container_hosts=x86_64 ;;
1871    hppa) container_hosts=x86_64 ;;
1872    i386) container_hosts=x86_64 ;;
1873    loongarch64) container_hosts=x86_64 ;;
1874    m68k) container_hosts=x86_64 ;;
1875    microblaze) container_hosts=x86_64 ;;
1876    mips64el) container_hosts=x86_64 ;;
1877    mips64) container_hosts=x86_64 ;;
1878    mipsel) container_hosts=x86_64 ;;
1879    mips) container_hosts=x86_64 ;;
1880    nios2) container_hosts=x86_64 ;;
1881    ppc) container_hosts=x86_64 ;;
1882    ppc64|ppc64le) container_hosts=x86_64 ;;
1883    riscv64) container_hosts=x86_64 ;;
1884    s390x) container_hosts=x86_64 ;;
1885    sh4) container_hosts=x86_64 ;;
1886    sparc64) container_hosts=x86_64 ;;
1887    tricore) container_hosts=x86_64 ;;
1888    x86_64) container_hosts="aarch64 ppc64el x86_64" ;;
1889    xtensa*) container_hosts=x86_64 ;;
1890  esac
1891
1892  for host in $container_hosts; do
1893    test "$container" != no || continue
1894    test "$host" = "$cpu" || continue
1895    case $target_arch in
1896      aarch64)
1897        # We don't have any bigendian build tools so we only use this for AArch64
1898        container_image=debian-arm64-cross
1899        container_cross_prefix=aarch64-linux-gnu-
1900        container_cross_cc=${container_cross_prefix}gcc-10
1901        ;;
1902      alpha)
1903        container_image=debian-alpha-cross
1904        container_cross_prefix=alpha-linux-gnu-
1905        ;;
1906      arm)
1907        # We don't have any bigendian build tools so we only use this for ARM
1908        container_image=debian-armhf-cross
1909        container_cross_prefix=arm-linux-gnueabihf-
1910        ;;
1911      cris)
1912        container_image=fedora-cris-cross
1913        container_cross_prefix=cris-linux-gnu-
1914        ;;
1915      hexagon)
1916        container_image=debian-hexagon-cross
1917        container_cross_prefix=hexagon-unknown-linux-musl-
1918        container_cross_cc=${container_cross_prefix}clang
1919        ;;
1920      hppa)
1921        container_image=debian-hppa-cross
1922        container_cross_prefix=hppa-linux-gnu-
1923        ;;
1924      i386)
1925        container_image=fedora-i386-cross
1926        container_cross_prefix=
1927        ;;
1928      loongarch64)
1929        container_image=debian-loongarch-cross
1930        container_cross_prefix=loongarch64-unknown-linux-gnu-
1931        ;;
1932      m68k)
1933        container_image=debian-m68k-cross
1934        container_cross_prefix=m68k-linux-gnu-
1935        ;;
1936      microblaze)
1937        container_image=debian-microblaze-cross
1938        container_cross_prefix=microblaze-linux-musl-
1939        ;;
1940      mips64el)
1941        container_image=debian-mips64el-cross
1942        container_cross_prefix=mips64el-linux-gnuabi64-
1943        ;;
1944      mips64)
1945        container_image=debian-mips64-cross
1946        container_cross_prefix=mips64-linux-gnuabi64-
1947        ;;
1948      mipsel)
1949        container_image=debian-mipsel-cross
1950        container_cross_prefix=mipsel-linux-gnu-
1951        ;;
1952      mips)
1953        container_image=debian-mips-cross
1954        container_cross_prefix=mips-linux-gnu-
1955        ;;
1956      nios2)
1957        container_image=debian-nios2-cross
1958        container_cross_prefix=nios2-linux-gnu-
1959        ;;
1960      ppc)
1961        container_image=debian-powerpc-test-cross
1962        container_cross_prefix=powerpc-linux-gnu-
1963        container_cross_cc=${container_cross_prefix}gcc-10
1964        ;;
1965      ppc64|ppc64le)
1966        container_image=debian-powerpc-test-cross
1967        container_cross_prefix=powerpc${target_arch#ppc}-linux-gnu-
1968        container_cross_cc=${container_cross_prefix}gcc-10
1969        ;;
1970      riscv64)
1971        container_image=debian-riscv64-test-cross
1972        container_cross_prefix=riscv64-linux-gnu-
1973        ;;
1974      s390x)
1975        container_image=debian-s390x-cross
1976        container_cross_prefix=s390x-linux-gnu-
1977        ;;
1978      sh4)
1979        container_image=debian-sh4-cross
1980        container_cross_prefix=sh4-linux-gnu-
1981        ;;
1982      sparc64)
1983        container_image=debian-sparc64-cross
1984        container_cross_prefix=sparc64-linux-gnu-
1985        ;;
1986      tricore)
1987        container_image=debian-tricore-cross
1988        container_cross_prefix=tricore-
1989        container_cross_as=tricore-as
1990        container_cross_ld=tricore-ld
1991        break
1992        ;;
1993      x86_64)
1994        container_image=debian-amd64-cross
1995        container_cross_prefix=x86_64-linux-gnu-
1996        ;;
1997      xtensa*)
1998        container_hosts=x86_64
1999        container_image=debian-xtensa-cross
2000
2001        # default to the dc232b cpu
2002        container_cross_prefix=/opt/2020.07/xtensa-dc232b-elf/bin/xtensa-dc232b-elf-
2003        ;;
2004    esac
2005    : ${container_cross_cc:=${container_cross_prefix}gcc}
2006    : ${container_cross_ar:=${container_cross_prefix}ar}
2007    : ${container_cross_as:=${container_cross_prefix}as}
2008    : ${container_cross_ld:=${container_cross_prefix}ld}
2009    : ${container_cross_nm:=${container_cross_prefix}nm}
2010    : ${container_cross_objcopy:=${container_cross_prefix}objcopy}
2011    : ${container_cross_ranlib:=${container_cross_prefix}ranlib}
2012    : ${container_cross_strip:=${container_cross_prefix}strip}
2013  done
2014
2015  try=cross
2016  case "$target_arch:$cpu" in
2017    aarch64_be:aarch64 | \
2018    armeb:arm | \
2019    i386:x86_64 | \
2020    mips*:mips64 | \
2021    ppc*:ppc64 | \
2022    sparc:sparc64 | \
2023    "$cpu:$cpu")
2024      try='native cross' ;;
2025  esac
2026  eval "target_cflags=\${cross_cc_cflags_$target_arch}"
2027  for thistry in $try; do
2028    case $thistry in
2029    native)
2030      target_cc=$cc
2031      target_ccas=$ccas
2032      target_ar=$ar
2033      target_as=$as
2034      target_ld=$ld
2035      target_nm=$nm
2036      target_objcopy=$objcopy
2037      target_ranlib=$ranlib
2038      target_strip=$strip
2039      ;;
2040    cross)
2041      target_cc=
2042      if eval test -n "\"\${cross_cc_$target_arch}\""; then
2043        if eval has "\"\${cross_cc_$target_arch}\""; then
2044          eval "target_cc=\"\${cross_cc_$target_arch}\""
2045        fi
2046      else
2047        compute_target_variable $target_arch target_cc gcc
2048      fi
2049      target_ccas=$target_cc
2050      compute_target_variable $target_arch target_ar ar
2051      compute_target_variable $target_arch target_as as
2052      compute_target_variable $target_arch target_ld ld
2053      compute_target_variable $target_arch target_nm nm
2054      compute_target_variable $target_arch target_objcopy objcopy
2055      compute_target_variable $target_arch target_ranlib ranlib
2056      compute_target_variable $target_arch target_strip strip
2057      ;;
2058    esac
2059
2060    if test -n "$target_cc"; then
2061      case $target_arch in
2062        i386|x86_64)
2063          if $target_cc --version | grep -qi "clang"; then
2064            continue
2065          fi
2066          ;;
2067      esac
2068    elif test -n "$target_as" && test -n "$target_ld"; then
2069      # Special handling for assembler only targets
2070      case $target in
2071        tricore-softmmu)
2072          build_static=
2073          got_cross_cc=yes
2074          break
2075          ;;
2076        *)
2077          continue
2078          ;;
2079      esac
2080    else
2081      continue
2082    fi
2083
2084    write_c_skeleton
2085    case $1 in
2086      *-softmmu)
2087        if do_compiler "$target_cc" $target_cflags -o $TMPO -c $TMPC &&
2088          do_compiler "$target_cc" $target_cflags -r -nostdlib -o "${TMPDIR1}/${TMPB}2.o" "$TMPO" -lgcc; then
2089          got_cross_cc=yes
2090          break
2091        fi
2092        ;;
2093      *)
2094        if do_compiler "$target_cc" $target_cflags -o $TMPE $TMPC -static ; then
2095          build_static=y
2096          got_cross_cc=yes
2097          break
2098        fi
2099        if do_compiler "$target_cc" $target_cflags -o $TMPE $TMPC ; then
2100          build_static=
2101          got_cross_cc=yes
2102          break
2103        fi
2104        ;;
2105    esac
2106  done
2107  if test $got_cross_cc != yes; then
2108    build_static=
2109    target_cc=
2110    target_ccas=
2111    target_ar=
2112    target_as=
2113    target_ld=
2114    target_nm=
2115    target_objcopy=
2116    target_ranlib=
2117    target_strip=
2118  fi
2119  test -n "$target_cc"
2120}
2121
2122write_target_makefile() {
2123  echo "EXTRA_CFLAGS=$target_cflags"
2124  if test -z "$target_cc" && test -z "$target_as"; then
2125    test -z "$container_image" && error_exit "Internal error: could not find cross compiler for $1?"
2126    echo "$1: docker-image-$container_image" >> Makefile.prereqs
2127    if test -n "$container_cross_cc"; then
2128      echo "CC=$docker_py cc --cc $container_cross_cc -i qemu/$container_image -s $source_path --"
2129      echo "CCAS=$docker_py cc --cc $container_cross_cc -i qemu/$container_image -s $source_path --"
2130    fi
2131    echo "AR=$docker_py cc --cc $container_cross_ar -i qemu/$container_image -s $source_path --"
2132    echo "AS=$docker_py cc --cc $container_cross_as -i qemu/$container_image -s $source_path --"
2133    echo "LD=$docker_py cc --cc $container_cross_ld -i qemu/$container_image -s $source_path --"
2134    echo "NM=$docker_py cc --cc $container_cross_nm -i qemu/$container_image -s $source_path --"
2135    echo "OBJCOPY=$docker_py cc --cc $container_cross_objcopy -i qemu/$container_image -s $source_path --"
2136    echo "RANLIB=$docker_py cc --cc $container_cross_ranlib -i qemu/$container_image -s $source_path --"
2137    echo "STRIP=$docker_py cc --cc $container_cross_strip -i qemu/$container_image -s $source_path --"
2138  else
2139    if test -n "$target_cc"; then
2140      echo "CC=$target_cc"
2141      echo "CCAS=$target_ccas"
2142    fi
2143    if test -n "$target_ar"; then
2144      echo "AR=$target_ar"
2145    fi
2146    if test -n "$target_as"; then
2147      echo "AS=$target_as"
2148    fi
2149    if test -n "$target_ld"; then
2150      echo "LD=$target_ld"
2151    fi
2152    if test -n "$target_nm"; then
2153      echo "NM=$target_nm"
2154    fi
2155    if test -n "$target_objcopy"; then
2156      echo "OBJCOPY=$target_objcopy"
2157    fi
2158    if test -n "$target_ranlib"; then
2159      echo "RANLIB=$target_ranlib"
2160    fi
2161    if test -n "$target_strip"; then
2162      echo "STRIP=$target_strip"
2163    fi
2164  fi
2165}
2166
2167##########################################
2168# check for vfio_user_server
2169
2170case "$vfio_user_server" in
2171  enabled )
2172    if test "$git_submodules_action" != "ignore"; then
2173      git_submodules="${git_submodules} subprojects/libvfio-user"
2174    fi
2175    ;;
2176esac
2177
2178##########################################
2179# End of CC checks
2180# After here, no more $cc or $ld runs
2181
2182write_c_skeleton
2183
2184if test "$fortify_source" = "yes" ; then
2185  QEMU_CFLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 $QEMU_CFLAGS"
2186fi
2187
2188if test "$have_asan" = "yes"; then
2189  QEMU_CFLAGS="-fsanitize=address $QEMU_CFLAGS"
2190  QEMU_LDFLAGS="-fsanitize=address $QEMU_LDFLAGS"
2191  if test "$have_asan_iface_h" = "no" ; then
2192      echo "ASAN build enabled, but ASAN header missing." \
2193           "Without code annotation, the report may be inferior."
2194  elif test "$have_asan_iface_fiber" = "no" ; then
2195      echo "ASAN build enabled, but ASAN header is too old." \
2196           "Without code annotation, the report may be inferior."
2197  fi
2198fi
2199if test "$have_tsan" = "yes" ; then
2200  if test "$have_tsan_iface_fiber" = "yes" ; then
2201    QEMU_CFLAGS="-fsanitize=thread $QEMU_CFLAGS"
2202    QEMU_LDFLAGS="-fsanitize=thread $QEMU_LDFLAGS"
2203  else
2204    error_exit "Cannot enable TSAN due to missing fiber annotation interface."
2205  fi
2206elif test "$tsan" = "yes" ; then
2207  error_exit "Cannot enable TSAN due to missing sanitize thread interface."
2208fi
2209if test "$have_ubsan" = "yes"; then
2210  QEMU_CFLAGS="-fsanitize=undefined $QEMU_CFLAGS"
2211  QEMU_LDFLAGS="-fsanitize=undefined $QEMU_LDFLAGS"
2212fi
2213
2214#######################################
2215# cross-compiled firmware targets
2216
2217# Set up build tree symlinks that point back into the source tree
2218# (these can be both files and directories).
2219# Caution: avoid adding files or directories here using wildcards. This
2220# will result in problems later if a new file matching the wildcard is
2221# added to the source tree -- nothing will cause configure to be rerun
2222# so the build tree will be missing the link back to the new file, and
2223# tests might fail. Prefer to keep the relevant files in their own
2224# directory and symlink the directory instead.
2225LINKS="Makefile"
2226LINKS="$LINKS pc-bios/optionrom/Makefile"
2227LINKS="$LINKS pc-bios/s390-ccw/Makefile"
2228LINKS="$LINKS pc-bios/vof/Makefile"
2229LINKS="$LINKS .gdbinit scripts" # scripts needed by relative path in .gdbinit
2230LINKS="$LINKS tests/avocado tests/data"
2231LINKS="$LINKS tests/qemu-iotests/check"
2232LINKS="$LINKS python"
2233LINKS="$LINKS contrib/plugins/Makefile "
2234for f in $LINKS ; do
2235    if [ -e "$source_path/$f" ]; then
2236        mkdir -p "$(dirname ./"$f")"
2237        symlink "$source_path/$f" "$f"
2238    fi
2239done
2240
2241echo "# Automatically generated by configure - do not modify" > Makefile.prereqs
2242
2243# Mac OS X ships with a broken assembler
2244roms=
2245if have_target i386-softmmu x86_64-softmmu && \
2246        test "$targetos" != "darwin" && test "$targetos" != "sunos" && \
2247        test "$targetos" != "haiku" && \
2248        probe_target_compiler i386-softmmu; then
2249    roms="pc-bios/optionrom"
2250    config_mak=pc-bios/optionrom/config.mak
2251    echo "# Automatically generated by configure - do not modify" > $config_mak
2252    echo "TOPSRC_DIR=$source_path" >> $config_mak
2253    write_target_makefile >> $config_mak
2254fi
2255
2256if have_target ppc-softmmu ppc64-softmmu && \
2257        probe_target_compiler ppc-softmmu; then
2258    roms="$roms pc-bios/vof"
2259    config_mak=pc-bios/vof/config.mak
2260    echo "# Automatically generated by configure - do not modify" > $config_mak
2261    echo "SRC_DIR=$source_path/pc-bios/vof" >> $config_mak
2262    write_target_makefile >> $config_mak
2263fi
2264
2265# Only build s390-ccw bios if the compiler has -march=z900 or -march=z10
2266# (which is the lowest architecture level that Clang supports)
2267if have_target s390x-softmmu && probe_target_compiler s390x-softmmu; then
2268  write_c_skeleton
2269  do_compiler "$target_cc" $target_cc_cflags -march=z900 -o $TMPO -c $TMPC
2270  has_z900=$?
2271  if [ $has_z900 = 0 ] || do_compiler "$target_cc" $target_cc_cflags -march=z10 -msoft-float -Werror -o $TMPO -c $TMPC; then
2272    if [ $has_z900 != 0 ]; then
2273      echo "WARNING: Your compiler does not support the z900!"
2274      echo "         The s390-ccw bios will only work with guest CPUs >= z10."
2275    fi
2276    roms="$roms pc-bios/s390-ccw"
2277    config_mak=pc-bios/s390-ccw/config-host.mak
2278    echo "# Automatically generated by configure - do not modify" > $config_mak
2279    echo "SRC_PATH=$source_path/pc-bios/s390-ccw" >> $config_mak
2280    write_target_makefile >> $config_mak
2281    # SLOF is required for building the s390-ccw firmware on s390x,
2282    # since it is using the libnet code from SLOF for network booting.
2283    git_submodules="${git_submodules} roms/SLOF"
2284  fi
2285fi
2286
2287#######################################
2288# generate config-host.mak
2289
2290if ! (GIT="$git" "$source_path/scripts/git-submodule.sh" "$git_submodules_action" "$git_submodules"); then
2291    exit 1
2292fi
2293
2294config_host_mak="config-host.mak"
2295
2296echo "# Automatically generated by configure - do not modify" > $config_host_mak
2297echo >> $config_host_mak
2298
2299echo all: >> $config_host_mak
2300echo "GIT=$git" >> $config_host_mak
2301echo "GIT_SUBMODULES=$git_submodules" >> $config_host_mak
2302echo "GIT_SUBMODULES_ACTION=$git_submodules_action" >> $config_host_mak
2303
2304if test "$debug_tcg" = "yes" ; then
2305  echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak
2306fi
2307if test "$mingw32" = "yes" ; then
2308  echo "CONFIG_WIN32=y" >> $config_host_mak
2309  echo "QEMU_GA_MANUFACTURER=${QEMU_GA_MANUFACTURER-QEMU}" >> $config_host_mak
2310  echo "QEMU_GA_DISTRO=${QEMU_GA_DISTRO-Linux}" >> $config_host_mak
2311  echo "QEMU_GA_VERSION=${QEMU_GA_VERSION-$(cat "$source_path"/VERSION)}" >> $config_host_mak
2312else
2313  echo "CONFIG_POSIX=y" >> $config_host_mak
2314fi
2315
2316if test "$linux" = "yes" ; then
2317  echo "CONFIG_LINUX=y" >> $config_host_mak
2318fi
2319
2320if test "$darwin" = "yes" ; then
2321  echo "CONFIG_DARWIN=y" >> $config_host_mak
2322fi
2323
2324if test "$solaris" = "yes" ; then
2325  echo "CONFIG_SOLARIS=y" >> $config_host_mak
2326fi
2327if test "$static" = "yes" ; then
2328  echo "CONFIG_STATIC=y" >> $config_host_mak
2329fi
2330echo "SRC_PATH=$source_path" >> $config_host_mak
2331echo "TARGET_DIRS=$target_list" >> $config_host_mak
2332if test "$modules" = "yes"; then
2333  echo "CONFIG_MODULES=y" >> $config_host_mak
2334fi
2335
2336# XXX: suppress that
2337if [ "$bsd" = "yes" ] ; then
2338  echo "CONFIG_BSD=y" >> $config_host_mak
2339fi
2340
2341echo "CONFIG_COROUTINE_BACKEND=$coroutine" >> $config_host_mak
2342
2343if test "$have_asan_iface_fiber" = "yes" ; then
2344    echo "CONFIG_ASAN_IFACE_FIBER=y" >> $config_host_mak
2345fi
2346
2347if test "$have_tsan" = "yes" && test "$have_tsan_iface_fiber" = "yes" ; then
2348    echo "CONFIG_TSAN=y" >> $config_host_mak
2349fi
2350
2351if test "$plugins" = "yes" ; then
2352    echo "CONFIG_PLUGIN=y" >> $config_host_mak
2353fi
2354
2355if test -n "$gdb_bin"; then
2356    gdb_version=$($gdb_bin --version | head -n 1)
2357    if version_ge ${gdb_version##* } 9.1; then
2358        echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
2359    else
2360        gdb_bin=""
2361    fi
2362fi
2363
2364if test "$container" != no; then
2365    echo "ENGINE=$container" >> $config_host_mak
2366fi
2367echo "ROMS=$roms" >> $config_host_mak
2368echo "MAKE=$make" >> $config_host_mak
2369echo "PYTHON=$python" >> $config_host_mak
2370echo "GENISOIMAGE=$genisoimage" >> $config_host_mak
2371echo "MESON=$meson" >> $config_host_mak
2372echo "NINJA=$ninja" >> $config_host_mak
2373echo "CC=$cc" >> $config_host_mak
2374echo "QEMU_CFLAGS=$QEMU_CFLAGS" >> $config_host_mak
2375echo "QEMU_OBJCFLAGS=$QEMU_OBJCFLAGS" >> $config_host_mak
2376echo "GLIB_CFLAGS=$glib_cflags" >> $config_host_mak
2377echo "GLIB_LIBS=$glib_libs" >> $config_host_mak
2378echo "GLIB_BINDIR=$glib_bindir" >> $config_host_mak
2379echo "GLIB_VERSION=$($pkg_config --modversion glib-2.0)" >> $config_host_mak
2380echo "QEMU_LDFLAGS=$QEMU_LDFLAGS" >> $config_host_mak
2381echo "EXESUF=$EXESUF" >> $config_host_mak
2382
2383# use included Linux headers
2384if test "$linux" = "yes" ; then
2385  mkdir -p linux-headers
2386  case "$cpu" in
2387  i386|x86_64)
2388    linux_arch=x86
2389    ;;
2390  ppc|ppc64)
2391    linux_arch=powerpc
2392    ;;
2393  s390x)
2394    linux_arch=s390
2395    ;;
2396  aarch64)
2397    linux_arch=arm64
2398    ;;
2399  loongarch*)
2400    linux_arch=loongarch
2401    ;;
2402  mips64)
2403    linux_arch=mips
2404    ;;
2405  *)
2406    # For most CPUs the kernel architecture name and QEMU CPU name match.
2407    linux_arch="$cpu"
2408    ;;
2409  esac
2410    # For non-KVM architectures we will not have asm headers
2411    if [ -e "$source_path/linux-headers/asm-$linux_arch" ]; then
2412      symlink "$source_path/linux-headers/asm-$linux_arch" linux-headers/asm
2413    fi
2414fi
2415
2416for target in $target_list; do
2417    target_dir="$target"
2418    target_name=$(echo $target | cut -d '-' -f 1)$EXESUF
2419    mkdir -p "$target_dir"
2420    case $target in
2421        *-user) symlink "../qemu-$target_name" "$target_dir/qemu-$target_name" ;;
2422        *) symlink "../qemu-system-$target_name" "$target_dir/qemu-system-$target_name" ;;
2423    esac
2424done
2425
2426if test "$default_targets" = "yes"; then
2427  echo "CONFIG_DEFAULT_TARGETS=y" >> $config_host_mak
2428fi
2429
2430if test "$ccache_cpp2" = "yes"; then
2431  echo "export CCACHE_CPP2=y" >> $config_host_mak
2432fi
2433
2434if test "$safe_stack" = "yes"; then
2435  echo "CONFIG_SAFESTACK=y" >> $config_host_mak
2436fi
2437
2438# tests/tcg configuration
2439(config_host_mak=tests/tcg/config-host.mak
2440mkdir -p tests/tcg
2441echo "# Automatically generated by configure - do not modify" > $config_host_mak
2442echo "SRC_PATH=$source_path" >> $config_host_mak
2443echo "HOST_CC=$host_cc" >> $config_host_mak
2444
2445# versioned checked in the main config_host.mak above
2446if test -n "$gdb_bin"; then
2447    echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
2448fi
2449if test "$plugins" = "yes" ; then
2450    echo "CONFIG_PLUGIN=y" >> $config_host_mak
2451fi
2452
2453tcg_tests_targets=
2454for target in $target_list; do
2455  arch=${target%%-*}
2456
2457  case $target in
2458    xtensa*-linux-user)
2459      # the toolchain is not complete with headers, only build softmmu tests
2460      continue
2461      ;;
2462    *-softmmu)
2463      test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || continue
2464      qemu="qemu-system-$arch"
2465      ;;
2466    *-linux-user|*-bsd-user)
2467      qemu="qemu-$arch"
2468      ;;
2469  esac
2470
2471  if probe_target_compiler $target || test -n "$container_image"; then
2472      test -n "$container_image" && build_static=y
2473      mkdir -p "tests/tcg/$target"
2474      config_target_mak=tests/tcg/$target/config-target.mak
2475      ln -sf "$source_path/tests/tcg/Makefile.target" "tests/tcg/$target/Makefile"
2476      echo "# Automatically generated by configure - do not modify" > "$config_target_mak"
2477      echo "TARGET_NAME=$arch" >> "$config_target_mak"
2478      echo "TARGET=$target" >> "$config_target_mak"
2479      write_target_makefile "build-tcg-tests-$target" >> "$config_target_mak"
2480      echo "BUILD_STATIC=$build_static" >> "$config_target_mak"
2481      echo "QEMU=$PWD/$qemu" >> "$config_target_mak"
2482      echo "run-tcg-tests-$target: $qemu\$(EXESUF)" >> Makefile.prereqs
2483      tcg_tests_targets="$tcg_tests_targets $target"
2484  fi
2485done
2486echo "TCG_TESTS_TARGETS=$tcg_tests_targets" >> config-host.mak)
2487
2488if test "$skip_meson" = no; then
2489  cross="config-meson.cross.new"
2490  meson_quote() {
2491    test $# = 0 && return
2492    echo "'$(echo $* | sed "s/ /','/g")'"
2493  }
2494
2495  echo "# Automatically generated by configure - do not modify" > $cross
2496  echo "[properties]" >> $cross
2497
2498  # unroll any custom device configs
2499  for a in $device_archs; do
2500      eval "c=\$devices_${a}"
2501      echo "${a}-softmmu = '$c'" >> $cross
2502  done
2503
2504  echo "[built-in options]" >> $cross
2505  echo "c_args = [$(meson_quote $CFLAGS $EXTRA_CFLAGS)]" >> $cross
2506  echo "cpp_args = [$(meson_quote $CXXFLAGS $EXTRA_CXXFLAGS)]" >> $cross
2507  test -n "$objcc" && echo "objc_args = [$(meson_quote $OBJCFLAGS $EXTRA_OBJCFLAGS)]" >> $cross
2508  echo "c_link_args = [$(meson_quote $CFLAGS $LDFLAGS $EXTRA_CFLAGS $EXTRA_LDFLAGS)]" >> $cross
2509  echo "cpp_link_args = [$(meson_quote $CXXFLAGS $LDFLAGS $EXTRA_CXXFLAGS $EXTRA_LDFLAGS)]" >> $cross
2510  echo "[binaries]" >> $cross
2511  echo "c = [$(meson_quote $cc $CPU_CFLAGS)]" >> $cross
2512  test -n "$cxx" && echo "cpp = [$(meson_quote $cxx $CPU_CFLAGS)]" >> $cross
2513  test -n "$objcc" && echo "objc = [$(meson_quote $objcc $CPU_CFLAGS)]" >> $cross
2514  echo "ar = [$(meson_quote $ar)]" >> $cross
2515  echo "nm = [$(meson_quote $nm)]" >> $cross
2516  echo "pkgconfig = [$(meson_quote $pkg_config_exe)]" >> $cross
2517  echo "ranlib = [$(meson_quote $ranlib)]" >> $cross
2518  if has $sdl2_config; then
2519    echo "sdl2-config = [$(meson_quote $sdl2_config)]" >> $cross
2520  fi
2521  echo "strip = [$(meson_quote $strip)]" >> $cross
2522  echo "widl = [$(meson_quote $widl)]" >> $cross
2523  echo "windres = [$(meson_quote $windres)]" >> $cross
2524  echo "windmc = [$(meson_quote $windmc)]" >> $cross
2525  if test "$cross_compile" = "yes"; then
2526    cross_arg="--cross-file config-meson.cross"
2527    echo "[host_machine]" >> $cross
2528    echo "system = '$targetos'" >> $cross
2529    case "$cpu" in
2530        i386)
2531            echo "cpu_family = 'x86'" >> $cross
2532            ;;
2533        *)
2534            echo "cpu_family = '$cpu'" >> $cross
2535            ;;
2536    esac
2537    echo "cpu = '$cpu'" >> $cross
2538    if test "$bigendian" = "yes" ; then
2539        echo "endian = 'big'" >> $cross
2540    else
2541        echo "endian = 'little'" >> $cross
2542    fi
2543  else
2544    cross_arg="--native-file config-meson.cross"
2545  fi
2546  mv $cross config-meson.cross
2547
2548  rm -rf meson-private meson-info meson-logs
2549
2550  # Built-in options
2551  test "$bindir" != "bin" && meson_option_add "-Dbindir=$bindir"
2552  test "$default_feature" = no && meson_option_add -Dauto_features=disabled
2553  test "$pie" = no && meson_option_add -Db_pie=false
2554  test "$werror" = yes && meson_option_add -Dwerror=true
2555
2556  # QEMU options
2557  test "$cfi" != false && meson_option_add "-Dcfi=$cfi"
2558  test "$fdt" != auto && meson_option_add "-Dfdt=$fdt"
2559  test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE"
2560  test "$qemu_suffix" != qemu && meson_option_add "-Dqemu_suffix=$qemu_suffix"
2561  test "$smbd" != '' && meson_option_add "-Dsmbd=$smbd"
2562  test "$tcg" != enabled && meson_option_add "-Dtcg=$tcg"
2563  test "$vfio_user_server" != auto && meson_option_add "-Dvfio_user_server=$vfio_user_server"
2564  run_meson() {
2565    NINJA=$ninja $meson setup --prefix "$prefix" "$@" $cross_arg "$PWD" "$source_path"
2566  }
2567  eval run_meson $meson_options
2568  if test "$?" -ne 0 ; then
2569      error_exit "meson setup failed"
2570  fi
2571fi
2572
2573# Save the configure command line for later reuse.
2574cat <<EOD >config.status
2575#!/bin/sh
2576# Generated by configure.
2577# Run this file to recreate the current configuration.
2578# Compiler output produced by configure, useful for debugging
2579# configure, is in config.log if it exists.
2580EOD
2581
2582preserve_env() {
2583    envname=$1
2584
2585    eval envval=\$$envname
2586
2587    if test -n "$envval"
2588    then
2589	echo "$envname='$envval'" >> config.status
2590	echo "export $envname" >> config.status
2591    else
2592	echo "unset $envname" >> config.status
2593    fi
2594}
2595
2596# Preserve various env variables that influence what
2597# features/build target configure will detect
2598preserve_env AR
2599preserve_env AS
2600preserve_env CC
2601preserve_env CFLAGS
2602preserve_env CXX
2603preserve_env CXXFLAGS
2604preserve_env LD
2605preserve_env LDFLAGS
2606preserve_env LD_LIBRARY_PATH
2607preserve_env MAKE
2608preserve_env NM
2609preserve_env OBJCFLAGS
2610preserve_env OBJCOPY
2611preserve_env PATH
2612preserve_env PKG_CONFIG
2613preserve_env PKG_CONFIG_LIBDIR
2614preserve_env PKG_CONFIG_PATH
2615preserve_env PYTHON
2616preserve_env QEMU_GA_MANUFACTURER
2617preserve_env QEMU_GA_DISTRO
2618preserve_env QEMU_GA_VERSION
2619preserve_env SDL2_CONFIG
2620preserve_env SMBD
2621preserve_env STRIP
2622preserve_env WIDL
2623preserve_env WINDRES
2624preserve_env WINDMC
2625
2626printf "exec" >>config.status
2627for i in "$0" "$@"; do
2628  test "$i" = --skip-meson || printf " %s" "$(quote_sh "$i")" >>config.status
2629done
2630echo ' "$@"' >>config.status
2631chmod +x config.status
2632
2633rm -r "$TMPDIR1"
2634