Skip to content

Commit

Permalink
Support -Zbuild-std
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed May 25, 2024
1 parent fb99ff7 commit 72346fd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Support `-Zbuild-std`.

### Fixed

### Changed
Expand Down
58 changes: 54 additions & 4 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ fn cfgs(config: &Config) -> Result<Vec<Cfg>, Errored> {
Ok(cfgs)
}

pub(crate) fn has_build_std(info: &DependencyBuilder) -> bool {
let mut args = info.program.args.iter();

while let Some(arg) = args.next() {
if arg == "-Z" {
if let Some(arg) = args.next().map(|arg| arg.to_string_lossy()) {
if arg.starts_with("build-std") {
return true;
}
}
}

if arg.to_string_lossy().starts_with("-Zbuild-std") {
return true;
}
}

false
}

/// Compiles dependencies and returns the crate names and corresponding rmeta files.
fn build_dependencies_inner(
config: &Config,
Expand Down Expand Up @@ -166,15 +186,18 @@ fn build_dependencies_inner(
.target
.crate_types
.iter()
.all(|ctype| !matches!(ctype.as_str(), "proc-macro" | "lib"))
.all(|ctype| !matches!(ctype.as_str(), "proc-macro" | "lib" | "rlib"))
{
continue;
}
for filename in &artifact.filenames {
import_paths.insert(filename.parent().unwrap().into());
}
let package_id = artifact.package_id;
if let Some(prev) = artifacts.insert(package_id.clone(), Ok(artifact.filenames)) {
if let Some(prev) = artifacts.insert(
package_id.clone(),
Ok((artifact.target.name, artifact.filenames)),
) {
artifacts.insert(
package_id.clone(),
Err(format!(
Expand Down Expand Up @@ -252,7 +275,7 @@ fn build_dependencies_inner(
.unwrap();

// Then go over all of its dependencies
let dependencies = root
let mut dependencies = root
.dependencies
.iter()
.filter(|dep| matches!(dep.kind, DependencyKind::Normal))
Expand Down Expand Up @@ -284,7 +307,7 @@ fn build_dependencies_inner(
let id = &package.id;
// Return the name chosen in `Cargo.toml` and the path to the corresponding artifact
match artifacts.remove(id) {
Some(Ok(artifacts)) => Some(Ok((name.replace('-', "_"), artifacts))),
Some(Ok((_, artifacts))) => Some(Ok((name.replace('-', "_"), artifacts))),
Some(Err(what)) => Some(Err(Errored {
command: Command::new(what),
errors: vec![],
Expand All @@ -306,6 +329,28 @@ fn build_dependencies_inner(
.collect::<Result<Vec<_>, Errored>>()?;
let import_paths = import_paths.into_iter().collect();
let import_libs = import_libs.into_iter().collect();

if has_build_std(info) {
let mut build_std_crates = HashSet::new();
build_std_crates.insert("core");
build_std_crates.insert("alloc");
build_std_crates.insert("proc_macro");
build_std_crates.insert("panic_unwind");
build_std_crates.insert("compiler_builtins");
build_std_crates.insert("std");
build_std_crates.insert("test");
build_std_crates.insert("panic_abort");

for (name, artifacts) in artifacts
.into_iter()
.filter_map(|(_, artifacts)| artifacts.ok())
{
if build_std_crates.remove(name.as_str()) {
dependencies.push((format!("noprelude:{name}"), artifacts));
}
}
}

return Ok(Dependencies {
dependencies,
import_paths,
Expand Down Expand Up @@ -381,6 +426,11 @@ pub fn build_dependencies(
) -> Result<Vec<OsString>, Errored> {
let dependencies = build_dependencies_inner(config, info)?;
let mut args = vec![];

if has_build_std(info) {
args.push("-Zunstable-options".into());
}

for (name, artifacts) in dependencies.dependencies {
for dependency in artifacts {
args.push("--extern".into());
Expand Down

0 comments on commit 72346fd

Please sign in to comment.