# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               active_variants 1.1
PortGroup               legacysupport 1.1

# clock_gettime
legacysupport.newest_darwin_requires_legacy 16

name                    qemu
version                 10.1.2
revision                0
categories              emulators
license                 GPL-2+
maintainers             {raimue @raimue} \
                        {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

description             Emulator for various architectures
long_description        QEMU is a generic and open source machine emulator. \
                        It can run OSes and programs made for one machine on a \
                        different machine. By using dynamic translation, it \
                        achieves very good performance.

homepage                https://www.qemu.org
master_sites            https://download.qemu.org/
use_xz                  yes

checksums               rmd160  34b708a1e351126ee993608adbfde2bbfb772ea2 \
                        sha256  9d75f331c1a5cb9b6eb8fd9f64f563ec2eab346c822cb97f8b35cd82d3f11479 \
                        size    142050300

set py_branch           3.13
set py_version          [string replace ${py_branch} 1 1]

depends_build-append    port:texinfo \
                        port:libtool \
                        port:ninja \
                        path:bin/pkg-config:pkgconfig \
                        port:py${py_version}-distlib \
                        port:py${py_version}-meson \
                        port:py${py_version}-sphinx \
                        port:py${py_version}-sphinx_rtd_theme

# perl5 is only used for build scripts, no linking
depends_build-append    port:perl5

license_noconflict      perl5

depends_lib-append      path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        path:lib/pkgconfig/gnutls.pc:gnutls \
                        path:lib/pkgconfig/pixman-1.pc:libpixman \
                        port:bzip2 \
                        port:libslirp \
                        port:lzfse \
                        port:lzo2 \
                        port:snappy \
                        port:zlib \
                        port:zstd \
                        port:dtc

# "error: unknown type name 'fpunchhole_t'" pre-10.12.4
patchfiles-append       patch-qemu-punchhole.diff

# IPPROTO workarounds
patchfiles-append       patch-qemu-net-colo.diff

# MADV_DONTNEED workaround
patchfiles-append       patch-qemu-include-qemu-madvise_h.diff

# MAP_JIT workaround
patchfiles-append       patch-qemu-tcg-region.diff

# hfscompression workaround
patchfiles-append       patch-qemu-block-file-posix.diff

# SDL2 GL workaround
patchfiles-append       patch-qemu-ui-sdl2.diff

# ERROR: Problem encountered: You either need GCC v7.4 or Clang v10.0 (or XCode Clang v15.0) to compile QEMU
compiler.blacklist      {clang < 1500} {macports-clang-[3-9].*} {macports-gcc-[56]} {*gcc-[34].*}
if {${os.platform} eq "darwin" && ${os.major} < 11 && [string match *clang* ${configure.compiler}]} {
    # Emulated TLS needed, which was added in LLVM 3.8
    configure.cflags-append     -femulated-tls
}

if {"${configure.build_arch}" eq "arm64"} {
    set host_cpu "aarch64"
} else {
    set host_cpu "${configure.build_arch}"
}
# Select compiler
configure.args          --cpu=${host_cpu} \
                        --cc=${configure.cc} \
                        --objcc=${configure.objc} \
                        --host-cc=${configure.cc}

# Do not use iasl, even if it is installed, #43911
configure.args-append   --iasl=/usr/bin/false

# Use MacPorts Python
configure.args-append   --python=${prefix}/bin/python${py_branch}

# Select features
configure.args-append   --disable-cocoa \
                        --disable-curses \
                        --disable-capstone \
                        --disable-sdl \
                        --disable-gtk \
                        --disable-opengl \
                        --disable-virglrenderer \
                        --enable-bzip2 \
                        --enable-hvf \
                        --disable-curl \
                        --disable-attr \
                        --disable-vde \
                        --disable-jack \
                        --disable-brlapi \
                        --disable-cap-ng \
                        --disable-spice-protocol \
                        --disable-spice \
                        --disable-libiscsi \
                        --disable-rbd \
                        --disable-smartcard \
                        --disable-libusb \
                        --disable-usb-redir \
                        --disable-seccomp \
                        --enable-slirp \
                        --disable-linux-aio \
                        --disable-glusterfs \
                        --disable-rdma \
                        --disable-libssh \
                        --disable-vnc \
                        --enable-gnutls \
                        --disable-gcrypt \
                        --disable-nettle \
                        --disable-numa \
                        --disable-xen \
                        --enable-snappy \
                        --enable-lzo \
                        --enable-lzfse \
                        --disable-png \
                        --enable-zstd \
                        --disable-dbus-display \
                        --enable-bochs \
                        --enable-cloop \
                        --enable-dmg \
                        --enable-qcow1 \
                        --enable-qed \
                        --enable-tools \
                        --enable-parallels \
                        --enable-vdi \
                        --enable-virtfs \
                        --enable-vvfat \
                        --enable-fdt=system \
                        --disable-fuse

# Use 'smbd' installed by samba port, rather than macOS; latter does not work with qemu.
configure.args-append   --smbd=${prefix}/sbin/smbd

# Hypervisor.framework introduced in 10.10
if {${os.platform} eq "darwin" && ${os.major} < 14} {
    configure.args-replace --enable-hvf --disable-hvf
}

# pthread_fchdir_np introduced in 10.12
if {${os.platform} eq "darwin" && ${os.major} < 16} {
    configure.args-replace --enable-virtfs --disable-virtfs
}

# libtool: unknown option character `-' in: --mode=compile
configure.env-append   LIBTOOL=${prefix}/bin/glibtool

set target_list {}
pre-configure {
    configure.args-append --target-list=${target_list}
    configure.args-append --extra-ldflags="${configure.ldflags}"
}

# The qemu build system bugs out when using in-source-tree builds.
# During builds, files are generated in directories such as pc-bios, which are
# prerequites to the config-host.mak file. The latter gets generated during a
# configure run and can (in theory) be used for reconfiguring the package.
# Practically, this reconfiguration fails because it doesn't take special flags
# into account like LDFLAGS and the like, so we have to avoid reconfiguring
# qemu.
# Additionally, just "touch"ing the file doesn't work either, because it is a
# prerequisite of other generated file which will, likewise, be generated twice.
# Using out-of-source builds we can work around this issue in a pretty nice way.
configure.dir   "${workpath}/build"

configure.cmd   "${worksrcpath}/configure"
build.dir       "${workpath}/build"

pre-configure {
    file mkdir ${configure.dir}
}

# disable silent rules
build.args-append       V=1

default_variants        +usb +spice +vnc +curses

foreach t {i386 x86_64 alpha {arm aarch64} cris hppa m68k {microblaze microblazeel} {mips mipsel mips64 mips64el} \
           nios2 or1k {ppc ppc64} riscv32 riscv64 rx s390x {sh4 sh4eb} {sparc sparc64} tricore {xtensa xtensaeb}} {
    variant target_[lindex $t 0] description "Add target support for [join $t {, }]" "append target_list \",[join $t -softmmu,]-softmmu\""
}
default_variants-append +target_i386 +target_x86_64 +target_arm

# https://trac.macports.org/ticket/62164#comment:10
if {${os.platform} eq "darwin" && ${os.major} >= 18} {
    default_variants-append +cocoa
}

variant cocoa description {Use the native Mac OS X graphical user interface} {
    configure.args-replace  --disable-cocoa --enable-cocoa
}

variant curses description {Use the curses text-only user interface} {
    configure.args-replace  --disable-curses --enable-curses
    depends_lib-append      port:ncurses \
                            port:libiconv
}

variant gtk3 description {Use the GTK+3 graphical user interface} {
    configure.args-replace  --disable-gtk --enable-gtk
    depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3 port:vte
}

variant sdl2 description {Use the SDL 2 graphical user interface} {
    configure.args-replace  --disable-sdl --enable-sdl
    depends_lib-append      port:libsdl2
}

variant usb description {Support forwarding of USB devices to the guest} {
    configure.args-replace  --disable-libusb --enable-libusb
    configure.args-replace  --disable-usb-redir --enable-usb-redir
    depends_lib-append      path:lib/pkgconfig/libusb-1.0.pc:libusb \
                            port:usbredir
}

variant curl description {Support network block devices using CURL} {
    # Factored to a separate variant to enable binary packages, as the
    # CURL port defaults to using OpenSSL for cryptography, which triggers
    # a licensing conflict.
    configure.args-replace  --disable-curl --enable-curl
    depends_lib-append      port:curl
}

variant vnc description {Support VNC server} {
    configure.args-replace  --disable-png --enable-png \
                            --disable-vnc --enable-vnc
    configure.args-append   --enable-vnc-jpeg \
                            --enable-vnc-sasl
    depends_lib-append      port:cyrus-sasl2 \
                            path:include/turbojpeg.h:libjpeg-turbo \
                            port:libpng
}

variant spice_protocol description {Support Spice protocol} {
    configure.args-replace  --disable-spice-protocol --enable-spice-protocol
    depends_build-append    port:spice-protocol
}

variant spice requires spice_protocol description {Support Spice server} {
    configure.args-replace  --disable-spice --enable-spice
    depends_lib-append      port:spice-server
}

variant vde description {Support VDE networking} {
    configure.args-replace  --disable-vde --enable-vde
    depends_lib-append      port:vde2
}

variant ssh description {Support remote block devices over SSH} {
    configure.args-replace  --disable-libssh --enable-libssh
    depends_lib-append      port:libssh
}

variant dbus description {Export the VM display through D-Bus} {
    configure.args-replace  --disable-dbus-display --enable-dbus-display
    configure.args-append   --enable-modules
}

variant fuse description {Support exporting block devices via FUSE} {
    configure.args-replace  --disable-fuse --enable-fuse
    depends_lib-append      port:macfuse
}

variant gl description {Support 3D acceleration in guests} {
    configure.args-replace  --disable-opengl --enable-opengl \
                            --disable-virglrenderer --enable-virglrenderer
    depends_lib-append      port:libepoxy \
                            port:virglrenderer
    require_active_variants libepoxy egl
}

# Default universal variant does not work
universal_variant       no

platform darwin 8 {
    depends_build-append port:gmake
    build.cmd ${prefix}/bin/gmake
}

livecheck.type  regex
livecheck.url   [lindex ${master_sites} 0]
livecheck.regex ${name}-(\\d+(?:\\.\\d+)*)(?:-\\d+)*\\.tar
