Skip to content

Commit 0d774ef

Browse files
committed
add SpirvSource.is_path() shortcut
1 parent 2d2f8cf commit 0d774ef

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

crates/cargo-gpu/src/install.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Install {
117117
/// Create the `rustc_codegen_spirv_dummy` crate that depends on `rustc_codegen_spirv`
118118
fn write_source_files(source: &SpirvSource, checkout: &Path) -> anyhow::Result<()> {
119119
// skip writing a dummy project if we use a local rust-gpu checkout
120-
if matches!(source, SpirvSource::Path { .. }) {
120+
if source.is_path() {
121121
return Ok(());
122122
}
123123
log::debug!(
@@ -141,10 +141,12 @@ impl Install {
141141
}
142142
SpirvSource::Git { url, rev } => format!("git = \"{url}\"\nrev = \"{rev}\""),
143143
SpirvSource::Path {
144-
rust_gpu_repo_root: rust_gpu_path,
144+
rust_gpu_repo_root,
145145
version,
146146
} => {
147-
let mut new_path = rust_gpu_path.to_owned();
147+
// this branch is currently unreachable, as we just build `rustc_codegen_spirv` directly,
148+
// since we don't need the `dummy` crate to make cargo download it for us
149+
let mut new_path = rust_gpu_repo_root.to_owned();
148150
new_path.push("crates/spirv-builder");
149151
format!("path = \"{new_path}\"\nversion = \"{version}\"")
150152
}
@@ -198,7 +200,6 @@ package = "rustc_codegen_spirv"
198200
self.spirv_builder_source.as_deref(),
199201
self.spirv_builder_version.as_deref(),
200202
)?;
201-
let source_is_path = matches!(source, SpirvSource::Path { .. });
202203
let install_dir = source.install_dir()?;
203204

204205
let dylib_filename = format!(
@@ -208,7 +209,7 @@ package = "rustc_codegen_spirv"
208209
);
209210

210211
let dest_dylib_path;
211-
if source_is_path {
212+
if source.is_path() {
212213
dest_dylib_path = install_dir
213214
.join("target")
214215
.join("release")
@@ -223,7 +224,8 @@ package = "rustc_codegen_spirv"
223224
}
224225
}
225226

226-
let skip_rebuild = !source_is_path && dest_dylib_path.is_file() && !self.rebuild_codegen;
227+
// if `source` is a path, always rebuild
228+
let skip_rebuild = !source.is_path() && dest_dylib_path.is_file() && !self.rebuild_codegen;
227229
if skip_rebuild {
228230
log::info!("...and so we are aborting the install step.");
229231
} else {
@@ -232,9 +234,9 @@ package = "rustc_codegen_spirv"
232234

233235
// TODO cache toolchain channel in a file?
234236
log::debug!("resolving toolchain version to use");
235-
let crate_metadata = query_metadata(&install_dir)
237+
let dummy_metadata = query_metadata(&install_dir)
236238
.context("resolving toolchain version: get `rustc_codegen_spirv_dummy` metadata")?;
237-
let rustc_codegen_spirv = crate_metadata.find_package("rustc_codegen_spirv").context(
239+
let rustc_codegen_spirv = dummy_metadata.find_package("rustc_codegen_spirv").context(
238240
"resolving toolchain version: expected a dependency on `rustc_codegen_spirv`",
239241
)?;
240242
let toolchain_channel =
@@ -252,7 +254,7 @@ package = "rustc_codegen_spirv"
252254
.context("ensuring toolchain and components exist")?;
253255

254256
// to prevent unsupported version errors when using older toolchains
255-
if !source_is_path {
257+
if !source.is_path() {
256258
log::debug!("remove Cargo.lock");
257259
std::fs::remove_file(install_dir.join("Cargo.lock"))
258260
.context("remove Cargo.lock")?;
@@ -265,7 +267,7 @@ package = "rustc_codegen_spirv"
265267
.arg(format!("+{toolchain_channel}"))
266268
.args(["build", "--release"])
267269
.env_remove("RUSTC");
268-
if source_is_path {
270+
if source.is_path() {
269271
build_command.args(["-p", "rustc_codegen_spirv", "--lib"]);
270272
}
271273

@@ -289,7 +291,7 @@ package = "rustc_codegen_spirv"
289291
let dylib_path = target.join("release").join(&dylib_filename);
290292
if dylib_path.is_file() {
291293
log::info!("successfully built {}", dylib_path.display());
292-
if !source_is_path {
294+
if !source.is_path() {
293295
std::fs::rename(&dylib_path, &dest_dylib_path)
294296
.context("renaming dylib path")?;
295297

crates/cargo-gpu/src/spirv_source.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ impl SpirvSource {
122122
}
123123
}
124124

125+
/// Returns true if self is a Path
126+
pub const fn is_path(&self) -> bool {
127+
matches!(self, Self::Path { .. })
128+
}
129+
125130
/// Parse a string like:
126131
/// `spirv-std v0.9.0 (https://github.com/Rust-GPU/rust-gpu?rev=54f6978c#54f6978c) (*)`
127132
/// Which would return:

0 commit comments

Comments
 (0)