Skip to content

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

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.

Activity

  1. added
    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
    on Jun 18, 2021
  2. added a commit that references this issue on Jun 18, 2021
    6ee606b
  3. cuviper commented on Jun 18, 2021

    @cuviper
    Member

    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

    So those would be the target-specific flags, right? (i.e. matching the TARGET that cargo also sets for the build script.)

    This would be a real boon to autocfg! (cc cuviper/autocfg#15 cuviper/autocfg#34)

  4. jonhoo commented on Jun 18, 2021

    @jonhoo
    ContributorAuthor

    Yes, my thinking was that they'd be set based on how Cargo decides to build the build script itself. See also the implementation of this in #9601, which is really quite simple.

  5. cuviper commented on Jun 18, 2021

    @cuviper
    Member

    Yes, my thinking was that they'd be set based on how Cargo decides to build the build script itself.

    I don't think that's quite the same thing, since the build script runs on the host. But AFAICT the call you added does use the current bcx.target_data, the same that TARGET is set from, so it should be what I want.

  6. added a commit that references this issue on Jul 20, 2021
    adde602
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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