Skip to content

rust: Give build scripts every cfg as CARGO_CFG_*, as Cargo does - #1671

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-buildscript-cfg-env
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-buildscript-cfg-env

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Cargo sets CARGO_CFG_<name> in the environment of a build script for every cfg that rustc reports for the target, including those set by flags such as --cfg in RUSTFLAGS. Values are joined with commas, and a cfg without a value is set to "".

buildscript_run.py (cfg_env) exported only the cfgs whose names start with unix, windows or target_. So build scripts never saw:

  • a cfg the toolchain sets in its rustc_flags. rustix picks its backend from CARGO_CFG_RUSTIX_USE_LIBC; the documented way to select the libc backend is --cfg rustix_use_libc, and it had no effect;
  • panic, debug_assertions and the other cfgs rustc reports.

A cfg without a value was set to 1 rather than "". When rustc also reports the bare name of a cfg that has values (target_has_atomic), that put a stray 1 into its values: CARGO_CFG_TARGET_HAS_ATOMIC=1,128,16,32,64,8,ptr.

This PR exports every cfg of the rustc --print cfg output the way Cargo does.

Affected version

origin/main 00c2ee4.

Reproduction

rustix 1.1.6's build script, run through buildscript_run.py. The --rustc-cfg file is rustc --print cfg --target x86_64-unknown-linux-gnu --cfg rustix_use_libc, and CARGO_ENCODED_RUSTFLAGS=--cfg=rustix_use_libc, as the rule passes it for a toolchain with that flag.

Expected

The build script selects the libc backend (--cfg=libc), as under Cargo with RUSTFLAGS=--cfg rustix_use_libc.

Actual

--cfg=lower_upper_exp_for_non_zero --cfg=rustc_diagnostics --cfg=linux_raw_dep --cfg=linux_raw --cfg=linux_like --cfg=linux_kernel

With this change, --cfg=linux_raw becomes --cfg=libc. Without the cfg, the build script still selects linux_raw.

Test plan

  • prelude/rust/tools/tests/test_buildscript_run.py: new CfgEnvTest cases for a user cfg, panic, debug_assertions, an empty value for a name-only cfg, and joined values without a stray entry. Both fail without the change; all 5 tests pass with it.
  • A build script that dumps CARGO_CFG_*, run by Cargo with RUSTFLAGS="--cfg httparse_disable_simd" and by a locally built buck2 with the same flag in the toolchain's rustc_flags (rustc 1.93, macOS): HTTPARSE_DISABLE_SIMD, PANIC, DEBUG_ASSERTIONS and UNIX now match Cargo. They were missing or 1 before. Two differences remain that this change does not touch:
    • The rule prints the cfgs with RUSTC_BOOTSTRAP, so rustc also reports unstable cfgs (ub_checks, fmt_debug, …) that stable Cargo does not. These are now exported too, as Cargo exports what rustc reports; unstable target_* cfgs were already exported before.
    • CARGO_CFG_FEATURE, which Cargo derives from the enabled features.

Cargo sets CARGO_CFG_<name> for every cfg that rustc reports for the
target, those from flags such as `--cfg` included. `buildscript_run.py`
kept only `unix`, `windows` and `target_*`, so build scripts never saw
`panic`, `debug_assertions` or a cfg the toolchain sets in its rustc flags.
rustix, for one, picks its backend from CARGO_CFG_RUSTIX_USE_LIBC: with
`--cfg rustix_use_libc` in the toolchain's rustc flags, it still built the
linux_raw backend.

Export every cfg of the `rustc --print cfg` output, with its values joined
by commas and empty for a cfg without a value, as Cargo does. A name-only
cfg was `1` before, which also put a stray `1` into the values of
`target_has_atomic` when rustc reports the bare name too.
@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 D134834983. (Because this pull request was imported automatically, there will not be any future comments.)

@meta-codesync meta-codesync Bot closed this in 9e21c90 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 9e21c90.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 27, 2026
Summary:
Cargo sets `CARGO_CFG_<name>` in the environment of a build script for every cfg that rustc reports for the target, including those set by flags such as `--cfg` in `RUSTFLAGS`. Values are joined with commas, and a cfg without a value is set to `""`.

`buildscript_run.py` (`cfg_env`) exported only the cfgs whose names start with `unix`, `windows` or `target_`. So build scripts never saw:

- a cfg the toolchain sets in its `rustc_flags`. rustix picks its backend from `CARGO_CFG_RUSTIX_USE_LIBC`; the documented way to select the libc backend is `--cfg rustix_use_libc`, and it had no effect;
- `panic`, `debug_assertions` and the other cfgs rustc reports.

A cfg without a value was set to `1` rather than `""`. When rustc also reports the bare name of a cfg that has values (`target_has_atomic`), that put a stray `1` into its values: `CARGO_CFG_TARGET_HAS_ATOMIC=1,128,16,32,64,8,ptr`.

This PR exports every cfg of the `rustc --print cfg` output the way Cargo does.

## Affected version

origin/main 00c2ee4f74.

## Reproduction

rustix 1.1.6's build script, run through `buildscript_run.py`. The `--rustc-cfg` file is `rustc --print cfg --target x86_64-unknown-linux-gnu --cfg rustix_use_libc`, and `CARGO_ENCODED_RUSTFLAGS=--cfg=rustix_use_libc`, as the rule passes it for a toolchain with that flag.

## Expected

The build script selects the libc backend (`--cfg=libc`), as under Cargo with `RUSTFLAGS=--cfg rustix_use_libc`.

## Actual

```
--cfg=lower_upper_exp_for_non_zero --cfg=rustc_diagnostics --cfg=linux_raw_dep --cfg=linux_raw --cfg=linux_like --cfg=linux_kernel
```

With this change, `--cfg=linux_raw` becomes `--cfg=libc`. Without the cfg, the build script still selects `linux_raw`.

X-link: meta/buck2#1671

Reviewed By: dtolnay

Differential Revision: D134834983

fbshipit-source-id: 9d0141d34ac1b47cd20991538c9a143bb66b289c
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