Skip to content

Commit 214c3a2

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

File tree

11 files changed

+448
-326
lines changed

11 files changed

+448
-326
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: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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+
};
59

610
use anyhow::Context as _;
711
use cargo_gpu_build::{
812
build::{CargoGpuBuilder, CargoGpuBuilderParams},
913
spirv_builder::{CompileResult, ModuleResult, SpirvBuilder},
1014
};
1115

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

1423
/// Args for just a build.
1524
#[derive(Debug, Clone, clap::Parser, serde::Deserialize, serde::Serialize)]
@@ -47,12 +56,12 @@ impl Default for BuildArgs {
4756
}
4857

4958
/// `cargo build` subcommands.
50-
#[derive(Clone, Debug, clap::Parser, serde::Deserialize, serde::Serialize)]
59+
#[derive(Clone, Debug, Default, clap::Parser, serde::Deserialize, serde::Serialize)]
5160
#[non_exhaustive]
5261
pub struct Build {
5362
/// CLI args for install the `rust-gpu` compiler and components.
5463
#[clap(flatten)]
55-
pub install: Install,
64+
pub install: InstallArgs,
5665

5766
/// CLI args for configuring the build of the shader.
5867
#[clap(flatten)]
@@ -191,26 +200,51 @@ impl Build {
191200
}
192201
}
193202

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

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

200234
#[test_log::test]
201235
fn builder_from_params() {
202-
crate::test::tests_teardown();
236+
tests_teardown();
203237

204-
let shader_crate_path = crate::test::shader_crate_template_path();
238+
let shader_crate_path = shader_crate_template_path();
205239
let output_dir = shader_crate_path.join("shaders");
206240

207241
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()),
242+
"target/debug/cargo-gpu".as_ref(),
243+
"build".as_ref(),
244+
"--shader-crate".as_ref(),
245+
shader_crate_path.as_os_str(),
246+
"--output-dir".as_ref(),
247+
output_dir.as_os_str(),
214248
];
215249
if let Cli {
216250
command: Command::Build(build),

0 commit comments

Comments
 (0)