Cross Compliation with Go via ENV vars?

292 views
Skip to first unread message

macrae...@gmail.com

unread,
Aug 30, 2017, 2:43:32 PM8/30/17
to bazel-discuss
Hi,

A few months ago when I was first getting started with Bazel I tried to sort out how to pass the GOOS and GOARCH to go_binary rules to get linux builds on MacOS. From what I read at the time I think it turned out not to really be feasible which is part of why we ended up setting up a docker container for building all our services. (it wasn't the only reason but it was a reason)

Now, I'm trying to get go to build a standalone binary so I can put it in a scratch docker image, and again it requires an ENV var rather than a compiler flag (CGO_ENABLED=0). Is there an elegant way to do this? I suppose I can pass it in my bazel invocation with --action_env but that seems heavy handed for something that is not actually being configured at invocation time.

Thanks!

-MacRae

macrae...@gmail.com

unread,
Aug 30, 2017, 2:55:59 PM8/30/17
to bazel-discuss, macrae...@gmail.com
I tried to pass it in with --action_env but that's not passed into the go_binary command.

macrae...@gmail.com

unread,
Aug 30, 2017, 3:52:42 PM8/30/17
to bazel-discuss, macrae...@gmail.com
Looks like per https://github.com/bazelbuild/rules_go/issues/161 this can be accomplished (on linux) purely with linker options so I'm good. Here's an example of ones that worked for me: https://github.com/kubernetes/kubernetes/blob/v1.7.0-alpha.4/cmd/kubeadm/BUILD#L11-L22

And for completeness, here's the bug tracking doing cross compilation correctly in go bazel: https://github.com/bazelbuild/rules_go/issues/70

Marcel Hlopko

unread,
Sep 1, 2017, 3:49:16 AM9/1/17
to macrae...@gmail.com, bazel-discuss, jayc...@google.com
Glad you found a solution. Adding +Jay Conrod just so he knows.

On Wed, Aug 30, 2017 at 9:52 PM <macrae...@gmail.com> wrote:
Looks like per https://github.com/bazelbuild/rules_go/issues/161 this can be accomplished (on linux) purely with linker options so I'm good. Here's an example of ones that worked for me: https://github.com/kubernetes/kubernetes/blob/v1.7.0-alpha.4/cmd/kubeadm/BUILD#L11-L22

And for completeness, here's the bug tracking doing cross compilation correctly in go bazel: https://github.com/bazelbuild/rules_go/issues/70

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/8a83c728-cdee-43e4-8f42-36f9f83bceaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
-- 
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Geschäftsführer: Paul Manicle, Halimah DeLaine Prado | Registergericht und -nummer: Hamburg, HRB 86891

Jay Conrod

unread,
Sep 1, 2017, 10:57:41 AM9/1/17
to Marcel Hlopko, macrae...@gmail.com, bazel-discuss, Ian Cottrell
+Ian Cottrell

Ian has been working on cross compilation with the new Bazel toolchain functionality, and this is mostly working, but we haven't tagged a release with the new functionality yet.

Also, it's now possible to produce static binaries using the "static" output group of go_binary. You can build from the command line with --output_groups=static if you're building the binary directly, or you can use the output_group attribute of filegroup to consume the static binary in a rule.

There are some issues linking static binaries on Mac, even when cross compiling. See my comment on #70. That also gives an example of building a docker image containing a static binary.

On Fri, Sep 1, 2017 at 3:49 AM, Marcel Hlopko <hlo...@google.com> wrote:
Glad you found a solution. Adding +Jay Conrod just so he knows.
On Wed, Aug 30, 2017 at 9:52 PM <macrae...@gmail.com> wrote:
Looks like per https://github.com/bazelbuild/rules_go/issues/161 this can be accomplished (on linux) purely with linker options so I'm good. Here's an example of ones that worked for me: https://github.com/kubernetes/kubernetes/blob/v1.7.0-alpha.4/cmd/kubeadm/BUILD#L11-L22

And for completeness, here's the bug tracking doing cross compilation correctly in go bazel: https://github.com/bazelbuild/rules_go/issues/70

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages