commit 196a9447baa5d26bf7043d98ad0e13c5e649ea9c
Author: Mihai Moldovan <ionic@macports.org>
Date:   Fri Nov 22 17:51:29 2019 +0100

    emulators/qemu: fix builds using out-of-source builds. Revbump.
    
    This targets mostly older platforms, but also generally changes behavior
    to a more correct one.
    
    The qemu build system is buggy when it comes to in-source-builds. Due to
    Makefile dependencies, it always tries to reconfigure using a special
    generated file at install time and also re-compiles the whole project.
    This would normally be just an annoyance, but because not all parameters
    are passed through (exported) correctly, the second build diverges from
    the correct first one.
    
    It shouldn't come as a surprise that Linux distros seem to use
    out-of-source builds for qemu, which nicely works around that problem.
    
    Additionally, older platforms have an mktemp implementation that always
    expects a template as its parameter, but the Makefile doesn't prove one
    in some cases. Hence, switch to GNU coreutils's on 10.10-, but keep
    using mktemp on more recent platforms because it seems to work fine
    there.
    
    Fixes: https://trac.macports.org/ticket/59257

diff --git a/emulators/qemu/Portfile b/emulators/qemu/Portfile
index 3f58aa5462d..27beae19a53 100644
--- a/emulators/qemu/Portfile
+++ b/emulators/qemu/Portfile
@@ -8,7 +8,7 @@ PortGroup legacysupport 1.0
 
 name                    qemu
 version                 4.1.0
-revision                0
+revision                1
 categories              emulators
 license                 GPL-2+
 platforms               darwin
@@ -106,6 +106,25 @@ pre-configure {
     configure.args-append --target-list=${target_list}
 }
 
+# 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
 
@@ -205,6 +224,13 @@ platform darwin 8 {
     build.cmd ${prefix}/bin/gmake
 }
 
+platform darwin {
+    if {${os.major} < 15} {
+        depends_build-append    port:coreutils
+        patchfiles-append       patch-Makefile-legacy-mktemp-to-coreutils.diff
+    }
+}
+
 livecheck.type  regex
 livecheck.url   [lindex ${master_sites} 0]
 livecheck.regex ${name}-(\\d+(?:\\.\\d+)*)(?:-\\d+)*\\.tar
diff --git a/emulators/qemu/files/patch-Makefile-legacy-mktemp-to-coreutils.diff b/emulators/qemu/files/patch-Makefile-legacy-mktemp-to-coreutils.diff
new file mode 100644
index 00000000000..2031f84cbb2
--- /dev/null
+++ b/emulators/qemu/files/patch-Makefile-legacy-mktemp-to-coreutils.diff
@@ -0,0 +1,11 @@
+--- a/Makefile	2019-08-15 21:01:42.000000000 +0200
++++ b/Makefile	2019-11-22 13:38:08.000000000 +0100
+@@ -879,7 +879,7 @@ ifdef INSTALL_BLOBS
+ endif
+ ifneq ($(DESCS),)
+ 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/firmware"
+-	set -e; tmpf=$$(mktemp); trap 'rm -f -- "$$tmpf"' EXIT; \
++	set -e; tmpf=$$(gmktemp); trap 'rm -f -- "$$tmpf"' EXIT; \
+ 	for x in $(DESCS); do \
+ 		sed -e 's,@DATADIR@,$(qemu_datadir),' \
+ 			"$(SRC_PATH)/pc-bios/descriptors/$$x" > "$$tmpf"; \
