Skip to content

Commit e50f61d

Browse files
committed
Move SpirvSource out of CLI
1 parent 4714578 commit e50f61d

File tree

10 files changed

+73
-34
lines changed

10 files changed

+73
-34
lines changed

Cargo.lock

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

crates/cargo-gpu/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ dunce.workspace = true
2929
tempfile.workspace = true
3030
test-log.workspace = true
3131
cargo_metadata = { workspace = true, features = ["builder"] }
32-
cargo-util-schemas.workspace = true
3332

3433
[lints]
3534
workspace = true

crates/cargo-gpu/src/install.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
use std::path::{Path, PathBuf};
44

55
use anyhow::Context as _;
6-
use rustc_codegen_spirv_cache::cache::cache_dir;
7-
use spirv_builder::SpirvBuilder;
8-
9-
use crate::{
6+
use rustc_codegen_spirv_cache::{
7+
cache::cache_dir,
108
spirv_source::{
119
get_channel_from_rustc_codegen_spirv_build_script, query_metadata, FindPackage as _,
1210
SpirvSource,
1311
},
14-
target_specs::update_target_specs_files,
1512
};
13+
use spirv_builder::SpirvBuilder;
14+
15+
use crate::target_specs::update_target_specs_files;
1616

1717
/// Represents a functional backend installation, whether it was cached or just installed.
1818
#[derive(Clone, Debug, Default)]
@@ -75,8 +75,9 @@ pub struct Install {
7575
pub shader_crate: PathBuf,
7676

7777
#[expect(
78+
rustdoc::bare_urls,
7879
clippy::doc_markdown,
79-
reason = "The URL should appear literally like this. But Clippy wants a markdown clickable link"
80+
reason = "The URL should appear literally like this. But Clippy & rustdoc want a markdown clickable link"
8081
)]
8182
/// Source of `spirv-builder` dependency
8283
/// Eg: "https://github.com/Rust-GPU/rust-gpu"
@@ -182,6 +183,9 @@ impl Install {
182183
new_path.push("crates/spirv-builder");
183184
format!("path = \"{new_path}\"\nversion = \"{version}\"")
184185
}
186+
// TODO: remove this once this module moves to rustc_codegen_spirv-cache
187+
#[expect(clippy::todo, reason = "temporary allow this")]
188+
_ => todo!(),
185189
};
186190
let cargo_toml = format!(
187191
r#"

crates/cargo-gpu/src/lib.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ mod linkage;
6363
mod lockfile;
6464
mod metadata;
6565
mod show;
66-
mod spirv_source;
6766
mod target_specs;
6867
mod test;
6968

@@ -163,16 +162,3 @@ pub struct Cli {
163162
#[clap(subcommand)]
164163
pub command: Command,
165164
}
166-
167-
/// Returns a string suitable to use as a directory.
168-
///
169-
/// Created from the spirv-builder source dep and the rustc channel.
170-
fn to_dirname(text: &str) -> String {
171-
text.replace(
172-
[std::path::MAIN_SEPARATOR, '\\', '/', '.', ':', '@', '='],
173-
"_",
174-
)
175-
.split(['{', '}', ' ', '\n', '"', '\''])
176-
.collect::<Vec<_>>()
177-
.concat()
178-
}

crates/cargo-gpu/src/show.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use std::{fs, path::Path};
44

55
use anyhow::bail;
6-
use rustc_codegen_spirv_cache::cache::cache_dir;
7-
8-
use crate::{
6+
use rustc_codegen_spirv_cache::{
7+
cache::cache_dir,
98
spirv_source::{query_metadata, SpirvSource},
10-
target_specs::update_target_specs_files,
119
};
1210

11+
use crate::target_specs::update_target_specs_files;
12+
1313
/// Show the computed source of the spirv-std dependency.
1414
#[derive(Clone, Debug, clap::Parser)]
1515
pub struct SpirvSourceDep {

crates/cargo-gpu/src/target_specs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use std::path::{Path, PathBuf};
2626

2727
use anyhow::Context as _;
2828
use cargo_metadata::Metadata;
29-
use rustc_codegen_spirv_cache::cache::cache_dir;
30-
31-
use crate::spirv_source::{FindPackage as _, SpirvSource};
29+
use rustc_codegen_spirv_cache::{
30+
cache::cache_dir,
31+
spirv_source::{FindPackage as _, SpirvSource},
32+
};
3233

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

crates/cargo-gpu/src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn copy_dir_all(
2020
}
2121
Ok(())
2222
}
23+
2324
pub fn shader_crate_template_path() -> std::path::PathBuf {
2425
let project_base = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2526
project_base.join("../shader-crate-template")

crates/rustc_codegen_spirv-cache/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ license.workspace = true
99

1010
[dependencies]
1111
thiserror.workspace = true
12+
anyhow.workspace = true
13+
log.workspace = true
1214
directories.workspace = true
15+
cargo_metadata.workspace = true
16+
17+
[dev-dependencies]
18+
test-log.workspace = true
19+
cargo-util-schemas.workspace = true
1320

1421
[lints]
1522
workspace = true

crates/rustc_codegen_spirv-cache/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
//! of `rustc_codegen_spirv` and their associated toolchains for you.
1313
1414
pub mod cache;
15+
pub mod spirv_source;

crates/cargo-gpu/src/spirv_source.rs renamed to crates/rustc_codegen_spirv-cache/src/spirv_source.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
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+
// TODO: remove this & fix documentation
8+
#![expect(clippy::missing_errors_doc, reason = "temporary allow")]
9+
710
use std::{
811
fs,
912
path::{Path, PathBuf},
@@ -15,11 +18,13 @@ use cargo_metadata::{
1518
semver::Version,
1619
Metadata, MetadataCommand, Package,
1720
};
18-
use rustc_codegen_spirv_cache::cache::cache_dir;
21+
22+
use crate::cache::cache_dir;
1923

2024
#[expect(
25+
rustdoc::bare_urls,
2126
clippy::doc_markdown,
22-
reason = "The URL should appear literally like this. But Clippy wants a markdown clickable link"
27+
reason = "The URL should appear literally like this. But Clippy & rustdoc want a markdown clickable link"
2328
)]
2429
/// The source and version of `rust-gpu`.
2530
/// Eg:
@@ -29,6 +34,7 @@ use rustc_codegen_spirv_cache::cache::cache_dir;
2934
/// - a revision of "abc213"
3035
/// * a local Path
3136
#[derive(Eq, PartialEq, Clone, Debug)]
37+
#[non_exhaustive]
3238
pub enum SpirvSource {
3339
/// If the shader specifies a simple version like `spirv-std = "0.9.0"` then the source of
3440
/// `rust-gpu` is the conventional crates.io version.
@@ -58,6 +64,7 @@ impl core::fmt::Display for SpirvSource {
5864
clippy::min_ident_chars,
5965
reason = "It's a core library trait implementation"
6066
)]
67+
#[inline]
6168
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6269
match self {
6370
Self::CratesIO(version) => version.fmt(f),
@@ -79,6 +86,7 @@ impl core::fmt::Display for SpirvSource {
7986

8087
impl SpirvSource {
8188
/// Figures out which source of `rust-gpu` to use
89+
#[inline]
8290
pub fn new(
8391
shader_crate_path: &Path,
8492
maybe_rust_gpu_source: Option<&str>,
@@ -105,6 +113,7 @@ impl SpirvSource {
105113
}
106114

107115
/// Look into the shader crate to get the version of `rust-gpu` it's using.
116+
#[inline]
108117
pub fn get_rust_gpu_deps_from_shader(shader_crate_path: &Path) -> anyhow::Result<Self> {
109118
let crate_metadata = query_metadata(shader_crate_path)?;
110119
let spirv_std_package = crate_metadata.find_package("spirv-std")?;
@@ -120,19 +129,25 @@ impl SpirvSource {
120129
/// Convert the `SpirvSource` to a cache directory in which we can build it.
121130
/// It needs to be dynamically created because an end-user might want to swap out the source,
122131
/// maybe using their own fork for example.
132+
#[inline]
123133
pub fn install_dir(&self) -> anyhow::Result<PathBuf> {
124134
match self {
125135
Self::Path {
126136
rust_gpu_repo_root, ..
127137
} => Ok(rust_gpu_repo_root.as_std_path().to_owned()),
128138
Self::CratesIO { .. } | Self::Git { .. } => {
129-
let dir = crate::to_dirname(self.to_string().as_ref());
139+
let dir = to_dirname(self.to_string().as_ref());
130140
Ok(cache_dir()?.join("codegen").join(dir))
131141
}
132142
}
133143
}
134144

135145
/// Returns true if self is a Path
146+
#[expect(
147+
clippy::must_use_candidate,
148+
reason = "calculations are cheap, `bool` is `Copy`"
149+
)]
150+
#[inline]
136151
pub const fn is_path(&self) -> bool {
137152
matches!(self, Self::Path { .. })
138153
}
@@ -191,7 +206,21 @@ impl SpirvSource {
191206
}
192207
}
193208

209+
/// Returns a string suitable to use as a directory.
210+
///
211+
/// Created from the spirv-builder source dep and the rustc channel.
212+
fn to_dirname(text: &str) -> String {
213+
text.replace(
214+
[std::path::MAIN_SEPARATOR, '\\', '/', '.', ':', '@', '='],
215+
"_",
216+
)
217+
.split(['{', '}', ' ', '\n', '"', '\''])
218+
.collect::<Vec<_>>()
219+
.concat()
220+
}
221+
194222
/// get the Package metadata from some crate
223+
#[inline]
195224
pub fn query_metadata(crate_path: &Path) -> anyhow::Result<Metadata> {
196225
log::debug!("Running `cargo metadata` on `{}`", crate_path.display());
197226
let metadata = MetadataCommand::new()
@@ -211,6 +240,7 @@ pub trait FindPackage {
211240
}
212241

213242
impl FindPackage for Metadata {
243+
#[inline]
214244
fn find_package(&self, crate_name: &str) -> anyhow::Result<&Package> {
215245
if let Some(package) = self
216246
.packages
@@ -229,6 +259,7 @@ impl FindPackage for Metadata {
229259
}
230260

231261
/// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
262+
#[inline]
232263
pub fn get_channel_from_rustc_codegen_spirv_build_script(
233264
rustc_codegen_spirv_package: &Package,
234265
) -> anyhow::Result<String> {
@@ -257,9 +288,14 @@ mod test {
257288
use cargo_metadata::{PackageBuilder, PackageId, Source};
258289
use cargo_util_schemas::manifest::PackageName;
259290

291+
pub fn shader_crate_template_path() -> std::path::PathBuf {
292+
let project_base = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
293+
project_base.join("../shader-crate-template")
294+
}
295+
260296
#[test_log::test]
261297
fn parsing_spirv_std_dep_for_shader_template() {
262-
let shader_template_path = crate::test::shader_crate_template_path();
298+
let shader_template_path = shader_crate_template_path();
263299
let source = SpirvSource::get_rust_gpu_deps_from_shader(&shader_template_path).unwrap();
264300
assert_eq!(
265301
source,
@@ -278,7 +314,7 @@ mod test {
278314

279315
#[test_log::test]
280316
fn cached_checkout_dir_sanity() {
281-
let shader_template_path = crate::test::shader_crate_template_path();
317+
let shader_template_path = shader_crate_template_path();
282318
let source = SpirvSource::get_rust_gpu_deps_from_shader(&shader_template_path).unwrap();
283319
let dir = source.install_dir().unwrap();
284320
let name = dir

0 commit comments

Comments
 (0)