Compiling Go to run on Android on a Nexus S

Originally posted on Google Plus on 2012-01-17.


It turns out to be really easy to cross compile Go. The Go build instructions are at http://golang.org/doc/install.html . I built Go like this on an Ubuntu 8.04 i686 :
  export GOROOT=$HOME/go-arm_v7
  export GOARCH=arm
  export GOOS=linux
  export GOBIN=$HOME/go-arm_v7/bin
  export PATH=$PATH:$GOBIN
  export GOARM=7
  hg clone -r release https://go.googlecode.com/hg/ $GOROOT
  cd $GOROOT/src
  env | grep '^GO'
  ./all.bash
I note that the tests failed and I have not investigated. The build may not be be entirely correct.

The Nexus S uses ARMv7 instructions. Another phone may not so maybe use
GOARM=5
I took a Go source file I had named map-1.go and compiled it using:
  5g map-1.go && 5l -o map-1 map-1.5
Running
file map-1
shows it is an ARM file:
  map-1: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
Now transfer it to a Nexus S as /sdcard/map-1 . "adb push" or mounting as a USB drive will work. I did not have either so I used Opera to download from an HTTP server.

It turns out /sdcard is mounted noexec . I should find a place to put binaries from development but for trying it out now I just:
  su
  mount -o remount /mnt/sdcard
And run it:
  /sdcard/map-1
The binary runs successfully!