Skip to content

use cargo rustc for macro expansion so -Zbuild-std can be supported#5214

Open
skrap wants to merge 1 commit into
rust-lang:mainfrom
skrap:bugfix/allow-zbuild-std-macro-expansion
Open

use cargo rustc for macro expansion so -Zbuild-std can be supported#5214
skrap wants to merge 1 commit into
rust-lang:mainfrom
skrap:bugfix/allow-zbuild-std-macro-expansion

Conversation

@skrap

@skrap skrap commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description

Address #5204 by invoking macro expansion through cargo rustc instead of rustc directly. This allows the use of the -Zbuild-std flag, enabling targets without a prebuilt std (e.g. tier 3 targets) to build the libc tests.

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot rustbot added ctest Issues relating to the ctest crate S-waiting-on-review labels Jun 24, 2026
@skrap skrap force-pushed the bugfix/allow-zbuild-std-macro-expansion branch 4 times, most recently from 0689c20 to b5da051 Compare June 24, 2026 18:34
&mut cargo_toml,
r#"
[package]
name = "libc"

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be configurable:

  1. Move this function body to a new expand_with_name that takes a name: Option<&str> parameter
  2. Use that parameter in the template here, or ctest-expansion if None
  3. expand can then call expand_with_name with None to keep the public API
  4. Add a crate_name(&str) function to TestGenerator that saves it in the config
  5. Replace internal calls to expand with expand_with_name, using the stored crate_name if available

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it works as is, what's the need for this change? I think everything using this is either "libc" or it doesn't care if it is called "libc" for the purposes of macro expansion.

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to be confusing if ctest users unrelated to libc get an error compiling libc, and also other users could be setting crate_name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize this crate was used outside of the libc context. Thanks. Also, slightly terrifying for me, ha.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change also drops the ability to use the RUSTC env var to use a custom rustc. I don't know if there's any way to keep that when using cargo rustc or not. Given that there's external users, I will have to check on that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a lot of places, but at least a few important places 🙂 https://github.com/search?q=path%3ACargo.toml+%2F%5Cbctest%5Cb.*%3D%2F&type=code. Certainly nothing to worry about, though, it's a low pressure crate.

@tgross35

Copy link
Copy Markdown
Contributor

Cc @mbyx for ctest

@tgross35 tgross35 added the stable-unneeded This PR is applied to main but already exists on libc-0.2 label Jun 24, 2026
@skrap skrap force-pushed the bugfix/allow-zbuild-std-macro-expansion branch from b5da051 to 1100563 Compare June 24, 2026 19:18
[lib]
path = '{}'
[lints.rust]
unexpected_cfgs = "allow"

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a toml comment about why

View changes since the review

name = "libc"
edition = "{EDITION}"
[lib]
path = '{}'

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a regular string here and escape it by replacing \->\\ and "->\", since this will act weird in the (unlikely) case the path contains a '

View changes since the review

.arg("--edition")
.arg(EDITION) // By default, -Zunpretty=expanded uses 2015 edition.
.arg(canonicalize(crate_path)?);
.env("RUSTFLAGS", "") // ignore RUSTFLAGS so we don't get -Dwarnings

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a TestGenerator::expansion_rustflags(&str) and set RUSTFLAGS only if that has been called, otherwise just let it inherit. Since there might be some global options needed to build.

View changes since the review


if env::var("LIBC_CI_ZBUILD_STD").is_ok() {
cmd.arg("-Zbuild-std=core,std");
}

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add TestGenerator::build_std(bool) and TestGenerator::build_core(bool) to control this, which should set -Zbuild-std=std / -Zbuild-std=core.

View changes since the review

Comment on lines +22 to +24
let mut cargo_toml = std::fs::File::create(&cargo_toml_path)?;
writeln!(
&mut cargo_toml,

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: just construct the string with format! then use fs::write to avoid bothering with the File object.

View changes since the review

cfg: &[(String, Option<String>)],
target: String,
) -> Result<String> {
let rustc = env::var("RUSTC").unwrap_or_else(|_| String::from("rustc"));

@tgross35 tgross35 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep something similar to this but with CARGO for setting something not in-path.

View changes since the review

@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

ctest Issues relating to the ctest crate S-waiting-on-author stable-unneeded This PR is applied to main but already exists on libc-0.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants