Saturday, April 23, 2011

Cross Compiling Google Go Code

This is more of a note to self, so I can look up this information in the future as needed. Google Go makes it really easy to cross-compile. Here is how you an compile your Go project for another architecture. The first step is to compile the cross compiling compilers (I can't believe I just typed that). So lets checkout the source code for the go compiler.
hg clone -r release go

Now to compile the Go compiler for the architecture of your machine, simply cd into the go/src directory and run the all.bash script
cd go/src; ./all.bash
If you want to create compilers for a different architecture you set the GOARCH environment variable and then run the make.bash script.
GOARCH=arm ./make.bash
GOARCH=386 ./make.bash
GOARCH=amd64 ./make.bash

The all.bash script compiles the compiler and runs the unit tests, the make.bash script just compiles the compilers.

This will create separate compilers, linkers and assemblers for each of the three architectures. Because each compiler, linker and assembler have a different name you can have support all architectures simultaneously.

Once you have the Go compilers compiled you can now compile your project for different architectures by creating a Go Makefile and setting the GOARCH environment variable before typing make.

GOARCH=arm make
GOARCH=386 make
GOARCH=amd64 make

That's it! You can now cross compile your Go code. More information on the Go compilers, creating a Go Makefile and getting started with Go can be found here, here, and here respectively. Happy Hacking!

1 comment:

  1. Thank you for your useful post. Besides, both all.bash and make.bash also create each architecture packages.