Subject: [bug#48044] [PATCH v3] build/go: Support cross compiling.

From: Efraim Flashner <[email-masked]>
Date: Mon, 23 Aug 2021 12:02:10 +0300

On Sun, Aug 22, 2021 at 11:52:29AM -0700, Sarah Morgensen wrote:
> Hi Efraim,
> 
> Thanks for doing this work!  I'm excited to see it in action.
> 
> Efraim Flashner <[email-masked]> writes:
> 
> > * guix/build-system/go.scm (lower): Only add target to private-keywords
> > when not cross compiling. Adjust bag depending if doing a native or
> > cross compile.
> > (%go-build-system-modules): Use source-module-closure, add (guix utils).
> > (go-cross-build): New procedure.
> > * guix/build/go-build-system.scm (setup-go-environment): Accept target
> > keyword. Add logic to choose correct target architecture when cross
> > compiling.
> > ---
> >
> > Third version of this patch. I think I'm ready to push it. I don't love
> > using source-module-closure to include (guix utils), but I need it for
> > gnu-triplet->nix-system in setup-go-environment instead of the custom
> > parsing I was doing before.
> 
> Can you do the parsing host-side and pass e.g. TARGET-GOOS/TARGET-GOARCH
> keyword arguments to the build-side?

I'll have to see if there's somewhere I can slot that in. We already
have target, but that's what we're parsing now.

I guess if I add them as a keywords then we can build for targets that
go supports but which Guix doesn't.

> > -(define* (setup-go-environment #:key inputs outputs #:allow-other-keys)
> > +(define* (setup-go-environment #:key inputs outputs target #:allow-other-keys)
> >    "Prepare a Go build environment for INPUTS and OUTPUTS.  Build a file system
> >  union of INPUTS.  Export GOPATH, which helps the compiler find the source code
> >  of the package being built and its dependencies, and GOBIN, which determines
> > @@ -149,6 +150,38 @@ dependencies, so it should be self-contained."
> >    ;; GOPATH behavior.
> >    (setenv "GO111MODULE" "off")
> >    (setenv "GOBIN" (string-append (assoc-ref outputs "out") "/bin"))
> > +
> > +  ;; Cross-build
> > +  (when target
> > +    ;; Parse the nix-system equivalent of the target and set the
> > +    ;; target for compilation accordingly.
> > +    (let* ((system (gnu-triplet->nix-system target))
> > +           (dash   (string-index system #\-))
> > +           (arch   (substring system 0 dash))
> > +           (os     (substring system (+ 1 dash))))
> 
> And then, if this parsing is host-side, you can probably just do
> something like
> 
> --8<---------------cut here---------------start------------->8---
> (match (string-split (gnu-triplet->nix-system target) #\-)
>   ((arch os)
>    [...]
> --8<---------------cut here---------------end--------------->8---

I like the way this looks much better. Even if we did just parse the
gnu-triplet we'd have our special case for arm-linux-gnueabihf (unless
we ignored it, as I did below). 

> > +      (match arch
> > +        ((or "arm" "armhf")
> > +         (setenv "GOARM" "7"))
> > +        ((or "mips" "mipsel")
> > +         (setenv "GOMIPS" "hardfloat"))
> > +        ((or "mips64" "mips64el")
> > +         (setenv "GOMIPS64" "hardfloat"))
> > +        ((or "powerpc64" "powerpc64le")
> > +         (setenv "GOPPC64" "power8"))
> > +        (_ #t))))
> 
> Are these choices obvious for those compiling for those architectures?
> If not, this could probably do with some documentation on why these were
> chosen.  (I note that these are all Go's defaults with the exception of
> GOARM).

For the mips I did just copy from the Go documentation, but for arm and
ppc64 I wanted to make sure we targeted the same systems that Guix
already supports.

> --
> Sarah

Thanks for taking a look at it.

-- 
Efraim Flashner   <[email-masked]>   רנשלפ םירפא
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted