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
cargo new foo 
cd foo 
echo 'fn main() { panic!("{:?}", std::env::var("OPT_LEVEL")); }' > build.rs 
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)