-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: document how to cross-compile from darwin/amd64 to darwin/arm64 with cgo #44112
Comments
feel free to add a page to the wiki |
I think the general question is how to cross compile with cgo, which boils down to how to cross compile C code. It is not specific to darwin. Basically, it needs a C cross compiler, and set CC and/or CGO_CFLAGS/CGO_LDFLAGS accordingly. Sometimes it is relatively easy, and sometimes it requires a nontrivial amount of work. I don't think we want do document about how to cross compile C. Are there existing documentation about that? Maybe we can link to that, and add the part of setting CC/CGO_CFLAGS/etc. |
I thought it was worth calling out darwin/arm64 as most build systems do not yet support Apple M1 and are on macOS 10.15 or earlier. One of the wiki pages seems fine? Perhaps https://github.com/golang/go/wiki/Darwin? |
This is an attempt to fix a lack of keychain support in darwin/arm64 binaries that have been cross-compiled on other platforms as described in 99designs#758 and hinted at in the linked 99designs/keyring@756c48d Given the keychain support from keyring[1] is provided by cgo, and CGO is disabled by default in cross-compilation, we need to enable that, and deal with dev tooling/libraries. I dug this solution from the Go issues, specifically golang/go#44112 Be warned, I am not familiar with the ins and outs of Go compilation, especially when it comes to cross-compilation of CGO code, but at least in this case, this change allows for a functional cross-compiled binary. I fully expect that attempting to cross-compile darwin/arm64 on anything other than darwin/amd64 (or the opposite way around) is going to end badly. [1] https://github.com/99designs/keyring
This is an attempt to fix a lack of keychain support in darwin/arm64 binaries that have been cross-compiled on other platforms as described in 99designs#758 and hinted at in the linked 99designs/keyring@756c48d Given the keychain support from keyring[1] is provided by cgo, and CGO is disabled by default in cross-compilation, we need to enable that, and deal with dev tooling/libraries. I dug this solution from the Go issues, specifically golang/go#44112 Be warned, I am not familiar with the ins and outs of Go compilation, especially when it comes to cross-compilation of CGO code, but at least in this case, this change allows for a functional cross-compiled binary. I fully expect that attempting to cross-compile darwin/arm64 on anything other than darwin/amd64 (or the opposite way around) is going to end badly. [1] https://github.com/99designs/keyring
I chose MacOSX.sdk but still got unsupported arch errors:
|
Any detailed information that do the same thing in GitHub Action? Since I don't sure the SDK path is the same form the action one. |
Is it still working on or just won't fix??? |
Now in the usual case In the usual case, with a standard Xcode installation, there is no need to set SDK path or anything (unless you're targeting iOS, not macOS). |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
Attempting to use cgo for cross-compiling a Go app for darwin/arm64 from darwin/amd64 is not entirely straightforward for those not familiar with Apple's toolchain when compiling from macOS 10.15 or earlier.
What version of Go are you using (
go version
)?go 1.16rc1
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I was trying to build Skaffold for darwin/arm64. We normally build Skaffold with cgo to take advantage of github.com/rjeczalik/notify's bindings to native file-watching APIs like macOS's FSEvents.
I understood that XCode 12.2 and later support cross-compilation between amd64 and arm64, I thought this would be straightforward. But my attempts failed with odd errors:
It turns out that cross-compiling for arm64 from amd64 requires using a macOS SDK 11.x. As I was building from a machine running macOS 10.15, the default SDK used for compilation was 10.15. It was not clear how to specify an 11.x SDK. From what I found online, it seemed like I should be able to build by specifying
CC='clang --target arm64-apple-macos11
, orCC='clang -isysroot
to point to a macOS 11.x SDK, but these failed in the similar fashion:After much more digging, it turns out the best way is to set the
SDKROOT
environment variable to point to the SDK. And to usexcrun
to find the newest SDK installed:The
--sdk macosx
chooses the MacOSX.sdk, which is normally linked to the latest SDK.The text was updated successfully, but these errors were encountered: