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

cgo: cross-compilation #1020

Open
ianthehat opened this issue Nov 14, 2017 · 10 comments
Open

cgo: cross-compilation #1020

ianthehat opened this issue Nov 14, 2017 · 10 comments

Comments

@ianthehat
Copy link
Contributor

This is currently not possible, as we can only build the standard library in "pure" mode if we are cross compiling.
In theory there is no reason to limit this quite so much, but we need a way to pick the pure version of the standard library when cross compiling without having to compile all libraries in pure mode.

@steeve
Copy link
Contributor

steeve commented Nov 14, 2017

Would it be possible to specify the toolchain itself?

The reason is I worry that relying on things like CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS, CGO_LDFLAGS or CC/CXX env vars might be a little overlapping with Bazel's way of doing things, which in my view is far more powerful.

@steeve
Copy link
Contributor

steeve commented Jan 6, 2018

hey @ianthehat, any good news for the new year ? :)

@ianthehat
Copy link
Contributor Author

Not even looked at this yet I am afraid

@jayconrod
Copy link
Contributor

(Re-triaging old issues)

This should work, at least theoretically. You can target a platform with --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo (note the _cgo suffix). An appropriate C/C++ toolchain must be configured.

Leaving this issue open because we don't have adequate test coverage or documentation yet.

@jayconrod jayconrod changed the title Cross compiling with cgo cgo: cross-compilation Sep 13, 2019
@devastating
Copy link

devastating commented Nov 27, 2019

(Re-triaging old issues)

This should work, at least theoretically. You can target a platform with --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo (note the _cgo suffix). An appropriate C/C++ toolchain must be configured.

Leaving this issue open because we don't have adequate test coverage or documentation yet.

Hello @jayconrod ,
Is this theoretically working for Linux only? I've tried it with "--platforms @io_bazel_rules_go//go/toolchain:darwin_amd64_cgo", and the binary built still shows "Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub"

These are the flags I was using:
CC=/usr/bin/gcc CXX=/usr/bin/g++ --features=pure --stamp --platforms @io_bazel_rules_go//go/toolchain:darwin_amd64_cgo

We are trying to cross-compile for darwin on Linux; my experiment is done on darwin and the result is the same.

Thanks in advance!

@jayconrod
Copy link
Contributor

@devastating The --features=pure flag disables cgo. Try without that.

@devastating
Copy link

devastating commented Nov 28, 2019

@devastating The --features=pure flag disables cgo. Try without that.

@jayconrod It works on MacOS - however, when I tried to really cross-compile on Linux, I got the error:

ERROR: While resolving toolchains for target @io_bazel_rules_go//:cgo_context_data: no matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type

Did the "_cgo" build feature not support cross-compilation? We are using bazel 1.1.0 release.
Thank you in advance!

@devastating
Copy link

I guessed I will have to wait until #1642 is close..

@ChuntaoLu
Copy link

Hey, this issue has been open for a while, is cross compiling cgo still not doable today?

@steeve
Copy link
Contributor

steeve commented May 2, 2021

It is if you supply your own C toolchain.

Alternatively, you can try this approach: https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho

@ianthehat ianthehat removed their assignment Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants