Repository navigation
Conversation
Cargo always sets OPT_LEVEL in the environment of a build script, and cc-rs fails without it: "Environment variable OPT_LEVEL not defined". The rule set it only when the toolchain's rustc flags held `-Copt-level=` with a digit, so build scripts that use cc-rs failed under a toolchain without such a flag, which is the default of `system_rust_toolchain`, and under `-Copt-level=s` or `z`. The shim works around this with an OPT_LEVEL in the fixups of each such crate. Take the last `-Copt-level=` of the rustc flags, `s` and `z` included, and rustc's default `0` without one. An OPT_LEVEL in the target's `env` still takes precedence.
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D134832191. (Because this pull request was imported automatically, there will not be any future comments.) |
dtolnay
approved these changes
Nov 27, 2026
meta-codesync Bot
pushed a commit
to meta/buck2-prelude
that referenced
this pull request
Nov 27, 2026
Summary:
Cargo always sets `OPT_LEVEL` (`0` to `3`, `s` or `z`) in the environment of a build script. cc-rs requires it: `cc::Build::compile` fails with `Environment variable OPT_LEVEL not defined` when it is missing (cc 1.2.52 and 1.4.x).
`buildscript_run` set `OPT_LEVEL` only when the toolchain's rustc flags contained `-Copt-level=` followed by a digit. So every build script that uses cc-rs (`ring`, `zstd-sys`, `libz-sys`, `psm`, …) fails:
- under a toolchain without such a flag, which includes the default `system_rust_toolchain` (`rustc_flags = []`);
- under `-Copt-level=s` or `-Copt-level=z`, which are excluded by the digit check.
The shim works around this crate by crate: the fixups of `psm`, `stacker` and `aws-lc-fips-sys` set `OPT_LEVEL = "3"`.
This PR takes the last `-Copt-level=` of the rustc flags, `s` and `z` included, and uses rustc's default `0` when there is none. An `OPT_LEVEL` in the target's `env` still takes precedence, as in those fixups.
## Affected version
origin/main 00c2ee4f74.
## Reproduction
A build script that compiles one C file with cc-rs 1.2.52, run through `buildscript_run` with the prelude's demo toolchains:
```rust
fn main() {
cc::Build::new().file("foo.c").compile("foo");
}
```
## Expected
The build script runs, as under Cargo.
## Actual
```
OPT_LEVEL = None
error occurred in cc-rs: Environment variable OPT_LEVEL not defined.
```
X-link: meta/buck2#1669
Reviewed By: dtolnay
Differential Revision: D134832191
fbshipit-source-id: e0c3f9089c49ec8615b58b85ca22992a5cea88e7
Contributor
|
This pull request has been merged in a97075f. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cargo always sets
OPT_LEVEL(0to3,sorz) in the environment of a build script. cc-rs requires it:cc::Build::compilefails withEnvironment variable OPT_LEVEL not definedwhen it is missing (cc 1.2.52 and 1.4.x).buildscript_runsetOPT_LEVELonly when the toolchain's rustc flags contained-Copt-level=followed by a digit. So every build script that uses cc-rs (ring,zstd-sys,libz-sys,psm, …) fails:system_rust_toolchain(rustc_flags = []);-Copt-level=sor-Copt-level=z, which are excluded by the digit check.The shim works around this crate by crate: the fixups of
psm,stackerandaws-lc-fips-syssetOPT_LEVEL = "3".This PR takes the last
-Copt-level=of the rustc flags,sandzincluded, and uses rustc's default0when there is none. AnOPT_LEVELin the target'senvstill takes precedence, as in those fixups.Affected version
origin/main 00c2ee4.
Reproduction
A build script that compiles one C file with cc-rs 1.2.52, run through
buildscript_runwith the prelude's demo toolchains:Expected
The build script runs, as under Cargo.
Actual
Test plan
tests/prelude/rust/buildscript_opt_level: a build script reports itsOPT_LEVELas a cfg, and the assertion expects a valid level in[rustc_flags].opt_level=""on origin/main,opt_level="0"with this change, andopt_level="s"withrustc_flags = ["-Copt-level=s"].libfoo.awith this change. To get past cc-rs, the local prelude also had theos.execvpchange of fix: resolve compiler shims through PATH #1519 (prelude/rust: from_any_dir.py execs the C compiler with os.execl, which does not search PATH — fails with ENOENT under the default system_cxx_toolchain (bareclang) #1491): under the defaultsystem_cxx_toolchain, the next step fails without it.