Skip to content

Commit 6c052f3

Browse files
committed
Decouple cache dir implementation from tests
1 parent e7950d4 commit 6c052f3

File tree

16 files changed

+175
-93
lines changed

16 files changed

+175
-93
lines changed

Cargo.lock

Lines changed: 9 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: 1 addition & 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-cache",
45
"crates/cargo-gpu",
56
"crates/xtask",

crates/cargo-gpu-cache/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ keywords.workspace = true
88
license.workspace = true
99

1010
[dependencies]
11+
rustc_codegen_spirv-cache = { path = "../rustc_codegen_spirv-cache", default-features = false }
1112
cargo_metadata.workspace = true
1213
anyhow.workspace = true
1314
spirv-builder.workspace = true
1415
legacy_target_specs.workspace = true
1516
clap = { workspace = true, optional = true }
16-
directories.workspace = true
1717
log.workspace = true
1818
relative-path.workspace = true
1919
serde.workspace = true
@@ -24,6 +24,7 @@ dunce.workspace = true
2424

2525
[dev-dependencies]
2626
test-log.workspace = true
27+
tempfile.workspace = true
2728
cargo_metadata = { workspace = true, features = ["builder"] }
2829
cargo-util-schemas.workspace = true
2930

crates/cargo-gpu-cache/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Build {
110110
crate::user_output!(
111111
"Compiling shaders at {}...\n",
112112
self.install.shader_crate.display()
113-
);
113+
)?;
114114
let result = self.build.spirv_builder.build()?;
115115
self.parse_compilation_result(&result)?;
116116
Ok(())

crates/cargo-gpu-cache/src/dump_usage.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub fn dump_full_usage_for_readme() -> anyhow::Result<()> {
1414
command.build();
1515

1616
write_help(&mut buffer, &mut command, 0)?;
17-
user_output!("{}", String::from_utf8(buffer)?);
17+
18+
let message = String::from_utf8(buffer)?;
19+
user_output!("{message}")?;
1820

1921
Ok(())
2022
}

crates/cargo-gpu-cache/src/install.rs

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

1215
/// Represents a functional backend installation, whether it was cached or just installed.
1316
#[derive(Clone, Debug, Default)]
@@ -284,7 +287,7 @@ package = "rustc_codegen_spirv"
284287
.context("remove Cargo.lock")?;
285288
}
286289

287-
crate::user_output!("Compiling `rustc_codegen_spirv` from source {}\n", source);
290+
crate::user_output!("Compiling `rustc_codegen_spirv` from source {}\n", source)?;
288291
let mut cargo = spirv_builder::cargo_cmd::CargoCmd::new();
289292
cargo
290293
.current_dir(&install_dir)

crates/cargo-gpu-cache/src/install_toolchain.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn ensure_toolchain_and_components_exist(
3636
format!("Install {message}").as_ref(),
3737
skip_toolchain_install_consent,
3838
)?;
39-
crate::user_output!("Installing {message}\n");
39+
crate::user_output!("Installing {message}\n")?;
4040

4141
let output_toolchain_add = std::process::Command::new("rustup")
4242
.args(["toolchain", "add"])
@@ -79,7 +79,7 @@ pub fn ensure_toolchain_and_components_exist(
7979
format!("Install {message}").as_ref(),
8080
skip_toolchain_install_consent,
8181
)?;
82-
crate::user_output!("Installing {message}\n");
82+
crate::user_output!("Installing {message}\n")?;
8383

8484
let output_component_add = std::process::Command::new("rustup")
8585
.args(["component", "add", "--toolchain"])
@@ -108,15 +108,15 @@ fn get_consent_for_toolchain_install(
108108
}
109109

110110
if !std::io::stdout().is_tty() {
111-
user_output!("No TTY detected so can't ask for consent to install Rust toolchain.");
111+
user_output!("No TTY detected so can't ask for consent to install Rust toolchain.")?;
112112
log::error!("Attempted to ask for consent when there's no TTY");
113113
#[expect(clippy::exit, reason = "can't ask for user consent if there's no TTY")]
114114
std::process::exit(1);
115115
}
116116

117117
log::debug!("asking for consent to install the required toolchain");
118118
crossterm::terminal::enable_raw_mode().context("enabling raw mode")?;
119-
crate::user_output!("{prompt} [y/n]: ");
119+
crate::user_output!("{prompt} [y/n]: ")?;
120120
let mut input = crossterm::event::read().context("reading crossterm event")?;
121121

