Skip to content

Commit 4714578

Browse files
committed
Decouple cache dir implementation from tests
1 parent 9561c16 commit 4714578

File tree

12 files changed

+137
-62
lines changed

12 files changed

+137
-62
lines changed

Cargo.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"crates/rustc_codegen_spirv-cache",
34
"crates/cargo-gpu",
45
"crates/xtask",
56
]
@@ -12,9 +13,17 @@ exclude = [
1213

1314
resolver = "2"
1415

16+
[workspace.package]
17+
version = "0.1.0"
18+
edition = "2021"
19+
repository = "https://github.com/Rust-GPU/cargo-gpu"
20+
keywords = ["gpu", "compiler", "rust-gpu"]
21+
license = "MIT OR Apache-2.0"
22+
1523
[workspace.dependencies]
1624
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "3df836eb9d7b01344f52737bf9a310d1fb5a0c26", default-features = false }
1725
anyhow = "1.0.98"
26+
thiserror = "2.0.12"
1827
clap = { version = "4.5.41", features = ["derive"] }
1928
crossterm = "0.29.0"
2029
directories = "6.0.0"

crates/cargo-gpu/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
[package]
22
name = "cargo-gpu"
3-
version = "0.1.0"
4-
edition = "2021"
53
description = "Generates shader .spv files from rust-gpu shader crates"
6-
repository = "https://github.com/Rust-GPU/cargo-gpu"
74
readme = "../../README.md"
8-
keywords = ["gpu", "compiler", "rust-gpu"]
9-
license = "MIT OR Apache-2.0"
5+
version.workspace = true
6+
edition.workspace = true
7+
repository.workspace = true
8+
keywords.workspace = true
9+
license.workspace = true
1010
default-run = "cargo-gpu"
11-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1211

1312
[dependencies]
13+
rustc_codegen_spirv-cache = { path = "../rustc_codegen_spirv-cache", default-features = false }
1414
cargo_metadata.workspace = true
1515
anyhow.workspace = true
1616
spirv-builder = { workspace = true, features = ["clap", "watch"] }
1717
legacy_target_specs.workspace = true
1818
clap.workspace = true
19-
directories.workspace = true
2019
env_logger.workspace = true
2120
log.workspace = true
2221
relative-path.workspace = true
@@ -27,9 +26,10 @@ semver.workspace = true
2726
dunce.workspace = true
2827

2928
[dev-dependencies]
29+
tempfile.workspace = true
3030
test-log.workspace = true
3131
cargo_metadata = { workspace = true, features = ["builder"] }
32-
cargo-util-schemas = "0.8.2"
32+
cargo-util-schemas.workspace = true
3333

3434
[lints]
3535
workspace = true

crates/cargo-gpu/src/install.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
22
3-
use crate::spirv_source::{
4-
get_channel_from_rustc_codegen_spirv_build_script, query_metadata, FindPackage as _,
5-
};
6-
use crate::target_specs::update_target_specs_files;
7-
use crate::{cache_dir, spirv_source::SpirvSource};
3+
use std::path::{Path, PathBuf};
4+
85
use anyhow::Context as _;
6+
use rustc_codegen_spirv_cache::cache::cache_dir;
97
use spirv_builder::SpirvBuilder;
10-
use std::path::{Path, PathBuf};
8+
9+
use crate::{
10+
spirv_source::{
11+
get_channel_from_rustc_codegen_spirv_build_script, query_metadata, FindPackage as _,
12+
SpirvSource,
13+
},
14+
target_specs::update_target_specs_files,
15+
};
1116

1217
/// Represents a functional backend installation, whether it was cached or just installed.
1318
#[derive(Clone, Debug, Default)]

crates/cargo-gpu/src/lib.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
//! conduct other post-processing, like converting the `spv` files into `wgsl` files,
5151
//! for example.
5252
53-
use anyhow::Context as _;
54-
5553
use crate::dump_usage::dump_full_usage_for_readme;
5654
use build::Build;
5755
use show::Show;
@@ -166,26 +164,6 @@ pub struct Cli {
166164
pub command: Command,
167165
}
168166

169-
/// The central cache directory of cargo gpu
170-
///
171-
/// # Errors
172-
/// may fail if we can't find the user home directory
173-
#[inline]
174-
pub fn cache_dir() -> anyhow::Result<std::path::PathBuf> {
175-
let dir = directories::BaseDirs::new()
176-
.with_context(|| "could not find the user home directory")?
177-
.cache_dir()
178-
.join("rust-gpu");
179-
180-
Ok(if cfg!(test) {
181-
let thread_id = std::thread::current().id();
182-
let id = format!("{thread_id:?}").replace('(', "-").replace(')', "");
183-
dir.join("tests").join(id)
184-
} else {
185-
dir
186-
})
187-
}
188-
189167
/// Returns a string suitable to use as a directory.
190168
///
191169
/// Created from the spirv-builder source dep and the rustc channel.

