Skip to content

rust: Always set OPT_LEVEL for build scripts - #1669

Closed
phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-buildscript-opt-level
Closed

phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-buildscript-opt-level

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

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 00c2ee4.

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:

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.

Test plan

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
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.)

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
@meta-codesync meta-codesync Bot closed this in a97075f Nov 27, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in a97075f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants