bazel-contrib / bazel-contrib/toolchains_llvm

Cross compiling for Darwin selects incorrect linker flags

Open
#386 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
371
Forks
283
Avg merge
1d 55m
Merged PRs (30d)
25

Description

When configuring to cross compile `darwin-x86 -> darwin-aarch64` or `darwin-aarch64 -> darwin-x86`, `toolchains_llvm` seems to be selecting some linker flags that prevent compilation. I'm working to support cross compilation on Sorbet (here's the PR https://github.com/sorbet/sorbet/pull/8162).

First, the [documentation](https://github.com/bazel-contrib/toolchains_llvm/tree/master?tab=readme-ov-file#cross-compilation) says that when cross compiling it will try to use `libstdc++` instead of `libc++`. However, that leads to an error telling the developer to switch to using `libc++`. There's some more context in this [forum answer](https://forums.developer.apple.com/forums/thread/113746) where they say `libstdc++` is deprecated for MacOS.

I was able to move further by setting `stdlib=libc++`, but then on [this line](https://github.com/bazel-contrib/toolchains_llvm/blob/1cd9e36e498983f061aa239e18043cfd26c7ac28/toolchain/cc_toolchain_config.bzl#L149) we enter the `else` statement because `is_xcompile` is true. Linking will then fail saying that `hash-style`, `build-id` and `z` are unknown arguments to the linker.

After patching that part to not enter the `else` statement, it then fails further down because it [always sets stdlib to be stdc++ when cross compiling](https://github.com/bazel-contrib/toolchains_llvm/blob/1cd9e36e498983f061aa239e18043cfd26c7ac28/toolchain/cc_toolchain_config.bzl#L187), which adds another incompatible linker flag.

After patching that spot, then I was able to cross compile `darwin-arm64 -> darwin-x86`. We put our patches in https://github.com/sorbet/bazel-toolchain/pull/10 because I don't know what the general solution would be, but it does get Sorbet to build and produces a valid x86 binary.

### Note

If you check the [PR on Sorbet](https://github.com/sorbet/sorbet/pull/8162), you'll see that I added the MacOS SDK as a local repository to be able to give the `llvm_toolchain` definition a sysroot, enabling me to cross compile.

I'm not sure if that's the intended approach. It feels a bit weird given that it's the same SDK and I would assume the regular toolchain already has that configured somewhere. Please, let me know if I'm setting up something wrong.

It would be nice to add examples of how to configure cross compilation on Darwin to the documentation. If the approach is considered to be correct, then we can contribute some docs.

Contributor guide

Open the contributing guide

Research direction

Start in toolchain/cc_toolchain_config.bzl at the referenced lines 149 and 187, then compare the cross-compilation documentation with the patches in sorbet/bazel-toolchain#10. Verify the Darwin x86-to-arm64 and arm64-to-x86 configurations and their sysroot setup. Done means both directions link without incompatible linker flags; add Darwin cross-compilation documentation if that approach is confirmed.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.