crates/cargo-gpu/src/show.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Display various information about `cargo gpu`, eg its cache directory.
22
3-
use crate::cache_dir;
4-
use crate::spirv_source::{query_metadata, SpirvSource};
5-
use crate::target_specs::update_target_specs_files;
3+
use std::{fs, path::Path};
4+
65
use anyhow::bail;
7-
use std::fs;
8-
use std::path::Path;
6+
use rustc_codegen_spirv_cache::cache::cache_dir;
7+
8+
use crate::{
9+
spirv_source::{query_metadata, SpirvSource},
10+
target_specs::update_target_specs_files,
11+
};
912

1013
/// Show the computed source of the spirv-std dependency.
1114
#[derive(Clone, Debug, clap::Parser)]

crates/cargo-gpu/src/spirv_source.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
55
//! From there we can look at the source code to get the required Rust toolchain.
66
7+
use std::{
8+
fs,
9+
path::{Path, PathBuf},
10+
};
11+
712
use anyhow::Context as _;
8-
use cargo_metadata::camino::{Utf8Path, Utf8PathBuf};
9-
use cargo_metadata::semver::Version;
10-
use cargo_metadata::{Metadata, MetadataCommand, Package};
11-
use std::fs;
12-
use std::path::{Path, PathBuf};
13+
use cargo_metadata::{
14+
camino::{Utf8Path, Utf8PathBuf},
15+
semver::Version,
16+
Metadata, MetadataCommand, Package,
17+
};
18+
use rustc_codegen_spirv_cache::cache::cache_dir;
1319

1420
#[expect(
1521
clippy::doc_markdown,
@@ -121,7 +127,7 @@ impl SpirvSource {
121127
} => Ok(rust_gpu_repo_root.as_std_path().to_owned()),
122128
Self::CratesIO { .. } | Self::Git { .. } => {
123129
let dir = crate::to_dirname(self.to_string().as_ref());
124-
Ok(crate::cache_dir()?.join("codegen").join(dir))
130+
Ok(cache_dir()?.join("codegen").join(dir))
125131
}
126132
}
127133
}

crates/cargo-gpu/src/target_specs.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
//! was implemented, so we can support both old and new target specs without having to worry
2323
//! which version of cargo gpu you are using. It'll "just work".
2424
25-
use crate::cache_dir;
26-
use crate::spirv_source::{FindPackage as _, SpirvSource};
25+
use std::path::{Path, PathBuf};
26+
2727
use anyhow::Context as _;
2828
use cargo_metadata::Metadata;
29-
use std::path::{Path, PathBuf};
29+
use rustc_codegen_spirv_cache::cache::cache_dir;
30+
31+
use crate::spirv_source::{FindPackage as _, SpirvSource};
3032

3133
/// Extract legacy target specs from our executable into some directory
3234
pub fn write_legacy_target_specs(target_spec_dir: &Path) -> anyhow::Result<()> {

crates/cargo-gpu/src/test.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! utilities for tests
2+
23
#![cfg(test)]
34

4-
use crate::cache_dir;
5-
use std::io::Write as _;
5+
use std::{cell::RefCell, io::Write as _};
66

77
fn copy_dir_all(
88
src: impl AsRef<std::path::Path>,
@@ -20,16 +20,23 @@ fn copy_dir_all(
2020
}
2121
Ok(())
2222
}
23-
2423
pub fn shader_crate_template_path() -> std::path::PathBuf {
2524
let project_base = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2625
project_base.join("../shader-crate-template")
2726
}
2827

28+
thread_local! {
29+
static TEMPDIR: RefCell<Option<tempfile::TempDir>> = RefCell::new(Some(
30+
tempfile::TempDir::with_prefix("shader_crate").unwrap(),
31+
));
32+
}
33+
2934
pub fn shader_crate_test_path() -> std::path::PathBuf {
30-
let shader_crate_path = crate::cache_dir().unwrap().join("shader_crate");
31-
copy_dir_all(shader_crate_template_path(), shader_crate_path.clone()).unwrap();
32-
shader_crate_path
35+
TEMPDIR.with_borrow(|tempdir| {
36+
let shader_crate_path = tempdir.as_ref().unwrap().path();
37+
copy_dir_all(shader_crate_template_path(), shader_crate_path).unwrap();
38+
shader_crate_path.to_path_buf()
39+
})
3340
}
3441

3542
pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::fs::File {
@@ -45,9 +52,7 @@ pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::
4552
}
4653

4754
pub fn tests_teardown() {
48-
let cache_dir = cache_dir().unwrap();
49-
if !cache_dir.exists() {
50-
return;
51-
}
52-
std::fs::remove_dir_all(cache_dir).unwrap();
55+
TEMPDIR.with_borrow_mut(|tempdir| {
56+
tempdir.take().unwrap().close().unwrap();
57+
});
5358
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "rustc_codegen_spirv-cache"
3+
description = "Cacher of `rust-gpu` codegen for required toolchain"
4+
version.workspace = true
5+
edition.workspace = true
6+
repository.workspace = true
7+
keywords.workspace = true
8+
license.workspace = true
9+
10+
[dependencies]
11+
thiserror.workspace = true
12+
directories.workspace = true
13+
14+
[lints]
15+
workspace = true

0 commit comments

Comments
 (0)