|
1 | 1 | //! `cargo gpu build`, analogous to `cargo build` |
2 | 2 |
|
3 | 3 | 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 | +}; |
5 | 9 |
|
6 | 10 | use anyhow::Context as _; |
7 | 11 | use cargo_gpu_build::{ |
8 | 12 | build::{CargoGpuBuilder, CargoGpuBuilderParams}, |
9 | 13 | spirv_builder::{CompileResult, ModuleResult, SpirvBuilder}, |
10 | 14 | }; |
11 | 15 |
|
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 | +}; |
13 | 22 |
|
14 | 23 | /// Args for just a build. |
15 | 24 | #[derive(Debug, Clone, clap::Parser, serde::Deserialize, serde::Serialize)] |
@@ -47,12 +56,12 @@ impl Default for BuildArgs { |
47 | 56 | } |
48 | 57 |
|
49 | 58 | /// `cargo build` subcommands. |
50 | | -#[derive(Clone, Debug, clap::Parser, serde::Deserialize, serde::Serialize)] |
| 59 | +#[derive(Clone, Debug, Default, clap::Parser, serde::Deserialize, serde::Serialize)] |
51 | 60 | #[non_exhaustive] |
52 | 61 | pub struct Build { |
53 | 62 | /// CLI args for install the `rust-gpu` compiler and components. |
54 | 63 | #[clap(flatten)] |
55 | | - pub install: Install, |
| 64 | + pub install: InstallArgs, |
56 | 65 |
|
57 | 66 | /// CLI args for configuring the build of the shader. |
58 | 67 | #[clap(flatten)] |
@@ -191,26 +200,51 @@ impl Build { |
191 | 200 | } |
192 | 201 | } |
193 | 202 |
|
| 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 | + |
194 | 225 | #[cfg(test)] |
195 | 226 | mod test { |
196 | 227 | use clap::Parser as _; |
197 | 228 |
|
198 | | - use crate::{Cli, Command}; |
| 229 | + use crate::{ |
| 230 | + test::{shader_crate_template_path, tests_teardown}, |
| 231 | + Cli, Command, |
| 232 | + }; |
199 | 233 |
|
200 | 234 | #[test_log::test] |
201 | 235 | fn builder_from_params() { |
202 | | - crate::test::tests_teardown(); |
| 236 | + tests_teardown(); |
203 | 237 |
|
204 | | - let shader_crate_path = crate::test::shader_crate_template_path(); |
| 238 | + let shader_crate_path = shader_crate_template_path(); |
205 | 239 | let output_dir = shader_crate_path.join("shaders"); |
206 | 240 |
|
207 | 241 | 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(), |
214 | 248 | ]; |
215 | 249 | if let Cli { |
216 | 250 | command: Command::Build(build), |
|
0 commit comments