Set cargo linker for cross compilation only if necessary

Viewed 953

According to various guides on cross-compiling Rust I need to set something like this in .cargo/config:

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

In my case I am cross compiling for Linux on Mac. This is fine, but I'd also like to be able to build the code natively on Linux (i.e. not cross-compiling). In that case surely the target is still x86_64-unknown-linux-musl, but I don't want to override linker then.

I guess you can probably do this using a custom build.rs, but is there any way to do this from .cargo/config? I thought you'd be able to do something like this:

[target.'cfg(all(host_os = "macos", target_os = "linux"))']
linker = "x86_64-linux-musl-gcc"

But there is no host_os.

1 Answers

I'm guessing that you've put .cargo/config inside of your Cargo workspace?

If this the case, you should consider moving host-specific configuration options (such as target.{target}.linker) into your user-specific ~/.cargo/config instead. That way, the linker is not tied to the workspace configuration, but instead to the user: different users on different hosts could have different linkers configured for different targets.

Related