Support for macOS weak linking

Hi all! We're looking for a way to specify weak_framework as a linker argument to specify which frameworks to weakly link for macOS targets. This would allow us to more easily cross-compile for macOS from other host platforms.

I asked about it on the Rust Programming Language Forum at How to use weak linking on macOS (link with `weak_framework`)? - help - The Rust Programming Language Forum a little while ago which contains some additional context and some alternatives we've tried. I didn't receive any replies there, so I thought it might be useful to raise it here as a feature request and ask how we might be able to proceed with this.

1 Like

Likely this simply isn't supported today? If you want to add support, this seems like a small and reasonable change so likely you can just file a PR. Does LLVM support this?

This has nothing to do with LLVM. It is a linker argument.

1 Like

I guess we need to add a link kind which can't be unstable. So it'd probably have to go through RFC or MCP

1 Like

I believe there are a couple of places you can currently specify this:

  1. If you happen to be building a cdylib, you can do this in build.rs:
if cfg!(target_os = "macos") {
    println!("cargo:rustc-cdylib-link-arg=-weak_framework");
    println!("cargo:rustc-cdylib-link-arg=Metal");
}
  1. In all cases, you can add to .cargo/config.toml:
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-args=-weak_framework Metal"]

The latter will only work if your .cargo directory is placed correctly for your build (either system-wide, or in a (transitive) parent directory) - see Configuration - The Cargo Book

1 Like

(1) could be interesting, we aren't currently building a cdylib, but I guess it's possible that we could create a separate cdylib crate that is empty except for that build.rs (at least as a temporary workaround).

(2) wouldn't work well for us because we can't expect everyone depending on our crate to modify their global .cargo/config.toml.