Skip to content

Build scripts receive wrong OPT_LEVEL when manually using -C opt-level #9358

@glandium

Description

@glandium

Problem
When building with RUSTFLAGS="-C opt-level=1" cargo build --release or cargo rustc --release -- -C opt-level=1, while the compilation will happen with opt-level 1 instead of the default 3 for the release profile, the build script will still get OPT_LEVEL set to 3, not 1.

Steps

  1. cargo new foo
  2. cd foo
  3. echo 'fn main() { panic!("{:?}", std::env::var("OPT_LEVEL")); }' > build.rs
  4. RUSTFLAGS="-C opt-level=1" cargo build --release or cargo rustc --release -- -C opt-level=1 output:
  thread 'main' panicked at 'Ok("3")', build.rs:1:13

Output of cargo version:
cargo 1.51.0 (43b129a20 2021-03-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions