Skip to content

CARGO_TARGET_<triple>_LINKER environment variable is ignored during build #8285

@smacintyre

Description

@smacintyre

Problem

Setting the CARGO_TARGET_<triple>_LINKER environment variable has no affect on cargo build.

I am targeting x86_64-unknown-linux-musl and I need to be able to build on both macOS (for development) and Linux (for CI). The linker is named differently on the two platforms: x86_64-linux-musl-gcc on macOS and musl-gcc on Linux (which is the what rustc expects by default). I would like to be able to the CARGO_TARGET__LINKER environment variable to override the default on macOS only.

Steps (macOS)

  1. rustup default stable (fails for nightly too)
  2. rustup target add x86_64-unknown-linux-musl
  3. brew install filosottile/musl-cross/musl-cross
  4. cargo new envtest
  5. cd envtest
  6. export CARGO_TARGET_x86_64_unknown_linux_musl_LINKER=x86_64-linux-musl-gcc
  7. cargo build --target x86_64-unknown-linux-musl

Result: error: linking with ``cc`` failed: exit code: 1

Expected Result: a binary linked with x86_64-linux-musl-gcc

Suggested Fix

Rather than just being able to specify target specific configurations in .cargo/config, it would be nice to be able to specify configurations by target and host combinations.

For example I want this rule to only apply when building for the musl target on macOS:

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

If narrow this rule by host would be amazing. Something like:

[target.x86_64-unknown-linux-musl.cfg(host_os = "macos")]
linker = "x86_64-linux-musl-gcc"

That said, being able to use the CARGO_TARGET_<triple>_LINKER environment variable is more than enough for my needs. Thank you.

Notes

Output of cargo version: cargo 1.43.0 (2cbe904 2020-05-03)

Release channels tested:

  • stable-x86_64-apple-darwin unchanged - rustc 1.43.1 (8d69840ab 2020-05-04)
  • nightly-x86_64-apple-darwin unchanged - rustc 1.45.0-nightly (a74d1862d 2020-05-14)

OSes tested on:

  • macOS Catalina 10.15.5 Beta (19F83c)
  • debian buster -- from the official rust:latest docker image

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-environment-variablesArea: environment variablesC-bugCategory: bugE-easyExperience: Easy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions