Skip to content

Build scripts are not informed of rustflags or rustc wrapper #9600

Closed
@jonhoo

Description

@jonhoo

Describe the problem you are trying to solve

I recently came across a bug in anyhow whose root cause is that it does "test compile" to see whether it can make use of some nightly features, but that test compile does not take into account Cargo configuration like [build] rustflags = . Looking through the environment variables Cargo sets for build scripts it doesn't appear that things like the rustc wrapper or the final rustflags is actually passed to the build script. Specifically, this can be seen by running

cargo new cargo-build-no-flags
cd cargo-build-no-flags
mkdir .cargo
cat >.cargo/config.toml <<EOF
[build]
rustflags = ["--cfg=wrapped"]
rustc-wrapper = "$PWD/.cargo/wrap.sh"
EOF
cat > .cargo/wrap.sh <<'EOF'
#!/bin/sh
rustc=$1
shift
exec "$rustc" "$@"
EOF
chmod +x .cargo/wrap.sh
cat > build.rs <<EOF
fn main() {
    for var in std::env::vars() {
        eprintln!("{:?}", var);
    }
}
EOF
cargo check -vv 2>&1 | grep '^\[cargo-build-no-flags' | tee build-output.log

Then, observe that "wrap", which appears in both build.rustflags and build.rustc-wrapper, does not appear in the build output:

$ grep wrap build-output.log
$

Describe the solution you'd like

The rust compiler is given to the build script in the form of the RUSTC environment variable, but that alone is not sufficient for a build script that wants to inspect (or use) the target Rust compilation context. The rustc configuration (the wrapper and RUSTFLAGS) should be passed to the build script as well, likely through two new environment variables:

  • CARGO_RUSTC_WRAPPER
  • CARGO_RUSTFLAGS

Note that these do not include _BUILD to avoid overlap with the environment variables Cargo reads.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions