Skip to content

Commit e336320

Browse files
authored
Merge pull request #235 from daxpedda/build-std
Support `-Zbuild-std`
2 parents 9f5fec5 + e1d2c69 commit e336320

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed

.github/workflows/rust.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,28 @@ jobs:
6060
run: cargo test --verbose
6161
- name: Test no-rustc mode
6262
run: cargo check --no-default-features
63+
64+
build-std:
65+
strategy:
66+
matrix:
67+
include:
68+
- os: ubuntu-latest
69+
host_target: x86_64-unknown-linux-gnu
70+
- os: macos-latest
71+
host_target: x86_64-apple-darwin
72+
- os: windows-latest
73+
host_target: i686-pc-windows-msvc
74+
runs-on: ${{ matrix.os }}
75+
# Run tests under a directory with a space in it to double check the windows path heuristic
76+
defaults:
77+
run:
78+
working-directory: "dir with spaces/build std"
79+
steps:
80+
- uses: actions/checkout@v4
81+
with:
82+
path: "dir with spaces/build std"
83+
- uses: dtolnay/rust-toolchain@nightly
84+
with:
85+
components: rust-src
86+
- name: Run build-std test
87+
run: cargo test --verbose --test build_std

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* `Text::diff()` creates a text status emitter that does not do full dumps of test stderr/stdout, but only emits the diff of the changes
13+
* Support `-Zbuild-std` by add
14+
* use `DependencyBuilder::build_std` to enable it
1315

1416
### Fixed
1517

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ features = ["capture-spantrace"]
4545
name = "integration"
4646
harness = false
4747

48+
[[test]]
49+
name = "build_std"
50+
test = false
51+
4852
[features]
4953
default = ["rustc"]
5054
rustc = []

src/dependencies.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ fn build_dependencies_inner(
9090
build.arg(format!("--target={target}"));
9191
}
9292

93+
if let Some(packages) = &info.build_std {
94+
if packages.is_empty() {
95+
build.arg("-Zbuild-std");
96+
} else {
97+
build.arg(format!("-Zbuild-std={packages}"));
98+
}
99+
}
100+
93101
// Reusable closure for setting up the environment both for artifact generation and `cargo_metadata`
94102
let set_locking = |cmd: &mut Command| {
95103
if let OutputConflictHandling::Error = config.output_conflict_handling {
@@ -166,15 +174,18 @@ fn build_dependencies_inner(
166174
.target
167175
.crate_types
168176
.iter()
169-
.all(|ctype| !matches!(ctype.as_str(), "proc-macro" | "lib"))
177+
.all(|ctype| !matches!(ctype.as_str(), "proc-macro" | "lib" | "rlib"))
170178
{
171179
continue;
172180
}
173181
for filename in &artifact.filenames {
174182
import_paths.insert(filename.parent().unwrap().into());
175183
}
176184
let package_id = artifact.package_id;
177-
if let Some(prev) = artifacts.insert(package_id.clone(), Ok(artifact.filenames)) {
185+
if let Some(prev) = artifacts.insert(
186+
package_id.clone(),
187+
Ok((artifact.target.name, artifact.filenames)),
188+
) {
178189
artifacts.insert(
179190
package_id.clone(),
180191
Err(format!(
@@ -252,7 +263,7 @@ fn build_dependencies_inner(
252263
.unwrap();
253264

254265
// Then go over all of its dependencies
255-
let dependencies = root
266+
let mut dependencies = root
256267
.dependencies
257268
.iter()
258269
.filter(|dep| matches!(dep.kind, DependencyKind::Normal))
@@ -284,7 +295,7 @@ fn build_dependencies_inner(
284295
let id = &package.id;
285296
// Return the name chosen in `Cargo.toml` and the path to the corresponding artifact
286297
match artifacts.remove(id) {
287-
Some(Ok(artifacts)) => Some(Ok((name.replace('-', "_"), artifacts))),
298+
Some(Ok((_, artifacts))) => Some(Ok((name.replace('-', "_"), artifacts))),
288299
Some(Err(what)) => Some(Err(Errored {
289300
command: Command::new(what),
290301
errors: vec![],
@@ -306,6 +317,28 @@ fn build_dependencies_inner(
306317
.collect::<Result<Vec<_>, Errored>>()?;
307318
let import_paths = import_paths.into_iter().collect();
308319
let import_libs = import_libs.into_iter().collect();
320+
321+
if info.build_std.is_some() {
322+
let mut build_std_crates = HashSet::new();
323+
build_std_crates.insert("core");
324+
build_std_crates.insert("alloc");
325+
build_std_crates.insert("proc_macro");
326+
build_std_crates.insert("panic_unwind");
327+
build_std_crates.insert("compiler_builtins");
328+
build_std_crates.insert("std");
329+
build_std_crates.insert("test");
330+
build_std_crates.insert("panic_abort");
331+
332+
for (name, artifacts) in artifacts
333+
.into_iter()
334+
.filter_map(|(_, artifacts)| artifacts.ok())
335+
{
336+
if build_std_crates.remove(name.as_str()) {
337+
dependencies.push((format!("noprelude:{name}"), artifacts));
338+
}
339+
}
340+
}
341+
309342
return Ok(Dependencies {
310343
dependencies,
311344
import_paths,
@@ -329,13 +362,17 @@ pub struct DependencyBuilder {
329362
/// The command to run can be changed from `cargo` to any custom command to build the
330363
/// dependencies in `crate_manifest_path`.
331364
pub program: CommandBuilder,
365+
/// Build with [`build-std`](https://doc.rust-lang.org/1.78.0/cargo/reference/unstable.html#build-std),
366+
/// which requires the nightly toolchain. The [`String`] can contain the standard library crates to build.
367+
pub build_std: Option<String>,
332368
}
333369

334370
impl Default for DependencyBuilder {
335371
fn default() -> Self {
336372
Self {
337373
crate_manifest_path: PathBuf::from("Cargo.toml"),
338374
program: CommandBuilder::cargo(),
375+
build_std: None,
339376
}
340377
}
341378
}
@@ -381,6 +418,11 @@ pub fn build_dependencies(
381418
) -> Result<Vec<OsString>, Errored> {
382419
let dependencies = build_dependencies_inner(config, info)?;
383420
let mut args = vec![];
421+
422+
if info.build_std.is_some() {
423+
args.push("-Zunstable-options".into());
424+
}
425+
384426
for (name, artifacts) in dependencies.dependencies {
385427
for dependency in artifacts {
386428
args.push("--extern".into());

tests/build_std.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use ui_test::dependencies::DependencyBuilder;
2+
use ui_test::spanned::Spanned;
3+
use ui_test::Config;
4+
5+
#[test]
6+
fn test() {
7+
let mut config = Config::rustc("tests/build_std");
8+
let revisioned = config.comment_defaults.base();
9+
10+
revisioned.exit_status = Spanned::dummy(0).into();
11+
revisioned.require_annotations = Spanned::dummy(false).into();
12+
revisioned.set_custom(
13+
"dependencies",
14+
DependencyBuilder {
15+
build_std: Some(String::new()),
16+
..DependencyBuilder::default()
17+
},
18+
);
19+
20+
ui_test::run_tests(config).unwrap();
21+
}

tests/build_std/test.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)