Subject: bug#20239: [wishlist] Add build hook to build for other platforms using qemu

From: [email-masked] (Ludovic Courtès)
Date: Tue, 28 Mar 2017 18:04:25 +0200

[email-masked] (Ludovic Courtès) skribis:

> Mark H Weaver <[email-masked]> skribis:
>
>> It would be great if we had a build hook to enable guix-daemon to
>> natively build packages for any system supported by qemu, by running the
>> build processes within qemu.
>
> QEMU has a ‘qemu-binfmt-conf.sh’ script that installs binfmt_misc
> handlers for all the architecture-specific ELF variants.  Once you’ve
> run this script, you can transparently run, say, ARM executables (the
> kernel takes care of invoking ‘qemu-arm’ for you).

[...]

> Then we just need to tell the daemon to not complain (“but I’m an
> 'x86_64-linux'”).

The attached patch does that.

However, there’s an added complication: the file name of the qemu-*
executables registered in binfmt_misc are apparently resolved relative
to the root directory of the process that does ‘execve’.

So we would need to add a guix-daemon --chroot-directory=DIR argument
for each element in the closure of QEMU.  Not great.

Thoughts?

Ludo’.

diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc
index 9b7bb5391..bca75a4f9 100644
--- a/nix/libstore/build.cc
+++ b/nix/libstore/build.cc
@@ -1672,15 +1672,6 @@ void DerivationGoal::startBuilder()
     f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
     startNest(nest, lvlInfo, f % showPaths(missingPaths) % curRound % nrRounds);
 
-    /* Right platform? */
-    if (!canBuildLocally(drv.platform)) {
-        if (settings.printBuildTrace)
-            printMsg(lvlError, format("@ unsupported-platform %1% %2%") % drvPath % drv.platform);
-        throw Error(
-            format("a `%1%' is required to build `%3%', but I am a `%2%'")
-            % drv.platform % settings.thisSystem % drvPath);
-    }
-
     /* Note: built-in builders are *not* running in a chroot environment so
        that we can easily implement them in Guile without having it as a
        derivation input (they are running under a separate build user,
@@ -2305,6 +2296,18 @@ void DerivationGoal::runChild()
 
         execve(drv.builder.c_str(), stringsToCharPtrs(args).data(), stringsToCharPtrs(envStrs).data());
 
+	int error = errno;
+
+	/* Right platform? */
+	if (error == ENOEXEC && !canBuildLocally(drv.platform)) {
+	    if (settings.printBuildTrace)
+		printMsg(lvlError, format("@ unsupported-platform %1% %2%") % drvPath % drv.platform);
+	    throw Error(
+		format("a `%1%' is required to build `%3%', but I am a `%2%'")
+		% drv.platform % settings.thisSystem % drvPath);
+	}
+
+	errno = error;
         throw SysError(format("executing `%1%'") % drv.builder);
 
     } catch (std::exception & e) {