Sunday, February 2, 2014

Installing Go (GoLang), With Cross-Compile Support, on a Raspberry Pi

I've written previously that I was working on playing with programming languages. For reasons I won't bother getting into in this post, I decided I wanted to play with Google's new flagship language, Go.

For a playground in which to experiment, I decided to use a Raspberry Pi running Raspbian 3.10.25+ (Wheezy) at the time of these instructions.

First, there was a version of Go in the apt-get repo. I installed it and discovered it was running version 1.0.x. I love the package management system, since it keeps things up to date without worrying so much about cruft and dependencies. But the downside is that getting new versions of packages means waiting until package maintainers get around to upgrading everything. Once I saw the version of Go installed by apt-get, I ran apt-get remove.

From there I followed Dave Cheney's installation from source instructions. Since I was using a Raspberry Pi with more memory, I didn't need to do the memory split and swap parts of his instructions.

His instructions were to first install prerequisite tools.

sudo apt-get install -y mercurial gcc libc6-dev

Next use Mercurial to get a copy of the Go compiler source.

hg clone -u default https://code.google.com/p/go $HOME/go

This places a copy of the current source under the directory "go" in your home directory. The next step is to actually build Go.

cd go/src
./all.bash

This will build Go and run a series of tests, although they will take somewhere between an hour and two hours to complete. The last messages should contain a reference to all the tests having passed; if they didn't, something went wrong (obviously.)

The next step is to add the path for the binaries. Instead of just exporting the path as mentioned in the instructions, I added it to the end of my .bash_profile.

PATH=$PATH:$HOME/go/bin

At this point you should be able to get a version.

go version

...will give something like:

go version devel +2fcaec19e523 Fri Jan 31 18:09:53 2014 +0400 linux/arm

That version corresponds to version 1.2.x of Go. What I did next was to set up Go for a cross compiling environment. Go actually has decent support for compiling binaries for other platforms without requiring a compilation environment on the target platform. There are problems with this, and it's possible a future release will iron them out.

The steps to create the cross-compile environment has been partially automated, thanks again to instructions from Dave Cheney. His instructions were for version 1.1.x, but seemed to work for 1.2.x.

Quick summary...

Grab support scripts from GitHub. I did this from the go/src directory.

git clone git://github.com/davecheney/golang-crosscompile.git
source golang-crosscompile/crosscompile.bash

Then we run the build the new compiler executables.

go-crosscompile-build-all

After a very long time, you should have a whole bunch of platform-specific packages residing in the go/pkg.

To do the actual cross-compile, you just run go-<platform> on the target .go file. The "source golang-crosscompile..." command above creates a series of aliases to create a command like "go-linux-arm" and "go-darwin-amd64". From the proper workspace directory, I can run something like

go-darwin-amd64 install

...and the proper platform-specific executable is created.

One note to remember is that the crosscompile.bash script doesn't seem to make these changes permanent. You can re-run the script before playing with the cross compile builds, or parse the script and add the proper aliases to your profile for permanent use.

No comments:

Post a Comment