Skip to content

Commit 5840b2b

Browse files
committed
Add watch feature for cargo-gpu library
1 parent 8834ea5 commit 5840b2b

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

crates/cargo-gpu-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ name = "cargo-gpu"
1414
path = "src/main.rs"
1515

1616
[dependencies]
17-
cargo-gpu = { path = "../cargo-gpu", features = ["clap"] }
17+
cargo-gpu = { path = "../cargo-gpu", features = ["watch", "clap"] }
1818
clap.workspace = true
1919
log.workspace = true
2020
env_logger.workspace = true

crates/cargo-gpu/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license.workspace = true
1111
[dependencies]
1212
cargo_metadata.workspace = true
1313
anyhow.workspace = true
14-
spirv-builder = { workspace = true, features = ["watch"] }
14+
spirv-builder.workspace = true
1515
legacy_target_specs.workspace = true
1616
clap = { workspace = true, optional = true }
1717
directories.workspace = true
@@ -28,6 +28,7 @@ cargo_metadata = { workspace = true, features = ["builder"] }
2828
cargo-util-schemas.workspace = true
2929

3030
[features]
31+
watch = ["spirv-builder/watch"]
3132
clap = ["dep:clap", "spirv-builder/clap"]
3233

3334
[lints]

crates/cargo-gpu/src/build.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
33
//! `cargo gpu build`, analogous to `cargo build`
44
5-
use crate::install::Install;
6-
use crate::linkage::Linkage;
7-
use crate::lockfile::LockfileMismatchHandler;
5+
use spirv_builder::SpirvBuilder;
6+
use std::path::PathBuf;
7+
8+
#[cfg(feature = "watch")]
89
use anyhow::Context as _;
9-
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
10+
#[cfg(feature = "watch")]
11+
use spirv_builder::{CompileResult, ModuleResult};
12+
#[cfg(feature = "watch")]
1013
use std::io::Write as _;
11-
use std::path::PathBuf;
14+
15+
use crate::install::Install;
16+
use crate::lockfile::LockfileMismatchHandler;
17+
18+
#[cfg(feature = "watch")]
19+
use crate::linkage::Linkage;
1220

1321
/// Args for just a build
1422
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
@@ -19,6 +27,7 @@ pub struct BuildArgs {
1927
pub output_dir: PathBuf,
2028

2129
/// Watch the shader crate directory and automatically recompile on changes.
30+
#[cfg(feature = "watch")]
2231
#[cfg_attr(feature = "clap", clap(long, short, action))]
2332
pub watch: bool,
2433

@@ -37,6 +46,7 @@ impl Default for BuildArgs {
3746
fn default() -> Self {
3847
Self {
3948
output_dir: PathBuf::from("./"),
49+
#[cfg(feature = "watch")]
4050
watch: false,
4151
spirv_builder: SpirvBuilder::default(),
4252
manifest_file: String::from("manifest.json"),
@@ -91,6 +101,7 @@ impl Build {
91101
std::env::current_dir()?.display()
92102
);
93103

104+
#[cfg(feature = "watch")]
94105
if self.build.watch {
95106
let this = self.clone();
96107
self.build
@@ -111,10 +122,12 @@ impl Build {
111122
let result = self.build.spirv_builder.build()?;
112123
self.parse_compilation_result(&result)?;
113124
}
125+
114126
Ok(())
115127
}
116128

117129
/// Parses compilation result from `SpirvBuilder` and writes it out to a file
130+
#[cfg(feature = "watch")]
118131
fn parse_compilation_result(&self, result: &CompileResult) -> anyhow::Result<()> {
119132
let shaders = match &result.module {
120133
ModuleResult::MultiModule(modules) => {

crates/cargo-gpu/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ impl Command {
144144
config::Config::clap_command_with_cargo_config(shader_crate_path, env_args)?;
145145
log::debug!("building with final merged arguments: {command:#?}");
146146

147-
// When watching, do one normal run to setup the `manifest.json` file.
147+
// When watching, do one normal run to setup the `manifest.json` file.
148+
#[cfg(feature = "watch")]
148149
if command.build.watch {
149150
command.build.watch = false;
150151
command.run()?;
151152
command.build.watch = true;
152153
}
154+
153155
command.run()?;
154156
}
155157
Self::Show(show) => show.run()?,

crates/cargo-gpu/src/linkage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Mainly for the Linkage struct, which is written to a json file.
22
3+
#![cfg(feature = "watch")]
4+
35
/// Shader source and entry point that can be used to create shader linkage.
46
#[derive(serde::Serialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
57
pub struct Linkage {

0 commit comments

Comments
 (0)