Skip to content

Commit 592022f

Browse files
committed
Refactor config & metadata modules of cargo-gpu
1 parent f8850ce commit 592022f

File tree

11 files changed

+453
-330
lines changed

11 files changed

+453
-330
lines changed

Cargo.lock

Lines changed: 0 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
@@ -26,7 +26,6 @@ dunce.workspace = true
2626
[dev-dependencies]
2727
tempfile.workspace = true
2828
test-log.workspace = true
29-
cargo_metadata = { workspace = true, features = ["builder"] }
3029

3130
[lints]
3231
workspace = true

crates/cargo-gpu/src/build.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
//! `cargo gpu build`, analogous to `cargo build`
22
33
use core::convert::Infallible;
4-
use std::{io::Write as _, panic, path::PathBuf};
4+
use std::{
5+
io::Write as _,
6+
panic,
7+
path::{Path, PathBuf},
8+
thread,
9+
};
510

611
use anyhow::Context as _;
712
use cargo_gpu_build::{
813
build::{CargoGpuBuilder, CargoGpuBuilderParams},
914
spirv_builder::{CompileResult, ModuleResult, SpirvBuilder},
1015
};
1116

12-
use crate::{install::Install, linkage::Linkage, user_consent::ask_for_user_consent};
17+
use crate::{
18+
install::InstallArgs,
19+
linkage::Linkage,
20+
metadata::{CargoMetadata, CargoMetadataSource},
21+
user_consent::ask_for_user_consent,
22+
};
1323

1424
/// Args for just a build.
1525
#[derive(Debug, Clone, clap::Parser, serde::Deserialize, serde::Serialize)]
@@ -47,12 +57,12 @@ impl Default for BuildArgs {
4757
}
4858

4959
/// `cargo build` subcommands.
50-
#[derive(Clone, Debug, clap::Parser, serde::Deserialize, serde::Serialize)]
60+
#[derive(Clone, Debug, Default, clap::Parser, serde::Deserialize, serde::Serialize)]
5161
#[non_exhaustive]
5262
pub struct Build {
5363
/// CLI args for install the `rust-gpu` compiler and components.
5464
#[clap(flatten)]
55-
pub install: Install,
65+
pub install: InstallArgs,
5666

5767
/// CLI args for configuring the build of the shader.
5868
#[clap(flatten)]
@@ -107,7 +117,7 @@ impl Build {
107117
fn watch(&self, mut crate_builder: CargoGpuBuilder) -> anyhow::Result<Infallible> {
108118
let this = self.clone();
109119
let mut watcher = crate_builder.watch()?;
110-
let watch_thread = std::thread::spawn(move || -> ! {
120+
let watch_thread = thread::spawn(move || -> ! {
111121
loop {
112122
let compile_result = match watcher.recv() {
113123
Ok(compile_result) => compile_result,
@@ -191,26 +201,51 @@ impl Build {
191201
}
192202
}
193203

204+
impl CargoMetadata for Build {
205+
fn patch(&mut self, shader_crate: &Path, source: CargoMetadataSource<'_>) {
206+
let CargoMetadataSource::Crate(_) = source else {
207+
return;
208+
};
209+
210+
let output_dir = self.build.output_dir.as_path();
211+
log::debug!(
212+
"found output dir path in crate metadata: {}",
213+
output_dir.display()
214+
);
215+
216+
let new_output_dir = shader_crate.join(output_dir);
217+
log::debug!(
218+
"setting that to be relative to the Cargo.toml it was found in: {}",
219+
new_output_dir.display()
220+
);
221+
222+
self.build.output_dir = new_output_dir;
223+
}
224+
}
225+
194226
#[cfg(test)]
195227
mod test {
196228
use clap::Parser as _;
197229

198-
use crate::{Cli, Command};
230+
use crate::{
231+
test::{shader_crate_template_path, tests_teardown},
232+
Cli, Command,
233+
};
199234

200235
#[test_log::test]
201236
fn builder_from_params() {
202-
crate::test::tests_teardown();
237+
tests_teardown();
203238

204-
let shader_crate_path = crate::test::shader_crate_template_path();
239+
let shader_crate_path = shader_crate_template_path();
205240
let output_dir = shader_crate_path.join("shaders");
206241

207242
let args = [
208-
"target/debug/cargo-gpu",
209-
"build",
210-
"--shader-crate",
211-
&format!("{}", shader_crate_path.display()),
212-
"--output-dir",
213-
&format!("{}", output_dir.display()),
243+
"target/debug/cargo-gpu".as_ref(),
244+
"build".as_ref(),
245+
"--shader-crate".as_ref(),
246+
shader_crate_path.as_os_str(),
247+
"--output-dir".as_ref(),
248+
output_dir.as_os_str(),
214249
];
215250
if let Cli {
216251
command: Command::Build(build),

0 commit comments

Comments
 (0)