OSDN Git Service

configure: clean up plugin option handling
[qmiga/qemu.git] / configure
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.  Unlike autoconf, we assume that unset exists.
8 unset CLICOLOR_FORCE GREP_OPTIONS BASH_ENV ENV MAIL MAILPATH CDPATH
9
10 # Don't allow CCACHE, if present, to use cached results of compile tests!
11 export CCACHE_RECACHE=yes
12
13 # make source path absolute
14 source_path=$(cd "$(dirname -- "$0")"; pwd)
15
16 if test "$PWD" = "$source_path"
17 then
18     echo "Using './build' as the directory for build output"
19
20     MARKER=build/auto-created-by-configure
21
22     if test -e build
23     then
24         if test -f $MARKER
25         then
26            rm -rf build
27         else
28             echo "ERROR: ./build dir already exists and was not previously created by configure"
29             exit 1
30         fi
31     fi
32
33     if ! mkdir build || ! touch $MARKER
34     then
35         echo "ERROR: Could not create ./build directory. Check the permissions on"
36         echo "your source directory, or try doing an out-of-tree build."
37         exit 1
38     fi
39
40     cat > GNUmakefile <<'EOF'
41 # This file is auto-generated by configure to support in-source tree
42 # 'make' command invocation
43
44 ifeq ($(MAKECMDGOALS),)
45 recurse: all
46 endif
47
48 .NOTPARALLEL: %
49 %: force
50         @echo 'changing dir to build for $(MAKE) "$(MAKECMDGOALS)"...'
51         @$(MAKE) -C build -f Makefile $(MAKECMDGOALS)
52         @if test "$(MAKECMDGOALS)" = "distclean" && \
53             test -e build/auto-created-by-configure ; \
54         then \
55             rm -rf build GNUmakefile ; \
56         fi
57 force: ;
58 .PHONY: force
59 GNUmakefile: ;
60
61 EOF
62     cd build
63     exec "$source_path/configure" "$@"
64 fi
65
66 # Temporary directory used for files created while
67 # configure runs. Since it is in the build directory
68 # we can safely blow away any previous version of it
69 # (and we need not jump through hoops to try to delete
70 # it when configure exits.)
71 TMPDIR1="config-temp"
72 rm -rf "${TMPDIR1}"
73 if ! mkdir -p "${TMPDIR1}"; then
74     echo "ERROR: failed to create temporary directory"
75     exit 1
76 fi
77
78 TMPB="qemu-conf"
79 TMPC="${TMPDIR1}/${TMPB}.c"
80 TMPO="${TMPDIR1}/${TMPB}.o"
81 TMPE="${TMPDIR1}/${TMPB}.exe"
82
83 rm -f config.log
84
85 # Print a helpful header at the top of config.log
86 echo "# QEMU configure log $(date)" >> config.log
87 printf "# Configured with:" >> config.log
88 # repeat the invocation to log and stdout for CI
89 invoke=$(printf " '%s'" "$0" "$@")
90 test -n "$GITLAB_CI" && echo "configuring with: $invoke"
91 { echo "$invoke"; echo; echo "#"; } >> config.log
92
93 quote_sh() {
94     printf "%s" "$1" | sed "s,','\\\\'',g; s,.*,'&',"
95 }
96
97 print_error() {
98     (echo
99     echo "ERROR: $1"
100     while test -n "$2"; do
101         echo "       $2"
102         shift
103     done
104     echo) >&2
105 }
106
107 error_exit() {
108     print_error "$@"
109     exit 1
110 }
111
112 do_compiler() {
113   # Run the compiler, capturing its output to the log. First argument
114   # is compiler binary to execute.
115   compiler="$1"
116   shift
117   if test -n "$BASH_VERSION"; then eval '
118       echo >>config.log "
119 funcs: ${FUNCNAME[*]}
120 lines: ${BASH_LINENO[*]}"
121   '; fi
122   echo $compiler "$@" >> config.log
123   $compiler "$@" >> config.log 2>&1 || return $?
124 }
125
126 do_cc() {
127     do_compiler "$cc" $CPU_CFLAGS "$@"
128 }
129
130 compile_object() {
131   local_cflags="$1"
132   do_cc $CFLAGS $EXTRA_CFLAGS $local_cflags -c -o $TMPO $TMPC
133 }
134
135 compile_prog() {
136   local_cflags="$1"
137   local_ldflags="$2"
138   do_cc $CFLAGS $EXTRA_CFLAGS $local_cflags -o $TMPE $TMPC \
139       $LDFLAGS $EXTRA_LDFLAGS $local_ldflags
140 }
141
142 # symbolically link $1 to $2.  Portable version of "ln -sf".
143 symlink() {
144   rm -rf "$2"
145   mkdir -p "$(dirname "$2")"
146   ln -s "$1" "$2"
147 }
148
149 # check whether a command is available to this shell (may be either an
150 # executable or a builtin)
151 has() {
152     type "$1" >/dev/null 2>&1
153 }
154
155 version_ge () {
156     local_ver1=$(expr "$1" : '\([0-9.]*\)' | tr . ' ')
157     local_ver2=$(echo "$2" | tr . ' ')
158     while true; do
159         set x $local_ver1
160         local_first=${2-0}
161         # 'shift 2' if $2 is set, or 'shift' if $2 is not set
162         shift ${2:+2}
163         local_ver1=$*
164         set x $local_ver2
165         # the second argument finished, the first must be greater or equal
166         test $# = 1 && return 0
167         test $local_first -lt $2 && return 1
168         test $local_first -gt $2 && return 0
169         shift ${2:+2}
170         local_ver2=$*
171     done
172 }
173
174 if printf %s\\n "$source_path" "$PWD" | grep -q "[[:space:]:]";
175 then
176   error_exit "main directory cannot contain spaces nor colons"
177 fi
178
179 # parse CC options first; some compiler tests are used to establish
180 # some defaults, based on the host environment
181
182 # default parameters
183 container_engine="auto"
184 cpu=""
185 cross_compile="no"
186 cross_prefix=""
187 host_cc="cc"
188 EXTRA_CFLAGS=""
189 EXTRA_CXXFLAGS=""
190 EXTRA_OBJCFLAGS=""
191 EXTRA_LDFLAGS=""
192
193 # Default value for a variable defining feature "foo".
194 #  * foo="no"  feature will only be used if --enable-foo arg is given
195 #  * foo=""    feature will be searched for, and if found, will be used
196 #              unless --disable-foo is given
197 #  * foo="yes" this value will only be set by --enable-foo flag.
198 #              feature will searched for,
199 #              if not found, configure exits with error
200 #
201 # Always add --enable-foo and --disable-foo command line args.
202 # Distributions want to ensure that several features are compiled in, and it
203 # is impossible without a --enable-foo that exits if a feature is not found.
204 default_feature=""
205
206 for opt do
207   optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
208   case "$opt" in
209   --cross-prefix=*) cross_prefix="$optarg"
210                     cross_compile="yes"
211   ;;
212   --cc=*) CC="$optarg"
213   ;;
214   --cxx=*) CXX="$optarg"
215   ;;
216   --objcc=*) objcc="$optarg"
217   ;;
218   --cpu=*) cpu="$optarg"
219   ;;
220   --extra-cflags=*)
221     EXTRA_CFLAGS="$EXTRA_CFLAGS $optarg"
222     EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $optarg"
223     EXTRA_OBJCFLAGS="$EXTRA_OBJCFLAGS $optarg"
224     ;;
225   --extra-cxxflags=*) EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $optarg"
226   ;;
227   --extra-objcflags=*) EXTRA_OBJCFLAGS="$EXTRA_OBJCFLAGS $optarg"
228   ;;
229   --extra-ldflags=*) EXTRA_LDFLAGS="$EXTRA_LDFLAGS $optarg"
230   ;;
231   --cross-cc-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --cross-cc-FOO option"
232   ;;
233   --cross-cc-cflags-*) cc_arch=${opt#--cross-cc-cflags-}; cc_arch=${cc_arch%%=*}
234                       eval "cross_cc_cflags_${cc_arch}=\$optarg"
235   ;;
236   --cross-cc-*) cc_arch=${opt#--cross-cc-}; cc_arch=${cc_arch%%=*}
237                 eval "cross_cc_${cc_arch}=\$optarg"
238   ;;
239   --cross-prefix-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --cross-prefix-FOO option"
240   ;;
241   --cross-prefix-*) cc_arch=${opt#--cross-prefix-}; cc_arch=${cc_arch%%=*}
242                     eval "cross_prefix_${cc_arch}=\$optarg"
243   ;;
244   --without-default-features) default_feature="no"
245   ;;
246   esac
247 done
248
249 default_cflags='-O2 -g'
250 git_submodules_action="update"
251 git="git"
252 docs="auto"
253 EXESUF=""
254 prefix="/usr/local"
255 qemu_suffix="qemu"
256 system="yes"
257 linux_user=""
258 bsd_user=""
259 plugins="$default_feature"
260 subdirs=""
261 ninja=""
262 python=
263 download="enabled"
264 bindir="bin"
265 skip_meson=no
266 use_containers="yes"
267 gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
268 gdb_arches=""
269 werror=""
270
271 # Don't accept a target_list environment variable.
272 unset target_list
273 unset target_list_exclude
274
275 # The following Meson options are handled manually (still they
276 # are included in the automatically generated help message)
277 # because they automatically enable/disable other options
278 tcg="auto"
279 cfi="false"
280
281 # Meson has PIE as a boolean rather than enabled/disabled/auto,
282 # and we also need to check for -static-pie before Meson runs
283 # which requires knowing whether --static is enabled.
284 pie=""
285 static="no"
286
287 # Preferred compiler:
288 #  ${CC} (if set)
289 #  ${cross_prefix}gcc (if cross-prefix specified)
290 #  system compiler
291 if test -z "${CC}${cross_prefix}"; then
292   cc="cc"
293 else
294   cc="${CC-${cross_prefix}gcc}"
295 fi
296
297 if test -z "${CXX}${cross_prefix}"; then
298   cxx="c++"
299 else
300   cxx="${CXX-${cross_prefix}g++}"
301 fi
302
303 # Preferred ObjC compiler:
304 # $objcc (if set, i.e. via --objcc option)
305 # ${cross_prefix}clang (if cross-prefix specified)
306 # clang (if available)
307 # $cc
308 if test -z "${objcc}${cross_prefix}"; then
309   if has clang; then
310     objcc=clang
311   else
312     objcc="$cc"
313   fi
314 else
315   objcc="${objcc-${cross_prefix}clang}"
316 fi
317
318 ar="${AR-${cross_prefix}ar}"
319 as="${AS-${cross_prefix}as}"
320 ccas="${CCAS-$cc}"
321 objcopy="${OBJCOPY-${cross_prefix}objcopy}"
322 ld="${LD-${cross_prefix}ld}"
323 ranlib="${RANLIB-${cross_prefix}ranlib}"
324 nm="${NM-${cross_prefix}nm}"
325 smbd="$SMBD"
326 strip="${STRIP-${cross_prefix}strip}"
327 widl="${WIDL-${cross_prefix}widl}"
328 windres="${WINDRES-${cross_prefix}windres}"
329 windmc="${WINDMC-${cross_prefix}windmc}"
330 pkg_config="${PKG_CONFIG-${cross_prefix}pkg-config}"
331 sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
332
333 check_define() {
334 cat > $TMPC <<EOF
335 #if !defined($1)
336 #error $1 not defined
337 #endif
338 int main(void) { return 0; }
339 EOF
340   compile_object
341 }
342
343 write_c_skeleton() {
344     cat > $TMPC <<EOF
345 int main(void) { return 0; }
346 EOF
347 }
348
349 if check_define __linux__ ; then
350   targetos=linux
351 elif check_define _WIN32 ; then
352   targetos=windows
353 elif check_define __OpenBSD__ ; then
354   targetos=openbsd
355 elif check_define __sun__ ; then
356   targetos=sunos
357 elif check_define __HAIKU__ ; then
358   targetos=haiku
359 elif check_define __FreeBSD__ ; then
360   targetos=freebsd
361 elif check_define __FreeBSD_kernel__ && check_define __GLIBC__; then
362   targetos=gnu/kfreebsd
363 elif check_define __DragonFly__ ; then
364   targetos=dragonfly
365 elif check_define __NetBSD__; then
366   targetos=netbsd
367 elif check_define __APPLE__; then
368   targetos=darwin
369 else
370   # This is a fatal error, but don't report it yet, because we
371   # might be going to just print the --help text, or it might
372   # be the result of a missing compiler.
373   targetos=bogus
374 fi
375
376 # OS specific
377
378 case $targetos in
379 windows)
380   pie="no"
381 ;;
382 haiku)
383   pie="no"
384 ;;
385 esac
386
387 if test ! -z "$cpu" ; then
388   # command line argument
389   :
390 elif check_define __i386__ ; then
391   cpu="i386"
392 elif check_define __x86_64__ ; then
393   if check_define __ILP32__ ; then
394     cpu="x32"
395   else
396     cpu="x86_64"
397   fi
398 elif check_define __sparc__ ; then
399   if check_define __arch64__ ; then
400     cpu="sparc64"
401   else
402     cpu="sparc"
403   fi
404 elif check_define _ARCH_PPC ; then
405   if check_define _ARCH_PPC64 ; then
406     if check_define _LITTLE_ENDIAN ; then
407       cpu="ppc64le"
408     else
409       cpu="ppc64"
410     fi
411   else
412     cpu="ppc"
413   fi
414 elif check_define __mips__ ; then
415   cpu="mips"
416 elif check_define __s390__ ; then
417   if check_define __s390x__ ; then
418     cpu="s390x"
419   else
420     cpu="s390"
421   fi
422 elif check_define __riscv ; then
423   if check_define _LP64 ; then
424     cpu="riscv64"
425   else
426     cpu="riscv32"
427   fi
428 elif check_define __arm__ ; then
429   cpu="arm"
430 elif check_define __aarch64__ ; then
431   cpu="aarch64"
432 elif check_define __loongarch64 ; then
433   cpu="loongarch64"
434 else
435   # Using uname is really broken, but it is just a fallback for architectures
436   # that are going to use TCI anyway
437   cpu=$(uname -m)
438   echo "WARNING: unrecognized host CPU, proceeding with 'uname -m' output '$cpu'"
439 fi
440
441 # Normalise host CPU name to the values used by Meson cross files and in source
442 # directories, and set multilib cflags.  The canonicalization isn't really
443 # necessary, because the architectures that we check for should not hit the
444 # 'uname -m' case, but better safe than sorry in case --cpu= is used.
445 #
446 # Note that this case should only have supported host CPUs, not guests.
447 # Please keep it sorted and synchronized with meson.build's host_arch.
448 host_arch=
449 linux_arch=
450 case "$cpu" in
451   aarch64)
452     host_arch=aarch64
453     linux_arch=arm64
454     ;;
455
456   armv*b|armv*l|arm)
457     cpu=arm
458     host_arch=arm
459     linux_arch=arm
460     ;;
461
462   i386|i486|i586|i686)
463     cpu="i386"
464     host_arch=i386
465     linux_arch=x86
466     CPU_CFLAGS="-m32"
467     ;;
468
469   loongarch*)
470     cpu=loongarch64
471     host_arch=loongarch64
472     ;;
473
474   mips64*)
475     cpu=mips64
476     host_arch=mips
477     linux_arch=mips
478     ;;
479   mips*)
480     cpu=mips
481     host_arch=mips
482     linux_arch=mips
483     ;;
484
485   ppc)
486     host_arch=ppc
487     linux_arch=powerpc
488     CPU_CFLAGS="-m32"
489     ;;
490   ppc64)
491     host_arch=ppc64
492     linux_arch=powerpc
493     CPU_CFLAGS="-m64 -mbig-endian"
494     ;;
495   ppc64le)
496     cpu=ppc64
497     host_arch=ppc64
498     linux_arch=powerpc
499     CPU_CFLAGS="-m64 -mlittle-endian"
500     ;;
501
502   riscv32 | riscv64)
503     host_arch=riscv
504     linux_arch=riscv
505     ;;
506
507   s390)
508     linux_arch=s390
509     CPU_CFLAGS="-m31"
510     ;;
511   s390x)
512     host_arch=s390x
513     linux_arch=s390
514     CPU_CFLAGS="-m64"
515     ;;
516
517   sparc|sun4[cdmuv])
518     cpu=sparc
519     CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc"
520     ;;
521   sparc64)
522     host_arch=sparc64
523     CPU_CFLAGS="-m64 -mcpu=ultrasparc"
524     ;;
525
526   x32)
527     cpu="x86_64"
528     host_arch=x86_64
529     linux_arch=x86
530     CPU_CFLAGS="-mx32"
531     ;;
532   x86_64|amd64)
533     cpu="x86_64"
534     host_arch=x86_64
535     linux_arch=x86
536     # ??? Only extremely old AMD cpus do not have cmpxchg16b.
537     # If we truly care, we should simply detect this case at
538     # runtime and generate the fallback to serial emulation.
539     CPU_CFLAGS="-m64 -mcx16"
540     ;;
541 esac
542
543 if test -n "$host_arch" && {
544     ! test -d "$source_path/linux-user/include/host/$host_arch" ||
545     ! test -d "$source_path/common-user/host/$host_arch"; }; then
546     error_exit "linux-user/include/host/$host_arch does not exist." \
547        "This is a bug in the configure script, please report it."
548 fi
549 if test -n "$linux_arch" && ! test -d "$source_path/linux-headers/asm-$linux_arch"; then
550     error_exit "linux-headers/asm-$linux_arch does not exist." \
551        "This is a bug in the configure script, please report it."
552 fi
553
554 check_py_version() {
555     # We require python >= 3.8.
556     # NB: a True python conditional creates a non-zero return code (Failure)
557     "$1" -c 'import sys; sys.exit(sys.version_info < (3,8))'
558 }
559
560 first_python=
561 if test -z "${PYTHON}"; then
562     # A bare 'python' is traditionally python 2.x, but some distros
563     # have it as python 3.x, so check in both places.
564     for binary in python3 python python3.12 python3.11 \
565                           python3.10 python3.9 python3.8; do
566         if has "$binary"; then
567             python=$(command -v "$binary")
568             if check_py_version "$python"; then
569                 # This one is good.
570                 first_python=
571                 break
572             else
573                 first_python=$python
574             fi
575         fi
576     done
577 else
578     # Same as above, but only check the environment variable.
579     has "${PYTHON}" || error_exit "The PYTHON environment variable does not point to an executable"
580     python=$(command -v "$PYTHON")
581     if check_py_version "$python"; then
582         # This one is good.
583         first_python=
584     else
585         first_python=$first_python
586     fi
587 fi
588
589 # Check for ancillary tools used in testing
590 genisoimage=
591 for binary in genisoimage mkisofs
592 do
593     if has $binary
594     then
595         genisoimage=$(command -v "$binary")
596         break
597     fi
598 done
599
600 if test "$targetos" = "windows" ; then
601   EXESUF=".exe"
602   prefix="/qemu"
603   bindir=""
604   qemu_suffix=""
605 fi
606
607 meson_option_build_array() {
608   printf '['
609   (if test "$targetos" = windows; then
610     IFS=\;
611   else
612     IFS=:
613   fi
614   for e in $1; do
615     printf '"""'
616     # backslash escape any '\' and '"' characters
617     printf "%s" "$e" | sed -e 's/\([\"]\)/\\\1/g'
618     printf '""",'
619   done)
620   printf ']\n'
621 }
622
623 . "$source_path/scripts/meson-buildoptions.sh"
624
625 meson_options=
626 meson_option_add() {
627   local arg
628   for arg; do
629     meson_options="$meson_options $(quote_sh "$arg")"
630   done
631 }
632 meson_option_parse() {
633   meson_options="$meson_options $(_meson_option_parse "$@")"
634   if test $? -eq 1; then
635     echo "ERROR: unknown option $1"
636     echo "Try '$0 --help' for more information"
637     exit 1
638   fi
639 }
640
641 for opt do
642   optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
643   case "$opt" in
644   --help|-h) show_help=yes
645   ;;
646   --version|-V) exec cat "$source_path/VERSION"
647   ;;
648   --prefix=*) prefix="$optarg"
649   ;;
650   --cross-prefix=*)
651   ;;
652   --cc=*)
653   ;;
654   --host-cc=*) host_cc="$optarg"
655   ;;
656   --cxx=*)
657   ;;
658   --objcc=*)
659   ;;
660   --make=*)
661   ;;
662   --install=*)
663   ;;
664   --python=*) python="$optarg"
665   ;;
666   --skip-meson) skip_meson=yes
667   ;;
668   --ninja=*) ninja="$optarg"
669   ;;
670   --smbd=*) smbd="$optarg"
671   ;;
672   --extra-cflags=*)
673   ;;
674   --extra-cxxflags=*)
675   ;;
676   --extra-objcflags=*)
677   ;;
678   --extra-ldflags=*)
679   ;;
680   --cross-cc-*)
681   ;;
682   --cross-prefix-*)
683   ;;
684   --enable-docs) docs=enabled
685   ;;
686   --disable-docs) docs=disabled
687   ;;
688   --cpu=*)
689   ;;
690   --target-list=*) target_list="$optarg"
691                    if test "$target_list_exclude"; then
692                        error_exit "Can't mix --target-list with --target-list-exclude"
693                    fi
694   ;;
695   --target-list-exclude=*) target_list_exclude="$optarg"
696                    if test "$target_list"; then
697                        error_exit "Can't mix --target-list-exclude with --target-list"
698                    fi
699   ;;
700   --with-default-devices) meson_option_add -Ddefault_devices=true
701   ;;
702   --without-default-devices) meson_option_add -Ddefault_devices=false
703   ;;
704   --with-devices-*[!a-zA-Z0-9_-]*=*) error_exit "Passed bad --with-devices-FOO option"
705   ;;
706   --with-devices-*) device_arch=${opt#--with-devices-};
707                     device_arch=${device_arch%%=*}
708                     cf=$source_path/configs/devices/$device_arch-softmmu/$optarg.mak
709                     if test -f "$cf"; then
710                         device_archs="$device_archs $device_arch"
711                         eval "devices_${device_arch}=\$optarg"
712                     else
713                         error_exit "File $cf does not exist"
714                     fi
715   ;;
716   --without-default-features) # processed above
717   ;;
718   --static) static="yes"
719   ;;
720   --bindir=*) bindir="$optarg"
721   ;;
722   --with-suffix=*) qemu_suffix="$optarg"
723   ;;
724   --host=*|--build=*|\
725   --disable-dependency-tracking|\
726   --sbindir=*|--sharedstatedir=*|\
727   --oldincludedir=*|--datarootdir=*|--infodir=*|\
728   --htmldir=*|--dvidir=*|--pdfdir=*|--psdir=*)
729     # These switches are silently ignored, for compatibility with
730     # autoconf-generated configure scripts. This allows QEMU's
731     # configure to be used by RPM and similar macros that set
732     # lots of directory switches by default.
733   ;;
734   --enable-debug)
735       # Enable debugging options that aren't excessively noisy
736       meson_option_parse --enable-debug-tcg ""
737       meson_option_parse --enable-debug-graph-lock ""
738       meson_option_parse --enable-debug-mutex ""
739       meson_option_add -Doptimization=0
740       default_cflags='-O0 -g'
741   ;;
742   --disable-tcg) tcg="disabled"
743   ;;
744   --enable-tcg) tcg="enabled"
745   ;;
746   --disable-system) system="no"
747   ;;
748   --enable-system) system="yes"
749   ;;
750   --disable-user)
751       linux_user="no" ;
752       bsd_user="no" ;
753   ;;
754   --enable-user) ;;
755   --disable-linux-user) linux_user="no"
756   ;;
757   --enable-linux-user) linux_user="yes"
758   ;;
759   --disable-bsd-user) bsd_user="no"
760   ;;
761   --enable-bsd-user) bsd_user="yes"
762   ;;
763   --enable-pie) pie="yes"
764   ;;
765   --disable-pie) pie="no"
766   ;;
767   --enable-werror) werror="yes"
768   ;;
769   --disable-werror) werror="no"
770   ;;
771   --enable-cfi) cfi=true
772   ;;
773   --disable-cfi) cfi=false
774   ;;
775   --disable-download) download="disabled"; git_submodules_action=validate;
776   ;;
777   --enable-download) download="enabled"; git_submodules_action=update;
778   ;;
779   --enable-plugins) plugins="yes"
780   ;;
781   --disable-plugins) plugins="no"
782   ;;
783   --enable-containers) use_containers="yes"
784   ;;
785   --disable-containers) use_containers="no"
786   ;;
787   --container-engine=*) container_engine="$optarg"
788   ;;
789   --gdb=*) gdb_bin="$optarg"
790   ;;
791   # everything else has the same name in configure and meson
792   --*) meson_option_parse "$opt" "$optarg"
793   ;;
794   # Pass through -Dxxxx options to meson
795   -D*) meson_options="$meson_options $opt"
796   ;;
797   esac
798 done
799
800 if ! test -e "$source_path/.git"
801 then
802     git_submodules_action="validate"
803 fi
804
805 if ! test -f "$source_path/subprojects/keycodemapdb/README" \
806     && test "$download" = disabled
807 then
808     echo
809     echo "ERROR: missing subprojects"
810     echo
811     if test -e "$source_path/.git"; then
812         echo "--disable-download specified but subprojects were not"
813         echo 'checked out.  Please invoke "meson subprojects download"'
814         echo "before configuring QEMU, or remove --disable-download"
815         echo "from the command line."
816     else
817         echo "This is not a GIT checkout but subproject content appears to"
818         echo "be missing. Do not use 'git archive' or GitHub download links"
819         echo "to acquire QEMU source archives. Non-GIT builds are only"
820         echo "supported with source archives linked from:"
821         echo
822         echo "  https://www.qemu.org/download/#source"
823         echo
824         echo "Developers working with GIT can use scripts/archive-source.sh"
825         echo "if they need to create valid source archives."
826     fi
827     echo
828     exit 1
829 fi
830
831 default_target_list=""
832 mak_wilds=""
833
834 if [ -n "$host_arch" ] && [ -d "$source_path/common-user/host/$host_arch" ]; then
835     if [ "$linux_user" != no ]; then
836         if [ "$targetos" = linux ]; then
837             linux_user=yes
838         elif [ "$linux_user" = yes ]; then
839             error_exit "linux-user not supported on this architecture"
840         fi
841         if [ "$linux_user" = "yes" ]; then
842             mak_wilds="${mak_wilds} $source_path/configs/targets/*-linux-user.mak"
843         fi
844     fi
845     if [ "$bsd_user" != no ]; then
846         if [ "$bsd_user" = "" ]; then
847             test $targetos = freebsd && bsd_user=yes
848         fi
849         if [ "$bsd_user" = yes ] && ! [ -d "$source_path/bsd-user/$targetos" ]; then
850             error_exit "bsd-user not supported on this host OS"
851         fi
852         if [ "$bsd_user" = "yes" ]; then
853             mak_wilds="${mak_wilds} $source_path/configs/targets/*-bsd-user.mak"
854         fi
855     fi
856 else
857     if [ "$linux_user" = yes ] || [ "$bsd_user" = yes ]; then
858         error_exit "user mode emulation not supported on this architecture"
859     fi
860 fi
861 if [ "$system" = "yes" ]; then
862     mak_wilds="${mak_wilds} $source_path/configs/targets/*-softmmu.mak"
863 fi
864
865 for config in $mak_wilds; do
866     target="$(basename "$config" .mak)"
867     if echo "$target_list_exclude" | grep -vq "$target"; then
868         default_target_list="${default_target_list} $target"
869     fi
870 done
871
872 if test x"$show_help" = x"yes" ; then
873 cat << EOF
874
875 Usage: configure [options]
876 Options: [defaults in brackets after descriptions]
877
878 Standard options:
879   --help                   print this message
880   --prefix=PREFIX          install in PREFIX [$prefix]
881   --target-list=LIST       set target list (default: build all)
882 $(echo Available targets: $default_target_list | \
883   fold -s -w 53 | sed -e 's/^/                           /')
884   --target-list-exclude=LIST exclude a set of targets from the default target-list
885
886 Advanced options (experts only):
887   -Dmesonoptname=val       passthrough option to meson unmodified
888   --cross-prefix=PREFIX    use PREFIX for compile tools, PREFIX can be blank [$cross_prefix]
889   --cc=CC                  use C compiler CC [$cc]
890   --host-cc=CC             when cross compiling, use C compiler CC for code run
891                            at build time [$host_cc]
892   --cxx=CXX                use C++ compiler CXX [$cxx]
893   --objcc=OBJCC            use Objective-C compiler OBJCC [$objcc]
894   --extra-cflags=CFLAGS    append extra C compiler flags CFLAGS
895   --extra-cxxflags=CXXFLAGS append extra C++ compiler flags CXXFLAGS
896   --extra-objcflags=OBJCFLAGS append extra Objective C compiler flags OBJCFLAGS
897   --extra-ldflags=LDFLAGS  append extra linker flags LDFLAGS
898   --cross-cc-ARCH=CC       use compiler when building ARCH guest test cases
899   --cross-cc-cflags-ARCH=  use compiler flags when building ARCH guest tests
900   --cross-prefix-ARCH=PREFIX cross compiler prefix when building ARCH guest test cases
901   --python=PYTHON          use specified python [$python]
902   --ninja=NINJA            use specified ninja [$ninja]
903   --smbd=SMBD              use specified smbd [$smbd]
904   --static                 enable static build [$static]
905   --bindir=PATH            install binaries in PATH
906   --with-suffix=SUFFIX     suffix for QEMU data inside datadir/libdir/sysconfdir/docdir [$qemu_suffix]
907   --without-default-features default all --enable-* options to "disabled"
908   --without-default-devices  do not include any device that is not needed to
909                            start the emulator (only use if you are including
910                            desired devices in configs/devices/)
911   --with-devices-ARCH=NAME override default configs/devices
912   --enable-debug           enable common debug build options
913   --disable-werror         disable compilation abort on warning
914   --cpu=CPU                Build for host CPU [$cpu]
915   --disable-containers     don't use containers for cross-building
916   --container-engine=TYPE  which container engine to use [$container_engine]
917   --gdb=GDB-path           gdb to use for gdbstub tests [$gdb_bin]
918 EOF
919   meson_options_help
920 cat << EOF
921   system          all system emulation targets
922   user            supported user emulation targets
923   linux-user      all linux usermode emulation targets
924   bsd-user        all BSD usermode emulation targets
925   pie             Position Independent Executables
926
927 NOTE: The object files are built at the place where configure is launched
928 EOF
929 exit 0
930 fi
931
932 # Remove old dependency files to make sure that they get properly regenerated
933 rm -f ./*/config-devices.mak.d
934
935 if test -z "$python"
936 then
937     # If first_python is set, there was a binary somewhere even though
938     # it was not suitable.  Use it for the error message.
939     if test -n "$first_python"; then
940         error_exit "Cannot use '$first_python', Python >= 3.8 is required." \
941             "Use --python=/path/to/python to specify a supported Python."
942     else
943         error_exit "Python not found. Use --python=/path/to/python"
944     fi
945 fi
946
947 if ! check_py_version "$python"; then
948   error_exit "Cannot use '$python', Python >= 3.8 is required." \
949              "Use --python=/path/to/python to specify a supported Python." \
950              "Maybe try:" \
951              "  openSUSE Leap 15.3+: zypper install python39" \
952              "  CentOS 8: dnf install python38"
953 fi
954
955 # Resolve PATH
956 python="$(command -v "$python")"
957
958 # Create a Python virtual environment using our configured python.
959 # The stdout of this script will be the location of a symlink that
960 # points to the configured Python.
961 # Entry point scripts for pip, meson, and sphinx are generated if those
962 # packages are present.
963
964 # Defaults assumed for now:
965 # - venv is cleared if it exists already;
966 # - venv is allowed to use system packages;
967 # - all setup can be performed offline;
968 # - missing packages may be fetched from PyPI,
969 #   unless --disable-download is passed.
970 # - pip is not installed into the venv when possible,
971 #   but ensurepip is called as a fallback when necessary.
972
973 echo "python determined to be '$python'"
974 echo "python version: $($python --version)"
975
976 python="$($python -B "${source_path}/python/scripts/mkvenv.py" create pyvenv)"
977 if test "$?" -ne 0 ; then
978     error_exit "python venv creation failed"
979 fi
980
981 # Suppress writing compiled files
982 python="$python -B"
983 mkvenv="$python ${source_path}/python/scripts/mkvenv.py"
984
985 # Finish preparing the virtual environment using vendored .whl files
986
987 if $python -c 'import sys; sys.exit(sys.version_info >= (3,11))'; then
988     $mkvenv ensure --dir "${source_path}/python/wheels" \
989         'tomli>=1.2.0' || exit 1
990 fi
991 $mkvenv ensuregroup --dir "${source_path}/python/wheels" \
992      ${source_path}/pythondeps.toml meson || exit 1
993
994 # At this point, we expect Meson to be installed and available.
995 # We expect mkvenv or pip to have created pyvenv/bin/meson for us.
996 # We ignore PATH completely here: we want to use the venv's Meson
997 # *exclusively*.
998
999 meson="$(cd pyvenv/bin; pwd)/meson"
1000
1001 # Conditionally ensure Sphinx is installed.
1002
1003 mkvenv_flags=""
1004 if test "$download" = "enabled" -a "$docs" = "enabled" ; then
1005     mkvenv_flags="--online"
1006 fi
1007
1008 if test "$docs" != "disabled" ; then
1009     if ! $mkvenv ensuregroup \
1010          $mkvenv_flags \
1011          ${source_path}/pythondeps.toml docs;
1012     then
1013         if test "$docs" = "enabled" ; then
1014             exit 1
1015         fi
1016         echo "Sphinx not found/usable, disabling docs."
1017         docs=disabled
1018     else
1019         docs=enabled
1020     fi
1021 fi
1022
1023 # Probe for ninja
1024
1025 if test -z "$ninja"; then
1026     for c in ninja ninja-build samu; do
1027         if has $c; then
1028             ninja=$(command -v "$c")
1029             break
1030         fi
1031     done
1032     if test -z "$ninja"; then
1033       error_exit "Cannot find Ninja"
1034     fi
1035 fi
1036
1037 # Consult white-list to determine whether to enable werror
1038 # by default.  Only enable by default for git builds
1039 if test -z "$werror" ; then
1040     if test -e "$source_path/.git" && \
1041         { test "$targetos" = linux || test "$targetos" = "windows"; }; then
1042         werror="yes"
1043     else
1044         werror="no"
1045     fi
1046 fi
1047
1048 if test "$targetos" = "bogus"; then
1049     # Now that we know that we're not printing the help and that
1050     # the compiler works (so the results of the check_defines we used
1051     # to identify the OS are reliable), if we didn't recognize the
1052     # host OS we should stop now.
1053     error_exit "Unrecognized host OS (uname -s reports '$(uname -s)')"
1054 fi
1055
1056 # test for any invalid configuration combinations
1057 if test "$targetos" = "windows"; then
1058   if test "$plugins" = "yes"; then
1059     error_exit "TCG plugins not currently supported on Windows platforms"
1060   fi
1061   plugins="no"
1062 fi
1063 if test "$tcg" = "disabled" ; then
1064   if test "$plugins" = "yes"; then
1065     error_exit "Can't enable plugins on non-TCG builds"
1066   fi
1067   plugins="no"
1068 fi
1069 if test "$static" = "yes" ; then
1070   if test "$plugins" = "yes"; then
1071     error_exit "static and plugins are mutually incompatible"
1072   fi
1073   plugins="no"
1074 fi
1075 if test "$plugins" != "no"; then
1076   plugins=yes
1077   subdirs="$subdirs contrib/plugins"
1078 fi
1079
1080 cat > $TMPC << EOF
1081
1082 #ifdef __linux__
1083 #  define THREAD __thread
1084 #else
1085 #  define THREAD
1086 #endif
1087 static THREAD int tls_var;
1088 int main(void) { return tls_var; }
1089 EOF
1090
1091 if test "$static" = "yes"; then
1092   if test "$pie" != "no" && compile_prog "-Werror -fPIE -DPIE" "-static-pie"; then
1093     pie="yes"
1094   elif test "$pie" = "yes"; then
1095     error_exit "-static-pie not available due to missing toolchain support"
1096   else
1097     pie="no"
1098   fi
1099 elif test "$pie" != "no"; then
1100   if compile_prog "-Werror -fPIE -DPIE" "-pie"; then
1101     pie="yes"
1102   elif test "$pie" = "yes"; then
1103     error_exit "PIE not available due to missing toolchain support"
1104   else
1105     echo "Disabling PIE due to missing toolchain support"
1106     pie="no"
1107   fi
1108 fi
1109
1110 ##########################################
1111
1112 if test -z "${target_list+xxx}" ; then
1113     default_targets=yes
1114     for target in $default_target_list; do
1115         target_list="$target_list $target"
1116     done
1117     target_list="${target_list# }"
1118 else
1119     default_targets=no
1120     target_list=$(echo "$target_list" | sed -e 's/,/ /g')
1121     for target in $target_list; do
1122         # Check that we recognised the target name; this allows a more
1123         # friendly error message than if we let it fall through.
1124         case " $default_target_list " in
1125             *" $target "*)
1126                 ;;
1127             *)
1128                 error_exit "Unknown target name '$target'"
1129                 ;;
1130         esac
1131     done
1132 fi
1133
1134 if test "$tcg" = "auto"; then
1135   if test -z "$target_list"; then
1136     tcg="disabled"
1137   else
1138     tcg="enabled"
1139   fi
1140 fi
1141
1142 #########################################
1143 # gdb test
1144
1145 if test -n "$gdb_bin"; then
1146     gdb_version=$($gdb_bin --version | head -n 1)
1147     if version_ge ${gdb_version##* } 9.1; then
1148         gdb_arches=$($python "$source_path/scripts/probe-gdb-support.py" $gdb_bin)
1149     else
1150         gdb_bin=""
1151     fi
1152 fi
1153
1154 ##########################################
1155 # big/little endian test
1156 cat > $TMPC << EOF
1157 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1158 # error LITTLE
1159 #endif
1160 int main(void) { return 0; }
1161 EOF
1162
1163 if ! compile_prog ; then
1164   bigendian="no"
1165 else
1166   cat > $TMPC << EOF
1167 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1168 # error BIG
1169 #endif
1170 int main(void) { return 0; }
1171 EOF
1172
1173   if ! compile_prog ; then
1174     bigendian="yes"
1175   else
1176     echo big/little test failed
1177     exit 1
1178   fi
1179 fi
1180
1181 ##########################################
1182 # functions to probe cross compilers
1183
1184 container="no"
1185 runc=""
1186 if test $use_containers = "yes" && (has "docker" || has "podman"); then
1187     case $($python "$source_path"/tests/docker/docker.py --engine "$container_engine" probe) in
1188         *docker) container=docker ;;
1189         podman) container=podman ;;
1190         no) container=no ;;
1191     esac
1192     if test "$container" != "no"; then
1193         docker_py="$python $source_path/tests/docker/docker.py --engine $container"
1194         runc=$container
1195     fi
1196 fi
1197
1198 # cross compilers defaults, can be overridden with --cross-cc-ARCH
1199 : ${cross_prefix_aarch64="aarch64-linux-gnu-"}
1200 : ${cross_prefix_aarch64_be="$cross_prefix_aarch64"}
1201 : ${cross_prefix_alpha="alpha-linux-gnu-"}
1202 : ${cross_prefix_arm="arm-linux-gnueabihf-"}
1203 : ${cross_prefix_armeb="$cross_prefix_arm"}
1204 : ${cross_prefix_hexagon="hexagon-unknown-linux-musl-"}
1205 : ${cross_prefix_loongarch64="loongarch64-unknown-linux-gnu-"}
1206 : ${cross_prefix_hppa="hppa-linux-gnu-"}
1207 : ${cross_prefix_i386="i686-linux-gnu-"}
1208 : ${cross_prefix_m68k="m68k-linux-gnu-"}
1209 : ${cross_prefix_microblaze="microblaze-linux-musl-"}
1210 : ${cross_prefix_mips64el="mips64el-linux-gnuabi64-"}
1211 : ${cross_prefix_mips64="mips64-linux-gnuabi64-"}
1212 : ${cross_prefix_mipsel="mipsel-linux-gnu-"}
1213 : ${cross_prefix_mips="mips-linux-gnu-"}
1214 : ${cross_prefix_nios2="nios2-linux-gnu-"}
1215 : ${cross_prefix_ppc="powerpc-linux-gnu-"}
1216 : ${cross_prefix_ppc64="powerpc64-linux-gnu-"}
1217 : ${cross_prefix_ppc64le="$cross_prefix_ppc64"}
1218 : ${cross_prefix_riscv64="riscv64-linux-gnu-"}
1219 : ${cross_prefix_s390x="s390x-linux-gnu-"}
1220 : ${cross_prefix_sh4="sh4-linux-gnu-"}
1221 : ${cross_prefix_sparc64="sparc64-linux-gnu-"}
1222 : ${cross_prefix_sparc="$cross_prefix_sparc64"}
1223 : ${cross_prefix_tricore="tricore-"}
1224 : ${cross_prefix_x86_64="x86_64-linux-gnu-"}
1225
1226 : ${cross_cc_aarch64_be="$cross_cc_aarch64"}
1227 : ${cross_cc_cflags_aarch64_be="-mbig-endian"}
1228 : ${cross_cc_armeb="$cross_cc_arm"}
1229 : ${cross_cc_cflags_armeb="-mbig-endian"}
1230 : ${cross_cc_hexagon="hexagon-unknown-linux-musl-clang"}
1231 : ${cross_cc_cflags_hexagon="-mv73 -O2 -static"}
1232 : ${cross_cc_cflags_i386="-m32"}
1233 : ${cross_cc_cflags_ppc="-m32 -mbig-endian"}
1234 : ${cross_cc_cflags_ppc64="-m64 -mbig-endian"}
1235 : ${cross_cc_ppc64le="$cross_cc_ppc64"}
1236 : ${cross_cc_cflags_ppc64le="-m64 -mlittle-endian"}
1237 : ${cross_cc_cflags_sparc64="-m64 -mcpu=ultrasparc"}
1238 : ${cross_cc_sparc="$cross_cc_sparc64"}
1239 : ${cross_cc_cflags_sparc="-m32 -mcpu=supersparc"}
1240 : ${cross_cc_cflags_x86_64="-m64"}
1241
1242 compute_target_variable() {
1243   eval "$2="
1244   if eval test -n "\"\${cross_prefix_$1}\""; then
1245     if eval has "\"\${cross_prefix_$1}\$3\""; then
1246       eval "$2=\"\${cross_prefix_$1}\$3\""
1247     fi
1248   fi
1249 }
1250
1251 have_target() {
1252   for i; do
1253     case " $target_list " in
1254       *" $i "*) return 0;;
1255       *) ;;
1256     esac
1257   done
1258   return 1
1259 }
1260
1261 # probe_target_compiler TARGET
1262 #
1263 # Look for a compiler for the given target, either native or cross.
1264 # Set variables target_* if a compiler is found, and container_cross_*
1265 # if a Docker-based cross-compiler image is known for the target.
1266 # Set got_cross_cc to yes/no depending on whether a non-container-based
1267 # compiler was found.
1268 #
1269 # If TARGET is a user-mode emulation target, also set build_static to
1270 # "y" if static linking is possible.
1271 #
1272 probe_target_compiler() {
1273   # reset all output variables
1274   got_cross_cc=no
1275   container_image=
1276   container_hosts=
1277   container_cross_cc=
1278   container_cross_ar=
1279   container_cross_as=
1280   container_cross_ld=
1281   container_cross_nm=
1282   container_cross_objcopy=
1283   container_cross_ranlib=
1284   container_cross_strip=
1285
1286   target_arch=${1%%-*}
1287   case $target_arch in
1288     aarch64) container_hosts="x86_64 aarch64" ;;
1289     alpha) container_hosts=x86_64 ;;
1290     arm) container_hosts="x86_64 aarch64" ;;
1291     cris) container_hosts=x86_64 ;;
1292     hexagon) container_hosts=x86_64 ;;
1293     hppa) container_hosts=x86_64 ;;
1294     i386) container_hosts=x86_64 ;;
1295     loongarch64) container_hosts=x86_64 ;;
1296     m68k) container_hosts=x86_64 ;;
1297     microblaze) container_hosts=x86_64 ;;
1298     mips64el) container_hosts=x86_64 ;;
1299     mips64) container_hosts=x86_64 ;;
1300     mipsel) container_hosts=x86_64 ;;
1301     mips) container_hosts=x86_64 ;;
1302     nios2) container_hosts=x86_64 ;;
1303     ppc) container_hosts=x86_64 ;;
1304     ppc64|ppc64le) container_hosts=x86_64 ;;
1305     riscv64) container_hosts=x86_64 ;;
1306     s390x) container_hosts=x86_64 ;;
1307     sh4) container_hosts=x86_64 ;;
1308     sparc64) container_hosts=x86_64 ;;
1309     tricore) container_hosts=x86_64 ;;
1310     x86_64) container_hosts="aarch64 ppc64le x86_64" ;;
1311     xtensa*) container_hosts=x86_64 ;;
1312   esac
1313
1314   for host in $container_hosts; do
1315     test "$container" != no || continue
1316     test "$host" = "$cpu" || continue
1317     case $target_arch in
1318       aarch64)
1319         # We don't have any bigendian build tools so we only use this for AArch64
1320         container_image=debian-arm64-cross
1321         container_cross_prefix=aarch64-linux-gnu-
1322         container_cross_cc=${container_cross_prefix}gcc
1323         ;;
1324       alpha)
1325         container_image=debian-alpha-cross
1326         container_cross_prefix=alpha-linux-gnu-
1327         ;;
1328       arm)
1329         # We don't have any bigendian build tools so we only use this for ARM
1330         container_image=debian-armhf-cross
1331         container_cross_prefix=arm-linux-gnueabihf-
1332         ;;
1333       cris)
1334         container_image=fedora-cris-cross
1335         container_cross_prefix=cris-linux-gnu-
1336         ;;
1337       hexagon)
1338         container_image=debian-hexagon-cross
1339         container_cross_prefix=hexagon-unknown-linux-musl-
1340         container_cross_cc=${container_cross_prefix}clang
1341         ;;
1342       hppa)
1343         container_image=debian-hppa-cross
1344         container_cross_prefix=hppa-linux-gnu-
1345         ;;
1346       i386)
1347         container_image=fedora-i386-cross
1348         container_cross_prefix=
1349         ;;
1350       loongarch64)
1351         container_image=debian-loongarch-cross
1352         container_cross_prefix=loongarch64-unknown-linux-gnu-
1353         ;;
1354       m68k)
1355         container_image=debian-m68k-cross
1356         container_cross_prefix=m68k-linux-gnu-
1357         ;;
1358       microblaze)
1359         container_image=debian-microblaze-cross
1360         container_cross_prefix=microblaze-linux-musl-
1361         ;;
1362       mips64el)
1363         container_image=debian-mips64el-cross
1364         container_cross_prefix=mips64el-linux-gnuabi64-
1365         ;;
1366       mips64)
1367         container_image=debian-mips64-cross
1368         container_cross_prefix=mips64-linux-gnuabi64-
1369         ;;
1370       mipsel)
1371         container_image=debian-mipsel-cross
1372         container_cross_prefix=mipsel-linux-gnu-
1373         ;;
1374       mips)
1375         container_image=debian-mips-cross
1376         container_cross_prefix=mips-linux-gnu-
1377         ;;
1378       nios2)
1379         container_image=debian-nios2-cross
1380         container_cross_prefix=nios2-linux-gnu-
1381         ;;
1382       ppc)
1383         container_image=debian-powerpc-test-cross
1384         container_cross_prefix=powerpc-linux-gnu-
1385         container_cross_cc=${container_cross_prefix}gcc
1386         ;;
1387       ppc64|ppc64le)
1388         container_image=debian-powerpc-test-cross
1389         container_cross_prefix=powerpc${target_arch#ppc}-linux-gnu-
1390         container_cross_cc=${container_cross_prefix}gcc-10
1391         ;;
1392       riscv64)
1393         container_image=debian-riscv64-test-cross
1394         container_cross_prefix=riscv64-linux-gnu-
1395         ;;
1396       s390x)
1397         container_image=debian-s390x-cross
1398         container_cross_prefix=s390x-linux-gnu-
1399         ;;
1400       sh4)
1401         container_image=debian-sh4-cross
1402         container_cross_prefix=sh4-linux-gnu-
1403         ;;
1404       sparc64)
1405         container_image=debian-sparc64-cross
1406         container_cross_prefix=sparc64-linux-gnu-
1407         ;;
1408       tricore)
1409         container_image=debian-tricore-cross
1410         container_cross_prefix=tricore-
1411         ;;
1412       x86_64)
1413         container_image=debian-amd64-cross
1414         container_cross_prefix=x86_64-linux-gnu-
1415         ;;
1416       xtensa*)
1417         container_image=debian-xtensa-cross
1418
1419         # default to the dc232b cpu
1420         container_cross_prefix=/opt/2020.07/xtensa-dc232b-elf/bin/xtensa-dc232b-elf-
1421         ;;
1422     esac
1423     : ${container_cross_cc:=${container_cross_prefix}gcc}
1424     : ${container_cross_ar:=${container_cross_prefix}ar}
1425     : ${container_cross_as:=${container_cross_prefix}as}
1426     : ${container_cross_ld:=${container_cross_prefix}ld}
1427     : ${container_cross_nm:=${container_cross_prefix}nm}
1428     : ${container_cross_objcopy:=${container_cross_prefix}objcopy}
1429     : ${container_cross_ranlib:=${container_cross_prefix}ranlib}
1430     : ${container_cross_strip:=${container_cross_prefix}strip}
1431   done
1432
1433   try=cross
1434   case "$target_arch:$cpu" in
1435     aarch64_be:aarch64 | \
1436     armeb:arm | \
1437     i386:x86_64 | \
1438     mips*:mips64 | \
1439     ppc*:ppc64 | \
1440     sparc:sparc64 | \
1441     "$cpu:$cpu")
1442       try='native cross' ;;
1443   esac
1444   eval "target_cflags=\${cross_cc_cflags_$target_arch}"
1445   for thistry in $try; do
1446     case $thistry in
1447     native)
1448       target_cc=$cc
1449       target_ccas=$ccas
1450       target_ar=$ar
1451       target_as=$as
1452       target_ld=$ld
1453       target_nm=$nm
1454       target_objcopy=$objcopy
1455       target_ranlib=$ranlib
1456       target_strip=$strip
1457       ;;
1458     cross)
1459       target_cc=
1460       if eval test -n "\"\${cross_cc_$target_arch}\""; then
1461         if eval has "\"\${cross_cc_$target_arch}\""; then
1462           eval "target_cc=\"\${cross_cc_$target_arch}\""
1463         fi
1464       else
1465         compute_target_variable $target_arch target_cc gcc
1466       fi
1467       target_ccas=$target_cc
1468       compute_target_variable $target_arch target_ar ar
1469       compute_target_variable $target_arch target_as as
1470       compute_target_variable $target_arch target_ld ld
1471       compute_target_variable $target_arch target_nm nm
1472       compute_target_variable $target_arch target_objcopy objcopy
1473       compute_target_variable $target_arch target_ranlib ranlib
1474       compute_target_variable $target_arch target_strip strip
1475       ;;
1476     esac
1477
1478     if test -n "$target_cc"; then
1479       case $target_arch in
1480         i386|x86_64)
1481           if $target_cc --version | grep -qi "clang"; then
1482             continue
1483           fi
1484           ;;
1485       esac
1486     elif test -n "$target_as" && test -n "$target_ld"; then
1487       # Special handling for assembler only targets
1488       case $target in
1489         tricore-softmmu)
1490           build_static=
1491           got_cross_cc=yes
1492           break
1493           ;;
1494         *)
1495           continue
1496           ;;
1497       esac
1498     else
1499       continue
1500     fi
1501
1502     write_c_skeleton
1503     case $1 in
1504       *-softmmu)
1505         if do_compiler "$target_cc" $target_cflags -o $TMPO -c $TMPC &&
1506           do_compiler "$target_cc" $target_cflags -r -nostdlib -o "${TMPDIR1}/${TMPB}2.o" "$TMPO" -lgcc; then
1507           got_cross_cc=yes
1508           break
1509         fi
1510         ;;
1511       *)
1512         if do_compiler "$target_cc" $target_cflags -o $TMPE $TMPC -static ; then
1513           build_static=y
1514           got_cross_cc=yes
1515           break
1516         fi
1517         if do_compiler "$target_cc" $target_cflags -o $TMPE $TMPC ; then
1518           build_static=
1519           got_cross_cc=yes
1520           break
1521         fi
1522         ;;
1523     esac
1524   done
1525   if test $got_cross_cc != yes; then
1526     build_static=
1527     target_cc=
1528     target_ccas=
1529     target_ar=
1530     target_as=
1531     target_ld=
1532     target_nm=
1533     target_objcopy=
1534     target_ranlib=
1535     target_strip=
1536   fi
1537   test -n "$target_cc"
1538 }
1539
1540 write_target_makefile() {
1541   echo "EXTRA_CFLAGS=$target_cflags"
1542   if test -z "$target_cc" && test -z "$target_as"; then
1543     test -z "$container_image" && error_exit "Internal error: could not find cross compiler for $1?"
1544     echo "$1: docker-image-$container_image" >> Makefile.prereqs
1545     if test -n "$container_cross_cc"; then
1546       echo "CC=$docker_py cc --cc $container_cross_cc -i qemu/$container_image -s $source_path --"
1547       echo "CCAS=$docker_py cc --cc $container_cross_cc -i qemu/$container_image -s $source_path --"
1548     fi
1549     echo "AR=$docker_py cc --cc $container_cross_ar -i qemu/$container_image -s $source_path --"
1550     echo "AS=$docker_py cc --cc $container_cross_as -i qemu/$container_image -s $source_path --"
1551     echo "LD=$docker_py cc --cc $container_cross_ld -i qemu/$container_image -s $source_path --"
1552     echo "NM=$docker_py cc --cc $container_cross_nm -i qemu/$container_image -s $source_path --"
1553     echo "OBJCOPY=$docker_py cc --cc $container_cross_objcopy -i qemu/$container_image -s $source_path --"
1554     echo "RANLIB=$docker_py cc --cc $container_cross_ranlib -i qemu/$container_image -s $source_path --"
1555     echo "STRIP=$docker_py cc --cc $container_cross_strip -i qemu/$container_image -s $source_path --"
1556   else
1557     if test -n "$target_cc"; then
1558       echo "CC=$target_cc"
1559       echo "CCAS=$target_ccas"
1560     fi
1561     if test -n "$target_ar"; then
1562       echo "AR=$target_ar"
1563     fi
1564     if test -n "$target_as"; then
1565       echo "AS=$target_as"
1566     fi
1567     if test -n "$target_ld"; then
1568       echo "LD=$target_ld"
1569     fi
1570     if test -n "$target_nm"; then
1571       echo "NM=$target_nm"
1572     fi
1573     if test -n "$target_objcopy"; then
1574       echo "OBJCOPY=$target_objcopy"
1575     fi
1576     if test -n "$target_ranlib"; then
1577       echo "RANLIB=$target_ranlib"
1578     fi
1579     if test -n "$target_strip"; then
1580       echo "STRIP=$target_strip"
1581     fi
1582   fi
1583 }
1584
1585 #######################################
1586 # cross-compiled firmware targets
1587
1588 # Set up build tree symlinks that point back into the source tree
1589 # (these can be both files and directories).
1590 # Caution: avoid adding files or directories here using wildcards. This
1591 # will result in problems later if a new file matching the wildcard is
1592 # added to the source tree -- nothing will cause configure to be rerun
1593 # so the build tree will be missing the link back to the new file, and
1594 # tests might fail. Prefer to keep the relevant files in their own
1595 # directory and symlink the directory instead.
1596 LINKS="Makefile"
1597 LINKS="$LINKS docs/config"
1598 LINKS="$LINKS pc-bios/optionrom/Makefile"
1599 LINKS="$LINKS pc-bios/s390-ccw/Makefile"
1600 LINKS="$LINKS pc-bios/vof/Makefile"
1601 LINKS="$LINKS .gdbinit scripts" # scripts needed by relative path in .gdbinit
1602 LINKS="$LINKS tests/avocado tests/data"
1603 LINKS="$LINKS tests/qemu-iotests/check"
1604 LINKS="$LINKS python"
1605 LINKS="$LINKS contrib/plugins/Makefile "
1606 for f in $LINKS ; do
1607     if [ -e "$source_path/$f" ]; then
1608         symlink "$source_path/$f" "$f"
1609     fi
1610 done
1611
1612 echo "# Automatically generated by configure - do not modify" > Makefile.prereqs
1613
1614 # Mac OS X ships with a broken assembler
1615 if have_target i386-softmmu x86_64-softmmu && \
1616         test "$targetos" != "darwin" && test "$targetos" != "sunos" && \
1617         test "$targetos" != "haiku" && \
1618         probe_target_compiler i386-softmmu; then
1619     subdirs="$subdirs pc-bios/optionrom"
1620     config_mak=pc-bios/optionrom/config.mak
1621     echo "# Automatically generated by configure - do not modify" > $config_mak
1622     echo "TOPSRC_DIR=$source_path" >> $config_mak
1623     write_target_makefile >> $config_mak
1624 fi
1625
1626 if have_target ppc-softmmu ppc64-softmmu && \
1627         probe_target_compiler ppc-softmmu; then
1628     subdirs="$subdirs pc-bios/vof"
1629     config_mak=pc-bios/vof/config.mak
1630     echo "# Automatically generated by configure - do not modify" > $config_mak
1631     echo "SRC_DIR=$source_path/pc-bios/vof" >> $config_mak
1632     write_target_makefile >> $config_mak
1633 fi
1634
1635 # Only build s390-ccw bios if the compiler has -march=z900 or -march=z10
1636 # (which is the lowest architecture level that Clang supports)
1637 if have_target s390x-softmmu && probe_target_compiler s390x-softmmu && \
1638     GIT=git "$source_path/scripts/git-submodule.sh" "$git_submodules_action" roms/SLOF >> config.log 2>&1; then
1639   write_c_skeleton
1640   do_compiler "$target_cc" $target_cc_cflags -march=z900 -o $TMPO -c $TMPC
1641   has_z900=$?
1642   if [ $has_z900 = 0 ] || do_compiler "$target_cc" $target_cc_cflags -march=z10 -msoft-float -Werror -o $TMPO -c $TMPC; then
1643     if [ $has_z900 != 0 ]; then
1644       echo "WARNING: Your compiler does not support the z900!"
1645       echo "         The s390-ccw bios will only work with guest CPUs >= z10."
1646     fi
1647     subdirs="$subdirs pc-bios/s390-ccw"
1648     config_mak=pc-bios/s390-ccw/config-host.mak
1649     echo "# Automatically generated by configure - do not modify" > $config_mak
1650     echo "SRC_PATH=$source_path/pc-bios/s390-ccw" >> $config_mak
1651     echo "GIT_SUBMODULES_ACTION=$git_submodules_action" >> $config_mak
1652     write_target_makefile >> $config_mak
1653   fi
1654 fi
1655
1656 #######################################
1657 # generate config-host.mak
1658
1659 config_host_mak="config-host.mak"
1660
1661 echo "# Automatically generated by configure - do not modify" > $config_host_mak
1662 echo >> $config_host_mak
1663
1664 echo all: >> $config_host_mak
1665
1666 if test "$targetos" = "windows"; then
1667   echo "QEMU_GA_MANUFACTURER=${QEMU_GA_MANUFACTURER-QEMU}" >> $config_host_mak
1668   echo "QEMU_GA_DISTRO=${QEMU_GA_DISTRO-Linux}" >> $config_host_mak
1669   echo "QEMU_GA_VERSION=${QEMU_GA_VERSION-$(cat "$source_path"/VERSION)}" >> $config_host_mak
1670 fi
1671
1672 echo "SRC_PATH=$source_path" >> $config_host_mak
1673 echo "TARGET_DIRS=$target_list" >> $config_host_mak
1674 echo "GDB=$gdb_bin" >> $config_host_mak
1675 if test "$container" != no; then
1676     echo "RUNC=$runc" >> $config_host_mak
1677 fi
1678 echo "SUBDIRS=$subdirs" >> $config_host_mak
1679 echo "PYTHON=$python" >> $config_host_mak
1680 echo "GENISOIMAGE=$genisoimage" >> $config_host_mak
1681 echo "MESON=$meson" >> $config_host_mak
1682 echo "NINJA=$ninja" >> $config_host_mak
1683 echo "EXESUF=$EXESUF" >> $config_host_mak
1684
1685 # use included Linux headers for KVM architectures
1686 if test "$targetos" = "linux" && test -n "$linux_arch"; then
1687   symlink "$source_path/linux-headers/asm-$linux_arch" linux-headers/asm
1688 fi
1689
1690 for target in $target_list; do
1691     target_dir="$target"
1692     target_name=$(echo $target | cut -d '-' -f 1)$EXESUF
1693     case $target in
1694         *-user) symlink "../qemu-$target_name" "$target_dir/qemu-$target_name" ;;
1695         *) symlink "../qemu-system-$target_name" "$target_dir/qemu-system-$target_name" ;;
1696     esac
1697 done
1698
1699 if test "$default_targets" = "yes"; then
1700   echo "CONFIG_DEFAULT_TARGETS=y" >> $config_host_mak
1701 fi
1702
1703 # contrib/plugins configuration
1704 echo "# Automatically generated by configure - do not modify" > contrib/plugins/$config_host_mak
1705 echo "SRC_PATH=$source_path/contrib/plugins" >> contrib/plugins/$config_host_mak
1706 echo "PKG_CONFIG=${pkg_config}" >> contrib/plugins/$config_host_mak
1707 echo "CC=$cc $CPU_CFLAGS" >> contrib/plugins/$config_host_mak
1708 echo "CFLAGS=${CFLAGS-$default_cflags} $EXTRA_CFLAGS" >> contrib/plugins/$config_host_mak
1709 if test "$targetos" = darwin; then
1710   echo "CONFIG_DARWIN=y" >> contrib/plugins/$config_host_mak
1711 fi
1712
1713 # tests/tcg configuration
1714 (config_host_mak=tests/tcg/config-host.mak
1715 mkdir -p tests/tcg
1716 echo "# Automatically generated by configure - do not modify" > $config_host_mak
1717 echo "SRC_PATH=$source_path" >> $config_host_mak
1718
1719 tcg_tests_targets=
1720 for target in $target_list; do
1721   arch=${target%%-*}
1722
1723   case $target in
1724     xtensa*-linux-user)
1725       # the toolchain is not complete with headers, only build system tests
1726       continue
1727       ;;
1728     *-softmmu)
1729       test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || continue
1730       qemu="qemu-system-$arch"
1731       ;;
1732     *-linux-user|*-bsd-user)
1733       qemu="qemu-$arch"
1734       ;;
1735   esac
1736
1737   if probe_target_compiler $target || test -n "$container_image"; then
1738       test -n "$container_image" && build_static=y
1739       mkdir -p "tests/tcg/$target"
1740       config_target_mak=tests/tcg/$target/config-target.mak
1741       ln -sf "$source_path/tests/tcg/Makefile.target" "tests/tcg/$target/Makefile"
1742       echo "# Automatically generated by configure - do not modify" > "$config_target_mak"
1743       echo "TARGET_NAME=$arch" >> "$config_target_mak"
1744       echo "TARGET=$target" >> "$config_target_mak"
1745       write_target_makefile "build-tcg-tests-$target" >> "$config_target_mak"
1746       echo "BUILD_STATIC=$build_static" >> "$config_target_mak"
1747       echo "QEMU=$PWD/$qemu" >> "$config_target_mak"
1748
1749       # will GDB work with these binaries?
1750       if test "${gdb_arches#*$arch}" != "$gdb_arches"; then
1751           echo "GDB=$gdb_bin" >> $config_target_mak
1752       fi
1753
1754       echo "run-tcg-tests-$target: $qemu\$(EXESUF)" >> Makefile.prereqs
1755       tcg_tests_targets="$tcg_tests_targets $target"
1756   fi
1757 done
1758
1759 if test "$tcg" = "enabled"; then
1760     echo "TCG_TESTS_TARGETS=$tcg_tests_targets" >> config-host.mak
1761 fi
1762 )
1763
1764 if test "$skip_meson" = no; then
1765   cross="config-meson.cross.new"
1766   meson_quote() {
1767     test $# = 0 && return
1768     echo "'$(echo $* | sed "s/ /','/g")'"
1769   }
1770
1771   echo "# Automatically generated by configure - do not modify" > $cross
1772   echo "[properties]" >> $cross
1773
1774   # unroll any custom device configs
1775   for a in $device_archs; do
1776       eval "c=\$devices_${a}"
1777       echo "${a}-softmmu = '$c'" >> $cross
1778   done
1779
1780   echo "[built-in options]" >> $cross
1781   echo "c_args = [$(meson_quote $CFLAGS $EXTRA_CFLAGS)]" >> $cross
1782   echo "cpp_args = [$(meson_quote $CXXFLAGS $EXTRA_CXXFLAGS)]" >> $cross
1783   test -n "$objcc" && echo "objc_args = [$(meson_quote $OBJCFLAGS $EXTRA_OBJCFLAGS)]" >> $cross
1784   echo "c_link_args = [$(meson_quote $CFLAGS $LDFLAGS $EXTRA_CFLAGS $EXTRA_LDFLAGS)]" >> $cross
1785   echo "cpp_link_args = [$(meson_quote $CXXFLAGS $LDFLAGS $EXTRA_CXXFLAGS $EXTRA_LDFLAGS)]" >> $cross
1786   echo "[binaries]" >> $cross
1787   echo "c = [$(meson_quote $cc $CPU_CFLAGS)]" >> $cross
1788   test -n "$cxx" && echo "cpp = [$(meson_quote $cxx $CPU_CFLAGS)]" >> $cross
1789   test -n "$objcc" && echo "objc = [$(meson_quote $objcc $CPU_CFLAGS)]" >> $cross
1790   echo "ar = [$(meson_quote $ar)]" >> $cross
1791   echo "nm = [$(meson_quote $nm)]" >> $cross
1792   echo "pkgconfig = [$(meson_quote $pkg_config)]" >> $cross
1793   echo "ranlib = [$(meson_quote $ranlib)]" >> $cross
1794   if has $sdl2_config; then
1795     echo "sdl2-config = [$(meson_quote $sdl2_config)]" >> $cross
1796   fi
1797   echo "strip = [$(meson_quote $strip)]" >> $cross
1798   echo "widl = [$(meson_quote $widl)]" >> $cross
1799   echo "windres = [$(meson_quote $windres)]" >> $cross
1800   echo "windmc = [$(meson_quote $windmc)]" >> $cross
1801   if test "$cross_compile" = "yes"; then
1802     echo "[host_machine]" >> $cross
1803     echo "system = '$targetos'" >> $cross
1804     case "$cpu" in
1805         i386)
1806             echo "cpu_family = 'x86'" >> $cross
1807             ;;
1808         *)
1809             echo "cpu_family = '$cpu'" >> $cross
1810             ;;
1811     esac
1812     echo "cpu = '$cpu'" >> $cross
1813     if test "$bigendian" = "yes" ; then
1814         echo "endian = 'big'" >> $cross
1815     else
1816         echo "endian = 'little'" >> $cross
1817     fi
1818     cross_arg="--cross-file config-meson.cross"
1819
1820     native="config-meson.native.new"
1821     echo "# Automatically generated by configure - do not modify" > $native
1822     echo "[binaries]" >> $native
1823     echo "c = [$(meson_quote $host_cc)]" >> $native
1824     mv $native config-meson.native
1825     cross_arg="$cross_arg --native-file config-meson.native"
1826   else
1827     cross_arg="--native-file config-meson.cross"
1828   fi
1829   mv $cross config-meson.cross
1830
1831   rm -rf meson-private meson-info meson-logs
1832
1833   # Built-in options
1834   test "$download" = "disabled" && meson_option_add "--wrap-mode=nodownload"
1835   test "$bindir" != "bin" && meson_option_add "-Dbindir=$bindir"
1836   test "$default_feature" = no && meson_option_add -Dauto_features=disabled
1837   test "$static" = yes && meson_option_add -Dprefer_static=true
1838   test "$pie" = no && meson_option_add -Db_pie=false
1839   test "$werror" = yes && meson_option_add -Dwerror=true
1840
1841   # QEMU options
1842   test "$cfi" != false && meson_option_add "-Dcfi=$cfi" "-Db_lto=$cfi"
1843   test "$docs" != auto && meson_option_add "-Ddocs=$docs"
1844   test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE"
1845   test "$plugins" = yes && meson_option_add "-Dplugins=true"
1846   test "$qemu_suffix" != qemu && meson_option_add "-Dqemu_suffix=$qemu_suffix"
1847   test "$smbd" != '' && meson_option_add "-Dsmbd=$smbd"
1848   test "$tcg" != enabled && meson_option_add "-Dtcg=$tcg"
1849   run_meson() {
1850     NINJA=$ninja $meson setup --prefix "$prefix" "$@" $cross_arg "$PWD" "$source_path"
1851   }
1852   eval run_meson $meson_options
1853   if test "$?" -ne 0 ; then
1854       error_exit "meson setup failed"
1855   fi
1856   echo "$meson" > build.ninja.stamp
1857 else
1858   if test -f meson-private/cmd_line.txt; then
1859     # Adjust old command line options that were removed
1860     # sed -i is not portable
1861     perl -i -ne '
1862       /^sphinx_build/ && next;
1863       print;' meson-private/cmd_line.txt
1864   fi
1865 fi
1866
1867 # Save the configure command line for later reuse.
1868 cat <<EOD >config.status
1869 #!/bin/sh
1870 # Generated by configure.
1871 # Run this file to recreate the current configuration.
1872 # Compiler output produced by configure, useful for debugging
1873 # configure, is in config.log if it exists.
1874 EOD
1875
1876 preserve_env() {
1877     envname=$1
1878
1879     eval envval=\$$envname
1880
1881     if test -n "$envval"
1882     then
1883         echo "$envname='$envval'" >> config.status
1884         echo "export $envname" >> config.status
1885     else
1886         echo "unset $envname" >> config.status
1887     fi
1888 }
1889
1890 # Preserve various env variables that influence what
1891 # features/build target configure will detect
1892 preserve_env AR
1893 preserve_env AS
1894 preserve_env CC
1895 preserve_env CFLAGS
1896 preserve_env CXX
1897 preserve_env CXXFLAGS
1898 preserve_env LD
1899 preserve_env LDFLAGS
1900 preserve_env LD_LIBRARY_PATH
1901 preserve_env NM
1902 preserve_env OBJCFLAGS
1903 preserve_env OBJCOPY
1904 preserve_env PATH
1905 preserve_env PKG_CONFIG
1906 preserve_env PKG_CONFIG_LIBDIR
1907 preserve_env PKG_CONFIG_PATH
1908 preserve_env PYTHON
1909 preserve_env QEMU_GA_MANUFACTURER
1910 preserve_env QEMU_GA_DISTRO
1911 preserve_env QEMU_GA_VERSION
1912 preserve_env SDL2_CONFIG
1913 preserve_env SMBD
1914 preserve_env STRIP
1915 preserve_env WIDL
1916 preserve_env WINDRES
1917 preserve_env WINDMC
1918
1919 printf "exec" >>config.status
1920 for i in "$0" "$@"; do
1921   test "$i" = --skip-meson || printf " %s" "$(quote_sh "$i")" >>config.status
1922 done
1923 echo ' "$@"' >>config.status
1924 chmod +x config.status
1925
1926 rm -r "$TMPDIR1"