Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: support cross compilation with cgo enabled #4714

Closed
minux opened this issue Jan 27, 2013 · 24 comments

Comments

@minux
Copy link
Member

commented Jan 27, 2013

currently cmd/cgo (and cmd/go)  hardcode the executable name of the compiler
to be "gcc", and cmd/go forbids using cgo when it's cross compiling.

We need a proper way to support cross compiling with cgo enabled because
it's infeasible to do native compile sometimes (for example, iOS and low-end
ARM machines)

Another use for this is remote testing, it's quite difficult to find an ARMv5
machine with enough RAM (>=512MiB) for make.bash, but if we support this,
maybe we can build on a PC and remote test on ARM machines, and 256MiB
is more than enough for testing.
@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 28, 2013

Comment 2:

This would also mean I could cross-compile linux-arm-cgo Android child helper binaries
from darwin-amd64 and be able to do proper Android DNS lookups.  Currently, without cgo,
I can't do native DNS so I'm resorting to hacks like using os/exec and parsing the first
line output of "ping -c 1 $HOSTNAME" for my http.Transport's net.Dial function.
@minux

This comment has been minimized.

Copy link
Member Author

commented Jan 29, 2013

Comment 3:

Hi brad, for now you can use a gcc wrapper to enable cgo cross compilation with Android
NDK,
https://golang.org/cl/6454055/
@robpike

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2013

Comment 4:

Labels changed: removed go1.1maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2013

Comment 5:

Issue #5848 has been merged into this issue.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2013

Comment 6:

Labels changed: added go1.2maybe.

@quarnster

This comment has been minimized.

Copy link

commented Aug 1, 2013

Comment 7:

What's needed to resolve this? FWIW I'm successfully cross compiling from darwin amd64
to linux 386 via 'CC=$(CC) CGO_ENABLED=1 GOOS=linux GOARCH=386 go build -x
-ldflags="-extld=$(CC)"' (where $(CC) is "i686-nptl-linux-gnu-gcc" in this instance) and
disabling the "cannot use cgo" error in src/cmd/go/build.go:
diff -r 3d177aef8dd3 src/cmd/go/build.go
--- a/src/cmd/go/build.go   Mon Jul 22 23:02:27 2013 +0400
+++ b/src/cmd/go/build.go   Thu Aug 01 13:34:13 2013 +0200
@@ -1854,9 +1854,9 @@
 )
 func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string, gxxfiles []string) (outGo, outObj []string, err error) {
-   if goos != toolGOOS {
-       return nil, nil, errors.New("cannot use cgo when compiling for a different operating
system")
-   }
+   // if goos != toolGOOS {
+   //  return nil, nil, errors.New("cannot use cgo when compiling for a different
operating system")
+   // }
    cgoCPPFLAGS := stringList(envList("CGO_CPPFLAGS"), p.CgoCPPFLAGS)
    cgoCFLAGS := stringList(envList("CGO_CFLAGS"), p.CgoCFLAGS)
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 4, 2013

Comment 8:

This would be nice but it didn't happen for 1.2.

Labels changed: added go1.3, removed go1.2maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 10:

Labels changed: added release-go1.3.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 11:

Labels changed: removed go1.3.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 12:

Labels changed: added repo-main.

@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Jan 26, 2014

Comment 13:

Started: https://golang.org/cl/57100043
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Feb 6, 2014

Comment 14:

This issue was updated by revision 2dc759d.

LGTM=minux.ma, iant
R=golang-codereviews, minux.ma, iant, rsc, dominik.honnef
CC=golang-codereviews
https://golang.org/cl/57100043
Committer: Ian Lance Taylor 
@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Feb 7, 2014

Comment 15:

As of rev. 6d3bdbd27761, the missing parts of this issue are, as I see them:
1. make.bat/make.rc support for CC_FOR_TARGET.
2. Support for remote testing. Minux tells me that the upcoming NaCL port includes a
mechanism for go test to use a helper binary to execute cross compiled tests. I think
Dave is working on getting that port up to date and submitted.
3. Documentation for setting up various cross compilers.
@gopherbot

This comment has been minimized.

Copy link

commented Feb 7, 2014

Comment 16 by joe@prevoty.com:

As of right now this patch isn't working. I've attempted to setup a cross compile
toolchain on OS X for linux, both on am64 with the following commands from the go tip
repo:
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC_FOR_TARGET=/opt/local/bin/x86_64-elf-gcc
CXX_FOR_TARGET=/opt/local/bin/x86_64-elf-g++ ./make.bash
After building I attempted to build source of a project which imports a library that
uses CGo using the following command:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build
And get the following result:
go build runtime/cgo: cannot use cgo when compiling for a different operating system
@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Feb 8, 2014

Comment 17:

Thank you. It seems I missed a check in the rework of the previous CL. Created
https://golang.org/cl/57210046.
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Feb 14, 2014

Comment 18:

This issue was updated by revision 9b0736f.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/57210046
Committer: Ian Lance Taylor 
@dvyukov

This comment has been minimized.

Copy link
Member

commented Mar 15, 2014

Comment 20:

It's marked as Go1.3. What is the status of this? It blocks race cross builds as well.
@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Mar 15, 2014

Comment 22:

From my viewpoint, the status is as described in #15. The various make.* needs updating,
documentation on cross compiler setups are missing and I haven't done anything about
remote testing.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Apr 3, 2014

Comment 23:

The piece about testing is the -exec flag for 'go build' and 'go test'.
@rsc

This comment has been minimized.

Copy link
Contributor

commented May 9, 2014

Comment 24:

I guess this just needs documentation but I'm not the one to write it. Anyone? If not
we'll just not include any mention in the release. It seems like this is working and
just needs documentation.

Labels changed: added release-go1.3maybe, removed release-go1.3.

@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented May 11, 2014

Comment 25:

Did you have something like https://golang.org/cl/100390043 in mind?
@gopherbot

This comment has been minimized.

Copy link

commented May 11, 2014

Comment 26:

CL https://golang.org/cl/100390043 mentions this issue.
@rsc

This comment has been minimized.

Copy link
Contributor

commented May 20, 2014

Comment 27:

This issue was updated by revision 88d07b2.

LGTM=iant, minux.ma, rsc
R=rsc, iant, r, minux.ma
CC=golang-codereviews
https://golang.org/cl/100390043
Committer: Russ Cox 
@rsc

This comment has been minimized.

Copy link
Contributor

commented May 20, 2014

Comment 28:

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015

@rsc rsc removed the release-go1.3maybe label Apr 14, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
9 participants
You can’t perform that action at this time.