cmd/cgo: compilation failure on Apple silicon when GOHOSTARCH=arm64 and GOARCH=amd64 #43476
Comments
<aside> Thanks to everyone who's put effort into supporting the new Apple processors. I've been amazed at how well things have worked out of the gate for me, and I very much appreciate all your efforts! |
/cc @ianlancetaylor |
I'm curious: what happens if you set |
@bcmills, your suggestion works, and is simpler that setting both of
(Note that it's |
In general, for cross-compilation (even on the same machine), the C compiler needs to be a cross compiler that targets the target platform, not the host platform. Setting CC (or CGO_CFLAGS, etc.) is the right thing to do, as @bcmills mentioned. (Arguably, the default target selection rule for the system's C compiler on Apple Silicon makes it more confusing and annoying.) As it works, is there anything we still need to do here, or we can close this? |
Change https://golang.org/cl/283812 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. (go1.16beta1-42-g3dd5867605)
What operating system and processor architecture are you using (
go env
)?CGO_ENABLED=1 GOARCH=amd64 gotip env
OutputWhat did you do?
I'm running the latest gotip master (go1.16beta1-42-g3dd5867605) built with
GOARCH=arm64
on an Apple Silicon laptop. When building an x86_64 executable withCGO_ENABLED=1 GOARCH=amd64
the compilation fails insrc/runtime/cgo/gcc_darwin_amd64.c
due to Clang's default arm64 target.This failure is limited to when cgo is enabled; I've seen no issues with the default behavior of
CGO_ENABLED=0
.This can be tested by building any executable under the
cmd
directory:What did you expect to see?
Compilation success, with an amd64 executable.
What did you see instead?
A compilation error.
While this may be an uncommon cross-build case, this is necessary to build an executable to run on an Intel Mac from a non-Rosetta2 Apple Mac. I found two potential solutions: manually setting
CGO_CFLAGS
andCGO_LDFLAGS
when runninggotip build
, and a small patch tocmd/go/internal/work
which adds the necessary flags by default.(The linked patch addresses would also impact other installations when using
CC=clang
. I haven't tested any combination other thanGOOS=darwin GOHOSTARCH=arm64
!)It's worth noting that while I encountered this issue on an Apple Silicon laptop using the default
CC=clang
, I've used CGO to cross-build on other platforms before, and did not encounter any similar issue. In particular, this seems to be partially caused by support for cross-compilation support in GCC vs. Clang; whereas a single toolchain GCC is built for a specific architecture (perhaps with 32- vs 64-bit support), a single Clang toolchain can support multiple targets. In these other cases I didn't bat an eye when needing to manually configureCC
,CGO_CFLAGS
, andCGO_LDFLAGS
.While this case is technically cross-building, it felt unexpected that I needed to tweak the behavior here. So this many be nothing more than a bug in my expectations.
The text was updated successfully, but these errors were encountered: