Skip to content

Commit cdf0eb2

Browse files
committed
Rewrite watch handling
1 parent dc35924 commit cdf0eb2

File tree

6 files changed

+44
-34
lines changed

6 files changed

+44
-34
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ license = "MIT OR Apache-2.0"
2626
cargo-gpu-build = { path = "crates/cargo-gpu-build", default-features = false }
2727
cargo-gpu-test-utils = { path = "crates/cargo-gpu-test-utils", default-features = false }
2828
rustc_codegen_spirv-cache = { path = "crates/rustc_codegen_spirv-cache", default-features = false }
29-
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "3a897aa96c83b7caede692cbf8da7e58d7014ec8", default-features = false }
29+
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "ad2a34abb0576e1cf032237d191e8e63de1c3cea", default-features = false }
3030
anyhow = "1.0.98"
3131
thiserror = "2.0.12"
3232
clap = { version = "4.5.41", features = ["derive"] }

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
//! Then is uses [`spirv-builder`](cache::spirv_builder) crate
1414
//! to pass the many additional parameters required to configure rustc and our codegen backend,
1515
//! but provide you with a toolchain-agnostic version that you may use from stable rustc.
16-
//!
17-
//! ## Building shader crates
18-
//!
19-
//! `cargo-gpu` takes a path to a shader crate to build, as well as a path to a directory
20-
//! to put the compiled `spv` source files. It also takes a path to an output manifest
21-
//! file where all shader entry points will be mapped to their `spv` source files. This
22-
//! manifest file can be used by build scripts (`build.rs` files) to generate linkage or
23-
//! conduct other post-processing, like converting the `spv` files into `wgsl` files,
24-
//! for example.
2516
2617
pub use rustc_codegen_spirv_cache as cache;
2718

crates/cargo-gpu/src/build.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
#![allow(clippy::shadow_reuse, reason = "let's not be silly")]
44
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
55

6-
use std::io::{self, Write as _};
7-
use std::path::PathBuf;
6+
use core::convert::Infallible;
7+
use std::{
8+
io::{self, Write as _},
9+
panic,
10+
path::PathBuf,
11+
};
812

913
use anyhow::Context as _;
1014
use cargo_gpu_build::lockfile::LockfileMismatchHandler;
1115
use rustc_codegen_spirv_cache::install::Install;
12-
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
16+
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder, Watch};
1317

1418
use crate::{linkage::Linkage, user_consent::ask_for_user_consent};
1519

@@ -103,7 +107,8 @@ impl Build {
103107
);
104108

105109
if self.build.watch {
106-
return self.watch();
110+
let never = self.watch()?;
111+
match never {}
107112
}
108113
self.build()
109114
}
@@ -119,21 +124,27 @@ impl Build {
119124
Ok(())
120125
}
121126

122-
/// Watches shader crate for changes using [`SpirvBuilder`]
123-
/// or returns an error depending on presence of `watch` feature.
124-
fn watch(&self) -> anyhow::Result<()> {
127+
/// Watches shader crate for changes using [`SpirvBuilder`].
128+
fn watch(&self) -> anyhow::Result<Infallible> {
125129
let this = self.clone();
126-
self.build
127-
.spirv_builder
128-
.watch(move |result, accept| {
129-
let parse_result = this.parse_compilation_result(&result);
130-
if let Some(accept) = accept {
131-
accept.submit(parse_result);
132-
}
133-
})?
130+
let Watch {
131+
first_compile,
132+
watch_thread,
133+
..
134+
} = self.build.spirv_builder.watch(move |result, accept| {
135+
let parse_result = this.parse_compilation_result(&result);
136+
if let Some(accept) = accept {
137+
accept.submit(parse_result);
138+
}
139+
})?;
140+
141+
first_compile
134142
.context("should always return the first compile result")
135143
.flatten()?;
136-
anyhow::bail!("unexpected end of watch")
144+
watch_thread.join().map_err(|payload| {
145+
log::error!("watch thread panicked");
146+
panic::resume_unwind(payload)
147+
})
137148
}
138149

139150
/// Parses compilation result from [`SpirvBuilder`] and writes it out to a file

crates/cargo-gpu/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33
//! This program allows you to easily compile your rust-gpu shaders,
44
//! without requiring you to fix your entire project to a specific toolchain.
55
//!
6-
//! For additional information see the [`cargo-gpu-build`](cargo_gpu_build) crate documentation.
6+
//! ## Building shader crates
7+
//!
8+
//! It takes a path to a shader crate to build, as well as a path to a directory to put
9+
//! the compiled `spv` source files. It also takes a path to an output manifest file
10+
//! where all shader entry points will be mapped to their `spv` source files.
11+
//! This manifest file can be used by build scripts (`build.rs` files) to generate linkage
12+
//! or conduct other post-processing, like converting the `spv` files into `wgsl` files, for example.
13+
//!
14+
//! For additional information, see the [`cargo-gpu-build`](cargo_gpu_build) crate documentation.
15+
//!
16+
//! ## Where the binaries are
17+
//!
18+
//! Prebuilt binaries are stored in the [cache directory](rustc_codegen_spirv_cache::cache::cache_dir()),
19+
//! which path differs by OS you are using.
720
821
#![expect(clippy::pub_use, reason = "part of public interface")]
922

crates/rustc_codegen_spirv-cache/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
//! Usually, this would require you to fix your entire project to that specific
1111
//! toolchain, but this project loosens that requirement by managing installations
1212
//! of `rustc_codegen_spirv` and their associated toolchains for you.
13-
//!
14-
//! ## Where the binaries are
15-
//!
16-
//! Prebuilt binaries are stored in the [cache directory](crate::cache::cache_dir()),
17-
//! which path differs by OS.
1813
1914
#![expect(clippy::pub_use, reason = "reexport for convenience")]
2015

0 commit comments

Comments
 (0)