*/*: Clean PYTHON_COMPAT of obsolete impls
[gentoo.git] / app-emulation / qemu / qemu-4.1.0-r1.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI="7"
5
6 PYTHON_COMPAT=( python{2_7,3_6,3_7} )
7 PYTHON_REQ_USE="ncurses,readline"
8
9 PLOCALES="bg de_DE fr_FR hu it tr zh_CN"
10
11 FIRMWARE_ABI_VERSION="4.0.0-r50"
12
13 inherit eutils linux-info toolchain-funcs multilib python-r1 \
14         udev fcaps readme.gentoo-r1 pax-utils l10n xdg-utils
15
16 if [[ ${PV} = *9999* ]]; then
17         EGIT_REPO_URI="git://git.qemu.org/qemu.git"
18         EGIT_SUBMODULES=(
19                 slirp
20                 tests/fp/berkeley-{test,soft}float-3
21                 ui/keycodemapdb
22         )
23         inherit git-r3
24         SRC_URI=""
25 else
26         SRC_URI="http://wiki.qemu-project.org/download/${P}.tar.xz"
27         KEYWORDS="~amd64 ~arm64 ~ppc ~ppc64 ~x86"
28 fi
29
30 DESCRIPTION="QEMU + Kernel-based Virtual Machine userland tools"
31 HOMEPAGE="http://www.qemu.org http://www.linux-kvm.org"
32
33 LICENSE="GPL-2 LGPL-2 BSD-2"
34 SLOT="0"
35
36 IUSE="accessibility +aio alsa bzip2 capstone +caps +curl debug doc
37         +fdt glusterfs gnutls gtk infiniband iscsi jemalloc +jpeg kernel_linux
38         kernel_FreeBSD lzo ncurses nfs nls numa opengl +oss +pin-upstream-blobs
39         +png pulseaudio python rbd sasl +seccomp sdl selinux smartcard snappy
40         spice ssh static static-user systemtap tci test usb usbredir vde
41         +vhost-net virgl virtfs +vnc vte xattr xen xfs +xkb"
42
43 COMMON_TARGETS="aarch64 alpha arm cris hppa i386 m68k microblaze microblazeel
44         mips mips64 mips64el mipsel nios2 or1k ppc ppc64 riscv32 riscv64 s390x
45         sh4 sh4eb sparc sparc64 x86_64 xtensa xtensaeb"
46 IUSE_SOFTMMU_TARGETS="${COMMON_TARGETS}
47         lm32 moxie tricore unicore32"
48 IUSE_USER_TARGETS="${COMMON_TARGETS}
49         aarch64_be armeb mipsn32 mipsn32el ppc64abi32 ppc64le sparc32plus
50         tilegx"
51
52 use_softmmu_targets=$(printf ' qemu_softmmu_targets_%s' ${IUSE_SOFTMMU_TARGETS})
53 use_user_targets=$(printf ' qemu_user_targets_%s' ${IUSE_USER_TARGETS})
54 IUSE+=" ${use_softmmu_targets} ${use_user_targets}"
55
56 RESTRICT="!test? ( test )"
57 # Allow no targets to be built so that people can get a tools-only build.
58 # Block USE flag configurations known to not work.
59 REQUIRED_USE="${PYTHON_REQUIRED_USE}
60         qemu_softmmu_targets_arm? ( fdt )
61         qemu_softmmu_targets_microblaze? ( fdt )
62         qemu_softmmu_targets_mips64el? ( fdt )
63         qemu_softmmu_targets_ppc64? ( fdt )
64         qemu_softmmu_targets_ppc? ( fdt )
65         qemu_softmmu_targets_riscv32? ( fdt )
66         qemu_softmmu_targets_riscv64? ( fdt )
67         static? ( static-user !alsa !gtk !opengl !pulseaudio !snappy )
68         virtfs? ( xattr )
69         vte? ( gtk )"
70
71 # Dependencies required for qemu tools (qemu-nbd, qemu-img, qemu-io, ...)
72 # and user/softmmu targets (qemu-*, qemu-system-*).
73 #
74 # Yep, you need both libcap and libcap-ng since virtfs only uses libcap.
75 #
76 # The attr lib isn't always linked in (although the USE flag is always
77 # respected).  This is because qemu supports using the C library's API
78 # when available rather than always using the external library.
79 ALL_DEPEND="
80         >=dev-libs/glib-2.0[static-libs(+)]
81         sys-libs/zlib[static-libs(+)]
82         python? ( ${PYTHON_DEPS} )
83         systemtap? ( dev-util/systemtap )
84         xattr? ( sys-apps/attr[static-libs(+)] )"
85
86 # Dependencies required for qemu tools (qemu-nbd, qemu-img, qemu-io, ...)
87 # softmmu targets (qemu-system-*).
88 SOFTMMU_TOOLS_DEPEND="
89         dev-libs/libxml2[static-libs(+)]
90         xkb? ( x11-libs/libxkbcommon[static-libs(+)] )
91         >=x11-libs/pixman-0.28.0[static-libs(+)]
92         accessibility? (
93                 app-accessibility/brltty[api]
94                 app-accessibility/brltty[static-libs(+)]
95         )
96         aio? ( dev-libs/libaio[static-libs(+)] )
97         alsa? ( >=media-libs/alsa-lib-1.0.13 )
98         bzip2? ( app-arch/bzip2[static-libs(+)] )
99         capstone? ( dev-libs/capstone:= )
100         caps? ( sys-libs/libcap-ng[static-libs(+)] )
101         curl? ( >=net-misc/curl-7.15.4[static-libs(+)] )
102         fdt? ( >=sys-apps/dtc-1.5.0[static-libs(+)] )
103         glusterfs? ( >=sys-cluster/glusterfs-3.4.0[static-libs(+)] )
104         gnutls? (
105                 dev-libs/nettle:=[static-libs(+)]
106                 >=net-libs/gnutls-3.0:=[static-libs(+)]
107         )
108         gtk? (
109                 x11-libs/gtk+:3
110                 vte? ( x11-libs/vte:2.91 )
111         )
112         infiniband? (
113                 sys-fabric/libibumad:=[static-libs(+)]
114                 sys-fabric/libibverbs:=[static-libs(+)]
115                 sys-fabric/librdmacm:=[static-libs(+)]
116         )
117         iscsi? ( net-libs/libiscsi )
118         jemalloc? ( dev-libs/jemalloc )
119         jpeg? ( virtual/jpeg:0=[static-libs(+)] )
120         lzo? ( dev-libs/lzo:2[static-libs(+)] )
121         ncurses? (
122                 sys-libs/ncurses:0=[unicode]
123                 sys-libs/ncurses:0=[static-libs(+)]
124         )
125         nfs? ( >=net-fs/libnfs-1.9.3:=[static-libs(+)] )
126         numa? ( sys-process/numactl[static-libs(+)] )
127         opengl? (
128                 virtual/opengl
129                 media-libs/libepoxy[static-libs(+)]
130                 media-libs/mesa[static-libs(+)]
131                 media-libs/mesa[egl,gbm]
132         )
133         png? ( media-libs/libpng:0=[static-libs(+)] )
134         pulseaudio? ( media-sound/pulseaudio )
135         rbd? ( sys-cluster/ceph[static-libs(+)] )
136         sasl? ( dev-libs/cyrus-sasl[static-libs(+)] )
137         sdl? (
138                 media-libs/libsdl2[X]
139                 media-libs/libsdl2[static-libs(+)]
140         )
141         seccomp? ( >=sys-libs/libseccomp-2.1.0[static-libs(+)] )
142         smartcard? ( >=app-emulation/libcacard-2.5.0[static-libs(+)] )
143         snappy? ( app-arch/snappy:= )
144         spice? (
145                 >=app-emulation/spice-protocol-0.12.3
146                 >=app-emulation/spice-0.12.0[static-libs(+)]
147         )
148         ssh? ( >=net-libs/libssh-0.8.6[static-libs(+)] )
149         usb? ( >=virtual/libusb-1-r2[static-libs(+)] )
150         usbredir? ( >=sys-apps/usbredir-0.6[static-libs(+)] )
151         vde? ( net-misc/vde[static-libs(+)] )
152         virgl? ( media-libs/virglrenderer[static-libs(+)] )
153         virtfs? ( sys-libs/libcap )
154         xen? ( app-emulation/xen-tools:= )
155         xfs? ( sys-fs/xfsprogs[static-libs(+)] )"
156
157 X86_FIRMWARE_DEPEND="
158         pin-upstream-blobs? (
159                 ~sys-firmware/edk2-ovmf-201905[binary]
160                 ~sys-firmware/ipxe-1.0.0_p20190728[binary]
161                 ~sys-firmware/seabios-1.12.0[binary,seavgabios]
162                 ~sys-firmware/sgabios-0.1_pre8[binary]
163         )
164         !pin-upstream-blobs? (
165                 sys-firmware/edk2-ovmf
166                 sys-firmware/ipxe
167                 >=sys-firmware/seabios-1.10.2[seavgabios]
168                 sys-firmware/sgabios
169         )"
170 PPC64_FIRMWARE_DEPEND="
171         pin-upstream-blobs? (
172                 ~sys-firmware/seabios-1.12.0[binary,seavgabios]
173         )
174         !pin-upstream-blobs? (
175                 >=sys-firmware/seabios-1.10.2[seavgabios]
176         )
177 "
178
179 BDEPEND="
180         $(python_gen_impl_dep)
181         dev-lang/perl
182         sys-apps/texinfo
183         virtual/pkgconfig
184         doc? ( dev-python/sphinx )
185         gtk? ( nls? ( sys-devel/gettext ) )
186         test? (
187                 dev-libs/glib[utils]
188                 sys-devel/bc
189         )
190 "
191 CDEPEND="
192         !static? (
193                 ${ALL_DEPEND//\[static-libs(+)]}
194                 ${SOFTMMU_TOOLS_DEPEND//\[static-libs(+)]}
195         )
196         qemu_softmmu_targets_i386? ( ${X86_FIRMWARE_DEPEND} )
197         qemu_softmmu_targets_x86_64? ( ${X86_FIRMWARE_DEPEND} )
198         qemu_softmmu_targets_ppc64? ( ${PPC64_FIRMWARE_DEPEND} )
199 "
200 DEPEND="${CDEPEND}
201         kernel_linux? ( >=sys-kernel/linux-headers-2.6.35 )
202         static? (
203                 ${ALL_DEPEND}
204                 ${SOFTMMU_TOOLS_DEPEND}
205         )
206         static-user? ( ${ALL_DEPEND} )"
207 RDEPEND="${CDEPEND}
208         acct-group/kvm
209         selinux? ( sec-policy/selinux-qemu )"
210
211 PATCHES=(
212         "${FILESDIR}"/${PN}-2.5.0-cflags.patch
213         "${FILESDIR}"/${PN}-2.5.0-sysmacros.patch
214         "${FILESDIR}"/${PN}-2.11.1-capstone_include_path.patch
215         "${FILESDIR}"/${PN}-4.0.0-sanitize-interp_info.patch
216         "${FILESDIR}"/${PN}-4.0.0-mkdir_systemtap.patch #684902
217         "${FILESDIR}"/${PN}-4.0.0-xkbcommon.patch
218 )
219
220 QA_PREBUILT="
221         usr/share/qemu/hppa-firmware.img
222         usr/share/qemu/openbios-ppc
223         usr/share/qemu/openbios-sparc64
224         usr/share/qemu/openbios-sparc32
225         usr/share/qemu/palcode-clipper
226         usr/share/qemu/s390-ccw.img
227         usr/share/qemu/s390-netboot.img
228         usr/share/qemu/u-boot.e500"
229
230 QA_WX_LOAD="usr/bin/qemu-i386
231         usr/bin/qemu-x86_64
232         usr/bin/qemu-alpha
233         usr/bin/qemu-arm
234         usr/bin/qemu-cris
235         usr/bin/qemu-m68k
236         usr/bin/qemu-microblaze
237         usr/bin/qemu-microblazeel
238         usr/bin/qemu-mips
239         usr/bin/qemu-mipsel
240         usr/bin/qemu-or1k
241         usr/bin/qemu-ppc
242         usr/bin/qemu-ppc64
243         usr/bin/qemu-ppc64abi32
244         usr/bin/qemu-sh4
245         usr/bin/qemu-sh4eb
246         usr/bin/qemu-sparc
247         usr/bin/qemu-sparc64
248         usr/bin/qemu-armeb
249         usr/bin/qemu-sparc32plus
250         usr/bin/qemu-s390x
251         usr/bin/qemu-unicore32"
252
253 DOC_CONTENTS="If you don't have kvm compiled into the kernel, make sure you have the
254 kernel module loaded before running kvm. The easiest way to ensure that the
255 kernel module is loaded is to load it on boot.
256         For AMD CPUs the module is called 'kvm-amd'.
257         For Intel CPUs the module is called 'kvm-intel'.
258 Please review /etc/conf.d/modules for how to load these.
259
260 Make sure your user is in the 'kvm' group. Just run
261         $ gpasswd -a <USER> kvm
262 then have <USER> re-login.
263
264 For brand new installs, the default permissions on /dev/kvm might not let
265 you access it.  You can tell udev to reset ownership/perms:
266         $ udevadm trigger -c add /dev/kvm
267
268 If you want to register binfmt handlers for qemu user targets:
269 For openrc:
270         # rc-update add qemu-binfmt
271 For systemd:
272         # ln -s /usr/share/qemu/binfmt.d/qemu.conf /etc/binfmt.d/qemu.conf"
273
274 pkg_pretend() {
275         if use kernel_linux && kernel_is lt 2 6 25; then
276                 eerror "This version of KVM requres a host kernel of 2.6.25 or higher."
277         elif use kernel_linux; then
278                 if ! linux_config_exists; then
279                         eerror "Unable to check your kernel for KVM support"
280                 else
281                         CONFIG_CHECK="~KVM ~TUN ~BRIDGE"
282                         ERROR_KVM="You must enable KVM in your kernel to continue"
283                         ERROR_KVM_AMD="If you have an AMD CPU, you must enable KVM_AMD in"
284                         ERROR_KVM_AMD+=" your kernel configuration."
285                         ERROR_KVM_INTEL="If you have an Intel CPU, you must enable"
286                         ERROR_KVM_INTEL+=" KVM_INTEL in your kernel configuration."
287                         ERROR_TUN="You will need the Universal TUN/TAP driver compiled"
288                         ERROR_TUN+=" into your kernel or loaded as a module to use the"
289                         ERROR_TUN+=" virtual network device if using -net tap."
290                         ERROR_BRIDGE="You will also need support for 802.1d"
291                         ERROR_BRIDGE+=" Ethernet Bridging for some network configurations."
292                         use vhost-net && CONFIG_CHECK+=" ~VHOST_NET"
293                         ERROR_VHOST_NET="You must enable VHOST_NET to have vhost-net"
294                         ERROR_VHOST_NET+=" support"
295
296                         if use amd64 || use x86 || use amd64-linux || use x86-linux; then
297                                 if grep -q AuthenticAMD /proc/cpuinfo; then
298                                         CONFIG_CHECK+=" ~KVM_AMD"
299                                 elif grep -q GenuineIntel /proc/cpuinfo; then
300                                         CONFIG_CHECK+=" ~KVM_INTEL"
301                                 fi
302                         fi
303
304                         use python && CONFIG_CHECK+=" ~DEBUG_FS"
305                         ERROR_DEBUG_FS="debugFS support required for kvm_stat"
306
307                         # Now do the actual checks setup above
308                         check_extra_config
309                 fi
310         fi
311
312         if grep -qs '/usr/bin/qemu-kvm' "${EROOT}"/etc/libvirt/qemu/*.xml; then
313                 eerror "The kvm/qemu-kvm wrappers no longer exist, but your libvirt"
314                 eerror "instances are still pointing to it.  Please update your"
315                 eerror "configs in /etc/libvirt/qemu/ to use the -enable-kvm flag"
316                 eerror "and the right system binary (e.g. qemu-system-x86_64)."
317                 die "update your virt configs to not use qemu-kvm"
318         fi
319 }
320
321 # Sanity check to make sure target lists are kept up-to-date.
322 check_targets() {
323         local var=$1 mak=$2
324         local detected sorted
325
326         pushd "${S}"/default-configs >/dev/null || die
327
328         # Force C locale until glibc is updated. #564936
329         detected=$(echo $(printf '%s\n' *-${mak}.mak | sed "s:-${mak}.mak::" | LC_COLLATE=C sort -u))
330         sorted=$(echo $(printf '%s\n' ${!var} | LC_COLLATE=C sort -u))
331         if [[ ${sorted} != "${detected}" ]] ; then
332                 eerror "The ebuild needs to be kept in sync."
333                 eerror "${var}: ${sorted}"
334                 eerror "$(printf '%-*s' ${#var} configure): ${detected}"
335                 die "sync ${var} to the list of targets"
336         fi
337
338         popd >/dev/null
339 }
340
341 handle_locales() {
342         # Make sure locale list is kept up-to-date.
343         local detected sorted
344         detected=$(echo $(cd po && printf '%s\n' *.po | grep -v messages.po | sed 's:.po$::' | sort -u))
345         sorted=$(echo $(printf '%s\n' ${PLOCALES} | sort -u))
346         if [[ ${sorted} != "${detected}" ]] ; then
347                 eerror "The ebuild needs to be kept in sync."
348                 eerror "PLOCALES: ${sorted}"
349                 eerror " po/*.po: ${detected}"
350                 die "sync PLOCALES"
351         fi
352
353         # Deal with selective install of locales.
354         if use nls ; then
355                 # Delete locales the user does not want. #577814
356                 rm_loc() { rm po/$1.po || die; }
357                 l10n_for_each_disabled_locale_do rm_loc
358         else
359                 # Cheap hack to disable gettext .mo generation.
360                 rm -f po/*.po
361         fi
362 }
363
364 src_prepare() {
365         check_targets IUSE_SOFTMMU_TARGETS softmmu
366         check_targets IUSE_USER_TARGETS linux-user
367
368         default
369
370         # Use correct toolchain to fix cross-compiling
371         tc-export AR LD NM OBJCOPY PKG_CONFIG
372         export WINDRES=${CHOST}-windres
373
374         # Verbose builds
375         MAKEOPTS+=" V=1"
376
377         # Run after we've applied all patches.
378         handle_locales
379
380         # Remove bundled copy of libfdt
381         rm -r dtc || die
382 }
383
384 ##
385 # configures qemu based on the build directory and the build type
386 # we are using.
387 #
388 qemu_src_configure() {
389         debug-print-function ${FUNCNAME} "$@"
390
391         local buildtype=$1
392         local builddir="${S}/${buildtype}-build"
393
394         mkdir "${builddir}"
395
396         local conf_opts=(
397                 --prefix=/usr
398                 --sysconfdir=/etc
399                 --bindir=/usr/bin
400                 --libdir=/usr/$(get_libdir)
401                 --datadir=/usr/share
402                 --docdir=/usr/share/doc/${PF}/html
403                 --mandir=/usr/share/man
404                 --with-confsuffix=/qemu
405                 --localstatedir=/var
406                 --disable-bsd-user
407                 --disable-guest-agent
408                 --disable-strip
409                 --disable-werror
410                 # We support gnutls/nettle for crypto operations.  It is possible
411                 # to use gcrypt when gnutls/nettle are disabled (but not when they
412                 # are enabled), but it's not really worth the hassle.  Disable it
413                 # all the time to avoid automatically detecting it. #568856
414                 --disable-gcrypt
415                 --python="${PYTHON}"
416                 --cc="$(tc-getCC)"
417                 --cxx="$(tc-getCXX)"
418                 --host-cc="$(tc-getBUILD_CC)"
419                 $(use_enable debug debug-info)
420                 $(use_enable debug debug-tcg)
421                 $(use_enable doc docs)
422                 $(use_enable tci tcg-interpreter)
423                 $(use_enable xattr attr)
424         )
425
426         # Disable options not used by user targets. This simplifies building
427         # static user targets (USE=static-user) considerably.
428         conf_notuser() {
429                 if [[ ${buildtype} == "user" ]] ; then
430                         echo "--disable-${2:-$1}"
431                 else
432                         use_enable "$@"
433                 fi
434         }
435         conf_opts+=(
436                 --disable-bluez
437                 $(conf_notuser accessibility brlapi)
438                 $(conf_notuser aio linux-aio)
439                 $(conf_notuser bzip2)
440                 $(conf_notuser capstone)
441                 $(conf_notuser caps cap-ng)
442                 $(conf_notuser curl)
443                 $(conf_notuser fdt)
444                 $(conf_notuser glusterfs)
445                 $(conf_notuser gnutls)
446                 $(conf_notuser gnutls nettle)
447                 $(conf_notuser gtk)
448                 $(conf_notuser infiniband rdma)
449                 $(conf_notuser iscsi libiscsi)
450                 $(conf_notuser jemalloc jemalloc)
451                 $(conf_notuser jpeg vnc-jpeg)
452                 $(conf_notuser kernel_linux kvm)
453                 $(conf_notuser lzo)
454                 $(conf_notuser ncurses curses)
455                 $(conf_notuser nfs libnfs)
456                 $(conf_notuser numa)
457                 $(conf_notuser opengl)
458                 $(conf_notuser png vnc-png)
459                 $(conf_notuser rbd)
460                 $(conf_notuser sasl vnc-sasl)
461                 $(conf_notuser sdl)
462                 $(conf_notuser seccomp)
463                 $(conf_notuser smartcard)
464                 $(conf_notuser snappy)
465                 $(conf_notuser spice)
466                 $(conf_notuser ssh libssh)
467                 $(conf_notuser usb libusb)
468                 $(conf_notuser usbredir usb-redir)
469                 $(conf_notuser vde)
470                 $(conf_notuser vhost-net)
471                 $(conf_notuser virgl virglrenderer)
472                 $(conf_notuser virtfs)
473                 $(conf_notuser vnc)
474                 $(conf_notuser vte)
475                 $(conf_notuser xen)
476                 $(conf_notuser xen xen-pci-passthrough)
477                 $(conf_notuser xfs xfsctl)
478                 $(conf_notuser xkb xkbcommon)
479         )
480
481         if [[ ${buildtype} == "user" ]] ; then
482                 conf_opts+=( --disable-libxml2 )
483         else
484                 conf_opts+=( --enable-libxml2 )
485         fi
486
487         if [[ ! ${buildtype} == "user" ]] ; then
488                 # audio options
489                 local audio_opts=(
490                         $(usev alsa)
491                         $(usev oss)
492                         $(usev sdl)
493                         $(usex pulseaudio pa "")
494                 )
495                 conf_opts+=(
496                         --audio-drv-list=$(printf "%s," "${audio_opts[@]}")
497                 )
498         fi
499
500         case ${buildtype} in
501         user)
502                 conf_opts+=(
503                         --enable-linux-user
504                         --disable-system
505                         --disable-blobs
506                         --disable-tools
507                 )
508                 local static_flag="static-user"
509                 ;;
510         softmmu)
511                 conf_opts+=(
512                         --disable-linux-user
513                         --enable-system
514                         --disable-tools
515                 )
516                 local static_flag="static"
517                 ;;
518         tools)
519                 conf_opts+=(
520                         --disable-linux-user
521                         --disable-system
522                         --disable-blobs
523                         --enable-tools
524                 )
525                 local static_flag="static"
526                 ;;
527         esac
528
529         local targets="${buildtype}_targets"
530         [[ -n ${targets} ]] && conf_opts+=( --target-list="${!targets}" )
531
532         # Add support for SystemTAP
533         use systemtap && conf_opts+=( --enable-trace-backend=dtrace )
534
535         # We always want to attempt to build with PIE support as it results
536         # in a more secure binary. But it doesn't work with static or if
537         # the current GCC doesn't have PIE support.
538         if use ${static_flag}; then
539                 conf_opts+=( --static --disable-pie )
540         else
541                 tc-enables-pie && conf_opts+=( --enable-pie )
542         fi
543
544         echo "../configure ${conf_opts[*]}"
545         cd "${builddir}"
546         ../configure "${conf_opts[@]}" || die "configure failed"
547
548         # FreeBSD's kernel does not support QEMU assigning/grabbing
549         # host USB devices yet
550         use kernel_FreeBSD && \
551                 sed -i -E -e "s|^(HOST_USB=)bsd|\1stub|" "${S}"/config-host.mak
552 }
553
554 src_configure() {
555         local target
556
557         python_setup
558
559         softmmu_targets= softmmu_bins=()
560         user_targets= user_bins=()
561
562         for target in ${IUSE_SOFTMMU_TARGETS} ; do
563                 if use "qemu_softmmu_targets_${target}"; then
564                         softmmu_targets+=",${target}-softmmu"
565                         softmmu_bins+=( "qemu-system-${target}" )
566                 fi
567         done
568
569         for target in ${IUSE_USER_TARGETS} ; do
570                 if use "qemu_user_targets_${target}"; then
571                         user_targets+=",${target}-linux-user"
572                         user_bins+=( "qemu-${target}" )
573                 fi
574         done
575
576         softmmu_targets=${softmmu_targets#,}
577         user_targets=${user_targets#,}
578
579         [[ -n ${softmmu_targets} ]] && qemu_src_configure "softmmu"
580         [[ -n ${user_targets}    ]] && qemu_src_configure "user"
581         qemu_src_configure "tools"
582 }
583
584 src_compile() {
585         if [[ -n ${user_targets} ]]; then
586                 cd "${S}/user-build"
587                 default
588         fi
589
590         if [[ -n ${softmmu_targets} ]]; then
591                 cd "${S}/softmmu-build"
592                 default
593         fi
594
595         cd "${S}/tools-build"
596         default
597 }
598
599 src_test() {
600         if [[ -n ${softmmu_targets} ]]; then
601                 cd "${S}/softmmu-build"
602                 pax-mark m */qemu-system-* #515550
603                 emake -j1 check
604                 emake -j1 check-report.html
605         fi
606 }
607
608 qemu_python_install() {
609         python_domodule "${S}/python/qemu"
610
611         python_doscript "${S}/scripts/kvm/vmxcap"
612         python_doscript "${S}/scripts/qmp/qmp-shell"
613         python_doscript "${S}/scripts/qmp/qemu-ga-client"
614 }
615
616 # Generate binfmt support files.
617 #   - /etc/init.d/qemu-binfmt script which registers the user handlers (openrc)
618 #   - /usr/share/qemu/binfmt.d/qemu.conf (for use with systemd-binfmt)
619 generate_initd() {
620         local out="${T}/qemu-binfmt"
621         local out_systemd="${T}/qemu.conf"
622         local d="${T}/binfmt.d"
623
624         einfo "Generating qemu binfmt scripts and configuration files"
625
626         # Generate the debian fragments first.
627         mkdir -p "${d}"
628         "${S}"/scripts/qemu-binfmt-conf.sh \
629                 --debian \
630                 --exportdir "${d}" \
631                 --qemu-path "${EPREFIX}/usr/bin" \
632                 || die
633         # Then turn the fragments into a shell script we can source.
634         sed -E -i \
635                 -e 's:^([^ ]+) (.*)$:\1="\2":' \
636                 "${d}"/* || die
637
638         # Generate the init.d script by assembling the fragments from above.
639         local f qcpu package interpreter magic mask
640         cat "${FILESDIR}"/qemu-binfmt.initd.head >"${out}" || die
641         for f in "${d}"/qemu-* ; do
642                 source "${f}"
643
644                 # Normalize the cpu logic like we do in the init.d for the native cpu.
645                 qcpu=${package#qemu-}
646                 case ${qcpu} in
647                 arm*)   qcpu="arm";;
648                 mips*)  qcpu="mips";;
649                 ppc*)   qcpu="ppc";;
650                 s390*)  qcpu="s390";;
651                 sh*)    qcpu="sh";;
652                 sparc*) qcpu="sparc";;
653                 esac
654
655                 # we use 'printf' here to be portable across 'sh'
656                 # implementations: #679168
657                 cat <<EOF >>"${out}"
658         if [ "\${cpu}" != "${qcpu}" -a -x "${interpreter}" ] ; then
659                 printf '%s\n' ':${package}:M::${magic}:${mask}:${interpreter}:'"\${QEMU_BINFMT_FLAGS}" >/proc/sys/fs/binfmt_misc/register
660         fi
661 EOF
662
663                 echo ":${package}:M::${magic}:${mask}:${interpreter}:OC" >>"${out_systemd}"
664
665         done
666         cat "${FILESDIR}"/qemu-binfmt.initd.tail >>"${out}" || die
667 }
668
669 src_install() {
670         if [[ -n ${user_targets} ]]; then
671                 cd "${S}/user-build"
672                 emake DESTDIR="${ED}" install
673
674                 # Install binfmt handler init script for user targets.
675                 generate_initd
676                 doinitd "${T}/qemu-binfmt"
677
678                 # Install binfmt/qemu.conf.
679                 insinto "/usr/share/qemu/binfmt.d"
680                 doins "${T}/qemu.conf"
681         fi
682
683         if [[ -n ${softmmu_targets} ]]; then
684                 cd "${S}/softmmu-build"
685                 emake DESTDIR="${ED}" install
686
687                 # This might not exist if the test failed. #512010
688                 [[ -e check-report.html ]] && dodoc check-report.html
689
690                 if use kernel_linux; then
691                         udev_newrules "${FILESDIR}"/65-kvm.rules-r1 65-kvm.rules
692                 fi
693
694                 if use python; then
695                         python_foreach_impl qemu_python_install
696                 fi
697         fi
698
699         cd "${S}/tools-build"
700         emake DESTDIR="${ED}" install
701
702         # Disable mprotect on the qemu binaries as they use JITs to be fast #459348
703         pushd "${ED}"/usr/bin >/dev/null
704         pax-mark mr "${softmmu_bins[@]}" "${user_bins[@]}" # bug 575594
705         popd >/dev/null
706
707         # Install config file example for qemu-bridge-helper
708         insinto "/etc/qemu"
709         doins "${FILESDIR}/bridge.conf"
710
711         cd "${S}"
712         dodoc Changelog MAINTAINERS docs/specs/pci-ids.txt
713         newdoc pc-bios/README README.pc-bios
714
715         # Disallow stripping of prebuilt firmware files.
716         dostrip -x ${QA_PREBUILT}
717
718         if [[ -n ${softmmu_targets} ]]; then
719                 # Remove SeaBIOS since we're using the SeaBIOS packaged one
720                 rm "${ED}/usr/share/qemu/bios.bin"
721                 rm "${ED}/usr/share/qemu/bios-256k.bin"
722                 if use qemu_softmmu_targets_x86_64 || use qemu_softmmu_targets_i386; then
723                         dosym ../seabios/bios.bin /usr/share/qemu/bios.bin
724                         dosym ../seabios/bios-256k.bin /usr/share/qemu/bios-256k.bin
725                 fi
726
727                 # Remove vgabios since we're using the seavgabios packaged one
728                 rm "${ED}/usr/share/qemu/vgabios.bin"
729                 rm "${ED}/usr/share/qemu/vgabios-cirrus.bin"
730                 rm "${ED}/usr/share/qemu/vgabios-qxl.bin"
731                 rm "${ED}/usr/share/qemu/vgabios-stdvga.bin"
732                 rm "${ED}/usr/share/qemu/vgabios-virtio.bin"
733                 rm "${ED}/usr/share/qemu/vgabios-vmware.bin"
734                 # PPC64 loads vgabios-stdvga
735                 if use qemu_softmmu_targets_x86_64 || use qemu_softmmu_targets_i386 || use qemu_softmmu_targets_ppc64; then
736                         dosym ../seavgabios/vgabios-isavga.bin /usr/share/qemu/vgabios.bin
737                         dosym ../seavgabios/vgabios-cirrus.bin /usr/share/qemu/vgabios-cirrus.bin
738                         dosym ../seavgabios/vgabios-qxl.bin /usr/share/qemu/vgabios-qxl.bin
739                         dosym ../seavgabios/vgabios-stdvga.bin /usr/share/qemu/vgabios-stdvga.bin
740                         dosym ../seavgabios/vgabios-virtio.bin /usr/share/qemu/vgabios-virtio.bin
741                         dosym ../seavgabios/vgabios-vmware.bin /usr/share/qemu/vgabios-vmware.bin
742                 fi
743
744                 # Remove sgabios since we're using the sgabios packaged one
745                 rm "${ED}/usr/share/qemu/sgabios.bin"
746                 if use qemu_softmmu_targets_x86_64 || use qemu_softmmu_targets_i386; then
747                         dosym ../sgabios/sgabios.bin /usr/share/qemu/sgabios.bin
748                 fi
749
750                 # Remove iPXE since we're using the iPXE packaged one
751                 rm "${ED}"/usr/share/qemu/pxe-*.rom
752                 if use qemu_softmmu_targets_x86_64 || use qemu_softmmu_targets_i386; then
753                         dosym ../ipxe/8086100e.rom /usr/share/qemu/pxe-e1000.rom
754                         dosym ../ipxe/80861209.rom /usr/share/qemu/pxe-eepro100.rom
755                         dosym ../ipxe/10500940.rom /usr/share/qemu/pxe-ne2k_pci.rom
756                         dosym ../ipxe/10222000.rom /usr/share/qemu/pxe-pcnet.rom
757                         dosym ../ipxe/10ec8139.rom /usr/share/qemu/pxe-rtl8139.rom
758                         dosym ../ipxe/1af41000.rom /usr/share/qemu/pxe-virtio.rom
759                 fi
760         fi
761
762         DISABLE_AUTOFORMATTING=true
763         readme.gentoo_create_doc
764 }
765
766 firmware_abi_change() {
767         local pv
768         for pv in ${REPLACING_VERSIONS}; do
769                 if ver_test $pv -lt ${FIRMWARE_ABI_VERSION}; then
770                         return 0
771                 fi
772         done
773         return 1
774 }
775
776 pkg_postinst() {
777         if [[ -n ${softmmu_targets} ]] && use kernel_linux; then
778                 udev_reload
779         fi
780
781         xdg_icon_cache_update
782
783         [[ -z ${EPREFIX} ]] && [[ -f ${EROOT}/usr/libexec/qemu-bridge-helper ]] && \
784                 fcaps cap_net_admin ${EROOT}/usr/libexec/qemu-bridge-helper
785
786         DISABLE_AUTOFORMATTING=true
787         readme.gentoo_print_elog
788
789         if use pin-upstream-blobs && firmware_abi_change; then
790                 ewarn "This version of qemu pins new versions of firmware blobs:"
791                 ewarn " $(best_version sys-firmware/edk2-ovmf)"
792                 ewarn " $(best_version sys-firmware/ipxe)"
793                 ewarn " $(best_version sys-firmware/seabios)"
794                 ewarn " $(best_version sys-firmware/sgabios)"
795                 ewarn "This might break resume of hibernated guests (started with a different"
796                 ewarn "firmware version) and live migration to/from qemu versions with different"
797                 ewarn "firmware. Please (cold) restart all running guests. For functional"
798                 ewarn "guest migration ensure that all"
799                 ewarn "hosts run at least"
800                 ewarn " app-emulation/qemu-${FIRMWARE_ABI_VERSION}."
801         fi
802 }
803
804 pkg_info() {
805         echo "Using:"
806         echo "  $(best_version app-emulation/spice-protocol)"
807         echo "  $(best_version sys-firmware/edk2-ovmf)"
808         if has_version 'sys-firmware/edk2-ovmf[binary]'; then
809                 echo "    USE=binary"
810         else
811                 echo "    USE=''"
812         fi
813         echo "  $(best_version sys-firmware/ipxe)"
814         echo "  $(best_version sys-firmware/seabios)"
815         if has_version 'sys-firmware/seabios[binary]'; then
816                 echo "    USE=binary"
817         else
818                 echo "    USE=''"
819         fi
820         echo "  $(best_version sys-firmware/sgabios)"
821 }
822
823 pkg_postrm() {
824         xdg_icon_cache_update
825 }