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