122122
if let crossterm::event::Event::Key(crossterm::event::KeyEvent {
@@ -138,7 +138,7 @@ fn get_consent_for_toolchain_install(
138138
{
139139
Ok(())
140140
} else {
141-
crate::user_output!("Exiting...\n");
141+
crate::user_output!("Exiting...\n")?;
142142
#[expect(clippy::exit, reason = "user requested abort")]
143143
std::process::exit(0);
144144
}

crates/cargo-gpu-cache/src/lib.rs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,14 @@
77
//!
88
//! # How it works
99
//!
10-
//! This library primarily manages installations of `rustc_codegen_spirv`, the
11-
//! codegen backend of rust-gpu to generate SPIR-V shader binaries. The codegen
12-
//! backend builds on internal, ever-changing interfaces of rustc, which requires
13-
//! fixing a version of rust-gpu to a specific version of the rustc compiler.
14-
//! Usually, this would require you to fix your entire project to that specific
15-
//! toolchain, but this project loosens that requirement by managing installations
16-
//! of `rustc_codegen_spirv` and their associated toolchains for you.
10+
//! This library manages installations of `rustc_codegen_spirv`
11+
//! using rust-gpu's [`rustc_codegen_spirv-cache`](rustc_codegen_spirv_cache) crate.
1712
//!
18-
//! We continue to use rust-gpu's `spirv_builder` crate to pass the many additional
19-
//! parameters required to configure rustc and our codegen backend, but provide you
20-
//! with a toolchain agnostic version that you may use from stable rustc.
13+
//! Then we continue to use rust-gpu's [`spirv-builder`](spirv_builder) crate
14+
//! to pass the many additional parameters required to configure rustc and our codegen backend,
15+
//! but provide you with a toolchain agnostic version that you may use from stable rustc.
2116
//! And a `cargo gpu` command line utility to simplify shader building even more.
2217
//!
23-
//! ## Where the binaries are
24-
//!
25-
//! We store our prebuilt `rustc_spirv_builder` binaries in the default cache
26-
//! directory of your OS:
27-
//! * Windows: `C:/users/<user>/AppData/Local/rust-gpu`
28-
//! * Mac: `~/Library/Caches/rust-gpu`
29-
//! * Linux: `~/.cache/rust-gpu`
30-
//!
3118
//! ## How we build the backend
3219
//!
3320
//! * retrieve the version of rust-gpu you want to use based on the version of the
@@ -50,8 +37,6 @@
5037
//! conduct other post-processing, like converting the `spv` files into `wgsl` files,
5138
//! for example.
5239
53-
use anyhow::Context as _;
54-
5540
use build::Build;
5641
use show::Show;
5742

@@ -74,28 +59,30 @@ mod test;
7459
pub use install::*;
7560
pub use spirv_builder;
7661

77-
/// Central function to write to the user.
62+
/// Writes formatted user output into a [writer](std::io::Write).
7863
#[macro_export]
79-
macro_rules! user_output {
80-
($($args: tt)*) => {
64+
macro_rules! write_user_output {
65+
($dst:expr, $($args:tt)*) => {{
8166
#[allow(
8267
clippy::allow_attributes,
8368
clippy::useless_attribute,
8469
unused_imports,
8570
reason = "`std::io::Write` is only sometimes called??"
8671
)]
87-
use std::io::Write as _;
72+
use ::std::io::Write as _;
73+
74+
let mut writer = $dst;
75+
#[expect(clippy::non_ascii_literal, reason = "CRAB GOOD. CRAB IMPORTANT.")]
76+
::std::write!(writer, "🦀 ")
77+
.and_then(|()| ::std::write!(writer, $($args)*))
78+
.and_then(|()| ::std::io::Write::flush(&mut writer))
79+
}};
80+
}
8881

89-
#[expect(
90-
clippy::non_ascii_literal,
91-
reason = "CRAB GOOD. CRAB IMPORTANT."
92-
)]
93-
{
94-
print!("🦀 ");
95-
}
96-
print!($($args)*);
97-
std::io::stdout().flush().unwrap();
98-
}
82+
/// Central function to write to the user.
83+
#[macro_export]
84+
macro_rules! user_output {
85+
($($args: tt)*) => { $crate::write_user_output!(::std::io::stdout(), $($args)*) };
9986
}
10087

10188
/// All of the available subcommands for `cargo gpu`
@@ -176,26 +163,6 @@ pub struct Cli {
176163
pub command: Command,
177164
}
178165

179-
/// The central cache directory of cargo gpu
180-
///
181-
/// # Errors
182-
/// may fail if we can't find the user home directory
183-
#[inline]
184-
pub fn cache_dir() -> anyhow::Result<std::path::PathBuf> {
185-
let dir = directories::BaseDirs::new()
186-
.with_context(|| "could not find the user home directory")?
187-
.cache_dir()
188-
.join("rust-gpu");
189-
190-
Ok(if cfg!(test) {
191-
let thread_id = std::thread::current().id();
192-
let id = format!("{thread_id:?}").replace('(', "-").replace(')', "");
193-
dir.join("tests").join(id)
194-
} else {
195-
dir
196-
})
197-
}
198-
199166
/// Returns a string suitable to use as a directory.
200167
///
201168
/// Created from the spirv-builder source dep and the rustc channel.

crates/cargo-gpu-cache/src/lockfile.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ impl LockfileMismatchHandler {
238238
Ok(())
239239
}
240240

241-
/// Exit and give the user advice on how to deal with the infamous v3/v4 Cargo lockfile version
242-
/// problem.
243-
#[expect(clippy::non_ascii_literal, reason = "It's CLI output")]
241+
/// Exit and give the user advice on how to deal with the infamous
242+
/// v3/v4 Cargo lockfile version problem.
243+
#[expect(clippy::unwrap_used, reason = "It's CLI output")]
244244
fn exit_with_v3v4_hack_suggestion() {
245245
crate::user_output!(
246246
"Conflicting `Cargo.lock` versions detected ⚠️\n\
@@ -259,7 +259,8 @@ impl LockfileMismatchHandler {
259259
\n\
260260
See `cargo gpu build --help` for more information.\n\
261261
"
262-
);
262+
)
263+
.unwrap();
263264
std::process::exit(1);
264265
}
265266
}

crates/cargo-gpu-cache/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_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)]

0 commit comments

Comments
 (0)