Repository navigation
Conversation
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.
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.) |
dtolnay
approved these changes
Nov 27, 2026
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
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 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--cfginRUSTFLAGS. 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 withunix,windowsortarget_. So build scripts never saw:rustc_flags. rustix picks its backend fromCARGO_CFG_RUSTIX_USE_LIBC; the documented way to select the libc backend is--cfg rustix_use_libc, and it had no effect;panic,debug_assertionsand the other cfgs rustc reports.A cfg without a value was set to
1rather than"". When rustc also reports the bare name of a cfg that has values (target_has_atomic), that put a stray1into its values:CARGO_CFG_TARGET_HAS_ATOMIC=1,128,16,32,64,8,ptr.This PR exports every cfg of the
rustc --print cfgoutput the way Cargo does.Affected version
origin/main 00c2ee4.
Reproduction
rustix 1.1.6's build script, run through
buildscript_run.py. The--rustc-cfgfile isrustc --print cfg --target x86_64-unknown-linux-gnu --cfg rustix_use_libc, andCARGO_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 withRUSTFLAGS=--cfg rustix_use_libc.Actual
With this change,
--cfg=linux_rawbecomes--cfg=libc. Without the cfg, the build script still selectslinux_raw.Test plan
prelude/rust/tools/tests/test_buildscript_run.py: newCfgEnvTestcases 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.CARGO_CFG_*, run by Cargo withRUSTFLAGS="--cfg httparse_disable_simd"and by a locally built buck2 with the same flag in the toolchain'srustc_flags(rustc 1.93, macOS):HTTPARSE_DISABLE_SIMD,PANIC,DEBUG_ASSERTIONSandUNIXnow match Cargo. They were missing or1before. Two differences remain that this change does not touch: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; unstabletarget_*cfgs were already exported before.CARGO_CFG_FEATURE, which Cargo derives from the enabled features.