Cross compiling Go

732 views
Skip to first unread message

vendion

unread,
Dec 24, 2009, 9:56:05 PM12/24/09
to golang-nuts
Ok I couldn't see if this question was already asked before but I was
wondering it it is possible to cross compile Go computer architecture
wise? This way I can develop on my desktop which is a amd64 system
but have the final version for my phone which is a ARM system. Would
this be as simple as editing the .bashrc file and telling it my GOARCH
is arm instead of amd64 and running make all for Go?

Rob 'Commander' Pike

unread,
Dec 24, 2009, 10:58:07 PM12/24/09
to vendion, golang-nuts

GOARCH and possibly GOOS, yes. With 6g and friends you're always
cross-compiling, in effect.

-rob

Jack Palevich

unread,
Dec 25, 2009, 1:18:07 AM12/25/09
to golang-nuts
Two things to keep in mind:

As Rob alluded to, your phone has to have a supported OS. The phone
has to support Linux, I think. So yes for Android and possibly Mameo
phones, probably not for Windows Mobile, RIM, Palm, or Apple phones.

The other thing is that you will probably have trouble with floating
point. The current versions of the ARM-architecture Go language
compiler, 5g, does not support the "softfp" EABI. It's being worked
on, but isn't there yet.

THis means that if your Go program uses floating point instructions,
it will compile, but you'll get a runtime error when your code
executes on ARM.

The reason is that Go's compilers are based on the Plan 9 C compilers,
which were written back when the ARM architecture supported a
different floating point standard than it does now. The ARM Go
compiler is still emitting code for the old no-longer-supported
floating point architecture.

Kai Backman

unread,
Dec 25, 2009, 12:55:01 PM12/25/09
to Jack Palevich, golang-nuts
On Thu, Dec 24, 2009 at 10:18 PM, Jack Palevich <jack.p...@gmail.com> wrote:
> The other thing is that you will probably have trouble with floating
> point. The current versions of the ARM-architecture Go language
> compiler, 5g, does not support the "softfp" EABI. It's being worked
> on, but isn't there yet.

As a temporary stopgap there is now code in the newest version that
skips over most (all?) float instructions in the instruction stream.
This lets you use packages like math that have static initializers
utilizing float that used to crash even if you didn't call any
explicit float instructions. The biggest benefit is probably that fmt
is now usable.

Kai

vendion

unread,
Dec 28, 2009, 11:16:02 AM12/28/09
to golang-nuts
On Dec 25, 1:18 am, Jack Palevich <jack.palev...@gmail.com> wrote:
> Two things to keep in mind:
>
> As Rob alluded to, your phone has to have a supported OS. The phone
> has to support Linux, I think. So yes for Android and possibly Mameo
> phones, probably not for Windows Mobile, RIM, Palm, or Apple phones.
>

As long as Go doesn't have a problem running with a 2.6.29-rc3 kernel
then I have the OS part covered, I have a Neo FreeRunner running the
SHR distro.

This still doesn't exactly answer my question though, I know that with
Go you are always cross compiling but I want to compile for x86_64 and
ARM arch on the same machine if this is possible with Go. If this is
possible I'm sure that means switching the settings in my .bashrc and
rebuilding Go but this is fine with me, unless the GOARCH field lets
you have multiple architectures specified.

Ian Lance Taylor

unread,
Dec 28, 2009, 1:35:49 PM12/28/09
to vendion, golang-nuts
vendion <ven...@gmail.com> writes:

> This still doesn't exactly answer my question though, I know that with
> Go you are always cross compiling but I want to compile for x86_64 and
> ARM arch on the same machine if this is possible with Go. If this is
> possible I'm sure that means switching the settings in my .bashrc and
> rebuilding Go but this is fine with me, unless the GOARCH field lets
> you have multiple architectures specified.

You don't have to rebuild Go, but you do have to set GOARCH to the
appropriate value before invoking 6l or 5l. So you will most likely
want to use a wrapper script.

Ian

vendion

unread,
Jan 5, 2010, 10:49:55 PM1/5/10
to golang-nuts

Two questions:

1) If I don't have to rebuild Go then how do I get 5l? My GOARCH is
currently set to x86_64 and I only have 6l and friends (makes sense
though 6l is for 64 bit)

2) What is meant by a wrapper script?

Rob 'Commander' Pike

unread,
Jan 5, 2010, 11:49:47 PM1/5/10
to vendion, golang-nuts

It's not exactly a rebuild, just a build. You can change the value of
GOARCH and build the compilers and packages for arm. This will not
harm your 6g or 8g installation, since the command names differ (5g
vs. 6g) and the package files go into directories identified by
architecture and operating system. You can have a world that works
with both.

There's work underway so you don't have to specify GOARCH for 5l, but
for now, the linker you use must be the correct one for the value of
GOARCH.

-rob

vendion

unread,
Jan 7, 2010, 11:42:16 AM1/7/10
to golang-nuts
On Jan 5, 11:49 pm, "Rob 'Commander' Pike" <r...@google.com> wrote:
> On Jan 6, 2010, at 2:49 PM, vendion wrote:
>
>
>
> > Ian Lance Taylor wrote:
> >> vendion <vend...@gmail.com> writes:
>
> >>> This still doesn't exactly answer my question though, I know that  
> >>> with
> >>>Goyou are alwayscrosscompilingbut I want to compile for x86_64  
> >>> and
> >>> ARM arch on the same machine if this is possible withGo.  If this  

> >>> is
> >>> possible I'm sure that means switching the settings in my .bashrc  
> >>> and
> >>> rebuildingGobut this is fine with me, unless the GOARCH field lets

> >>> you have multiple architectures specified.
>
> >> You don't have to rebuildGo, but you do have to set GOARCH to the

