Skip to content

Commit

Permalink
Merge pull request #235 from daxpedda/build-std
Browse files Browse the repository at this point in the history
Support `-Zbuild-std`
  • Loading branch information
oli-obk authored Jun 11, 2024
2 parents 9f5fec5 + e1d2c69 commit e336320
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@ jobs:
run: cargo test --verbose
- name: Test no-rustc mode
run: cargo check --no-default-features

build-std:
strategy:
matrix:
include:
- os: ubuntu-latest
host_target: x86_64-unknown-linux-gnu
- os: macos-latest
host_target: x86_64-apple-darwin
- os: windows-latest
host_target: i686-pc-windows-msvc
runs-on: ${{ matrix.os }}
# Run tests under a directory with a space in it to double check the windows path heuristic
defaults:
run:
working-directory: "dir with spaces/build std"
steps:
- uses: actions/checkout@v4
with:
path: "dir with spaces/build std"
- uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src
- name: Run build-std test
run: cargo test --verbose --test build_std
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* `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
* Support `-Zbuild-std` by add
* use `DependencyBuilder::build_std` to enable it

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ features = ["capture-spantrace"]
name = "integration"
harness = false

[[test]]
name = "build_std"
test = false

[features]
default = ["rustc"]
rustc = []
50 changes: 46 additions & 4 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ fn build_dependencies_inner(
build.arg(format!("--target={target}"));
}

if let Some(packages) = &info.build_std {
if packages.is_empty() {
build.arg("-Zbuild-std");
} else {
build.arg(format!("-Zbuild-std={packages}"));
}
}

// Reusable closure for setting up the environment both for artifact generation and `cargo_metadata`
let set_locking = |cmd: &mut Command| {
if let OutputConflictHandling::Error = config.output_conflict_handling {
Expand Down Expand Up @@ -166,15 +174,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 +263,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 +295,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 +317,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 info.build_std.is_some() {
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 All @@ -329,13 +362,17 @@ pub struct DependencyBuilder {
/// The command to run can be changed from `cargo` to any custom command to build the
/// dependencies in `crate_manifest_path`.
pub program: CommandBuilder,
/// Build with [`build-std`](https://doc.rust-lang.org/1.78.0/cargo/reference/unstable.html#build-std),
/// which requires the nightly toolchain. The [`String`] can contain the standard library crates to build.
pub build_std: Option<String>,
}

impl Default for DependencyBuilder {
fn default() -> Self {
Self {
crate_manifest_path: PathBuf::from("Cargo.toml"),
program: CommandBuilder::cargo(),
build_std: None,
}
}
}
Expand Down Expand Up @@ -381,6 +418,11 @@ pub fn build_dependencies(
) -> Result<Vec<OsString>, Errored> {
let dependencies = build_dependencies_inner(config, info)?;
let mut args = vec![];

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

for (name, artifacts) in dependencies.dependencies {
for dependency in artifacts {
args.push("--extern".into());
Expand Down
21 changes: 21 additions & 0 deletions tests/build_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use ui_test::dependencies::DependencyBuilder;
use ui_test::spanned::Spanned;
use ui_test::Config;

#[test]
fn test() {
let mut config = Config::rustc("tests/build_std");
let revisioned = config.comment_defaults.base();

revisioned.exit_status = Spanned::dummy(0).into();
revisioned.require_annotations = Spanned::dummy(false).into();
revisioned.set_custom(
"dependencies",
DependencyBuilder {
build_std: Some(String::new()),
..DependencyBuilder::default()
},
);

ui_test::run_tests(config).unwrap();
}
Empty file added tests/build_std/test.rs
Empty file.

0 comments on commit e336320

Please sign in to comment.