Skip to content

Commit 24b50f4

Browse files
committed
Preparing to move config & metadata modules into cargo-gpu-build
1 parent 78530d0 commit 24b50f4

File tree

8 files changed

+183
-117
lines changed

8 files changed

+183
-117
lines changed

Cargo.lock

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

crates/cargo-gpu-build/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ rustc_codegen_spirv-cache = { path = "../rustc_codegen_spirv-cache" }
1212
dunce.workspace = true
1313
thiserror.workspace = true
1414
log.workspace = true
15+
serde.workspace = true
16+
clap = { workspace = true, optional = true }
1517

1618
[features]
1719
# Rebuilds target shader crate upon changes
1820
watch = ["rustc_codegen_spirv-cache/watch"]
1921
# Enables `clap` support for public structs
20-
clap = ["rustc_codegen_spirv-cache/clap"]
22+
clap = ["dep:clap", "rustc_codegen_spirv-cache/clap"]
2123

2224
[lints]
2325
workspace = true

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

Lines changed: 95 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,33 @@ use crate::{
2323
#[cfg(feature = "watch")]
2424
use crate::spirv_builder::SpirvWatcher;
2525

26-
/// Parameters for [`CargoGpuBuilder::new()`].
27-
#[derive(Debug, Clone)]
26+
/// Metadata specific to the build process.
27+
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
28+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
2829
#[non_exhaustive]
29-
pub struct CargoGpuBuilderParams<W, T, C, O, E> {
30-
/// Parameters of the shader crate build.
31-
pub build: SpirvBuilder,
32-
/// Parameters of the codegen backend installation for the shader crate.
33-
pub install: SpirvCodegenBackendInstaller,
30+
pub struct CargoGpuBuildMetadata {
31+
/// The flattened [`SpirvBuilder`].
32+
#[cfg_attr(feature = "clap", clap(flatten))]
33+
#[serde(flatten)]
34+
pub spirv_builder: SpirvBuilder,
35+
}
36+
37+
impl From<SpirvBuilder> for CargoGpuBuildMetadata {
38+
#[inline]
39+
fn from(spirv_builder: SpirvBuilder) -> Self {
40+
Self { spirv_builder }
41+
}
42+
}
43+
44+
/// Metadata specific to the codegen backend installation process.
45+
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
46+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
47+
#[non_exhaustive]
48+
pub struct CargoGpuInstallMetadata {
49+
/// The flattened [`SpirvCodegenBackendInstaller`].
50+
#[cfg_attr(feature = "clap", clap(flatten))]
51+
#[serde(flatten)]
52+
pub spirv_installer: SpirvCodegenBackendInstaller,
3453
/// There is a tricky situation where a shader crate that depends on workspace config can have
3554
/// a different `Cargo.lock` lockfile version from the the workspace's `Cargo.lock`. This can
3655
/// prevent builds when an old Rust toolchain doesn't recognise the newer lockfile version.
@@ -52,7 +71,29 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
5271
/// way source URLs are encoded. See these PRs for more details:
5372
/// * <https://github.com/rust-lang/cargo/pull/12280>
5473
/// * <https://github.com/rust-lang/cargo/pull/14595>
74+
#[cfg_attr(feature = "clap", clap(long, action, verbatim_doc_comment))]
5575
pub force_overwrite_lockfiles_v4_to_v3: bool,
76+
}
77+
78+
/// Metadata for both shader crate build and codegen backend installation.
79+
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
80+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
81+
#[non_exhaustive]
82+
pub struct CargoGpuMetadata {
83+
/// Parameters of the shader crate build.
84+
#[cfg_attr(feature = "clap", clap(flatten))]
85+
pub build: CargoGpuBuildMetadata,
86+
/// Parameters of the codegen backend installation for the shader crate.
87+
#[cfg_attr(feature = "clap", clap(flatten))]
88+
pub install: CargoGpuInstallMetadata,
89+
}
90+
91+
/// Parameters for [`CargoGpuBuilder::new()`].
92+
#[derive(Debug, Clone)]
93+
#[non_exhaustive]
94+
pub struct CargoGpuBuilderParams<W, T, C, O, E> {
95+
/// Parameters of the shader crate build & codegen backend installation.
96+
pub metadata: CargoGpuMetadata,
5697
/// Writer of user output.
5798
pub writer: W,
5899
/// Callbacks to halt toolchain installation.
@@ -62,41 +103,41 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
62103
}
63104

64105
impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
65-
/// Replaces build parameters of the shader crate.
106+
/// Replaces of the shader crate build & codegen backend installation.
66107
#[inline]
67108
#[must_use]
68-
pub fn build(self, build: SpirvBuilder) -> Self {
69-
Self { build, ..self }
109+
pub fn metadata(self, metadata: CargoGpuMetadata) -> Self {
110+
Self { metadata, ..self }
70111
}
71112

72-
/// Replaces codegen backend installation parameters of the shader crate.
113+
/// Replaces build parameters of the shader crate.
73114
#[inline]
74115
#[must_use]
75-
pub fn install(self, install: SpirvCodegenBackendInstaller) -> Self {
76-
Self { install, ..self }
116+
pub fn build(self, build: CargoGpuBuildMetadata) -> Self {
117+
let metadata = CargoGpuMetadata {
118+
build,
119+
..self.metadata
120+
};
121+
Self { metadata, ..self }
77122
}
78123

79-
/// Sets whether to force overwriting lockfiles from v4 to v3.
124+
/// Replaces codegen backend installation parameters of the shader crate.
80125
#[inline]
81126
#[must_use]
82-
pub fn force_overwrite_lockfiles_v4_to_v3(
83-
self,
84-
force_overwrite_lockfiles_v4_to_v3: bool,
85-
) -> Self {
86-
Self {
87-
force_overwrite_lockfiles_v4_to_v3,
88-
..self
89-
}
127+
pub fn install(self, install: CargoGpuInstallMetadata) -> Self {
128+
let metadata = CargoGpuMetadata {
129+
install,
130+
..self.metadata
131+
};
132+
Self { metadata, ..self }
90133
}
91134

92135
/// Replaces the writer of user output.
93136
#[inline]
94137
#[must_use]
95138
pub fn writer<NW>(self, writer: NW) -> CargoGpuBuilderParams<NW, T, C, O, E> {
96139
CargoGpuBuilderParams {
97-
build: self.build,
98-
install: self.install,
99-
force_overwrite_lockfiles_v4_to_v3: self.force_overwrite_lockfiles_v4_to_v3,
140+
metadata: self.metadata,
100141
writer,
101142
halt: self.halt,
102143
stdio_cfg: self.stdio_cfg,
@@ -111,9 +152,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
111152
halt: HaltToolchainInstallation<NT, NC>,
112153
) -> CargoGpuBuilderParams<W, NT, NC, O, E> {
113154
CargoGpuBuilderParams {
114-
build: self.build,
115-
install: self.install,
116-
force_overwrite_lockfiles_v4_to_v3: self.force_overwrite_lockfiles_v4_to_v3,
155+
metadata: self.metadata,
117156
writer: self.writer,
118157
halt,
119158
stdio_cfg: self.stdio_cfg,
@@ -128,9 +167,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
128167
stdio_cfg: StdioCfg<NO, NE>,
129168
) -> CargoGpuBuilderParams<W, T, C, NO, NE> {
130169
CargoGpuBuilderParams {
131-
build: self.build,
132-
install: self.install,
133-
force_overwrite_lockfiles_v4_to_v3: self.force_overwrite_lockfiles_v4_to_v3,
170+
metadata: self.metadata,
134171
writer: self.writer,
135172
halt: self.halt,
136173
stdio_cfg,
@@ -147,11 +184,18 @@ pub type DefaultCargoGpuBuilderParams = CargoGpuBuilderParams<
147184
InheritStderr,
148185
>;
149186

150-
impl From<SpirvBuilder> for DefaultCargoGpuBuilderParams {
187+
impl<T> From<T> for DefaultCargoGpuBuilderParams
188+
where
189+
T: Into<CargoGpuBuildMetadata>,
190+
{
151191
#[inline]
152-
fn from(build: SpirvBuilder) -> Self {
192+
fn from(value: T) -> Self {
193+
let metadata = CargoGpuMetadata {
194+
build: value.into(),
195+
install: CargoGpuInstallMetadata::default(),
196+
};
153197
Self {
154-
build,
198+
metadata,
155199
..Self::default()
156200
}
157201
}
@@ -161,9 +205,7 @@ impl Default for DefaultCargoGpuBuilderParams {
161205
#[inline]
162206
fn default() -> Self {
163207
Self {
164-
build: SpirvBuilder::default(),
165-
install: SpirvCodegenBackendInstaller::default(),
166-
force_overwrite_lockfiles_v4_to_v3: false,
208+
metadata: CargoGpuMetadata::default(),
167209
writer: io::stdout(),
168210
halt: HaltToolchainInstallation::noop(),
169211
stdio_cfg: StdioCfg::inherit(),
@@ -212,23 +254,29 @@ where
212254
E: FnMut() -> Stdio,
213255
{
214256
let CargoGpuBuilderParams {
215-
mut build,
216-
install,
217-
force_overwrite_lockfiles_v4_to_v3,
257+
metadata,
218258
mut writer,
219259
halt,
220260
mut stdio_cfg,
221261
} = params.into();
262+
let CargoGpuMetadata { build, install } = metadata;
263+
let CargoGpuBuildMetadata {
264+
spirv_builder: mut builder,
265+
} = build;
266+
let CargoGpuInstallMetadata {
267+
spirv_installer: installer,
268+
force_overwrite_lockfiles_v4_to_v3,
269+
} = install;
222270

223-
if build.target.is_none() {
271+
if builder.target.is_none() {
224272
return Err(NewCargoGpuBuilderError::MissingTarget);
225273
}
226-
let path_to_crate = build
274+
let path_to_crate = builder
227275
.path_to_crate
228276
.as_ref()
229277
.ok_or(NewCargoGpuBuilderError::MissingCratePath)?;
230278
let shader_crate = dunce::canonicalize(path_to_crate)?;
231-
build.path_to_crate = Some(shader_crate.clone());
279+
builder.path_to_crate = Some(shader_crate.clone());
232280

233281
let backend_install_params = SpirvCodegenBackendInstallParams::from(&shader_crate)
234282
.writer(&mut writer)
@@ -240,7 +288,7 @@ where
240288
stdout: || (stdio_cfg.stdout)(),
241289
stderr: || (stdio_cfg.stderr)(),
242290
});
243-
let codegen_backend = install.install(backend_install_params)?;
291+
let codegen_backend = installer.install(backend_install_params)?;
244292

245293
let lockfile_mismatch_handler = LockfileMismatchHandler::new(
246294
&shader_crate,
@@ -250,12 +298,12 @@ where
250298

251299
#[expect(clippy::unreachable, reason = "target was set")]
252300
codegen_backend
253-
.configure_spirv_builder(&mut build)
301+
.configure_spirv_builder(&mut builder)
254302
.unwrap_or_else(|_| unreachable!("target was set before calling this function"));
255303

256304
Ok(Self {
257-
builder: build,
258-
installer: install,
305+
builder,
306+
installer,
259307
codegen_backend,
260308
lockfile_mismatch_handler,
261309
writer,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl LockfileMismatchHandler {
6868
})
6969
}
7070

71-
/// See docs for [`force_overwrite_lockfiles_v4_to_v3`](field@crate::build::CargoGpuBuilderParams::force_overwrite_lockfiles_v4_to_v3)
71+
/// See docs for [`force_overwrite_lockfiles_v4_to_v3`](field@crate::build::CargoGpuInstallMetadata::force_overwrite_lockfiles_v4_to_v3)
7272
/// flag for why we do this.
7373
fn ensure_workspace_rust_version_does_not_conflict_with_shader(
7474
shader_crate_path: &Path,
@@ -96,7 +96,7 @@ impl LockfileMismatchHandler {
9696
}
9797
}
9898

99-
/// See docs for [`force_overwrite_lockfiles_v4_to_v3`](field@crate::build::CargoGpuBuilderParams::force_overwrite_lockfiles_v4_to_v3)
99+
/// See docs for [`force_overwrite_lockfiles_v4_to_v3`](field@crate::build::CargoGpuInstallMetadata::force_overwrite_lockfiles_v4_to_v3)
100100
/// flag for why we do this.
101101
fn ensure_shader_rust_version_does_not_conflict_with_any_cargo_locks(
102102
shader_crate_path: &Path,
@@ -334,7 +334,7 @@ pub enum LockfileMismatchError {
334334
/// Conflicting lockfile manifest versions detected, with advice on how to resolve them
335335
/// by setting the [`force_overwrite_lockfiles_v4_to_v3`] flag.
336336
///
337-
/// [`force_overwrite_lockfiles_v4_to_v3`]: field@crate::build::CargoGpuBuilderParams::force_overwrite_lockfiles_v4_to_v3
337+
/// [`force_overwrite_lockfiles_v4_to_v3`]: field@crate::build::CargoGpuInstallMetadata::force_overwrite_lockfiles_v4_to_v3
338338
#[error(
339339
r#"conflicting `Cargo.lock` versions detected ⚠️
340340

crates/cargo-gpu/src/build.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{
1010

1111
use anyhow::Context as _;
1212
use cargo_gpu_build::{
13-
build::{CargoGpuBuilder, CargoGpuBuilderParams},
14-
spirv_builder::{CompileResult, ModuleResult, SpirvBuilder},
13+
build::{CargoGpuBuildMetadata, CargoGpuBuilder, CargoGpuBuilderParams},
14+
spirv_builder::{CompileResult, ModuleResult},
1515
};
1616

1717
use crate::{
@@ -34,10 +34,10 @@ pub struct BuildArgs {
3434
#[clap(long, short, action)]
3535
pub watch: bool,
3636

37-
/// The flattened [`SpirvBuilder`].
37+
/// The flattened [`CargoGpuBuildMetadata`].
3838
#[clap(flatten)]
3939
#[serde(flatten)]
40-
pub spirv_builder: SpirvBuilder,
40+
pub build_meta: CargoGpuBuildMetadata,
4141

4242
/// Renames the manifest.json file to the given name.
4343
#[clap(long, short, default_value = "manifest.json")]
@@ -50,7 +50,7 @@ impl Default for BuildArgs {
5050
Self {
5151
output_dir: PathBuf::from("./"),
5252
watch: false,
53-
spirv_builder: SpirvBuilder::default(),
53+
build_meta: CargoGpuBuildMetadata::default(),
5454
manifest_file: String::from("manifest.json"),
5555
}
5656
}
@@ -77,29 +77,33 @@ impl Build {
7777
/// Returns an error if the build process fails somehow.
7878
#[inline]
7979
pub fn run(&mut self) -> anyhow::Result<()> {
80-
self.build.spirv_builder.path_to_crate = Some(self.install.shader_crate.clone());
80+
let Self { install, build } = self;
81+
let InstallArgs { install_meta, .. } = install;
82+
let BuildArgs { build_meta, .. } = build;
8183

82-
let halt = ask_for_user_consent(self.install.auto_install_rust_toolchain);
83-
let crate_builder_params = CargoGpuBuilderParams::from(self.build.spirv_builder.clone())
84-
.install(self.install.spirv_installer.clone())
85-
.force_overwrite_lockfiles_v4_to_v3(self.install.force_overwrite_lockfiles_v4_to_v3)
84+
build_meta.spirv_builder.path_to_crate = Some(install.shader_crate.clone());
85+
86+
let skip_consent = install.auto_install_rust_toolchain;
87+
let halt = ask_for_user_consent(skip_consent);
88+
let crate_builder_params = CargoGpuBuilderParams::from(build_meta.clone())
89+
.install(install_meta.clone())
8690
.halt(halt);
8791
let crate_builder = CargoGpuBuilder::new(crate_builder_params)?;
8892

89-
self.install.spirv_installer = crate_builder.installer.clone();
90-
self.build.spirv_builder = crate_builder.builder.clone();
93+
install_meta.spirv_installer = crate_builder.installer.clone();
94+
build_meta.spirv_builder = crate_builder.builder.clone();
9195

9296
// Ensure the shader output dir exists
9397
log::debug!(
9498
"ensuring output-dir '{}' exists",
95-
self.build.output_dir.display()
99+
build.output_dir.display()
96100
);
97-
std::fs::create_dir_all(&self.build.output_dir)?;
98-
let canonicalized = dunce::canonicalize(&self.build.output_dir)?;
101+
std::fs::create_dir_all(&build.output_dir)?;
102+
let canonicalized = dunce::canonicalize(&build.output_dir)?;
99103
log::debug!("canonicalized output dir: {}", canonicalized.display());
100-
self.build.output_dir = canonicalized;
104+
build.output_dir = canonicalized;
101105

102-
if self.build.watch {
106+
if build.watch {
103107
let never = self.watch(crate_builder)?;
104108
match never {}
105109
}

0 commit comments

Comments
 (0)