Cross compile support for go plugin

Currently snapcraft doesn’t really help with cross-compiling code for an architecture different to the host. The basics are there but plugins don’t implement it, except for building a kernel snap.

I’m starting with the Go plugin because Go makes it pretty straightforward to build for another architecture and/ or operating system.
Snapcraft also needs to know how to setup the host for multi-arch packages and get build-packages for the target architecture.

2 Likes

Do you have notes on the design?

There are no notes on the original work. Maybe @kalikiana can offer to write general notes about cross compilation in a new post as guidance. It is pretty simple though and up to the plugin, what today isn’t really supported and would need some thought is cross compilation when using classic confinement.

https://github.com/snapcore/snapcraft/pull/1253

General enablement for cross-compiling:

  • Add foreign architecture FOO if needed
  • Setup sources.list with [arch=FOO]
  • Install libc6-dev-FOO-cross’
  • Install dependencies with :foo

Setup in the Go plugin:

  • Set CC/CXX/PKG_CONFIG_PATH/CGO_ENABLED/GOARCH
  • Use go build instead of go install

This is implemented here:
https://github.com/snapcore/snapcraft/pull/1253

How would that look to the person interacting with snapcraft? I mean, command line interface, how would snapcraft.yaml be tuned to indicate this is supported, etc.

On the plugin side it’s actually quite simple. Setting a handful of environment variables and because of a quirk in Go not using go install. I’ll come up with a little introductory post on how to enable that in other plugins.

Nothing to be done for the user. The --target= option that already exists is used with the internal architecture mappings to select the necessary packages and compiler names/ paths. At least for Go I’m not aware of a use case where custom setup would be needed. I haven’t looked into other plugins yet.

1 Like

Orthogonally I’m implementing support for cross-compiling in LXD containers which isn’t currently possible.

For those following this topic:

The PRs go plugin: Add support for cross-compilation and go plugin: Cross compile with CGo which I split out of the original branch which was becoming too difficult to review have been merged. Go cross-compile to your heart’s content (pun intended)!