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

Cross compiling Go 1.21 from Linux to Darwin fails due to missing resolv.h #104

Closed
evanj opened this issue Jul 19, 2023 · 2 comments
Closed

Comments

@evanj
Copy link

evanj commented Jul 19, 2023

Using hermetic_cc_toolchain to cross-compile a Go application for Darwin/Mac OS X using Go 1.21 fails with the following error:

ERROR: /home/bits/.cache/bazel/_bazel_bits/031ab286c04183ba9beae60237d64a79/external/io_bazel_rules_go/BUILD.bazel:42:7: GoStdlib external/io_bazel_rules_go/stdlib_/pkg failed: (Exit 1): builder failed: error executing command (from target @io_bazel_rules_go//:stdlib) bazel-out/k8-opt-exec-2B5CBBC6-ST-b33d65c724e6/bin/external/go_sdk/builder_reset/builder stdlib -sdk external/go_sdk -installsuffix darwin_arm64 -out ... (remaining 7 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
# net
bazel-out/k8-fastbuild-ST-b33d65c724e6/bin/external/io_bazel_rules_go/stdlib_/src/net/cgo_unix_cgo_darwin.go:10:10: fatal error: 'resolv.h' file not found
#include <resolv.h>
         ^~~~~~~~~~
1 error generated.
stdlib: error running subcommand external/go_sdk/bin/go: exit status 1
Target //hello:hello failed to build
Use --verbose_failures to see the command lines of failed build steps.

It appears that Go 1.21 added #include <resolv.h> to the Darwin build in the net package. This header does not appear to be included in the Zig sandbox, and looking really quickly: it may be missing from the upstream Zig project also, although it does exist for musl and glibc. Sadly I don't have enough understanding of how this cross compilation magic works to understand how to fix this, but I suspect Zig may need to add this header file?

A workaround is to set pure="on" on the go_binary rule, which causes the standard library to be compiled with CGO_ENABLED=0, which avoids the problematic include.

To reproduce, see my example project: https://github.com/evanj/rulesgotest

The following works and produces a correct binary with Go 1.20.6, but fails with Go 1.21rc3:

$ bazel build --incompatible_enable_cc_toolchain_resolution --platforms @zig_sdk//platform:darwin_arm64 --extra_toolchains @zig_sdk//toolchain:darwin_arm64 //hello

ERROR: /home/bits/.cache/bazel/_bazel_bits/031ab286c04183ba9beae60237d64a79/external/io_bazel_rules_go/BUILD.bazel:42:7: GoStdlib external/io_bazel_rules_go/stdlib_/pkg failed: (Exit 1): builder failed: error executing command (from target @io_bazel_rules_go//:stdlib) bazel-out/k8-opt-exec-2B5CBBC6-ST-b33d65c724e6/bin/external/go_sdk/builder_reset/builder stdlib -sdk external/go_sdk -installsuffix darwin_arm64 -out ... (remaining 7 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
# net
bazel-out/k8-fastbuild-ST-b33d65c724e6/bin/external/io_bazel_rules_go/stdlib_/src/net/cgo_unix_cgo_darwin.go:10:10: fatal error: 'resolv.h' file not found
#include <resolv.h>
         ^~~~~~~~~~
1 error generated.
stdlib: error running subcommand external/go_sdk/bin/go: exit status 1
Target //hello:hello failed to build
Use --verbose_failures to see the command lines of failed build steps.
@motiejus
Copy link
Collaborator

This header does not appear to be included in the Zig sandbox, and looking really quickly: it may be missing from the upstream Zig project also,

Upstream zig includes only what is necessary to build zig itself for MacOS. resolv.h is not in scope, so it is not included. zig cc accepts -F, which can be used for all those extra headers and sweet tbd files.

Currently it is just not implemented in hermetic_cc_toolchain. Also see #10 and https://github.com/uber/hermetic_cc_toolchain#osx-sysroot

There is a branch which I started with, but did not finish and am unlikely to continue: https://github.com/motiejus/hermetic_cc_toolchain/tree/macos ; whoever has interest in this work are welcome to pick it up and submit a PR.

@evanj
Copy link
Author

evanj commented Jul 20, 2023

Thanks for the context! I figured it was something like that. I now see the README.md does in fact document this, and I just didn't find it. I may try to make an edit that mentions the Go standard library, resolv.h and CoreFoundation, since those are the errors that show up when attempting to use this, in the hopes that search results may lead future people to the fact that this is known to not work. Thanks!

@ghost ghost mentioned this issue Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants