rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.86k stars 2.43k forks source link

support generic target tables and env variables #14264

Open jameshilliard opened 4 months ago

jameshilliard commented 4 months ago

Problem

When building for targets from a meta build system like buildroot it is preferable to be able to unconditionally set target config/env variables without having to care about the target triple as we use target specific toolchains that will only support a single target architecture typically.

Proposed Solution

https://github.com/rust-lang/cargo/pull/9603

epage commented 4 months ago

The linked PR doesn't provide much details on the proposed solution besides a code dump. Could you provide more details on the problem, what the user-facing solution would be, and how that helps?

jameshilliard commented 4 months ago

Could you provide more details on the problem, what the user-facing solution would be, and how that helps?

Currently to say set a target linker we have to generate an env variable like CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER, it would be nice to be able to set the target linker for all target artifact builds using a non-target-triple specific variable like CARGO_TARGET_LINKER.

epage commented 4 months ago

In TOML terms,

[target]
linker = ...
[target.<triple>]
linker = ...

translates to

{
    "target": {
        "linker": "...",
        "<triple>": {
            "linker": "...",
        }
    }
}

Personally, I would worry about putting target-config fields at the same level as <triple> / <cfg>

Some options

Huh, we don't specify what happens if multiple targets apply. In moving this forward, we should probably verify what is done in that case and be able to talk whether a design for solving this problem works with that solution or not.

jameshilliard commented 4 months ago

Huh, we don't specify what happens if multiple targets apply. In moving this forward, we should probably verify what is done in that case and be able to talk whether a design for solving this problem works with that solution or not.

So in my pull request I actually wrote a test to cover this specific scenario. The way I implemented the logic was to have any target triple specific configs take precedence over generic target configs if they exist.