Skip to content

Commit b36dd19

Browse files
committed
Support -Zbuild-std
1 parent 9f5fec5 commit b36dd19

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

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());

0 commit comments

Comments
 (0)