> >> appropriate value before invoking 6l or 5l.  So you will most likely
> >> want to use a wrapper script.
>
> >> Ian
>
> > Two questions:
>
> > 1) If I don't have to rebuildGothen how do I get 5l?  My GOARCH is

> > currently set to x86_64 and I only have 6l and friends (makes sense
> > though 6l is for 64 bit)
>
> > 2) What is meant by a wrapper script?
>
> It's not exactly a rebuild, just a build.  You can change the value of  
> GOARCH and build the compilers and packages for arm.  This will not  
> harm your 6g or 8g installation, since the command names differ (5g  
> vs. 6g) and the package filesgointo directories identified by  

> architecture and operating system.  You can have a world that works  
> with both.
>
> There's work underway so you don't have to specify GOARCH for 5l, but  
> for now, the linker you use must be the correct one for the value of  
> GOARCH.
>
> -rob

Well I think I might have ran into a problem, when building the new
release so I would also have the compiler for ARM and I got this error
during the make all

%%%% making libcgo %%%%

make[1]: Entering directory `/home/vendion/go/src/libcgo'
gcc -O2 -fPIC -o linux_arm.o -c linux_arm.c
make[1]: *** No rule to make target `arm.o', needed by `libcgo.so'.
Stop.
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory `/home/vendion/go/src/libcgo'
make: *** [build] Error 1

Running make for amd64 went fine with no problems though.

Mathieu Lonjaret

unread,
Jan 7, 2010, 1:19:29 PM1/7/10
to golang-nuts
> Well I think I might have ran into a problem, when building the new
> release so I would also have the compiler for ARM and I got this error
> during the make all
>
> %%%% making libcgo %%%%
>
> make[1]: Entering directory `/home/vendion/go/src/libcgo'
> gcc -O2 -fPIC -o linux_arm.o -c linux_arm.c

I ran into that problem as well but running "make-arm.bash" instead as
rsc suggested in another thread worked.
Well, everything built, but I get "Illegal instruction" whatever I try
to run so it's not much better...

Joseph Stewart

unread,
Jan 7, 2010, 4:11:38 PM1/7/10
to golang-nuts
Correct me if I'm wrong, but isn't this a symptom of lacking the floating-point functions?

Can I volunteer to help on this? I need a mentor to get me started, but I can lend a hand.

-joe

Mathieu Lonjaret

unread,
Jan 7, 2010, 5:18:53 PM1/7/10
to Joseph Stewart, golang-nuts

Well, I'm getting that whatever small example I try, without any (that
I know of) float involved. Like this:

package main

func main() {
var d int = 3
d++
}

Joseph Stewart

unread,
Jan 7, 2010, 5:41:52 PM1/7/10
to golang-nuts
Good point. If I can get to it, I'll see if I can figure out how to dump the symbols in the .5 file to see if any obvious FP stuff is there, but if it's in the stdlib, we're out of luck for a while.

Perhaps someone here who's successfully running code on the ARM can speak up and tell us about their environment.

Regards,

-joe

Kai Backman

unread,
Jan 13, 2010, 4:31:49 PM1/13/10
to Mathieu Lonjaret, Joseph Stewart, golang-nuts
This sounds like you are running on an arm architecture pre v6. Set
GOARM=5 and run make-arm to install a version that uses ARMv5
instructions instead. If this doesn't help can you please find the
offending instruction in a debugger, the above example should most
certainly work.

Kai

Mathieu Lonjaret

unread,
Jan 13, 2010, 6:22:35 PM1/13/10
to Kai Backman, golang-nuts
On Wed, Jan 13, 2010 at 10:31 PM, Kai Backman <kai.b...@gmail.com> wrote:
> This sounds like you are running on an arm architecture pre v6. Set
> GOARM=5 and run make-arm to install a version that uses ARMv5
> instructions instead. If this doesn't help can you please find the
> offending instruction in a debugger, the above example should most
> certainly work.
>
>  Kai

That did it for me, now the code runs on the machine I'm building it on.
Next step is to try and have it running on the n900 but I think I'm
missing some deps there, I'll report on that later.
Kudos to you, Kai! :-)

Cheers,
Mathieu

Joseph Stewart

unread,
Jan 13, 2010, 7:18:04 PM1/13/10
to golang-nuts
I second that... it works like a charm on my ARM targets now. It was interesting to follow the path of where GOARM went...

-joe
 
Cheers,
Mathieu

Kai Backman

unread,
Jan 14, 2010, 12:01:34 AM1/14/10
to Joseph Stewart, golang-nuts
Cool, I'm happy it fixed it. I'm contemplating making GOARM=5 default
given that there is a larger body of people complaining of not being
able to compile vs. the higher performance of the LDREX and STREX
versions.

Kai

Mathieu Lonjaret

unread,
Jan 26, 2010, 5:06:56 PM1/26/10
to Kai Backman, Joseph Stewart, golang-nuts
Fyi, I've just found out golang is now packaged for the n900 in the
extras-devel repo for it. I've just tried hello world on it, works
like a charm. :)
Thanks to Kees Jongenburger for that.

Cheers,
Mathieu

Kai Backman

unread,
Jan 27, 2010, 2:55:21 AM1/27/10
to Mathieu Lonjaret, Joseph Stewart, golang-nuts
Cool! Thanks for the update. :-)

Kai

keesj

unread,
Feb 8, 2010, 3:21:43 PM2/8/10
to golang-nuts
Hi,

On Jan 26, 11:06 pm, Mathieu Lonjaret <mathieu.lonja...@gmail.com>
wrote:

Glad somebody noticed :p

It took a few patches to the makefiles to make it compile under
scratchbox so if you have any problem/improvements just ask.

Greetings

Reply all
Reply to author
Forward
0 new messages