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